Skip to content

Commit f88f5b3

Browse files
authored
Merge pull request #14 from dwisiswant0/master
Add many methods & fix some bugs. See CHANGELOG.md.
2 parents b244c48 + edde590 commit f88f5b3

File tree

3 files changed

+115
-32
lines changed

3 files changed

+115
-32
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22

33
All notable changes to `AntiScanScanClub` will be documented in this file.
44

5+
## Version 2.0.2
6+
7+
### Added
8+
9+
- filterFileFind() method (deep search for filterFile() method), check whether the destination file and/ path is in the filter_files.txt
10+
- addToFilterFiles() method, add file and/ path to filter_files.txt
11+
- md5LocalFilterFiles() method for validate in restoreFilterFiles() method
12+
- getRemoteFilterFiles() method, getting filter_files.txt from remote repository
13+
14+
### Fixed
15+
16+
- Avoid nested arrays in filterInput() _(related v1.0.3 bug)_
17+
518
## Version 2.0.1
619

720
### Added
21+
822
- getPublicFiles() method for get all files in public path recursively
923
- getAllRoutes() method for get uri of all registered routes
1024
- whitelistPublicFiles() method for whitelisting all public files recursively
@@ -13,12 +27,14 @@ All notable changes to `AntiScanScanClub` will be documented in this file.
1327
## Version 2.0.0
1428

1529
### Added
30+
1631
- whitelistFile() method, for whitelisting files/path from filterFile()
1732
- restoreFilterFiles() method to restoring filter_files.txt to default
1833

1934
## Version 1.0.3
2035

