最近在写驱动程序的时候,遇到一个奇怪的问题:
PCWSTR GetCurrentProcessFileName()
{
DWORD dwAddress = (DWORD)PsGetCurrentProcess();
if(KeGetCurrentIrql() != PASSIVE_LEVEL) return NULL;
if(dwAddress == 0 || dwAddress == 0xFFFFFFFF)return NULL;
dwAddress += 0x1B0;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
dwAddress += 0x10;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
dwAddress += 0x3C;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
KdPrint(("Current Process Full Path Name: %ws\n", (PCWSTR)dwAddress));
return (PCWSTR)dwAddress;
}
上面这个函数想必大家都不陌生,在xp/2k的驱动中可以得到当前进程的全路径,然而我在测试的时候发现,其实有些情况会出错,导致我的虚拟机直接蓝屏,但是我想不通问什么?
上面红色的代码,是最终得到ImageFile这个Unicode字符串地址的代码,在这之前的一步,dwAddress 一般情况下就是0x20000,而执行完红色的代码后,dwAddress 大概就是0x2yyyy的样子,反正比0x20000要大。
然而有些时候,执行完红色的代码后,得到的好像不是0x2yyyy,而是0xyyyy,这样就没有得到ImageFile的地址,而是和0x20000的偏移量。
——虽然我没有想清楚问什么,windows会这样搞!但是这里可以对上面的函数做点修改,暂时得到正确的函数体来得到正确的ImageFile的地址,我修改的函数如下:
PCWSTR GetCurrentProcessFileName()
{
DWORD dwAddress = (DWORD)PsGetCurrentProcess();
DWORD dwAddress1 ;
if(KeGetCurrentIrql() != PASSIVE_LEVEL)
return NULL;
if(dwAddress == 0 || dwAddress == 0xFFFFFFFF)
return NULL;
dwAddress += 0x1B0;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
dwAddress += 0x10;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
dwAddress1=dwAddress;//20000
dwAddress += 0x3C;
if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
if(dwAddress<dwAddress1)
dwAddress=dwAddress+dwAddress1;
KdPrint(("Current Process Full Path Name: %ws\n", (PCWSTR)dwAddress));
return (PCWSTR)dwAddress;
}
希望大牛们帮我想想,不胜感激!
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)