Skip to content

Commit ab69c0b

Browse files
committed
bug fix
1 parent f296f0d commit ab69c0b

File tree

9 files changed

+26
-22
lines changed

9 files changed

+26
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Sort by letter.
4040
- [ooxmlXXE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/othervulns/ooxmlXXE.java)
4141
- [PathTraversal](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/PathTraversal.java)
4242
- [RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/Rce.java)
43+
- [Swagger](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/config/SwaggerConfig.java)
4344
- [SpEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SpEL.java)
4445
- [SQL Injection](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SQLI.java)
4546
- [SSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SSRF.java)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>sec</groupId>
88
<artifactId>java-sec-code</artifactId>
99
<version>1.0.0</version>
10-
<packaging>war</packaging>
10+
<packaging>jar</packaging>
1111

1212
<properties>
1313
<maven.compiler.source>1.8</maven.compiler.source> <!-- mvn clean package-->

src/main/java/org/joychou/config/SafeDomainParser.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ public SafeDomainParser() {
2929
try {
3030
// 读取resources目录下的文件
3131
ClassPathResource resource = new ClassPathResource(safeDomainClassPath);
32-
File file = resource.getFile();
33-
3432
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
3533
DocumentBuilder db = dbf.newDocumentBuilder();
36-
Document doc = db.parse(file); // parse xml
34+
Document doc = db.parse(resource.getInputStream()); // parse xml
3735

3836
NodeList rootNode = doc.getElementsByTagName(rootTag); // 解析根节点domains
3937
Node domainsNode = rootNode.item(0);
@@ -68,6 +66,7 @@ public SafeDomainParser() {
6866

6967
WebConfig wc = new WebConfig();
7068
wc.setSafeDomains(safeDomains);
69+
logger.info(safeDomains.toString());
7170
wc.setBlockDomains(blockDomains);
7271

7372
// 解析SSRF配置
@@ -86,11 +85,10 @@ public SafeDomainParser() {
8685
try {
8786
// 读取resources目录下的文件
8887
ClassPathResource resource = new ClassPathResource(ssrfSafeDomainClassPath);
89-
File file = resource.getFile();
90-
9188
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
9289
DocumentBuilder db = dbf.newDocumentBuilder();
93-
Document doc = db.parse(file); // parse xml
90+
// 修复打包成jar包运行,不能读取文件的bug
91+
Document doc = db.parse(resource.getInputStream()); // parse xml
9492

9593
NodeList rootNode = doc.getElementsByTagName(ssrfRootTag); // 解析根节点
9694
Node domainsNode = rootNode.item(0);
@@ -130,6 +128,7 @@ public SafeDomainParser() {
130128
logger.error(e.toString());
131129
}
132130

131+
logger.info(ssrfBlockIps.toString());
133132
wc.setSsrfBlockDomains(ssrfBlockDomains);
134133
wc.setSsrfBlockIps(ssrfBlockIps);
135134
wc.setSsrfSafeDomains(ssrfSafeDomains);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,18 @@ public String ImageIO(@RequestParam String url) {
150150
}
151151

152152

153-
/**
154-
* The default setting of followRedirects is true.
155-
* UserAgent is <code>okhttp/2.5.0</code>.
156-
*/
157153
@GetMapping("/okhttp/sec")
158154
public String okhttp(@RequestParam String url) {
159155

160156
try {
161157
SecurityUtil.startSSRFHook();
162-
HttpUtils.okhttp(url);
158+
return HttpUtils.okhttp(url);
163159
} catch (SSRFException | IOException e) {
164160
return e.getMessage();
165161
} finally {
166162
SecurityUtil.stopSSRFHook();
167163
}
168164

169-
return "okhttp ssrf test";
170165
}
171166

172167

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.slf4j.LoggerFactory;
77
import org.springframework.web.bind.annotation.*;
88

9+
import java.net.MalformedURLException;
10+
import java.net.URL;
911
import java.util.ArrayList;
1012
import java.util.regex.Matcher;
1113
import java.util.regex.Pattern;
@@ -92,15 +94,16 @@ public String regex(@RequestParam("url") String url) {
9294
* More details: https://github.com/JoyChou93/java-sec-code/wiki/URL-whtielist-Bypass
9395
*/
9496
@GetMapping("/vuln/url_bypass")
95-
public String url_bypass(String url) {
97+
public String url_bypass(String url) throws MalformedURLException {
9698

9799
logger.info("url: " + url);
98100

99101
if (!SecurityUtil.isHttp(url)) {
100102
return "Url is not http or https";
101103
}
102104

103-
String host = SecurityUtil.gethost(url);
105+
URL u = new URL(url);
106+
String host = u.getHost();
104107
logger.info("host: " + host);
105108

106109
// endsWith .

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
import org.springframework.security.access.AccessDeniedException;
88
import org.springframework.security.web.access.AccessDeniedHandler;
99

10-
11-
import javax.servlet.ServletException;
1210
import javax.servlet.http.HttpServletRequest;
1311
import javax.servlet.http.HttpServletResponse;
1412
import java.io.IOException;
1513

1614
/**
17-
* Design csrf access denied page.
15+
* Csrf access denied page.
1816
*
17+
* @author JoyChou
1918
*/
2019
public class CsrfAccessDeniedHandler implements AccessDeniedHandler {
2120

2221
protected final Logger logger= LoggerFactory.getLogger(this.getClass());
2322

2423
@Override
2524
public void handle(HttpServletRequest request, HttpServletResponse response,
26-
AccessDeniedException accessDeniedException) throws IOException, ServletException {
25+
AccessDeniedException accessDeniedException) throws IOException {
2726

2827
logger.info("[-] URL: " + request.getRequestURL() + "?" + request.getQueryString() + "\t" +
2928
"Referer: " + request.getHeader("referer"));

src/main/java/org/joychou/security/ssrf/SSRFChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static boolean checkURLFckSSRF(String url) {
5050
/**
5151
* 解析url的ip,判断ip是否是内网ip,所以TTL设置为0的情况不适用。
5252
* url只允许https或者http,并且设置默认连接超时时间。
53-
* 该修复方案会主动请求重定向后的链接。最好用Hook方式获取到所有url后,进行判断,代码待续…
53+
* 该修复方案会主动请求重定向后的链接。
5454
*
5555
* @param url check的url
5656
* @param checkTimes 设置重定向检测的最大次数,建议设置为10次

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,16 @@ public static String Jsoup(String url) {
146146
}
147147

148148

149-
public static void okhttp(String url) throws IOException {
149+
/**
150+
* The default setting of followRedirects is true. The option of followRedirects is true.
151+
*
152+
* UserAgent is <code>okhttp/2.5.0</code>.
153+
*/
154+
public static String okhttp(String url) throws IOException {
150155
OkHttpClient client = new OkHttpClient();
156+
// client.setFollowRedirects(false);
151157
com.squareup.okhttp.Request ok_http = new com.squareup.okhttp.Request.Builder().url(url).build();
152-
client.newCall(ok_http).execute();
158+
return client.newCall(ok_http).execute().body().string();
153159
}
154160

155161

src/main/resources/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<p>Hello <span th:text="${user}"></span>.</p>
99
<p>Welcome to login java-sec-code application. <a th:href="@{/appInfo}">Application Infomation</a></p>
1010
<p>
11+
<a th:href="@{/swagger-ui.html}">Swagger</a>&nbsp;&nbsp;
1112
<a th:href="@{/codeinject?filepath=/tmp;cat /etc/passwd}">CmdInject</a>&nbsp;&nbsp;
1213
<a th:href="@{/jsonp/getToken?_callback=test}">JSONP</a>&nbsp;&nbsp;
1314
<a th:href="@{/file/pic}">FileUpload</a>&nbsp;&nbsp;

0 commit comments

Comments
 (0)