#!/usr/bin/python3 from .DebianAdvisory import DebianAdvisory as da from .CVEParse import CVEParse as cveparse import re import datetime from html.parser import HTMLParser from bs4 import BeautifulSoup from bs4 import NavigableString from pymongo import MongoClient import urllib.request import logging, sys import pickle import json from fixcwes import ret_roots def getDLAs(): src2monthDLAs = dict() totalDLAs = dict() totalDLAsList = [] base_url = 'https://lists.debian.org/debian-lts-announce/' logging.info('Checking for DLAs...\n') dlas = [] more = True i = 0 j = 0 for year in range(2014,2019): for month in range(1,13): totalDLAs[str(year) + ',' + str(month)] = [] i = 0 while more: try: url = base_url + str(year) + '/' + str(month).zfill(2) + '/msg' + str(i).zfill(5) + '.html' print('Opening url: ' + url + '\n') req = urllib.request.urlopen(url) charset = req.info().get_content_charset() if charset is None: charset = 'utf-8' dla = req.read().decode(charset) dlas.append([dla, year, month]) p1 = re.compile('Package.*: .*') p2 = re.compile('CVE-[0-9]{4}-[0-9]*') (pkg, cves) = parseDLAhtml(dla, p1, p2) pkg = fixURL(url, pkg) if pkg: totalDLAs[str(year) + ',' + str(month)] += cves try: src2monthDLAs[pkg].append((cves, [year,month])) except KeyError: src2monthDLAs[pkg] = [] src2monthDLAs[pkg].append((cves, [year,month])) except urllib.error.HTTPError as err: if (i>1): break i+=1 print(totalDLAs[str(year) + ',' + str(month)]) totalDLAs[str(year) + ',' + str(month)] = list(set(totalDLAs[str(year) + ',' + str(month)])) totalDLAsList.append(len(totalDLAs[str(year) + ',' + str(month)])) j += 1 print(totalDLAs) print(totalDLAsList) with open("DLA_sum.txt","wb") as fp: pickle.dump(totalDLAsList,fp) with open("src2month_DLA.txt","wb") as fp: pickle.dump(src2monthDLAs,fp) with open("src2month_DLA.json","w") as fp: json.dump(src2monthDLAs,fp) return(totalDLAsList) def permonthDLA(src2monthDLAs): client = MongoClient() out = dict() out_cvss = dict() out_cwe = dict() for pkg in src2monthDLAs: (out[pkg], out_cvss[pkg], out_cwe[pkg]) = perPackage(pkg, src2monthDLAs[pkg], out, out_cvss, client) #out_cwe[pkg] = perPackage_cwe(pkg, src2monthDLAs[pkg]) with open("DLA_src2month.json","w") as fp: json.dump(out,fp) with open("DLA_withcvss.json","w") as fp: json.dump(out_cvss,fp) # with open("DLA_withcwe.json","w") as fp: json.dump(out_cwe,fp) def perPackage(pkg, dlas, cvss, out, client): root_list = ['682', '118', '330', '435', '664', '691', '693', '697', '703', '707', '710' ] monthyear = [] monthyear_cvss = [] monthyear_cwe = [] haveseen = dict() for i in range(2000,2019): temp = [] temp_cvss = [] temp_cwe = [] for j in range(12): temp.append(0) temp_cvss.append([0,0,0,0]) temp_cwe.append([0]*12) monthyear.append(temp) monthyear_cvss.append(temp_cvss) monthyear_cwe.append(temp_cwe) for dla in dlas: for cve_id in dla[0]: if cve_id in haveseen: continue else: haveseen[cve_id] = 1 cve = cveparse.fetchCVE(cve_id, client) (cve_date, cve_base, cve_impact, cve_exploit, cwe) = cveparse.parseCVE(cve_id, cve) new_year = dla[1][0] new_month = dla[1][1] if (cve_date.year