-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress_spam_cleanup.pl
271 lines (239 loc) · 6.61 KB
/
wordpress_spam_cleanup.pl
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
#!/usr/bin/perl
# The MIT License (MIT)
# Copyright (c) 2013 Matt Okeson-Harlow
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# wordpress comment spam cleanup
use strict;
use warnings;
use English qw( -no_match_vars );
use Carp;
use DBI;
use Getopt::Long;
use Term::ReadKey;
# database settings
# these can be set to your defaults to avoid having to enter them as cli
# options.
my $database = q{};
my $username = q{};
my $password = q{};
my $prefix = q{wp_};
my $ip_limit = 3; # how many times does an IP have to be listed to get banned
# flags
my $debug;
my $ip_report; # print ip counts
my $word_report; # print word counts
my $tables; # print ip tables block
my $delete; # do delete
my $verbose;
my $output = q{-};
my $output_fh;
my %ip_list;
my %word_list;
my @delete_id;
# process command line options
my $options_ok = GetOptions(
'database=s' => \$database,
'username=s' => \$username,
'password=s' => \$password,
'prefix=s' => \$prefix,
'output=s' => \$output,
'limit=i' => \$ip_limit,
'ip' => \$ip_report,
'tables' => \$tables,
'delete' => \$delete,
'word' => \$word_report,
'verbose' => \$verbose,
);
croak q{Username required.} if $username eq q{};
croak q{Database required.} if $database eq q{};
# setup our output filehandle
if ( $output eq q{-} ) {
$output_fh = \*STDOUT;
}
else {
open $output_fh, '>', $output or croak qq{ERROR: $OS_ERROR};
}
# if a password is not configured in the script and it is not specified on the
# cli, prompt for one.
if ( $password eq q{} ) {
print {$output_fh} qq{Enter password for '$username' on '$database': };
ReadMode 'noecho';
$password = ReadLine 0;
chomp $password;
ReadMode 'normal';
}
print {$output_fh} qq{\nWorking...\n};
# setup the database DSN for later
my $dsn = qq{DBI:mysql:database=$database};
my $comment_table = $prefix . q{comments};
# words that will be checked against comment_author, comment_author_url,
# comment_content
# in vim, select the list, then :!sort
my @banned_list = (
'acyclovir',
'adderall',
'adipex',
'allegra',
'ambien',
'amoxicillin',
'ativan',
'carisoprodol',
'casino',
'celebrex',
'cialis',
'claritin',
'clomid',
'codiene',
'codeine',
'coumadin',
'crestor',
'diflucan',
'effexor',
'ephedra',
'ephedrine',
'erectile',
'femdom',
'fioricet',
'flexeril',
'flonase',
'forex',
'fuck',
'hoodia',
'hydrocodone',
'incest',
'ionamin',
'lamisil',
'lasix',
'lexapro',
'levitra',
'lipitor',
'lortab',
'mevacor',
'milf',
'morphine',
'nexium',
'nude',
'oxycodone',
'oxycontin',
'parishilton',
'paxil',
'percocet',
'plavix',
'porn',
'prevacid',
'prilosec',
'propecia',
'prozac',
'ringtone',
'ring tone',
'rolex',
'soma',
'tagamet',
'tamoxifen',
'tenuate',
'tramadol',
'tramal',
'ultram',
'valium',
'valtrex',
'viagra',
'vicodin',
'vioxx',
'wellbutrin',
'xanax',
'xxx',
'zithromax',
'zoloft',
'zyban',
'zyrtec',
);
# regexp matches, use this for website url's mostly
my @banned_regexp = (
'[.]cn/',
'quizilla[.]com',
);
# setup the database connection
my $dbh = DBI->connect($dsn,$username,$password)
or croak qq{ERROR: $DBI::errstr};
# process the banned list
# get the comment ID and IP address
for my $word (@banned_list) {
my $sql = qq{
select comment_ID,comment_author_IP from $comment_table
where
comment_author_url like '%$word%'
or comment_content like '%$word%'
or comment_author like '%$word%'};
my $sth = $dbh->prepare($sql) or croak qq{ERROR: $DBI::errstr};
$sth->execute or croak qq{ERROR: $DBI::errstr};
while ( my ($id,$ip) = $sth->fetchrow_array ) {
$ip_list{$ip}++;
push @delete_id, $id;
}
$word_list{$word} = $DBI::rows;
$sth->finish;
}
# process the regexp list
for my $regexp (@banned_regexp) {
my $sql = qq{
select comment_ID,comment_author_IP from $comment_table
where
comment_author_url regexp '$regexp'
or comment_content regexp '$regexp'
or comment_author regexp '$regexp'};
my $sth = $dbh->prepare($sql) or croak qq{ERROR: $DBI::errstr};
$sth->execute or croak qq{ERROR: $DBI::errstr};
while ( my ($id,$ip) = $sth->fetchrow_array ) {
$ip_list{$ip}++;
push @delete_id, $id;
}
$word_list{$regexp} = $DBI::rows;
$sth->finish;
}
# reporting
for my $ip ( keys %ip_list) {
if ( $ip_report) {
print {$output_fh} qq{IP: $ip : $ip_list{$ip}\n};
}
if ($tables) {
if ( $ip_list{$ip} ge $ip_limit ) {
print
{$output_fh} qq{iptables -A INPUT -p tcp -m tcp --src $ip --dport 80 -j DROP -m comment --comment 'WordPress $database spam'\n}
or croak qq{ERROR: $OS_ERROR};
}
}
}
if ( $word_report ) {
for my $word ( sort keys %word_list) {
if ( $word_list{$word} ) {
print {$output_fh} qq{WORD : $word : $word_list{$word}\n}
or croak qq{ERROR: $OS_ERROR};
}
}
}
# deleting
if ( $delete ) {
my $sql = qq{delete from $comment_table where comment_id=?};
my $sth = $dbh->prepare($sql) or croak qq{ERROR: $DBI::errstr};
for my $id ( @delete_id ) {
if ( $verbose ) {
print {$output_fh} qq{Deleting $id\n} or croak qq{ERROR :$OS_ERROR};
}
$sth->execute($id) or croak qq{ERROR: $DBI::errstr};
}
$sth->finish or croak qq{ERROR: $DBI::errstr};
}
$dbh->disconnect;