0

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');
2
  • I don't know if this can help you, but I can do the following using the shell builtin printf: printf '\x02\x03\x04' | awk '{print length($0)}' -> 3 Commented Nov 23, 2014 at 15:07
  • Tried that and got the same behaviour Commented Nov 23, 2014 at 15:40

1 Answer 1

1

What do you expect? The stdin of your input_parse.py is the stdout of the writing script. As soon as you've read all your binary data, the stdin is finished. So calling a shell afterwards detects a closed stdin and terminates.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the insight. Do you have a suggestion how to make it work? I've tried process.wait(), but it does not help
You want the shell's stdin to be the terminal instead of the pipe? Try p = subprocess.Popen(["sh"], stdin=open('/dev/tty')); p.wait()
I need to send the binary string to the script first.
My suggestion doesn't affect the incoming data on the pipe and raw_input statement. It replaces the subsequent system("sh") with a Popen instance in which stdin is redirected back to the terminal. But based on your last update I don't understand why you need to run sh. Surely it can't be just to execute cat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.