11package org .joychou .controller ;
22
33
4- import org .springframework . stereotype . Controller ;
5- import org .springframework . web . bind . annotation . RequestMapping ;
6- import org .springframework .web .bind .annotation .ResponseBody ;
4+ import org .slf4j . Logger ;
5+ import org .slf4j . LoggerFactory ;
6+ import org .springframework .web .bind .annotation .* ;
77
8- import javax .servlet .http .HttpServletRequest ;
98import java .net .URI ;
109import java .net .URL ;
1110import java .util .ArrayList ;
2019 * @version 2018.08.23
2120 */
2221
23- @ Controller
22+ @ RestController
2423@ RequestMapping ("/url" )
2524public class URLWhiteList {
2625
2726
2827 private String domainwhitelist [] = {"joychou.org" , "joychou.com" };
29-
28+ private static final Logger logger = LoggerFactory . getLogger ( URLWhiteList . class );
3029 /**
3130 * bypass poc: bypassjoychou.org
32- * http://localhost:8080/url/endswith?url=http://aaajoychou.org
31+ * http://localhost:8080/url/vuln/ endswith?url=http://aaajoychou.org
3332 *
3433 */
35- @ RequestMapping ("/endswith" )
36- @ ResponseBody
37- public String endsWith (HttpServletRequest request ) throws Exception {
38- String url = request .getParameter ("url" );
34+ @ GetMapping ("/vuln/endsWith" )
35+ public String endsWith (@ RequestParam ("url" ) String url ) throws Exception {
3936 URL u = new URL (url );
4037 String host = u .getHost ().toLowerCase ();
4138
@@ -47,15 +44,15 @@ public String endsWith(HttpServletRequest request) throws Exception{
4744 return "Bad url." ;
4845 }
4946
47+
5048 /**
5149 * bypass poc: joychou.org.bypass.com or bypassjoychou.org.
52- * http://localhost:8080/url/contains?url=http://joychou.org.bypass.com http://bypassjoychou.org
50+ * http://localhost:8080/url/vuln/contains?url=http://joychou.org.bypass.com
51+ * http://localhost:8080/url/vuln/contains?url=http://bypassjoychou.org
5352 *
5453 */
55- @ RequestMapping ("/contains" )
56- @ ResponseBody
57- public String contains (HttpServletRequest request ) throws Exception {
58- String url = request .getParameter ("url" );
54+ @ GetMapping ("/vuln/contains" )
55+ public String contains (@ RequestParam ("url" ) String url ) throws Exception {
5956 URL u = new URL (url );
6057 String host = u .getHost ().toLowerCase ();
6158
@@ -70,13 +67,11 @@ public String contains(HttpServletRequest request) throws Exception{
7067
7168 /**
7269 * bypass poc: bypassjoychou.org. It's the same with endsWith.
73- * http://localhost:8080/url/regex?url=http://aaajoychou.org
70+ * http://localhost:8080/url/vuln/ regex?url=http://aaajoychou.org
7471 *
7572 */
76- @ RequestMapping ("/regex" )
77- @ ResponseBody
78- public String regex (HttpServletRequest request ) throws Exception {
79- String url = request .getParameter ("url" );
73+ @ GetMapping ("/vuln/regex" )
74+ public String regex (@ RequestParam ("url" ) String url ) throws Exception {
8075 URL u = new URL (url );
8176 String host = u .getHost ().toLowerCase ();
8277
@@ -92,15 +87,14 @@ public String regex(HttpServletRequest request) throws Exception{
9287
9388 /**
9489 * bypass poc: joychou.org.bypass.com or bypassjoychou.org. It's the same with contains.
95- * http://localhost:8080/url/indexof ?url=http://joychou.org.bypass.com http://bypassjoychou.org
90+ * http://localhost:8080/url/vuln/indexOf ?url=http://joychou.org.bypass.com http://bypassjoychou.org
9691 *
9792 */
98- @ RequestMapping ("/indexof" )
99- @ ResponseBody
100- public String indexOf (HttpServletRequest request ) throws Exception {
101- String url = request .getParameter ("url" );
93+ @ GetMapping ("/vuln/indexOf" )
94+ public String indexOf (@ RequestParam ("url" ) String url ) throws Exception {
10295 URL u = new URL (url );
10396 String host = u .getHost ();
97+
10498 // If indexOf returns -1, it means that no string was found.
10599 for (String domain : domainwhitelist ){
106100 if (host .indexOf (domain ) != -1 ) {
@@ -113,24 +107,22 @@ public String indexOf(HttpServletRequest request) throws Exception{
113107 /**
114108 * The bypass of using java.net.URL to getHost.
115109 *
116- * Bypass poc1: curl -v 'http://localhost:8080/url/url_bypass?url=http://evel.com%[email protected] /a.html' 117- * Bypass poc2: curl -v 'http://localhost:8080/url/url_bypass?url=http://evil.com%5cwww.joychou.org/a.html'
110+ * Bypass poc1: curl -v 'http://localhost:8080/url/vuln/ url_bypass?url=http://evel.com%[email protected] /a.html' 111+ * Bypass poc2: curl -v 'http://localhost:8080/url/vuln/ url_bypass?url=http://evil.com%5cwww.joychou.org/a.html'
118112 *
119113 * Detail: https://github.com/JoyChou93/java-sec-code/wiki/URL-whtielist-Bypass
120114 */
121- @ RequestMapping ("/url_bypass" )
122- @ ResponseBody
123- public String url_bypass (HttpServletRequest request ) throws Exception {
124- String url = request .getParameter ("url" );
125- System .out .println ("url: " + url );
115+ @ GetMapping ("/vuln/url_bypass" )
116+ public String url_bypass (@ RequestParam ("url" ) String url ) throws Exception {
117+ logger .info ("url: " + url );
126118 URL u = new URL (url );
127119
128120 if (!u .getProtocol ().startsWith ("http" ) && !u .getProtocol ().startsWith ("https" )) {
129121 return "Url is not http or https" ;
130122 }
131123
132124 String host = u .getHost ().toLowerCase ();
133- System . out . println ("host: " + host );
125+ logger . info ("host: " + host );
134126
135127 // endsWith .
136128 for (String domain : domainwhitelist ){
@@ -145,18 +137,16 @@ public String url_bypass(HttpServletRequest request) throws Exception{
145137
146138
147139 /**
148- * First-level host whitelist.
149- * http://localhost:8080/url/seccode1 ?url=http://aa.taobao.com
140+ * 一级域名白名单 First-level host whitelist.
141+ * http://localhost:8080/url/sec/endswith ?url=http://aa.joychou.org
150142 *
151143 */
152- @ RequestMapping ("/seccode1" )
153- @ ResponseBody
154- public String seccode1 (HttpServletRequest request ) throws Exception {
144+ @ GetMapping ("/sec/endswith" )
145+ public String sec_endswith (@ RequestParam ("url" ) String url ) throws Exception {
155146
156- String whiteDomainlists [] = {"taobao.com" , "tmall.com" };
157- String url = request .getParameter ("url" );
147+ String whiteDomainlists [] = {"joychou.org" , "joychou.com" };
158148
159- URI uri = new URI (url );
149+ URI uri = new URI (url ); // 必须用URI
160150 if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
161151 return "SecurityUtil is not http or https" ;
162152 }
@@ -174,15 +164,13 @@ public String seccode1(HttpServletRequest request) throws Exception{
174164 }
175165
176166 /**
177- * Muti -level host whitelist.
178- * http://localhost:8080/url/seccode2 ?url=http://ccc.bbb.taobao.com
167+ * 多级域名白名单 Multi -level host whitelist.
168+ * http://localhost:8080/url/sec/multi_level_hos ?url=http://ccc.bbb.joychou.org
179169 *
180170 */
181- @ RequestMapping ("/seccode2" )
182- @ ResponseBody
183- public String seccode2 (HttpServletRequest request ) throws Exception {
184- String whiteDomainlists [] = {"aaa.taobao.com" , "ccc.bbb.taobao.com" };
185- String url = request .getParameter ("url" );
171+ @ GetMapping ("/sec/multi_level_host" )
172+ public String sec_multi_level_host (@ RequestParam ("url" ) String url ) throws Exception {
173+ String whiteDomainlists [] = {"aaa.joychou.org" , "ccc.bbb.joychou.org" };
186174
187175 URI uri = new URI (url );
188176 if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
@@ -199,21 +187,20 @@ public String seccode2(HttpServletRequest request) throws Exception{
199187 return "Bad url." ;
200188 }
201189
190+
202191 /**
203- * Muti -level host whitelist.
204- * http://localhost:8080/url/seccode3 ?url=http://ccc.bbb.taobao.com
192+ * 多级域名白名单 Multi -level host whitelist.
193+ * http://localhost:8080/url/sec/array_indexOf ?url=http://ccc.bbb.joychou.org
205194 *
206195 */
207- @ RequestMapping ("/seccode3" )
208- @ ResponseBody
209- public String seccode3 (HttpServletRequest request ) throws Exception {
196+ @ GetMapping ("/sec/array_indexOf" )
197+ public String sec_array_indexOf (@ RequestParam ("url" ) String url ) throws Exception {
210198
211199 // Define muti-level host whitelist.
212- ArrayList <String > whiteDomainlists = new ArrayList <String >();
213- whiteDomainlists .add ("bbb.taobao.com " );
214- whiteDomainlists .add ("ccc.bbb.taobao.com " );
200+ ArrayList <String > whiteDomainlists = new ArrayList <>();
201+ whiteDomainlists .add ("bbb.joychou.org " );
202+ whiteDomainlists .add ("ccc.bbb.joychou.org " );
215203
216- String url = request .getParameter ("url" );
217204 URI uri = new URI (url );
218205
219206 if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
0 commit comments