| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.samples; |
| 7 |
|
| 8 |
import java.io.BufferedReader; |
| 9 |
import java.io.InputStreamReader; |
| 10 |
import java.sql.Connection; |
| 11 |
import java.sql.DriverManager; |
| 12 |
import java.sql.PreparedStatement; |
| 13 |
import java.sql.ResultSet; |
| 14 |
import java.sql.SQLException; |
| 15 |
import java.sql.Statement; |
| 16 |
|
| 17 |
/** |
| 18 |
* SQL Injection is a common security vulnerability for applications that use |
| 19 |
* database. It is one of the most common security vulnerabilities for web |
| 20 |
* applications today. This sample application shows how SQL injection works, |
| 21 |
* and how to protect the application from it. |
| 22 |
*/ |
| 23 |
public class SQLInjection { |
| 24 |
|
| 25 |
private Connection conn; |
| 26 |
private Statement stat; |
| 27 |
|
| 28 |
/** |
| 29 |
* This method is called when executing this sample application from the |
| 30 |
* command line. |
| 31 |
* |
| 32 |
* @param args the command line parameters |
| 33 |
* @throws Exception on failure |
| 34 |
*/ |
| 35 |
public static void main(String... args) throws Exception { |
| 36 |
new SQLInjection().run("org.h2.Driver", |
| 37 |
"jdbc:h2:./test", "sa", "sa"); |
| 38 |
// new SQLInjection().run("org.postgresql.Driver", |
| 39 |
// "jdbc:postgresql:jpox2", "sa", "sa"); |
| 40 |
// new SQLInjection().run("com.mysql.cj.jdbc.Driver", |
| 41 |
// "jdbc:mysql://localhost/test", "sa", "sa"); |
| 42 |
// new SQLInjection().run("org.hsqldb.jdbcDriver", |
| 43 |
// "jdbc:hsqldb:test", "sa", ""); |
| 44 |
// new SQLInjection().run( |
| 45 |
// "org.apache.derby.iapi.jdbc.AutoloadedDriver", |
| 46 |
// "jdbc:derby:test3;create=true", "sa", "sa"); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Run the test against the specified database. |
| 51 |
* |
| 52 |
* @param driver the JDBC driver name |
| 53 |
* @param url the database URL |
| 54 |
* @param user the user name |
| 55 |
* @param password the password |
| 56 |
*/ |
| 57 |
void run(String driver, String url, String user, String password) |
| 58 |
throws Exception { |
| 59 |
Class.forName(driver); |
| 60 |
conn = DriverManager.getConnection(url, user, password); |
| 61 |
stat = conn.createStatement(); |
| 62 |
try { |
| 63 |
stat.execute("DROP TABLE USERS"); |
| 64 |
} catch (SQLException e) { |
| 65 |
// ignore |
| 66 |
} |
| 67 |
stat.execute("CREATE TABLE USERS(ID INT PRIMARY KEY, " + |
| 68 |
"NAME VARCHAR(255), PASSWORD VARCHAR(255))"); |
| 69 |
stat.execute("INSERT INTO USERS VALUES(1, 'admin', 'super')"); |
| 70 |
stat.execute("INSERT INTO USERS VALUES(2, 'guest', '123456')"); |
| 71 |
stat.execute("INSERT INTO USERS VALUES(3, 'test', 'abc')"); |
| 72 |
|
| 73 |
loginByNameInsecure(); |
| 74 |
|
| 75 |
if (url.startsWith("jdbc:h2:")) { |
| 76 |
loginStoredProcedureInsecure(); |
| 77 |
limitRowAccess(); |
| 78 |
} |
| 79 |
|
| 80 |
loginByNameSecure(); |
| 81 |
|
| 82 |
if (url.startsWith("jdbc:h2:")) { |
| 83 |
stat.execute("SET ALLOW_LITERALS NONE"); |
| 84 |
stat.execute("SET ALLOW_LITERALS NUMBERS"); |
| 85 |
stat.execute("SET ALLOW_LITERALS ALL"); |
| 86 |
} |
| 87 |
|
| 88 |
loginByIdInsecure(); |
| 89 |
loginByIdSecure(); |
| 90 |
|
| 91 |
try { |
| 92 |
stat.execute("DROP TABLE ITEMS"); |
| 93 |
} catch (SQLException e) { |
| 94 |
// ignore |
| 95 |
} |
| 96 |
|
| 97 |
stat.execute("CREATE TABLE ITEMS(ID INT PRIMARY KEY, " + |
| 98 |
"NAME VARCHAR(255), ACTIVE INT)"); |
| 99 |
stat.execute("INSERT INTO ITEMS VALUES(0, 'XBox', 0)"); |
| 100 |
stat.execute("INSERT INTO ITEMS VALUES(1, 'XBox 360', 1)"); |
| 101 |
stat.execute("INSERT INTO ITEMS VALUES(2, 'PlayStation 1', 0)"); |
| 102 |
stat.execute("INSERT INTO ITEMS VALUES(3, 'PlayStation 2', 1)"); |
| 103 |
stat.execute("INSERT INTO ITEMS VALUES(4, 'PlayStation 3', 1)"); |
| 104 |
|
| 105 |
listActiveItems(); |
| 106 |
|
| 107 |
if (url.startsWith("jdbc:h2:")) { |
| 108 |
stat.execute("DROP CONSTANT IF EXISTS TYPE_INACTIVE"); |
| 109 |
stat.execute("DROP CONSTANT IF EXISTS TYPE_ACTIVE"); |
| 110 |
stat.execute("CREATE CONSTANT TYPE_INACTIVE VALUE 0"); |
| 111 |
stat.execute("CREATE CONSTANT TYPE_ACTIVE VALUE 1"); |
| 112 |
listActiveItemsUsingConstants(); |
| 113 |
} |
| 114 |
|
| 115 |
listItemsSortedInsecure(); |
| 116 |
listItemsSortedSecure(); |
| 117 |
|
| 118 |
if (url.startsWith("jdbc:h2:")) { |
| 119 |
listItemsSortedSecureParam(); |
| 120 |
storePasswordHashWithSalt(); |
| 121 |
} |
| 122 |
|
| 123 |
conn.close(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Simulate a login using an insecure method. |
| 128 |
*/ |
| 129 |
void loginByNameInsecure() throws Exception { |
| 130 |
System.out.println("Insecure Systems Inc. - login"); |
| 131 |
String name = input("Name?"); |
| 132 |
String password = input("Password?"); |
| 133 |
ResultSet rs = stat.executeQuery("SELECT * FROM USERS WHERE " + |
| 134 |
"NAME='" + name + "' AND PASSWORD='" + password + "'"); |
| 135 |
if (rs.next()) { |
| 136 |
System.out.println("Welcome!"); |
| 137 |
} else { |
| 138 |
System.out.println("Access denied!"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Utility method to get a user record given the user name and password. |
| 144 |
* This method is secure. |
| 145 |
* |
| 146 |
* @param conn the database connection |
| 147 |
* @param userName the user name |
| 148 |
* @param password the password |
| 149 |
* @return a result set with the user record if the password matches |
| 150 |
* @throws Exception on failure |
| 151 |
*/ |
| 152 |
public static ResultSet getUser(Connection conn, String userName, |
| 153 |
String password) throws Exception { |
| 154 |
PreparedStatement prep = conn.prepareStatement( |
| 155 |
"SELECT * FROM USERS WHERE NAME=? AND PASSWORD=?"); |
| 156 |
prep.setString(1, userName); |
| 157 |
prep.setString(2, password); |
| 158 |
return prep.executeQuery(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Utility method to change a password of a user. |
| 163 |
* This method is secure, except that the old password is not checked. |
| 164 |
* |
| 165 |
* @param conn the database connection |
| 166 |
* @param userName the user name |
| 167 |
* @param password the password |
| 168 |
* @return the new password |
| 169 |
* @throws Exception on failure |
| 170 |
*/ |
| 171 |
public static String changePassword(Connection conn, String userName, |
| 172 |
String password) throws Exception { |
| 173 |
PreparedStatement prep = conn.prepareStatement( |
| 174 |
"UPDATE USERS SET PASSWORD=? WHERE NAME=?"); |
| 175 |
prep.setString(1, password); |
| 176 |
prep.setString(2, userName); |
| 177 |
prep.executeUpdate(); |
| 178 |
return password; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Simulate a login using an insecure method. |
| 183 |
* A stored procedure is used here. |
| 184 |
*/ |
| 185 |
void loginStoredProcedureInsecure() throws Exception { |
| 186 |
System.out.println("Insecure Systems Inc. - login using a stored procedure"); |
| 187 |
stat.execute("CREATE ALIAS IF NOT EXISTS GET_USER FOR 'org.h2.samples.SQLInjection.getUser'"); |
| 188 |
stat.execute("CREATE ALIAS IF NOT EXISTS CHANGE_PASSWORD FOR 'org.h2.samples.SQLInjection.changePassword'"); |
| 189 |
String name = input("Name?"); |
| 190 |
String password = input("Password?"); |
| 191 |
ResultSet rs = stat.executeQuery( |
| 192 |
"CALL GET_USER('" + name + "', '" + password + "')"); |
| 193 |
if (rs.next()) { |
| 194 |
System.out.println("Welcome!"); |
| 195 |
} else { |
| 196 |
System.out.println("Access denied!"); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Simulate a login using a secure method. |
| 202 |
*/ |
| 203 |
void loginByNameSecure() throws Exception { |
| 204 |
System.out.println("Secure Systems Inc. - login using placeholders"); |
| 205 |
String name = input("Name?"); |
| 206 |
String password = input("Password?"); |
| 207 |
PreparedStatement prep = conn.prepareStatement( |
| 208 |
"SELECT * FROM USERS WHERE " + |
| 209 |
"NAME=? AND PASSWORD=?"); |
| 210 |
prep.setString(1, name); |
| 211 |
prep.setString(2, password); |
| 212 |
ResultSet rs = prep.executeQuery(); |
| 213 |
if (rs.next()) { |
| 214 |
System.out.println("Welcome!"); |
| 215 |
} else { |
| 216 |
System.out.println("Access denied!"); |
| 217 |
} |
| 218 |
rs.close(); |
| 219 |
prep.close(); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Sample code to limit access only to specific rows. |
| 224 |
*/ |
| 225 |
void limitRowAccess() throws Exception { |
| 226 |
System.out.println("Secure Systems Inc. - limit row access"); |
| 227 |
stat.execute("DROP TABLE IF EXISTS SESSION_USER"); |
| 228 |
stat.execute("CREATE TABLE SESSION_USER(ID INT, USER INT)"); |
| 229 |
stat.execute("DROP VIEW IF EXISTS MY_USER"); |
| 230 |
stat.execute("CREATE VIEW MY_USER AS " + |
| 231 |
"SELECT U.* FROM SESSION_USER S, USERS U " + |
| 232 |
"WHERE S.ID=SESSION_ID() AND S.USER=U.ID"); |
| 233 |
stat.execute("INSERT INTO SESSION_USER VALUES(SESSION_ID(), 1)"); |
| 234 |
ResultSet rs = stat.executeQuery("SELECT ID, NAME FROM MY_USER"); |
| 235 |
while (rs.next()) { |
| 236 |
System.out.println(rs.getString(1) + ": " + rs.getString(2)); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Simulate a login using an insecure method. |
| 242 |
*/ |
| 243 |
void loginByIdInsecure() throws Exception { |
| 244 |
System.out.println("Half Secure Systems Inc. - login by id"); |
| 245 |
String id = input("User ID?"); |
| 246 |
String password = input("Password?"); |
| 247 |
try { |
| 248 |
PreparedStatement prep = conn.prepareStatement( |
| 249 |
"SELECT * FROM USERS WHERE " + |
| 250 |
"ID=" + id + " AND PASSWORD=?"); |
| 251 |
prep.setString(1, password); |
| 252 |
ResultSet rs = prep.executeQuery(); |
| 253 |
if (rs.next()) { |
| 254 |
System.out.println("Welcome!"); |
| 255 |
} else { |
| 256 |
System.out.println("Access denied!"); |
| 257 |
} |
| 258 |
rs.close(); |
| 259 |
prep.close(); |
| 260 |
} catch (SQLException e) { |
| 261 |
System.out.println(e); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Simulate a login using a secure method. |
| 267 |
*/ |
| 268 |
void loginByIdSecure() throws Exception { |
| 269 |
System.out.println("Secure Systems Inc. - login by id"); |
| 270 |
String id = input("User ID?"); |
| 271 |
String password = input("Password?"); |
| 272 |
try { |
| 273 |
PreparedStatement prep = conn.prepareStatement( |
| 274 |
"SELECT * FROM USERS WHERE " + |
| 275 |
"ID=? AND PASSWORD=?"); |
| 276 |
prep.setInt(1, Integer.parseInt(id)); |
| 277 |
prep.setString(2, password); |
| 278 |
ResultSet rs = prep.executeQuery(); |
| 279 |
if (rs.next()) { |
| 280 |
System.out.println("Welcome!"); |
| 281 |
} else { |
| 282 |
System.out.println("Access denied!"); |
| 283 |
} |
| 284 |
rs.close(); |
| 285 |
prep.close(); |
| 286 |
} catch (Exception e) { |
| 287 |
System.out.println(e); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* List active items. |
| 293 |
* The method uses the hard coded value '1', and therefore the database |
| 294 |
* can not verify if the SQL statement was constructed with user |
| 295 |
* input or not. |
| 296 |
*/ |
| 297 |
void listActiveItems() throws Exception { |
| 298 |
System.out.println("Half Secure Systems Inc. - list active items"); |
| 299 |
ResultSet rs = stat.executeQuery( |
| 300 |
"SELECT NAME FROM ITEMS WHERE ACTIVE=1"); |
| 301 |
while (rs.next()) { |
| 302 |
System.out.println("Name: " + rs.getString(1)); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* List active items. |
| 308 |
* The method uses a constant, and therefore the database |
| 309 |
* knows it does not contain user input. |
| 310 |
*/ |
| 311 |
void listActiveItemsUsingConstants() throws Exception { |
| 312 |
System.out.println("Secure Systems Inc. - list active items"); |
| 313 |
ResultSet rs = stat.executeQuery( |
| 314 |
"SELECT NAME FROM ITEMS WHERE ACTIVE=TYPE_ACTIVE"); |
| 315 |
while (rs.next()) { |
| 316 |
System.out.println("Name: " + rs.getString(1)); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* List items using a specified sort order. |
| 322 |
* The method is not secure as user input is used to construct the |
| 323 |
* SQL statement. |
| 324 |
*/ |
| 325 |
void listItemsSortedInsecure() throws Exception { |
| 326 |
System.out.println("Insecure Systems Inc. - list items"); |
| 327 |
String order = input("order (id, name)?"); |
| 328 |
try { |
| 329 |
ResultSet rs = stat.executeQuery( |
| 330 |
"SELECT ID, NAME FROM ITEMS ORDER BY " + order); |
| 331 |
while (rs.next()) { |
| 332 |
System.out.println(rs.getString(1) + ": " + rs.getString(2)); |
| 333 |
} |
| 334 |
} catch (SQLException e) { |
| 335 |
System.out.println(e); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* List items using a specified sort order. |
| 341 |
* The method is secure as the user input is validated before use. |
| 342 |
* However the database has no chance to verify this. |
| 343 |
*/ |
| 344 |
void listItemsSortedSecure() throws Exception { |
| 345 |
System.out.println("Secure Systems Inc. - list items"); |
| 346 |
String order = input("order (id, name)?"); |
| 347 |
if (!order.matches("[a-zA-Z0-9_]*")) { |
| 348 |
order = "id"; |
| 349 |
} |
| 350 |
try { |
| 351 |
ResultSet rs = stat.executeQuery( |
| 352 |
"SELECT ID, NAME FROM ITEMS ORDER BY " + order); |
| 353 |
while (rs.next()) { |
| 354 |
System.out.println(rs.getString(1) + ": " + rs.getString(2)); |
| 355 |
} |
| 356 |
} catch (SQLException e) { |
| 357 |
System.out.println(e); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* List items using a specified sort order. |
| 363 |
* The method is secure as a parameterized statement is used. |
| 364 |
*/ |
| 365 |
void listItemsSortedSecureParam() throws Exception { |
| 366 |
System.out.println("Secure Systems Inc. - list items"); |
| 367 |
String order = input("order (1, 2, -1, -2)?"); |
| 368 |
PreparedStatement prep = conn.prepareStatement( |
| 369 |
"SELECT ID, NAME FROM ITEMS ORDER BY ?"); |
| 370 |
try { |
| 371 |
prep.setInt(1, Integer.parseInt(order)); |
| 372 |
ResultSet rs = prep.executeQuery(); |
| 373 |
while (rs.next()) { |
| 374 |
System.out.println(rs.getString(1) + ": " + rs.getString(2)); |
| 375 |
} |
| 376 |
rs.close(); |
| 377 |
} catch (Exception e) { |
| 378 |
System.out.println(e); |
| 379 |
} |
| 380 |
prep.close(); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* This method creates a one way hash from the password |
| 385 |
* (using a random salt), and stores this information instead of the |
| 386 |
* password. |
| 387 |
*/ |
| 388 |
void storePasswordHashWithSalt() throws Exception { |
| 389 |
System.out.println("Very Secure Systems Inc. - login"); |
| 390 |
stat.execute("DROP TABLE IF EXISTS USERS2"); |
| 391 |
stat.execute("CREATE TABLE USERS2(ID INT PRIMARY KEY, " + |
| 392 |
"NAME VARCHAR, SALT BINARY, HASH BINARY)"); |
| 393 |
stat.execute("INSERT INTO USERS2 VALUES" + |
| 394 |
"(1, 'admin', SECURE_RAND(16), NULL)"); |
| 395 |
stat.execute("DROP CONSTANT IF EXISTS HASH_ITERATIONS"); |
| 396 |
stat.execute("DROP CONSTANT IF EXISTS HASH_ALGORITHM"); |
| 397 |
stat.execute("CREATE CONSTANT HASH_ITERATIONS VALUE 100"); |
| 398 |
stat.execute("CREATE CONSTANT HASH_ALGORITHM VALUE 'SHA256'"); |
| 399 |
stat.execute("UPDATE USERS2 SET " + |
| 400 |
"HASH=HASH(HASH_ALGORITHM, " + |
| 401 |
"STRINGTOUTF8('abc' || SALT), HASH_ITERATIONS) " + |
| 402 |
"WHERE ID=1"); |
| 403 |
String user = input("user?"); |
| 404 |
String password = input("password?"); |
| 405 |
stat.execute("SET ALLOW_LITERALS NONE"); |
| 406 |
PreparedStatement prep = conn.prepareStatement( |
| 407 |
"SELECT * FROM USERS2 WHERE NAME=? AND " + |
| 408 |
"HASH=HASH(HASH_ALGORITHM, STRINGTOUTF8(? || SALT), HASH_ITERATIONS)"); |
| 409 |
prep.setString(1, user); |
| 410 |
prep.setString(2, password); |
| 411 |
ResultSet rs = prep.executeQuery(); |
| 412 |
while (rs.next()) { |
| 413 |
System.out.println("name: " + rs.getString("NAME")); |
| 414 |
System.out.println("salt: " + rs.getString("SALT")); |
| 415 |
System.out.println("hash: " + rs.getString("HASH")); |
| 416 |
} |
| 417 |
rs.close(); |
| 418 |
prep.close(); |
| 419 |
stat.execute("SET ALLOW_LITERALS ALL"); |
| 420 |
stat.close(); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Utility method to get user input from the command line. |
| 425 |
* |
| 426 |
* @param prompt the prompt |
| 427 |
* @return the user input |
| 428 |
*/ |
| 429 |
String input(String prompt) throws Exception { |
| 430 |
System.out.print(prompt); |
| 431 |
return new BufferedReader(new InputStreamReader(System.in)).readLine(); |
| 432 |
} |
| 433 |
|
| 434 |
} |