-
Notifications
You must be signed in to change notification settings - Fork 98
/
crawl_data.py
142 lines (111 loc) · 3.65 KB
/
crawl_data.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
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
"""
auth: bigger.wing
version:
function:
usage:
note: 本地下载后的解析和在线解析有一点区别
本地下载后的解析,源文件中多出了tbody标签
在线解析的,源文件中无tbody标签
"""
import json
import requests
import pymysql
from nsfocus_auth import ns_authentication
requests.packages.urllib3.disable_warnings()
from multiprocessing.dummy import Pool as ThreadPool
from bs4 import BeautifulSoup as bsp
from module_crawl_to_db import crawl_to_db
from module_get_config import get_config
# 扫描器登录认证
config = get_config()
scanner = config['scanner']['host']
authentication = ns_authentication(scanner)
def get_vul_data(id):
sessionid = authentication['sessionid']
csrftoken = authentication['csrftoken']
left_menustatue_NSFOCUSRSAS = '3|0|https://' + scanner + '/template/index/'
url = 'https://' + scanner + '/template/show_vul_desc'
params = {'id': id}
headers = {
'Cookie': 'sessionid=' + sessionid + '; csrftoken=' + csrftoken + '; left_menustatue_NSFOCUSRSAS=' + left_menustatue_NSFOCUSRSAS
}
res = requests.get(url=url, params=params, headers=headers, verify=False)
if res.text:
result = {
'id': id,
'content': res.text,
'vul_url': res.url,
'status_code': 1,
'msg': 'Found'
}
else:
result = {
'id': id,
'content': '',
'vul_url': res.url,
'status_code': 0,
'msg': 'NOT Found'
}
return result
def get_vul_detail(id):
vul_detail = {'id': id}
data = get_vul_data(id)
if data['status_code'] == 1:
content = data['content']
try:
res = bsp(content, 'html.parser') # html.parser参数用于去除warning
# 获取漏洞风险等级
img = res.table.td.img.get('src')
vul_risk_icon = list(img.split('/'))[-1]
if 'vuln_high.gif' == vul_risk_icon:
vul_risk = 'high'
elif 'vuln_middle.gif' in vul_risk_icon:
vul_risk = 'middle'
elif 'vuln_low.gif' in vul_risk_icon:
vul_risk = 'low'
else:
pass
vul_detail.update({'风险等级': vul_risk})
# tr下面的内容以k:v的形式输出
vul_desc = res.table.find_all('tr')
for item in vul_desc:
name = item.th.get_text().strip()
value = item.td.get_text().strip()
result = {name: value}
vul_detail.update(result)
# print(json.dumps(vul_detail, indent=4, ensure_ascii=False))
print(vul_detail)
crawl_to_db(vul_detail)
except:
pass
def get_new_list():
hostname = config['database']['host']
username = config['database']['username']
password = config['database']['password']
conn = pymysql.connect(
host=hostname,
user=username,
password=password,
db='gaea',
charset='utf8mb4'
)
cursor = conn.cursor()
sql = "select vul_id from vul_detail"
cursor.execute(sql)
result = cursor.fetchall()
all_list = list(range(1000, 120000))
for x in result:
all_list.remove(int(x[0]))
return all_list
if __name__ == '__main__':
# 获取扫描线程。因扫描器本身性能问题,线程数不能设置过大,否则会导致扫描器卡死
# 建议在空闲的时候爬
thread = get_config()['scanner']['thread']
# 获取需要增量更新的id
# ids = list(range(100001, 100005))
ids = get_new_list()
# 多线程爬取
pool = ThreadPool(thread)
pool.map(get_vul_detail, ids)