fancyStuff90
fancyStuff90

Reputation: 29

Python, how to console input external programms within my python programm?

i am studying computer science and in my newest module "IT- Security" i have a task to crack a private key from DSA with given values (which are given from a docker imagecontainer i loaded on my pc).

I do the following to get the values i need:

  1. netcat 127.0.0.1 4444
  2. The Docker Container asks me about the Task- ID, i have to type input: 5A
  3. The Docker Container asks me about my Student-ID, i have to type in: 12x4x32
  4. The Docker Container wants another input...

So - i managed to get the netcat command done with the subprocess module. The Command Line asks me for the input for No. 2). But after that im stucking. i cant handle the command to give the values 5A, Student ID and so on and i dont know why it is like that. I have to write a programm for that because i have to make a lot of requests to send different inputs from 4) and up to get different values from the Docker Image.

I tried to search for it on google and here, but i only found tutorials or examples where one call was made like "ls" or "dir" or something like that. I tried to make a subprocess.PIPE but all i get there is a traceback.

import subprocess
subprocess.call("ncat 127.0.0.1 4444")
subprocess.call("5A") #doesnt input, stucks here

These 3 lines of code shows my scenario. The ncat call is done and now i have to enter the value 5A but i dont know how.

Can somebody help me? Sorry i am new to python - dont know if there is a better module or something like that

Upvotes: 1

Views: 202

Answers (1)

Pi Marillion
Pi Marillion

Reputation: 4674

There are a couple issues

  1. The subprocess.call function needs to have the program ncat and its arguments 127.0.0.1 and 4444 split into a list like this:

    import subprocess
    subprocess.call(['ncat', '127.0.0.1', '4444'])
    
    • Any time you call something in bash or another shell, this splitting into a list (or array) is happening under the hood
    • Python's subprocess module wants you to do it explicitly instead of implicitly
    • You can use the shell=True keyword instead, but don't.
  2. As written, the ncat program will take over your terminal and expect inputs from you (the user) rather than the Python program until it's finished, at which point control returns to Python

This second point can be very easy or very hard to deal with.

If the container asks each question as a single line, then you could do something like this using subprocess.Popen:

from subprocess import Popen, PIPE

# start `ncat` to talk to the container
cmd = ['ncat', '127.0.0.1', '4444']
proc = Popen(cmd, encoding='utf8', stdin=PIPE, stdout=PIPE)

# read Task ID question from the container
q = proc.stdout.readline()
if 'task' not in q.lower():
    sys.exit('container was supposed to ask about task ID')

# send response to Task ID question
proc.stdin.write('5A\n')  # newline is like pressing enter


# read Task ID question from the container
q = proc.stdout.readline()
if 'student' not in q.lower():
    sys.exit('container was supposed to ask about student ID')

# send response to Student ID question
proc.stdin.write('12x4x32\n')  # newline is like pressing enter

...

# eventually, read the remaining output from the container
output_string = proc.stdout.read()

# and make sure that `ncat` finishes
proc.wait()

Upvotes: 2

Related Questions