|
| 1 | +# |
| 2 | +# Copyright (c) 2017 nexB Inc. and others. All rights reserved. |
| 3 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 4 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with VulnerableCode require an acknowledgment. |
| 6 | +# |
| 7 | +# You may not use this software except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | +# specific language governing permissions and limitations under the License. |
| 13 | +# |
| 14 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 15 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 16 | +# |
| 17 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 18 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 19 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 20 | +# for any legal advice. |
| 21 | +# VulnerableCode is a free software code scanning tool from nexB Inc. and others. |
| 22 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 23 | + |
| 24 | +import logging |
| 25 | +import re |
| 26 | +from urllib.request import urlopen |
| 27 | + |
| 28 | +import bs4 |
| 29 | + |
| 30 | + |
| 31 | +DEBIAN_ROOT_URL = 'https://security-tracker.debian.org' |
| 32 | + |
| 33 | + |
| 34 | +def extract_tracker_paths(html): |
| 35 | + """ |
| 36 | + Return a list of tracker URL paths extracted from the given `html` input. |
| 37 | + """ |
| 38 | + soup = bs4.BeautifulSoup(html, 'lxml') |
| 39 | + tracker_links = soup.findAll('a', href=re.compile('^/track+.*')) |
| 40 | + return [link.get('href') for link in tracker_links] |
| 41 | + |
| 42 | + |
| 43 | +def extract_cves_from_tracker(html): |
| 44 | + """ |
| 45 | + Return all CVEs extracted from the given `html` input. |
| 46 | + """ |
| 47 | + cve_id = [] |
| 48 | + package_name = [] |
| 49 | + vulnerability_status = [] |
| 50 | + soup = bs4.BeautifulSoup(html, 'lxml') |
| 51 | + |
| 52 | + for tag in soup.find_all('a'): |
| 53 | + href = tag.get('href') |
| 54 | + |
| 55 | + if re.search('/tracker/CVE-(.+)', href): |
| 56 | + id = re.findall('(?<=/tracker/).*', href) |
| 57 | + cve_id.append(id[0]) |
| 58 | + |
| 59 | + if re.search('^/tracker/TEMP-+.*', href): |
| 60 | + id = re.findall('(?<=/tracker/).*', href) |
| 61 | + cve_id.append(id[0]) |
| 62 | + |
| 63 | + if re.search('/tracker/source-package/(.+)', href): |
| 64 | + pkg = re.findall('(?<=/tracker/source-package/).*', href) |
| 65 | + package_name.append(pkg[0]) |
| 66 | + |
| 67 | + # if package name is empty, use the previous package name |
| 68 | + if href == '/tracker/source-package/': |
| 69 | + package_name.append(pkg) |
| 70 | + |
| 71 | + for tag in soup.find_all('td'): |
| 72 | + if 'medium' in tag or 'low' in tag or 'not yet assigned' in tag: |
| 73 | + vulnerability_status.append(tag.text) |
| 74 | + elif tag.find_all('span', {'class': 'red'}) and tag.text == 'high**' or tag.text == 'high': |
| 75 | + vulnerability_status.append(tag.text) |
| 76 | + |
| 77 | + return cve_id, package_name, vulnerability_status |
| 78 | + |
| 79 | + |
| 80 | +def scrape_cves(): |
| 81 | + """ |
| 82 | + Runs the full scraping process of Debian CVEs. |
| 83 | + """ |
| 84 | + tracker_root_html = urlopen(f'{DEBIAN_ROOT_URL}/tracker/').read() |
| 85 | + tracker_paths = extract_tracker_paths(tracker_root_html) |
| 86 | + |
| 87 | + cves = [] |
| 88 | + for tracker_path in tracker_paths: |
| 89 | + tracker_url = f'{DEBIAN_ROOT_URL}{tracker_path}/' |
| 90 | + logging.info(f'Visiting: {tracker_url}') |
| 91 | + html = urlopen(tracker_url).read() |
| 92 | + cves.append(extract_cves_from_tracker(html)) |
| 93 | + |
| 94 | + return cves |
0 commit comments