Skip to content

Commit e4190d6

Browse files
committed
Add RestTemplate SSRF
1 parent 707d395 commit e4190d6

File tree

7 files changed

+124
-4
lines changed

7 files changed

+124
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.joychou.config;
2+
3+
import org.springframework.boot.web.client.RestTemplateBuilder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
import java.io.IOException;
10+
import java.net.HttpURLConnection;
11+
12+
13+
class CustomClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
14+
15+
16+
@Override
17+
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
18+
super.prepareConnection(connection, httpMethod);
19+
// Use custom ClientHttpRequestFactory to set followRedirects false.
20+
connection.setInstanceFollowRedirects(false);
21+
}
22+
}
23+
24+
@Configuration
25+
public class HttpServiceConfig {
26+
27+
@Bean
28+
public RestTemplate restTemplateBanRedirects(RestTemplateBuilder builder) {
29+
return builder.requestFactory(CustomClientHttpRequestFactory.class).build();
30+
}
31+
32+
33+
@Bean
34+
public RestTemplate restTemplate(RestTemplateBuilder builder) {
35+
return builder.build();
36+
}
37+
38+
}

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import org.joychou.security.SecurityUtil;
44
import org.joychou.security.ssrf.SSRFException;
5+
import org.joychou.service.HttpService;
56
import org.joychou.util.HttpUtils;
67
import org.joychou.util.WebUtils;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
10+
import org.springframework.http.HttpHeaders;
11+
import org.springframework.http.MediaType;
912
import org.springframework.web.bind.annotation.*;
1013

14+
import javax.annotation.Resource;
1115
import javax.servlet.http.HttpServletResponse;
1216
import java.io.*;
1317
import java.net.*;
@@ -23,8 +27,10 @@
2327
@RequestMapping("/ssrf")
2428
public class SSRF {
2529

26-
private static Logger logger = LoggerFactory.getLogger(SSRF.class);
30+
private static final Logger logger = LoggerFactory.getLogger(SSRF.class);
2731

32+
@Resource
33+
private HttpService httpService;
2834

2935
/**
3036
* http://localhost:8080/ssrf/urlConnection/vuln?url=file:///etc/passwd
@@ -266,4 +272,27 @@ public String HttpSyncClients(@RequestParam("url") String url) {
266272
}
267273

268274

275+
/**
276+
* http://127.0.0.1:8080/ssrf/restTemplate/vuln?url=http://www.baidu.com <p>
277+
* Only support HTTP protocol. <p>
278+
* Redirects: GET HttpMethod follow redirects by default, other HttpMethods do not follow redirects<p>
279+
* User-Agent: Java/1.8.0_102 <p>
280+
*/
281+
@GetMapping("/restTemplate/vuln1")
282+
public String RestTemplateUrlBanRedirects(String url){
283+
HttpHeaders headers = new HttpHeaders();
284+
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
285+
return httpService.RequestHttpBanRedirects(url, headers);
286+
}
287+
288+
289+
@GetMapping("/restTemplate/vuln2")
290+
public String RestTemplateUrl(String url){
291+
HttpHeaders headers = new HttpHeaders();
292+
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
293+
return httpService.RequestHttp(url, headers);
294+
}
295+
296+
297+
269298
}

src/main/java/org/joychou/filter/OriginFilter.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import javax.servlet.http.HttpServletRequest;
77
import javax.servlet.http.HttpServletResponse;
88
import java.io.IOException;
9-
10-
import org.apache.catalina.servlet4preview.http.HttpFilter;
119
import org.joychou.security.SecurityUtil;
1210
import org.slf4j.Logger;
1311
import org.slf4j.LoggerFactory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.joychou.impl;
2+
3+
4+
import org.joychou.service.HttpService;
5+
import org.springframework.http.HttpEntity;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.web.client.RestTemplate;
10+
import org.springframework.http.HttpMethod;
11+
12+
import javax.annotation.Resource;
13+
14+
@Service
15+
public class HttpServiceImpl implements HttpService {
16+
17+
@Resource
18+
private RestTemplate restTemplate;
19+
20+
@Resource
21+
private RestTemplate restTemplateBanRedirects;
22+
23+
/**
24+
* Http request by RestTemplate. Only support HTTP protocol. <p>
25+
* Redirects: GET HttpMethod follow redirects by default, other HttpMethods do not follow redirects.<p>
26+
* User-Agent: Java/1.8.0_102 <p>
27+
*/
28+
public String RequestHttp(String url, HttpHeaders headers) {
29+
HttpEntity<String> entity = new HttpEntity<>(headers);
30+
ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
31+
return re.getBody();
32+
}
33+
34+
/**
35+
* Http request by RestTemplate. Only support HTTP protocol. <p>
36+
* Redirects: Disable followRedirects.<p>
37+
* User-Agent: Java/1.8.0_102 <p>
38+
*/
39+
public String RequestHttpBanRedirects(String url, HttpHeaders headers) {
40+
HttpEntity<String> entity = new HttpEntity<>(headers);
41+
ResponseEntity<String> re = restTemplateBanRedirects.exchange(url, HttpMethod.GET, entity, String.class);
42+
return re.getBody();
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.joychou.service;
2+
3+
4+
import org.springframework.http.HttpHeaders;
5+
6+
public interface HttpService {
7+
8+
String RequestHttp(String url, HttpHeaders headers);
9+
10+
String RequestHttpBanRedirects(String url, HttpHeaders headers);
11+
}

src/main/java/org/joychou/util/HttpUtils.java

-1
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,4 @@ public static String HttpAsyncClients(String url) {
221221
}
222222
}
223223
}
224-
225224
}

src/main/java/org/joychou/util/JwtUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static Boolean verifyTokenByJavaJwt(String token) {
8383

8484

8585
public static String getNicknameByJavaJwt(String token) {
86+
// If the signature is not verified, there will be security issues.
8687
if (!verifyTokenByJavaJwt(token)) {
8788
log.error("token is invalid");
8889
return null;

0 commit comments

Comments
 (0)