Skip to content

Commit 6b8b1d1

Browse files
committed
closes #6
1 parent 179f45e commit 6b8b1d1

File tree

6 files changed

+112
-71
lines changed

6 files changed

+112
-71
lines changed

README.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Sort by letter.
4848
- [JSONP](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/jsonp/JSONP.java)
4949
- [PathTraversal](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/PathTraversal.java)
5050
- [RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/Rce.java)
51-
- [SPEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SPEL.java)
51+
- [SpEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SpEL.java)
5252
- [SQL Injection](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SQLI.java)
5353
- [SSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SSRF.java)
5454
- [SSTI](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SSTI.java)
@@ -84,35 +84,19 @@ spring.datasource.username=root
8484
spring.datasource.password=woshishujukumima
8585
```
8686

87-
- Tomcat
8887
- IDEA
88+
- Tomcat
8989
- JAR
9090

91-
### Tomcat
9291

93-
- Exclude tomcat in pom.xml.
94-
95-
```xml
96-
<dependency>
97-
<groupId>org.springframework.boot</groupId>
98-
<artifactId>spring-boot-starter-web</artifactId>
99-
<exclusions>
100-
<exclusion>
101-
<groupId>org.springframework.boot</groupId>
102-
<artifactId>spring-boot-starter-tomcat</artifactId>
103-
</exclusion>
104-
</exclusions>
105-
</dependency>
106-
```
92+
### IDEA
10793

108-
- Build war package by `mvn clean package`.
109-
- Copy war package to tomcat webapps directory.
110-
- Start tomcat application.
94+
Click `run` button.
11195

11296
Example:
11397

11498
```
115-
http://localhost:8080/java-sec-code-1.0.0/rce/exec?cmd=whoami
99+
http://localhost:8080/rce/exec?cmd=whoami
116100
```
117101

118102
return:
@@ -121,14 +105,17 @@ return:
121105
Viarus
122106
```
123107

124-
### IDEA
108+
### Tomcat
125109

126-
Click `run` button.
110+
111+
- Build war package by `mvn clean package`.
112+
- Copy war package to tomcat webapps directory.
113+
- Start tomcat application.
127114

128115
Example:
129116

130117
```
131-
http://localhost:8080/rce/exec?cmd=whoami
118+
http://localhost:8080/java-sec-code-1.0.0/rce/exec?cmd=whoami
132119
```
133120

134121
return:
@@ -137,6 +124,7 @@ return:
137124
Viarus
138125
```
139126

127+
140128
### JAR
141129

142130
Change `war` to `jar` in `pom.xml`.
@@ -154,6 +142,7 @@ Build package and run.
154142
mvn clean package -DskipTests
155143
java -jar target/java-sec-code-1.0.0.jar
156144
```
145+
157146
## Contributors
158147

