13
13
import datetime
14
14
import os
15
15
import requests
16
- import urllib
17
16
import threading
18
17
import gzip
19
18
import xml .etree .cElementTree as eT
20
19
import multiprocessing
21
20
from .config import project_directory , Config , config_path
22
21
from .log import logger
23
22
from .dependencies import Dependencies
23
+
24
+ try :
25
+ from urllib import urlretrieve # Python2
26
+ except ImportError :
27
+ from urllib .request import urlretrieve # Python3
28
+
24
29
try :
25
30
from configparser import ConfigParser
26
31
except ImportError :
@@ -52,7 +57,7 @@ def cve_parse(self):
52
57
if not isinstance (cve_file , list ):
53
58
tree = self .parse_xml (cve_file )
54
59
root = tree .getroot ()
55
- childs = root .iter ( ' %sentry' % self .NS )
60
+ childs = root .findall ( '.// %sentry' % self .NS )
56
61
for child in childs : # child is entry Element
57
62
cve_id = child .attrib ['id' ]
58
63
cve_info = self .cve_info (child )
@@ -62,7 +67,7 @@ def cve_parse(self):
62
67
for filename in cve_file :
63
68
tree = self .parse_xml (filename )
64
69
root = tree .getroot ()
65
- childs = root .iter ( ' %sentry' % self .NS )
70
+ childs = root .findall ( '.// %sentry' % self .NS )
66
71
for child in childs : # child is entry Element
67
72
cve_id = child .attrib ['id' ]
68
73
cve_info = self .cve_info (child )
@@ -97,7 +102,7 @@ def cve_info(self, entry):
97
102
'wu-ftpd' , 'cluster_server' , 'catos' , 'mantis' , 'quicktime' , 'security_linux' , 'firefox' ,
98
103
'jetty_http_server' , 'php:' , 'enterprise_linux' , 'oracle10g' , 'oracle9g' , 'oracle8g' , 'firehol' ,
99
104
'fetchmail' , 'postgresql' , 'freebsd' , 'chrome' ]
100
- products = entry .iter ( ' %sproduct' % self .VULN )
105
+ products = entry .findall ( '.// %sproduct' % self .VULN )
101
106
access_complexity = entry .find ('.//%saccess-complexity' % self .CVSS )
102
107
for product in products :
103
108
module_version = product .text .split (':' )
@@ -158,7 +163,8 @@ def rule_xml(self):
158
163
rule_path = project_directory + '/rules/CVI-999'
159
164
tree .write (rule_path + str (self .year )[1 :] + '.xml' )
160
165
endtime = datetime .datetime .now ()
161
- logger .info ('CVE-999' + str (self .year )[1 :] + '.xml Rule update succeeds, times:%ds' % (endtime - starttime ).seconds )
166
+ logger .info (
167
+ 'CVE-999' + str (self .year )[1 :] + '.xml Rule update succeeds, times:%ds' % (endtime - starttime ).seconds )
162
168
163
169
def pretty (self , e , level = 0 ):
164
170
"""
@@ -179,7 +185,7 @@ def rule_parse(self, file_):
179
185
"""
180
186
tree = self .parse_xml (file_ )
181
187
root = tree .getroot ()
182
- cves = root .iter ( ' cve' )
188
+ cves = root .findall ( '.// cve' )
183
189
for cve_child in cves :
184
190
cve_id = cve_child .attrib ['id' ]
185
191
cve_level = cve_child .attrib ['level' ]
@@ -191,7 +197,7 @@ def rule_parse(self, file_):
191
197
def rule_info (cve_child ):
192
198
rule_info = {}
193
199
cpe_list = []
194
- products = cve_child .iter ( ' product' )
200
+ products = cve_child .findall ( '.// product' )
195
201
for product in products :
196
202
cpe_list .append (product .text .lower ())
197
203
rule_info ['cpe' ] = cpe_list
@@ -234,7 +240,7 @@ def log_result(self):
234
240
for cve_child in self ._scan_result [module_ ]:
235
241
cve_id = cve_child
236
242
level = self ._scan_result [module_ ][cve_id ]
237
- logger .warning ('Find the module ' + module_ + ' have ' + cve_id + ',level: ' + level )
243
+ logger .warning ('Find the module ' + module_ + ' have ' + cve_id + ',level: ' + level )
238
244
count = len (self ._scan_result [module_ ])
239
245
logger .warning ('The ' + module_ + ' module have ' + str (count ) + ' CVE Vul(s)' )
240
246
@@ -246,14 +252,14 @@ def rule_parse():
246
252
if is_update ():
247
253
gz_files = download_rule_gz ()
248
254
un_gz (gz_files )
249
- pool = multiprocessing .Pool (processes = 50 )
250
- for year in range (2002 , datetime .datetime .now ().year + 1 ):
255
+ pool = multiprocessing .Pool ()
256
+ for year in range (2002 , datetime .datetime .now ().year + 1 ):
251
257
cve_xml = "../rules/%d.xml" % year
252
258
pool .apply_async (rule_single , args = (cve_xml , year ))
253
259
pool .close ()
254
260
pool .join ()
255
- for year in range (2002 , datetime .datetime .now ().year + 1 ):
256
- os .remove (project_directory + "/rules/%d.xml" % year )
261
+ for year in range (2002 , datetime .datetime .now ().year + 1 ):
262
+ os .remove (project_directory + "/rules/%d.xml" % year )
257
263
logger .info ("The rule update success, start scan cve vuls" )
258
264
return True
259
265
else :
@@ -264,18 +270,19 @@ def download_rule_gz():
264
270
threads = []
265
271
files = []
266
272
start_time = datetime .datetime .now ()
267
- for year in range (2002 , datetime .datetime .now ().year + 1 ):
273
+ for year in range (2002 , datetime .datetime .now ().year + 1 ):
268
274
url = "https://static.nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-" + str (year ) + ".xml.gz"
269
275
logger .info ("start download " + str (year ) + ".xml.gz" )
270
- thread = threading .Thread (target = urllib .urlretrieve , args = (url , project_directory + "/rules/" + str (year )+ ".xml.gz" ))
276
+ thread = threading .Thread (target = urlretrieve ,
277
+ args = (url , project_directory + "/rules/" + str (year ) + ".xml.gz" ))
271
278
thread .start ()
272
279
threads .append (thread )
273
280
logger .info ('CVE-' + str (year ) + " is download success" )
274
- files .append (project_directory + "/rules/" + str (year ) + ".xml.gz" )
281
+ files .append (project_directory + "/rules/" + str (year ) + ".xml.gz" )
275
282
for t in threads :
276
283
t .join ()
277
284
end_time = datetime .datetime .now ()
278
- logger .info ("All CVE xml file already download success, use time:%ds" % (end_time - start_time ).seconds )
285
+ logger .info ("All CVE xml file already download success, use time:%ds" % (end_time - start_time ).seconds )
279
286
return files
280
287
281
288
@@ -286,11 +293,11 @@ def un_gz(gz_files):
286
293
for gz_file in gz_files :
287
294
f_name = gz_file .replace (".gz" , "" )
288
295
g_file = gzip .GzipFile (gz_file )
289
- open (f_name , "w +" ).write (g_file .read ())
296
+ open (f_name , "wb +" ).write (g_file .read ())
290
297
g_file .close ()
291
298
os .remove (gz_file )
292
299
end_time = datetime .datetime .now ()
293
- logger .info ("Decompress success, use time:%ds" % (end_time - start_time ).seconds )
300
+ logger .info ("Decompress success, use time:%ds" % (end_time - start_time ).seconds )
294
301
return True
295
302
296
303
@@ -300,9 +307,9 @@ def rule_single(target_directory, year):
300
307
301
308
def is_update ():
302
309
url = "https://static.nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-modified.meta"
303
- r = requests .get (url )
310
+ r = requests .get (url , verify = False )
304
311
index = r .text .find ('sha256:' )
305
- sha256_now = r .text [index + 7 :].strip ()
312
+ sha256_now = r .text [index + 7 :].strip ()
306
313
sha256_local = Config (level1 = 'cve' , level2 = 'modified' ).value
307
314
if sha256_local != sha256_now :
308
315
logger .info ("The CVE Rule already update, start update local rule" )
@@ -314,7 +321,7 @@ def is_update():
314
321
config .write (fi )
315
322
fi .close ()
316
323
except IOError as e :
317
- logger .warning (e . message )
324
+ logger .warning (e )
318
325
logger .info ("The sha256 been update" )
319
326
return True
320
327
return False
0 commit comments