Skip to content

Commit 7b187f2

Browse files
committed
Add XXE & SSRF Vuln Code
1 parent 9dd930e commit 7b187f2

File tree

10 files changed

+329
-187
lines changed

10 files changed

+329
-187
lines changed

java-sec-code.iml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@
6767
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:2.6.0" level="project" />
6868
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.24" level="project" />
6969
<orderEntry type="library" name="Maven: org.jdom:jdom2:2.0.6" level="project" />
70-
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.1" level="project" />
71-
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
70+
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.0" level="project" />
71+
<orderEntry type="library" name="Maven: jaxen:jaxen:1.1.6" level="project" />
72+
<orderEntry type="library" name="Maven: com.google.guava:guava:23.0" level="project" />
73+
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
74+
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.0.18" level="project" />
75+
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.1" level="project" />
76+
<orderEntry type="library" name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
7277
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.1" level="project" />
7378
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.4" level="project" />
7479
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
@@ -193,5 +198,7 @@
193198
<orderEntry type="library" name="Maven: xml-resolver:xml-resolver:1.2" level="project" />
194199
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.4.01" level="project" />
195200
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.22" level="project" />
201+
<orderEntry type="library" name="Maven: org.jsoup:jsoup:1.10.2" level="project" />
202+
<orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" />
196203
</component>
197204
</module>

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
<dependency>
6363
<groupId>org.dom4j</groupId>
6464
<artifactId>dom4j</artifactId>
65-
<version>2.1.1</version>
65+
<version>2.1.0</version>
6666
</dependency>
6767

6868

6969
<!-- 获取url根域名-->
7070
<dependency>
7171
<groupId>com.google.guava</groupId>
7272
<artifactId>guava</artifactId>
73-
<version>21.0</version>
73+
<version>23.0</version>
7474
</dependency>
7575

7676
<dependency>
@@ -215,7 +215,19 @@
215215
<version>2.0.0</version>
216216
</dependency>
217217

218+
<!-- ssrf -->
219+
<dependency>
220+
<groupId>org.jsoup</groupId>
221+
<artifactId>jsoup</artifactId>
222+
<version>1.10.2</version>
223+
</dependency>
218224

225+
<!-- ssrf -->
226+
<dependency>
227+
<groupId>commons-io</groupId>
228+
<artifactId>commons-io</artifactId>
229+
<version>2.5</version>
230+
</dependency>
219231
</dependencies>
220232

221233
<dependencyManagement>

src/main/java/org/joychou/controller/SSRF.java

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
import com.squareup.okhttp.OkHttpClient;
55
import org.apache.commons.httpclient.HttpClient;
66
import org.apache.commons.httpclient.methods.GetMethod;
7+
import org.apache.commons.io.IOUtils;
78
import org.apache.http.HttpResponse;
89
import org.apache.http.HttpStatus;
910
import org.apache.http.client.fluent.Request;
1011
import org.apache.http.client.methods.HttpGet;
1112
import org.apache.http.impl.client.CloseableHttpClient;
1213
import org.apache.http.impl.client.HttpClients;
1314
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;
1519
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.bind.annotation.RequestParam;
1621
import org.springframework.web.bind.annotation.ResponseBody;
22+
import org.springframework.web.bind.annotation.RestController;
1723

1824

1925
import javax.imageio.ImageIO;
2026
import javax.servlet.http.HttpServletRequest;
2127
import javax.servlet.http.HttpServletResponse;
2228
import java.io.*;
23-
import java.net.URL;
24-
import java.net.URLConnection;
25-
import java.net.HttpURLConnection;
29+
import java.net.*;
2630

2731

2832
/**
@@ -31,12 +35,13 @@
3135
* @desc Java ssrf vuls code.
3236
*/
3337

34-
@Controller
38+
@RestController
3539
@RequestMapping("/ssrf")
3640
public class SSRF {
3741

42+
private static Logger logger = LoggerFactory.getLogger(SSRF.class);
43+
3844
@RequestMapping("/urlConnection")
39-
@ResponseBody
4045
public static String ssrf_URLConnection(HttpServletRequest request)
4146
{
4247
try {
@@ -169,9 +174,7 @@ public static void ssrf_okhttp(HttpServletRequest request) throws IOException {
169174
*/
170175
@RequestMapping("/HttpClient")
171176
@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) {
175178
CloseableHttpClient client = HttpClients.createDefault();
176179
HttpGet httpGet = new HttpGet(url);
177180
try {
@@ -193,26 +196,18 @@ public static String ssrf_HttpClient(HttpServletRequest request) {
193196

194197
/**
195198
* 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
197200
*
198201
*/
199-
@RequestMapping("/commonsHttpClient")
202+
@RequestMapping("/commonsHttpClient/sec")
200203
@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) {
206205
if (!SecurityUtil.checkSSRFWithoutRedirect(url)) {
207206
return "Bad man. I got u.";
208207
}
209-
// Create an instance of HttpClient.
210-
HttpClient client = new HttpClient();
211208

212-
// Create a method instance.
209+
HttpClient client = new HttpClient();
213210
GetMethod method = new GetMethod(url);
214-
215-
// forbid 302 redirection
216211
method.setFollowRedirects(false);
217212

218213
try {
@@ -238,19 +233,63 @@ public static String commonsHttpClient(HttpServletRequest request) {
238233

239234
}
240235

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+
241281

242282
/**
243283
* 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
245285
*
246286
*/
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) {
251289
try {
252290
URL u = new URL(url);
253291
if (!SecurityUtil.checkSSRF(url)) {
292+
logger.error("[-] SSRF check failed. Original Url: "+ url);
254293
return "SSRF check failed.";
255294
}
256295
ImageIO.read(u); // send request

src/main/java/org/joychou/controller/SpEL.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.joychou.controller;
22

3-
import org.springframework.expression.EvaluationContext;
43
import org.springframework.expression.ExpressionParser;
54
import org.springframework.expression.spel.standard.SpelExpressionParser;
65
import org.springframework.web.bind.annotation.RequestMapping;

0 commit comments

Comments
 (0)