Skip to content

Commit 05ae55e

Browse files
authored
Merge pull request JoyChou93#9 from Anemone95/master
Add more vulnerable code snippets about SQLi(mybatis) and XSS(reflect, stored)
2 parents d0ece30 + da5ea84 commit 05ae55e

File tree

6 files changed

+120
-19
lines changed

6 files changed

+120
-19
lines changed

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import org.joychou.mapper.UserMapper;
55
import org.joychou.dao.User;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.web.bind.annotation.*;
89

9-
import javax.servlet.http.HttpServletRequest;
1010
import java.sql.*;
11+
import java.util.List;
1112

1213

1314
/**
@@ -16,14 +17,18 @@
1617
* @desc SQL Injection
1718
*/
1819

20+
@SuppressWarnings("Duplicates")
1921
@RestController
2022
@RequestMapping("/sqli")
2123
public class SQLI {
2224

2325
private static String driver = "com.mysql.jdbc.Driver";
24-
private static String url = "jdbc:mysql://localhost:3306/java_sec_code";
25-
private static String user = "root";
26-
private static String password = "woshishujukumima";
26+
@Value("${spring.datasource.url}")
27+
private String url;
28+
@Value("${spring.datasource.username}")
29+
private String user;
30+
@Value("${spring.datasource.password}")
31+
private String password;
2732

2833
@Autowired
2934
private UserMapper userMapper;
@@ -36,7 +41,7 @@ public class SQLI {
3641
* @param username username
3742
*/
3843
@RequestMapping("/jdbc/vul")
39-
public static String jdbc_sqli_vul(@RequestParam("username") String username){
44+
public String jdbc_sqli_vul(@RequestParam("username") String username){
4045
String result = "";
4146
try {
4247
Class.forName(driver);
@@ -88,7 +93,7 @@ public static String jdbc_sqli_vul(@RequestParam("username") String username){
8893
* @param username username
8994
*/
9095
@RequestMapping("/jdbc/sec")
91-
public static String jdbc_sqli_sec(@RequestParam("username") String username){
96+
public String jdbc_sqli_sec(@RequestParam("username") String username){
9297

9398
String result = "";
9499
try {
@@ -134,6 +139,28 @@ public static String jdbc_sqli_sec(@RequestParam("username") String username){
134139
return result;
135140
}
136141

142+
/**
143+
* vul code
144+
* http://localhost:8080/sqli/mybatis/vul01?username=joychou' or '1'='1
145+
*
146+
* @param username username
147+
*/
148+
@GetMapping("/mybatis/vul01")
149+
public List<User> mybatis_vul1(@RequestParam("username") String username) {
150+
return userMapper.findByUserNameVul(username);
151+
}
152+
153+
/**
154+
* vul code
155+
* http://localhost:8080/sqli/mybatis/vul02?username=joychou' or '1'='1' %23
156+
*
157+
* @param username username
158+
*/
159+
@GetMapping("/mybatis/vul02")
160+
public List<User> mybatis_vul2(@RequestParam("username") String username) {
161+
return userMapper.findByUserNameVul2(username);
162+
}
163+
137164

138165
/**
139166
* security code
@@ -142,20 +169,18 @@ public static String jdbc_sqli_sec(@RequestParam("username") String username){
142169
* @param username username
143170
*/
144171
@GetMapping("/mybatis/sec01")
145-
public User mybatis_vul1(@RequestParam("username") String username) {
172+
public User mybatis_sec1(@RequestParam("username") String username) {
146173
return userMapper.findByUserName(username);
147174
}
148175

149-
150-
151176
/**
152177
* security code
153178
* http://localhost:8080/sqli/mybatis/sec02?id=1
154179
*
155180
* @param id id
156181
*/
157182
@GetMapping("/mybatis/sec02")
158-
public User mybatis_v(@RequestParam("id") Integer id) {
183+
public User mybatis_sec2(@RequestParam("id") Integer id) {
159184
return userMapper.findById(id);
160185
}
161186

@@ -165,7 +190,7 @@ public User mybatis_v(@RequestParam("id") Integer id) {
165190
* http://localhost:8080/sqli/mybatis/sec03
166191
**/
167192
@GetMapping("/mybatis/sec03")
168-
public User mybatis_vul2() {
193+
public User mybatis_sec3() {
169194
return userMapper.OrderByUsername();
170195
}
171196

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

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

33
import org.apache.commons.lang.StringUtils;
4+
import org.joychou.dao.User;
5+
import org.joychou.mapper.UserMapper;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
48
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.CookieValue;
510
import org.springframework.web.bind.annotation.RequestMapping;
611
import org.springframework.web.bind.annotation.ResponseBody;
712

13+
import javax.annotation.Resource;
14+
import javax.servlet.http.Cookie;
815
import javax.servlet.http.HttpServletRequest;
16+
import javax.servlet.http.HttpServletResponse;
17+
import java.sql.Connection;
18+
import java.sql.DriverManager;
19+
import java.sql.Statement;
920

1021
/**
1122
* @author JoyChou ([email protected])
@@ -16,15 +27,59 @@
1627
@Controller
1728
@RequestMapping("/xss")
1829
public class XSS {
19-
@RequestMapping("/print")
30+
31+
/**
32+
* Vul Code.
33+
* ReflectXSS
34+
* http://localhost:8080/xss/reflect?xss=<script>alert(1)</script>
35+
*
36+
* @param xss unescape string
37+
*/
38+
@RequestMapping("/reflect")
39+
@ResponseBody
40+
public static String reflect(String xss)
41+
{
42+
return xss;
43+
}
44+
45+
/**
46+
* Vul Code.
47+
* StoredXSS Step1
48+
* http://localhost:8080/xss/stored/store?xss=<script>alert(1)</script>
49+
*
50+
* @param xss unescape string
51+
*/
52+
@RequestMapping("/stored/store")
2053
@ResponseBody
21-
public static String ssrf_URLConnection(HttpServletRequest request)
54+
public String store(String xss, HttpServletResponse response)
2255
{
23-
String con = request.getParameter("con");
24-
return con;
56+
Cookie cookie = new Cookie("xss", xss);
57+
response.addCookie(cookie);
58+
return "Set param into cookie";
59+
}
2560

26-
// fix code
27-
// return encode(con);
61+
/**
62+
* Vul Code.
63+
* StoredXSS Step2
64+
* http://localhost:8080/xss/stored/show
65+
*
66+
* @param xss unescape string
67+
*/
68+
@RequestMapping("/stored/show")
69+
@ResponseBody
70+
public String show(@CookieValue("xss") String xss)
71+
{
72+
return xss;
73+
}
74+
/**
75+
* safe Code.
76+
* http://localhost:8080/xss/safe
77+
*
78+
*/
79+
@RequestMapping("/safe")
80+
@ResponseBody
81+
public static String safe(String xss){
82+
return encode(xss);
2883
}
2984

3085
public static String encode(String origin) {

src/main/java/org/joychou/mapper/UserMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.apache.ibatis.annotations.Select;
66
import org.joychou.dao.User;
77

8+
import java.util.List;
9+
810
@Mapper
911
public interface UserMapper {
1012

@@ -15,7 +17,13 @@ public interface UserMapper {
1517
@Select("select * from users where username = #{username}")
1618
User findByUserName(@Param("username") String username);
1719

20+
@Select("select * from users where username = '${username}'")
21+
List<User> findByUserNameVul(@Param("username") String username);
22+
23+
List<User> findByUserNameVul2(String username);
24+
1825
User findById(Integer id);
1926

2027
User OrderByUsername();
28+
2129
}

src/main/resources/application.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11

2-
spring.datasource.url=jdbc:mysql://localhost:3306/java_sec_code?AllowPublicKeyRetrieval=true&useSSL=false
2+
spring.datasource.url=jdbc:mysql://localhost:3306/java_sec_code?AllowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
33
spring.datasource.username=root
44
spring.datasource.password=woshishujukumima
55
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
66
mybatis.mapper-locations=classpath:mapper/*.xml
77

8-
98
# Spring Boot Actuator Vulnerable Config
109
management.security.enabled=false
1110
# logging.config=classpath:logback-online.xml

src/main/resources/create_db.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
USE `java_sec_code`;
2+
CREATE TABLE IF NOT EXISTS `users`(
3+
`id` INT UNSIGNED AUTO_INCREMENT,
4+
`username` VARCHAR(255) NOT NULL,
5+
`password` VARCHAR(255) NOT NULL,
6+
PRIMARY KEY (`id`)
7+
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
8+
INSERT INTO `users` VALUES (1, 'admin', 'admin123');
9+
INSERT INTO `users` VALUES (2, 'joychou', 'joychou123');

src/main/resources/mapper/UserMapper.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
<!--select * from users where username = #{username}-->
1414
<!--</select>-->
1515

16+
<select id="findByUserNameVul2" parameterType="String" resultMap="User">
17+
select * from users where username like '%${_parameter}%'
18+
</select>
19+
1620
<select id="findById" resultMap="User">
1721
select * from users where id = #{id}
1822
</select>
1923

24+
2025
<select id="OrderByUsername" resultMap="User">
2126
select * from users order by id asc limit 1
2227
</select>

0 commit comments

Comments
 (0)