-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexploit-scanner-cli.php
265 lines (234 loc) · 8.51 KB
/
exploit-scanner-cli.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
<?php
if ( class_exists( "WP_CLI_Command" ) ) {
/**
* WordPress Exploit Scanner
*/
class ExploitScannerCLI extends WP_CLI_Command {
/**
* Start Exploit Scanner scan.
*
* ## OPTIONS
*
* [--show-suspicious-styles]
* : Search for suspicious styles - (display:none and visibility:hidden can be used to hide spam, but may cause many false positives)
*
* [--file-size=<size-in-kb>]
* : Upper file size limit in KB - (files larger than this are skipped and will be listed at the end of scan)
*
* [--files-per-block=<no-of-files>]
* : Number of files per batch - (to help reduce memory limit errors the scan processes a series of file batches)
*
* [--report_all_unknown_files]
* : Reports also unkown files outside of wp-includes, wp-admin and wp root directory
*
* [--export-csv=<file-name>]
* : It will export result to specified csv file
*
* ## EXAMPLES
*
* wp exploit-scanner scan
*
* @synopsis
*/
function scan( $args, $assoc_args ) {
$default = array(
'show-suspicious-styles' => true,
'file-size' => 400,
'files-per-block' => 250,
'report_all_unknown_files' => false,
'export-csv' => false
);
$assoc_args = wp_parse_args( $assoc_args, $default );
if ( !is_numeric( $assoc_args[ 'file-size' ] ) ) {
WP_CLI::error( "--file-size : Upper file size limit should be numeric" );
return;
}
if ( !is_numeric( $assoc_args[ 'files-per-block' ] ) ) {
WP_CLI::error( "--files-per-block : Number of files per batch should be numeric" );
return;
}
$fes_args = array(
'start' => 0,
'fsl' => intval( $assoc_args[ 'file-size' ] ),
'max' => intval( $assoc_args[ 'files-per-block' ] ),
'report_all_unknown_files' => $assoc_args[ 'report_all_unknown_files' ],
'display_pattern' => $assoc_args[ 'show-suspicious-styles' ]
);
WP_CLI::warning( "Star File Scanning..." );
$scan_flag = true;
$scanner = new File_Exploit_Scanner( ABSPATH, $fes_args );
// Fix for save transient error
delete_transient( 'exploitscanner_results_trans' );
delete_transient( 'exploitscanner_files' );
$file_progress = new \cli\progress\Bar( 'Progress', 1000 );
$file_progress->tick();
while ( $scan_flag ) {
$result = $scanner->run();
if ( is_wp_error( $result ) ) {
$file_progress->finish();
WP_CLI::error( 'Files list not properly saved as a transient' );
$scan_flag = false;
} else if ( $result ) {
$scan_flag = false;
$file_progress->finish();
WP_CLI::success( 'All files scanned' );
} else {
$file_progress->tick( $scanner->max_batch_size );
$scanner->start = $scanner->start + $scanner->max_batch_size;
}
}
WP_CLI::warning( "Star Database Scanning..." );
$db_scanner = new DB_Exploit_Scanner();
$db_scanner->run();
WP_CLI::success( 'Database scanned' );
$this->result( $args, $assoc_args );
}
/**
* Show last Exploit Scanner scan result.
*
* [--export-csv=<file-name>]
* : It will export result to specified csv file
* ## EXAMPLES
*
* wp exploit-scanner result
*
* @synopsis
*/
function result( $args, $assoc_args ) {
$default = array( 'export-csv' => false );
$assoc_args = wp_parse_args( $assoc_args, $default );
delete_transient( 'exploitscanner_results_trans' );
delete_transient( 'exploitscanner_files' );
$results = get_option( 'exploitscanner_results' );
if ( $assoc_args[ 'export-csv' ] !== false ) {
$export_csv = array();
$export_csv [ ] = array( 'Level', 'Location', 'Description', 'What was matched' );
$colorize_start = '';
$colorize_end = '';
} else {
$colorize_start = '%R';
$colorize_end = '%n';
}
foreach ( array( 'severe', 'warning', 'note' ) as $l ) {
if ( !empty( $results[ $l ] ) ) {
//Level ' . ucwords($l) . ' (' . count($results[$l]) . ' matches)
$lines = array();
if ( false === $assoc_args[ 'export-csv' ] ) {
WP_CLI::line( '' );
WP_CLI::line( '' );
WP_CLI::line( 'Level ' . ucwords( $l ) . ' (' . count( $results[ $l ] ) . ' matches)' );
}
if ( count( $results[ $l ] ) > 0 ) {
//$#$#
foreach ( $results[ $l ] as &$r ) {
$r[ 'Level' ] = ucwords( $l );
$r[ 'Location' ] = '';
if ( isset( $r[ 'loc' ] ) ) {
$r[ 'Location' ] .= $r[ 'loc' ];
if ( isset( $r[ 'line_no' ] ) ) {
$r[ 'Location' ] .= ':' . $r[ 'line_no' ];
}
}
$r[ 'Description' ] = '';
if ( isset( $r[ 'desc' ] ) ) {
$r[ 'Description' ] .= $r[ 'desc' ];
}
$r[ 'What was matched' ] = '';
if ( isset( $r[ 'line' ] ) ) {
$text = $r[ 'line' ];
if ( strlen( $text ) > 200 ) {
$start = strpos( $text, '$#$#' ) - 50;
if ( $start < 0 ) {
$start = 0;
}
$end = strrpos( $text, '#$#$' ) + 50;
$text = substr( $text, $start, $end - $start + 1 );
}
$start = strpos( $text, '$#$#' );
$temp_text = substr( $text, $start + 4 );
$end = strrpos( $temp_text, '#$#$' );
$highlight_text = substr( $temp_text, 0, $end );
$replace_text = '$#$#' . $highlight_text . '#$#$';
if ( false === $assoc_args[ 'export-csv' ] ) {
$r[ 'What was matched' ] = htmlspecialchars_decode( str_replace( $replace_text, \cli\Colors::colorize( $colorize_start . $highlight_text . $colorize_end ), $text ) );
} else {
$r[ 'What was matched' ] = htmlspecialchars_decode( str_replace( $replace_text, $colorize_start . $highlight_text . $colorize_end, $text ) );
}
}
if ( false !== $assoc_args[ 'export-csv' ] ) {
$export_csv [ ] = array(
$r[ 'Level' ],
$r[ 'Location' ],
$r[ 'Description' ],
$r[ 'What was matched' ]
);
}
}
if ( false === $assoc_args[ 'export-csv' ] ) {
$fields = array( 'Location', 'Description', 'What was matched' );
\WP_CLI\Utils\format_items( 'table', $results[ $l ], $fields );
}
}
}
}
if ( false !== $assoc_args[ 'export-csv' ] ) {
if ( file_exists( $assoc_args[ 'export-csv' ] ) ) {
WP_CLI::warning( sprintf( "File already exists. The following file will be overwritten %s", $assoc_args[ 'export-csv' ] ) );
}
$fp = fopen( $assoc_args[ 'export-csv' ], 'w+' );
foreach ( $export_csv as $fields ) {
fputcsv( $fp, $fields );
}
WP_CLI::success( sprintf( "File created: %s", $assoc_args[ 'export-csv' ] ) );
}
}
/**
* Generate Hash for current/specified WordPress.
*
*## OPTIONS
*
* [<wordpress-path>]
* : WordPress Directory Path to generate hash for
*
* ## EXAMPLES
*
* wp exploit-scanner generate <dir-name>
*
* @synopsis
*/
function generate( $args, $assoc_args ) {
if ( count( $args ) > 0 ) {
$path = $args[ 0 ];
} else {
$path = ABSPATH;
}
$path = trailingslashit( realpath( $path ) );
WP_CLI::warning( 'Scan Path : ' . $path );
//Detect Version
if ( !file_exists( $path . 'wp-includes/version.php' ) ) {
WP_CLI::error( 'Not able to determine WordPress Version: Please check if it is valid WordPress directory' );
return;
}
preg_match_all( "/wp_version([^\']+)\'([^\']+)'/im", file_get_contents( $path . 'wp-includes/version.php' ), $version_matches );
$wordpress_version = $version_matches[ 2 ][ 0 ];
WP_CLI::warning( 'WordPress Version : ' . $wordpress_version );
$directory_it = new RecursiveDirectoryIterator( $path );
$file_name = 'hashes-' . $wordpress_version . '.php';
$full_hash_file_path = trailingslashit( dirname( __FILE__ ) ) . 'hashes/' . $file_name;
if ( file_exists( $full_hash_file_path ) ) {
WP_CLI::confirm( sprintf( '%s already exist, Are you sure you want to regenerate it again ?', $file_name ) );
}
$hash_file = fopen( $full_hash_file_path, "w" );
fwrite( $hash_file, '<?php' . PHP_EOL . '$filehashes = array(' );
$file_progress = new \cli\progress\Bar( 'Progress', 1100 );
foreach ( new RecursiveIteratorIterator( $directory_it ) as $file ) {
fwrite( $hash_file, "'" . str_replace( $path, '', $file ) . "' => '" . md5_file( $file ) . "'," . PHP_EOL );
$file_progress->tick();
}
fwrite( $hash_file, ');' . PHP_EOL );
fclose( $file );
WP_CLI::success( sprintf( "File created: %s", $file_name ) );
}
}
WP_CLI::add_command( 'exploit-scanner', 'ExploitScannerCLI' );
}