-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.php
executable file
·681 lines (608 loc) · 18.6 KB
/
main.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?php
/**
Written by Cvar1984 <[email protected]>, November 2022
Copyright (C) 2022 Cvar1984
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
$minute = 60;
$limit = (60 * $minute); // 60 (seconds) = 1 Minutes
ini_set('memory_limit', '-1');
ini_set('max_execution_time', $limit);
set_time_limit($limit);
ini_set('display_errors', 1); // debug
/**
* Check if function is available
*
* @param callable $callback
* @return boolean
*/
function isWorking($callback)
{
$securityDisabled = ini_get('disable_functions');
$securityDisabled = explode(',', $securityDisabled);
if(in_array($callback, $securityDisabled)) {
return false;
}
if(!function_exists($callback)) {
return false;
}
return true;
}
if (isWorking('curl_exec')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disable strict peer verification (caution in production)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
/**
* Recursive listing files
*
* @param string $directory
* @param array $entries_array optional
* @return array of files
*/
function recursiveScan($directory, &$entries_array = array()) // :array
{
// Check if the directory exists and is readable
if (!is_dir($directory) || !is_readable($directory)) {
return $entries_array;
}
// Open the directory
$handle = opendir($directory);
if (!$handle) {
return $entries_array;
}
// Iterate over the directory contents
while (($entry = readdir($handle)) !== false) {
// Skip the current directory and parent directory
if ($entry === '.' || $entry === '..') {
continue;
}
// Get the full path to the entry
$entryPath = $directory . DIRECTORY_SEPARATOR . $entry;
// Check if the entry is a symlink
if (is_link($entryPath)) {
continue;
}
// Check if the entry is a directory
if (is_dir($entryPath)) {
// Recursively scan the directory
$entries_array = recursiveScan($entryPath, $entries_array);
} elseif (is_readable($entryPath)) {
// Add the file to the writable array
$entries_array['file_readable'][] = $entryPath;
} else {
// Add the file to the non-writable array
$entries_array['file_not_readable'][] = $entryPath;
}
}
// Close the directory
closedir($handle);
// Return the entries array
return $entries_array;
}
/**
*
* Sort array of list file by lastest modified time
*
* @param array $files Array of files
*
* @return array
*
*/
function sortByLastModified($files)
{
@array_multisort(array_map('filemtime', $files), SORT_DESC, $files);
return $files;
}
/**
*
* Recurisively list a file by descending modified time
*
* @param string $path
*
* @return array
*
*/
function getSortedByTime($path) // :array
{
// Get the writable and non-writable files from the directory
$result = recursiveScan($path);
$readable = $result['file_readable'];
//$notReadable = isset($result['file_not_readable']) ? $result['file_not_readable'] : array();
if (isset($result['file_not_readable'])) {
$notReadable = $result['file_not_readable'];
} else {
$notReadable = array();
}
// Sort the writable files by their last modified time
$readable = sortByLastModified($readable);
// Return the sorted files
return array(
'file_readable' => $readable,
'file_not_readable' => $notReadable,
);
}
/**
* Recursively list a file by descending modified time and extension.
*
* @param string $path The directory path to scan.
* @param array $ext An array of file extensions to filter.
* @return array An associative array containing two keys: 'file_readable' and 'file_not_readable'.
* Each key contains an array of file paths, sorted by their last modified time.
*/
function getSortedByExtension($path, $ext)
{
$result = getSortedByTime($path);
$fileReadable = $result['file_readable'];
//isset($result['file_not_readable']) ? $result['file_not_readable'] : false;
foreach ($fileReadable as $entry) {
$pathinfo = pathinfo($entry, PATHINFO_EXTENSION);
if (in_array($pathinfo, $ext)) {
$sortedWritableFile[] = $entry;
}
}
if (isset($fileNotWritable)) {
foreach ($fileNotWritable as $entry) {
$pathinfo = pathinfo($entry, PATHINFO_EXTENSION);
if (in_array($pathinfo, $ext)) {
$sortedNotWritableFile[] = $entry;
}
}
} else {
$sortedNotWritableFile = false;
}
return array(
'file_readable' => $sortedWritableFile,
'file_not_readable' => $sortedNotWritableFile
);
}
/**
* Get lowercase Array of tokens in a file
*
* @param string $filename
* @return array
*/
function getFileTokens($filename)
{
// Replace short PHP tags with PHP tags
$fileContent = file_get_contents($filename);
$fileContent = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $fileContent);
$fileContent = preg_replace('/<\?([^p=\w])/m', '<?php ', $fileContent);
// Get the file tokens
$tokens = token_get_all($fileContent);
// Create an output array
$output = array();
// Iterate over the tokens and add the token types to the output array
foreach ($tokens as $token) {
//$output[] = is_array($token) ? $token[1] : $token;
if (is_array($token)) {
$output[] = $token[1];
} else {
$output[] = $token;
}
}
// Remove any duplicate or empty tokens from the output array
$output = array_values(array_unique(array_filter(array_map("trim", $output))));
// Return the output array
return $output;
}
/**
* recursively search for a specific case within an array, including nested arrays.
*
* @param string $needle
* @param array $haystack
* @return array
*/
function inStringArray($needle, $haystack)
{
$matches = array();
foreach ($haystack as $key => $value) {
if (is_string($value)) {
// Check if string is found using strcasecmp
if (strcasecmp($value, $needle) === 0) {
$matches[] = $key;
}
} elseif (is_array($value)) {
// Recursively search within sub-arrays
$subMatches = inStringArray($needle, $value);
if (!empty($subMatches)) {
// Prepend current key to sub-matches
foreach ($subMatches as $subMatch) {
$matches[] = $key . '[' . $subMatch . ']';
}
}
}
}
return $matches;
}
/**
* Compare tokens and return array of matched tokens
*
* @param array $tokenNeedles
* @param array $tokenHaystack
* @return array
*/
function compareTokens($tokenNeedles, $tokenHaystack)
{
$output = array();
foreach ($tokenNeedles as $tokenNeedle) {
if (inStringArray($tokenNeedle, $tokenHaystack)) {
$output[] = $tokenNeedle;
}
}
return $output;
}
/**
* Return array of string from url
*
* @param string $url
* @return array
*/
function urlFileArray($url)
{
if (isset($GLOBALS['ch'])) {
curl_setopt($GLOBALS['ch'], CURLOPT_URL, $url);
// Handle potential cURL errors
if (curl_errno($GLOBALS['ch'])) {
$error_msg = curl_error($GLOBALS['ch']);
//curl_close($GLOBALS['ch']);
trigger_error("cURL error fetching URL: $error_msg", E_USER_WARNING);
return array();
}
$content = curl_exec($GLOBALS['ch']);
//curl_close($GLOBALS['ch']);
return explode("\n", $content);
} else if (isWorking('file_get_contents')) {
$context = stream_context_create(
array(
'http' => array(
'ignore_errors' => true, // Handle potential errors gracefully
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)
);
$content = file_get_contents($url, false, $context); // Use error suppression for cleaner handling
// If file_get_contents fails, return false
if ($content === false) {
trigger_error("Failed to fetch URL using file_get_contents", E_USER_WARNING);
return array();
}
return explode("\n", $content);
} else if (isWorking('file')) {
$content = @file($url, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Use error suppression for cleaner handling
// If file() fails, return false
if ($content === false) {
trigger_error("Failed to fetch URL using file", E_USER_WARNING);
return array();
}
return $content;
}
trigger_error("No suitable methods found to fetch URL content", E_USER_WARNING);
return array();
}
/**
* Get Online Vibes check, return gyatt if fail
*
* @param string $hashSum
* @param string $APIKey
* @return array|bool|null
*/
function vTotalCheckHash($hashSum, $APIKey)
{
if (!isset($GLOBALS['ch']) || empty($APIKey)) {
return false;
}
curl_setopt($GLOBALS['ch'], CURLOPT_URL, sprintf('https://www.virustotal.com/api/v3/files/%s', $hashSum));
curl_setopt($GLOBALS['ch'], CURLOPT_HTTPHEADER, array(sprintf('x-apikey: %s', $APIKey)));
if (curl_errno($GLOBALS['ch'])) {
$error_msg = curl_error($GLOBALS['ch']);
//curl_close($GLOBALS['ch']);
trigger_error("cURL error fetching URL: $error_msg", E_USER_WARNING);
return false;
}
$result = curl_exec($GLOBALS['ch']);
return json_decode($result, true);
}
$APIKey = array(
'',
);
$ext = array(
'php',
'phps',
'pht',
'phpt',
'phtm',
'phtml',
'phar',
'php3',
'php4',
'php5',
'php7',
'shtml',
'suspected'
);
$tokenNeedles = array(
// Obfuscation
'base64_decode',
'rawurldecode',
'urldecode',
'gzinflate',
'gzuncompress',
'str_rot13',
'convert_uu',
'htmlspecialchars_decode',
'bin2hex',
'hex2bin',
'hexdec',
'chr',
'strrev',
'goto',
'implode',
'strtr',
'extract',
'parse_str', //works like extract if only one argument is given.
'substr',
'mb_substr',
'str_replace',
'substr_replace',
'preg_replace', // able to do eval on match
'exif_read_data',
'readgzfile',
// Shell / Process
'eval',
'exec',
'shell_exec',
'system',
'passthru',
'pcntl_fork',
'fsockopen',
'proc_open',
'popen ',
'assert', // identical to eval
'posix_kill',
'posix_setpgid',
'posix_setsid',
'posix_setuid',
'proc_nice',
'proc_close',
'proc_terminate',
'apache_child_terminate',
'`',
// Server Information
'posix_getuid',
'posix_geteuid',
'posix_getegid',
'posix_getpwuid',
'posix_getgrgid',
'posix_mkfifo',
'posix_getlogin',
'posix_ttyname',
'getenv',
'proc_get_status',
'get_cfg_var',
'disk_free_space',
'disk_total_space',
'diskfreespace',
'getlastmo',
'getmyinode',
'getmypid',
'getmyuid',
'getmygid',
'fileowner',
'filegroup',
'get_current_user',
'pathinfo',
'getcwd',
'sys_get_temp_dir',
'basename',
'phpinfo',
'php_uname',
// Database
'mysql_connect',
'mysqli_connect',
'mysqli_query',
'mysql_query',
// I/O
'fopen',
'fsockopen',
'file_put_contents',
'file_get_contents',
'url_get_contents',
'stream_get_meta_data',
'move_uploaded_file',
'$_files',
'copy',
'include',
'include_once',
'require',
'require_once',
'__file__',
// Miscellaneous
'mail',
'putenv',
'curl_init',
'tmpfile',
'allow_url_fopen',
'ini_set',
'set_time_limit',
'session_start',
'symlink',
'__halt_compiler',
'__compiler_halt_offset__',
'error_reporting',
'create_function',
'get_magic_quotes_gpc',
'$auth_pass',
'$password',
'$pass',
'$SISTEMIT_COM_ENC',
);
$whitelistMD5Sums = urlFileArray('https://raw.githubusercontent.com/Cvar1984/sussyfinder/main/whitelist.txt');
$blacklistMD5Sums = urlFileArray('https://raw.githubusercontent.com/Cvar1984/sussyfinder/main/blacklist.txt');
?>
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Sussy Finder</title>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Ubuntu+Mono&display=swap');
body {
font-family: 'Ubuntu Mono', monospace;
color: #8a8a8a;
}
table {
border-spacing: 0;
padding: 10px;
border-radius: 7px;
border: 3px solid #d6d6d6;
}
tr,
td {
padding: 7px;
}
th {
color: #8a8a8a;
padding: 7px;
font-size: 25px;
}
input[type=submit]:focus {
background: #ff9999;
color: #fff;
border: 3px solid #ff9999;
}
input[type=submit]:hover {
border: 3px solid #ff9999;
cursor: pointer;
}
input[type=text]:hover {
border: 3px solid #ff9999;
}
input {
font-family: 'Ubuntu Mono', monospace;
}
input[type=text] {
border: 3px solid #d6d6d6;
outline: none;
padding: 7px;
color: #8a8a8a;
width: 100%;
border-radius: 7px;
}
input[type=submit] {
color: #8a8a8a;
border: 3px solid #d6d6d6;
outline: none;
background: none;
padding: 7px;
width: 100%;
border-radius: 7px;
}
</style>
</head>
<body>
<script type="text/javascript">
function copytable(el) {
var urlField = document.getElementById(el)
var range = document.createRange()
range.selectNode(urlField)
window.getSelection().addRange(range)
document.execCommand('copy')
}
</script>
<form method="post">
<table align="center" width="30%">
<tr>
<th>
Sussy Finder
</th>
</tr>
<tr>
<td>
<input type="text" name="dir" value="<?= getcwd() ?>">
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="SEARCH">
</td>
</tr>
<?php if (isset($_POST['submit'])) { ?>
<tr>
<td>
<span style="font-weight:bold;font-size:25px;">RESULT</span>
<input type=button value="Copy to Clipboard" onClick="copytable('result')">
</td>
</tr>
</table>
<table id="result" align="center" width="30%">
<?php
$path = $_POST['dir'];
$result = getSortedByExtension($path, $ext);
$fileReadable = $result['file_readable'];
$fileNotWritable = $result['file_not_readable'];
$fileReadable = sortByLastModified($fileReadable);
$currentKeyIndex = 0;
$actionCount = 0;
$duplicateFiles = array();
foreach ($fileReadable as $file) {
$filePath = str_replace('\\', '/', $file);
$filePath = str_replace('//', '/', $file);
$fileSum = md5_file($filePath);
if (in_array($fileSum, $whitelistMD5Sums)) { // if in whitelist skip
continue;
} elseif (in_array($fileSum, $blacklistMD5Sums)) { // if in blacklist alert and remove
printf('<tr><td><span style="color:red;">%s (Blacklist)(%s)</span></td></tr>', $filePath, $fileSum);
unlink($filePath);
continue;
} elseif (($duplicatePath = array_search($fileSum, $duplicateFiles)) !== false) {
printf('<tr><td><span style="color:#212121;">%s -> %s(%s)</span></td></tr>', $filePath, $duplicatePath, $fileSum);
continue;
}
$duplicateFiles[$filePath] = $fileSum;
$vTotalRes = vTotalCheckHash($fileSum, $APIKey[$currentKeyIndex]);
$actionCount++;
// keep track of the number of actions performed within a loop
//if ($actionCount >= 240) {
if ($actionCount >= 1) {
$currentKeyIndex = ($currentKeyIndex + 1) % count($APIKey);
$actionCount = 0;
}
if (isset($vTotalRes['data'])) {
$matchedString = inStringArray('malicious', $vTotalRes); // matching casecmp
if (!empty($matchedString)) {
printf('<tr><td><span style="color:#ff0000;">%s (VTotal Webshell)(%s)</span></td></tr>', $filePath, $fileSum);
unlink($filePath);
continue;
} else if ($vTotalRes['data']['attributes']['total_votes']['malicious'] > 0) {
printf('<tr><td><span style="color:#eed202;">%s (VTotal Malicious)(%s)</span></td></tr>', $filePath, $fileSum);
//unlink($filePath);
continue;
}
}
$tokens = getFileTokens($filePath);
$cmp = compareTokens($tokens, $tokenNeedles);
$cmp = implode(', ', $cmp);
if (!empty($cmp)) {
printf('<tr><td><span style="color:#3f3f3f;">%s (%s)</span></td></tr>', $filePath, $cmp);
}
}
}
?>
</table>
</form>
</body>
</html>