-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwp-ban.php
322 lines (275 loc) · 9.51 KB
/
wp-ban.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
<?php
/*
Plugin Name: WP-Ban
Plugin URI: https://lesterchan.net/portfolio/programming/php/
Description: Ban users by IP, IP Range, host name, user agent and referer url from visiting your WordPress's blog. It will display a custom ban message when the banned IP, IP range, host name, user agent or referer url tries to visit you blog. You can also exclude certain IPs from being banned. There will be statistics recordered on how many times they attemp to visit your blog. It allows wildcard matching too.
Version: 1.69.1
Author: Lester 'GaMerZ' Chan
Author URI: https://lesterchan.net
Text Domain: wp-ban
*/
/*
Copyright 2022 Lester Chan (email : lesterchan@gmail.com)
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
### Create Text Domain For Translation
add_action( 'plugins_loaded', 'ban_textdomain' );
function ban_textdomain() {
load_plugin_textdomain( 'wp-ban' );
}
### Function: Ban Menu
add_action('admin_menu', 'ban_menu');
function ban_menu() {
add_options_page(__('Ban', 'wp-ban'), __('Ban', 'wp-ban'), 'manage_options', 'wp-ban/ban-options.php');
}
### Function: Get IP Address (http://stackoverflow.com/a/2031935)
function ban_get_ip() {
$banned_options = get_option( 'banned_options' );
if( intval( $banned_options['reverse_proxy'] ) === 1 ) {
foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
if ( array_key_exists( $key, $_SERVER ) === true ) {
foreach ( explode( ',', $_SERVER[$key] ) as $ip ) {
$ip = trim( $ip );
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
return esc_attr( $ip );
}
}
}
}
} else if( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
if( strpos( $ip, ',' ) !== false ) {
$ip = explode( ',', $ip );
$ip = $ip[0];
}
return esc_attr( $ip );
}
return '';
}
### Function: Preview Banned Message
add_action('wp_ajax_ban-admin', 'preview_banned_message');
function preview_banned_message() {
$banned_stats = get_option('banned_stats');
$banned_message = stripslashes(get_option('banned_message'));
$banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
$banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
$banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][ban_get_ip()]), $banned_message);
$banned_message = str_replace("%USER_IP%", ban_get_ip(), $banned_message);
$banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(ban_get_ip()), $banned_message);
$banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
echo $banned_message;
exit();
}
### Function: Print Out Banned Message
function print_banned_message() {
$banned_ip = ban_get_ip();
$banned_stats = get_option( 'banned_stats' );
if ( isset( $banned_stats['count'] ) ) {
$banned_stats['count'] += 1;
} else {
$banned_stats['count'] = 1;
}
if ( isset( $banned_stats['users'][$banned_ip] ) ) {
$banned_stats['users'][$banned_ip] += 1;
} else {
$banned_stats['users'][$banned_ip] = 1;
}
update_option( 'banned_stats', $banned_stats );
$banned_message = str_replace(
array(
'%SITE_NAME%',
'%SITE_URL%',
'%USER_ATTEMPTS_COUNT%',
'%USER_IP%',
'%USER_HOSTNAME%',
'%TOTAL_ATTEMPTS_COUNT%'
),
array(
get_option( 'blogname' ),
get_option( 'siteurl' ),
number_format_i18n( $banned_stats['users'][$banned_ip] ),
$banned_ip,
@gethostbyaddr( $banned_ip ),
number_format_i18n( $banned_stats['count'] )
),
stripslashes( get_option( 'banned_message' ) )
);
echo '<!DOCTYPE html>' . "\n";
echo $banned_message;
exit();
}
### Function: Process Banning
function process_ban($banarray, $against) {
if(!empty($banarray) && !empty($against)) {
foreach($banarray as $cban) {
if(preg_match_wildcard($cban, $against)) {
print_banned_message();
}
}
}
return;
}
### Function: Process Banned IP Range
function process_ban_ip_range($banned_ips_range) {
if(!empty($banned_ips_range)) {
foreach($banned_ips_range as $banned_ip_range) {
$range = explode('-', $banned_ip_range);
$range_start = trim($range[0]);
$range_end = trim($range[1]);
if(check_ip_within_range(ban_get_ip(), $range_start, $range_end)) {
print_banned_message();
break;
}
}
}
}
### Function: Banned
add_action( 'init', 'banned' );
function banned() {
$ip = ban_get_ip();
if ( $ip === 'unknown' ) {
return;
}
$banned_ips = get_option( 'banned_ips' );
if ( is_array( $banned_ips ) )
$banned_ips = array_filter( $banned_ips );
$banned_ips_range = get_option( 'banned_ips_range' );
if ( is_array( $banned_ips_range ) )
$banned_ips_range = array_filter( $banned_ips_range );
$banned_hosts = get_option( 'banned_hosts' );
if ( is_array( $banned_hosts ) )
$banned_hosts = array_filter( $banned_hosts );
$banned_referers = get_option( 'banned_referers' );
if ( is_array( $banned_referers ) )
$banned_referers = array_filter( $banned_referers );
$banned_user_agents = get_option( 'banned_user_agents' );
if ( is_array( $banned_user_agents ) )
$banned_user_agents = array_filter( $banned_user_agents );
$banned_exclude_ips = get_option('banned_exclude_ips');
if ( is_array( $banned_exclude_ips ) )
$banned_exclude_ips = array_filter( $banned_exclude_ips );
$is_excluded = false;
if ( ! empty( $banned_exclude_ips ) ) {
foreach( $banned_exclude_ips as $banned_exclude_ip ) {
if ( $ip === $banned_exclude_ip ) {
$is_excluded = true;
break;
}
}
}
if ( ! $is_excluded ) {
if( ! empty( $banned_ips ) ) {
process_ban( $banned_ips, $ip );
}
if ( ! empty( $banned_ips_range ) ) {
process_ban_ip_range( $banned_ips_range );
}
if ( ! empty( $banned_hosts ) ) {
process_ban( $banned_hosts, @gethostbyaddr( $ip ) );
}
if ( ! empty( $banned_referers ) && ! empty( $_SERVER['HTTP_REFERER'] ) ) {
process_ban( $banned_referers, $_SERVER['HTTP_REFERER'] );
}
if ( ! empty( $banned_user_agents ) && ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
process_ban( $banned_user_agents, $_SERVER['HTTP_USER_AGENT'] );
}
}
}
### Function: Check Whether Or Not The IP Address Belongs To Admin
function is_admin_ip($check) {
return preg_match_wildcard($check, ban_get_ip());
}
### Function: Check Whether IP Within A Given IP Range
function check_ip_within_range($ip, $range_start, $range_end) {
$range_start = ip2long($range_start);
$range_end = ip2long($range_end);
$ip = ip2long($ip);
if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
return true;
}
return false;
}
### Function: Check Whether Or Not The Hostname Belongs To Admin
function is_admin_hostname($check) {
return preg_match_wildcard($check, @gethostbyaddr(ban_get_ip()));
}
### Function: Check Whether Or Not The Referer Belongs To This Site
function is_admin_referer($check) {
$url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
foreach($url_patterns as $url) {
if(preg_match_wildcard($check, $url)) {
return true;
}
}
return false;
}
### Function: Check Whether Or Not The User Agent Is Used by Admin
function is_admin_user_agent($check) {
return preg_match_wildcard($check, $_SERVER['HTTP_USER_AGENT']);
}
### Function: Wildcard Check
function preg_match_wildcard($regex, $subject) {
$regex = preg_quote($regex, '#');
$regex = str_replace('\*', '.*', $regex);
if(preg_match("#^$regex$#", $subject)) {
return true;
} else {
return false;
}
}
### Function: Activate Plugin
register_activation_hook( __FILE__, 'ban_activation' );
function ban_activation( $network_wide )
{
if ( is_multisite() && $network_wide )
{
$ms_sites = wp_get_sites();
if( 0 < sizeof( $ms_sites ) )
{
foreach ( $ms_sites as $ms_site )
{
switch_to_blog( $ms_site['blog_id'] );
ban_activate();
}
}
restore_current_blog();
}
else
{
ban_activate();
}
}
function ban_activate() {
add_option('banned_ips', array());
add_option('banned_hosts',array());
add_option('banned_stats', array('users' => array(), 'count' => 0));
add_option('banned_message', '<html>'."\n".
'<head>'."\n".
'<meta charset="utf-8">'."\n".
'<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
'</head>'."\n".
'<body>'."\n".
'<div id="wp-ban-container">'."\n".
'<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
'</div>'."\n".
'</body>'."\n".
'</html>', 'Banned Message');
// Database Upgrade For WP-Ban 1.11
add_option('banned_referers', array());
add_option('banned_exclude_ips', array());
add_option('banned_ips_range', array());
// Database Upgrade For WP-Ban 1.30
add_option('banned_user_agents', array());
// Database Upgrade For WP-Ban 1.64
add_option( 'banned_options', array( 'reverse_proxy' => 0 ) );
}