我有一个python脚本读取使用raw_input
,然后打开使用system("sh")
壳,e.gPython:raw_input将东西留在输入缓冲区中?
r_input = raw_input('Enter the key\n')
system("sh")
我需要养活这个脚本一些二进制数据,这一点我使用打印或sys.stdout.write
,例如
python -c "import sys; sys.stdout.write('\x02\x03\x04')" | python ./input_parse.py
的问题是,尽管输入完全读取,外壳立即退出,因为如果它得到来自stdin
一些富余。当我从键盘喂脚本,它的工作原理,但我不能把二进制数据在那里,它被解释为字符串...
我试过subprocess
,像这样的(但没有太大的区别):
python -c "import subprocess; process = subprocess.Popen(['python', 'input_parse.py], shell=False, stdin=subprocess.PIPE);process.stdin.write('hi')" | python ./py_deobf_input_parse.py
我该如何得到这个工作?
更新:我发现了一个解决方案,可能不理想,但工作。基本上我使用了相同的subprocess
模型,但必须在我的输入结尾添加\n
。然后我做了另一个process.stdin.write
将命令传递给打开的shell。我不得不在他们之间入睡,否则它会立即退出。如果有人找到更好的答案,我仍然会给予奖励。
python -c "import subprocess; import time; process = subprocess.Popen(['./py_input_parse'], stdin=subprocess.PIPE); process.stdin.write('\x02\x02\x02\x02\n'); process.stdin.flush(); time.sleep(2); process.stdin.write('cat ./flag.txt \n');
我不知道这是否可以帮助你,但我可以使用shell内置'printf'来执行以下操作:'printf'\ x02 \ x03 \ x04'| awk'{print length($ 0)}'' - >'3' – gboffi 2014-11-23 15:07:18
试过并得到相同的行为 – 2014-11-23 15:40:58