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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# A helper script to go through the latest comments added to a bugzilla
# to see if any of them link to external sites. If the reviewer deems them
# spammy, the script will tag them as such.
#
# Caution: work in progress
#
__author__ = 'Konstantin Ryabitsev <konstantin@linuxfoundation.org>'
import sys
import requests
import argparse
import logging
import re
import shelve
import datetime
import notify2
import time
from urllib.parse import urlparse
from configparser import ConfigParser
logger = logging.getLogger('default')
APIKEY = None
BZURL = None
REQSESSION = None
CACHEDATA = None
def notify_desktop(message):
notify2.init('bugjunker')
n = notify2.Notification('bugjunker', message)
n.set_timeout(notify2.EXPIRES_NEVER)
n.show()
def get_session():
global REQSESSION
if REQSESSION is None:
REQSESSION = requests.session()
REQSESSION.headers.update({'User-Agent': 'bugjunker'})
return REQSESSION
def ban_hammer(spammers):
params = {}
for spammer in set(spammers):
path = 'user/{spammer}'.format(spammer=spammer)
logger.info('Banning %s', spammer)
payload = {
'email_enabled': False,
'login_denied_text': 'Spammer',
}
bz_put(path, params, payload)
def tag_hammer(spamcids, spamtag):
params = {}
for cid in set(spamcids):
logger.info('Tagging comment %s', cid)
path = 'bug/comment/{cid}/tags'.format(cid=cid)
payload = {
'comment_id': cid,
'add': [spamtag],
}
bz_put(path, params, payload)
def bug_hammer(spambugs, args):
params = {}
for bugid in set(spambugs):
logger.info('Junking bug %s', bugid)
path = 'bug/{bugid}'.format(bugid=bugid)
payload = {
'groups': {'add': [args.group]},
'status': args.status,
'resolution': args.resolution,
}
bz_put(path, params, payload)
def bz_get(path, params):
url = '{BZURL}/rest/{path}'.format(BZURL=BZURL, path=path)
params['api_key'] = APIKEY
ses = get_session()
res = ses.get(url, params=params)
return res.json()
def bz_put(path, params, payload):
url = '{BZURL}/rest/{path}'.format(BZURL=BZURL, path=path)
params['api_key'] = APIKEY
ses = get_session()
res = ses.put(url, params=params, json=payload)
return res.json()
def load_cache(cachefile):
global CACHEDATA
if CACHEDATA is not None:
return CACHEDATA
# noinspection PyBroadException
try:
with shelve.open(cachefile, 'r') as wc:
logger.info('Loading cache from %s', cachefile)
CACHEDATA = dict(wc)
except:
CACHEDATA = {
'seencids': list(),
'seenaids': list(),
'okdomains': list(),
'okfolks': list(),
}
pass
if 'lastrun' in CACHEDATA:
lastrun = CACHEDATA['lastrun']
else:
lastrun = '24h'
return lastrun, CACHEDATA
def save_cache(cachefile, cachedata):
with shelve.open(cachefile, 'c') as wc:
for key, val in cachedata.items():
wc[key] = val
wc.sync()
def check_bad_urls(urls, okdomains):
for url in urls:
try:
up = urlparse(url)
except ValueError:
return url, None
isok = False
for okd in okdomains:
if okd == up.netloc:
isok = True
break
if not isok:
return url, up.netloc
return None, None
def is_junk_attachment(attid):
attid = str(attid)
logger.info(' checking attachment %s', attid)
path = 'bug/attachment/{attid}'.format(attid=attid)
attinfo = bz_get(path, {})
if attid not in attinfo['attachments']:
return False
attdata = attinfo['attachments'][attid]
if attdata['is_patch'] or attdata['content_type'] in ('text/plain',):
return False
if attdata['content_type'] == 'text/html':
# Almost certainly junk
logger.info(' junking attachment %s', attid)
payload = {
'content_type': 'text/plain',
'filename': 'caution.txt',
'is_private': True,
}
bz_put(path, {}, payload)
return True
return False
def process_bugs(cmdargs, cachefile, c, bugs, spamtag):
spammers = list()
spamcids = list()
spambugs = list()
for bug in bugs:
logger.info('Analyzing [%s]: %s', bug['id'], bug['summary'])
params = {}
bugid = bug['id']
path = 'bug/{bugid}/comment'.format(bugid=bugid)
comments = bz_get(path, params)
for f1, f2 in comments['bugs'].items():
c_count = -1
for comment in f2['comments']:
c_count += 1
cid = comment['id']
if cid in c['seencids']:
# already seen, skip
continue
c['seencids'].append(cid)
creator = comment['creator']
if creator in c['okfolks']:
# Known good person
continue
tags = comment['tags']
if spamtag in tags:
# already marked as spammy
continue
if creator in spammers:
# Made by a known spammer, banit
spamcids.append(cid)
logger.info(' auto-tagging comment by %s: %s', creator, cid)
continue
if cmdargs.checkatt:
attid = comment['attachment_id']
if attid is not None and attid not in c['seenaids']:
c['seenaids'].append(attid)
if is_junk_attachment(attid):
logger.info(' check if spammer: %s', creator)
# Look for remote URLs in the comment
if bug['url'].find('http') > -1 or comment['text'].find('http') > -1:
urls = re.findall(r'(https?://\S+)', comment['text'])
if len(bug['url']):
urls.append(bug['url'])
badurl, baddomain = check_bad_urls(urls, c['okdomains'])
if badurl is not None:
if cmdargs.noninteractive:
notify_desktop('Spam in %s: %s' % (bug['id'], baddomain))
when = datetime.datetime.strptime(bug['last_change_time'], '%Y-%m-%dT%H:%M:%SZ')
c['seencids'].remove(cid)
c['lastrun'] = when.strftime('%Y-%m-%d %H:%M:%S')
c['needsinput'] = True
return spammers, spamcids, spambugs
logger.info(' ---')
logger.info(' suspish URL: %s', badurl)
logger.info(' checkit out: %s/show_bug.cgi?id=%s#c%s',
BZURL, bugid, c_count)
baw = input(' (b)an, (a)llow, (w)hitelist %s: ' % baddomain)
if baw == 'a':
c['okfolks'].append(creator)
save_cache(cachefile, c)
continue
if baw == 'w':
logger.info(' whitelisted %s', baddomain)
c['okdomains'].append(baddomain)
c['okfolks'].append(creator)
save_cache(cachefile, c)
continue
logger.info(' spamcid: %s', cid)
spamcids.append(cid)
# If it's a comment #0, then the whole bug needs junking
if c_count == 0:
spambugs.append(bug['id'])
if creator not in spammers:
logger.info(' spammer: %s', creator)
spammers.append(creator)
return spammers, spamcids, spambugs
def main(args):
global BZURL
global APIKEY
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
if args.quiet:
ch.setLevel(logging.CRITICAL)
elif args.debug:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
logger.addHandler(ch)
logger.info('Loading configuration file %s', args.config)
config = ConfigParser()
config.read(args.config)
BZURL = config.get('main', 'url')
APIKEY = config.get('main', 'apikey')
spamtag = config.get('main', 'spamtag')
if config.get('main', 'logfile'):
ch = logging.FileHandler(config.get('main', 'logfile'))
fmt = '[%(process)d] %(asctime)s - %(message)s'
ch.setFormatter(logging.Formatter(fmt))
ch.setLevel(logging.INFO)
logger.addHandler(ch)
cachefile = config.get('main', 'cache')
lastrun, c = load_cache(cachefile)
if args.lookback is not None:
lastrun = args.lookback
while True:
if args.noninteractive and 'needsinput' in c and c['needsinput']:
logger.info('Need to run interactively to make some decisions')
sys.exit(0)
params = {
'chfieldfrom': lastrun,
'include_fields': 'id,summary,last_change_time,url',
}
logger.info('Querying %s for changes since %s', BZURL, lastrun)
unow = datetime.datetime.utcnow()
json = bz_get('bug', params)
c['lastrun'] = unow.strftime('%Y-%m-%d %H:%M:%S')
c['needsinput'] = False
if len(json['bugs']):
spammers, spamcids, spambugs = process_bugs(args, cachefile, c, json['bugs'], spamtag)
if len(spammers) or len(spamcids) or len(spambugs):
ban_hammer(spammers)
tag_hammer(spamcids, spamtag)
bug_hammer(spambugs, args)
else:
logger.info('No new spam found')
else:
logger.info('No changes since %s', lastrun)
save_cache(cachefile, c)
if not args.sleep:
sys.exit(0)
logger.info('Sleeping %d seconds', args.sleep)
time.sleep(args.sleep)
def cmd():
description = 'Junk spammy bugzilla comments and ban their authors'
parser = argparse.ArgumentParser(description=description, prog='bz-comment-junker.py')
parser.add_argument('-c', '--config', required=True,
help='Configuration file')
parser.add_argument('-q', '--quiet', action='store_true', default=False,
help='Output only errors')
parser.add_argument('-d', '--debug', action='store_true', default=False,
help='Add debugging info')
parser.add_argument('-l', '--lookback', default=None,
help='How far back to look (default: since last run, or 24h if no cached data)')
parser.add_argument('-n', '--noninteractive', action='store_true', default=False,
help='Run non-interactively and send an alert when potential spam is found')
parser.add_argument('-a', '--check-attachments', action='store_true', dest='checkatt', default=False,
help='Check attachments for junkiness')
parser.add_argument('--sleep', type=int, default=0,
help='After the run, sleep N seconds and then run again')
parser.add_argument('--status', default='RESOLVED',
help='Status value for junked bugs')
parser.add_argument('--resolution', default='INVALID',
help='Resolution value for junked bugs')
parser.add_argument('--product', default='Other',
help='Product value for junked bugs')
parser.add_argument('--component', default='Spam',
help='Component value for junked bugs')
parser.add_argument('--group', default='Junk',
help='Private group name for junked bugs')
args = parser.parse_args()
main(args)
if __name__ == '__main__':
cmd()
|