qilingframework / qiling

A True Instrumentable Binary Emulation Framework
https://qiling.io
GNU General Public License v2.0
5.14k stars 744 forks source link

DOS disks should not use fs_mapper API #1375

Open VinceQu opened 1 year ago

VinceQu commented 1 year ago

*Describe the bug The 8086 DOS/MBR example in the qiling/examples folder is not running correctly.

Fresh install with pip. Ubuntu 22.04 in WSL.

Sample Code

qiling/examples> python3 doogie_8086_crack.py

Traceback (most recent call last):
  File "/qiling/examples/doogie_8086_crack.py", line 204, in <module>
    ql = first_stage()
  File "/qiling/examples/doogie_8086_crack.py", line 193, in first_stage
    ql = Qiling(["rootfs/8086/doogie/doogie.DOS_MBR"], "rootfs/8086", console=False)
  File "/home/user/.local/lib/python3.10/site-packages/qiling/core.py", line 190, in __init__
    self.loader.run()
  File "/home/user/.local/lib/python3.10/site-packages/qiling/loader/dos.py", line 86, in run
    if not self.ql.os.fs_mapper.has_mapping(0x80):
  File "/home/user/.local/lib/python3.10/site-packages/qiling/os/mapper.py", line 85, in has_mapping
    return vpath in self
  File "/home/user/.local/lib/python3.10/site-packages/qiling/os/mapper.py", line 72, in __contains__
    absvpath = self.path.virtual_abspath(vpath)
  File "/home/user/.local/lib/python3.10/site-packages/qiling/os/path.py", line 305, in virtual_abspath
    absvpath = self.__virtual_abspath(virtpath)
  File "/home/user/.local/lib/python3.10/site-packages/qiling/os/path.py", line 99, in __virtual_abspath
    vpath = self.PureVirtualPath(virtpath)
  File "/usr/lib/python3.10/pathlib.py", line 562, in __new__
    return cls._from_parts(args)
  File "/usr/lib/python3.10/pathlib.py", line 594, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/usr/lib/python3.10/pathlib.py", line 578, in _parse_args
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not in

Expected behavior The example should run.

Additional context Lines 86 and 87 of qiling/loader/dos.py

            if not self.ql.os.fs_mapper.has_mapping(0x80):
                self.ql.os.fs_mapper.add_mapping(0x80, QlDisk(path, 0x80))

It seems like the "has_mapping()" function expects a "str" not an "int" as os.fspath() is later called with the passed value as an argument. Sadly i am not very familiar with the internal structure of qiling. I am guessing that simply turning the 0x80 into a string ("0x80") might lead to other problems further down the line.

elicn commented 1 year ago

Indeed, that is an expected side effect of #1336, and should be fixed. Implementing DOS disks using fs_mapper is not quite intuitive since its API doesn't make much sense in that context. Until we figure this out, you may use the workaround you mentioned: turning the disk index (usually 0x80) into a string. As long as it is consistent, it should work just fine.

I am changing the issue subject to reflect that.

limbernie commented 1 month ago

I encountered this issue too. Had to check out old_dev branch for the DOS examples to work.