forked from nexylan/PHPAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.php
217 lines (183 loc) · 6.36 KB
/
scan.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
<?php
/*
* This file is part of the Nexylan packages.
*
* (c) Nexylan SAS <contact@nexylan.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
ini_set('memory_limit', '512M');
require_once __DIR__.'/vendor/autoload.php';
// We are Gentoo users, we love color in bash shell :)
$colors = new Colors();
// Detect more than 10000 consecutive characters on first line
function detect_obfuscated($filecontent)
{
$weirdlength = 1000;
//if (isset($filecontent[1]) && strlen($filecontent[1]) > $weirdlength && preg_match("/[A-Za-z0-9\\\]{$weirdlength}/",$filecontent[1])) { // If a line contains more than 10,000 characters, write it to stdout
for ($line = 0; $line <= 1; ++$line) {
if (isset($filecontent[$line]) && strlen($filecontent[$line]) > $weirdlength) { // If a line contains more than 10,000 characters, write it to stdout
return true;
}
}
return false;
}
// Detect eval functions on first line
function detect_onelineshell($filecontent)
{
$lines = 3;
for ($i = 0; $i < $lines; ++$i) {
if (isset($filecontent[$i]) && preg_match("/(eval[\s]*\(|system\(|\\\x)/", $filecontent[$i])) {
return true;
}
}
if (isset($filecontent[count($filecontent) - 1]) && preg_match("/(eval\(|system\()/", $filecontent[count($filecontent) - 1])) {
return true;
}
return false;
}
// Detect php files in upload folder
function detect_upload($filename)
{
if (preg_match('#/wp-content/uploads#', $filename) && filesize($filename) > 1024) {
return true;
}
return false;
}
// Detect webshells patterns
function detect_shell($filecontent)
{
global $shells;
foreach ($shells as $shell) {
$shell = trim($shell);
if (preg_match("/$shell/", implode($filecontent))) {
return true;
}
}
return false;
}
// Check whitelist
function in_whitelist($filename)
{
global $whitelist;
foreach ($whitelist as $wl) {
if (false !== strpos($filename, trim($wl))) {
return true;
}
}
return false;
}
// Display a report of infected file
function report_file($file, $reason)
{
global $colors;
if (!in_whitelist($file)) {
echo $colors->getColoredString("Infected file (reason : $reason) :\n", 'red');
echo $colors->getColoredString("\t$file\n", 'light_blue');
}
}
// Delete the infected file with/without confirmation
function delete_file($file, $content, $confirmation)
{
global $colors;
echo $colors->getColoredString("This file ($file) containing the following code :\n", 'cyan');
echo "\t".$content."\n";
if ($confirmation) {
echo $colors->getColoredString('Delete ? (y/n)', 'cyan');
$handle = fopen('php://stdin', 'r');
$input = fgets($handle);
if (trim($input) == 'y') {
unlink($file);
} else {
echo "$input";
}
} else {
unlink($file);
}
}
// Propose and apply patch
function patch_file($file, $content)
{
global $colors;
$newfile = preg_replace("/^.*<\?php/", '<?php', $content[0]);
$fp = fopen("$file.fixed", 'w');
fwrite($fp, $newfile);
for ($i = 1; $i < count($content); ++$i) {
fwrite($fp, $content[$i]);
}
fclose($fp);
exec("diff -u $file $file.fixed > fix.patch");
if (filesize('fix.patch') > 0) {
$diff = file('fix.patch');
echo $colors->getColoredString("I'm proposing the following patch. What do you think ?\n", 'cyan');
echo "\n".implode($diff)."\n";
echo $colors->getColoredString('Apply ? (y/n)', 'cyan');
$handle = fopen('php://stdin', 'r');
$input = fgets($handle);
if (trim($input) == 'y') {
exec("patch $file < fix.patch");
unlink('fix.patch');
unlink("$file.fixed");
} else {
echo 'No patch applied';
}
}
unlink('fix.patch');
}
// Main(void)
if (empty($argv[1])) {
die("Usage: php find.php directory_to_scan > infected.txt\n");
} else {
echo $colors->getColoredString('Scanning '.$argv[1]." for potential obfuscated malware...\n\n", 'green');
$data = array();
if (file_exists($argv[1])) {
if (is_dir($argv[1])) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($argv[1]), RecursiveIteratorIterator::SELF_FIRST); // Grab array of the entire structures of $argv[1] (a directory)
} else {
// Scan file directly
$files = array($argv[1]);
}
$c = 0; // Counter for files processed
$f = 0; // Counter for files with potential malware
// Preload data
$shells = file(dirname(__FILE__).'/data/webshells.txt');
$whitelist = file(dirname(__FILE__).'/data/whitelist.txt');
foreach ($files as $file) {
if (is_dir($file) === true) { // Not in use, was used to check directory traversal was working properly
} else { // If is file
if (preg_match("/\.php$/", $file)) { // Currently only selects PHP scripts for scanning
$arr = file($file); // Puts each line of the file into an array element
if (detect_shell($arr)) {
report_file($file, 'Shell script pattern');
++$f;
continue;
}
if (detect_obfuscated($arr)) {
report_file($file, 'obfuscated code on first line');
++$f;
continue;
}
if (detect_onelineshell($arr)) {
report_file($file, 'First-line file with eval');
if (count($arr) == 1) {
delete_file($file, implode($arr), true);
} else {
patch_file($file, $arr);
}
++$f;
continue;
}
if (detect_upload($file)) {
report_file($file, 'PHP file in wordpress upload dir');
++$f;
continue;
}
}
}
++$c;
}
} else {
die('The specified scan path does not exist!');
}
}