1

I'm executing the following subprocess...

p.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"])

...and it hangs.

But if I execute kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget then it executes fine. Is there something wrong with using the input or piping operator?

I also tried sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)

The entire code looks like this UPDATED WITH SUGGESTIONS

import subprocess as sp
import pdb
for i in range(4201265,4201323):
 pdb.set_trace()
 d = hex(i)[2:]
 output = " "
 for i in range(len(d),0,-2):
 output = output + d[i-2:i] + " "
 out_buffer = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" + output + "00 00 00 00"
 text_file = open("exploit4.txt", "w")
 text_file.write("%s" % out_buffer)
 # sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
 with open("exploit4.txt") as inhandle:
 p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
 p2 = sp.Popen("./rtarget",stdin=p.stdout,stdout=sp.PIPE)
 [output,error] = p2.communicate()

I'm getting an error is

 File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
 errread, errwrite)
 File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
 raise child_exception
OSError: [Errno 8] Exec format error

After debugging it occurs at the fire subprocess call p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)

asked Oct 18, 2016 at 19:31
2
  • Don't forget to close the file after you write it. Commented Oct 18, 2016 at 19:48
  • 2
    Also, as a general rule, don't update the code in your question with the best answer, or your question will become meaningless. Commented Oct 18, 2016 at 19:49

1 Answer 1

2

Since you're using redirection and piping, you have to enable shell=True

sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"],shell=True)

but it would be much cleaner to use Popen on both executables and feeding the contents of exploit4.txt as input. Example below, adapted to your case:

import subprocess
 with open("exploit4.txt") as inhandle:
 p = subprocess.Popen("./hex2raw",stdin=inhandle,stdout=subprocess.PIPE)
 p2 = subprocess.Popen("./rtarget",stdin=p.stdout,stdout=subprocess.PIPE)
 [output,error] = p2.communicate()
 print(output)
 # checking return codes is also a good idea
 rc2 = p2.wait()
 rc = p.wait()

Explanation:

  1. open the input file, get its handle inhandle
  2. open the first subprocess, redirecting stdin with inhandle, and stdout to an output stream. Get the pipe handle (p)
  3. open the second subprocess, redirecting stdin with previous process stdout, and stdout to an output stream
  4. let the second process communicate. It will "pull" the first one by consuming its output: both processes work in a pipe fashion
  5. get return codes and print the result

Note: you get "format error" because one or both executables are actually shell or other non-native executables. In that case, just add the shell=True option to the relevant Popen calls.

answered Oct 18, 2016 at 19:34
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this, still no luck. What would be the Popen syntax?
I was brewing an example using 2 grep commands, and I have adapted it to your case.
semantics look great, but something is still wrong -- check the post (updated with new code and error)
oh, those must be shell scripts. Try adding shell=True for the ones executables being shell scripts (if you don't know, try to open them in a text editor), this time it will work better :)
It works well - thanks. I would just argue rc2 = p2.wait() and rc = p.wait() are a little confusing (since you do not wait any more, and just get the status code), I think it's a little bad for readability.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.