4
4
import com .squareup .okhttp .OkHttpClient ;
5
5
import org .apache .commons .httpclient .HttpClient ;
6
6
import org .apache .commons .httpclient .methods .GetMethod ;
7
+ import org .apache .commons .io .IOUtils ;
7
8
import org .apache .http .HttpResponse ;
8
9
import org .apache .http .HttpStatus ;
9
10
import org .apache .http .client .fluent .Request ;
10
11
import org .apache .http .client .methods .HttpGet ;
11
12
import org .apache .http .impl .client .CloseableHttpClient ;
12
13
import org .apache .http .impl .client .HttpClients ;
13
14
import org .joychou .security .SecurityUtil ;
14
- import org .springframework .stereotype .Controller ;
15
+ import org .jsoup .Jsoup ;
16
+ import org .jsoup .nodes .Document ;
17
+ import org .slf4j .Logger ;
18
+ import org .slf4j .LoggerFactory ;
15
19
import org .springframework .web .bind .annotation .RequestMapping ;
20
+ import org .springframework .web .bind .annotation .RequestParam ;
16
21
import org .springframework .web .bind .annotation .ResponseBody ;
22
+ import org .springframework .web .bind .annotation .RestController ;
17
23
18
24
19
25
import javax .imageio .ImageIO ;
20
26
import javax .servlet .http .HttpServletRequest ;
21
27
import javax .servlet .http .HttpServletResponse ;
22
28
import java .io .*;
23
- import java .net .URL ;
24
- import java .net .URLConnection ;
25
- import java .net .HttpURLConnection ;
29
+ import java .net .*;
26
30
27
31
28
32
/**
31
35
* @desc Java ssrf vuls code.
32
36
*/
33
37
34
- @ Controller
38
+ @ RestController
35
39
@ RequestMapping ("/ssrf" )
36
40
public class SSRF {
37
41
42
+ private static Logger logger = LoggerFactory .getLogger (SSRF .class );
43
+
38
44
@ RequestMapping ("/urlConnection" )
39
- @ ResponseBody
40
45
public static String ssrf_URLConnection (HttpServletRequest request )
41
46
{
42
47
try {
@@ -169,9 +174,7 @@ public static void ssrf_okhttp(HttpServletRequest request) throws IOException {
169
174
*/
170
175
@ RequestMapping ("/HttpClient" )
171
176
@ ResponseBody
172
- public static String ssrf_HttpClient (HttpServletRequest request ) {
173
-
174
- String url = request .getParameter ("url" );
177
+ public static String ssrf_HttpClient (@ RequestParam String url ) {
175
178
CloseableHttpClient client = HttpClients .createDefault ();
176
179
HttpGet httpGet = new HttpGet (url );
177
180
try {
@@ -193,26 +196,18 @@ public static String ssrf_HttpClient(HttpServletRequest request) {
193
196
194
197
/**
195
198
* Safe code.
196
- * http://localhost:8080/ssrf/commonsHttpClient?url=http://www.baidu.com
199
+ * http://localhost:8080/ssrf/commonsHttpClient/sec ?url=http://www.baidu.com
197
200
*
198
201
*/
199
- @ RequestMapping ("/commonsHttpClient" )
202
+ @ RequestMapping ("/commonsHttpClient/sec " )
200
203
@ ResponseBody
201
- public static String commonsHttpClient (HttpServletRequest request ) {
202
-
203
- String url = request .getParameter ("url" );
204
-
205
- // Security check
204
+ public static String commonsHttpClient (@ RequestParam String url ) {
206
205
if (!SecurityUtil .checkSSRFWithoutRedirect (url )) {
207
206
return "Bad man. I got u." ;
208
207
}
209
- // Create an instance of HttpClient.
210
- HttpClient client = new HttpClient ();
211
208
212
- // Create a method instance.
209
+ HttpClient client = new HttpClient ();
213
210
GetMethod method = new GetMethod (url );
214
-
215
- // forbid 302 redirection
216
211
method .setFollowRedirects (false );
217
212
218
213
try {
@@ -238,19 +233,63 @@ public static String commonsHttpClient(HttpServletRequest request) {
238
233
239
234
}
240
235
236
+ /**
237
+ * jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文本内容。
238
+ * http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com
239
+ *
240
+ */
241
+ @ RequestMapping ("/Jsoup" )
242
+ @ ResponseBody
243
+ public static String Jsoup (@ RequestParam String url ) {
244
+ try {
245
+ Document doc = Jsoup .connect (url )
246
+ .userAgent (
247
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) "
248
+ + "Chrome/64.0.3282.167 Safari/537.36" )
249
+ .timeout (3000 )
250
+ .cookie ("name" , "joychou" ) // request请求带的cookie
251
+ .followRedirects (false )
252
+ .execute ().parse ();
253
+ } catch (MalformedURLException e ) {
254
+ return "exception: " + e .toString ();
255
+ } catch (Exception e ) {
256
+ return "exception: " + e .toString ();
257
+ }
258
+
259
+ return "Jsoup ssrf" ;
260
+ }
261
+
262
+
263
+ /**
264
+ * 用途:IOUtils可远程获取URL图片
265
+ * 默认重定向:是
266
+ * 封装类:URLConnection
267
+ * http://localhost:8080/ssrf/IOUtils?url=http://www.baidu.com
268
+ */
269
+ @ RequestMapping ("/IOUtils" )
270
+ public static String IOUtils (@ RequestParam String url ) {
271
+ try {
272
+ // IOUtils.toByteArray内部用URLConnection进行了封装
273
+ byte [] b = IOUtils .toByteArray (URI .create (url ));
274
+ } catch (Exception e ) {
275
+ return "exception: " + e .toString ();
276
+ }
277
+
278
+ return "IOUtils ssrf" ;
279
+ }
280
+
241
281
242
282
/**
243
283
* Safe code.
244
- * http://localhost:8080/ssrf/ImageIO_safe ?url=http://www.baidu.com
284
+ * http://localhost:8080/ssrf/ImageIO/sec ?url=http://www.baidu.com
245
285
*
246
286
*/
247
- @ RequestMapping ("/ImageIO_safe" )
248
- @ ResponseBody
249
- public static String ssrf_ImageIO_safecode (HttpServletRequest request ) {
250
- String url = request .getParameter ("url" );
287
+ @ RequestMapping ("/ImageIO/sec" )
288
+ public static String ImageIOSec (@ RequestParam String url ) {
251
289
try {
252
290
URL u = new URL (url );
253
291
if (!SecurityUtil .checkSSRF (url )) {
292
+ logger .error ("[-] SSRF check failed. Original Url: " + url );
254
293
return "SSRF check failed." ;
255
294
}
256
295
ImageIO .read (u ); // send request
0 commit comments