qilingframework / qiling

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

Fix for running Qiling inside of IDAPython interpreter. #1482

Closed xorhex closed 1 month ago

xorhex commented 1 month ago

Fix for #1426 / #1417 / #1362. Hopefully this fix works for more than just me :crossed_fingers:

Checklist

Which kind of PR do you create?

Coding convention?

Extra tests?

Changelog?

Target branch?

One last thing


elicn commented 1 month ago

Code looks a bit cumbersome to be. Why not just use the following instead?

try:
    # ...
    sys.stdin.fileno()
    sys.stdout.fileno()
    sys.stderr.fileno()
except UnsupportedOperation:
    ...

Attempting to access the methods will raise the UnsupportedOperation exceotion without explicitly checking and raising. This is actually how the original code works.

xorhex commented 1 month ago

Added error catching for AttributeError as UnsupportedOperation does not catch this error.

Without AttributeError being handled:

  File "/home/user/.local/lib/python3.10/site-packages/qiling/os/os.py", line 65, in __init__
    sys.stdout.fileno()
  File "/home/user/.local/lib/python3.10/site-packages/colorama/ansitowin32.py", line 29, in __getattr__
    return getattr(self.__wrapped, name)
AttributeError: 'IDAPythonStdOut' object has no attribute 'fileno'

Tested locally and this fix also works for my instance of IDA.

elicn commented 1 month ago

Thanks for the update. Can you also test that in a regular Python interpreter? (e.g. IDLE)

xorhex commented 1 month ago

Sorry for the delay - testing in Linux terminal window:

❯ python3 emu.py 
[=]     Initiate stack address at 0xfffdd000 
[=]     Loading /home/user/rootfs/x86_windows/test.exe to 0x400000
[=]     PE entry point at 0x45e990
[=]     TEB is at 0x6000
[=]     PEB is at 0x61b0
[=]     LDR is at 0x6630
[=]     Loading ntdll.dll ...
[=]     Done loading ntdll.dll
[=]     Loading kernel32.dll ...
[=]     Loading kernelbase.dll ...
[=]     Done loading kernelbase.dll
[=]     Done loading kernel32.dll
[=]     Loading mscoree.dll ...
[=]     Calling mscoree.dll DllMain at 0x10200a30
[=]     GetSystemTimeAsFileTime(lpSystemTimeAsFileTime = 0xffffcfd8)
[=]     GetCurrentProcessId() = 0x7cc
[=]     GetCurrentThreadId() = 0x0
[=]     GetTickCount() = 0x30d40
[=]     QueryPerformanceCounter(lpPerformanceCount = 0xffffcfd0) = 0x0
[=]     GetVersionExA(lpVersionInformation = "\x94") = 0x1
[=]     HeapCreate(flOptions = 0, dwInitialSize = 0x1000, dwMaximumSize = 0) = 0x50006d0
[=]     GetModuleHandleW(lpModuleName = "KERNELBASE.DLL") = 0x10000000
[=]     GetProcAddress(hModule = 0x10000000, lpProcName = "FlsAlloc") = 0x100fbea0
[=]     GetProcAddress(hModule = 0x10000000, lpProcName = "FlsGetValue") = 0x100e70c0

Seems to print to the console just fine.

xorhex commented 1 month ago

Anything else needed from me? Thanks!