|
7 | 7 | This module is intended for general purpose functions that can be used |
8 | 8 | thoughout the qiling framework |
9 | 9 | """ |
10 | | -import importlib, logging, os, logging, copy, re, pefile, magic, configparser |
| 10 | +import importlib, logging, os, logging, copy, re, pefile, configparser |
11 | 11 | from logging import LogRecord |
12 | 12 | from pathlib import Path |
13 | 13 | from .exception import * |
@@ -333,8 +333,11 @@ def getident(): |
333 | 333 | return arch, ostype, archendian |
334 | 334 |
|
335 | 335 | def ql_pe_parse_emu_env(path): |
| 336 | + try: |
| 337 | + pe = pefile.PE(path, fast_load=True) |
| 338 | + except: |
| 339 | + return None, None, None |
336 | 340 |
|
337 | | - pe = pefile.PE(path, fast_load=True) |
338 | 341 | ostype = None |
339 | 342 | arch = None |
340 | 343 | archendian = None |
@@ -364,43 +367,30 @@ def ql_pe_parse_emu_env(path): |
364 | 367 | return arch, ostype, archendian |
365 | 368 |
|
366 | 369 | def ql_guess_emu_env(path): |
367 | | - if os.path.isdir(path) and (str(path)).endswith(".kext"): |
368 | | - return QL_ARCH.X8664, QL_OS.MACOS, QL_ENDIAN.EL |
369 | | - |
370 | 370 | arch = None |
371 | 371 | ostype = None |
372 | 372 | archendian = None |
373 | 373 |
|
374 | | - ftype = magic.from_file(path) |
| 374 | + if os.path.isdir(path) and (str(path)).endswith(".kext"): |
| 375 | + return QL_ARCH.X8664, QL_OS.MACOS, QL_ENDIAN.EL |
| 376 | + |
| 377 | + if os.path.isfile(path) and (str(path)).endswith(".DOS_COM"): |
| 378 | + return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL |
375 | 379 |
|
376 | | - if "ELF" in ftype: |
377 | | - arch, ostype, archendian = ql_elf_parse_emu_env(path) |
378 | | - elif "Mach-O" in ftype: |
| 380 | + if os.path.isfile(path) and (str(path)).endswith(".DOS_MBR"): |
| 381 | + return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL |
| 382 | + |
| 383 | + if os.path.isfile(path) and (str(path)).endswith(".DOS_EXE"): |
| 384 | + return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL |
| 385 | + |
| 386 | + arch, ostype, archendian = ql_elf_parse_emu_env(path) |
| 387 | + |
| 388 | + if arch == None or ostype == None or archendian == None: |
379 | 389 | arch, ostype, archendian = ql_macho_parse_emu_env(path) |
380 | | - elif "PE32" in ftype: |
381 | | - arch, ostype, archendian = ql_pe_parse_emu_env(path) |
382 | | - elif ("COM" in ftype and "DOS" in ftype) or "COM" in path: |
383 | | - arch = QL_ARCH.A8086 |
384 | | - ostype = QL_OS.DOS |
385 | | - archendian = QL_ENDIAN.EL |
386 | | - elif "MBR" in ftype and "DOS" in ftype: # Yes, we put MBR into dos.py. |
387 | | - arch = QL_ARCH.A8086 |
388 | | - ostype = QL_OS.DOS |
389 | | - archendian = QL_ENDIAN.EL |
390 | | - elif "MS-DOS" in ftype: |
391 | | - # Here we have to distinguish between real 16bit DOS executables and EFI excutables. |
392 | | - # I could confirm from specs that all UEFI executables should be PE/PE32+. |
393 | | - # But 16bit DOS executables don't have a valid NT header. |
394 | | - # I'm not sure why libmagic(file) classify EFI executables as "MS-DOS executable" |
395 | | - try: |
396 | | - pefile.PE(path) |
397 | | - except pefile.PEFormatError: |
398 | | - arch = QL_ARCH.A8086 |
399 | | - ostype = QL_OS.DOS |
400 | | - archendian = QL_ENDIAN.EL |
401 | | - else: |
402 | | - arch, ostype, archendian = ql_pe_parse_emu_env(path) |
403 | 390 |
|
| 391 | + if arch == None or ostype == None or archendian == None: |
| 392 | + arch, ostype, archendian = ql_pe_parse_emu_env(path) |
| 393 | + |
404 | 394 | if ostype not in (QL_OS_ALL): |
405 | 395 | raise QlErrorOsType("[!] File does not belong to either 'linux', 'windows', 'freebsd', 'macos', 'ios', 'dos'") |
406 | 396 |
|
|
0 commit comments