在去除前面的那些东西之后,我们的内存块里面还有一堆MEM_MAPPED的内容,就像这样的块:
基址 | 分配基址 | 分配保护 | 大小 | 状态 | 保护 | 类型 |
00140000 | 00140000 | 00000002 PAGE_READONLY | 00001000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED |
MSDN里面这样解释MEM_MAPPED这种类型:
Indicates that the memory pages within the region are mapped into the view of a section.
很自然地我们想用GetMappedFileName来看看映射到这些内存区域的文件名,于是发现了下面几个块的确是做了文件映射:
基址 | 分配基址 | 分配保护 | 大小 | 状态 | 保护 | 类型 | 映射文件 |
00270000 | 00270000 | 00000002 PAGE_READONLY | 00016000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED | DeviceHarddiskVolume3WINDOWSsystem32unicode.nls |
00290000 | 00290000 | 00000002 PAGE_READONLY | 00041000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED | DeviceHarddiskVolume3WINDOWSsystem32locale.nls |
002e0000 | 002e0000 | 00000002 PAGE_READONLY | 00041000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED | DeviceHarddiskVolume3WINDOWSsystem32sortkey.nls |
00330000 | 00330000 | 00000002 PAGE_READONLY | 00006000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED | DeviceHarddiskVolume3WINDOWSsystem32sorttbls.nls |
003b0000 | 003b0000 | 00000002 PAGE_READONLY | 00003000 | 00001000 MEM_COMMIT | 00000002 PAGE_READONLY | 00040000 MEM_MAPPED | DeviceHarddiskVolume3WINDOWSsystem32ctype.nls |
那么其它东西是哪里来的?
使用GetLastError取错误信息:“error: 000003ee : 文件所在的卷已被外部改变,因此打开的文件不再有效。”,诡异得很!一怒之下使用UnmapViewOfFile将这些内存块给Unmap了,不幸的是程序就这样挂掉了!
写段代码查询一下当前打开的句柄,看看有没有file mapping的内容:
void enum_handles(HANDLE hProcess)
{
// 枚举已经打开的句柄,取其名称
HMODULE hNtDll = NULL; // nt.dll句柄
ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;
NTQUERYOBJECT NtQueryObject = NULL;
SYSTEM_HANDLE_INFORMATION *hInfo = NULL;
int nNumHandle = 0, i; // 句柄数量
NTSTATUS Status;
ULONG nSize, nCount;
char cBuffer[0x40000], cInfoBuffer[0x1000];
OBJECT_ALL_INFORMATION *pInfo;
OBJECT_NAME_INFORMATION* pName;
DWORD nId = GetProcessId(hProcess);
// 取导出函数
hNtDll = GetModuleHandle( "ntdll.dll" );
ZwQuerySystemInformation = ( ZWQUERYSYSTEMINFORMATION )GetProcAddress( hNtDll, "ZwQuerySystemInformation" );
NtQueryObject = ( NTQUERYOBJECT )GetProcAddress( hNtDll, "NtQueryObject" );
// 查询句柄信息
Status = ZwQuerySystemInformation(SystemHandleInformation,
cBuffer,
0x40000,
&nSize);
if(NT_SUCCESS(Status))
{
nNumHandle = *(PULONG)cBuffer;
hInfo = (SYSTEM_HANDLE_INFORMATION*)(cBuffer + 4);
nCount = 0;
for(i = 0; i < nNumHandle; i++)
{
if(hInfo[i].ProcessId != nId) continue;
Status = NtQueryObject(hInfo[i].Handle, ObjectAllInformation, cInfoBuffer, 0x1000, &nSize);
if(NT_SUCCESS(Status))
{
pInfo = (OBJECT_ALL_INFORMATION*)cInfoBuffer;
nCount++;
……………………….
}
}
}
}
网上有资料说NtQueryObject会让程序挂掉,但是没有发现有此现象,奇怪!上面的代码可以得到下面的句柄列表:
句柄 | 名称 | 类型 |
0c | DeviceHarddiskVolume5embedetoolsDebugbin | File |
24c | BaseNamedObjectsDBWinMutex | Mutant |
e54 | (null) | Section |
e60 | BaseNamedObjectsP???Lx.DAT!Memo | Section |
e64 | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGJ.IMD | File |
e68 | (null) | Section |
e94 | (null) | Section |
e98 | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGVR.IMD | File |
e9c | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGID.IMD | File |
ea0 | BaseNamedObjectsGlobalFileMappingIncmpIdxMSPYhld23qwe2527 | Section |
ea4 | (null) | Section |
ea8 | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGJ.IMD | File |
eb4 | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGC.IMD | File |
ed0 | BaseNamedObjectsLocalMutex2341MSPYhld23qwe2527 | Mutant |
ee4 | DeviceHarddiskVolume3Program FilesCommon FilesMicrosoft SharedIMEIMSC40APINTLGIX.IMD | File |
ee8 |