0

I am trying to write a python script to test a 4 digit pincode with brute forcing(and a password) when connecting to local host. The command that needs to be run is:

echo password pincode | nc localhost 30002 >> /tmp/joesPin/pinNumber

(writes response to a new file).

This worked when written as a bash script, but I am struggling with the subprocess module in Python.

import subprocess

password = "UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"

for i in range(10000):

    pincode = str('{0:04}'.format(i)) #changes 4 to 0004
    subprocess.call('echo', password, pincode,'|','nc localhost 30002 >> /tmp/joesPin/' + pincode,shell=True)

I want it to call:

echo UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ 0001 | nc localhost 30002 >> /tmp/joesPin/0001
idjaw
  • 25,487
  • 7
  • 64
  • 83
Joe M.
  • 1

1 Answers1

0

There are different ways to pipe the output of a command in Python.

Option 1: You can set the stdout argument of the subprocess.call command and write the output somewhere.

Option 2: You can use subprocess.PIPE in the Popencall and save the output to use with another command.

proc = subprocess.Popen(['echo', password, pincode], stdout=subprocess.PIPE)
output = proc.communicate()[0] # now contains the output of running "proc"

file = '/tmp/joesPin/pinNumber'
with open(file, 'a+') as out:
    subprocess.call(['nc localhost 30002'], stdout=out, shell=True)

Setting the stdout field in subprocess.call writes the output of the subprocess to the file descriptor given in stdout.

To use the output of the 1st process as the stdin input of the 2nd process:

proc = subprocess.Popen(['echo', password, pincode], stdout=subprocess.PIPE)
output = proc.communicate()[0] # now contains the output of running "proc"

file = '/tmp/joesPin/pinNumber'
proc2 = subprocess.Popen(['nc localhost 30002'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
proc2.stdin.write(output)
result = proc2.communicate()[0]

# now you can write the output to the file:
with open (file, 'a+') as outfile:
    outfile.write(result)
xgord
  • 4,606
  • 6
  • 30
  • 51