2136
### Fixed
37+
2238
- Fix filterInput() bug failed to handle array input fields (reported by [@mirfansulaiman](https://github.com/mirfansulaiman))
2339

2440
## Version 1.0.2-dev1

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ var_dump(whitelistAllRoutes()); // @return array
204204
var_dump($ASSC->restoreFilterFiles()); // @return bool
205205
```
206206
207+
- **Add file and/ path to filterFile() rejection**
208+
209+
```php
210+
$file = "api/adminLists";
211+
212+
var_dump(addToFilterFiles($file)); // @return integer/bool
213+
```
214+
207215
### NOTE
208216
209217
- If you call `filterInput()` and/ `filterFile()` method, you no longer need to call `addToBlacklisted()` method.
@@ -235,4 +243,4 @@ license. Please see the [LICENSE file](LICENSE) for more information.
235243
236244
## Version
237245
238-
**Current version is 2.0.1** and still development.
246+
**Current version is 2.0.2** and still development.

src/AntiScanScanClub.php

Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@
1010

1111
class AntiScanScanClub
1212
{
13+
/**
14+
* @var string $defaultBlacklists
15+
*/
16+
private $defaultBlacklists = "blacklists.json";
17+
1318
/**
1419
* @var string $filterRules
1520
*/
16-
private $filterRules = "filter_rules.json";
21+
private $filterRules = "filter_rules.json";
1722

1823
/**
1924
* @var string $filterFiles
2025
*/
2126
private $filterFiles = "filter_files.txt";
2227

2328
/**
24-
* @var string $defaultBlacklists
29+
* @var constant string REMOTE_REPO
2530
*/
26-
private $defaultBlacklists = "blacklists.json";
31+
private const REMOTE_REPO = "https://github.com/noobsec/AntiScanScanClub-laravel";
2732

2833
/**
29-
* @var string $remoteRepo
34+
* @var constant string FILTER_FILES_MD5
3035
*/
31-
private $remoteRepo = "https://github.com/noobsec/AntiScanScanClub-laravel";
36+
private const FILTER_FILES_MD5 = "05c2fe4cad6dc3ea1a3bf2becdb9153f";
3237

3338
/**
3439
* AntiScanScanClub.
@@ -117,26 +122,22 @@ public function filterInput($data = [], $blocker = FALSE, $clientIp) {
117122
foreach ($data as $key => $value) {
118123
foreach ($objectRules as $key => $object) {
119124
if (is_array($value)) {
120-
foreach ($value as $key => $array) {
121-
$filtered = preg_match("/" . $object['rule'] . "/", $array);
122-
$value = $array;
123-
if ($filtered) break;
124-
}
125+
return $this->filterInput($value, $blocker, $clientIp);
125126
} else {
126127
$filtered = preg_match("/" . $object['rule'] . "/", $value);
128+
if ($filtered) break;
127129
}
130+
}
131+
}
128132

129-
if ($filtered) {
130-
if ($blocker === TRUE) $this->addToBlacklisted($clientIp, $object['description'] . " (" . $value . ")");
131-
return abort($this->abort);
132-
}
133-
}
134-
}
133+
if ($filtered) {
134+
if ($blocker === TRUE) $this->addToBlacklisted($clientIp, $object['description'] . " (" . $value . ")");
135+
return abort($this->abort);
136+
}
135137

136138
return FALSE;
137139
}
138140

139-
140141
/**
141142
* Prevention of access to credentials and/ important files/path
142143
* e.g: wp-admin.php, .git/, backups.tar.gz, www.sql (see many more at filter_files.txt)
@@ -149,6 +150,23 @@ public function filterInput($data = [], $blocker = FALSE, $clientIp) {
149150
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
150151
*/
151152
public function filterFile($url = NULL, $blocker = FALSE, $clientIp) {
153+
$filterFileFind = $this->filterFileFind($url);
154+
155+
if ($filterFileFind === TRUE) {
156+
if ($blocker === TRUE) $this->addToBlacklisted($clientIp, "Trying to access " . $url);
157+
return abort($this->abort);
158+
} else {
159+
return FALSE;
160+
}
161+
}
162+
163+
/**
164+
* Check whether the destination file and/ path is in the filter_files.txt
165+
*
166+
* @param string $file and/ path to check
167+
* @return bool
168+
*/
169+
private function filterFileFind($file) {
152170
$filterFiles = __DIR__ . "/" . $this->filterFiles;
153171
$getFile = @file_get_contents($filterFiles);
154172

@@ -159,10 +177,9 @@ public function filterFile($url = NULL, $blocker = FALSE, $clientIp) {
159177
$objectFiles = file($filterFiles);
160178

161179
foreach ($objectFiles as $key => $value) {
162-
$file = trim($value);
163-
if (substr($url, 1) === trim($file)) {
164-
if ($blocker === TRUE) $this->addToBlacklisted($clientIp, "Trying to access " . $file);
165-
return abort($this->abort);
180+
$list = trim($value);
181+
if (substr($file, 1) === trim($list)) {
182+
return TRUE;
166183
}
167184
}
168185

@@ -188,6 +205,22 @@ public function addToBlacklisted($clientIp, $attack = NULL) {
188205
return $this->writeToBlacklistsFile($add);
189206
}
190207

208+
/**
209+
* Add file and/ path to filter_files.txt
210+
*
211+
* @param string $file and/ path
212+
* @return integer/bool
213+
*/
214+
public function addToFilterFiles($file) {
215+
$filterFiles = __DIR__ . "/" . $this->filterFiles;
216+
$filterFileFind = $this->filterFileFind($file);
217+
218+
if ($filterFileFind === FALSE) {
219+
return file_put_contents($filterFiles, $file, FILE_APPEND);
220+
} else {
221+
return FALSE;
222+
}
223+
}
191224

192225
/**
193226
* Clean the client IP from blacklists
@@ -203,7 +236,6 @@ public function cronBlacklistedRules() {
203236
}
204237
}
205238

206-
207239
/**
208240
* Remove client IP from blacklists rule
209241
*
@@ -218,7 +250,6 @@ public function removeFromBlacklists($clientIp) {
218250
return $this->writeToBlacklistsFile($this->list_object);
219251
}
220252

221-
222253
/**
223254
* Purge and/ clean all client IPs from blacklists
224255
*
@@ -228,7 +259,6 @@ public function purgeBlacklistsFile() {
228259
return $this->writeToBlacklistsFile([]);
229260
}
230261

231-
232262
/**
233263
* Write visitor data to blacklists file
234264
*
@@ -347,23 +377,52 @@ public function whitelistFile($search) {
347377
}
348378

349379
/**
350-
* Restore filter_files.txt lists to default
380+
* MD5 checksum for local filter_files.txt
351381
*
352382
* @return bool
353383
*/
354-
public function restoreFilterFiles() {
355-
$defaultFilterFiles = @file_get_contents($this->remoteRepo . "/raw/master/src/" . $this->filterFiles);
384+
public function md5LocalFilterFiles() {
385+
$localFilterFiles = __DIR__ . "/" . $this->filterFiles;
386+
387+
if (md5_file($localFilterFiles) === self::FILTER_FILES_MD5) {
388+
return TRUE;
389+
} else {
390+
return FALSE;
391+
}
392+
}
393+
394+
/**
395+
* Getting filter_files.txt from remote repository
396+
*
397+
* @return string
398+
*/
399+
private function getRemoteFilterFiles() {
400+
$defaultFilterFiles = @file_get_contents(self::REMOTE_REPO . "/raw/master/src/" . $this->filterFiles);
356401

357402
if ($defaultFilterFiles === FALSE) {
358403
throw new \Exception("Error While Getting default filter files from Repo", 1);
359404
}
360405

361-
$write = file_put_contents(__DIR__ . "/" . $this->filterFiles, $defaultFilterFiles);
406+
return $defaultFilterFiles;
407+
}
362408

363-
if ($write === 84213) {
364-
return TRUE;
409+
/**
410+
* Restore filter_files.txt lists to default
411+
*
412+
* @return bool
413+
*/
414+
public function restoreFilterFiles() {
415+
$remoteFilterFiles = $this->getRemoteFilterFiles();
416+
417+
if ($this->md5LocalFilterFiles() === FALSE) {
418+
$write = file_put_contents(__DIR__ . "/" . $this->filterFiles, $remoteFilterFiles);
419+
if ($write === 84213 && $this->md5LocalFilterFiles() === TRUE) {
420+
return TRUE;
421+
} else {
422+
return FALSE;
423+
}
365424
} else {
366-
return FALSE;
425+
return TRUE;
367426
}
368427
}
369428
}

0 commit comments

Comments
 (0)