/
findspam.py
241 lines (235 loc) · 18.5 KB
/
findspam.py
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
# -*- coding: utf-8 -*-
import regex
import phonenumbers
def has_repeated_words(s):
words = regex.split("\\W", s)
words = [w for w in words if w != "" and not w.isdigit()]
curr = 0
prev = ""
for w in words:
if w == prev:
curr += 1
else:
curr = 0
prev = w
if curr >= 5:
return True
return curr >= 5
class FindSpam:
bad_keywords = ["baba ?ji", "fifa.*coins?", "fifabay", "Long Path Tool",
"fifaodell", "brianfo", "tosterone", "bajotz",
"vashi?k[ae]r[ae]n", "kolcak"
"porn", "molvi", "judi bola", "ituBola.com", "lost lover'?s?",
"acai", "skin ?care", "rejuvenated skin",
"LifeForce", "swtor2credits", "me2.do", "black magic",
"bam2u", "Neuro(3X|flexyn|fuse|luma|plex)", "Nutra", "TesteroneXL", "Nitroxin",
"Bowtrol", "Slim ?Genix", "Cleanse EFX",
"Forskolin", "Blackline Elite", "TestCore Pro",
"Xtreme Antler", "Maxx Test 3000", "orvigomax",
"Cheap Wigs?", "jivam", "Brain Power", "Maximum ?Shred",
"aging skin", "acne( prone)? skin",
"skin (serum|eye)", "bagprada", "6611165613", "Apowersoft",
"Service Solahart", "junisse",
"(fake|original|uk|novelty) (passports?|driver'?s? licen[cs]e|ID cards?)",
"(support|service|helpline)( phone)? number|1[ -]?[ -]?[ -]?866[ -]?978[ -]?(6819|6762)",
"(mcafee|hotmail|gmail|outlook|yahoo|lexmark (printer)?) ?(password( recovery)?|tech)? ?((customer|technical) (support|service))? (support|contact|telephone|help(line)?|phone) number",
"kitchen for sale", "pdftoexcelconverter", "keepbrowsersafe", "SpyHunter",
"pcerror-fix", "filerepairtool", "combatpcviruses", "SkinCentric",
"JobsTribune", "join the illuminati", "Brorsoft", "Remo Recover",
"kinnaristeel", "clash of (clan|stone)s? (cheats?|tricks?|gems?)",
r"(?x:B [\s_]* A [\s_]* M \W{0,5} W [\s_]* A [\s_]* R [\s_]* \.? [\s_]* C [\s_]* O [\s_]* M)",
"slumber pm", "1-844-400-7325", "male enhancement", "bestcollegechina",
"bbwdesire", "rsorder", "Shopping ?Cart ?Elite", "Easy ?Data ?Feed",
"breasts? enlargement", "best property management", "examguidez",
"maxman ?power", "niagen"]
bad_keywords_nwb = [u"ಌ", "babyliss", "garcinia", "acai ?berry", # "nwb" == "no word boundary"
"aging ?cream", "b ?a ?m ?((w ?o ?w)|(w ?a ?r))",
"abam26", "watch2live", "cogniq", "eye ?serum", "tophealth"]
blacklisted_websites = ["online ?kelas", "careyourhealths", "wowtoes",
"ipubsoft", "orabank", "powerigfaustralia",
"cfpchampionship2015playofflive",
"maletestosteronebooster",
"healthcaresup", "filerepairforum",
"lxwpro-t", "casque-beatsbydre", "tenderpublish",
"elliskinantiaging", "funmac", "lovebiscuits",
"Eglobalfitness", "musclezx90site", "fifapal",
"hits4slim", "screenshot\\.net", "downloadavideo\\.net",
"strongmenmuscle", "sh\\.st/", "musclehealthfitness",
"svelmeblog", "preply\\.com", "hellofifa",
"fifa15online", "wearepropeople.com", "tagwitty",
"axsoccertours", "ragednablog", "ios8easyjailbreak",
"totalfitnesspoint", "trustessaywriting",
"trustmyessay", "faasoft", "besttvshows", "mytechlabs",
"giikers", "pagetube", "myenv\\.org",
"pelevoniface",
"skinphysiciantips", "fifa2coins", "xtrememusclerecoveryrev",
"diabacordoesitwork", "thehealthyadvise",
"premiumpureforskolinrev", "hyperglycemiaabout", "dietandhealthguide",
"health\\d{3,}", "sourceforge\\.net/projects/freepdftojpgconverter",
"pdftoexel\\.wordpress\\.com", "best7th\\.in",
"recoverytoolbox\\.com", "mkmk9", "malwaretips", "intellipaat\\.com",
"webbuildersguide\\.com", "idealshare.net", "lankabpoacademy\\.com",
"evomailserver\\.com", "gameart\\.net",
"sofotex\\.com",
"mybloggingmoney\\.com", "windows-techsupport\\.com",
"supplementsdeal\\.com", "drivethelife\\.com",
"lafozi\\.com", "open-swiss-bank\\.com",
"healthy-weight-loss-tips\\.com",
"tenorshare\\.com", "advancedpdfconverter\\.com",
"fix-computer\\.net",
"macvideoconverterpro\\.com", "password-master\\.net",
"photorecovery-formac\\.com",
"rarpasswordunlocker\\.net",
"windows7-password-reset\\.net", "windowspasswordcracker\\.com"
"windowspasswordreset\\.net",
"youtubedownloaderconverter\\.net",
"smartpcfixer\\.com", "1fix\\.org",
"drivertuner\\.com", "easyfix\\.org", "errorsfixer\\.org",
"faq800\\.com", "fix1\\.org", "guru4pc\\.net", "howto4pc\\.org",
"official-drivers\\.com", "pceasynow\\.com",
"regeasypro\\.com", "registryware\\.org", "smartfixer\\.net",
"smartfixer\\.org", "wisefixer\\.com", "wisefixer\\.net",
"passwordunlocker\\.com",
"password-unlocker\\.com", "passwordtech\\.com", "goshareware\\.com",
"nemopdf\\.com", "apowersoft\\.com", "downloaddailymotion\\.com",
"free-download-youtube\\.com", "free-music-downloader\\.com",
"video-download-capture\\.com", "videograbber\\.net",
"password-buster\\.com",
"remorecover\\.com", "remosoftware\\.com", "crazybulkreviewsz\\.com",
"\\bpatch\\.com\\b", "ajgilworld\\.com", "santomais", "viilms",
"clashofclansastucegemmes\\.com", "mothersday-2014\\.org",
"bestcelebritiesvideo\\.com", "shopnhlbruins\\.com",
"downloadscanpst\\.com",
"listoffreeware\\.com", "bigasoft\\.com", "opclub07\\.com",
"allavsoft", "vpnranks\\.com",
"musclebuildingproducts\\.info", "magichealthandwellness\\.com",
"vanskeys\\.com", "cheapessaywritingservice", "edbtopsts\\.com",
"texts\\.io", "writage\\.com", "mobitsolutions\\.com",
"askpcexperts\\.com", "anonymousvpnsoftware\\.com",
"ecouponcode\\.com", "wasel\\.com", "i-spire\\.com",
"internetwasel\\.com", "waselpro\\.com", "iwasl\\.com",
"vpnfaqs\\.com", "vpnanswers\\.com", "bestcheapvpnservice\\.com",
"unblockingtwitter\\.com", "openingblockedsite\\.com",
"arabicdownloads\\.com", "arabicsoftdownload\\.com",
"repairtoolbox\\.com", "couchsurfing\\.com",
"gta5codes\\.fr", "musclezx90au\\.com",
"fallclassicrun\\.com", "forgrams\\.com",
"cloudinsights\\.net",
"(premium|priceless)-inkjet\\.com", "antivirus\\.comodo\\.com",
"clusterlinks\\.com", "connectify\\.me", "liftserump\\.com",
"kizi1000\\.in", "weightruinations\\.com",
"\\Bfacts\\.(com|net|org)", "products\\.odosta\\.com",
"rackons\\.com", "imonitorsoft\\.com",
"analec\\.com", "livesportstv\\.us",
"dermaessencecreamblog\\.com", "stadtbett\\.com",
"healthcaresdiscussion\\.com",
"recovery(pro)?\\.(com|net|org)", "password\\.(com|net|org)",
"\\.repair\"", "optimalstackfacts", "x4facts", "endomondo\\.com",
"litindia\\.in", "shoppingcartelite\\.com",
"customizedwallpaper\\.com", "cracksofts\\.com",
"crevalorsite\\.com", "macfixz\\.com", "moviesexplore\\.com",
"iphoneunlocking\\.org", "wrinklerewindblog\\.net",
"thehealthvictory\\.com", "goldenhealthreview\\.com",
"bloggermaking\\.com", "supportphonenumber\\.com",
"healthsupreviews", "slimbodyketone", "prinenidz\\.com",
"maddenmobilehack", "supplements4help", "cheaproduct\\.com",
"cacherealestate\\.com", "Matrixhackka007", "aoatech\\.com",
"pharaohtools"]
rules = [
{'regex': u"(?i)\\b(%s)\\b|%s" % ("|".join(bad_keywords), "|".join(bad_keywords_nwb)), 'all': True,
'sites': [], 'reason': "Bad keyword in {}", 'title': True, 'body': True, 'username': True, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"(?i)\\b(baba|nike)\\b", 'all': True,
'sites': [], 'reason': "Bad keyword in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"(?i)\\bgratis\\b", 'all': True,
'sites': ['softwarerecs.stackexchange.com'], 'reason': "Bad keyword in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"(?i)\\p{Script=Hangul}", 'all': True,
'sites': [], 'reason': "Korean character in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"(?i)(>>>>|===>|==>>>)(?s).*http", 'all': True,
'sites': [], 'reason': "Bad keyword in {}", 'title': True, 'body': True, 'username': True, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"<blockquote>[^\/]*<blockquote>", 'all': True,
'sites': [], 'reason': "Nested quote blocks in {}", 'title': False, 'body': True, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"(?i)\\b(weight (body ?builder|loo?s[es]|reduction)|muscles?|anti aging|loo?s[es] weight|wrinkles?|diet ?plan|supplements?)\\b", 'all': True,
'sites': ["fitness.stackexchange.com", "biology.stackexchange.com", "health.stackexchange.com"], 'reason': "Bad keyword in {}", 'title': True, 'body': False, 'username': True, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"(?i)^(?:(?=.*?\\b(?:online|hd)\\b)(?=.*?(?:free|full|unlimited)).*?movies?\\b|(?=.*?\\b(?:acai|kisn)\\b)(?=.*?care).*products?\\b|(?=.*?packer).*mover)", 'all': True,
'sites': [], 'reason': "Bad keywords in {}", 'title': True, 'body': False, 'username': True, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"\\d(?:_*\\d){9}|\\+?\\d_*\\d[\\s\\-]?(?:_*\\d){8,11}|\\d[ -]?\\d{3}[ -]?\\d{3}[ -]?\\d{4}", 'all': True,
'sites': ["patents.stackexchange.com", "math.stackexchange.com"], 'reason': "Phone number detected in {}", 'validation_method': 'check_phone_numbers', 'title': True, 'body': False, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"(?i)\\b(nigg(a|er)|asshole|fag|fuck(ing?)?|shit|whore|cunt)s?\\b", 'all': True,
'sites': [], 'reason': "Offensive {} detected", 'insensitive':True, 'title': True, 'body': True, 'username': False, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"(?i)\\b(crap)\\b", 'all': True, 'sites': [], 'reason': "Offensive {} detected", 'insensitive': True, 'title': True, 'body': False, 'username': False, 'stripcodeblocks': False, 'body_summary': False},
{'regex': ur"^(?=.*\p{upper})\P{lower}*$", 'all': True, 'sites': [], 'reason': "All-caps title", 'title': True, 'body': False, 'username': False, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"^(?=.*[0-9])[^\\pL]*$", 'all': True, 'sites': [], 'reason': "Numbers-only title", 'title': True, 'body': False, 'username': False, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"https?://(?!(www\\.)?example\\.com)[a-zA-Z0-9_.-]+\\.[a-zA-Z]{2,4}(/[a-zA-Z0-9_/?=.-])?", 'all': True,
'sites': ["stackoverflow.com", "superuser.com", "askubuntu.com"], 'reason': "URL in title", 'title': True, 'body': False, 'username': False, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"(?i)(%s)" % "|".join(blacklisted_websites), 'all': True,
'sites': [], 'reason': "Blacklisted website in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': False, 'body_summary': True},
{'regex': u"([^\\s_.?!=0-9-])\\1{10,}", 'all': True, 'sites': [], 'reason': "Repeating characters in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'method': has_repeated_words, 'all': True, 'sites': [], 'reason': "Repeating words in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"^(.)\\1+$", 'all': True, 'sites': [], 'reason': "{} has only one unique char", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"(?<![=#/])\\b[A-z0-9_.%+-]+@(?!example\\.com)[A-z0-9_.%+-]+\\.[A-z]{2,4}\\b", 'all': True,
'sites': ["stackoverflow.com", "superuser.com", "serverfault.com", "askubuntu.com", "webapps.stackexchange.com", "salesforce.stackexchange.com", "unix.stackexchange.com"], 'reason': "Email in {}", 'title': True, 'body': True, 'username': False, 'stripcodeblocks': True, 'body_summary': False},
{'regex': u"(?i)(tejveer ?iq|ser?vice pemanas?)", 'all': True, 'sites': [], 'reason': "Blacklisted username", 'title': False, 'body': False, 'username': True, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u"(?i)vs", 'all': False, 'sites': ["patents.stackexchange.com"], 'reason': 'Bad keyword in {}', 'title': True, 'body': False, 'username': False, 'stripcodeblocks': False, 'body_summary': False},
{'regex': u".*<pre>.*", 'all': False, 'sites': ["puzzling.stackexchange.com"], 'reason': 'Code block', 'title': False, 'body': True, 'username': False, 'stripcodeblocks': False, 'report_everywhere': False, 'body_summary': False}
]
@staticmethod
def test_post(title, body, user_name, site, is_answer, body_is_summary):
result = []
for rule in FindSpam.rules:
body_to_check = body
if rule['stripcodeblocks']:
body_to_check = regex.sub("<pre>.*?</pre>", "", body, flags=regex.DOTALL)
body_to_check = regex.sub("<code>.*?</code>", "", body_to_check, flags=regex.DOTALL)
if rule['reason'] == 'Phone number detected in {}':
body_to_check = regex.sub("<img[^>]+>", "", body_to_check)
body_to_check = regex.sub("<a[^>]+>", "", body_to_check)
if rule['all'] != (site in rule['sites']):
compiled_regex = None
if 'regex' in rule:
compiled_regex = regex.compile(rule['regex'], regex.UNICODE)
matched_title = compiled_regex.findall(title)
matched_username = compiled_regex.findall(user_name)
matched_body = None
else:
assert 'method' in rule
matched_title = rule['method'](title)
matched_username = rule['method'](user_name)
matched_body = None
if not body_is_summary or rule['body_summary']:
if 'regex' in rule:
matched_body = compiled_regex.findall(body_to_check)
else:
matched_body = rule['method'](body_to_check)
if matched_title and rule['title']:
try:
if getattr(FindSpam, "%s" % rule['validation_method'])(matched_title):
result.append(rule['reason'].replace("{}", "title"))
except KeyError: # There is no special logic for this rule
result.append(rule['reason'].replace("{}", "title"))
if matched_username and rule['username']:
try:
if getattr(FindSpam, "%s" % rule['validation_method'])(matched_username):
result.append(rule['reason'].replace("{}", "username"))
except KeyError: # There is no special logic for this rule
result.append(rule['reason'].replace("{}", "username"))
if matched_body and rule['body']:
type_of_post = "answer" if is_answer else "body"
try:
if getattr(FindSpam, "%s" % rule['validation_method'])(matched_body):
result.append(rule['reason'].replace("{}", type_of_post))
except KeyError: # There is no special logic for this rule
result.append(rule['reason'].replace("{}", type_of_post))
return result
@staticmethod
def check_phone_numbers(matched):
test_formats = ["IN", "US", None]
for phone_number in matched:
for testf in test_formats:
try:
z = phonenumbers.parse(phone_number, testf)
if phonenumbers.is_possible_number(z) and phonenumbers.is_valid_number(z):
print "Possible %s, Valid %s, Explain: %s" % (phonenumbers.is_possible_number(z), phonenumbers.is_valid_number(z), z)
return True
except phonenumbers.phonenumberutil.NumberParseException:
pass
return False