11package org .joychou .controller ;
22
33
4- import org .springframework .stereotype .Controller ;
5- import org .springframework .web .bind .annotation .RequestMapping ;
6- import org .springframework .web .bind .annotation .ResponseBody ;
4+ import org .joychou .mapper .UserMapper ;
5+ import org .joychou .dao .User ;
6+ import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .web .bind .annotation .*;
78
89import javax .servlet .http .HttpServletRequest ;
910import java .sql .*;
1516 * @desc SQL Injection
1617 */
1718
18- @ Controller
19+ @ RestController
1920@ RequestMapping ("/sqli" )
2021public class SQLI {
2122
22- @ RequestMapping ("/jdbc" )
23- @ ResponseBody
24- public static String jdbc_sqli (HttpServletRequest request ){
23+ 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" ;
2527
26- String name = request .getParameter ("name" );
27- String driver = "com.mysql.jdbc.Driver" ;
28- String url = "jdbc:mysql://localhost:3306/sectest" ;
29- String user = "root" ;
30- String password = "woshishujukumima" ;
28+ @ Autowired
29+ private UserMapper userMapper ;
30+
31+
32+ /**
33+ * Vul Code.
34+ * http://localhost:8080/sqli/jdbc/vul?username=joychou
35+ *
36+ * @param username username
37+ */
38+ @ RequestMapping ("/jdbc/vul" )
39+ public static String jdbc_sqli_vul (@ RequestParam ("username" ) String username ){
3140 String result = "" ;
3241 try {
3342 Class .forName (driver );
34- Connection con = DriverManager .getConnection (url ,user ,password );
43+ Connection con = DriverManager .getConnection (url , user , password );
3544
3645 if (!con .isClosed ())
3746 System .out .println ("Connecting to Database successfully." );
3847
3948 // sqli vuln code 漏洞代码
4049 Statement statement = con .createStatement ();
41- String sql = "select * from users where name = '" + name + "'" ;
50+ String sql = "select * from users where username = '" + username + "'" ;
4251 System .out .println (sql );
4352 ResultSet rs = statement .executeQuery (sql );
4453
45- // fix code 用预处理修复SQL注入
46- // String sql = "select * from users where name = ?";
47- // PreparedStatement st = con.prepareStatement(sql);
48- // st.setString(1, name);
49- // System.out.println(st.toString()); // 预处理后的sql
50- // ResultSet rs = st.executeQuery();
5154
5255 System .out .println ("-----------------" );
5356
5457 while (rs .next ()){
55- String res_name = rs .getString ("name " );
58+ String res_name = rs .getString ("username " );
5659 String res_pwd = rs .getString ("password" );
5760 result += res_name + ": " + res_pwd + "\n " ;
5861 System .out .println (res_name + ": " + res_pwd );
@@ -77,4 +80,94 @@ public static String jdbc_sqli(HttpServletRequest request){
7780 return result ;
7881 }
7982
83+
84+ /**
85+ * Security Code.
86+ * http://localhost:8080/sqli/jdbc/sec?username=joychou
87+ *
88+ * @param username username
89+ */
90+ @ RequestMapping ("/jdbc/sec" )
91+ public static String jdbc_sqli_sec (@ RequestParam ("username" ) String username ){
92+
93+ String result = "" ;
94+ try {
95+ Class .forName (driver );
96+ Connection con = DriverManager .getConnection (url , user , password );
97+
98+ if (!con .isClosed ())
99+ System .out .println ("Connecting to Database successfully." );
100+
101+
102+ // fix code
103+ String sql = "select * from users where username = ?" ;
104+ PreparedStatement st = con .prepareStatement (sql );
105+ st .setString (1 , username );
106+ System .out .println (st .toString ()); // sql after prepare statement
107+ ResultSet rs = st .executeQuery ();
108+
109+ System .out .println ("-----------------" );
110+
111+ while (rs .next ()){
112+ String res_name = rs .getString ("username" );
113+ String res_pwd = rs .getString ("password" );
114+ result += res_name + ": " + res_pwd + "\n " ;
115+ System .out .println (res_name + ": " + res_pwd );
116+
117+ }
118+ rs .close ();
119+ con .close ();
120+
121+
122+ }catch (ClassNotFoundException e ) {
123+ System .out .println ("Sorry,can`t find the Driver!" );
124+ e .printStackTrace ();
125+ }catch (SQLException e ) {
126+ e .printStackTrace ();
127+ }catch (Exception e ) {
128+ e .printStackTrace ();
129+
130+ }finally {
131+ System .out .println ("-----------------" );
132+ System .out .println ("Connect database done." );
133+ }
134+ return result ;
135+ }
136+
137+
138+ /**
139+ * security code
140+ * http://localhost:8080/sqli/mybatis/sec01?username=joychou
141+ *
142+ * @param username username
143+ */
144+ @ GetMapping ("/mybatis/sec01" )
145+ public User mybatis_vul1 (@ RequestParam ("username" ) String username ) {
146+ return userMapper .findByUserName (username );
147+ }
148+
149+
150+
151+ /**
152+ * security code
153+ * http://localhost:8080/sqli/mybatis/sec02?id=1
154+ *
155+ * @param id id
156+ */
157+ @ GetMapping ("/mybatis/sec02" )
158+ public User mybatis_v (@ RequestParam ("id" ) Integer id ) {
159+ return userMapper .findById (id );
160+ }
161+
162+
163+ /**
164+ * security code
165+ * http://localhost:8080/sqli/mybatis/sec03
166+ **/
167+ @ GetMapping ("/mybatis/sec03" )
168+ public User mybatis_vul2 () {
169+ return userMapper .OrderByUsername ();
170+ }
171+
172+
80173}
0 commit comments