在发送STDIN输入之前等待子过程的提示
我有一个Linux X86二进制文件,该二进制要求密码,并将打印出密码是正确还是不正确的。我想使用python来模糊输入。
下面是我运行二进制文件的屏幕截图,然后给它给字符串“ asdf”,然后接收字符串“错误”
屏幕截图:
到目前为止,我试图使用python3 subprocess模块来
- 运行二进制当子过程
- 接收到密码的提示
- 时,请发送字符串。
- 在此处接收响应
是我的脚本
p = subprocess.Popen("/home/pj/Desktop/L1/lab1",stdin=subprocess.PIPE, stdout=subprocess.PIPE)
print (p.communicate()[0])
运行此脚本的结果是,
b'Please supply the code: \nIncorrect\n'
我希望只收到提示,但是在我有机会发送输入之前,二进制文件也返回不正确的响应。
如何改进脚本以成功与此二进制互动?
I have a linux x86 binary that asks for a password and will print out whether the password is correct or incorrect. I would like to use python to fuzz the input.
Below is a screenshot of me running the binary, then giving it the string "asdf", and receiving the string "incorrect"
Screenshot:
So far, I have tried to use the Python3 subprocess module in order to
- run the binary as a subprocess
- receive the prompt for a password
- send a string.
- receive the response
Here is my script
p = subprocess.Popen("/home/pj/Desktop/L1/lab1",stdin=subprocess.PIPE, stdout=subprocess.PIPE)
print (p.communicate()[0])
the result of running this script is
b'Please supply the code: \nIncorrect\n'
I am expecting to receive only the prompt however the binary is returning a response of incorrect as well before I have gotten the chance to send my input.
How can I improve my script in order to interact with this binary successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读文档
因此,您什么也没发送到该过程,然后读取
stdout
的全部。在您的情况下,您实际上不需要等待提示即可将数据发送到该过程,因为流不同步地工作:仅在尝试读取其
stdin
>:如果您的输入时,该过程才能获得您的输入:如果您坚持出于何种原因等待提示(例如,您的流程也会在提示之前检查输入,期待其他内容),您需要手动阅读
stdout
,请非常小心您阅读了多少:因为Python的file.read
is阻止,一个简单的read()
会死去,因为它等待EOF,并且子过程不会关闭stdout
- 因此不会产生EOF-来自你。如果输入或输出长度可能会超过STDIO的缓冲长度(在您的特定情况下不太可能),则您还需要在单独的线程中进行stdout读取和stdin写作。这是一个示例,使用
pexpect
这为您服务(我是我使用pexpect.fdexpect
代替pexpect.spawn
在文档中提出的建议'因为它在所有平台上都可以使用):Read the documentation carefully (emphasis mine):
So, you're sending nothing to the process, and reading all of
stdout
at once.In your case, you don't really need to wait for the prompt to send data to the process because streams work asynchronously: the process will get your input only when it tries to read its
STDIN
:If you insist on waiting for the prompt for whatever reason (e.g. your process checks the input before the prompt, too, expecting something else), you need to read
STDOUT
manually and be VERY careful how much you read: since Python'sfile.read
is blocking, a simpleread()
will deadlock because it waits for EOF and the subprocess doesn't closeSTDOUT
-- thus doesn't produce EOF -- until it get input from you. If the input or output length is likely to go over stdio's buffer length (unlikely in your specific case), you also need to do stdout reading and stdin writing in separate threads.Here's an example using
pexpect
that takes care of that for you (I'm usingpexpect.fdexpect
instead ofpexpect.spawn
suggested in the doc 'cuz it works on all platforms):