This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
waf.php
338 lines (331 loc) · 11.5 KB
/
waf.php
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
<?php
class aWAF {
function __construct() {
$this->IPHeader = "REMOTE_ADDR";
$this->CookieCheck = true;
$this->CookieCheckParam = 'username';
return true;
}
function shorten_string($string, $wordsreturned) {
$retval = $string;
$array = explode(" ", $string);
if (count($array)<=$wordsreturned){
$retval = $string;
} else {
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}
function vulnDetectedHTML($Method, $BadWord, $DisplayName, $TypeVuln) {
header('HTTP/1.0 400 Bad Request');
echo 'There is problem with your request, please try again.';
die(); // Block request.
}
function getArray($Type) {
switch ($Type) {
case 'SQL':
return array(
"'",
'´',
'..',
';',
'SELECT FROM',
'SELECT * FROM',
'FROM',
'VALUES',
'DELETE FROM',
'ONION',
'union',
'UNION',
'DROP TABLE',
'--',
'INSERT INTO',
'UPDATE `users` SET',
'UPDATE settings SET',
'UPDATE `settings` SET',
'UPDATE users SET',
'WHERE username',
') or true--',
') or ("")=(',
'" or true--',
'" or ""="',
'" or 1--',
'" or "x"="',
'WHERE id',
'DROP TABLE',
'0x50',
'mid((select',
'union(((((((',
'concat(0x',
'concat(',
'OR boolean',
'or HAVING',
"OR '1",
'0x3c62723e3c62723e3c62723e',
'0x3c696d67207372633d22',
'+#1q%0AuNiOn all#qa%0A#%0AsEleCt',
'unhex(hex(Concat(',
'Table_schema,0x3e,',
'0x00', // \0 [This is a zero, not the letter O]
'0x08', // \b
'0x09', // \t
'0x0a', // \n
'0x0d', // \r
'0x1a', // \Z
'0x22', // \"
'0x25', // \%
'0x27', // \'
'0x5c', // \\
'0x5f', // \_
'%2F',
'exec',
'chmod',
'chown',
'eval',
'shell_exec',
'curl_multi_exec',
'apache_setenv',
'..'
);
break;
case 'XSS':
return array('<img',
'img>',
'<image',
'document.cookie',
'onerror()',
'script>',
'<script',
'alert(',
"curl",
"telnet",
"bash",
"|",
";",
"ncat",
"netcat",
"ping",
"passwd",
'window.',
'String.fromCharCode(',
'javascript:',
'onmouseover="',
'<BODY onload',
'<style',
'svg onload');
break;
default:
return false;
break;
}
}
function arrayFlatten(array $array) {
$flatten = array();
array_walk_recursive($array, function($value) use(&$flatten) {
$flatten[] = $value;
});
return $flatten;
}
function sqlCheck($Value, $Method, $DisplayName) {
// For false alerts.
$Replace = array("can't" => "cant",
"don't" => "dont");
foreach ($Replace as $key => $value_rep) {
$Value = str_replace($key, $value_rep, $Value);
}
$BadWords = $this->getArray('SQL');
foreach ($BadWords as $BadWord) {
if (strpos(strtolower($Value), strtolower($BadWord)) !== false) {
// String contains some Vuln.
$this->vulnDetectedHTML($Method, $BadWord, $Value, 'SQL Injection');
}
}
}
function xssCheck($Value, $Method, $DisplayName) {
// For false alerts.
$Replace = array("<3" => ":heart:");
foreach ($Replace as $key => $value_rep) {
$Value = str_replace($key, $value_rep, $Value);
}
$BadWords = $this->getArray('XSS');
foreach ($BadWords as $BadWord) {
if (strpos(strtolower($Value), strtolower($BadWord)) !== false) {
// String contains some Vuln.
$this->vulnDetectedHTML($Method, $BadWord, $DisplayName, 'XSS (Cross-Site-Scripting)');
}
}
}
function is_html($string) {
return $string != strip_tags($string) ? true:false;
}
function santizeString($String) {
$String = escapeshellarg($String);
$String = htmlentities($String);
$XSS = $this->getArray('XSS');
foreach ($XSS as $replace) {
$String = str_replace($replace, '', $String);
}
$SQL = $this->getArray('SQL');
foreach ($SQL as $replace) {
$String = str_replace($replace, '', $String);
}
return $String;
}
function htmlCheck($value, $Method, $DisplayName) {
if ($this->is_html(strtolower($value)) !== false) {
// HTML Detected!
$this->vulnDetectedHTML($Method, "HTML CHARS", $DisplayName, 'XSS (HTML)');
}
}
function arrayValues($Array) {
return array_values($Array);
}
function checkGET() {
foreach ($_GET as $key => $value) {
if (is_array($value)) {
$flattened = $this->arrayFlatten($value);
foreach ($flattened as $sub_key => $sub_value) {
$this->sqlCheck($sub_value, "_GET", $sub_key);
$this->xssCheck($sub_value, "_GET", $sub_key);
$this->htmlCheck($sub_value, "_GET", $sub_key);
}
} else {
$this->sqlCheck($value, "_GET", $key);
$this->xssCheck($value, "_GET", $key);
$this->htmlCheck($value, "_GET", $key);
}
}
}
function checkPOST() {
foreach ($_POST as $key => $value) {
if (is_array($value)) {
$flattened = $this->arrayFlatten($value);
foreach ($flattened as $sub_key => $sub_value) {
$this->sqlCheck($sub_value, "_POST", $sub_key);
$this->xssCheck($sub_value, "_POST", $sub_key);
$this->htmlCheck($sub_value, "_POST", $sub_key);
}
} else {
$this->sqlCheck($value, "_POST", $key);
$this->xssCheck($value, "_POST", $key);
$this->htmlCheck($value, "_POST", $key);
}
}
}
function checkCOOKIE() {
foreach ($_COOKIE as $key => $value) {
if (is_array($value)) {
$flattened = $this->arrayFlatten($value);
foreach ($flattened as $sub_key => $sub_value) {
$this->sqlCheck($sub_value, "_COOKIE", $sub_key);
$this->xssCheck($sub_value, "_COOKIE", $sub_key);
$this->htmlCheck($sub_value, "_COOKIE", $sub_key);
}
} else {
$this->sqlCheck($value, "_COOKIE", $key);
$this->xssCheck($value, "_COOKIE", $key);
$this->htmlCheck($value, "_COOKIE", $key);
}
}
}
function gua() {
if (isset($_SERVER['HTTP_USER_AGENT'])) {
return $_SERVER['HTTP_USER_AGENT'];
}
return md5(rand());
}
function cutGua($string) {
$five = substr($string, 0, 4);
$last = substr($string, -3);
return md5($five.$last);
}
function getCSRF() {
if (isset($_SESSION['token'])) {
$token_age = time() - $_SESSION['token_time'];
if ($token_age <= 300){ /* Less than five minutes has passed. */
return $_SESSION['token'];
} else {
$token = md5(uniqid(rand(), TRUE));
$_SESSION['token'] = $token . "asd648" . $this->cutGua($this->gua());
$_SESSION['token_time'] = time();
return $_SESSION['token'];
}
} else {
$token = md5(uniqid(rand(), TRUE));
$_SESSION['token'] = $token . "asd648" . $this->cutGua($this->gua());
$_SESSION['token_time'] = time();
return $_SESSION['token'];
}
}
function verifyCSRF($Value) {
if (isset($_SESSION['token'])) {
$token_age = time() - $_SESSION['token_time'];
if ($token_age <= 300){ /* Less than five minutes has passed. */
if ($Value == $_SESSION['token']) {
$Explode = explode('asd648', $_SESSION['token']);
$gua = $Explode[1];
if ($this->cutGua($this->gua()) == $gua) {
// Validated, Done!
unset($_SESSION['token']);
unset($_SESSION['token_time']);
return true;
}
unset($_SESSION['token']);
unset($_SESSION['token_time']);
return false;
}
} else {
return false;
}
} else {
return false;
}
}
function useCloudflare() {
$this->IPHeader = "HTTP_CF_CONNECTING_IP";
}
function useBlazingfast() {
$this->IPHeader = "X-Real-IP";
}
function customIPHeader($String = 'REMOTE_ADDR') {
$this->IPHeader = $String;
}
function antiCookieSteal($listparams = 'username') {
$this->CookieCheck = true;
$this->CookieCheckParam = $listparams;
}
function cookieCheck() {
// Check Anti-Cookie steal trick.
if ($this->CookieCheck == true) {
// Check then.
if (isset($_SESSION)) { // Session set.
if (isset($_SESSION[$this->CookieCheckParam])) { // Logged.
if (!(isset($_SESSION['aWAF-IP']))) {
$_SESSION['aWAF-IP'] = $_SERVER[$this->IPHeader];
return true;
} else {
if (!($_SESSION['aWAF-IP'] == $_SERVER[$this->IPHeader])) {
// Changed IP.
unset($_SESSION['aWAF-IP']);
unset($_SESSION);
@session_destroy();
@session_start();
return true;
}
}
}
}
}
}
function start() {
@session_start();
@$this->checkGET();
@$this->checkPOST();
@$this->checkCOOKIE();
if ($this->CookieCheck == true) {
$this->cookieCheck();
}
}
}
?>