三环传入一个指定的PID,驱动将保护通过删除目标进程的句柄表和PID,这样在任务管理器中将无法结束进程(注意:虽然任务管理器无法结束,但是由于VMware注册了一个回调函数,导致只要自身关闭会发生蓝屏,所以需要把VM3DMP.sys去掉)
我所使用的系统是Win7 sp1,不同的系统特征码等也会不一样,请自行提取
//搜索特征码头文件
#pragma once
#include
typedef enum _SYSTEM_INFORMATION_CLASS {
SystemBasicInformation,
SystemProcessorInformation, // obsolete...delete
SystemPerformanceInformation,
SystemTimeOfDayInformation,
SystemPathInformation,
SystemProcessInformation,
SystemCallCountInformation,
SystemDeviceInformation,
SystemProcessorPerformanceInformation,
SystemFlagsInformation,
SystemCallTimeInformation,
SystemModuleInformation,
SystemLocksInformation,
SystemStackTraceInformation,
SystemPagedPoolInformation,
SystemNonPagedPoolInformation,
SystemHandleInformation,
SystemObjectInformation,
SystemPageFileInformation,
SystemVdmInstemulInformation,
SystemVdmBopInformation,
SystemFileCacheInformation,
SystemPoolTagInformation,
SystemInterruptInformation,
SystemDpcBehaviorInformation,
SystemFullMemoryInformation,
SystemLoadGdiDriverInformation,
SystemUnloadGdiDriverInformation,
SystemTimeAdjustmentInformation,
SystemSummaryMemoryInformation,
SystemMirrorMemoryInformation,
SystemPerformanceTraceInformation,
SystemObsolete0,
SystemExceptionInformation,
SystemCrashDumpStateInformation,
SystemKernelDebuggerInformation,
SystemContextSwitchInformation,
SystemRegistryQuotaInformation,
SystemExtendServiceTableInformation,
SystemPrioritySeperation,
SystemVerifierAddDriverInformation,
SystemVerifierRemoveDriverInformation,
SystemProcessorIdleInformation,
SystemLegacyDriverInformation,
SystemCurrentTimeZoneInformation,
SystemLookasideInformation,
SystemTimeSlipNotification,
SystemSessionCreate,
SystemSessionDetach,
SystemSessionInformation,
SystemRangeStartInformation,
SystemVerifierInformation,
SystemVerifierThunkExtend,
SystemSessionProcessInformation,
SystemLoadGdiDriverInSystemSpace,
SystemNumaProcessorMap,
SystemPrefetcherInformation,
SystemExtendedProcessInformation,
SystemRecommendedSharedDataAlignment,
SystemComPlusPackage,
SystemNumaAvailableMemory,
SystemProcessorPowerInformation,
SystemEmulationBasicInformation,
SystemEmulationProcessorInformation,
SystemExtendedHandleInformation,
SystemLostDelayedWriteInformation,
SystemBigPoolInformation,
SystemSessionPoolTagInformation,
SystemSessionMappedViewInformation,
SystemHotpatchInformation,
SystemObjectSecurityMode,
SystemWatchdogTimerHandler,
SystemWatchdogTimerInformation,
SystemLogicalProcessorInformation,
SystemWow64SharedInformation,
SystemRegisterFirmwareTableInformationHandler,
SystemFirmwareTableInformation,
SystemModuleInformationEx,
SystemVerifierTriageInformation,
SystemSuperfetchInformation,
SystemMemoryListInformation,
SystemFileCacheInformationEx,
MaxSystemInfoClass // MaxSystemInfoClass should always be the last enum
} SYSTEM_INFORMATION_CLASS;
typedef struct _RTL_PROCESS_MODULE_INFORMATION {
HANDLE Section; // Not filled in
PVOID MappedBase;
PVOID ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
typedef struct _RTL_PROCESS_MODULES {
ULONG NumberOfModules;
RTL_PROCESS_MODULE_INFORMATION Modules[1];
} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
NTSTATUS NTAPI ZwQuerySystemInformation(
__in SYSTEM_INFORMATION_CLASS SystemInformationClass,
__out_bcount_opt(SystemInformationLength) PVOID SystemInformation,
__in ULONG SystemInformationLength,
__out_opt PULONG ReturnLength
);
typedef struct _FindCode
{
UCHAR code[0x200];
ULONG len;
int offset;
ULONG lastAddressOffset;
}FindCode, *PFindCode;
void initFindCodeStruct(PFindCode findCode, PCHAR code, ULONG_PTR offset, ULONG_PTR lastAddrOffset);
ULONG_PTR findAddressByCode(ULONG_PTR beginAddr, ULONG_PTR endAddr, PFindCode findCode, ULONG numbers);
ULONG_PTR QuerySysModule(char * MoudleName, _Out_opt_ ULONG_PTR * module);
ULONG searchNtCode(char * code, int offset);
ULONG searchCode(char * moduleName, char * segmentName, char * code, int offset);
//搜索特征码
#include "Search.h"
#include
UCHAR charToHex(UCHAR * ch)
{
unsigned char temps[2] = { 0 };
for (int i = 0; i < 2; i++)
{
if (ch[i] >= '0' && ch[i] <= '9')
{
temps[i] = (ch[i] - '0');
}
else if (ch[i] >= 'A' && ch[i] <= 'F')
{
temps[i] = (ch[i] - 'A') + 0xA;
}
else if (ch[i] >= 'a' && ch[i] <= 'f')
{
temps[i] = (ch[i] - 'a') + 0xA;
}
}
return ((temps[0] << 4) & 0xf0) | (temps[1] & 0xf);
}
void initFindCodeStruct(PFindCode findCode, PCHAR code, ULONG_PTR offset, ULONG_PTR lastAddrOffset)
{
memset(findCode, 0, sizeof(FindCode));
findCode->lastAddressOffset = lastAddrOffset;
findCode->offset = offset;
PCHAR pTemp = code;
ULONG_PTR i = 0;
for (i = 0; *pTemp != '\0'; i++)
{
if (*pTemp == '*' || *pTemp == '?')
{
findCode->code[i] = *pTemp;
pTemp++;
continue;
}
findCode->code[i] = charToHex(pTemp);
pTemp += 2;
}
findCode->len = i;
}
ULONG_PTR findAddressByCode(ULONG_PTR beginAddr, ULONG_PTR endAddr, PFindCode findCode, ULONG numbers)
{
ULONG64 j = 0;
LARGE_INTEGER rtna = { 0 };
for (ULONG_PTR i = beginAddr; i <= endAddr; i++)
{
if (!MmIsAddressValid((PVOID)i))
{
i = i & (~0xfff) + PAGE_SIZE - 1;
continue;
}
for (j = 0; j < numbers; j++)
{
FindCode fc = findCode[j];
ULONG_PTR tempAddress = i;
UCHAR * code = (UCHAR *)(tempAddress + fc.offset);
BOOLEAN isFlags = FALSE;
for (ULONG_PTR k = 0; k < fc.len; k++)
{
if (!MmIsAddressValid((PVOID)(code + k)))
{
isFlags = TRUE;
break;
}
if (fc.code[k] == '*' || fc.code[k] == '?') continue;
if (code[k] != fc.code[k])
{
isFlags = TRUE;
break;
}
}
if (isFlags) break;
}
//找到了
if (j == numbers)
{
rtna.QuadPart = i;
rtna.LowPart += findCode[0].lastAddressOffset;
break;
}
}
return rtna.QuadPart;
}
char * CharToUper(char * wstr, BOOLEAN isAllocateMemory)
{
char * ret = NULL;
if (isAllocateMemory)
{
int len = strlen(wstr) + 2;
ret = ExAllocatePool(PagedPool, len);
memset(ret, 0, len);
memcpy(ret, wstr, len - 2);
}
else
{
ret = wstr;
}
_strupr(ret);
return ret;
}
//返回值为模块的大小
ULONG_PTR QuerySysModule(char * MoudleName, _Out_opt_ ULONG_PTR * module)
{
RTL_PROCESS_MODULES info;
ULONG retPro = NULL;
ULONG_PTR moduleSize = 0;
NTSTATUS ststas = ZwQuerySystemInformation(SystemModuleInformation, &info, sizeof(info), &retPro);
char * moduleUper = CharToUper(MoudleName, TRUE);
if (ststas == STATUS_INFO_LENGTH_MISMATCH)
{
//申请长度
ULONG len = retPro + sizeof(RTL_PROCESS_MODULES);
PRTL_PROCESS_MODULES mem = (PRTL_PROCESS_MODULES)ExAllocatePool(PagedPool, len);
memset(mem, 0, len);
ststas = ZwQuerySystemInformation(SystemModuleInformation, mem, len, &retPro);
if (!NT_SUCCESS(ststas))
{
ExFreePool(moduleUper);
ExFreePool(mem);
return 0;
}
//开始查询
if (strstr(MoudleName, "ntkrnlpa.exe") || strstr(MoudleName, "ntoskrnl.exe"))
{
PRTL_PROCESS_MODULE_INFORMATION ModuleInfo = &(mem->Modules[0]);
*module = ModuleInfo->ImageBase;
moduleSize = ModuleInfo->ImageSize;
}
else
{
for (int i = 0; i < mem->NumberOfModules; i++)
{
PRTL_PROCESS_MODULE_INFORMATION processModule = &mem->Modules[i];
CharToUper(processModule->FullPathName, FALSE);
if (strstr(processModule->FullPathName, moduleUper))
{
if (module)
{
*module = processModule->ImageBase;
}
moduleSize = processModule->ImageSize;
break;
}
}
}
ExFreePool(mem);
}
ExFreePool(moduleUper);
return moduleSize;
}
ULONG searchNtCode(char * code,int offset)
{
FindCode fs[1] = { 0 };
initFindCodeStruct(&fs[0], code, 0, offset);
SIZE_T moduleBase = 0;
ULONG size = QuerySysModule("ntoskrnl.exe", &moduleBase);
ULONG_PTR func = findAddressByCode(moduleBase, size + moduleBase, fs, 1);
return func;
}
ULONG searchCode(char * moduleName, char * segmentName, char * code, int offset)
{
FindCode fs[1] = { 0 };
initFindCodeStruct(&fs[0], code, 0, offset);
SIZE_T moduleBase = 0;
ULONG size = QuerySysModule(moduleName, &moduleBase);
if (!moduleBase)
{
return 0;
}
PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)moduleBase;
PIMAGE_NT_HEADERS pNts = (PIMAGE_NT_HEADERS)((PUCHAR)moduleBase + pDos->e_lfanew);
PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNts);
PIMAGE_SECTION_HEADER pTemp = NULL;
for (int i = 0; i < pNts->FileHeader.NumberOfSections; i++)
{
char bufName[9] = {0};
memcpy(bufName, pSection->Name, 8);
if (_stricmp(bufName, segmentName) == 0)
{
pTemp = pSection;
break;
}
pSection++;
}
if (pTemp)
{
moduleBase = pSection->VirtualAddress + moduleBase;
size = pSection->SizeOfRawData;
}
ULONG_PTR func = findAddressByCode(moduleBase, size + moduleBase, fs, 1);
return func;
}
//隐藏句柄表的驱动程序
#include
#include"Search.h"
#define My_Code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS)
typedef struct _DEVICE_EXTENSION {
UNICODE_STRING SymLinkName;
} DEVICE_EXTENSION, * PDEVICE_EXTENSION;
typedef struct Hread {
ULONG Flage;
ULONG Addr;
ULONG WriteBufferAddr;
ULONG Size;
ULONG Pid;
}_Hread, * PtrHread;
VOID DriverUnload(PDRIVER_OBJECT pDriverObject)
{
DbgPrint("--------------DRIVER_UNLOAD-----------------");
PDEVICE_OBJECT pDevObj;
pDevObj = pDriverObject->DeviceObject;
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
UNICODE_STRING pLinkName = pDevExt->SymLinkName;
IoDeleteSymbolicLink(&pLinkName);
IoDeleteDevice(pDevObj);
}
NTSTATUS DefDispatchRoutine(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return status;
}
NTSTATUS BreakList(ULONG PID)
{
//获取TableCode位置
ULONG func = searchCode("ntoskrnl.exe", "PAGE", "8BFF558BEC83EC0C5356648B352401000033DB66FF8E8400000057FF75088B3D", 0);
func = func + 32;
DbgPrintEx(0,77,"%X\n",func);
DbgBreakPoint();
ULONG PspCidTable = func;
ULONG TableCode = ***(ULONG***)PspCidTable;
if (TableCode && 0x1 == 0)
{
ULONG PID_EPROCESS = TableCode + (PID / 4) * 8;
//清空EPROCESS的PID
*(ULONG*)((*(ULONG*)PID_EPROCESS && 0xFFFFFFFE) + 0xB4) = 0;
//清空句柄表
*(ULONG*)PID_EPROCESS = 0;
*(ULONG*)(PID_EPROCESS + 4) = 0;
}
else if (TableCode && 0x1 == 1)
{
//寻找目标EPROCESS
TableCode = TableCode & 0xFFFFFFFE;
ULONG Order_Number = (PID / 4) / 512;
TableCode = TableCode + Order_Number * 4;
Order_Number = (PID / 4) % 512;
ULONG PID_EPROCESS_Position = (*(ULONG*)TableCode + Order_Number * 8);
ULONG PID_EPROCESS = *(ULONG*)PID_EPROCESS_Position & 0xFFFFFFF8;
//清空EPROCESS的PID
*(ULONG*)(PID_EPROCESS + 0xB4) = 0;
//清空句柄表
*(ULONG*)PID_EPROCESS_Position = 0;
*(ULONG*)(PID_EPROCESS_Position + 4) = 0;
}
return STATUS_SUCCESS;
}
NTSTATUS IoctlDispatchRoutine(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
ULONG_PTR Informaiton = 0;
PVOID InputData = NULL;
ULONG InputDataLength = 0;
PVOID OutputData = NULL;
ULONG OutputDataLength = 0;
PIO_STACK_LOCATION IoStackLocation = IoGetCurrentIrpStackLocation(pIrp); // Irp堆栈
InputData = pIrp->AssociatedIrp.SystemBuffer;
OutputData = pIrp->AssociatedIrp.SystemBuffer;
InputDataLength = IoStackLocation->Parameters.DeviceIoControl.InputBufferLength; // 输入数据大小
OutputDataLength = IoStackLocation->Parameters.DeviceIoControl.OutputBufferLength; // 输出数据大小
ULONG Code = IoStackLocation->Parameters.DeviceIoControl.IoControlCode;
switch (Code)
{
case My_Code:
{
PtrHread PtrBuff = (PtrHread)InputData;
ULONG Pid = PtrBuff->Pid;
BreakList(Pid);
DbgPrint("要操作进程PID: %d", Pid);
Status = STATUS_SUCCESS;
break;
}
}
pIrp->IoStatus.Status = Status; // 设置IRP完成状态,会设置用户模式下的GetLastError
pIrp->IoStatus.Information = Informaiton; // 设置操作的字节
IoCompleteRequest(pIrp, IO_NO_INCREMENT); // 完成IRP,不增加优先级
return Status;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath)
{
pDriverObject->DriverUnload = DriverUnload;//注册驱动卸载函数
pDriverObject->MajorFunction[IRP_MJ_CREATE] = DefDispatchRoutine; // 注册派遣函数
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DefDispatchRoutine;
pDriverObject->MajorFunction[IRP_MJ_WRITE] = DefDispatchRoutine;
pDriverObject->MajorFunction[IRP_MJ_READ] = DefDispatchRoutine;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IoctlDispatchRoutine;
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
//创建设备名称的字符串
UNICODE_STRING devName;
RtlInitUnicodeString(&devName, L"\\Device\\MyDevice");
//创建设备
status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION), &devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevObj);
pDevObj->Flags |= DO_BUFFERED_IO;//将设备设置为缓冲I/O设备
pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;//得到设备扩展
//创建符号链接
UNICODE_STRING symLinkName;
RtlInitUnicodeString(&symLinkName, L"\\??\\MyDevice");
pDevExt->SymLinkName = symLinkName;
status = IoCreateSymbolicLink(&symLinkName, &devName);
return STATUS_SUCCESS;
}
//三环与驱动通信的应用程序
#include
#include
#include
// 自定义的控制信号
#define My_Code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS)
typedef struct Hread {
ULONG Flage;
ULONG Addr;
ULONG WriteBufferAddr;
ULONG Size;
ULONG Pid;
}_Hread, * PtrHread;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handle = CreateFileA("\\\\.\\MyDevice", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
unsigned char RetBufferData[20] = { 0 };
DWORD ReturnLength = 4;
_Hread buf;
printf_s("请输入你要抹除的句柄PID:");
scanf_s("%d", &buf.Pid);
buf.Flage = 2;
buf.Addr = 0x401234;
buf.WriteBufferAddr = 1024;
buf.Size = 100;
DeviceIoControl(handle, My_Code, &buf, 20, (LPVOID)RetBufferData, 4, &ReturnLength, 0);
system("pause");
CloseHandle(handle);
return 0;
}