11
11
find ../path/to/binaries -type f -executable | xargs python3 contrib/devtools/symbol-check.py
12
12
'''
13
13
import sys
14
- from typing import List , Optional
14
+ from typing import List
15
15
16
16
import lief
17
17
@@ -144,9 +144,8 @@ def check_version(max_versions, version, arch) -> bool:
144
144
else :
145
145
return ver <= max_versions [lib ][arch ]
146
146
147
- def check_imported_symbols (filename ) -> bool :
147
+ def check_imported_symbols (binary ) -> bool :
148
148
ok : bool = True
149
- binary = lief .parse (filename )
150
149
151
150
for symbol in binary .imported_symbols :
152
151
if not symbol .imported :
@@ -161,62 +160,55 @@ def check_imported_symbols(filename) -> bool:
161
160
ok = False
162
161
return ok
163
162
164
- def check_exported_symbols (filename ) -> bool :
163
+ def check_exported_symbols (binary ) -> bool :
165
164
ok : bool = True
166
- binary = lief .parse (filename )
167
165
168
166
for symbol in binary .dynamic_symbols :
169
167
if not symbol .exported :
170
168
continue
171
169
name = symbol .name
172
170
if binary .header .machine_type == LIEF_ELF_ARCH_RISCV or name in IGNORE_EXPORTS :
173
171
continue
174
- print (f'{ filename } : export of symbol { name } not allowed!' )
172
+ print (f'{ binary . name } : export of symbol { name } not allowed!' )
175
173
ok = False
176
174
return ok
177
175
178
- def check_ELF_libraries (filename ) -> bool :
176
+ def check_ELF_libraries (binary ) -> bool :
179
177
ok : bool = True
180
- binary = lief .parse (filename )
181
178
for library in binary .libraries :
182
179
if library not in ELF_ALLOWED_LIBRARIES :
183
180
print (f'{ filename } : { library } is not in ALLOWED_LIBRARIES!' )
184
181
ok = False
185
182
return ok
186
183
187
- def check_MACHO_libraries (filename ) -> bool :
184
+ def check_MACHO_libraries (binary ) -> bool :
188
185
ok : bool = True
189
- binary = lief .parse (filename )
190
186
for dylib in binary .libraries :
191
187
split = dylib .name .split ('/' )
192
188
if split [- 1 ] not in MACHO_ALLOWED_LIBRARIES :
193
189
print (f'{ split [- 1 ]} is not in ALLOWED_LIBRARIES!' )
194
190
ok = False
195
191
return ok
196
192
197
- def check_MACHO_min_os (filename ) -> bool :
198
- binary = lief .parse (filename )
193
+ def check_MACHO_min_os (binary ) -> bool :
199
194
if binary .build_version .minos == [10 ,15 ,0 ]:
200
195
return True
201
196
return False
202
197
203
- def check_MACHO_sdk (filename ) -> bool :
204
- binary = lief .parse (filename )
198
+ def check_MACHO_sdk (binary ) -> bool :
205
199
if binary .build_version .sdk == [10 , 15 , 6 ]:
206
200
return True
207
201
return False
208
202
209
- def check_PE_libraries (filename ) -> bool :
203
+ def check_PE_libraries (binary ) -> bool :
210
204
ok : bool = True
211
- binary = lief .parse (filename )
212
205
for dylib in binary .libraries :
213
206
if dylib not in PE_ALLOWED_LIBRARIES :
214
207
print (f'{ dylib } is not in ALLOWED_LIBRARIES!' )
215
208
ok = False
216
209
return ok
217
210
218
- def check_PE_subsystem_version (filename ) -> bool :
219
- binary = lief .parse (filename )
211
+ def check_PE_subsystem_version (binary ) -> bool :
220
212
major : int = binary .optional_header .major_subsystem_version
221
213
minor : int = binary .optional_header .minor_subsystem_version
222
214
if major == 6 and minor == 1 :
@@ -240,30 +232,20 @@ def check_PE_subsystem_version(filename) -> bool:
240
232
]
241
233
}
242
234
243
- def identify_executable (executable ) -> Optional [str ]:
244
- with open (filename , 'rb' ) as f :
245
- magic = f .read (4 )
246
- if magic .startswith (b'MZ' ):
247
- return 'PE'
248
- elif magic .startswith (b'\x7f ELF' ):
249
- return 'ELF'
250
- elif magic .startswith (b'\xcf \xfa ' ):
251
- return 'MACHO'
252
- return None
253
-
254
235
if __name__ == '__main__' :
255
236
retval : int = 0
256
237
for filename in sys .argv [1 :]:
257
238
try :
258
- etype = identify_executable (filename )
259
- if etype is None :
260
- print (f'{ filename } : unknown format' )
239
+ binary = lief .parse (filename )
240
+ etype = binary .format .name
241
+ if etype == lief .EXE_FORMATS .UNKNOWN :
242
+ print (f'{ filename } : unknown executable format' )
261
243
retval = 1
262
244
continue
263
245
264
246
failed : List [str ] = []
265
247
for (name , func ) in CHECKS [etype ]:
266
- if not func (filename ):
248
+ if not func (binary ):
267
249
failed .append (name )
268
250
if failed :
269
251
print (f'{ filename } : failed { " " .join (failed )} ' )
0 commit comments