1010
1111class 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