I'm using netcat to create a backdoor running a python script with the following command:

netcat -l -p 1234 -e 'python /script.py'

then I'm connecting to the backdoor with another shell using:

netcat localhost 1234

script.py is a simple loop that reads input, saves it to a file, and then prints it back. Now whatever I write in the second shell goes to the script and is successfully saved to a file. However, I don't see the output of the script on the second shell. I tried both the print and sys.stdout.write methods of python and both seem to fail. I don't know why the output is relayed back to the second shell if I use this:

netcat localhost 1234 /bin/bash

But not with my script. I'm obviously missing something important. Here is my script:

import sys

while 1:
    kbInput = sys.stdin.readline()
    sys.stdout.write( 'Input: '+kbInput)
    f  = open("output.txt", "w")

Your writes to stdout are being buffered by python, and only written when the buffer is full. There are 2 simple fixes:

  1. Add the -u option to your python command to ask for unbuffered output ('python -u /script.py').

  2. Alternatively, flush the output after each write. In your example, after the line sys.stdout.write( 'Input: '+kbInput) add the line:

  • Omg finally. At some point I assumed it was something like that but I used close() which ended up causing an error. Thanks a bunch! – Mazen Mar 17 '17 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.