Skip to content

Commit 59a72ef

Browse files
committed
19/10/15 add more xss&sql vuln code
1 parent d0ece30 commit 59a72ef

File tree

6 files changed

+121
-18
lines changed

6 files changed

+121
-18
lines changed

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

+37-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
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;
8+
import org.springframework.context.annotation.Configuration;
79
import org.springframework.web.bind.annotation.*;
810

911
import javax.servlet.http.HttpServletRequest;
1012
import java.sql.*;
13+
import java.util.List;
1114

1215

1316
/**
@@ -16,14 +19,18 @@
1619
* @desc SQL Injection
1720
*/
1821

22+
@SuppressWarnings("Duplicates")
1923
@RestController
2024
@RequestMapping("/sqli")
2125
public class SQLI {
2226

2327
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";
28+
@Value("${spring.datasource.url}")
29+
private String url;
30+
@Value("${spring.datasource.username}")
31+
private String user;
32+
@Value("${spring.datasource.password}")
33+
private String password;
2734

2835
@Autowired
2936
private UserMapper userMapper;
@@ -36,7 +43,7 @@ public class SQLI {
3643
* @param username username
3744
*/
3845
@RequestMapping("/jdbc/vul")
39-
public static String jdbc_sqli_vul(@RequestParam("username") String username){
46+
public String jdbc_sqli_vul(@RequestParam("username") String username){
4047
String result = "";
4148
try {
4249
Class.forName(driver);
@@ -88,7 +95,7 @@ public static String jdbc_sqli_vul(@RequestParam("username") String username){
8895
* @param username username
8996
*/
9097
@RequestMapping("/jdbc/sec")
91-
public static String jdbc_sqli_sec(@RequestParam("username") String username){
98+
public String jdbc_sqli_sec(@RequestParam("username") String username){
9299

93100
String result = "";
94101
try {
@@ -134,6 +141,28 @@ public static String jdbc_sqli_sec(@RequestParam("username") String username){
134141
return result;
135142
}
136143

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

138167
/**
139168
* security code
@@ -142,20 +171,18 @@ public static String jdbc_sqli_sec(@RequestParam("username") String username){
142171
* @param username username
143172
*/
144173
@GetMapping("/mybatis/sec01")
145-
public User mybatis_vul1(@RequestParam("username") String username) {
174+
public User mybatis_sec1(@RequestParam("username") String username) {
146175
return userMapper.findByUserName(username);
147176
}
148177

149-
150-
151178
/**
152179
* security code
153180
* http://localhost:8080/sqli/mybatis/sec02?id=1
154181
*
155182
* @param id id
156183
*/
157184
@GetMapping("/mybatis/sec02")
158-
public User mybatis_v(@RequestParam("id") Integer id) {
185+
public User mybatis_sec2(@RequestParam("id") Integer id) {
159186
return userMapper.findById(id);
160187
}
161188

@@ -165,7 +192,7 @@ public User mybatis_v(@RequestParam("id") Integer id) {
165192
* http://localhost:8080/sqli/mybatis/sec03
166193
**/
167194
@GetMapping("/mybatis/sec03")
168-
public User mybatis_vul2() {
195+
public User mybatis_sec3() {
169196
return userMapper.OrderByUsername();
170197
}
171198

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

+61-6
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

+8
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

+1-2
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

+9
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

+5
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)