Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions vulnerabilities/pipelines/enhance_with_metasploit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

import logging
import re
from traceback import format_exc as traceback_format_exc

import requests
Expand Down Expand Up @@ -69,11 +70,26 @@ def add_vulnerability_exploit(record, logger):
vulnerabilities = set()
references = record.get("references", [])

interesting_references = [
ref for ref in references if not ref.startswith("OSVDB") and not ref.startswith("URL-")
]
# Regex to allow short commit hashes (6–40 chars)
commit_pattern = re.compile(r"https://github\.com/.+/commit/[a-f0-9]{6,40}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this function instead and this should not be github-specific.


if not interesting_references:
interesting_references = []
commit_links = set()

for ref in references:
if ref.startswith("OSVDB"):
continue

# Handle "URL-" prefixed references
if ref.startswith("URL-"):
url = ref[4:]
interesting_references.append(url)
if commit_pattern.match(url):
commit_links.add(url)
else:
interesting_references.append(ref)

if not interesting_references and not commit_links:
return 0

for ref in interesting_references:
Expand All @@ -94,17 +110,22 @@ def add_vulnerability_exploit(record, logger):
source_url = ""
if path := record.get("path"):
source_url = f"https://github.com/rapid7/metasploit-framework/tree/master{path}"
source_date_published = None

# Add unique commit links to notes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this comment? The code below looks obvious

if commit_links:
notes["commit_links"] = sorted(commit_links)

source_date_published = None
if disclosure_date := record.get("disclosure_date"):
try:
source_date_published = dateparser.parse(disclosure_date).date()
except ValueError as e:
logger(
f"Error while parsing date {disclosure_date} with error {e!r}:\n{traceback_format_exc()}",
f"Error parsing date {disclosure_date}: {e!r}\n{traceback_format_exc()}",
level=logging.ERROR,
)

# Save or update exploit records
for vulnerability in vulnerabilities:
Exploit.objects.update_or_create(
vulnerability=vulnerability,
Expand All @@ -117,4 +138,5 @@ def add_vulnerability_exploit(record, logger):
"source_url": source_url,
},
)

return 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we return 1 here? This is highly unsual