-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploit_scan.py
More file actions
636 lines (564 loc) · 24.1 KB
/
Copy pathexploit_scan.py
File metadata and controls
636 lines (564 loc) · 24.1 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Sedate
import re, os, sys, time, shutil, MySQLdb
import urllib, urllib2, smtplib, zipfile, threading
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from email.utils import formataddr
reload(sys)
sys.setdefaultencoding('utf-8')
# 数据库操作
class db_util(object):
def __init__(self):
self.host = '127.0.0.1'
self.username = 'root'
self.password = 'toor'
self.database = 'test'
self.db = None
self.cursor = None
self.results = None
# 连接数据库
def db_conn(self):
try:
# 连接数据库
self.db = MySQLdb.connect(self.host, self.username, self.password, self.database, charset='utf8')
# 获取游标
self.cursor = self.db.cursor()
print 'connect success...\n'
except Exception as e:
print 'database connect error...\n'
print e
# 关闭连接
def db_close(self):
self.cursor.close()
self.db.close()
# 查询
def select(self, sql):
self.db_conn()
try:
# 执行sql
self.cursor.execute(sql)
# 查询
self.results = self.cursor.fetchall()
except Exception as e:
print 'select error\n'
print e
finally:
self.db_close()
# 更改
def update(self, sql):
self.db_conn()
flag = -1
try:
# 执行sql
flag = self.cursor.execute(sql)
# 提交
self.db.commit()
except Exception as e:
# 回滚
self.db.rollback()
print 'update error\n'
print e
finally:
self.db_close()
return flag
# 爬取
class spider(db_util):
def __init__(self):
db_util.__init__(self)
self.ltime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'}
# 判断是否是新数据 存已爬到url
self.url_flag_php = []
self.url_flag_apache = []
self.url_flag_cnvd = []
self.url_flag_centos = []
self.ids = None
#
# php
#
# 获取漏洞url
def get_info_php(self):
flag = 0
threads = []
try:
start_url = 'https://bugs.php.net/search.php?limit=30&order_by=id&direction=DESC&cmd=display&status=Open&bug_type=All'
req = urllib2.Request(start_url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
temp = re.findall('Assigned</a></th>(.*?)<!-- BEGIN PREV/NEXT -->', resp, re.S | re.M)
if not len(temp):
return
temp2 = re.findall('<tr (.*?)</tr>', temp[0], re.S | re.M)
for t2 in temp2:
# 是否是当天漏洞
if self.ltime in t2:
flag = 1
url = re.findall('<a href="(.*?)">', t2, re.S | re.M)
# 是否在已爬取链接中
if url[0] in self.url_flag_php:
print '>>> php:no new bug\n'
return
# 添加到已爬取链接
self.url_flag_php.append(url[0])
td = threading.Thread(target=self.show_info_php, args=('https://bugs.php.net/%s' % url[0],))
threads.append(td)
else:
if flag != 0:
break
print '>>> php:bug null\n'
break
except Exception as e:
print e
pass
for td in threads:
try:
td.start()
except Exception as e:
print e
print '>>> threading error...\n'
pass
for td in threads:
td.join()
# 获取漏洞详情
def show_info_php(self, url):
try:
req = urllib2.Request(url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
for result in self.results:
# 用户是否使用php环境
if 'php' in result[3]:
# 是否存在目录 不存在则创建
if not os.path.exists('bugs/%s' % result[1]):
os.makedirs('bugs/%s' % result[1])
title = re.findall('<title>(.*?)</title>', resp, re.S | re.M)
if not len(title):
return
level = re.findall('<input type="radio" id="score-(.*?)" name="score" value="0" checked="checked">',
resp, re.S | re.M)
lv = ''
if level:
t = int(level[0][-1])
if t < 6 and t > 3:
lv = '高'
if t == 3:
lv = '中'
if t < 3 and t > 0:
lv = '低'
e = 'php'
# 添加到要发送给用户的html内容
self.into_html(result, url, title, e, lv)
# 将漏洞详情写入html文件
# 子文件夹名以客户名称命名 后面可直接判断文件所属客户
text = format_name(title[0])
f = open('bugs/%s/%s.html' % (result[1], text), 'w') # 标题存在非法文件名 替换掉
f.write(resp)
f.close()
except Exception as e:
print e
pass
#
# apache
#
# 获取漏洞url
def get_info_apache(self):
flag = 0
threads = []
try:
start_url = 'http://www.cvedetails.com/vulnerability-list/'
req = urllib2.Request(start_url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
temp = re.findall('<tr class="srrowns">(.*?)</tr>', resp, re.S | re.M)
if not len(temp):
return
for t in temp:
if self.ltime in t:
flag = 1
url = re.findall('<a href="(.*?)"', t, re.S | re.M)
if url[0] in self.url_flag_apache:
print '>>> apache:no new bug\n'
return
self.url_flag_apache.append(url[0])
td = threading.Thread(target=self.show_info_apache, args=('http://www.cvedetails.com%s' % url[0],))
threads.append(td)
else:
if flag != 0:
break
print '>>> apache:bug null\n'
break
except Exception as e:
print e
pass
for td in threads:
try:
td.start()
except Exception as e:
print e
print 'threading error...\n'
pass
for td in threads:
td.join()
# 获取漏洞详情
def show_info_apache(self, url):
try:
req = urllib2.Request(url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
for result in self.results:
if 'apache' in result[3]:
if not os.path.exists('bugs/%s' % result[1]):
os.makedirs('bugs/%s' % result[1])
title = re.findall('<title>(.*?)</title>', resp, re.S | re.M)
if not len(title):
return
level = re.findall('<div class="cvssbox"(.*?)</div>',
resp, re.S | re.M)
lv = ''
if level:
t = int(level[0][-3])
if t < 10 and t > 6:
lv = '高'
if t < 7 and t > 3:
lv = '中'
if t < 3 and t >= 0:
lv = '低'
e = 'apache'
# 添加到要发送给用户的html内容
self.into_html(result, url, title, e, lv)
# 正则匹配漏洞详情
temp = re.findall('<td valign="top" id="cvedetails">(.*?)<td valign="top" align="left">', resp,
re.S | re.M)
# 将匹配到的详情写入html
text = format_name(title[0])
f = open('bugs/%s/%s.html' % (result[1], text), 'w') # 标题存在非法文件名 替换掉
f.write(temp[0])
f.close()
except Exception as e:
print e
pass
#
# cnvd
#
# 获取漏洞url
def get_info_cnvd(self):
pages = 0 # 分页爬取 (0第一页 每页递增20)
flag = 0
threads = []
while True:
try:
# cnvd可条件查询 按时间查询
start_url = 'http://www.cnvd.org.cn/flaw/list.htm?field=&startDate=%s&flag=true&order=&number=%s&endDate=%s&max=20&offset=%d' % (
self.ltime, urllib.quote('请输入精确编号'), self.ltime, pages)
req = urllib2.Request(start_url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read().decode('utf-8').encode('gb2312')
temp = re.findall('<tbody>(.*?)</tbody>', resp, re.S | re.M)
if not len(temp):
return
url = re.findall('href="/flaw/show/(.*?)"', temp[0], re.S | re.M)
# 当天是否有漏洞
if url:
for u in url:
flag = 1
# 是否已爬取
if u in self.url_flag_cnvd:
print '>>> cnvd:no new bug\n'
return
# 加入已爬取列表
self.url_flag_cnvd.append(u)
# 创建线程
td = threading.Thread(target=self.show_info_cnvd,
args=('http://www.cnvd.org.cn/flaw/show/%s' % u,))
threads.append(td)
else:
if flag != 0:
return
print '>>> cnvd:bug null\n'
return
except Exception as e:
print e
pass
for td in threads:
try:
td.start()
except Exception as e:
print e
print '>>> threading error...\n'
pass
for td in threads:
td.join()
# 清空线程列表
threads = []
# 递增20 爬取下一页
pages += 20
# 获取漏洞详情
def show_info_cnvd(self, url):
try:
req = urllib2.Request(url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read() # .decode('utf-8').encode('GBK')
temp = re.findall('<div class="tableDiv">(.*?)</div>', resp, re.S | re.M)
title = re.findall('<h1 >(.*?)</h1>', resp, re.S | re.M)
# program = re.findall('<td class="alignRight">漏洞解决方案</td>\r\n\t\t\t\t\t\t\t\t\t\t<td>(.*?)</td>',resp,re.S|re.M)
if not len(temp) or not len(title):
return
for result in self.results:
environment = result[3].split(',') # 获取用户环境
for e in environment:
# 用户所用环境是否出现在爬取到的漏洞中
if e in temp[0]:
if not os.path.exists('bugs/%s' % result[1]):
os.makedirs('bugs/%s' % result[1])
level = re.findall('<td class="denle">(.*?)\(', resp, re.S | re.M)
level = level[0].replace('\t', '')
level = level.replace('\r', '')
level = level.replace('\n', '')
lv = ''
if level:
lv = level.split('>')
self.into_html(result, url, title, e, lv[-1])
'''
if os.path.exists('bugs/%s.txt' % result[1]):
txt = open('bugs/%s.txt' % result[1], 'a+')
else:
txt = open('bugs/%s.txt' % result[1], 'a+')
txt.write(
'<style>tr{text-align:center}td{border:1px black solid}</style><table style="border:1px black solid;border-collapse: collapse;"><tr><td>序号</td><td>漏洞描述</td><td>爆出时间</td><td>所属组件</td><td>危害等级</td><td>解决建议</td></tr>')
self.ids = locals()
self.ids['_%s_count' % result[1]] = 1
txt.write(
'<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (
self.ids['_%s_count' % result[1]], url, title[0], self.ltime, e, lv[-1], program[0]))
self.ids['_%s_count' % result[1]] += 1
txt.close()
'''
text = format_name(title[0])
f = open('bugs/%s/%s.html' % (result[1], text), 'w') # 标题存在非法文件名 替换掉
f.write(temp[0].decode('utf-8').encode('gb2312')) # 解码后编码写入html
f.close()
except Exception as e:
print e
pass
#
# centos
#
# 获取漏洞url
def get_info_centos(self):
flag = 0
threads = []
try:
start_url = 'https://bugs.centos.org/view_all_bug_page.php'
req = urllib2.Request(start_url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
temp = re.findall('<tr class="status-(.*?)</tr>', resp, re.S | re.M)
if not len(temp):
return
for t in temp:
if self.ltime in t:
flag = 1
url = re.findall('<a href="(.*?)">', t, re.S | re.M)
if url[0] in self.url_flag_centos:
print '>>> centos:no new bug\n'
return
self.url_flag_centos.append(url[0])
td = threading.Thread(target=self.show_info_centos,
args=('https://bugs.centos.org%s' % url[0], t,))
threads.append(td)
else:
if flag != 0:
break
print '>>> centos:bug null\n'
break
except Exception as e:
print e
pass
for td in threads:
try:
td.start()
except Exception as e:
print e
print 'threading error...\n'
pass
for td in threads:
td.join()
# 获取漏洞详情
def show_info_centos(self, url, t):
try:
req = urllib2.Request(url, headers=self.headers)
resp = urllib2.urlopen(req, timeout=60).read()
for result in self.results:
environment = result[3].split(',') # 获取用户环境
for e in environment:
if e in t:
title = re.findall('<td class="column-summary">(.*?)</td>', t, re.S | re.M)
if not len(title):
return
if not os.path.exists('bugs/%s' % result[1]):
os.makedirs('bugs/%s' % result[1])
level = re.findall('<td class="bug-severity">(.*?)</td>',
resp, re.S | re.M)
if level[0] == 'major':
lv = '高'
elif level[0] == 'minor':
lv = '低'
else:
lv = level[0]
# 添加到要发送给用户的html内容
self.into_html(result, url, title, e, lv)
'''
if os.path.exists('bugs/%s.txt' % result[1]):
txt = open('bugs/%s.txt' % result[1], 'a+')
else:
txt = open('bugs/%s.txt' % result[1], 'a+')
txt.write('<style>tr{text-align:center}td{border:1px black solid}</style><table style="border:1px black solid;border-collapse: collapse;"><tr><td>序号</td><td>漏洞描述</td><td>爆出时间</td><td>所属组件</td><td>危害等级</td><td>解决建议</td></tr>')
self.ids = locals()
self.ids['_%s_count' % result[1]] = 1
txt.write(
'<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>待沟通</td></tr>' % (
self.ids['_%s_count' % result[1]],url,title[0],self.ltime,e,level[0]))
self.ids['_%s_count' % result[1]] += 1
txt.close()
'''
# 将匹配到的详情写入html
text = format_name(title[0])
f = open('bugs/%s/%s.html' % (result[1], text), 'w') # 标题存在非法文件名 替换掉
f.write(resp)
f.close()
except Exception as e:
print e
pass
# 添加到要发送给用户的html内容
def into_html(self, result, url, title, e, level):
if os.path.exists('bugs/%s.txt' % result[1]):
txt = open('bugs/%s.txt' % result[1], 'a+')
else:
txt = open('bugs/%s.txt' % result[1], 'a+')
txt.write(
'<style>tr{text-align:center}td{border:1px black solid}</style><table style="border:1px black solid;border-collapse: collapse;"><tr><td>序号</td><td>漏洞描述</td><td>曝出时间</td><td>所属组件</td><td>危害等级</td></tr>')
self.ids = locals()
self.ids['_%s_count' % result[1]] = 1
txt.write(
'<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td></tr>' % (
self.ids['_%s_count' % result[1]], url, title[0], self.ltime, e, level))
self.ids['_%s_count' % result[1]] += 1
txt.close()
# 获取用户详情
def get_userinfo(self):
# 查询用户信息
sql = 'select * from user_info'
self.select(sql)
# 清理集合
def clear_data(self):
# 获取当前时间
nowtime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
# 是否是新的一天
if self.ltime != nowtime:
# 清空变量
self.url_flag_php = []
self.url_flag_apache = []
self.url_flag_cnvd = []
self.url_flag_centos = []
# 更改当天日期
self.ltime = nowtime
# 入口
def main(self):
self.clear_data()
self.get_userinfo()
self.get_info_php()
self.get_info_apache()
self.get_info_cnvd()
self.get_info_centos()
# 目录不为空
if os.listdir('bugs'):
try:
# 遍历目录
for dirpath, dirnames, filenames in os.walk('bugs'):
for d_name in dirnames:
# 压缩
f = zipfile.ZipFile('bugs/%s.zip' % d_name.decode('GBK'), 'w', zipfile.ZIP_DEFLATED)
for sub_dirpath, sub_dirnames, sub_filenames in os.walk('bugs/%s' % d_name):
for sf_name in sub_filenames:
f.write(os.path.join(sub_dirpath, sf_name))
# os.removedirs('bugs/%s/' % d_name.decode('GBK'))
f.close()
# 压缩后删除文件夹及内容
shutil.rmtree('bugs/%s/' % d_name.decode('GBK'))
# 获取用户信息
for user in self.results:
# 用户名与压缩包名是否匹配 匹配则此压缩包属于该用户
if user[1] == d_name.decode('GBK'):
# 获取该压缩包路径
filedir = 'bugs/%s.zip' % d_name.decode('GBK')
txt = open('bugs/%s.txt' % d_name.decode('GBK'), 'r')
emailhtml = '%s<tr><td colspan="5"><br/>针对以上安全风险信息有任何疑问,请随时与我们安全技术工程师联系。<br/>联系人:孙权<br/>手机号:18017682556<br/></td></tr></table>' % txt.read()
txt.close()
# 发送邮箱
datetime = time.strftime('%Y年%m月%d日', time.localtime(time.time()))
send_mail(user[1], user[2], '来自灵码企业安全:安全漏洞威胁预警【%s】' % datetime, emailhtml,
filedir)
# 删除压缩包及邮件文本
os.remove('bugs/%s.zip' % d_name.decode('GBK'))
os.remove('bugs/%s.txt' % d_name.decode('GBK'))
except Exception as e:
print e
pass
# 发送邮件
def send_mail(user, email, title, info, filedir):
# 发送者邮箱
my_sender = 'sedate_hack@163.com'
# 客户邮箱
my_user = email.split(',')
# 发送者名称
from_user = '%s' % Header('灵码企业安全', 'utf-8')
# 用户名称
to_user = '%s' % Header(user, 'utf-8')
try:
# msg = MIMEText(info,'html','utf-8')
msg = MIMEMultipart() # 分多个部分
msg['Accept-Language'] = 'zh-CN'
msg['Accept-Charset'] = 'ISO-8859-1,utf-8'
msg['From'] = formataddr([from_user, my_sender])
msg['To'] = ','.join(my_user)
msg['Subject'] = '%s' % Header(title, 'utf-8') # title # 主题
# html部分
part = MIMEText(info, 'html', 'utf-8')
msg.attach(part)
# 附件部分
part = MIMEApplication(open(filedir, 'rb').read()) # 从传入压缩包路径读取压缩文件
part.add_header('Content-Disposition', 'attachment',
filename='%s' % Header(filedir[filedir.index('/') + 1:], 'utf-8')) # 设置发送给客户文件名
msg.attach(part)
server = smtplib.SMTP('smtp.163.com', 25)
server.login(my_sender, '******') # 登录
server.sendmail(my_sender, my_user, msg.as_string()) # 发送
server.quit()
print '>>> send e-mail success\n'
except Exception as e:
print e
print '>>> send e-mail error\n'
pass
# 格式化文件名
def format_name(text):
text = text.replace('\\', '')
text = text.replace('/', '')
text = text.replace(':', '')
text = text.replace('\*', '')
text = text.replace('?', '')
text = text.replace('"', '')
text = text.replace('<', '')
text = text.replace('>', '')
text = text.replace('|', '')
return text
# 初始化对象
sp = spider()
# 定时任务
def job_start():
start = time.time()
# 开始
sp.main()
timer = threading.Timer(1800, job_start)
timer.start()
end = time.time()
print end - start
if __name__ == '__main__':
job_start()