Skip to content

Support Windows PE metadata parsing for Python binaries #5159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions cve_bin_tool/checkers/openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class OpensslChecker(Checker):
CONTAINS_PATTERNS = [r"part of OpenSSL", r"openssl.cnf", r"-DOPENSSL_"]
FILENAME_PATTERNS = [r"libssl.so.", r"libcrypto.so"]
VERSION_PATTERNS = [
# for general format: OpenSSL 1.0.2u�BOpenSSL 3.0.0�BOpenSSL 1.1.1k
r"OpenSSL\s+([0-9]+\.[0-9]+\.[0-9]+[a-z]*)",

r"OpenSSL ([0-9]+\.[0-9]+\.[0-9]+[a-z]*) [a-zA-Z0-9 ]+\r?\n(?:%s \(Library: %s\)|[a-zA-Z0-9:,_ \.\-\r\n]*OPENSSLDIR|ssl)",
r"(?:%s \(Library: %s\)\r?\n|OPENSSLDIR[a-zA-Z0-9:/ \"\-\r\n]*)OpenSSL ([0-9]+\.[0-9]+\.[0-9]+[a-z]*) [a-zA-Z0-9 ]+",
]
Expand Down
3 changes: 3 additions & 0 deletions cve_bin_tool/checkers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class PythonChecker(Checker):
]
FILENAME_PATTERNS = [r"python"]
VERSION_PATTERNS = [
# to match the data from PE file
r"[Pp]ython ([0-9]+\.[0-9]+\.[0-9]+)",

r"src\\python[23]\\Python-([23]+\.[0-9]+\.[0-9]+)",
r"python(?:[23]+\.[0-9]+)-([23]+\.[0-9]+\.[0-9]+)",
r"pymalloc_debug\r?\n([23]+\.[0-9]+\.[0-9]+)",
Expand Down
22 changes: 22 additions & 0 deletions cve_bin_tool/version_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ def is_linux_kernel(self, filename: str) -> tuple[bool, str | None]:

return False, output

# used to get product name, version, vendor info PE metadata
def extract_version_from_pe(self, filename: str) -> str:
info = ""
try:
import pefile
with pefile.PE(filename) as pe:
#pe = pefile.PE(filename)
for fileinfo in pe.FileInfo:
for entry in fileinfo:
if entry.Key == b'StringFileInfo':
for st in entry.StringTable:
entries = st.entries
product_name = entries.get(b'ProductName', b'').decode(errors='ignore')
product_version = entries.get(b'ProductVersion', b'').decode(errors='ignore')
company_name = entries.get(b'CompanyName', b'').decode(errors='ignore')
info = (f" {product_name} {product_version} {company_name}")
self.logger.debug(f"peFile.PE Metadata:{info}")
except Exception as e:
LOGGER.debug(f"[PE Metadata] Failed to parse PE file {filename}: {e}")
return info

def scan_file(self, filename: str) -> Iterator[ScanInfo]:
"""Scans a file to see if it contains any of the target libraries,
and whether any of those contain CVEs"""
Expand Down Expand Up @@ -261,6 +282,7 @@ def scan_file(self, filename: str) -> Iterator[ScanInfo]:

# parse binary file's strings
lines = parse_strings(filename)
lines += self.extract_version_from_pe(filename)

if self.no_scan:
yield from self.run_checkers(filename, lines)
Expand Down