Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
0x79H opened this issue Jun 21, 2023 · 5 comments

Comments

@0x79H
Copy link

0x79H commented Jun 21, 2023

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: 1706049
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 #475 (comment), 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
Copy link
Author

0x79H commented Jun 21, 2023

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

sys.stdin.fileno()

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
Copy link
Member

elicn commented Jun 28, 2023

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
Copy link

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

sys.stdin.fileno()

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
Copy link
Author

0x79H commented Dec 8, 2023

did you solved the problem?

two way in my comment #1362 (comment)

way1: use 1.4.5

pip install qiling==1.4.5

way2: case AttributeError

if ur qiling version > b3de208

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 (b3de208) 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
Copy link
Member

elicn commented Jul 25, 2024

Covered by #1482

@elicn elicn closed this as completed Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants