强杀进程
PsTerminateProcessPspTerminateProcess PspTerminateThreadByPointerPspExitThread未导出函数
#include <ntddk.h> #include <ntimage.h> #include <ntdef.h> #include "Ioctlcmd.h" const WCHAR deviceLinkBuffer[] = L"\\DosDevices\\KillProc"; const WCHAR deviceNameBuffer[] = L"\\Device\\KillProc"; typedef NTSTATUS (*NTQUERYSYSTEMINFORMATION)( IN ULONG SystemInformationClass, OUT PVOID SystemInformation, IN ULONG SystemInformationLength, OUT PULONG ReturnLength OPTIONAL ); typedef unsigned long DWORD; NTQUERYSYSTEMINFORMATION NtQuerySystemInformation; #define SystemModuleInformation 11 typedef struct _SYSTEM_MODULE_INFORMATION { ULONG Reserved[2]; PVOID Base; ULONG Size; ULONG Flags; USHORT Index; USHORT Unknown; USHORT LoadCount; USHORT ModuleNameOffset; CHAR ImageName[256]; } SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION; PDEVICE_OBJECT g_HookDevice; NTSTATUS PsLookupProcessByProcessId(ULONG ProcessId,PEPROCESS *Process); typedef NTSTATUS (*PSPTERPROC) ( PEPROCESS Process, NTSTATUS ExitStatus ); PSPTERPROC MyPspTerminateProcess = NULL ; NTSTATUS OnUnload(IN PDRIVER_OBJECT DriverObject) { UNICODE_STRING deviceLinkUnicodeString; PDEVICE_OBJECT p_NextObj; DbgPrint("OnUnload called\n"); p_NextObj = DriverObject->DeviceObject; if (p_NextObj != NULL) { RtlInitUnicodeString( &deviceLinkUnicodeString, deviceLinkBuffer ); IoDeleteSymbolicLink( &deviceLinkUnicodeString ); IoDeleteDevice( DriverObject->DeviceObject ); } return STATUS_SUCCESS; } NTSTATUS DispatchControl( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PIO_STACK_LOCATION irpStack; PVOID inputBuffer; PVOID outputBuffer; PVOID userBuffer; ULONG inputBufferLength; ULONG outputBufferLength; ULONG ioControlCode; NTSTATUS ntstatus; unsigned int i; unsigned total = 0; ULONG count = 0; HANDLE handle; ULONG cnt; PEPROCESS Eprocess = NULL; DWORD pid; ntstatus = Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0; irpStack = IoGetCurrentIrpStackLocation (Irp); inputBuffer = Irp->AssociatedIrp.SystemBuffer; inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength; outputBuffer = Irp->AssociatedIrp.SystemBuffer; outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength; ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode; switch (irpStack->MajorFunction) { case IRP_MJ_CREATE: break; case IRP_MJ_SHUTDOWN: break; case IRP_MJ_CLOSE: break; case IRP_MJ_DEVICE_CONTROL: if(IOCTL_TRANSFER_TYPE(ioControlCode) == METHOD_NEITHER) { outputBuffer = Irp->UserBuffer; } switch (ioControlCode ) { case IOCTL_PROC_KILL: if(MyPspTerminateProcess==NULL) { *(DWORD*)outputBuffer = -1; Irp->IoStatus.Information = sizeof(DWORD); } else { pid = *(DWORD*)inputBuffer; { ntstatus = PsLookupProcessByProcessId(pid , &Eprocess); if(!NT_SUCCESS(ntstatus)) { DbgPrint("Failed to lookup process 0x%x, status %8.8x\n", pid , ntstatus); *(DWORD*)outputBuffer = 1; Irp->IoStatus.Information = sizeof(DWORD); break; } DbgPrint("Lookup of process 0x%x, PEPROCESS at %8.8x\n", pid, Eprocess); ntstatus = MyPspTerminateProcess(Eprocess, 0); if(!NT_SUCCESS(ntstatus)) { DbgPrint("Failed to terminate process 0x%x, status %8.8x\n", pid, ntstatus); *(DWORD*)outputBuffer = 2; Irp->IoStatus.Information = sizeof(DWORD); break; } *(DWORD*)outputBuffer = 0; Irp->IoStatus.Information = sizeof(DWORD); DbgPrint("Process 0x%x terminated\n", pid); } } break; default: break; } IoCompleteRequest( Irp, IO_NO_INCREMENT ); } return ntstatus; } NTSTATUS DispatchCreate ( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) { pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } ULONG GetFunctionAddr( IN PCWSTR FunctionName) { UNICODE_STRING UniCodeFunctionName; RtlInitUnicodeString( &UniCodeFunctionName, FunctionName ); return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName ); } VOID DoFind(IN PVOID pContext) { NTSTATUS ret; PSYSTEM_MODULE_INFORMATION module = NULL; ULONG n=0; void *buf = NULL; ULONG ntosknlBase; ULONG ntosknlEndAddr; ULONG curAddr; ULONG code1_sp3=0x8b55ff8b,code2_sp3=0xA16456EC,code3_sp3=0x00000124,code4_sp3=0x3B08758B; ULONG i; NtQuerySystemInformation=(NTQUERYSYSTEMINFORMATION)GetFunctionAddr(L"NtQuerySystemInformation"); if (!NtQuerySystemInformation) { DbgPrint("Find NtQuerySystemInformation faild!"); goto Ret; } ret=NtQuerySystemInformation(SystemModuleInformation,&n,0,&n); if (NULL==( buf=ExAllocatePoolWithTag(NonPagedPool, n, 'DFSP'))) { DbgPrint("ExAllocatePool() failed\n" ); goto Ret; } ret=NtQuerySystemInformation(SystemModuleInformation,buf,n,NULL); if (!NT_SUCCESS(ret)) { DbgPrint("NtQuerySystemInformation faild!"); goto Ret; } module=(PSYSTEM_MODULE_INFORMATION)((PULONG)buf+1); ntosknlEndAddr=(ULONG)module->Base+(ULONG)module->Size; ntosknlBase=(ULONG)module->Base; curAddr=ntosknlBase; ExFreePool(buf); for (i=curAddr;i<=ntosknlEndAddr;i++) { if (*((ULONG *)i)==code1_sp3) { if (*((ULONG *)(i+4))==code2_sp3) { if (*((ULONG *)(i+8))==code3_sp3) { if (*((ULONG *)(i+12))==code4_sp3) { MyPspTerminateProcess=(PSPTERPROC)i; break; } } } } } Ret: PsTerminateSystemThread(STATUS_SUCCESS); } VOID GetPspAddr() { HANDLE hThread; PVOID objtowait=0; NTSTATUS dwStatus = PsCreateSystemThread( &hThread, 0, NULL, (HANDLE)0, NULL, DoFind, NULL ); NTSTATUS st; if ((KeGetCurrentIrql())!=PASSIVE_LEVEL) { st=KfRaiseIrql(PASSIVE_LEVEL); } if ((KeGetCurrentIrql())!=PASSIVE_LEVEL) { return; } ObReferenceObjectByHandle( hThread, THREAD_ALL_ACCESS, NULL, KernelMode, &objtowait, NULL ); st=KeWaitForSingleObject(objtowait,Executive,KernelMode,FALSE,NULL); //NULL表示无限期等待. return; } NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { NTSTATUS rc; RTL_OSVERSIONINFOW osvi; NTSTATUS ntStatus; UNICODE_STRING deviceNameUnicodeString; UNICODE_STRING deviceLinkUnicodeString; RtlInitUnicodeString (&deviceNameUnicodeString, deviceNameBuffer ); RtlInitUnicodeString (&deviceLinkUnicodeString, deviceLinkBuffer ); ntStatus = IoCreateDevice ( DriverObject, 0, &deviceNameUnicodeString, FILE_DEVICE_SWAP, 0, TRUE, &g_HookDevice ); if(! NT_SUCCESS(ntStatus)) { DbgPrint(("Failed to create device!\n")); return ntStatus; } ntStatus = IoCreateSymbolicLink (&deviceLinkUnicodeString, &deviceNameUnicodeString ); if(! NT_SUCCESS(ntStatus)) { IoDeleteDevice(DriverObject->DeviceObject); DbgPrint("Failed to create symbolic link!\n"); return ntStatus; } DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate; DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchControl; DriverObject->DriverUnload = OnUnload; GetPspAddr(); if(MyPspTerminateProcess == NULL) { DbgPrint("PspFunc Not Find!\n"); } return STATUS_SUCCESS; }
首先对 PsTerminateSystemThread 进行反汇编:
fffff800`03f9260a c3 ret
typedef NTSTATUS (__fastcall *PSPTERMINATETHREADBYPOINTER) ( IN PETHREAD Thread, IN NTSTATUS ExitStatus, IN BOOLEAN DirectTerminate );
ULONG32 callcode=0; ULONG64 AddressOfPspTTBP=0, AddressOfPsTST=0, i=0; AddressOfPsTST=(ULONG64)GetFunctionAddr(L"PsTerminateSystemThread"); if(AddressOfPsTST==0) return STATUS_UNSUCCESSFUL; for(i=1;i<0xff;i++) { if(MmIsAddressValid((PVOID)(AddressOfPsTST+i))!=FALSE) { if(*(BYTE *)(AddressOfPsTST+i)==0x01 && *(BYTE *)(AddressOfPsTST+i+1)==0xe8) //目标地址-原始地址-5=机器码 ==> 目标地址=机器码+5+原始地址 { RtlMoveMemory(&callcode,(PVOID)(AddressOfPsTST+i+2),4); AddressOfPspTTBP=(ULONG64)callcode + 5 + AddressOfPsTST+i+1; } } } PspTerminateThreadByPointer=(PSPTERMINATETHREADBYPOINTER)AddressOfPspTTBP;
PETHREAD Thread=NULL; PEPROCESS tProcess=NULL; NTSTATUS status=0; for(i=4;i<0x40000;i+=4) { status=PsLookupThreadByThreadId((HANDLE)i, &Thread); if(NT_SUCCESS(status)) { tProcess=IoThreadToProcess(Thread); if(tProcess==Process) PspTerminateThreadByPointer(Thread,0,1); ObDereferenceObject(Thread); } }
case IOCTL_KILLPROCESS: { __try { Inbuff = *(ULONG*)pIoBuffer; KdPrint(("Kill Process:%d",Inbuff)); Eprocess = (ULONGLONG)GetEProcessByPid((HANDLE)Inbuff); if(Eprocess != 0){ MyPsTerminateSystemProcess((PEPROCESS)Eprocess); ObDereferenceObject((PEPROCESS)Eprocess); RtlCopyMemory( pIrp->AssociatedIrp.SystemBuffer, &Eprocess, sizeof(ULONGLONG) ); }else { RtlCopyMemory( pIrp->AssociatedIrp.SystemBuffer, &Eprocess, sizeof(ULONGLONG) ); } status = STATUS_SUCCESS; } __except(EXCEPTION_EXECUTE_HANDLER) { KdPrint(("结束进程出现异常!")); } break; }