} else { IsGetSSDT=true; } for (uIndex = 0;uIndex<pNewSSDT->NumberOfServices;uIndex++) { SSDT[uIndex].index = uIndex; //序号 SSDT[uIndex].OriginalAddress=pNewSSDT->ServiceTableBase[uIndex]; //原始地址 SSDT[uIndex].CurrentAddress=KeServiceDescriptorTable.ServiceTableBase[uIndex]; } SSDTNumber=pNewSSDT->NumberOfServices; //有多少项}BOOLEAN LoadKernel(){ NTSTATUS status; UNICODE_STRING uFileName; HANDLE hFile; OBJECT_ATTRIBUTES ObjAttr; IO_STATUS_BLOCK IoStatusBlock; LARGE_INTEGER FileOffset; ULONG retsize; PVOID lpVirtualPointer; ULONG uLoop; ULONG SectionVirtualAddress,SectionSize; PIMAGE_DOS_HEADER ImageDosHeader; PIMAGE_NT_HEADERS ImageNtHeader; PIMAGE_SECTION_HEADER lpImageSectionHeader; InitializeObjectAttributes(&ObjAttr,\ &uFileName,\ OBJ_CASE_INSENSITIVE,\ NULL,\ NULL); RtlInitUnicodeString(&uFileName,NtosVersionName[NtosVersion]); //打开文件 status = ZwCreateFile(\ &hFile,\ FILE_ALL_ACCESS,\ &ObjAttr,\ &IoStatusBlock,\ 0,\ FILE_ATTRIBUTE_NORMAL,\ FILE_SHARE_READ,\ FILE_OPEN,\ FILE_NON_DIRECTORY_FILE,\ NULL,\ 0); if(!NT_SUCCESS(status)) { KdPrint(("CreateFile Failed!\n")); return false; } //读取DOS头 FileOffset.QuadPart = 0; ImageDosHeader=(PIMAGE_DOS_HEADER)ExAllocatePool(NonPagedPool, sizeof(IMAGE_DOS_HEADER)); //记得释放 status = ZwReadFile(hFile,\ NULL,\ NULL,\ NULL,\ &IoStatusBlock,\ ImageDosHeader,\ sizeof(IMAGE_DOS_HEADER),\ &FileOffset,\ 0); if(!NT_SUCCESS(status)) { KdPrint(("Read ImageDosHeader Failed!")); ZwClose(hFile); return false; } //读取NT头 ImageNtHeader=(PIMAGE_NT_HEADERS)ExAllocatePool(NonPagedPool, sizeof(IMAGE_NT_HEADERS)); //记得释放 FileOffset.QuadPart = ImageDosHeader->e_lfanew; status = ZwReadFile(hFile,\ NULL,\ NULL,\ NULL,\ &IoStatusBlock,\ ImageNtHeader,\ sizeof(IMAGE_NT_HEADERS),\ &FileOffset,\ 0); if(!NT_SUCCESS(status)) { KdPrint(("Read ImageNtHeaders Failed!")); ZwClose(hFile); return false; } ImageBase=ImageNtHeader->OptionalHeader.ImageBase; //读取区表 lpImageSectionHeader = (PIMAGE_SECTION_HEADER)ExAllocatePool(NonPagedPool,sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader->FileHeader.NumberOfSections); FileOffset.QuadPart += sizeof(IMAGE_NT_HEADERS); status = ZwReadFile(hFile,\ NULL,\ NULL,\ NULL,\ &IoStatusBlock,\ lpImageSectionHeader,\ sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader->FileHeader.NumberOfSections,\ &FileOffset,\ 0); if(!NT_SUCCESS(status)) { KdPrint(("Read ImageSectionHeader Failed!")); ExFreePool(lpImageSectionHeader); ZwClose(hFile); return false; } //COPY数据到内存 lpVirtualPointer = ExAllocatePool(NonPagedPool,\ ImageNtHeader->OptionalHeader.SizeOfImage); if(lpVirtualPointer == 0) { KdPrint(("lpVirtualPointer Alloc space Failed!")); ZwClose(hFile); return false; } memset(lpVirtualPointer,0,ImageNtHeader->OptionalHeader.SizeOfImage); //COPY DOS头 RtlCopyMemory(lpVirtualPointer,\ ImageDosHeader,\ sizeof(IMAGE_DOS_HEADER)); //COPY NT头 RtlCopyMemory((PVOID)((ULONG)lpVirtualPointer+ImageDosHeader->e_lfanew),\ ImageNtHeader,\ sizeof(IMAGE_NT_HEADERS)); //COPY 区表 RtlCopyMemory((PVOID)((ULONG)lpVirtualPointer+ImageDosHeader->e_lfanew+sizeof(IMAGE_NT_HEADERS)),\ lpImageSectionHeader,\ sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader->FileHeader.NumberOfSections); //依次COPY 各区段数据 for(uLoop = 0;uLoop < ImageNtHeader->FileHeader.NumberOfSections;uLoop++) { SectionVirtualAddress = lpImageSectionHeader[uLoop].VirtualAddress;//对应区段相对偏移 if(lpImageSectionHeader[uLoop].Misc.VirtualSize > lpImageSectionHeader[uLoop].SizeOfRawData) SectionSize = lpImageSectionHeader[uLoop].Misc.VirtualSize;//取最大的占用空间 else SectionSize = lpImageSectionHeader[uLoop].SizeOfRawData; FileOffset.QuadPart = lpImageSectionHeader[uLoop].PointerToRawData;//对应区段的超始地址 status = ZwReadFile(hFile,\ NULL,\ NULL,\ NULL,\ &IoStatusBlock,\ (PVOID)((ULONG)lpVirtualPointer+SectionVirtualAddress),\ SectionSize,\ &FileOffset,\ 0); if(!NT_SUCCESS(status)) { KdPrint(("SectionData Read Failed!")); ExFreePool(lpImageSectionHeader); ExFreePool(lpVirtualPointer); ZwClose(hFile); return false; } } SetNewSSDT(lpVirtualPointer); ExFreePool(lpImageSectionHeader);//释放区段内存空间 ExFreePool(ImageNtHeader); ZwClose(hFile);//关闭句柄 return true;}// 获取kernel模块的信息NTSTATUS GetKernelModuleInfo(){ NTSTATUS status = STATUS_UNSUCCESSFUL; PSYSTEM_MODULE_INFO_LIST pSysModInfoList = NULL; ULONG ulLength = 0; status = ZwQuerySystemInformation(SystemModuleInformation, pSysModInfoList, ulLength, &ulLength); if(status != STATUS_INFO_LENGTH_MISMATCH) { return STATUS_UNSUCCESSFUL; } pSysModInfoList = (PSYSTEM_MODULE_INFO_LIST)ExAllocatePool(NonPagedPool, ulLength); if(NULL == pSysModInfoList) { return STATUS_UNSUCCESSFUL; } status = ZwQuerySystemInformation(SystemModuleInformation, pSysModInfoList, ulLength, &ulLength); if(!NT_SUCCESS(status)) { ExFreePool(pSysModInfoList); return STATUS_UNSUCCESSFUL; } OldImageBase=(ULONG)pSysModInfoList->smi[0].Base; //得到当前内核的基址 if(strstr(pSysModInfoList->smi[0].ImageName,"ntoskrnl.exe")) { NtosVersion=0; } if(strstr(pSysModInfoList->smi[0].ImageName,"ntkrnlpa.exe")) { NtosVersion=1; } if(strstr(pSysModInfoList->smi[0].ImageName,"ntkrnlmp.exe")) { NtosVersion=2; } if(strstr(pSysModInfoList->smi[0].ImageName,"ntkrpamp.exe")) { NtosVersion=3; } ExFreePool(pSysModInfoList); return STATUS_SUCCESS;}NTSTATUS Status;HANDLE FileHandle;IO_STATUS_BLOCK ioStatus;FILE_STANDARD_INFORMATION FileInformation;BOOLEAN GetSSDTName(){ if (KeGetCurrentIrql() > PASSIVE_LEVEL) { return STATUS_UNSUCCESSFUL; } //设置NTDLL路径 UNICODE_STRING uniFileName; RtlInitUnicodeString(&uniFileName, L"\\SystemRoot\\system32\\ntdll.dll"); //初始化打开文件的属性 OBJECT_ATTRIBUTES objectAttributes; InitializeObjectAttributes(&objectAttributes, &uniFileName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL); ////创建文件 Status = IoCreateFile(&FileHandle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &objectAttributes, &ioStatus, 0, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0, CreateFileTypeNone, NULL, IO_NO_PARAMETER_CHECKING); if (!NT_SUCCESS(Status)) { DbgPrint("IoCreateFile failed!status:0x%08x\n", Status); return false; }