159148
Core developers : [JoyChou](https://github.com/JoyChou93).

README_zh.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@
1010

1111
每个漏洞类型代码默认存在安全漏洞(除非本身不存在漏洞),相关修复代码在注释里。具体可查看每个漏洞代码和注释。
1212

13-
## 认证
14-
15-
### 登录
16-
17-
[http://localhost:8080/login](http://localhost:8080/login)
18-
19-
如果未登录,访问任何页面都会重定向到login页面。用户名和密码如下。
20-
21-
```
22-
admin/admin123
23-
joychou/joychou123
24-
```
25-
### 登出
26-
27-
[http://localhost:8080/logout](http://localhost:8080/logout)
28-
29-
### 记住我
30-
31-
Tomcat默认JSESSION会话有效时间为30分钟,所以30分钟不操作会话将过期。为了解决这一问题,引入rememberMe功能,默认过期时间为2周。
3213

3314
## 漏洞代码
3415

@@ -44,7 +25,7 @@ Tomcat默认JSESSION会话有效时间为30分钟,所以30分钟不操作会
4425
- [JSONP](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/jsonp/JSONP.java)
4526
- [PathTraversal](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/PathTraversal.java)
4627
- [RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/Rce.java)
47-
- [SPEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SPEL.java)
28+
- [SpEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SpEL.java)
4829
- [SQL Injection](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SQLI.java)
4930
- [SSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SSRF.java)
5031
- [SSTI](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SSTI.java)
@@ -81,17 +62,20 @@ spring.datasource.username=root
8162
spring.datasource.password=woshishujukumima
8263
```
8364

84-
### Tomcat
65+
- IDEA
66+
- Tomcat
67+
- JAR
8568

86-
1. 生成war包 `mvn clean package`
87-
2. 将target目录的war包,cp到Tomcat的webapps目录。
88-
3. 重启Tomcat应用。
8969

9070

71+
### IDEA
72+
73+
直接点击run按钮即可运行。
74+
9175
例子:
9276

9377
```
94-
http://localhost:8080/java-sec-code-1.0.0/rce/exec?cmd=whoami
78+
http://localhost:8080/rce/exec?cmd=whoami
9579
```
9680

9781
返回:
@@ -100,14 +84,17 @@ http://localhost:8080/java-sec-code-1.0.0/rce/exec?cmd=whoami
10084
Viarus
10185
```
10286

103-
### IDEA
87+
### Tomcat
88+
89+
1. 生成war包 `mvn clean package`
90+
2. 将target目录的war包,cp到Tomcat的webapps目录。
91+
3. 重启Tomcat应用。
10492

105-
直接点击run按钮即可运行。
10693

10794
例子:
10895

10996
```
110-
http://localhost:8080/rce/exec?cmd=whoami
97+
http://localhost:8080/java-sec-code-1.0.0/rce/exec?cmd=whoami
11198
```
11299

113100
返回:
@@ -117,7 +104,6 @@ Viarus
117104
```
118105

119106

120-
121107
### JAR包
122108

123109

@@ -137,6 +123,27 @@ mvn clean package -DskipTests
137123
java -jar 打包后的jar包路径
138124
```
139125

126+
## 认证
127+
128+
### 登录
129+
130+
[http://localhost:8080/login](http://localhost:8080/login)
131+
132+
如果未登录,访问任何页面都会重定向到login页面。用户名和密码如下。
133+
134+
```
135+
admin/admin123
136+
joychou/joychou123
137+
```
138+
### 登出
139+
140+
[http://localhost:8080/logout](http://localhost:8080/logout)
141+
142+
### 记住我
143+
144+
Tomcat默认JSESSION会话有效时间为30分钟,所以30分钟不操作会话将过期。为了解决这一问题,引入rememberMe功能,默认过期时间为2周。
145+
146+
140147
## 贡献者
141148

142149
核心开发者: [JoyChou](https://github.com/JoyChou93).其他开发者:[lightless](https://github.com/lightless233), [Anemone95](https://github.com/Anemone95)。欢迎各位提交PR。
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.joychou.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
7+
/**
8+
* Solve can't get value in filter by @Value when not using embed tomcat.
9+
*
10+
* @author JoyChou @2019-07-24
11+
*/
12+
@Component
13+
public class WebConfig {
14+
15+
private static Boolean referSecEnabled = false;
16+
private static String[] callbacks;
17+
private static String[] referWhitelist;
18+
private static String[] referUris;
19+
20+
@Value("${joychou.security.referer.enabled}")
21+
public void setReferSecEnabled(Boolean referSecEnabled){
22+
WebConfig.referSecEnabled = referSecEnabled;
23+
}
24+
public static Boolean getReferSecEnabled(){
25+
return referSecEnabled;
26+
}
27+
28+
29+
@Value("${joychou.security.jsonp.callback}")
30+
public void setCallbacks(String[] callbacks){
31+
WebConfig.callbacks = callbacks;
32+
}
33+
public static String[] getCallbacks(){
34+
return callbacks;
35+
}
36+
37+
38+
@Value("${joychou.security.referer.hostwhitelist}")
39+
public void setReferWhitelist(String[] referWhitelist){
40+
WebConfig.referWhitelist = referWhitelist;
41+
}
42+
public static String[] getReferWhitelist(){
43+
return referWhitelist;
44+
}
45+
46+
47+
@Value("${joychou.security.referer.uri}")
48+
public void setReferUris(String[] referUris)
49+
{
50+
WebConfig.referUris = referUris;
51+
}
52+
public static String[] getReferUris(){
53+
return referUris;
54+
}
55+
}

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

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

33

44
import com.alibaba.fastjson.JSON;
5+
import org.apache.catalina.util.ServerInfo;
56
import org.springframework.stereotype.Controller;
67
import org.springframework.ui.Model;
78
import org.springframework.web.bind.annotation.RequestMapping;
@@ -26,6 +27,7 @@ public static String appInfo(HttpServletRequest request) {
2627
String username = request.getUserPrincipal().getName();
2728
Map<String, String> m = new HashMap<>();
2829

30+
m.put("tomcat_version", ServerInfo.getServerInfo());
2931
m.put("username", username);
3032
m.put("login", "success");
3133
m.put("app_name", "java security code");

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import java.io.IOException;
99

1010
import org.apache.commons.lang.StringUtils;
11+
import org.joychou.config.WebConfig;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
13-
import org.springframework.beans.factory.annotation.Value;
1414
import org.springframework.util.AntPathMatcher;
1515
import org.springframework.util.PathMatcher;
1616

@@ -25,18 +25,6 @@
2525
@WebFilter(filterName = "referFilter", urlPatterns = "/*")
2626
public class HttpFilter implements Filter {
2727

28-
@Value("${joychou.security.referer.enabled}")
29-
private Boolean referSecEnabled = false;
30-
31-
@Value("${joychou.security.jsonp.callback}")
32-
private String[] callbacks;
33-
34-
@Value("${joychou.security.referer.hostwhitelist}")
35-
private String[] referWhitelist;
36-
37-
@Value("${joychou.security.referer.uri}")
38-
private String[] referUris;
39-
4028
@Override
4129
public void init(FilterConfig filterConfig) throws ServletException {
4230

@@ -54,20 +42,20 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain filter
5442
String refer = request.getHeader("referer");
5543
PathMatcher matcher = new AntPathMatcher();
5644
boolean isMatch = false;
57-
for (String uri: referUris) {
45+
for (String uri: WebConfig.getReferUris()) {
5846
if ( matcher.match (uri, request.getRequestURI()) ) {
5947
isMatch = true;
6048
break;
6149
}
6250
}
6351

6452
if (isMatch) {
65-
if (referSecEnabled) {
53+
if (WebConfig.getReferSecEnabled()) {
6654
// Check referer for all GET requests with callback parameters.
67-
for (String callback: callbacks) {
55+
for (String callback: WebConfig.getCallbacks()) {
6856
if (request.getMethod().equals("GET") && StringUtils.isNotBlank(request.getParameter(callback)) ){
6957
// If the check of referer fails, a 403 forbidden error page will be returned.
70-
if (!SecurityUtil.checkURLbyEndsWith(refer, referWhitelist)){
58+
if (!SecurityUtil.checkURLbyEndsWith(refer, WebConfig.getReferWhitelist())){
7159
logger.info("[-] URL: " + request.getRequestURL() + "?" + request.getQueryString() + "\t" + "Referer: " + refer);
7260
response.sendRedirect("https://test.joychou.org/error3.html");
7361
return;

src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<p>Hello <span th:text="${user}"></span>.</p>
9-
<p>Welcome to login java-sec-code application.</p>
9+
<p>Welcome to login java-sec-code application. <a th:href="@{/appInfo}">Application Infomation</a></p>
1010
<a th:href="@{/logout}">logout</a>
1111
</body>
1212
</html>

0 commit comments

Comments
 (0)