-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-sql-injection-blocking.php
More file actions
265 lines (226 loc) · 8.11 KB
/
04-sql-injection-blocking.php
File metadata and controls
265 lines (226 loc) · 8.11 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
<?php
/**
* Example 04: SQL Injection Blocking
*
* This example demonstrates how to block SQL injection attacks using:
* - OWASP CRS-style rules
* - Custom pattern matching
*
* Common SQLi patterns detected:
* - UNION SELECT attacks
* - Boolean-based injection
* - Time-based injection
* - Comment injection
* - Hex encoding
*
* Run: php examples/04-sql-injection-blocking.php
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Flowd\Phirewall\Config;
use Flowd\Phirewall\Config\DiagnosticsCounters;
use Flowd\Phirewall\Config\DiagnosticsDispatcher;
use Flowd\Phirewall\Http\Firewall;
use Flowd\Phirewall\Owasp\SecRuleLoader;
use Flowd\Phirewall\Store\InMemoryCache;
use Nyholm\Psr7\ServerRequest;
echo "=== SQL Injection Blocking Example ===\n\n";
// =============================================================================
// OWASP CRS RULES FOR SQL INJECTION
// =============================================================================
$sqlInjectionRules = <<<'CRS'
# =============================================================================
# SQL Injection Detection Rules
# Based on OWASP Core Rule Set patterns
# =============================================================================
# SQL Injection - UNION SELECT attacks
# Example: ?id=1 UNION SELECT username,password FROM users
SecRule ARGS "@rx (?i)\bunion\b.*\bselect\b" \
"id:942100,phase:2,deny,msg:'SQL Injection: UNION SELECT'"
# SQL Injection - SELECT FROM attacks
# Example: ?name='; SELECT * FROM users--
SecRule ARGS "@rx (?i)\bselect\b.*\bfrom\b" \
"id:942110,phase:2,deny,msg:'SQL Injection: SELECT FROM'"
# SQL Injection - Boolean-based blind
# Example: ?id=1' OR '1'='1
SecRule ARGS "@rx (?i)('\s*(or|and)\s*'|'\s*=\s*')" \
"id:942120,phase:2,deny,msg:'SQL Injection: Boolean-based'"
# SQL Injection - Numeric boolean
# Example: ?id=1 OR 1=1
SecRule ARGS "@rx (?i)\bor\s+\d+\s*=\s*\d+" \
"id:942130,phase:2,deny,msg:'SQL Injection: Numeric boolean'"
# SQL Injection - Comment sequences
# Example: ?id=1--
# Example: ?id=1/**/
SecRule ARGS "@rx (--\s*$|/\*|\*/)" \
"id:942140,phase:2,deny,msg:'SQL Injection: Comment sequence'"
# SQL Injection - Stacked queries
# Example: ?id=1; DROP TABLE users
SecRule ARGS "@rx (?i);\s*(drop|delete|insert|update|create|alter|truncate)\b" \
"id:942150,phase:2,deny,msg:'SQL Injection: Stacked query'"
# SQL Injection - Hex encoding
# Example: ?id=0x61646D696E (hex for 'admin')
SecRule ARGS "@rx (?i)0x[0-9a-f]{4,}" \
"id:942160,phase:2,deny,msg:'SQL Injection: Hex encoding'"
# SQL Injection - Benchmark/Sleep (time-based)
# Example: ?id=1 AND SLEEP(5)
SecRule ARGS "@rx (?i)\b(benchmark|sleep|waitfor)\s*\(" \
"id:942170,phase:2,deny,msg:'SQL Injection: Time-based'"
# SQL Injection - Common SQL functions
# Example: ?id=CHAR(65)
SecRule ARGS "@rx (?i)\b(char|concat|substring|ascii|ord)\s*\(" \
"id:942180,phase:2,deny,msg:'SQL Injection: SQL function'"
# SQL Injection - Database enumeration
# Example: ?id=1 AND (SELECT count(*) FROM information_schema.tables)>0
SecRule ARGS "@rx (?i)information_schema" \
"id:942190,phase:2,deny,msg:'SQL Injection: DB enumeration'"
CRS;
echo "Loading OWASP-style SQL injection rules...\n";
$result = SecRuleLoader::fromStringWithReport($sqlInjectionRules);
$coreRuleSet = $result['rules'];
echo sprintf('Rules loaded: %d%s', $result['parsed'], PHP_EOL);
echo "Rules skipped: {$result['skipped']}\n\n";
// List loaded rules
echo "Active rules:\n";
foreach ($coreRuleSet->ids() as $id) {
echo sprintf(' - Rule %d: ', $id) . ($coreRuleSet->isEnabled($id) ? 'enabled' : 'disabled') . "\n";
}
echo "\n";
// =============================================================================
// CONFIGURATION
// =============================================================================
$diagnostics = new DiagnosticsCounters();
$config = new Config(new InMemoryCache(), new DiagnosticsDispatcher($diagnostics));
$config->enableResponseHeaders();
$config->blocklists->owasp('sql-injection', $coreRuleSet);
// Enable diagnostics header to see which rule matched
$config->enableOwaspDiagnosticsHeader();
$firewall = new Firewall($config);
// =============================================================================
// TEST CASES
// =============================================================================
echo "=== Testing SQL Injection Patterns ===\n\n";
$testCases = [
// Safe requests
[
'description' => 'Safe: Normal search query',
'url' => '/api/search?q=hello+world',
'expected' => 'ALLOW',
],
[
'description' => 'Safe: Numeric ID',
'url' => '/api/users?id=123',
'expected' => 'ALLOW',
],
[
'description' => 'Safe: Normal filter',
'url' => '/api/products?category=electronics&sort=price',
'expected' => 'ALLOW',
],
// SQL Injection attempts
[
'description' => 'SQLi: UNION SELECT attack',
'url' => '/api/users?id=1+UNION+SELECT+username,password+FROM+users',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Boolean-based OR injection',
'url' => "/api/login?user=admin'OR'1'='1",
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Numeric boolean',
'url' => '/api/users?id=1+OR+1=1',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Comment-based',
'url' => '/api/users?id=1--',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Block comment',
'url' => '/api/users?id=1/**/UNION/**/SELECT',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Stacked DROP TABLE',
'url' => '/api/users?id=1;DROP+TABLE+users',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Stacked DELETE',
'url' => '/api/users?id=1;DELETE+FROM+users',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Hex encoding',
'url' => '/api/users?name=0x61646D696E',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Time-based SLEEP',
'url' => '/api/users?id=1+AND+SLEEP(5)',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Time-based BENCHMARK',
'url' => '/api/users?id=1+AND+BENCHMARK(10000000,SHA1(1))',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: CHAR function',
'url' => '/api/users?name=CHAR(65,66,67)',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: CONCAT function',
'url' => '/api/users?name=CONCAT(0x3a,user())',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: Information schema',
'url' => '/api/users?id=1+AND+(SELECT+*+FROM+information_schema.tables)',
'expected' => 'BLOCK',
],
[
'description' => 'SQLi: SELECT FROM users',
'url' => "/api/search?q=';SELECT+*+FROM+users--",
'expected' => 'BLOCK',
],
];
$passed = 0;
$failed = 0;
foreach ($testCases as $testCase) {
$request = new ServerRequest('GET', $testCase['url']);
$result = $firewall->decide($request);
$actual = $result->isBlocked() ? 'BLOCK' : 'ALLOW';
$status = $actual === $testCase['expected'] ? 'PASS' : 'FAIL';
if ($status === 'PASS') {
++$passed;
} else {
++$failed;
}
echo sprintf(
"[%s] %s\n",
$status,
$testCase['description']
);
if ($result->isBlocked()) {
$ruleId = $result->headers['X-Phirewall-Owasp-Rule'] ?? 'n/a';
echo sprintf(" Blocked by rule: %s\n", $ruleId);
}
// Show URL for failed tests
if ($status === 'FAIL') {
echo sprintf(" URL: %s\n", $testCase['url']);
echo sprintf(" Expected: %s, Got: %s\n", $testCase['expected'], $actual);
}
}
echo "\n=== Results ===\n";
echo sprintf('Passed: %d%s', $passed, PHP_EOL);
echo sprintf('Failed: %d%s', $failed, PHP_EOL);
echo "\n=== Diagnostics ===\n";
$counters = $diagnostics->all();
echo "Blocked requests: " . ($counters['blocklisted']['total'] ?? 0) . "\n";
echo "Passed requests: " . ($counters['passed']['total'] ?? 0) . "\n";
echo "\n=== Example Complete ===\n";