1
1
package org .joychou .controller ;
2
2
3
3
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 .* ;
7
7
8
- import javax .servlet .http .HttpServletRequest ;
9
8
import java .net .URI ;
10
9
import java .net .URL ;
11
10
import java .util .ArrayList ;
20
19
* @version 2018.08.23
21
20
*/
22
21
23
- @ Controller
22
+ @ RestController
24
23
@ RequestMapping ("/url" )
25
24
public class URLWhiteList {
26
25
27
26
28
27
private String domainwhitelist [] = {"joychou.org" , "joychou.com" };
29
-
28
+ private static final Logger logger = LoggerFactory . getLogger ( URLWhiteList . class );
30
29
/**
31
30
* 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
33
32
*
34
33
*/
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 {
39
36
URL u = new URL (url );
40
37
String host = u .getHost ().toLowerCase ();
41
38
@@ -47,15 +44,15 @@ public String endsWith(HttpServletRequest request) throws Exception{
47
44
return "Bad url." ;
48
45
}
49
46
47
+
50
48
/**
51
49
* 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
53
52
*
54
53
*/
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 {
59
56
URL u = new URL (url );
60
57
String host = u .getHost ().toLowerCase ();
61
58
@@ -70,13 +67,11 @@ public String contains(HttpServletRequest request) throws Exception{
70
67
71
68
/**
72
69
* 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
74
71
*
75
72
*/
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 {
80
75
URL u = new URL (url );
81
76
String host = u .getHost ().toLowerCase ();
82
77
@@ -92,15 +87,14 @@ public String regex(HttpServletRequest request) throws Exception{
92
87
93
88
/**
94
89
* 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
96
91
*
97
92
*/
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 {
102
95
URL u = new URL (url );
103
96
String host = u .getHost ();
97
+
104
98
// If indexOf returns -1, it means that no string was found.
105
99
for (String domain : domainwhitelist ){
106
100
if (host .indexOf (domain ) != -1 ) {
@@ -113,24 +107,22 @@ public String indexOf(HttpServletRequest request) throws Exception{
113
107
/**
114
108
* The bypass of using java.net.URL to getHost.
115
109
*
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'
118
112
*
119
113
* Detail: https://github.com/JoyChou93/java-sec-code/wiki/URL-whtielist-Bypass
120
114
*/
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 );
126
118
URL u = new URL (url );
127
119
128
120
if (!u .getProtocol ().startsWith ("http" ) && !u .getProtocol ().startsWith ("https" )) {
129
121
return "Url is not http or https" ;
130
122
}
131
123
132
124
String host = u .getHost ().toLowerCase ();
133
- System . out . println ("host: " + host );
125
+ logger . info ("host: " + host );
134
126
135
127
// endsWith .
136
128
for (String domain : domainwhitelist ){
@@ -145,18 +137,16 @@ public String url_bypass(HttpServletRequest request) throws Exception{
145
137
146
138
147
139
/**
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
150
142
*
151
143
*/
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 {
155
146
156
- String whiteDomainlists [] = {"taobao.com" , "tmall.com" };
157
- String url = request .getParameter ("url" );
147
+ String whiteDomainlists [] = {"joychou.org" , "joychou.com" };
158
148
159
- URI uri = new URI (url );
149
+ URI uri = new URI (url ); // 必须用URI
160
150
if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
161
151
return "SecurityUtil is not http or https" ;
162
152
}
@@ -174,15 +164,13 @@ public String seccode1(HttpServletRequest request) throws Exception{
174
164
}
175
165
176
166
/**
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
179
169
*
180
170
*/
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" };
186
174
187
175
URI uri = new URI (url );
188
176
if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
@@ -199,21 +187,20 @@ public String seccode2(HttpServletRequest request) throws Exception{
199
187
return "Bad url." ;
200
188
}
201
189
190
+
202
191
/**
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
205
194
*
206
195
*/
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 {
210
198
211
199
// 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 " );
215
203
216
- String url = request .getParameter ("url" );
217
204
URI uri = new URI (url );
218
205
219
206
if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
0 commit comments