开头写什么好呢,写下设计这个程序驱动的初衷吧。
设计驱动的本意是监视机房里学生是否有开非法软件,如果是,就杀掉。
先看代码
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegisterString)
{
NTSTATUS status = STATUS_SUCCESS;
//驱动卸载处理
pDriverObj->DriverUnload = DriverUnload;
status = PsSetCreateProcessNotifyRoutine(ProcessMonitorCallback, FALSE);
return status;
}
入口处注册了一个进程回调函数,这个函数会在每个进程创建完毕以后调用一次。
卸载函数
void DriverUnload(PDRIVER_OBJECT pDriveObj)
{
//取消监视
PsSetCreateProcessNotifyRoutine(ProcessMonitorCallback, TRUE);
DbgPrint("driver unloaded ...\n");
}
回调函数
VOID ProcessMonitorCallback(HANDLE hParentId, HANDLE hProcessId, BOOLEAN bCreate)
{
ULONG BufferSize = 10;//定义一个读取内存的长度,由于是演示版本 我就定义得很短。
NTSTATUS status;
BOOLEAN bIsAttached = FALSE;//定义一个附加判断标志
PPEB pPeb = NULL;//这个结构也未公开,可以自己申明,也可以网上查资料
PKAPC_STATE KAPC = (PKAPC_STATE)ExAllocatePool(NonPagedPool, sizeof(PKAPC_STATE));//申请一个KAPC结构,并对这个结构分配内存
if (bCreate)//如果传进来的参数是ture 以为着是进程创建,反之是进程关闭,这里我们只用处理创建
{
PEPROCESS Process = NULL;//这个结构体并未公开,但是WDM有申明,可以直接用
status = PsLookupProcessByProcessId(hProcessId, &Process);//通过回调函数参数2 获得EPROCESS结构
pPeb = PsGetProcessPeb(Process);//这个函数未公开,但是网上有资料,自己申明下就可以直接用
KeStackAttachProcess(Process, KAPC);
bIsAttached = TRUE;//附加进程,并改变标志
__try
{
//peb结构+0x008处为EPROCESS所属进程的基址
ProbeForRead((PVOID)(((PULONG)((PUCHAR)pPeb + 0x008))), (SIZE_T)BufferSize, 1);
DbgPrint("add is-%x..\n",baseadd);
....do someting//例如copy基址+偏移量处一段长度的内存,然后和特征库进行对比,如果匹配,则是非法进程,杀死进程
//这里我只是简单的把基址打印出来
}
__except (1)
{
DbgPrint("read process memoryfailed...\n");
}
if (bIsAttached != FALSE)
{
//释放KAPC的内存,清除peb结构体
KeUnstackDetachProcess(KAPC);
memset(pPeb, 0, sizeof(PPEB));
pPeb = NULL;
ExFreePool(KAPC);
DbgPrint("OVER.\n");
}
}
}
以上就是代码,在XP下能捕获任何进程的创建,并能打印出出基址,我修改代码 读取一段内存,也很正常
但在win7 32位下,加载驱动 ,过一会儿就蓝屏了(但是能读到部分程序的内存,例如记事本),一开始我觉得可能是读取内存导致的错误,后来我改掉代码,只附加R3进程,而且附加以后只打印一段字符串然后马上解除attach 可是加载驱动没多久还是一样蓝屏。
所以在此贴出代码,希望高手能分析下win7下蓝屏的原因
用到的头文件
#include <Ntifs.h>
#include <WinDef.h>
用到的PEB结构体
typedef struct _PEB
{
UCHAR InheritedAddressSpace;
UCHAR ReadImageFileExecOptions;
UCHAR BeingDebugged;
UCHAR BitField;
ULONG ImageUsesLargePages : 1;
ULONG IsProtectedProcess : 1;
ULONG IsLegacyProcess : 1;
ULONG IsImageDynamicallyRelocated : 1;
ULONG SpareBits : 4;
PVOID Mutant;
PVOID ImageBaseAddress;
PVOID SubSystemData;
PVOID ProcessHeap;
PVOID AtlThunkSListPtr;
PVOID IFEOKey;
ULONG CrossProcessFlags;
ULONG ProcessInJob : 1;
ULONG ProcessInitializing : 1;
ULONG ReservedBits0 : 30;
union
{
PVOID KernelCallbackTable;
PVOID UserSharedInfoPtr;
};
ULONG SystemReserved[1];
ULONG SpareUlong;
ULONG TlsExpansionCounter;
PVOID TlsBitmap;
ULONG TlsBitmapBits[2];
PVOID ReadOnlySharedMemoryBase;
PVOID HotpatchInformation;
VOID * * ReadOnlyStaticServerData;
PVOID AnsiCodePageData;
PVOID OemCodePageData;
PVOID UnicodeCaseTableData;
ULONG NumberOfProcessors;
ULONG NtGlobalFlag;
LARGE_INTEGER CriticalSectionTimeout;
ULONG HeapSegmentReserve;
ULONG HeapSegmentCommit;
ULONG HeapDeCommitTotalFreeThreshold;
ULONG HeapDeCommitFreeBlockThreshold;
ULONG NumberOfHeaps;
ULONG MaximumNumberOfHeaps;
VOID * * ProcessHeaps;
PVOID GdiSharedHandleTable;
PVOID ProcessStarterHelper;
ULONG GdiDCAttributeList;
ULONG OSMajorVersion;
ULONG OSMinorVersion;
WORD OSBuildNumber;
WORD OSCSDVersion;
ULONG OSPlatformId;
ULONG ImageSubsystem;
ULONG ImageSubsystemMajorVersion;
ULONG ImageSubsystemMinorVersion;
ULONG ImageProcessAffinityMask;
ULONG GdiHandleBuffer[34];
PVOID PostProcessInitRoutine;
PVOID TlsExpansionBitmap;
ULONG TlsExpansionBitmapBits[32];
ULONG SessionId;
ULARGE_INTEGER AppCompatFlags;
ULARGE_INTEGER AppCompatFlagsUser;
PVOID pShimData;
PVOID AppCompatInfo;
UNICODE_STRING CSDVersion;
ULONG MinimumStackCommit;
LIST_ENTRY FlsListHead;
PVOID FlsBitmap;
ULONG FlsBitmapBits[4];
ULONG FlsHighIndex;
PVOID WerRegistrationData;
PVOID WerShipAssertPtr;
} PEB, *PPEB;
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课