Skip to content

Commit b4c924a

Browse files
authored
refactor: scan_file() in versionscanner.py (#1226)
* refactor: scan_file() in versionscanner.py * fix: isort and black
1 parent f0f4a9d commit b4c924a

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

cve_bin_tool/version_scanner.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,17 @@ def remove_skiplist(self, skips):
9090
def print_checkers(self):
9191
self.logger.info(f'Checkers: {", ".join(self.checkers.keys())}')
9292

93-
def scan_file(self, filename):
94-
"""Scans a file to see if it contains any of the target libraries,
95-
and whether any of those contain CVEs"""
96-
97-
self.logger.debug(f"Scanning file: {filename}")
98-
self.total_scanned_files += 1
99-
100-
# Do not try to scan symlinks
101-
if os.path.islink(filename):
102-
return None
103-
104-
# Ensure filename is a file
105-
if not os.path.isfile(filename):
106-
self.logger.warning(f"Invalid file {filename} cannot be scanned")
107-
return None
93+
def is_executable(self, filename):
94+
"""check if file is an ELF binary file"""
10895

109-
# step 1: check if it's an ELF binary file
11096
if inpath("file"):
11197
# use system file if available (for performance reasons)
11298
output = subprocess.check_output(["file", filename])
11399
output = output.decode(sys.stdout.encoding)
114100

115101
if "cannot open" in output:
116102
self.logger.warning(f"Unopenable file {filename} cannot be scanned")
117-
return None
103+
return False
118104

119105
if (
120106
("LSB " not in output)
@@ -126,11 +112,16 @@ def scan_file(self, filename):
126112
and ("PKG-INFO: " not in output)
127113
and ("METADATA: " not in output)
128114
):
129-
return None
115+
return False
130116
# otherwise use python implementation of file
131117
elif not is_binary(filename):
132-
return None
133-
# parse binary file's strings
118+
return False
119+
120+
return True, output
121+
122+
def parse_strings(self, filename):
123+
"""parse binary file's strings"""
124+
134125
if inpath("strings"):
135126
# use "strings" on system if available (for performance)
136127
lines = (
@@ -140,7 +131,38 @@ def scan_file(self, filename):
140131
)
141132
else:
142133
# Otherwise, use python implementation
143-
lines = Strings(filename).parse()
134+
s = Strings(filename)
135+
lines = s.parse()
136+
return lines
137+
138+
def scan_file(self, filename):
139+
"""Scans a file to see if it contains any of the target libraries,
140+
and whether any of those contain CVEs"""
141+
142+
self.logger.debug(f"Scanning file: {filename}")
143+
self.total_scanned_files += 1
144+
145+
# Do not try to scan symlinks
146+
if os.path.islink(filename):
147+
return None
148+
149+
# Ensure filename is a file
150+
if not os.path.isfile(filename):
151+
self.logger.warning(f"Invalid file {filename} cannot be scanned")
152+
return None
153+
154+
# check if it's an ELF binary file
155+
try:
156+
t, output = self.is_executable(filename)
157+
except:
158+
t = self.is_executable(filename)
159+
160+
if not t:
161+
return None
162+
163+
# parse binary file's strings
164+
lines = self.parse_strings(filename)
165+
144166
# If python package then strip the lines to avoid detecting other product strings
145167
if "PKG-INFO: " in output or "METADATA: " in output:
146168
lines = lines[1:3]

0 commit comments

Comments
 (0)