@@ -90,31 +90,17 @@ def remove_skiplist(self, skips):
90
90
def print_checkers (self ):
91
91
self .logger .info (f'Checkers: { ", " .join (self .checkers .keys ())} ' )
92
92
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"""
108
95
109
- # step 1: check if it's an ELF binary file
110
96
if inpath ("file" ):
111
97
# use system file if available (for performance reasons)
112
98
output = subprocess .check_output (["file" , filename ])
113
99
output = output .decode (sys .stdout .encoding )
114
100
115
101
if "cannot open" in output :
116
102
self .logger .warning (f"Unopenable file { filename } cannot be scanned" )
117
- return None
103
+ return False
118
104
119
105
if (
120
106
("LSB " not in output )
@@ -126,11 +112,16 @@ def scan_file(self, filename):
126
112
and ("PKG-INFO: " not in output )
127
113
and ("METADATA: " not in output )
128
114
):
129
- return None
115
+ return False
130
116
# otherwise use python implementation of file
131
117
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
+
134
125
if inpath ("strings" ):
135
126
# use "strings" on system if available (for performance)
136
127
lines = (
@@ -140,7 +131,38 @@ def scan_file(self, filename):
140
131
)
141
132
else :
142
133
# 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
+
144
166
# If python package then strip the lines to avoid detecting other product strings
145
167
if "PKG-INFO: " in output or "METADATA: " in output :
146
168
lines = lines [1 :3 ]
0 commit comments