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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
| #include <ntddk.h>
VOID DriverUnload(PDRIVER_OBJECT driver) { DbgPrint("停止运行了\n"); }
ULONG getProcessInLink(char* name) { ULONG curProcess; __asm { mov eax, dword ptr fs : [0x124] ; mov ecx, [eax + 0x44]; mov curProcess, ecx; } PLIST_ENTRY plistProcess = (PLIST_ENTRY)(curProcess + 0x88); while (plistProcess->Flink != (PLIST_ENTRY)(curProcess + 0x88)) { ULONG nextProcess = ((ULONG)(plistProcess)) - 0x88; plistProcess = plistProcess->Flink; if (strncmp(name, (PCHAR)(nextProcess + 0x174),strlen(name)>15?15: strlen(name)) == 0) { return nextProcess; } } return NULL; }
PUNICODE_STRING getAllNameByEprocessAddr(ULONG eprocessAddr) { return *(PULONG)(eprocessAddr + 0x1F4); }
UNICODE_STRING processStr = RTL_CONSTANT_STRING(L"Process");; UNICODE_STRING threadStr = RTL_CONSTANT_STRING(L"Thread");; void analyseLocalHandleDescAddr(ULONG handleDescAddr) { ULONG objectAddr = *(PULONG)(handleDescAddr); objectAddr &= 0xFFFFFFF8; if (objectAddr != NULL) { PUNICODE_STRING pTypeName = (*(PULONG)(objectAddr + 0x8)) + 0x040; if (RtlCompareUnicodeString(pTypeName, &processStr, FALSE) == 0) { KdPrint(("进程内核对象地址为%p,进程ID:%d,全路径进程名:%wZ\n", objectAddr + 0x18, *(INT*)(objectAddr+0x18 + 0x084), getAllNameByEprocessAddr(objectAddr+0x18))); } else if (RtlCompareUnicodeString(pTypeName, &threadStr, FALSE) == 0) { KdPrint(("线程内核对象地址为%p,线程ID:%d\n", objectAddr + 0x18, *(INT*)(objectAddr + 0x18 +0x1f0))); } else { KdPrint(("%wZ内核对象地址为%p\n", pTypeName,objectAddr + 0x18)); } } }
void forEachHandleFromProcess(char* name) { ULONG eprocessAddress = getProcessInLink(name); if (!eprocessAddress) { KdPrint(("指定进程不存在\n")); return; } ULONG handleTableDescripAddr = *(PULONG)(eprocessAddress + 0xc4); int handleNum = *(INT*)(handleTableDescripAddr + 0x03c); KdPrint(("指定进程_HANDLE_TABLE中句柄计数为%d\n", handleNum)); ULONG handleTableAddr = *(PULONG)handleTableDescripAddr; KdPrint(("当前进程句柄表层级为%d\n", (handleTableAddr & 0x3)+1)); if ((handleTableAddr & 0x3)!=0) { KdPrint(("当前进程句柄表并非一级句柄表,不处理\n")); return; } for (size_t i = 0; i < handleNum; i++) { analyseLocalHandleDescAddr(handleTableAddr + i * 8); } }
ULONG getFuncAddr(PCWSTR funcName) { UNICODE_STRING UFuncName; RtlInitUnicodeString(&UFuncName, funcName); ULONG funcAddr = (ULONG)MmGetSystemRoutineAddress(&UFuncName); return funcAddr; }
ULONG GetPspCidTable() { return *(PULONG)(*(PULONG)(getFuncAddr(L"PsLookupProcessByProcessId") + 0x1A)); }
void analyseGlobalHandleDescAddr(ULONG handleDescAddr) { ULONG objectAddr = *(PULONG)(handleDescAddr); objectAddr &= 0xFFFFFFF8; if (objectAddr != NULL) { PUNICODE_STRING pTypeName = (*(PULONG)(objectAddr - 0x18 + 0x8)) + 0x040; if (RtlCompareUnicodeString(pTypeName, &processStr, FALSE) == 0) { KdPrint(("进程内核对象地址为%p,进程ID:%d,全路径进程名:%wZ\n", objectAddr, *(INT*)(objectAddr + 0x084), getAllNameByEprocessAddr(objectAddr))); } else if (RtlCompareUnicodeString(pTypeName, &threadStr, FALSE) == 0) { KdPrint(("线程内核对象地址为%p,线程ID:%d\n", objectAddr, *(INT*)(objectAddr + 0x1f0))); } else { KdPrint(("未知内核对象地址为%p\n", objectAddr)); } } }
void forEachGlobalHandle() { ULONG pspCidTable = GetPspCidTable(); int handleNum = *(INT*)(pspCidTable + 0x03c); KdPrint(("全局句柄_HANDLE_TABLE中句柄计数为%d\n", handleNum)); ULONG tableCode= *(PULONG)(pspCidTable); KdPrint(("全局句柄层级为%d\n", (tableCode & 0x3)+1)); if ((tableCode & 0x3) != 0) { KdPrint(("全局句柄表非一级,未处理\n")); return; } ULONG handleTableAddr = tableCode; for (size_t i = 0; i < 512; i++) { analyseGlobalHandleDescAddr(handleTableAddr + i * 8); } }
ULONG findEprocessAddrByGlobalHandle(char* processName) { ULONG pspCidTable = GetPspCidTable(); ULONG tableCode = *(PULONG)(pspCidTable); if ((tableCode & 0x3) != 0) { KdPrint(("全局句柄表非一级,未处理\n")); return NULL; } for (size_t i = 0; i < 512; i++) { ULONG handleDesc = *(PULONG)(tableCode + i * 8); handleDesc &= 0xFFFFFFF8; if (handleDesc != NULL) { PUNICODE_STRING pTypeName = (*(PULONG)(handleDesc - 0x18 + 0x8)) + 0x040; if (RtlCompareUnicodeString(pTypeName, &processStr, FALSE) == 0) { int length = strlen(processName) > 15 ? 15 : strlen(processName); if (strncmp(handleDesc + 0x174, processName, length) == 0) { return handleDesc; } } } } return NULL; }
void ZwKillProcess(ULONG pid) { HANDLE ProcessHandle = NULL; OBJECT_ATTRIBUTES obj; CLIENT_ID cid = { 0 }; NTSTATUS ntStatus = STATUS_UNSUCCESSFUL; InitializeObjectAttributes(&obj, NULL, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL); cid.UniqueProcess = (HANDLE)pid; cid.UniqueThread = 0; ntStatus = ZwOpenProcess(&ProcessHandle, GENERIC_ALL, &obj, &cid); if (NT_SUCCESS(ntStatus)) { ZwTerminateProcess(ProcessHandle, 0); ZwClose(ProcessHandle); } ZwClose(ProcessHandle); }
BOOLEAN isHandleSave(char* processName) { ULONG eprocessAddr = findEprocessAddrByGlobalHandle(processName); if (!eprocessAddr) { KdPrint(("目标进程未开启")); return TRUE; } ULONG pspCidTable = GetPspCidTable(); ULONG tableCode = *(PULONG)(pspCidTable); if ((tableCode & 0x3) != 0) { KdPrint(("全局句柄表非一级,未处理\n")); return FALSE; } for (size_t i = 0; i < 512; i++) { ULONG handleDesc = *(PULONG)(tableCode + i * 8); handleDesc &= 0xFFFFFFF8; if (handleDesc != NULL) { PUNICODE_STRING pTypeName = (*(PULONG)(handleDesc - 0x18 + 0x8)) + 0x040; if (RtlCompareUnicodeString(pTypeName, &processStr, FALSE) == 0) { int length = strlen(processName) > 15 ? 15 : strlen(processName); if (strncmp(handleDesc + 0x174, processName, length) != 0) { ULONG localHandleTableDescripAddr = *(PULONG)(handleDesc + 0xc4); ULONG localTableCode = *(PULONG)localHandleTableDescripAddr; switch (localTableCode & 0x3) { case 0: for (size_t i = 0; i < 512; i++) { ULONG eprocessInProcess = ((*(PULONG)(localTableCode + i * 8)) & 0xFFFFFFF8) + 0x18; if (eprocessInProcess == eprocessAddr) { if (strcmp(handleDesc + 0x174, "csrss.exe") == 0) continue; KdPrint(("中止进程%d进程id的进程 进程:%wZ 疑似攻击 %s进程", *(PULONG)(handleDesc +0x84), getAllNameByEprocessAddr(handleDesc), processName)); ZwKillProcess(*(PULONG)(handleDesc + 0x84)); return FALSE; } } break; case 1: localTableCode &= 0xFFFFFFFC; for (size_t i = 0; i < 1024; i++) { ULONG contentItem = *(PULONG)(localTableCode + i * 4); if (contentItem==NULL) { break; } for (size_t j = 0; j < 512; j++) { ULONG eprocessInProcess = ((*(PULONG)(contentItem + j * 8)) & 0xFFFFFFF8)+0x18; if (eprocessInProcess== eprocessAddr) { if (strcmp(handleDesc + 0x174, "csrss.exe") == 0) continue; KdPrint(("中止进程%d进程id的进程 进程:%wZ 疑似攻击 %s进程", *(PULONG)(handleDesc + 0x84), getAllNameByEprocessAddr(handleDesc),processName)); ZwKillProcess(*(PULONG)(handleDesc + 0x84)); return FALSE; } } } break; default: KdPrint(("当前进程句柄表为3级句柄,不处理\n")); return FALSE; break; } } } } } return TRUE; }
NTSTATUS DriverEntry(PDRIVER_OBJECT pdriver, PUNICODE_STRING pReg) { pdriver->DriverUnload = DriverUnload; KdPrint(("wantProtect.exe是安全的?:%d",isHandleSave("wantProtect.exe"))); return STATUS_SUCCESS; }
|