Skip to content

Commit 0e4f22e

Browse files
committed
Add httpclient SSRF vul code
1 parent 12ab307 commit 0e4f22e

File tree

11 files changed

+156
-102
lines changed

11 files changed

+156
-102
lines changed

java-sec-code.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,6 @@
170170
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.6.RELEASE" level="project" />
171171
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-security:2.1.5.RELEASE" level="project" />
172172
<orderEntry type="library" name="Maven: commons-net:commons-net:3.6" level="project" />
173+
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
173174
</component>
174175
</module>

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<artifactId>httpclient</artifactId>
8585
<version>4.3.6</version>
8686
</dependency>
87+
8788
<dependency>
8889
<groupId>org.apache.httpcomponents</groupId>
8990
<artifactId>fluent-hc</artifactId>
@@ -161,6 +162,13 @@
161162
<version>3.6</version>
162163
</dependency>
163164

165+
<!-- HttpClient SSRF -->
166+
<dependency>
167+
<groupId>commons-httpclient</groupId>
168+
<artifactId>commons-httpclient</artifactId>
169+
<version>3.1</version>
170+
</dependency>
171+
164172
</dependencies>
165173

166174
<dependencyManagement>

src/main/java/org/joychou/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
99

1010

11-
@ServletComponentScan
11+
@ServletComponentScan // do filter
1212
@SpringBootApplication
1313
// @EnableEurekaClient // 测试Eureka请打开注释,防止控制台一直有warning
1414
public class Application extends SpringBootServletInitializer {

src/main/java/org/joychou/CsrfAccessDeniedHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
public class CsrfAccessDeniedHandler implements AccessDeniedHandler {
1515

1616
/**
17-
* @desc 返回自定义拦截页面
17+
* Design csrf access denied page.
18+
*
1819
*/
1920
@Override
2021
public void handle(HttpServletRequest request, HttpServletResponse response,
2122
AccessDeniedException accessDeniedException) throws IOException, ServletException {
2223
response.setContentType(MediaType.TEXT_HTML_VALUE); // content-type: text/html
2324
response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 403 forbidden
24-
response.getWriter().write("CSRF check failed by JoyChou."); // response
25+
response.getWriter().write("CSRF check failed by JoyChou."); // response contents
2526
}
2627

2728
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.google.common.io.Files;
44
import com.squareup.okhttp.OkHttpClient;
5+
import org.apache.commons.httpclient.HttpClient;
6+
import org.apache.commons.httpclient.methods.GetMethod;
57
import org.apache.http.HttpResponse;
8+
import org.apache.http.HttpStatus;
69
import org.apache.http.client.fluent.Request;
710
import org.apache.http.client.methods.HttpGet;
811
import org.apache.http.impl.client.CloseableHttpClient;
@@ -176,6 +179,55 @@ public static String ssrf_HttpClient(HttpServletRequest request) {
176179
}
177180

178181

182+
/**
183+
* Safe code: http://localhost:8080/ssrf/commonsHttpClient?url=http://www.baidu.com
184+
*
185+
* @param request
186+
* @return
187+
*/
188+
@RequestMapping("/commonsHttpClient")
189+
@ResponseBody
190+
public static String commonsHttpClient(HttpServletRequest request) {
191+
192+
String url = request.getParameter("url");
193+
194+
// Security check
195+
if (!SecurityUtil.checkSSRFWithoutRedirect(url)) {
196+
return "Bad man. I got u.";
197+
}
198+
// Create an instance of HttpClient.
199+
HttpClient client = new HttpClient();
200+
201+
// Create a method instance.
202+
GetMethod method = new GetMethod(url);
203+
204+
// forbid 302 redirection
205+
method.setFollowRedirects(false);
206+
207+
try {
208+
// Send http request.
209+
int status_code = client.executeMethod(method);
210+
211+
// Only allow the url that status_code is 200.
212+
if (status_code != HttpStatus.SC_OK) {
213+
return "Method failed: " + method.getStatusLine();
214+
}
215+
216+
// Read the response body.
217+
byte[] resBody = method.getResponseBody();
218+
return new String(resBody);
219+
220+
} catch (IOException e) {
221+
return "Error: " + e.getMessage();
222+
} finally {
223+
// Release the connection.
224+
method.releaseConnection();
225+
}
226+
227+
228+
}
229+
230+
179231
/**
180232
* http://localhost:8080/ssrf/ImageIO_safe?url=
181233
*

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,28 @@
1313
import org.joychou.security.SecurityUtil;
1414

1515
/**
16-
* @author JoyChou ([email protected])
17-
* @date 2017.12.28
18-
* @desc Java url redirect.
19-
* @fix Check redirect url whitelist.
16+
* The vulnerability code and security code of Java url redirect.
17+
* The security code is checking whitelist of url redirect.
18+
*
19+
* @author JoyChou ([email protected])
20+
* @version 2017.12.28
2021
*/
2122

22-
2323
@Controller
2424
@RequestMapping("/urlRedirect")
2525
public class URLRedirect {
2626

2727
/**
28-
* usage: http://localhost:8080/urlRedirect/redirect?url=http://www.baidu.com
29-
*
28+
* http://localhost:8080/urlRedirect/redirect?url=http://www.baidu.com
3029
*/
3130
@GetMapping("/redirect")
3231
public String redirect(@RequestParam("url") String url) {
3332
return "redirect:" + url;
3433
}
3534

35+
3636
/**
37-
* usage: http://localhost:8080/urlRedirect/setHeader?url=http://www.baidu.com
38-
*
37+
* http://localhost:8080/urlRedirect/setHeader?url=http://www.baidu.com
3938
*/
4039
@RequestMapping("/setHeader")
4140
@ResponseBody
@@ -45,9 +44,9 @@ public static void setHeader(HttpServletRequest request, HttpServletResponse res
4544
response.setHeader("Location", url);
4645
}
4746

47+
4848
/**
49-
* usage: http://localhost:8080/urlRedirect/sendRedirect?url=http://www.baidu.com
50-
*
49+
* http://localhost:8080/urlRedirect/sendRedirect?url=http://www.baidu.com
5150
*/
5251
@RequestMapping("/sendRedirect")
5352
@ResponseBody
@@ -58,13 +57,12 @@ public static void sendRedirect(HttpServletRequest request, HttpServletResponse
5857

5958

6059
/**
61-
* desc: security code.Because it can only jump according to the path, it cannot jump according to other urls.
62-
* usage: http://localhost:8080/urlRedirect/forward?url=/urlRedirect/test
63-
*
60+
* Safe code. Because it can only jump according to the path, it cannot jump according to other urls.
61+
* http://localhost:8080/urlRedirect/forward?url=/urlRedirect/test
6462
*/
6563
@RequestMapping("/forward")
6664
@ResponseBody
67-
public static void forward(HttpServletRequest request, HttpServletResponse response) throws IOException{
65+
public static void forward(HttpServletRequest request, HttpServletResponse response) {
6866
String url = request.getParameter("url");
6967
RequestDispatcher rd =request.getRequestDispatcher(url);
7068
try{
@@ -74,10 +72,10 @@ public static void forward(HttpServletRequest request, HttpServletResponse respo
7472
}
7573
}
7674

75+
7776
/**
78-
* desc: sendRedirect security code
79-
* usage: http://localhost:8080/urlRedirect/sendRedirect_seccode?url=http://www.baidu.com
80-
*
77+
* Safe code of sendRedirect.
78+
* http://localhost:8080/urlRedirect/sendRedirect_seccode?url=http://www.baidu.com
8179
*/
8280
@RequestMapping("/sendRedirect_seccode")
8381
@ResponseBody

0 commit comments

Comments
 (0)