1
1
package org .joychou .controller ;
2
2
3
- import com .squareup .okhttp .OkHttpClient ;
4
- import org .apache .commons .httpclient .HttpClient ;
5
- import org .apache .commons .httpclient .methods .GetMethod ;
6
- import org .apache .commons .io .IOUtils ;
7
- import org .apache .http .HttpResponse ;
8
- import org .apache .http .HttpStatus ;
9
- import org .apache .http .client .fluent .Request ;
10
- import org .apache .http .client .methods .HttpGet ;
11
- import org .apache .http .impl .client .CloseableHttpClient ;
12
- import org .apache .http .impl .client .HttpClients ;
13
3
import org .joychou .security .SecurityUtil ;
4
+ import org .joychou .security .ssrf .SSRFException ;
5
+ import org .joychou .util .HttpUtils ;
14
6
import org .joychou .util .WebUtils ;
15
- import org .jsoup .Jsoup ;
16
- import org .jsoup .nodes .Document ;
17
7
import org .slf4j .Logger ;
18
8
import org .slf4j .LoggerFactory ;
19
9
import org .springframework .web .bind .annotation .RequestMapping ;
20
10
import org .springframework .web .bind .annotation .RequestParam ;
21
11
import org .springframework .web .bind .annotation .ResponseBody ;
22
12
import org .springframework .web .bind .annotation .RestController ;
23
13
24
-
25
- import javax .imageio .ImageIO ;
26
14
import javax .servlet .http .HttpServletResponse ;
27
15
import java .io .*;
28
16
import java .net .*;
@@ -40,58 +28,58 @@ public class SSRF {
40
28
41
29
private static Logger logger = LoggerFactory .getLogger (SSRF .class );
42
30
43
- @ RequestMapping ("/urlConnection" )
44
- public static String ssrf_URLConnection (@ RequestParam String url ) {
45
- try {
46
- URL u = new URL (url );
47
- URLConnection urlConnection = u .openConnection ();
48
- BufferedReader in = new BufferedReader (new InputStreamReader (urlConnection .getInputStream ())); //send request
49
- String inputLine ;
50
- StringBuilder html = new StringBuilder ();
51
31
52
- while ((inputLine = in .readLine ()) != null ) {
53
- html .append (inputLine );
54
- }
55
- in .close ();
56
- return html .toString ();
57
- } catch (Exception e ) {
58
- logger .error (e .toString ());
59
- return "fail" ;
32
+ @ RequestMapping ("/urlConnection/vuln" )
33
+ public static String URLConnectionVuln (String url ) {
34
+ return HttpUtils .URLConnection (url );
35
+ }
36
+
37
+
38
+ @ RequestMapping ("/urlConnection/sec" )
39
+ public static String URLConnectionSec (String url ) {
40
+
41
+ // Decline not http/https protocol
42
+ if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
43
+ return "[-] SSRF check failed" ;
60
44
}
45
+
46
+ try {
47
+ SecurityUtil .startSSRFHook ();
48
+ return HttpUtils .URLConnection (url );
49
+ } catch (SSRFException | IOException e ) {
50
+ return e .getMessage ();
51
+ } finally {
52
+ SecurityUtil .stopSSRFHook ();
53
+ }
54
+
61
55
}
62
56
63
57
64
- @ RequestMapping ("/HttpURLConnection" )
58
+ @ RequestMapping ("/HttpURLConnection/sec " )
65
59
@ ResponseBody
66
- public static String ssrf_httpURLConnection (@ RequestParam String url ) {
60
+ public static String httpURLConnection (@ RequestParam String url ) {
67
61
try {
68
- URL u = new URL (url );
69
- URLConnection urlConnection = u .openConnection ();
70
- HttpURLConnection httpUrl = (HttpURLConnection ) urlConnection ;
71
- BufferedReader in = new BufferedReader (new InputStreamReader (httpUrl .getInputStream ())); //send request
72
- String inputLine ;
73
- StringBuilder html = new StringBuilder ();
74
-
75
- while ((inputLine = in .readLine ()) != null ) {
76
- html .append (inputLine );
77
- }
78
- in .close ();
79
- return html .toString ();
80
- } catch (Exception e ) {
81
- logger .error (e .toString ());
82
- return "fail" ;
62
+ SecurityUtil .startSSRFHook ();
63
+ return HttpUtils .HTTPURLConnection (url );
64
+ } catch (SSRFException | IOException e ) {
65
+ return e .getMessage ();
66
+ } finally {
67
+ SecurityUtil .stopSSRFHook ();
83
68
}
84
69
}
85
70
86
71
87
- @ RequestMapping ("/Request" )
72
+ // http://localhost:8080/ssrf/request/sec?url=http://www.baidu.com
73
+ @ RequestMapping ("/request/sec" )
88
74
@ ResponseBody
89
- public static String ssrf_Request (@ RequestParam String url ) {
75
+ public static String request (@ RequestParam String url ) {
90
76
try {
91
- return Request .Get (url ).execute ().returnContent ().toString ();
92
- } catch (Exception e ) {
93
- logger .error (e .toString ());
94
- return "fail" ;
77
+ SecurityUtil .startSSRFHook ();
78
+ return HttpUtils .request (url );
79
+ } catch (SSRFException | IOException e ) {
80
+ return e .getMessage ();
81
+ } finally {
82
+ SecurityUtil .stopSSRFHook ();
95
83
}
96
84
}
97
85
@@ -106,7 +94,7 @@ public static String ssrf_Request(@RequestParam String url) {
106
94
*/
107
95
@ RequestMapping ("/openStream" )
108
96
@ ResponseBody
109
- public static void ssrf_openStream (@ RequestParam String url , HttpServletResponse response ) throws IOException {
97
+ public static void openStream (@ RequestParam String url , HttpServletResponse response ) throws IOException {
110
98
InputStream inputStream = null ;
111
99
OutputStream outputStream = null ;
112
100
try {
@@ -136,164 +124,112 @@ public static void ssrf_openStream(@RequestParam String url, HttpServletResponse
136
124
}
137
125
138
126
139
- @ RequestMapping ("/ImageIO" )
127
+ @ RequestMapping ("/ImageIO/sec " )
140
128
@ ResponseBody
141
- public static void ssrf_ImageIO (@ RequestParam String url ) {
129
+ public static String ImageIO (@ RequestParam String url ) {
142
130
try {
143
- URL u = new URL (url );
144
- ImageIO .read (u ); // send request
145
- } catch (Exception e ) {
146
- logger .error (e .toString ());
131
+ SecurityUtil .startSSRFHook ();
132
+ HttpUtils .imageIO (url );
133
+ } catch (SSRFException | IOException e ) {
134
+ return e .getMessage ();
135
+ } finally {
136
+ SecurityUtil .stopSSRFHook ();
147
137
}
138
+
139
+ return "ImageIO ssrf test" ;
148
140
}
149
141
150
142
151
- @ RequestMapping ("/okhttp" )
143
+ @ RequestMapping ("/okhttp/sec " )
152
144
@ ResponseBody
153
- public static void ssrf_okhttp (@ RequestParam String url ) throws IOException {
154
- OkHttpClient client = new OkHttpClient ();
155
- com .squareup .okhttp .Request ok_http = new com .squareup .okhttp .Request .Builder ().url (url ).build ();
156
- client .newCall (ok_http ).execute ();
145
+ public static String okhttp (@ RequestParam String url ) {
146
+
147
+ try {
148
+ SecurityUtil .startSSRFHook ();
149
+ HttpUtils .okhttp (url );
150
+ } catch (SSRFException | IOException e ) {
151
+ return e .getMessage ();
152
+ } finally {
153
+ SecurityUtil .stopSSRFHook ();
154
+ }
155
+
156
+ return "okhttp ssrf test" ;
157
157
}
158
158
159
159
160
160
/**
161
- * http://localhost:8080/ssrf/HttpClient/sec?url=http://www.baidu.com
162
- *
163
- * @return The response of url param.
161
+ * http://localhost:8080/ssrf/httpclient/sec?url=http://www.baidu.com
164
162
*/
165
- @ RequestMapping ("/HttpClient /sec" )
163
+ @ RequestMapping ("/httpclient /sec" )
166
164
@ ResponseBody
167
- public static String ssrf_HttpClient (@ RequestParam String url ) {
168
- StringBuilder result = new StringBuilder ();
165
+ public static String HttpClient (@ RequestParam String url ) {
166
+
169
167
try {
170
168
SecurityUtil .startSSRFHook ();
171
- CloseableHttpClient client = HttpClients .createDefault ();
172
- HttpGet httpGet = new HttpGet (url );
173
- HttpResponse httpResponse = client .execute (httpGet ); // send request
174
- BufferedReader rd = new BufferedReader (new InputStreamReader (httpResponse .getEntity ().getContent ()));
175
-
176
- String line ;
177
- while ((line = rd .readLine ()) != null ) {
178
- result .append (line );
179
- }
180
-
181
- // SecurityUtil.stopSSRFHook();
182
- return result .toString ();
183
-
184
- } catch (Exception e ) {
185
- return e .toString ();
169
+ return HttpUtils .httpClient (url );
170
+ } catch (SSRFException | IOException e ) {
171
+ return e .getMessage ();
186
172
} finally {
187
173
SecurityUtil .stopSSRFHook ();
188
174
}
175
+
189
176
}
190
177
191
178
192
179
/**
193
- * https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient
194
- * UserAgent: Jakarta Commons-HttpClient/3.1 (2007.08 publish)
195
- * <p>
196
180
* http://localhost:8080/ssrf/commonsHttpClient/sec?url=http://www.baidu.com
197
181
*/
198
182
@ RequestMapping ("/commonsHttpClient/sec" )
199
183
@ ResponseBody
200
184
public static String commonsHttpClient (@ RequestParam String url ) {
201
- if (!SecurityUtil .checkSSRFByWhitehosts (url )) {
202
- return "Bad man. I got u." ;
203
- }
204
-
205
- HttpClient client = new HttpClient ();
206
- GetMethod method = new GetMethod (url );
207
- method .setFollowRedirects (false );
208
185
209
186
try {
210
- // Send http request.
211
- int status_code = client .executeMethod (method );
212
-
213
- // Only allow the url that status_code is 200.
214
- if (status_code != HttpStatus .SC_OK ) {
215
- return "Method failed: " + method .getStatusLine ();
216
- }
217
-
218
- // Read the response body.
219
- byte [] resBody = method .getResponseBody ();
220
- return new String (resBody );
221
-
222
- } catch (IOException e ) {
223
- return "Error: " + e .getMessage ();
187
+ SecurityUtil .startSSRFHook ();
188
+ return HttpUtils .commonHttpClient (url );
189
+ } catch (SSRFException | IOException e ) {
190
+ return e .getMessage ();
224
191
} finally {
225
- // Release the connection.
226
- method .releaseConnection ();
192
+ SecurityUtil .stopSSRFHook ();
227
193
}
228
194
229
-
230
195
}
231
196
232
197
/**
233
- * jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文本内容。
234
- * <p>
235
198
* http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com
236
199
*/
237
- @ RequestMapping ("/Jsoup" )
200
+ @ RequestMapping ("/Jsoup/sec " )
238
201
@ ResponseBody
239
202
public static String Jsoup (@ RequestParam String url ) {
203
+
240
204
try {
241
- Document doc = Jsoup .connect (url )
242
- .userAgent (
243
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) "
244
- + "Chrome/64.0.3282.167 Safari/537.36" )
245
- .timeout (3000 )
246
- .cookie ("name" , "joychou" ) // request请求带的cookie
247
- .followRedirects (false )
248
- .execute ().parse ();
249
- logger .info (doc .html ());
250
- } catch (MalformedURLException e ) {
251
- return "exception: " + e .toString ();
252
- } catch (IOException e ) {
253
- logger .error (e .toString ());
254
- return "exception: " + e .toString ();
205
+ SecurityUtil .startSSRFHook ();
206
+ return HttpUtils .Jsoup (url );
207
+ } catch (SSRFException | IOException e ) {
208
+ return e .getMessage ();
209
+ } finally {
210
+ SecurityUtil .stopSSRFHook ();
255
211
}
256
212
257
- return "Jsoup ssrf" ;
258
213
}
259
214
260
215
261
216
/**
262
- * 用途:IOUtils可远程获取URL图片
263
- * 默认重定向:是
264
- * 封装类:URLConnection
265
- * http://localhost:8080/ssrf/IOUtils?url=http://www.baidu.com
217
+ * http://localhost:8080/ssrf/IOUtils/sec?url=http://www.baidu.com
266
218
*/
267
- @ RequestMapping ("/IOUtils" )
268
- public static String IOUtils (@ RequestParam String url ) {
219
+ @ RequestMapping ("/IOUtils/sec" )
220
+ public static String IOUtils (String url ) {
221
+
269
222
try {
270
- // IOUtils.toByteArray内部用URLConnection进行了封装
271
- IOUtils .toByteArray (URI .create (url ));
272
- } catch (Exception e ) {
273
- return "exception: " + e .toString ();
223
+ SecurityUtil .startSSRFHook ();
224
+ HttpUtils .IOUtils (url );
225
+ } catch (SSRFException | IOException e ) {
226
+ return e .getMessage ();
227
+ } finally {
228
+ SecurityUtil .stopSSRFHook ();
274
229
}
275
230
276
- return "IOUtils ssrf" ;
231
+ return "IOUtils ssrf test " ;
277
232
}
278
233
279
234
280
- /**
281
- * Safe code.
282
- * http://localhost:8080/ssrf/ImageIO/sec?url=http://www.baidu.com
283
- */
284
- @ RequestMapping ("/ImageIO/sec" )
285
- public static String ImageIOSec (@ RequestParam String url ) {
286
- try {
287
- URL u = new URL (url );
288
- if (!SecurityUtil .checkSSRFWithoutRedirect (url )) {
289
- logger .error ("[-] SSRF check failed. Original Url: " + url );
290
- return "SSRF check failed." ;
291
- }
292
- ImageIO .read (u ); // send request
293
- } catch (Exception e ) {
294
- return e .toString ();
295
- }
296
-
297
- return "ImageIO ssrf safe code." ;
298
- }
299
235
}
0 commit comments