Skip to content

Commit 0ab0c7b

Browse files
committed
feat(parser): add Windows PE parser
Built from #5159, this alternative has the following advantages: - it doesn't create a fake "line" that is passed to binary checker - python binary checker doesn't have to be updated - it allows the end-user to disable this new behavior through "-s pe" - any specific PE handling such as setting the product to lower case or converting "Python Software Foundation" to "python" is done in pe.py For now, pe parser runs only on pyd files but this could be updated. Credits shall be given to @alex-cheng-techman which created most of the original code Signed-off-by: Fabrice Fontaine <[email protected]>
1 parent d7a69a6 commit 0ab0c7b

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ A number of checkers are available for finding vulnerable components in specific
310310
| Ruby | `Gemfile.lock` |
311311
| R | `renv.lock` |
312312
| Swift | `Package.resolved` |
313+
| Windows PE | `.pyd` files |
313314

314315
More information on [language-specific checkers](https://github.com/intel/cve-bin-tool/blob/main/doc/MANUAL.md#language-specific-checkers) can be found in the [CVE Binary Tool manual](https://cve-bin-tool.readthedocs.io/en/latest/MANUAL.html).
315316

cve_bin_tool/parsers/pe.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (C) 2025 Orange
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
"""Python script containing all functionalities related to parsing of Windows PE files."""
4+
5+
import pefile
6+
7+
from cve_bin_tool.parsers import Parser
8+
from cve_bin_tool.util import ProductInfo, ScanInfo
9+
10+
11+
class PeParser(Parser):
12+
"""
13+
Parser for Windows PE files
14+
"""
15+
16+
PARSER_MATCH_FILENAMES = [
17+
".pyd",
18+
]
19+
20+
def __init__(self, cve_db, logger):
21+
"""Initialize the pe package metadata parser."""
22+
super().__init__(cve_db, logger)
23+
24+
def run_checker(self, filename):
25+
"""
26+
This generator runs only for Windows PE files.
27+
There are no actual checkers.
28+
"""
29+
self.filename = filename
30+
try:
31+
with pefile.PE(filename) as pe:
32+
for fileinfo in pe.FileInfo:
33+
for entry in fileinfo:
34+
if entry.Key == b"StringFileInfo":
35+
for st in entry.StringTable:
36+
entries = st.entries
37+
vendor = entries.get(b"CompanyName", b"").decode(
38+
errors="ignore"
39+
)
40+
if vendor == "Python Software Foundation":
41+
vendor = "python"
42+
product = (
43+
entries.get(b"ProductName", b"")
44+
.decode(errors="ignore")
45+
.lower()
46+
)
47+
version = entries.get(b"ProductVersion", b"").decode(
48+
errors="ignore"
49+
)
50+
self.logger.debug(product)
51+
vendorlist: list[ScanInfo] = [
52+
ScanInfo(
53+
ProductInfo(vendor, product, version),
54+
self.filename,
55+
)
56+
]
57+
yield from vendorlist
58+
except pefile.PEFormatError:
59+
self.logger.debug(f"Failed to parse PE file {filename}")
60+
self.logger.debug(f"Done scanning file: {filename}")

doc/MANUAL.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
- [Dart](#dart)
9595
- [C/C++](#cc)
9696
- [OpenWrt opkg](#openwrt-opkg)
97+
- [Windows PE](#windows-pe)
9798
- [Feedback \& Contributions](#feedback--contributions)
9899
- [Security Issues](#security-issues)
99100

@@ -573,7 +574,7 @@ The CVE binary tool is utilized to identify vulnerabilities within a software. W
573574

574575
Once the database is populated, the CVE binary tool conducts searches for CVEs using two distinct methods:
575576

576-
- The first approach involves examining language component lists (e.g., requirement.txt, package.json) for different programming languages. Presently, the CVE binary tool provides support for 12 languages: Dart, Go, Java, JavaScript, OpenWrt Opkg, Python, Perl, PHP, R, Ruby, Rust, and Swift. If your desired language is not listed, you can refer to this guide on [how to add a parser](../cve_bin_tool/parsers/README.md) for it.
577+
- The first approach involves examining language component lists (e.g., requirement.txt, package.json) for different programming languages. Presently, the CVE binary tool provides support for 12 languages: Dart, Go, Java, JavaScript, OpenWrt Opkg, Python, Perl, PHP, R, Ruby, Rust, Swift and Windows PE. If your desired language is not listed, you can refer to this guide on [how to add a parser](../cve_bin_tool/parsers/README.md) for it.
577578

578579
- The second method employs checkers to gather information about software vulnerabilities. Checkers consist of predefined information about software entities. The CVE binary tool scans binaries for patterns matching the descriptions provided by the checkers, thereby extracting details like software version and vendor. At present, the CVE binary tool includes over 300 checkers. Crafting new checkers is a straightforward process and can serve as a beginner-friendly task. You can learn more about [adding checkers here](../cve_bin_tool/checkers/README.md).
579580

@@ -1605,6 +1606,10 @@ Here's an example of what a [`conan.lock`](https://github.com/intel/cve-bin-tool
16051606

16061607
The scanner examines the `.control` file within an embedded system to identify components. The CPE-IDs and versions are used to search the database for vulnerabilities. Packages with no CPE-ID are ignored to avoid wrong results.
16071608

1609+
### Windows PE
1610+
1611+
The scanner examines the Windows PE file to identify components. The product name (in lower case), company name and versions are used to search the database for vulnerabilities.
1612+
16081613
## Feedback & Contributions
16091614

16101615
Bugs and feature requests can be made via [GitHub issues](https://github.com/intel/cve-bin-tool/issues).

doc/PARSERS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following parsers have been added to the project:
1717
- **JavaParser**
1818
- **JavascriptParser**
1919
- **OpkgParser**
20+
- **PeParser**
2021
- **PerlParser**
2122
- **PhpParser**
2223
- **PythonParser**
@@ -39,6 +40,7 @@ To utilize these parsers, ensure that your project includes the following import
3940
from cve_bin_tool.parsers.java import JavaParser
4041
from cve_bin_tool.parsers.javascript import JavascriptParser
4142
from cve_bin_tool.parsers.opkg import OpkgParser
43+
from cve_bin_tool.parsers.perl import PeParser
4244
from cve_bin_tool.parsers.perl import PerlParser
4345
from cve_bin_tool.parsers.php import PhpParser
4446
from cve_bin_tool.parsers.python import PythonParser, PythonRequirementsParser

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ lib4sbom==0.8.4 # Pinned due to bug. Was lib4sbom>=0.7.2
1313
lib4vex>=0.2.0
1414
packageurl-python
1515
packaging>=22.0
16+
pefile
1617
plotly
1718
python-gnupg
1819
pyyaml>=5.4

0 commit comments

Comments
 (0)