|
29 | 29 | base_url = "https://lwn.net/" |
30 | 30 |
|
31 | 31 |
|
32 | | -def extractPackageData(advisoryLink,dist,advisoryId): |
33 | | - |
34 | | - content = rq.get(advisoryLink).content |
35 | | - soup = bs(content,"html.parser") |
36 | | - text = soup.find('div',{'class':'ArticleText'}).get_text() |
37 | | - phrases = text.split('\n') |
38 | | - cves = [] |
39 | | - references = [] |
40 | | - summary = "" |
41 | | - for i in range(len(phrases)): |
42 | | - words = phrases[i].split() |
43 | | - if phrases[i].startswith('Subject:'): |
44 | | - summary = phrases[i+1].strip() |
45 | | - for word in words: |
46 | | - if word.startswith('CVE-') and word != 'CVE-ID': |
47 | | - cves.append(word) |
48 | | - elif word.startswith('https://') or word.startswith('http://'): |
49 | | - references.append(word) |
50 | | - |
51 | | - cves = list(set(cves)) |
52 | | - |
53 | | - dist = re.sub(r'\W+', '', dist).replace('_','').lower() |
54 | | - |
55 | | - return {'cve_ids':cves,'references':references,'summary':summary,'advisory_id':advisoryId,'distributor':dist,'advisory_link':advisoryLink} |
| 32 | +def extractPackageData(advisoryLink, dist, advisoryId): |
| 33 | + |
| 34 | + content = rq.get(advisoryLink).content |
| 35 | + soup = bs(content, "html.parser") |
| 36 | + text = soup.find('div', {'class': 'ArticleText'}).get_text() |
| 37 | + phrases = text.split('\n') |
| 38 | + cves = [] |
| 39 | + references = [] |
| 40 | + summary = "" |
| 41 | + for i in range(len(phrases)): |
| 42 | + words = phrases[i].split() |
| 43 | + if phrases[i].startswith('Subject:'): |
| 44 | + summary = phrases[i + 1].strip() |
| 45 | + for word in words: |
| 46 | + if word.startswith('CVE-') and word != 'CVE-ID': |
| 47 | + cves.append(word) |
| 48 | + elif word.startswith('https://') or word.startswith('http://'): |
| 49 | + references.append(word) |
| 50 | + |
| 51 | + cves = list(set(cves)) |
| 52 | + |
| 53 | + dist = re.sub(r'\W+', '', dist).replace('_', '').lower() |
| 54 | + |
| 55 | + return { |
| 56 | + 'cve_ids': cves, |
| 57 | + 'references': references, |
| 58 | + 'summary': summary, |
| 59 | + 'advisory_id': advisoryId, |
| 60 | + 'distributor': dist, |
| 61 | + 'advisory_link': advisoryLink} |
| 62 | + |
56 | 63 |
|
57 | 64 | def getDistributors(): |
58 | | - url = base_url+"Alerts/" |
59 | | - content = rq.get(url).content |
60 | | - soup = bs(content,"html.parser") |
61 | | - dists = [] |
62 | | - distsLinks = [] |
63 | | - tables = soup.find_all('table',{'cellspacing':"4",}) |
| 65 | + url = base_url + "Alerts/" |
| 66 | + content = rq.get(url).content |
| 67 | + soup = bs(content, "html.parser") |
| 68 | + dists = [] |
| 69 | + distsLinks = [] |
| 70 | + tables = soup.find_all('table', {'cellspacing': "4", }) |
| 71 | + |
| 72 | + for table in tables: |
| 73 | + distsLinks += table.find_all('a') |
64 | 74 |
|
65 | | - for table in tables: |
66 | | - distsLinks += table.find_all('a') |
| 75 | + for a in distsLinks: |
| 76 | + dists.append(a['href']) |
67 | 77 |
|
68 | | - for a in distsLinks: |
69 | | - dists.append(a['href']) |
| 78 | + return dists |
70 | 79 |
|
71 | | - return dists |
72 | 80 |
|
73 | 81 | def scrape_vulnerabilities(): |
74 | | - dists = getDistributors() |
75 | | - packagesVulns = {} |
76 | | - dists = dists[:5] |
77 | | - for dist in dists: |
78 | | - distUrl = base_url+"Alerts/"+dist+"?n=100" |
79 | | - distContent = rq.get(distUrl).content |
80 | | - distSoup = bs(distContent,"html.parser") |
81 | | - articleSoup = distSoup.find('div',{'class':'ArticleText'}) |
82 | | - text = articleSoup.get_text() |
83 | | - total = int(text[text.find("(")+1:text.find(")")].split()[0]) |
84 | | - curr_offset = 0 |
85 | | - while curr_offset < total: |
86 | | - |
87 | | - table = articleSoup.find('table',{'cellpadding':4}) |
88 | | - |
89 | | - data = table.find_all('tr') |
90 | | - data = data[1:] |
91 | | - for row in data: |
92 | | - rowElements = row.find_all('td') |
93 | | - aTag = rowElements[0].find('a') |
94 | | - advisoryLink = base_url[:-1]+aTag['href'] |
95 | | - advisoryId = aTag.get_text() |
96 | | - package_names = rowElements[1].get_text().split(',') |
97 | | - date = rowElements[2].get_text() |
98 | | - for package_name in package_names: |
99 | | - extracted_data = extractPackageData(advisoryLink,dist,advisoryId) |
100 | | - if packagesVulns.get(package_name): |
101 | | - packagesVulns[package_name].append(extracted_data) |
102 | | - else: |
103 | | - packagesVulns[package_name] = [extracted_data] |
104 | | - |
105 | | - |
106 | | - curr_offset += 100 |
107 | | - distUrl = distUrl+"&offset="+str(curr_offset) |
108 | | - distSoup = bs(distContent,"html.parser") |
109 | | - articleSoup = distSoup.find('div',{'class':'ArticleText'}) |
110 | | - |
111 | | - |
112 | | - return packagesVulns |
| 82 | + dists = getDistributors() |
| 83 | + packagesVulns = {} |
| 84 | + for dist in dists: |
| 85 | + distUrl = base_url + "Alerts/" + dist + "?n=100" |
| 86 | + distContent = rq.get(distUrl).content |
| 87 | + distSoup = bs(distContent, "html.parser") |
| 88 | + articleSoup = distSoup.find('div', {'class': 'ArticleText'}) |
| 89 | + text = articleSoup.get_text() |
| 90 | + total = int(text[text.find("(") + 1:text.find(")")].split()[0]) |
| 91 | + curr_offset = 0 |
| 92 | + while curr_offset < total: |
| 93 | + |
| 94 | + table = articleSoup.find('table', {'cellpadding': 4}) |
| 95 | + |
| 96 | + data = table.find_all('tr') |
| 97 | + data = data[1:] |
| 98 | + for row in data: |
| 99 | + rowElements = row.find_all('td') |
| 100 | + aTag = rowElements[0].find('a') |
| 101 | + advisoryLink = base_url[:-1] + aTag['href'] |
| 102 | + advisoryId = aTag.get_text() |
| 103 | + package_names = rowElements[1].get_text().split(',') |
| 104 | + date = rowElements[2].get_text() |
| 105 | + for package_name in package_names: |
| 106 | + extracted_data = extractPackageData( |
| 107 | + advisoryLink, dist, advisoryId) |
| 108 | + if packagesVulns.get(package_name): |
| 109 | + packagesVulns[package_name].append(extracted_data) |
| 110 | + else: |
| 111 | + packagesVulns[package_name] = [extracted_data] |
| 112 | + |
| 113 | + curr_offset += 100 |
| 114 | + distUrl = distUrl + "&offset=" + str(curr_offset) |
| 115 | + distSoup = bs(distContent, "html.parser") |
| 116 | + articleSoup = distSoup.find('div', {'class': 'ArticleText'}) |
| 117 | + |
| 118 | + return packagesVulns |
0 commit comments