qilingframework / qiling

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

AttributeError when setup in qilingida.py, because sys.stdin is None at idapython in windows. #1362

Closed 0x79H closed 3 months ago

0x79H commented 1 year ago

error AttributeError: 'NoneType' object has no attribute 'fileno', when call sys.stdin.fileno() at setup in qilingida.py

ida 7.7πŸ˜‰ windows 11 python 3.10.5 qiling version: 1706049d10e955740bff29226bd98d334f902301 target is arm elf file. ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV)

Traceback (most recent call last):
  File "C:/Users/root/AppData/Roaming/Hex-Rays/IDA Pro/plugins/qilingida.py", line 810, in activate
    self.action_handler.ql_handle_menu_action(self.action_type)
  File "C:/Users/root/AppData/Roaming/Hex-Rays/IDA Pro/plugins/qilingida.py", line 2098, in ql_handle_menu_action
    [x.handler() for x in self.menuitems if x.action == action]
  File "C:/Users/root/AppData/Roaming/Hex-Rays/IDA Pro/plugins/qilingida.py", line 2098, in <listcomp>
    [x.handler() for x in self.menuitems if x.action == action]
  File "C:/Users/root/AppData/Roaming/Hex-Rays/IDA Pro/plugins/qilingida.py", line 1037, in ql_start
    self.qlemu.start()
  File "C:/Users/root/AppData/Roaming/Hex-Rays/IDA Pro/plugins/qilingida.py", line 889, in start
    self.ql = Qiling(argv=self.path, rootfs=self.rootfs, verbose=QL_VERBOSE.DEBUG, env=self.env, log_plain=True, *args, **kwargs)
  File "C:\Python\Python3-10-5\lib\site-packages\qiling\core.py", line 189, in __init__
    self._os = select_os(ostype)(self)
  File "C:\Python\Python3-10-5\lib\site-packages\qiling\os\linux\linux.py", line 30, in __init__
    super(QlOsLinux, self).__init__(ql)
  File "C:\Python\Python3-10-5\lib\site-packages\qiling\os\posix\posix.py", line 142, in __init__
    super().__init__(ql)
  File "C:\Python\Python3-10-5\lib\site-packages\qiling\os\os.py", line 63, in __init__
    sys.stdin.fileno()
AttributeError: 'NoneType' object has no attribute 'fileno'

i check sys.stdin is None and i know $IDAdir/python/3/init.py set stdout and stderr but not set stdin

try to fix this and find https://github.com/qilingframework/qiling/issues/475#issuecomment-692589558, same problem but not helpful.

no idea why sys.stdin is None in idapython, and no idea how to fix this.

any idea? thanks.

0x79H commented 1 year ago

i checkout last release tag:1.4.5 , and sys.stdin is None too. but ida plugin work well.

maybe bug after this commit https://github.com/qilingframework/qiling/commit/b3de208edb3888ee8a45462fa0a9df2bc9302f90 so maybe we should case exception: AttributeError there https://github.com/qilingframework/qiling/blob/1706049d10e955740bff29226bd98d334f902301/qiling/os/os.py#L63

let me just use 1.4.5 now πŸ˜‰

and i still don't know why stdin is None, maybe it is None in linux version too? maybe stdin is None can crash something, any info?

elicn commented 1 year ago

I wasn't familiar with the fact that IDA does not define stdin. We should consider the implications of that and think where redirect stdin from in case the emulated program uses it.

As a short term workaround, however, you could set sys.stdin to be a dummy pipe.InteractiveInStream before Qiling is initialized.

hohohoho123 commented 11 months ago

i checkout last release tag:1.4.5 , and sys.stdin is None too. but ida plugin work well.

maybe bug after this commit b3de208 so maybe we should case exception: AttributeError there

https://github.com/qilingframework/qiling/blob/1706049d10e955740bff29226bd98d334f902301/qiling/os/os.py#L63

let me just use 1.4.5 now πŸ˜‰

and i still don't know why stdin is None, maybe it is None in linux version too? maybe stdin is None can crash something, any info?

did you solved the problem?

0x79H commented 11 months ago

did you solved the problem?

two way in my comment https://github.com/qilingframework/qiling/issues/1362#issuecomment-1600375342

way1: use 1.4.5

pip install qiling==1.4.5

way2: case AttributeError

if ur qiling version > https://github.com/qilingframework/qiling/commit/b3de208edb3888ee8a45462fa0a9df2bc9302f90

diff --git "a/C:\\Python\\Python3-10-5\\lib\\site-packages\\qiling\\os\\os.p_" "b/C:\\Python\\Python3-10-5\\lib\\site-packages\\qiling\\os\\os.py"
index 6d759bf..6018310 100755
--- "a/C:\\Python\\Python3-10-5\\lib\\site-packages\\qiling\\os\\os.p_"
+++ "b/C:\\Python\\Python3-10-5\\lib\\site-packages\\qiling\\os\\os.py"
@@ -61,7 +61,7 @@ class QlOs:
             # such as fileno(). here we use this to determine how we are going to use
             # the environment standard streams
             sys.stdin.fileno()
-        except UnsupportedOperation:
+        except (UnsupportedOperation, AttributeError):
             # Qiling is used on an interactive shell or embedded python interpreter.
             # if the internal stream buffer is accessible, we should use it
             self._stdin  = getattr(sys.stdin,  'buffer', sys.stdin)

i think this is not the best way to fix, because AttributeError is not ideal. If we are in IDAPython, we need to set them using this code.

            self._stdin  = getattr(sys.stdin,  'buffer', sys.stdin)
            self._stdout = getattr(sys.stdout, 'buffer', sys.stdout)
            self._stderr = getattr(sys.stderr, 'buffer', sys.stderr)

Because qiling set them like this before 1.4.6, but this commit (https://github.com/qilingframework/qiling/commit/b3de208edb3888ee8a45462fa0a9df2bc9302f90) disrupts the original logic. And has been committed to the release version of 1.4.6 .

I haven't closed this issue because it remains unresolved, and I'm uncertain about any potential unintended consequences of my modifications. I only tested it on Windows with IDA 7.7, so I just opened an issue without submitting a pull request at that time.

Now, I believe it's better to use assert to check if sys.stdin is None in order to confirm Qiling in IDAPython, triggering AssertionError instead of AttributeError. I believe this deliberate use of None (not use assert sys.stdin) helps avoid potential issues, just as in the new pull request.

elicn commented 3 months ago

Covered by #1482