-
Notifications
You must be signed in to change notification settings - Fork 654
/
SSRF.java
318 lines (263 loc) · 10.1 KB
/
SSRF.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package org.joychou.controller;
import cn.hutool.http.HttpUtil;
import org.joychou.security.SecurityUtil;
import org.joychou.security.ssrf.SSRFException;
import org.joychou.service.HttpService;
import org.joychou.util.HttpUtils;
import org.joychou.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.*;
/**
* Java SSRF vuln or security code.
*
* @author JoyChou @2017-12-28
*/
@RestController
@RequestMapping("/ssrf")
public class SSRF {
private static final Logger logger = LoggerFactory.getLogger(SSRF.class);
@Resource
private HttpService httpService;
/**
* <p>
* The default setting of followRedirects is true. <br>
* Protocol: file ftp mailto http https jar netdoc. <br>
* UserAgent is Java/1.8.0_102.
* </p>
* <a href="http://localhost:8080/ssrf/urlConnection/vuln?url=file:///etc/passwd">http://localhost:8080/ssrf/urlConnection/vuln?url=file:///etc/passwd</a>
*/
@RequestMapping(value = "/urlConnection/vuln", method = {RequestMethod.POST, RequestMethod.GET})
public String URLConnectionVuln(String url) {
return HttpUtils.URLConnection(url);
}
@GetMapping("/urlConnection/sec")
public String URLConnectionSec(String url) {
// Decline not http/https protocol
if (!SecurityUtil.isHttp(url)) {
return "[-] SSRF check failed";
}
try {
SecurityUtil.startSSRFHook();
return HttpUtils.URLConnection(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is Java/1.8.0_102.
*/
@GetMapping("/HttpURLConnection/sec")
public String httpURLConnection(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.HttpURLConnection(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
@GetMapping("/HttpURLConnection/vuln")
public String httpURLConnectionVuln(@RequestParam String url) {
return HttpUtils.HttpURLConnection(url);
}
/**
* The default setting of followRedirects is true.
* UserAgent is <code>Apache-HttpClient/4.5.12 (Java/1.8.0_102)</code>. <br>
* <a href="http://localhost:8080/ssrf/request/sec?url=http://test.joychou.org">http://localhost:8080/ssrf/request/sec?url=http://test.joychou.org</a>
*/
@GetMapping("/request/sec")
public String request(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.request(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* Download the url file. <br>
* <code>new URL(String url).openConnection()</code> <br>
* <code>new URL(String url).openStream()</code> <br>
* <code>new URL(String url).getContent()</code> <br>
* <a href="http://localhost:8080/ssrf/openStream?url=file:///etc/passwd">http://localhost:8080/ssrf/openStream?url=file:///etc/passwd</a>
*/
@GetMapping("/openStream")
public void openStream(@RequestParam String url, HttpServletResponse response) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String downLoadImgFileName = WebUtils.getNameWithoutExtension(url) + "." + WebUtils.getFileExtension(url);
// download
response.setHeader("content-disposition", "attachment;fileName=" + downLoadImgFileName);
URL u = new URL(url);
int length;
byte[] bytes = new byte[1024];
inputStream = u.openStream(); // send request
outputStream = response.getOutputStream();
while ((length = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, length);
}
} catch (Exception e) {
logger.error(e.toString());
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is Java/1.8.0_102.
*/
@GetMapping("/ImageIO/sec")
public String ImageIO(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
HttpUtils.imageIO(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
return "ImageIO ssrf test";
}
@GetMapping("/okhttp/sec")
public String okhttp(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.okhttp(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is <code>Apache-HttpClient/4.5.12 (Java/1.8.0_102)</code>. <br>
* <a href="http://localhost:8080/ssrf/httpclient/sec?url=http://www.baidu.com">http://localhost:8080/ssrf/httpclient/sec?url=http://www.baidu.com</a>
*/
@GetMapping("/httpclient/sec")
public String HttpClient(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.httpClient(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is <code>Jakarta Commons-HttpClient/3.1</code>.
* <a href="http://localhost:8080/ssrf/commonsHttpClient/sec?url=http://www.baidu.com">http://localhost:8080/ssrf/commonsHttpClient/sec?url=http://www.baidu.com</a>
*/
@GetMapping("/commonsHttpClient/sec")
public String commonsHttpClient(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.commonHttpClient(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is the useragent of browser.<br>
* <a href="http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com">http://localhost:8080/ssrf/Jsoup?url=http://www.baidu.com</a>
*/
@GetMapping("/Jsoup/sec")
public String Jsoup(@RequestParam String url) {
try {
SecurityUtil.startSSRFHook();
return HttpUtils.Jsoup(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
}
/**
* The default setting of followRedirects is true.
* UserAgent is <code>Java/1.8.0_102</code>. <br>
* <a href="http://localhost:8080/ssrf/IOUtils/sec?url=http://www.baidu.com">http://localhost:8080/ssrf/IOUtils/sec?url=http://www.baidu.com</a>
*/
@GetMapping("/IOUtils/sec")
public String IOUtils(String url) {
try {
SecurityUtil.startSSRFHook();
HttpUtils.IOUtils(url);
} catch (SSRFException | IOException e) {
return e.getMessage();
} finally {
SecurityUtil.stopSSRFHook();
}
return "IOUtils ssrf test";
}
/**
* The default setting of followRedirects is true.
* UserAgent is <code>Apache-HttpAsyncClient/4.1.4 (Java/1.8.0_102)</code>.
*/
@GetMapping("/HttpSyncClients/vuln")
public String HttpSyncClients(@RequestParam("url") String url) {
return HttpUtils.HttpAsyncClients(url);
}
/**
* Only support HTTP protocol. <br>
* GET HttpMethod follow redirects by default, other HttpMethods do not follow redirects. <br>
* User-Agent is Java/1.8.0_102. <br>
* <a href="http://127.0.0.1:8080/ssrf/restTemplate/vuln1?url=http://www.baidu.com">http://127.0.0.1:8080/ssrf/restTemplate/vuln1?url=http://www.baidu.com</a>
*/
@GetMapping("/restTemplate/vuln1")
public String RestTemplateUrlBanRedirects(String url){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return httpService.RequestHttpBanRedirects(url, headers);
}
@GetMapping("/restTemplate/vuln2")
public String RestTemplateUrl(String url){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return httpService.RequestHttp(url, headers);
}
/**
* UserAgent is Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 Hutool.
* Do not follow redirects. <br>
* <a href="http://127.0.0.1:8080/ssrf/hutool/vuln?url=http://www.baidu.com">http://127.0.0.1:8080/ssrf/hutool/vuln?url=http://www.baidu.com</a>
*/
@GetMapping("/hutool/vuln")
public String hutoolHttp(String url){
return HttpUtil.get(url);
}
/**
* DnsRebind SSRF in java by setting ttl is zero. <br>
* <a href="http://localhost:8080/ssrf/dnsrebind/vuln?url=http://test.joychou.org">http://localhost:8080/ssrf/dnsrebind/vuln?url=dnsrebind_url</a>
*/
@GetMapping("/dnsrebind/vuln")
public String DnsRebind(String url) {
java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "0");
if (!SecurityUtil.checkSSRFWithoutRedirect(url)) {
return "Dangerous url";
}
return HttpUtil.get(url);
}
}