-
Notifications
You must be signed in to change notification settings - Fork 21
/
wp-waf.php
425 lines (370 loc) · 15.4 KB
/
wp-waf.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
<?php
/*
Plugin Name: WP WAF
Plugin URI: http://wordpress.org/plugins/wp-waf/
Description: WP WAF - WordPress Application Firewall. Protects against web attacks. Email notification is disabled by default, notification can be activated and configured in <strong>Settings -> WP WAF</strong>. Go to your <a href="options-general.php?page=wp_waf">WP WAF configuration</a> page.
Author: Gianni 'guelfoweb' Amato
Version: 2.0
Author URI: https://github.com/guelfoweb/wp-waf/
*/
$blog_wpurl = get_bloginfo( 'wpurl' );
$blog_name = get_bloginfo( 'name' );
$admin_email = get_option( 'admin_email' );
/* Set alert */
$alert = "<br><center>";
$alert .= "<h2>WP_WAF</h2>";
$alert .= "<img src='" . plugin_dir_url( __FILE__ ) . "stuff/wp_waf.png' /><br>";
$alert .= "<b><font color=\"red\">Your request has been blocked!</font></b><br><br>";
$alert .= "<i>See <a href='" . plugin_dir_url( __FILE__ ) . "stuff/README'>README</a> file for more info.</i>";
$alert .= "</center>";
/* Set filter */
$xss = "javascript|vbscript|expression|applet|meta|xml|blink|";
$xss .= "link|style|script|embed|object|iframe|frame|frameset|";
$xss .= "ilayer|layer|bgsound|title|base|form|img|body|href|div|cdata";
$ua = "curl|wget|winhttp|HTTrack|clshttp|loader|email|harvest|extract|grab|miner|";
$ua .= "libwww-perl|acunetix|sqlmap|python|nikto|scan";
$sql = "[\x22\x27](\s)*(or|and)(\s).*(\s)*\x3d|";
$sql .= "cmd=ls|cmd%3Dls|";
$sql .= "(drop|alter|create|truncate).*(index|table|database)|";
$sql .= "insert(\s).*(into|member.|value.)|";
$sql .= "(select|union|order).*(select|union|order)|";
$sql .= "0x[0-9a-f][0-9a-f]|";
$sql .= "benchmark\([0-9]+,[a-z]+|benchmark\%28+[0-9]+%2c[a-z]+|";
$sql .= "eval\(.*\(.*|eval%28.*%28.*|";
$sql .= "update.*set.*=|delete.*from";
$traversal = "\.\.\/|\.\.\\|%2e%2e%2f|%2e%2e\/|\.\.%2f|%2e%2e%5c";
$rfi = "%00|";
$rfi .= "(?:((?:ht|f)tp(?:s?)|file|webdav)\:\/\/|~\/|\/).*\.\w{2,3}|";
$rfi .= "(?:((?:ht|f)tp(?:s?)|file|webdav)%3a%2f%2f|%7e%2f%2f).*\.\w{2,3}";
/* Block request and send email */
function wp_waf_email( $attack_type, $log, $matched, $via ) {
global $admin_email, $blog_wpurl, $blog_name;
$settings = (array) get_option( 'waf_settings' );
$waf_email = isset( $settings['waf_email'] ) ? $settings['waf_email'] : $admin_email;
$waf_msg = isset( $settings['waf_msg'] ) ? $settings['waf_msg'] : '';
/* Compose email */
$subject = "WP_WAF - $blog_name";
$body = "== Attack Details ==\n\n";
$body .= "TYPE: $attack_type\n";
$body .= "MATCHED: \"$matched\"\n";
$body .= "ACTION: Blocked\n";
$body .= "$log";
/* Send email */
if ( isset( $settings[$via] ) && $waf_email != "" ) {
@mail( $waf_email, $subject, $body );
}
global $alert;
switch ( $waf_msg ) {
case "":
die( $alert );
break;
case "waf_logo":
die ( $alert );
break;
case "waf_blank":
die( "" );
break;
}
}
/* Attack filter */
function wp_waf_filter( $content ) {
$req_method = $_SERVER['REQUEST_METHOD'];
$req_referr = $_SERVER['HTTP_REFERER'];
$req_uagent = $_SERVER['HTTP_USER_AGENT'];
$req_query = $_SERVER['QUERY_STRING'];
$req_uri = $_SERVER['REQUEST_URI'];
$req_ip = getenv( 'REMOTE_ADDR' );
$url = ( !empty( $_SERVER['HTTPS'] ) ) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$time = date( "M j G:i:s Y" );
/* Info User Log */
$msg = "\nDATE/TIME: ".$time;
$msg .= "\n\nFROM IP: http://whois.domaintools.com/".$req_ip;
$msg .= "\nURI: ".$req_uri;
$msg .= "\nSTRING: ".$req_query;
$msg .= "\nMETHOD: ".$req_method;
$msg .= "\nUSERAGENT: ".$req_uagent;
$msg .= "\nREFERRER: ".$req_referr;
$msg .= "\n";
global $xss, $ua, $traversal, $sql, $rfi;
$settings = (array) get_option( 'waf_settings' );
/* Method Blacklist*/
if ( preg_match( "/^(TRACE|DELETE|TRACK)/i", $req_method, $matched ) ) {
wp_waf_email( 'Method Blacklist', $msg, $matched[1], 'waf_method' );
}
/* Referrer */
elseif ( preg_match( "/<[^>]*(".$xss.")[^>]*>/i", $req_referr, $matched ) ) {
wp_waf_email( 'Referrer XSS', $msg, $matched[1], 'waf_referrer' );
}
/* User Agent Empty */
elseif ( preg_match( "/(^$)/i", $req_uagent, $matched ) ) {
wp_waf_email( 'User Agent Empty', $msg, $matched[1], 'waf_useragent_blank' );
}
/* User Agent Blacklist */
elseif ( preg_match( "/^(".$ua.").*/i", $req_uagent, $matched ) ) {
wp_waf_email( 'User Agent Blacklist', $msg, $matched[1], 'waf_useragent' );
}
/* Query - > 255 */
elseif ( strlen( $req_query ) > 255 ) {
if ( $settings['waf_query_too_long'] != "" ) {
wp_waf_email( 'Query Too Long', $msg, '> 255', 'waf_query_too_long' );
}
}
/* Query - Cross Site Scripting */
elseif ( preg_match( "/(<|<.)[^>]*(".$xss.")[^>]*>/i", $req_query, $matched ) ) {
wp_waf_email( 'Query XSS', $msg, $matched[1], 'waf_query' );
}
elseif ( preg_match( "/((\%3c)|(\%3c).)[^(\%3e)]*(".$xss.")[^(\%3e)]*(%3e)/i", $req_query, $matched ) ) {
wp_waf_email( 'Query XSS', $msg, $matched[1], 'waf_query' );
}
/* Query - traversal */
elseif ( preg_match( "/^.*(".$traversal.").*/i", $req_query, $matched ) ) {
wp_waf_email( 'Query traversal', $msg, $matched[1], 'waf_query' );
}
/* Query - Remote File Inclusion */
elseif ( preg_match( "/^.*(".$rfi.").*/i", $req_query, $matched ) ) {
wp_waf_email( 'Query RFI', $msg, $matched[1], 'waf_query' );
}
/* Query - Sql injection */
elseif ( preg_match( "/^.*(".$sql.").*/i", $req_query, $matched ) ) {
wp_waf_email( 'Query SQL', $msg, $matched[1], 'waf_query' );
}
}
add_action( 'posts_selection', 'wp_waf_filter' );
function wp_waf_install() {
/* echo getcwd() = wp-admin */
$htaccess = '../.htaccess';
$htaback = '../original.htaccess';
$htawpwaf = '../wp-content/plugins/wp-waf/stuff/wp-waf.htaccess';
if ( file_exists( $htawpwaf ) ) {
/* from wordpress repository */
$htawpwaf = '../wp-content/plugins/wp-waf/stuff/wp-waf.htaccess';
} else {
/* form github repository */
$htawpwaf = '../wp-content/plugins/wp-waf-master/stuff/wp-waf.htaccess';
}
/* Configure .htaccess */
/* verify is .htaccss is ok */
if ( file_exists( $htaccess ) ) {
if ( file_exists( $htaback ) ) {
/* get content original.htaccess and wp-waf.htaccess */
$htaccess_content = file_get_contents($htaback, true);
$htawpwaf_content = file_get_contents($htawpwaf, true);
/* remove original .htaccess */
unlink( $htaccess );
/* write a new .htaccess */
$fh = fopen($htaccess, 'w') or die("can't open .htaccess");
fwrite($fh, $htaccess_content);
fwrite($fh, "\n");
fwrite($fh, $htawpwaf_content);
fclose($fh);
} else {
/* make backup */
copy( $htaccess, $htaback );
/* get content original.htaccess and wp-waf.htaccess */
$htaccess_content = file_get_contents($htaccess, true);
$htawpwaf_content = file_get_contents($htawpwaf, true);
/* remove original .htaccess */
unlink( $htaccess );
/* write a new .htaccess */
$fh = fopen($htaccess, 'w') or die("can't open .htaccess");
fwrite($fh, $htawpwaf_content);
fwrite($fh, "\n");
fwrite($fh, $htaccess_content);
fclose($fh);
}
} else {
echo "Sorry, .htaccess not found. Reconfigure .htaccess file and reinstall WP WAF.";
}
}
if ( isset( $_GET['activate'] ) && $_GET['activate'] == 'true' ) {
wp_waf_install();
}
function wp_waf_init() {
register_setting( 'waf_settings_group', 'waf_settings', 'wp_waf_validation' );
}
add_action( 'admin_init', 'wp_waf_init' );
function wp_waf_validation( $input ) {
$input['waf_email'] = wp_filter_nohtml_kses( $input['waf_email'] );
return $input;
}
function wp_waf_settings_link( $links, $file ) {
static $this_plugin;
if ( empty( $this_plugin ) )
$this_plugin = plugin_basename( __FILE__ );
if ( $file == $this_plugin )
$links[] = '<a href="' . admin_url( 'options-general.php?page=wp_waf' ) . '">' . __( 'Settings', 'wp-waf' ) . '</a>';
return $links;
}
add_filter( 'plugin_action_links', 'wp_waf_settings_link', 10, 2 );
function wp_waf_plugin_menu() {
add_options_page( 'WAF Options', 'WP WAF', 'manage_options', 'wp_waf', 'wp_waf_settings' );
}
add_action( 'admin_menu', 'wp_waf_plugin_menu' );
function wp_waf_settings() {
global $admin_email, $blog_wpurl;
?>
<div class="wrap">
<h2><?php _e( 'WP WAF Settings', 'wp-waf' ); ?></h2>
<p><?php _e( 'The WP_WAF is a Web Application Firewall for Wordpress. Protects against web attacks.', 'wp-waf' ); ?></p>
<div class="clear" id="poststuff" style="width: 560px;">
<form method="post" action="options.php">
<?php
settings_fields( 'waf_settings_group' );
$settings = get_option( 'waf_settings' );
$waf_email = isset( $settings['waf_email'] ) ? $settings['waf_email'] : $admin_email;
$waf_query = isset( $settings['waf_query'] ) ? $settings['waf_query'] : false;
$waf_method = isset( $settings['waf_method'] ) ? $settings['waf_method'] : false;
$waf_referrer = isset( $settings['waf_referrer'] ) ? $settings['waf_referrer'] : false;
$waf_useragent = isset( $settings['waf_useragent'] ) ? $settings['waf_useragent'] : false;
$waf_useragent_blank = isset( $settings['waf_useragent_blank'] ) ? $settings['waf_useragent_blank'] : false;
$waf_query_too_long = isset( $settings['waf_query_too_long'] ) ? $settings['waf_query_too_long'] : false;
$waf_disable_dirlist = isset( $settings['waf_disable_dirlist'] ) ? $settings['waf_disable_dirlist'] : false;
// configure Directory Listing
$waftag = ' BEGIN WP WAF';
$dirlist = 'Options -Indexes';
$htaccess = '../.htaccess';
$content = file_get_contents($htaccess);
if ( isset( $settings['waf_disable_dirlist'] ) != "" ) {
// Disabling Directory Listing (secure mode)
if(strpos($content, $dirlist)) { #YES
echo "Directory Listing <font color='green'>is Disabled</font> (secure mode).";
} else {
if(strpos($content, $waftag)) { #YES
$add_dirlist = str_replace($waftag, $waftag."\n".$dirlist, $content);
file_put_contents($htaccess,$add_dirlist);
echo "Directory Listing <font color='green'>is now Disabled</font> (secure mode).";
} else {
echo "WP WAF Tag not found. Directory Listing <font color='red'>is Enabled by default</font> (INsecure mode).";
}
}
} else {
// Enabling Directory Listing (INsecure mode)
if(strpos($content, $dirlist)) {
if(strpos($content, $waftag)) { #YES
$rem_dirlist = str_replace($waftag."\n".$dirlist, $waftag, $content);
file_put_contents($htaccess,$rem_dirlist);
echo "Directory Listing <font color='red'>is now Enabled</font> (INsecure mode).";
} else {
echo "WP WAF Tag not found. Directory Listing <font color='red'>is Enabled by defaut</font> (INsecure mode).";
}
} else {
echo "Directory Listing <font color='red'>is Enabled</font> (INsecure mode).";
}
}
$waf_msg = isset( $settings['waf_msg'] ) ? $settings['waf_msg'] : 'waf_logo';
?>
<div class="postbox">
<h3 style="cursor: default;"><?php _e( 'Manage email notification of attacks', 'wp-waf' ); ?></h3>
<div class="inside">
<table class="widefat">
<tr valign="top">
<th scope="row">
<?php _e( 'Email Address' , 'wp-waf' ); ?>
</th>
<td>
<input type="text" name="waf_settings[waf_email]" value="<?php echo $waf_email; ?>" />
</td>
</tr>
</table>
</div>
</div>
<!-- /Manage email -->
<div class="postbox">
<h3 style="cursor: default;"><?php _e( 'Notifications for attacks type', 'wp-waf' ); ?></h3>
<div class="inside">
<table class="widefat">
<tr valign="top">
<th scope="row"><?php _e( 'Query', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_query]" value="1" id="waf_query" <?php checked( '1', $waf_query ); ?> />
</td>
</tr>
<tr valign="top" class="alternate">
<th scope="row"><?php _e( 'Method', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_method]" value="1" id="waf_method" <?php checked( '1', $waf_method ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Referrer', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_referrer]" value="1" id="waf_referrer" <?php checked( '1', $waf_referrer ); ?> />
</td>
</tr>
<tr valign="top" class="alternate">
<th scope="row"><?php _e( 'User Agent', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_useragent]" value="1" id="waf_useragent" <?php checked( '1', $waf_useragent ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'User Agent Empty', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_useragent_blank]" value="1" id="waf_useragent_blank" <?php checked( '1', $waf_useragent_blank ); ?> />
<?php echo '<em>'; _e( 'Not recommended', 'wp-waf' ); echo '</em>'; ?>
</td>
</tr>
</table>
</div>
</div>
<!-- /Notifications for attacks type -->
<div class="postbox">
<h3 style="cursor: default;"><?php _e( 'Under the Hood', 'wp-waf' ); ?></h3>
<div class="inside">
<table class="widefat">
<tr valign="top">
<th scope="row"><?php _e( 'Block Query > 255 char', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_query_too_long]" value="1" id="waf_query_too_long" <?php checked( '1', $waf_query_too_long ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Disable Directory Listing', 'wp-waf' ); ?></th>
<td>
<input type="checkbox" name="waf_settings[waf_disable_dirlist]" value="1" id="waf_disable_dirlist" <?php checked( '1', $waf_disable_dirlist ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Attack Message', 'wp-waf' ); ?></th>
<td>
<select name="waf_settings[waf_msg]">
<option <?php selected( 'waf_logo', $waf_msg ); ?> value="waf_logo">
<?php _e( 'Logo', 'wp-waf' ); ?>
</option>
<option <?php selected( 'waf_blank', $waf_msg ); ?> value="waf_blank">
<?php _e( 'Blank Page', 'wp-waf' ); ?>
</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<!-- /Under the Hood -->
<div class="postbox">
<h3 style="cursor: default;"><?php _e( 'Test Configuration', 'wp-waf' ); ?></h3>
<div class="inside">
<table class="widefat">
<tr valign="top">
<th scope="row">
<?php printf( __( '%1$sTest your configuration now!%2$s', 'wp-waf' ), '<a href="'.$blog_wpurl.'/?s=<script>alert(31337)</script>" target="_blank">', '</a>' ); ?>
</th>
</tr>
</table>
</div>
</div>
<!-- /Test configuration -->
<p class="submit">
<input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'wp-waf' ) ?>" />
</p>
</form>
</div>
<!-- /poststuff -->
</div>
<!-- /wrap -->
<?php }
// Close function wp_waf_settings()
function wp_waf_load_languages() {
load_plugin_textdomain( 'wp-waf', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'wp_waf_load_languages' );