Skip to content

Commit 9ade532

Browse files
committed
remove python-magic
1 parent e871122 commit 9ade532

File tree

12 files changed

+56
-46
lines changed

12 files changed

+56
-46
lines changed

examples/doogie_8086_crack.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ def show_once(ql: Qiling, key):
129129
# In this stage, we show every key.
130130
def third_stage(keys):
131131
# To setup terminal again, we have to restart the whole program.
132-
ql = Qiling(["rootfs/8086/doogie/doogie.bin"],
132+
ql = Qiling(["rootfs/8086/doogie/doogie.DOS_MBR"],
133133
"rootfs/8086",
134134
console=False,
135135
log_dir=".")
136-
ql.add_fs_mapper(0x80, QlDisk("rootfs/8086/doogie/doogie.bin", 0x80))
136+
ql.add_fs_mapper(0x80, QlDisk("rootfs/8086/doogie/doogie.DOS_MBR", 0x80))
137137
ql.set_api((0x1a, 4), set_required_datetime, QL_INTERCEPT.EXIT)
138138
hk = ql.hook_code(stop, begin=0x8018, end=0x8018)
139139
ql.run()
@@ -183,11 +183,11 @@ def stop(ql, addr, data):
183183

184184
# In this stage, we get the encrypted data which xored with the specific date.
185185
def first_stage():
186-
ql = Qiling(["rootfs/8086/doogie/doogie.bin"],
186+
ql = Qiling(["rootfs/8086/doogie/doogie.DOS_MBR"],
187187
"rootfs/8086",
188188
console=False,
189189
log_dir=".")
190-
ql.add_fs_mapper(0x80, QlDisk("rootfs/8086/doogie/doogie.bin", 0x80))
190+
ql.add_fs_mapper(0x80, QlDisk("rootfs/8086/doogie/doogie.DOS_MBR", 0x80))
191191
# Doogie suggests that the datetime should be 1990-02-06.
192192
ql.set_api((0x1a, 4), set_required_datetime, QL_INTERCEPT.EXIT)
193193
# A workaround to stop the program.

examples/petya_8086_crack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def input_key(ql, addr, data):
4343
curses.ungetch(ord("\n"))
4444
curses.ungetch(ord("\r"))
4545

46-
ql = Qiling(["rootfs/8086/petya/mbr.bin"],
46+
ql = Qiling(["rootfs/8086/petya/petya.DOS_MBR"],
4747
"rootfs/8086",
4848
console=False,
4949
output="debug",
@@ -87,7 +87,7 @@ def second_stage(ql: Qiling):
8787

8888
# In this stage, we have to wait for petya being load to the right place.
8989
def first_stage():
90-
ql = Qiling(["rootfs/8086/petya/mbr.bin"],
90+
ql = Qiling(["rootfs/8086/petya/petya.DOS_MBR"],
9191
"rootfs/8086",
9292
console=False,
9393
output="debug",
File renamed without changes.
15.3 KB
Binary file not shown.

qiling/utils.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
This module is intended for general purpose functions that can be used
88
thoughout the qiling framework
99
"""
10-
import importlib, logging, os, logging, copy, re, pefile, magic, configparser
10+
import importlib, logging, os, logging, copy, re, pefile, configparser
1111
from logging import LogRecord
1212
from pathlib import Path
1313
from .exception import *
@@ -333,8 +333,11 @@ def getident():
333333
return arch, ostype, archendian
334334

335335
def ql_pe_parse_emu_env(path):
336+
try:
337+
pe = pefile.PE(path, fast_load=True)
338+
except:
339+
return None, None, None
336340

337-
pe = pefile.PE(path, fast_load=True)
338341
ostype = None
339342
arch = None
340343
archendian = None
@@ -364,43 +367,30 @@ def ql_pe_parse_emu_env(path):
364367
return arch, ostype, archendian
365368

366369
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-
370370
arch = None
371371
ostype = None
372372
archendian = None
373373

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
375379

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:
379389
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)
403390

391+
if arch == None or ostype == None or archendian == None:
392+
arch, ostype, archendian = ql_pe_parse_emu_env(path)
393+
404394
if ostype not in (QL_OS_ALL):
405395
raise QlErrorOsType("[!] File does not belong to either 'linux', 'windows', 'freebsd', 'macos', 'ios', 'dos'")
406396

setup.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
with open("README.md", "r", encoding="utf-8") as ld:
2626
long_description = ld.read()
2727

28-
if sys.platform in ('linux', 'cygwin'):
29-
requirements += ["python-magic>=0.4.16"]
30-
else:
31-
requirements += ["python-magic-bin>=0.4.14"]
32-
3328
if "win32" in sys.platform:
3429
requirements += ["windows-curses>=2.1.0"]
3530

tests/ARKA.EXE

15.3 KB
Binary file not shown.

tests/test_dos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class DOSTest(unittest.TestCase):
2020

2121
def test_dos_8086_hello(self):
22-
ql = Qiling(["../examples/rootfs/8086/dos/HI.COM"], "../examples/rootfs/8086/dos")
22+
ql = Qiling(["../examples/rootfs/8086/dos/HI.DOS_COM"], "../examples/rootfs/8086/dos")
2323
ql.run()
2424
del ql
2525

0 commit comments

Comments
 (0)