-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSqlInjectionTest.java
More file actions
408 lines (360 loc) · 15.5 KB
/
SqlInjectionTest.java
File metadata and controls
408 lines (360 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package vulnerabilities;
import dev.aikido.agent_api.vulnerabilities.sql_injection.Dialect;
import dev.aikido.agent_api.vulnerabilities.sql_injection.SqlDetector;
import org.junit.jupiter.api.Test;
import static dev.aikido.agent_api.vulnerabilities.sql_injection.SqlDetector.detectSqlInjection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SqlInjectionTest {
private void isNotSqlInjection(String sql, String input, String dialect) {
if ("mysql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("mysql"));
assertEquals(0, result, String.format("Expected no SQL injection for SQL: %s and input: %s", sql, input));
}
if ("postgresql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("postgresql"));
assertEquals(0, result, String.format("Expected no SQL injection for SQL: %s and input: %s", sql, input));
}
}
private void isSqlInjection(String sql, String input, String dialect) {
if ("mysql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("mysql"));
assertEquals(1, result, String.format("Expected SQL injection for SQL: %s and input: %s", sql, input));
}
if ("postgresql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("postgresql"));
assertEquals(1, result, String.format("Expected SQL injection for SQL: %s and input: %s", sql, input));
}
}
private void isSqlTokenizeError(String sql, String input, String dialect) {
if ("mysql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("mysql"));
assertEquals(3, result, String.format("Expected SQL tokenize error for SQL: %s and input: %s", sql, input));
}
if ("postgresql".equals(dialect) || "all".equals(dialect)) {
int result = detectSqlInjection(sql, input, new Dialect("postgresql"));
assertEquals(3, result, String.format("Expected SQL tokenize error for SQL: %s and input: %s", sql, input));
}
}
/**
* Removed tests :
* -> `I'm writting you` : Invalid SQL
* -> Moved a lot of the keywords/words together collection to BAD_SQL_COMMANDS.
* -> Removed the following GOOD_SQL_COMMANDS : "abcdefghijklmnop@hotmail.com", "steve@yahoo.com"
* Reason : This should never occur unencapsulated in query, results in 5 tokens or so being morphed into one.
*/
private static final String[] BAD_SQL_COMMANDS = {
"Roses are red insErt are blue",
"Roses are red cREATE are blue",
"Roses are red drop are blue",
"Roses are red updatE are blue",
"Roses are red SELECT are blue",
"Roses are red dataBASE are blue",
"Roses are red alter are blue",
"Roses are red grant are blue",
"Roses are red savepoint are blue",
"Roses are red commit are blue",
"Roses are red or blue",
"Roses are red and lovely",
"This is a group_concat_test",
"Termin;ate",
"Roses <> violets",
"Roses < Violets",
"Roses > Violets",
"Roses != Violets",
"Roses asks red truncates asks blue",
"Roses asks reddelete asks blue",
"Roses asks red WHEREis blue",
"Roses asks red ORis isAND",
"I was benchmark ing",
"We were delay ed",
"I will waitfor you"
};
// List of good SQL commands that should not be flagged as SQL injection
private static final String[] GOOD_SQL_COMMANDS = {
" ",
"#",
"'"
};
/*
List of SQL commands that are not considered injections
Moved ["'union' is not UNION", "UNION"], to IS_NOT_INJECTION : This is in fact, not an injection.
*/
private static final String[][] IS_NOT_INJECTION = {
{"'UNION 123' UNION \"UNION 123\"", "UNION 123"},
{"'union' is not \"UNION\"", "UNION!"},
{"\"UNION;\"", "UNION;"},
{"SELECT * FROM table", "*"},
{"\"COPY/*\"", "COPY/*"},
{"'union' is not \"UNION--\"", "UNION--"},
{"'union' is not UNION", "UNION"}
};
// List of SQL commands that are considered injections
private static final String[][] IS_INJECTION = {
{"UNTER;", "UNTER;"}
};
@Test
public void testBadSqlCommands() {
for (String sql : BAD_SQL_COMMANDS) {
isSqlInjection(sql, sql, "all");
}
}
@Test
public void testGoodSqlCommands() {
for (String sql : GOOD_SQL_COMMANDS) {
isNotSqlInjection(sql, sql, "all");
}
}
@Test
public void testIsInjection() {
for (String[] sqlPair : IS_INJECTION) {
isSqlInjection(sqlPair[0], sqlPair[1], "all");
}
}
@Test
public void testIsNotInjection() {
for (String[] sqlPair : IS_NOT_INJECTION) {
isNotSqlInjection(sqlPair[0], sqlPair[1], "all");
}
}
@Test
public void testShouldReturnEarly() {
// Test cases where the function should return True
// User input is empty
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", ""));
// User input is a single char"postgresql"acter
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "a"));
// User input is larger than query
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "SELECT * FROM users WHERE id = 1"));
// User input not in query
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "DELETE"));
// User input is alphanumerical
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users123", "users123"));
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users_123", "users_123"));
assertTrue(SqlDetector.shouldReturnEarly("SELECT __1 FROM users_123", "__1"));
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM table_name_is_fun_12", "table_name_is_fun_12"));
// User input is a valid comma-separated number list
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "1,2,3"));
// User input is a valid number
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "123"));
// User input is a valid number with spaces
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", " 123 "));
// User input is a valid number with commas
assertTrue(SqlDetector.shouldReturnEarly("SELECT * FROM users", "1, 2, 3"));
// Test cases where the function should return False
// User input is in query
assertFalse(SqlDetector.shouldReturnEarly("SELECT * FROM users", " users"));
// User input is a valid string in query
assertFalse(SqlDetector.shouldReturnEarly("SELECT * FROM users", "SELECT "));
// User input is a valid string in query with special characters
assertFalse(SqlDetector.shouldReturnEarly("SELECT * FROM users; DROP TABLE", "users; DROP TABLE"));
}
@Test
public void testAllowEscapeSequences() {
isNotSqlInjection("SELECT * FROM users WHERE id = '\nusers'", "\nusers", "all");
isNotSqlInjection("SELECT * FROM users WHERE id = '\rusers'", "\rusers", "all");
isNotSqlInjection("SELECT * FROM users WHERE id = '\tusers'", "\tusers", "all");
}
/**
* Marked "SELECT * FROM users WHERE id IN ('123')", "'123'" as not a sql injection, Reason :
* We replace '123' token with another token and the token count remains the same. This is also
* Not an actual SQL Injection so the algorithm is valid in it's reasoning.
*/
@Test
public void testUserInputInsideIn() {
isNotSqlInjection("SELECT * FROM users WHERE id IN ('123')", "'123'", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN (123)", "123", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN (123, 456)", "123", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN (123, 456)", "456", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN ('123')", "123", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN (13,14,15)", "13,14,15", "all");
isNotSqlInjection("SELECT * FROM users WHERE id IN (13, 14, 154)", "13, 14, 154", "all");
// Invalid query that should be detected as SQL injection:
isSqlInjection("SELECT * FROM users WHERE id IN (13, 14, 154) OR (1=1)", "13, 14, 154) OR (1=1", "all");
}
@Test
public void testCheckStringSafelyEscaped() {
// Invalid queries that should be detected as SQL injection:
isSqlInjection(
"SELECT * FROM comments WHERE comment = \"I\" \"m writing you\"",
"I\" \"m writing you", "all"
);
isSqlInjection("SELECT * FROM `comm`ents``", "`comm`ents", "all");
// Valid queries that should not be detected as SQL injection:
isNotSqlInjection(
"SELECT * FROM comments WHERE comment = \"I\\'m writing you\"", "I'm writing you", "all"
);
isNotSqlInjection(
"SELECT * FROM comments WHERE comment = 'I\"m writing you'", "I\"m writing you", "all"
);
isNotSqlInjection(
"SELECT * FROM comments WHERE comment = \"I\\`m writing you\"", "I`m writing you", "all"
);
isSqlTokenizeError(
"SELECT * FROM comments WHERE comment = 'I'm writing you'", "I'm writing you", "all"
);
// Positive example of same query:
isSqlInjection(
"SELECT * FROM comments WHERE comment = 'I'm writing you--'",
"I'm writing you--", "all"
);
isSqlInjection(
"SELECT * FROM comments WHERE comment = 'I'm writing you''",
"I'm writing you'", "all"
);
// Invalid query in postgres, tests fallback:
isSqlInjection("SELECT * FROM `comm` ents", "`comm` ents", "postgresql");
// MySQL Specific code:
isSqlInjection("SELECT * FROM `comm` ents", "`comm` ents", "mysql");
isNotSqlInjection("SELECT * FROM `comm'ents`", "comm'ents", "mysql");
}
@Test
public void testNotFlagSelectQueries() {
isNotSqlInjection("SELECT * FROM users WHERE id = 1", "SELECT", "all");
}
@Test
public void testNotFlagEscapedHash() {
isNotSqlInjection("SELECT * FROM hashtags WHERE name = '#hashtag'", "#hashtag", "all");
}
@Test
public void testCommentSameAsUserInput() {
isSqlInjection(
"SELECT * FROM hashtags WHERE name = '-- Query by name' -- Query by name",
"-- Query by name", "all"
);
}
@Test
public void testInputOccursInComment() {
isNotSqlInjection(
"SELECT * FROM hashtags WHERE name = 'name' -- Query by name", "name", "all"
);
}
@Test
public void testUserInputIsMultiline() {
isSqlInjection("SELECT * FROM users WHERE id = 'a'\nOR 1=1#'", "a'\nOR 1=1#", "mysql");
isNotSqlInjection("SELECT * FROM users WHERE id = 'a\nb\nc';", "a\nb\nc", "all");
}
@Test
public void testUserInputIsLongerThanQuery() {
isNotSqlInjection("SELECT * FROM users", "SELECT * FROM users WHERE id = 'a'", "all");
}
@Test
public void testMultilineQueries() {
isSqlInjection(
"""
SELECT * FROM `users`
WHERE id = 123
""",
"users`", "all"
);
isSqlInjection(
"""
SELECT *
FROM users
WHERE id = '1' OR 1=1
""",
"1' OR 1=1", "all"
);
isSqlInjection(
"""
SELECT *
FROM users
WHERE id = '1' OR 1=1
AND is_escaped = '1'' OR 1=1'
""",
"1' OR 1=1", "all"
);
isSqlInjection(
"""
SELECT *
FROM users
WHERE id = '1' OR 1=1
AND is_escaped = "1' OR 1=1"
""",
"1' OR 1=1", "all"
);
isNotSqlInjection(
"""
SELECT * FROM `users`
WHERE id = 123
""",
"123", "all"
);
isNotSqlInjection(
"""
SELECT * FROM `us``ers`
WHERE id = 123
""",
"users", "all"
);
isNotSqlInjection(
"""
SELECT * FROM users
WHERE id = 123
""",
"123", "all"
);
isNotSqlInjection(
"""
SELECT * FROM users
WHERE id = '123'
""",
"123", "all"
);
isNotSqlInjection(
"""
SELECT *
FROM users
WHERE is_escaped = "1' OR 1=1"
""",
"1' OR 1=1", "all"
);
}
@Test
public void testLowercasedInputSqlInjection() {
String sql = """
SELECT id,
email,
password_hash,
registered_at,
is_confirmed,
first_name,
last_name
FROM users WHERE email_lowercase = '' or 1=1 -- a'
""";
String expectedSqlInjection = "' OR 1=1 -- a";
isSqlInjection(sql, expectedSqlInjection, "all");
}
@Test
public void testUnterminatedStrings() {
isSqlTokenizeError("SELECT * FROM users WHERE id = 'users\\'", "users\\", "all");
isSqlTokenizeError("SELECT * FROM users WHERE id = 'users\\\\'", "users\\\\", "all");
}
/**
* Marked the following as SQL injection since this would result in 2 or more tokens becoming one :
* is_not_sql_injection("foobar)", "foobar)")
* is_not_sql_injection("foobar )", "foobar )")
* is_not_sql_injection("€foobar()", "€foobar()")
*/
@Test
public void testFunctionCallsAsSqlInjections() {
isSqlInjection("foobar()", "foobar()", "all");
isSqlInjection("foobar(1234567)", "foobar(1234567)", "all");
isSqlInjection("foobar ()", "foobar ()", "all");
isSqlInjection(".foobar()", ".foobar()", "all");
isSqlInjection("20+foobar()", "20+foobar()", "all");
isSqlInjection("20-foobar(", "20-foobar(", "all");
isSqlInjection("20<foobar()", "20<foobar()", "all");
isSqlInjection("20*foobar ()", "20*foobar ()", "all");
isSqlInjection("!foobar()", "!foobar()", "all");
isSqlInjection("=foobar()", "=foobar()", "all");
isSqlInjection("1foobar()", "1foobar()", "all");
isSqlInjection("1foo_bar()", "1foo_bar()", "all");
isSqlInjection("1foo-bar()", "1foo-bar()", "all");
isSqlInjection("#foobar()", "#foobar()", "all");
isSqlInjection("foobar)", "foobar)", "all");
isSqlInjection("foobar )", "foobar )", "all");
isSqlInjection("€foobar()", "€foobar()", "all");
}
}