-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclass-antivirus-checkinternals.php
296 lines (247 loc) · 5.95 KB
/
class-antivirus-checkinternals.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
<?php
/**
* AntiVirus: Check Blog Internals module.
*
* @package AntiVirus
* @subpackage CheckInternals
*/
// Quit.
defined( 'ABSPATH' ) || exit;
/**
* AntiVirus_CheckInternals
*
* @since 1.4 Ported from "Checksum Verifier" plugin.
*/
class AntiVirus_CheckInternals extends AntiVirus {
/**
* Check blog internals like theme files and the permalink structure.
*/
public static function check_blog_internals() {
// Execute checks.
if ( ! self::_check_theme_files() && ! self::_check_permalink_structure() ) {
return;
}
// Send notification.
self::_send_warning_notification(
esc_html__( 'Virus suspected', 'antivirus' ),
sprintf(
"%s\r\n%s",
esc_html__( 'The daily antivirus scan of your blog suggests alarm.', 'antivirus' ),
get_bloginfo( 'url' )
)
);
// Store alert in database.
self::_update_option(
'cronjob_alert',
1
);
}
/**
* Check the files of the currently activated theme.
*
* @return array|false Results array or false on failure.
*/
public static function _check_theme_files() {
// Check if there are any files.
$files = self::_get_theme_files();
if ( ! $files ) {
return false;
}
$results = array();
// Loop through files.
foreach ( $files as $file ) {
$result = self::check_theme_file( $file );
if ( $result ) {
$results[ $file ] = $result;
}
}
// Return results if found.
if ( ! empty( $results ) ) {
return $results;
}
return false;
}
/**
* Check a single file.
*
* @param string $file File path.
*
* @return array|false Results array or false on failure.
*/
public static function check_theme_file( $file ) {
// Simple file path check.
if ( filter_var( $file, FILTER_SANITIZE_URL ) !== $file ) {
return false;
}
// Sanitize file string.
if ( validate_file( $file ) !== 0 ) {
return false;
}
// No file?
if ( ! $file ) {
return false;
}
// Get file content.
$content = self::_get_file_content( $file );
if ( ! $content ) {
return false;
}
$results = array();
// Loop through lines.
foreach ( $content as $num => $line ) {
$result = self::_check_file_line( $line, $num );
if ( $result ) {
$results[ $num ] = $result;
}
}
// Return results if found.
if ( ! empty( $results ) ) {
return $results;
}
return false;
}
/**
* Check the permalink structure.
*
* @return array|false Results array or false on failure.
*/
private static function _check_permalink_structure() {
$structure = get_option( 'permalink_structure' );
if ( ! $structure ) {
return false;
}
// Regex check.
preg_match_all(
self::_php_match_pattern(),
$structure,
$matches
);
// Save matches.
if ( $matches[1] ) {
return $matches[1];
}
return false;
}
/**
* Get the regular expression for all disallowed words/functions.
*
* @return string Regular expression.
*/
private static function _php_match_pattern() {
return '/\b(assert|file_get_contents|curl_exec|popen|proc_open|unserialize|eval|base64_encode|base64_decode|create_function|exec|shell_exec|system|passthru|ob_get_contents|file|curl_init|readfile|fopen|fsockopen|pfsockopen|fclose|fread|file_put_contents)\b\s*?\(/';
}
/**
* Check a specific line number.
*
* @param string $line The line to check.
* @param int $num Line number.
*
* @return array|bool An array of matched lines or false on failure.
*/
private static function _check_file_line( $line, $num ) {
// Trim value.
$line = trim( (string) $line );
// Make sure the values aren't empty.
if ( ! $line || ! isset( $num ) ) {
return false;
}
$results = array();
$output = array();
// Check if the regex matches.
preg_match_all(
self::_php_match_pattern(),
$line,
$matches
);
// Save matches.
if ( $matches[1] ) {
$results = $matches[1];
}
// Look for frames.
preg_match_all(
'/<\s*?(i?frame)/',
$line,
$matches
);
// Save matches.
if ( $matches[1] ) {
$results = array_merge( $results, $matches[1] );
}
// Look for the MailPoet vulnerability.
preg_match_all(
'/explode\s?\(chr\s?\(\s?\(\d{3}\s?-\s?\d{3}\s?\)\s?\)\s?,/',
$line,
$matches
);
// Save matches.
if ( $matches[0] ) {
$results = array_merge( $results, $matches[0] );
}
if ( $results ) {
// Remove duplicates.
$results = array_unique( $results );
// Get whitelist.
$md5 = self::_get_white_list();
// Loop through results.
foreach ( $results as $tag ) {
$string = str_replace(
$tag,
'@span@' . $tag . '@/span@',
self::_get_dotted_line( $line, $tag )
);
// Add line to output if it's not on the whitelist.
if ( ! in_array( md5( $num . $string ), $md5 ) ) {
$output[] = $string;
}
}
return $output;
}
return false;
}
/**
* Get file contents
*
* @param string $file File path.
*
* @return array An array containing all the lines of the file.
*/
private static function _get_file_content( $file ) {
return file( WP_CONTENT_DIR . $file );
}
/**
* Shorten a string, append ellipsis.
*
* @param string $line The line.
* @param string $tag The tag we're looking for.
* @param int $max Maximum number of chars on each side.
*
* @return string|false The shortened string or false on failure.
*/
private static function _get_dotted_line( $line, $tag, $max = 100 ) {
// No values?
if ( ! $line || ! $tag ) {
return false;
}
// Return tag if it's higher than the maximum.
if ( strlen( $tag ) > $max ) {
return $tag;
}
// Get difference between the tag and the maximum.
$left = round( ( $max - strlen( $tag ) ) / 2 );
// Quote regular expression characters.
$tag = preg_quote( $tag, '/' );
// Shorten string on the right side.
$output = preg_replace(
'/(' . $tag . ')(.{' . $left . '}).{0,}$/',
'$1$2 ...',
$line
);
// Shorten string on the left side.
$output = preg_replace(
'/^.{0,}(.{' . $left . ',})(' . $tag . ')/',
'... $1$2',
$output
);
return $output;
}
}