I have a python script that reads using raw_input and then opens a shell using system("sh"), e.g
r_input = raw_input('Enter the key\n')
system("sh")
I need to feed this script some binary data, which I do using print or sys.stdout.write, e.g.
python -c "import sys; sys.stdout.write('\x02\x03\x04')" | python ./input_parse.py
The problem is that despite that the input is read entirely, the shell exits immediately, as if it gets some leftover from the stdin. When I feed the script from the keyboard, it works, but I can't put binary data there, it is interpreted as string...
I tried subprocess, like this (but not much difference):
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
How can I get this to work?
Update: I have found a solution, maybe not ideal, but worked. Basically I used the same subprocess model, but had to add the \n to the end of my input. Then I did another process.stdin.write to pass the command to the opened shell. I had to add asleep between them, otherwise it was exiting immediately. I still will give a credit if someone finds a nicer answer.
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');
printf:printf '\x02\x03\x04' | awk '{print length($0)}'->3