-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjitsu.class.php
627 lines (560 loc) · 15.6 KB
/
jitsu.class.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* jitsu - A PHP based website security framework
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Security
* @package jitsu
* @author m0rtem <chris@is-the.ninja>
* @copyright 2016
*/
class jitsu
{
public $version = "1.0.0";
// Filter rules from: https://github.com/PHPIDS/PHPIDS
public $filter_rules_json = "/jitsu/filter_rules.json";
public $filter_rules;
public $path_to_blacklist = "/jitsu/data/blacklist.txt";
public $path_to_excludes = "/jitsu/data/excludes.txt";
// GeoIP
public $path_to_geoip = "geoip/geoip.inc";
public $path_to_geoipcity = "geoip/geoipcity.inc";
public $path_to_geoipregionvars = "geoip/geoipregionvars.php";
public $path_to_geolitecity = "/jitsu/geoip/GeoLiteCity.dat";
private $dbtable = "jitsu";
private $password = "P@SSW0RD";
// Set this to true to filter out attacks; RECOMMENDED ON!
private $filterQuery = true;
// Set this to true if you want to kill the connection apon payload detection
private $killConnection = true;
/**
* This is the construct method which gets initialized with each class call,
* all the settings and configs that occur before site load happens here.
*/
public function __construct()
{
// Check if IP banned
if(self::ifUserBanned(self::getIPAddress()))
{
die(self::getIPAddress()." Banned.");
}
$filter_rules = @file_get_contents(dirname(__DIR__).$this->filter_rules_json, FILE_USE_INCLUDE_PATH);
if($filter_rules === FALSE)
{
exit("No filter file found or unable to read file correctly.");
}
else
{
$this->filter_rules = json_decode($filter_rules,true);
}
$firewall = self::firewall($_GET, $_POST);
if(!empty($firewall))
{
// Log event
self::log($firewall);
// If filter query is set true
if($this->filterQuery)
{
$_GET = self::filter($_GET);
$_POST = self::filter($_POST);
}
// If kill connection is set true
if($this->killConnection)
{
die();
}
}
}
/**
* ifUserBanned
*/
public function ifUserBanned ($data)
{
// If data is set then run
if(isset($data))
{
$file = fopen(dirname(__DIR__).$this->path_to_blacklist, "r") or exit("Unable to open file!");
while(!feof($file))
{
if(trim(fgets($file)) == $data)
{
return true;
}
}
fclose($file);
}
}
public function ifExcluded ($data)
{
// If data is set then run
if(isset($data))
{
$file = fopen(dirname(__DIR__).$this->path_to_excludes, "r") or exit("Unable to open file!");
while(!feof($file))
{
if(trim(fgets($file)) == $data)
{
return true;
}
}
fclose($file);
}
}
public function sessionCheck($session)
{
if($session == crc32($this->password))
{
return true;
}
else
{
return false;
}
}
public function login($password)
{
if($password == $this->password)
{
return true;
}
else
{
return false;
}
}
public function clearLogs()
{
$db = self::db();
$sql = "TRUNCATE `".$this->dbtable."`";
if(!$result = $db->query($sql))
{
die('There was an error running the query [' . $db->error . ']');
}
}
public function addToBlacklist ($data)
{
// If data is set then run
if(isset($data))
{
$handle = fopen(dirname(__DIR__).$this->path_to_blacklist, 'a') or die('Cannot open file: '.$eventFileName);
fwrite($handle, $data.PHP_EOL);
fclose($handle);
}
}
public function getBlacklistHTML ()
{
$file = fopen(dirname(__DIR__).$this->path_to_blacklist, "r") or exit("Unable to open file!");
$blacklistHTML = "<div class=\"well well-sm\">";
$i = 0;
while(!feof($file))
{
$ip = trim(fgets($file));
$blacklistHTML .= $ip."<br />";
$i++;
}
if($i==1){$blacklistHTML .= "Nothing to see here!";}
fclose($file);
$blacklistHTML .= "</div>";
return $blacklistHTML;
}
public function getAttackHistoryHTML()
{
$db = self::db();
$sql = "SELECT * FROM `".$this->dbtable."`";
if(!$result = $db->query($sql))
{
die('There was an error running the query [' . $db->error . ']');
}
$resultHTML = "<table id=\"example\" class=\"table table-striped table-bordered dt-responsive nowrap\" cellspacing=\"0\" width=\"100%\">";
$resultHTML .= "<thead><tr>";
$resultHTML .= "<th>Method</th>";
$resultHTML .= "<th>Description</th>";
$resultHTML .= "<th>Tags</th>";
$resultHTML .= "<th>Key</th>";
$resultHTML .= "<th>Value</th>";
$resultHTML .= "<th>Impact</th>";
$resultHTML .= "<th>Date/Time</th>";
$resultHTML .= "<th>Ip</th>";
$resultHTML .= "<th>RuleREGEX</th>";
$resultHTML .= "<th>OS</th>";
$resultHTML .= "<th>Browser</th>";
$resultHTML .= "<th>User Agent</th>";
$resultHTML .= "<th>Referrer</th>";
$resultHTML .= "<th>URL</th>";
$resultHTML .= "<th>Lat.</th>";
$resultHTML .= "<th>Long.</th>";
$resultHTML .= "<th>City</th>";
$resultHTML .= "<th>Region</th>";
$resultHTML .= "<th>Country</th>";
$resultHTML .= "</tr></thead><tbody>";
while($row = $result->fetch_assoc())
{
$resultHTML .= "<tr>
<td>".$row["method"]."</td>
<td>".$row["description"]."</td>
<td>".$row["tags"]."</td>
<td>".htmlentities($row["key"])."</td>
<td>".htmlentities($row["value"])."</td>
<td>".$row["impact"]."</td>
<td>".$row["datetime"]."</td>
<td>".htmlentities($row["ip"])."</td>
<td>".$row["rule"]."</td>
<td>".htmlentities($row["os"])."</td>
<td>".htmlentities($row["browser"])."</td>
<td>".htmlentities($row["user_agent"])."</td>
<td>".htmlentities($row["referrer"])."</td>
<td>".htmlentities($row["page_url"])."</td>
<td>".$row["lat"]."</td>
<td>".$row["long"]."</td>
<td>".$row["city"]."</td>
<td>".$row["region"]."</td>
<td>".$row["country"]."</td>
</tr>";
}
$resultHTML .= "</tbody></table>";
return $resultHTML;
}
private function db ()
{
require(dirname(__DIR__)."/db.php");
return $db;
}
private function log($data)
{
$db = self::db();
require_once($this->path_to_geoip);
require_once($this->path_to_geoipcity);
require_once($this->path_to_geoipregionvars);
$gi = geoip_open(dirname(__DIR__).$this->path_to_geolitecity, GEOIP_STANDARD);
foreach($data as $bit)
{
$method = $bit["method"];
$key = $db->escape_string($bit["key"]);
$value = $db->escape_string($bit["value"]);
$ruleID = $bit["ruleID"];
$rule = $bit["rule"];
$description = $bit["description"];
$tags = "";
if(is_array($bit["tags"]))
{
foreach($bit["tags"] as $tag_key => $tag_value)
{
$tags .= $tag_value .",";
}
}
else
{
$tags = $bit["tags"];
}
$tags = rtrim($tags, ",");
$impact = $bit["impact"];
$ip = $db->escape_string(self::getIPAddress());
$os = $db->escape_string(self::getOS());
$browser = $db->escape_string(self::getBrowser());
$datetime = date("Y-m-d H:i:s");
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = $db->escape_string($url);
$userAgent = $db->escape_string($_SERVER['HTTP_USER_AGENT']);
if(isset($_SERVER['HTTP_REFERER']))
{
$referrer = $db->escape_string($_SERVER['HTTP_REFERER']);
}
else
{
$referrer = "";
}
$rsGeoData = geoip_record_by_addr($gi, $ip);
if($rsGeoData)
{
$lat = $rsGeoData->latitude;
$long = $rsGeoData->longitude;
$city = $rsGeoData->city;
$region = $rsGeoData->region;
$country = $rsGeoData->country_name;
}
else
{
$lat = 0;
$long = 0;
$city = "";
$region = "";
$country = "";
}
$statement = $db->prepare("INSERT INTO `".$this->dbtable."` (`method`,`key`,`value`,`ruleID`,`rule`,`description`,`tags`,`impact`,`ip`,`os`,`browser`,`user_agent`,`referrer`,`datetime`,`page_url`,`lat`,`long`,`city`,`region`,`country`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$statement->bind_param('sssisssissssssssssss', $method, $key, $value, $ruleID, $rule, $description, $tags, $impact, $ip, $os, $browser, $userAgent, $referrer, $datetime, $url, $lat, $long, $city, $region, $country);
$statement->execute();
$statement->close();
}
geoip_close($gi);
}
/**
* Retrieves the best guess of the client's actual IP address.
* Takes into account numerous HTTP proxy headers due to variations
* in how different ISPs handle IP addresses in headers between hops.
*/
public function getIPAddress()
{
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($ip_keys as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ip)
{
$ip = trim($ip);
}
}
}
return $ip;
}
private function getOS()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value)
{
if (preg_match($regex, $user_agent))
{
$os_platform = $value;
}
}
return $os_platform;
}
private function getBrowser()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browser = "Unknown Browser";
$browser_array = array(
'/msie/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edge/i' => 'Edge',
'/opera/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/mobile/i' => 'Handheld Browser'
);
foreach ($browser_array as $regex => $value)
{
if (preg_match($regex, $user_agent))
{
$browser = $value;
}
}
return $browser;
}
private function filter ($DATA)
{
$NEWDATA = array();
if(!empty($DATA))
{
foreach($DATA as $key => $value)
{
foreach($this->filter_rules["filters"] as $filter)
{
if(!is_array($key))
{
$excluded = self::ifExcluded($key);
if($excluded)
{
continue;
}
$key = strtolower($key);
}
if(!is_array($value))
{
$value = strtolower($value);
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value);
if($isMatchedKey)
{
$key = preg_replace('/' . $filter["rule"] . '/ms', "", $key);
}
if($isMatchedValue)
{
$value = preg_replace('/' . $filter["rule"] . '/ms', "", $value);
}
}
elseif(is_array($value))
{
foreach($value as $key2 => $value2)
{
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key2);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value2);
if($isMatchedKey)
{
$key = preg_replace('/' . $filter["rule"] . '/ms', "", $key2);
}
if($isMatchedValue)
{
$value = preg_replace('/' . $filter["rule"] . '/ms', "", $value2);
}
}
}
}
$NEWDATA[$key] = $value;
}
return $NEWDATA;
}
}
private function firewall ($GET, $POST)
{
$resultArray = array();
if(!empty($GET))
{
foreach($GET as $key => $value)
{
foreach($this->filter_rules["filters"] as $filter)
{
if(!is_array($key))
{
$excluded = self::ifExcluded($key);
if($excluded)
{
continue;
}
$key = strtolower($key);
}
if(is_array($value))
{
foreach($value as $key2 => $value2)
{
$value = strtolower($value2);
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key2);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value2);
if($isMatchedKey || $isMatchedValue)
{
$resultArray[] = array(
"method"=>"GET",
"key"=>$key2,
"value"=>$value2,
"ruleID"=>$filter["id"],
"rule"=>$filter["rule"],
"description"=>$filter["description"],
"tags"=>$filter["tags"]["tag"],
"impact"=>$filter["impact"]
);
}
}
}
elseif(!is_array($value))
{
$value = strtolower($value);
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value);
if($isMatchedKey || $isMatchedValue)
{
$resultArray[] = array(
"method"=>"GET",
"key"=>$key,
"value"=>$value,
"ruleID"=>$filter["id"],
"rule"=>$filter["rule"],
"description"=>$filter["description"],
"tags"=>$filter["tags"]["tag"],
"impact"=>$filter["impact"]
);
}
}
}
//echo "key: ".$key." value:".$value;
}
}
if(!empty($POST))
{
foreach($POST as $key => $value)
{
foreach($this->filter_rules["filters"] as $filter)
{
if(!is_array($key))
{
$excluded = self::ifExcluded($key);
if($excluded)
{
continue;
}
$key = strtolower($key);
}
if(is_array($value))
{
foreach($value as $key2 => $value2)
{
$value = strtolower($value2);
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key2);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value2);
if($isMatchedKey || $isMatchedValue)
{
$resultArray[] = array(
"method"=>"GET",
"key"=>$key2,
"value"=>$value2,
"ruleID"=>$filter["id"],
"rule"=>$filter["rule"],
"description"=>$filter["description"],
"tags"=>$filter["tags"]["tag"],
"impact"=>$filter["impact"]
);
}
}
}
elseif(!is_array($value))
{
$value = strtolower($value);
$isMatchedKey = (bool) preg_match('/' . $filter["rule"] . '/ms', $key);
$isMatchedValue = (bool) preg_match('/' . $filter["rule"] . '/ms', $value);
if($isMatchedKey || $isMatchedValue)
{
$resultArray[] = array(
"method"=>"GET",
"key"=>$key,
"value"=>$value,
"ruleID"=>$filter["id"],
"rule"=>$filter["rule"],
"description"=>$filter["description"],
"tags"=>$filter["tags"]["tag"],
"impact"=>$filter["impact"]
);
}
}
}
//echo "key: ".$key." value:".$value;
}
}
return $resultArray;
}
}
?>