kmodule.c
资源名称:kilster.rar [点击查看]
上传用户:nobole
上传日期:2013-04-01
资源大小:481k
文件大小:21k
源码类别:
进程与线程
开发平台:
Visual C++
- // This module hooks the internals r thread's swapcontext
- // in order to enumerate all running threads (and processes)
- // in the system. This is intended to detect some hidden processes
- // by rootkits (like fu), etc.
- //
- // This is proof-of-concept software, and no warranty is given
- // Use at your own risk.
- //
- // wowocock, there@citiz.net, 2005
- //
- #ifdef ALLOC_DATA_PRAGMA
- #pragma data_seg("PAGE")
- #endif
- #include <ntddk.h>
- #include <windef.h>
- typedef PVOID *PPVOID;
- #include "w2k_def_jr.h"
- const WCHAR devLink[] = L"\??\Myklister";
- const WCHAR devName[] = L"\Device\Myklister";
- #include "kmodule.h"
- ServiceTableInfo SrvTables[MAX_SERVICETABLES];//kmodule.h
- DWORD nSrvTables = 0;
- KLISTER_PROCINFO procs[MAX_PROCS];//kmodule.h
- ULONG gOsMajorVersion = 0;
- ULONG gOsMinorVersion = 0;
- /*线程所在进程
- lkd>dt _ETHREAD
- +000 struct _KTHREAD Tcb
- +034 struct _KAPC_STATE ApcState
- +044 struct _KPROCESS *Process
- +22c struct _EPROCESS *ThreadsProcess
- KTHREAD 偏移 +044 处的 KPROCESS *Process ,是指向线程所在进程的 KPROCESS 结构的指针。
- KTHREAD 偏移 +22c 处的 EPROCESS *ThreadsProcess ,是指向线程所在进程的 EPROCESS 结构的指针。
- 我们 KPROCESS 结构在 EPROCESS 结构中,并且位于 EPROCESS 结构开始处。+044 *Process 和
- +22c *ThreadsProcess 指向的是同一地址。
- */
- DWORD gThreadsProcessOffset =0x44;//
- DWORD gCidOffset =0;
- DWORD gNprocs = 0;
- PEPROCESS gDeletedEProcess = NULL;//typedef struct _EPROCESS w2k_def_jr.h
- PEPROCESS gBakDeletedEProcess = NULL;
- #define SYSNAME "System"
- #define IS_WINDOWS2000()
- ((gOsMajorVersion == 5) && (gOsMinorVersion == 0))//??
- #define IS_WINDOWSXP()
- ((gOsMajorVersion == 5) && (gOsMinorVersion == 1))
- #define IS_WINDOWSXP_OR_LATER()
- (((gOsMajorVersion == 5) && (gOsMinorVersion >= 1)) ||
- (gOsMajorVersion > 5))
- #define IS_WINDOWSDOTNET_OR_LATER()
- (((gOsMajorVersion == 5) && (gOsMinorVersion >= 2)) ||
- (gOsMajorVersion > 5))
- NTKERNELAPI//?????????????????
- NTSTATUS
- NTAPI
- PsLookupThreadByThreadId (
- IN PVOID UniqueThreadId,
- OUT PETHREAD *Thread
- );
- NTSTATUS PsLookupProcessByProcessId(IN ULONG ulProcId, OUT PEPROCESS * pEProcess);
- void insertProc (PKLISTER_PROCINFO obAddr);
- void deleteProc (PEPROCESS obAddr);
- ULONG ProcessNameOffset = 0;///////////////////?????
- ULONG GetProcessNameOffset()// 得到进程名位 <<内核级HOOK的几种实现与应用>>
- {
- PEPROCESS curproc;
- int i;
- curproc = PsGetCurrentProcess();//PsGetCurrentProcess returns a pointer to the process of the current thread
- //
- // Scan for 12KB, hopping the KPEB never grows that big!
- //
- for( i = 0; i < 3*PAGE_SIZE; i++ ) {
- if( !strncmp( SYSNAME, (PCHAR) curproc + i, strlen(SYSNAME) )) {//#define SYSNAME "System"
- return i;//返回
- }
- }
- //
- // Name not found - oh, well
- //
- return 0;
- }
- ///////////////////////////////////////////////////////////////////
- //说明:
- //ProcessData is the function that gets the required data from the _KTHREAD, _ETHREAD and _EPROCESS structures and stores the data in a separate chaining hash table.
- //I am using the threads virtual memory address as the key to the hash table (first I used the thread ID, however in theory one could modify a malicious thread to have the same ID as some non-malicious thread) and a thread is only inserted once during its lifetime. Because I use the threads memory address as the key I have to make sure that the entry is removed from the table when the thread is terminated since a new thread can be allocated to the same memory address. When a thread is terminating it signals this by setting the Terminated flag in the CrossThreadFlags entry which is part of the _ETHREAD structure. The ProcessData function looks like this:
- void __stdcall ProcessData(DWORD * pEthread)//请查看xfocus:<<Detecting Hidden Processes by Hooking the SwapContext Function>>
- {
- // NOTICE: WinDbg gives offsets in BYTEs, we use DWORDS
- DWORD * pEprocess = (DWORD *)*(DWORD *)((PUCHAR)pEthread+gThreadsProcessOffset);//(pEthread->ThreadsProcess);
- DWORD * pCid = (DWORD *)((PUCHAR)pEthread+gCidOffset);//&(pEthread->Cid);
- DWORD key;
- KLISTER_PROCINFO data;
- DbgPrint("ProcessDatan");//jution
- /*PETHREAD pethread = (PETHREAD)pEthread;
- DWORD * ServiceTable =(DWORD *)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceTable;
- DWORD nServiceLimit = pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceLimit;*/
- /*insertServTable ((int)*(pCid+1);,
- (int)pEthread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceTable,
- (int)pEthread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceLimit);*/
- if(gDeletedEProcess!= NULL)
- {
- gBakDeletedEProcess = gDeletedEProcess;
- deleteProc(gDeletedEProcess);
- gDeletedEProcess = NULL;
- return;
- }
- if((DWORD*)gBakDeletedEProcess == pEprocess) return;
- // FIXME: A thread could be hidden by setting threadsProcess or CID
- // field as NULL!
- if (pEprocess != NULL && pCid != NULL)
- {
- data.obAddr = (DWORD)pEprocess;
- data.pid = *(pCid);
- data.tid = *(pCid+1);
- //data.imageName = (pEprocess->ImageFileName);
- //strncpy(data.imageName,pEprocess->ImageFileName,16);
- strncpy(data.name,((PUCHAR)pEprocess+ProcessNameOffset),16);
- DbgPrint("pID:%08x tID:%8x %sn",data.pid,data.tid,data.name);
- insertProc(&data);
- // The thread is terminated so remove it from the
- // hashtable.
- /*if (*(pEthread + offsets.crossThreadFlags) & 1)
- {
- Remove(key, pHashTable);
- }
- else
- {
- Insert(key, &data, pHashTable);
- }*/
- }
- }
- PBYTE GoBackAddr = NULL;
- PBYTE ChangAddr = NULL;
- PBYTE CallContextAddr = NULL;
- DWORD CallContextOffset = 0;
- __declspec(naked) VOID HookSwap()/////请查看pjf的《线程调度的监视》
- { DbgPrint("HookSwap()n");//jution ok
- _asm
- {
- pushad
- pushfd
- cli
- }
- DbgPrint("Switch out!n");//jution ????????????
- _asm
- {
- // EDI holds the thread whose context we will switch out.
- push edi
- call ProcessData
- }
- DbgPrint("Switch in!n");//jution
- _asm
- {
- // ESI holds the thread whose context we will switch in.
- push esi
- call ProcessData
- }
- _asm
- {
- sti
- popfd
- popad
- }
- _asm jmp DWORD PTR[GoBackAddr]
- }
- NTSYSAPI WORD NtBuildNumber;
- PCHAR GetSwapAddr()
- {
- PCHAR res = 0;
- NTSTATUS Status;
- PETHREAD Thread;
- if (NtBuildNumber <= 2195)
- Status = PsLookupThreadByThreadId((PVOID)4, &(PETHREAD)Thread);
- else
- Status = PsLookupThreadByThreadId((PVOID)8, &(PETHREAD)Thread);
- if (NT_SUCCESS(Status))
- {
- if (MmIsAddressValid(Thread))
- {
- res = (PCHAR)(Thread->Tcb.KernelStack);
- if(IS_WINDOWSDOTNET_OR_LATER())
- res = (PCHAR)*(DWORD*)((PCHAR)Thread+0x20);//add for win2003 Tcb.KernelStack
- }
- if (MmIsAddressValid(res+8))
- //res = *(PULONG(res+8));
- {
- _asm
- {
- mov eax,res
- add eax,8
- mov eax,[eax]
- mov res,eax
- }
- }
- else
- {
- res = 0;
- return NULL;
- }
- }
- _asm
- {
- mov eax,res
- sub eax,5
- mov ChangAddr,eax
- mov edx,[eax+1]
- mov CallContextOffset,edx
- add eax,edx
- add eax,5
- mov GoBackAddr,eax
- mov res,eax
- }
- return res;
- }
- BOOL HookSwapFunction(BOOL flag)
- {DbgPrint("HookSwapFunction()n");//jution
- if (flag == TRUE)/////////////////
- {
- KIRQL OldIrql=0;
- DWORD NewOffset;//HookSwap-ChangAddr-5;
- _asm
- {
- mov eax,HookSwap
- mov edx,ChangAddr
- sub eax,edx
- sub eax,5
- mov NewOffset,eax
- }
- DbgPrint("HookSwapFunction333");//jution
- PAGED_CODE()
- ASSERT(KeGetCurrentIrql()<=DISPATCH_LEVEL);
- KeRaiseIrql(2,&OldIrql);//HIGH_LEVEL
- //Bug Check 0x50: PAGE_FAULT_IN_NONPAGED_AREA
- //The PAGE_FAULT_IN_NONPAGED_AREA bug check has a value of 0x00000050. This indicates that invalid system memory has been referenced.
- //驱动程序通过调用ExAllocatePool获得的非页式系统空间内存
- _asm
- {
- mov eax,ChangAddr
- push NewOffset//??????????????????????????????????????????????????????????????????????
- pop dword ptr[eax+1]//??????????????????????????????????????????????????????????????????????
- //mov edx,NewOffset
- // mov dword ptr[eax+1],edx//????????????????????????????
- }
- KeLowerIrql(OldIrql);
- }
- //Bug Check 0xD1: DRIVER_IRQL_NOT_LESS_OR_EQUAL
- else
- {
- KIRQL OldIrql=0;
- KeRaiseIrql(2,&OldIrql);///HIGH_LEVEL
- _asm
- {
- mov eax,ChangAddr
- push CallContextOffset
- pop dword ptr[eax+1]
- }
- KeLowerIrql(OldIrql);
- DbgPrint("HookSwapFunctionFALSE");//jution
- }
- }
- void insertProc (PKLISTER_PROCINFO pdata)
- {
- DWORD i;
- KIRQL OldIrql=0;
- if( gNprocs >= MAX_PROCS)
- return;
- KeRaiseIrql(2,&OldIrql);////提升当前IRQL,防止被中断;
- DbgPrint("insertProc");//jution
- for (i = 0; i < gNprocs; i++)
- if (procs[i].obAddr == pdata->obAddr) return;
- procs [gNprocs].pid = pdata->pid;
- procs [gNprocs].tid = pdata->tid;
- procs [gNprocs].obAddr = pdata->obAddr;
- strncpy (procs [gNprocs].name, pdata->name, 16);
- gNprocs++;
- KeLowerIrql(OldIrql);
- }
- void deleteProc (PEPROCESS obAddr)
- {
- DWORD i;
- KIRQL OldIrql=0;
- if( gNprocs == 0)
- return;
- DbgPrint("deleteProc");//jution
- KeRaiseIrql(2,&OldIrql);
- for (i = 0; i < gNprocs; i++)
- if (procs[i].obAddr == (int)obAddr)
- {
- procs [i].pid = 0;
- procs [i].tid = 0;
- procs [i].obAddr = 0;
- RtlZeroMemory (procs [i].name, 16);
- break;
- }
- if(i == gNprocs) return;
- memmove(&procs[i],&procs[i+1],(gNprocs-i-1)*sizeof(KLISTER_PROCINFO));
- procs [gNprocs-1].pid = 0;
- procs [gNprocs-1].tid = 0;
- procs [gNprocs-1].obAddr = 0;
- RtlZeroMemory (procs [gNprocs-1].name, 16);
- gNprocs--;
- KeLowerIrql(OldIrql);
- }
- void insertServTable (int tid, int addr, int n) {// DWORD addr jution change
- DWORD i;
- DbgPrint("insertServTable");//jution
- for (i = 0; i < nSrvTables; i++)
- if (SrvTables[i].addr==addr) {///d:ntddkjutionkilsterkmodule.c(328) : warning C4018: '==' : signed/unsigned mismatch
- // we ignore buffer overflow here ;)
- // if (SrvTables[i].nthreads < MAX_THREADS)
- // SrvTables[i].threads[SrvTables[i].nthreads] = tid;
- SrvTables[i].n = n;
- SrvTables[i].nthreads++;
- return;
- }
- SrvTables[nSrvTables].addr = addr;
- SrvTables[i].n = n;
- //SrvTables[nSrvTables].threads[0] = tid;
- SrvTables[nSrvTables].nthreads = 1;
- nSrvTables++;
- }
- PEPROCESS processObject (PETHREAD ethread) {
- return (PEPROCESS)(ethread->Tcb.ApcState.Process);
- }
- /*
- char* processName (PEPROCESS eprocess) {
- return &eprocess->ImageFileName[0];
- }
- */
- void createProcList () {
- int i;
- PVOID obj;
- PETHREAD pethread;
- gNprocs = 0;
- nSrvTables = 0;
- DbgPrint("createProcList");//jution
- /*for (obj = pKiWaitInListHead->Flink;
- obj && obj != pKiWaitInListHead; obj = ((PLIST_ENTRY)obj)->Flink) {
- pethread = (PETHREAD) ((char*)obj - WAITLIST_OFFSET);
- insertServTable ((int)pethread->Cid.UniqueThread,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceTable,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceLimit);
- insertProc (processObject (pethread));
- }
- for (obj = pKiWaitOutListHead->Flink;
- obj && obj != pKiWaitOutListHead; obj = ((PLIST_ENTRY)obj)->Flink) {
- pethread = (PETHREAD) ((char*)obj - WAITLIST_OFFSET);
- insertServTable ((int)pethread->Cid.UniqueThread,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceTable,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceLimit);
- insertProc (processObject (pethread));
- }
- for (i = 0; i < 32; i++)
- for (obj = pKiDispatcherReadyListHead[i].Flink;
- obj != &pKiDispatcherReadyListHead[i];
- obj = ((PLIST_ENTRY)obj)->Flink) {
- pethread = (PETHREAD) ((char*)obj - WAITLIST_OFFSET);
- insertServTable ((int)pethread->Cid.UniqueThread,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceTable,
- (int)pethread->Tcb.pServiceDescriptorTable->ntoskrnl.ServiceLimit);
- insertProc (processObject (pethread));
- }*/
- }
- PIDTGATE readIDT() {
- IDTR idtr;
- __asm {
- sidt idtr;
- }
- return(PIDTGATE) idtr.base;
- }
- NTSTATUS klisterDeviceControl(
- IN PDEVICE_OBJECT pDeviceObject,
- IN ULONG IoControlCode,
- IN PVOID pInputBuffer,
- IN ULONG InputBufferLength,
- OUT PVOID pOutputBuffer,
- IN ULONG OutputBufferLength,
- OUT PIO_STATUS_BLOCK IoStatus
- )
- {
- DWORD maxnproc, maxTbls, n, i, STn;
- PKLISTER_INIT pkl_init;
- IoStatus->Status = STATUS_SUCCESS;
- IoStatus->Information = 0;
- switch ( IoControlCode )
- {
- case IOCTL_KLISTER_INIT:
- if ((InputBufferLength != sizeof(KLISTER_INIT) ) || (pInputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- pkl_init = (PKLISTER_INIT) pInputBuffer;
- /*pKiWaitInListHead = (PLIST_ENTRY) pkl_init->pKiWaitInListHead_addr;
- pKiWaitOutListHead = (PLIST_ENTRY) pkl_init->pKiWaitOutListHead_addr;
- pKiDispatcherReadyListHead = (PLIST_ENTRY) pkl_init->pKiDispatcherReadyListHead_addr;
- KdPrint ((" pKiWaitInListHead: %#xn", pKiWaitInListHead));
- KdPrint ((" pKiWaitOutListHead: %#xn", pKiWaitOutListHead));
- KdPrint ((" pKiDispatcherReadyListHead: %#xn", pKiDispatcherReadyListHead));*/
- /*for (i = 0; i < MAX_PROCS; i++)
- procs[i].pid = 0;*/
- //RtlZeroMemory(procs,sizeof(KLISTER_PROCINFO)*MAX_PROCS);
- for (i = 0; i < MAX_SERVICETABLES; i++)
- SrvTables[i].addr = 0;
- DbgPrint("IOCTL_KLISTER_INITn");//jution
- break;
- case IOCTL_KLISTER_LISTPROC:
- if ((OutputBufferLength < sizeof (KLISTER_PROCINFO)) || (pOutputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- DbgPrint("IOCTL_KLISTER_LISTPROCn");//jution
- maxnproc = OutputBufferLength/sizeof (KLISTER_PROCINFO);
- //createProcList();
- if (gNprocs > maxnproc)
- n = maxnproc*sizeof (KLISTER_PROCINFO);
- else
- n = gNprocs * sizeof (KLISTER_PROCINFO);
- if (OutputBufferLength < n)
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- memcpy (pOutputBuffer, (PVOID) &procs, n);
- IoStatus->Information = n;
- break;
- case IOCTL_KLISTER_DUMP_IDT:
- if ((OutputBufferLength < sizeof (IDTGATE)*IDT_NGATES)
- || (pOutputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- n = sizeof (IDTGATE) * IDT_NGATES;
- memcpy (pOutputBuffer, (PVOID) readIDT(), n);
- IoStatus->Information = n;
- break;
- case IOCTL_KLISTER_FIND_ST:
- if ((OutputBufferLength < sizeof (ServiceTableInfo))
- || (pOutputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- maxTbls = OutputBufferLength/sizeof(ServiceTableInfo);
- createProcList();
- if (nSrvTables > maxTbls) n = maxTbls*sizeof (ServiceTableInfo);
- else n = nSrvTables * sizeof (ServiceTableInfo);
- memcpy (pOutputBuffer, (PVOID) SrvTables, n);
- IoStatus->Information = n;
- break;
- case IOCTL_KLISTER_DUMP_ST:
- if ((InputBufferLength != sizeof(int) ) || (pInputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- if ((OutputBufferLength < sizeof (int))
- || (pOutputBuffer == NULL))
- {
- IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
- break;
- }
- STn = *(int*)pInputBuffer; // no of ST to dump
- //KdPrint ((" STn: %#xn", STn));
- DbgPrint(" STn: %#xn"); //jution add
- n = sizeof (int) * (SrvTables[STn].n);
- KdPrint(("klister: n = %dn", n));
- n = (n > OutputBufferLength) ? OutputBufferLength : n;
- KdPrint(("klister: n = %dn", n));
- memcpy (pOutputBuffer, (PVOID) SrvTables[STn].addr, n);
- KdPrint (("memcpy (pOutputBuffer, (PVOID) SrvTables[STn].addr, n)"));
- KdPrint (("pOutputBuffer = %#xn", pOutputBuffer));
- KdPrint (("SrvTables[STn].addr = %#xn", SrvTables[STn].addr));
- IoStatus->Information = n;
- break;
- default:
- IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST;
- break;
- }
- return IoStatus->Status;
- }
- NTSTATUS klisterDispatch(
- IN PDEVICE_OBJECT pDeviceObject,
- IN PIRP pIrp
- )
- {
- PIO_STACK_LOCATION irpStack;
- PVOID pInputBuffer;
- PVOID pOutputBuffer;
- ULONG inputBufferLength;
- ULONG outputBufferLength;
- ULONG ioControlCode;
- NTSTATUS ntstatus;
- irpStack = IoGetCurrentIrpStackLocation (pIrp);
- pInputBuffer = pIrp->AssociatedIrp.SystemBuffer;
- inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
- pOutputBuffer = pIrp->AssociatedIrp.SystemBuffer;
- outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
- ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
- ntstatus = pIrp->IoStatus.Status = STATUS_SUCCESS;
- pIrp->IoStatus.Information = 0;
- switch (irpStack->MajorFunction) {
- case IRP_MJ_CREATE:
- break;
- case IRP_MJ_SHUTDOWN:
- break;
- case IRP_MJ_CLOSE:
- break;
- case IRP_MJ_DEVICE_CONTROL:
- ntstatus = klisterDeviceControl(
- pDeviceObject, ioControlCode,
- pInputBuffer, inputBufferLength,
- pOutputBuffer, outputBufferLength,
- &pIrp->IoStatus);
- break;
- }
- IoCompleteRequest( pIrp, IO_NO_INCREMENT );
- return ntstatus;
- }
- VOID ProcessCreateMon ( IN HANDLE hParentId, IN HANDLE PId,IN BOOLEAN bCreate )
- {
- PEPROCESS EProcess;
- ULONG ulCurrentProcessId;
- LPTSTR lpCurProc;
- NTSTATUS status;
- status = PsLookupProcessByProcessId( (ULONG)PId, &EProcess);
- if (!NT_SUCCESS( status ))
- {
- DbgPrint("PsLookupProcessByProcessId()n");////根据PID得到进程名
- return ;
- }
- if ( bCreate )
- {
- lpCurProc = (LPTSTR)EProcess;
- lpCurProc = lpCurProc + ProcessNameOffset;
- DbgPrint( "CREATE PROCESS = PROCESS NAME: %s , PROCESS PARENTID: %d, PROCESS ID: %d, PROCESS ADDRESS %x:n",
- lpCurProc,
- hParentId,
- PId,
- EProcess );
- }
- else
- {
- DbgPrint( "TERMINATED == PROCESS ID: %dn", PId);
- gDeletedEProcess = EProcess;
- }
- }
- NTSTATUS klisterUnload(IN PDRIVER_OBJECT pDriverObject)
- {
- UNICODE_STRING devLinkUnicd;
- PDEVICE_OBJECT pObj;
- pObj = pDriverObject->DeviceObject;
- if (GoBackAddr)//PBYTE GoBackAddr = NULL;
- HookSwapFunction(FALSE);
- PsSetCreateProcessNotifyRoutine(ProcessCreateMon, TRUE);
- if (pObj != NULL)
- {
- RtlInitUnicodeString( &devLinkUnicd, devLink );
- IoDeleteSymbolicLink( &devLinkUnicd );
- IoDeleteDevice( pDriverObject->DeviceObject );
- return STATUS_SUCCESS;
- }
- return STATUS_SUCCESS;
- }
- NTSTATUS DriverEntry(
- IN PDRIVER_OBJECT pDriverObject,
- IN PUNICODE_STRING pRegistryPath
- )
- {
- NTSTATUS ntStatus;
- UNICODE_STRING devNameUnicd;
- UNICODE_STRING devLinkUnicd;
- PDEVICE_OBJECT pDevice;
- RtlInitUnicodeString (&devNameUnicd,
- devName );//const WCHAR devName[] = L"\Device\Myklister";
- RtlInitUnicodeString (&devLinkUnicd,
- devLink );//const WCHAR devLink[] = L"\??\Myklister";
- //WDM驱动程序可以调用IoCreateDevice函数创建设备对象,但设备对象的管理则由I/O管理器负责。
- //DriverObject(PDRIVER_OBJECT)指向与该设备对象相关的驱动程序对象,通常就是调用IoCreateDevice函数创建该设备对象的驱动程序对象。过滤器驱动程序有时需要用这个指针来寻找被过滤设备的驱动程序对象,然后查看其MajorFunction表项。
- ntStatus = IoCreateDevice ( pDriverObject,
- 0,
- &devNameUnicd,
- FILE_DEVICE_KLISTER,//?????????
- 0,
- TRUE,
- &pDevice );
- if( !NT_SUCCESS(ntStatus)) {
- DbgPrint(("klister: cannot create device.n"));
- return ntStatus;
- }
- ntStatus = IoCreateSymbolicLink (&devLinkUnicd, &devNameUnicd);//在WDM驱动程序中创建一个符号连接,可以调用IoCreateSymbolicLink函数
- if( !NT_SUCCESS(ntStatus)) {
- DbgPrint(("klister: cannot create symlink to device.n"));
- return ntStatus;
- }
- // else DbgPrint(("klister:create symlink to device.n"));////////////////jution add
- DbgPrint("klister:create symlink to device()n");
- pDriverObject->MajorFunction[IRP_MJ_SHUTDOWN]=
- pDriverObject->MajorFunction[IRP_MJ_CREATE]=
- pDriverObject->MajorFunction[IRP_MJ_CLOSE]=
- pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = klisterDispatch;
- pDriverObject->DriverUnload=klisterUnload;
- //add by sjl
- PsGetVersion( &gOsMajorVersion,
- &gOsMinorVersion,
- NULL,
- NULL );
- if(IS_WINDOWS2000())
- {
- gThreadsProcessOffset = 0x22c;
- gCidOffset = 0x1e0;
- DbgPrint("IS_WINDOWS2000n");
- }
- else if(IS_WINDOWSXP())
- {
- gThreadsProcessOffset =0x220;//////////
- gCidOffset = 0x1ec;
- }
- else if(IS_WINDOWSDOTNET_OR_LATER())
- {
- gThreadsProcessOffset =0x228;
- gCidOffset = 0x1f4;
- }
- ProcessNameOffset=GetProcessNameOffset();//// 得到进程名位移
- GetSwapAddr();//1111111111111111
- DbgPrint("oooooooooooooo");
- if (GoBackAddr){
- DbgPrint("uuuuuuuu");
- HookSwapFunction(TRUE);//2222222222222222
- }
- //HookSwap();
- else DbgPrint("jjjjjjjjjjjjjjjjjjjj");
- ntStatus = PsSetCreateProcessNotifyRoutine(ProcessCreateMon, FALSE);
- //让用户注册系统建立与删除进程或线程时调用回调函数的Fully Documented例程,
- //知道它们就是Mark Russinovich的NTPMON的实现的最主要的两个函数吧
- if (!NT_SUCCESS( ntStatus ))
- {
- DbgPrint("PsSetCreateProcessNotifyRoutine()n");
- return ntStatus;
- }
- DbgPrint("klister load succesfully1111n");
- KdPrint (("klister load succesfullyn"));
- return STATUS_SUCCESS;
- }
- //修改自KLISTER所以相关的头文件从KLISTER里找吧