首页
社区
课程
招聘
[求助]想做inline hook检测,将ntkrnlpa.exe映射到内存中,但是发现重定位表的数据和硬盘上的不一样,这个是这么回事啊?
发表于: 2011-5-13 13:26 7272

[求助]想做inline hook检测,将ntkrnlpa.exe映射到内存中,但是发现重定位表的数据和硬盘上的不一样,这个是这么回事啊?

2011-5-13 13:26
7272
【求助】想做inline hook检测,将ntkrnlpa.exe映射到内存中,但是发现重定位表的数据错了,和硬盘上的不一样,这个是这么回事啊?

是和STUD_PE工具查看的重定位表比较的。

首先,每一个重定位块数据的VirtualAddress和SizeOfBlock都是正确的,就是每一个的word数据错了。

摘要代码如下:

void XRelocate(ULONG uBaseAddress, LONG lDeltaOfBase, ULONG BaseRelocAddress)
{
  int i;
  WORD *pSection;
  int nCount;
  PIMAGE_BASE_RELOCATION pBaseRelocation;
  ULONG uOneNeedRelocateAddress;
  ULONG uOneRelocateValue;

  pBaseRelocation = (PIMAGE_BASE_RELOCATION)BaseRelocAddress;
  while (pBaseRelocation->VirtualAddress != 0)
  {
    nCount = (pBaseRelocation->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION))/sizeof(WORD)-1;
    dprintf("relocate address = [0x%08X], size = [0x%08X], number = [%d].", pBaseRelocation->VirtualAddress, pBaseRelocation->SizeOfBlock, nCount);
    if (pBaseRelocation->VirtualAddress == 0xDB000)
    {
      pSection = (WORD*)(pBaseRelocation+sizeof(IMAGE_BASE_RELOCATION));
      for(i=0; i<nCount; ++i)
      {
        dprintf("rva = [0x%04X].", (pSection[i));   [COLOR="Red"] //这里打印出的数据错了,和硬盘上的数据不一致[/COLOR]
      }
    }

    pBaseRelocation = (PIMAGE_BASE_RELOCATION)((ULONG)pBaseRelocation+pBaseRelocation->SizeOfBlock);
  }
}

void XInitApisOriginalCode(void)
{
  ANSI_STRING astrFullPathName;
  UNICODE_STRING ustrFullPathName;
  PUNICODE_STRING pstrFullPathName = &ustrFullPathName;

  ULONG i, j;
  HANDLE hFile, hSection, hMod;

  IMAGE_DOS_HEADER* pImageDosHeader; 
  IMAGE_OPTIONAL_HEADER* pImageOptionalHeader; 
  IMAGE_BASE_RELOCATION* pBaseRelocation; 

  DWORD* arrayOfFunctionAddresses; 
  DWORD* arrayOfFunctionNames; 
  WORD* arrayOfFunctionOrdinals; 
  DWORD functionOrdinal; 
  DWORD Base, functionAddress; 
  char* functionName;
  PVOID pBaseAddress = NULL; 
  SIZE_T size = 0;

  OBJECT_ATTRIBUTES oa = {sizeof oa, 0, pstrFullPathName, OBJ_CASE_INSENSITIVE}; 
  IO_STATUS_BLOCK iosb; 
  NTSTATUS ntStatus;

  XAPI *pApi = NULL;

  char szKernelName[MAXIMUM_FILENAME_LENGTH] = "\\Device\\HarddiskVolume1\\Windows\\System32\\";
  ULONG ntKernelBase = XGetSystemBase(szKernelName+strlen(szKernelName), MAXIMUM_FILENAME_LENGTH-strlen(szKernelName));
  dprintf(szKernelName);

  RtlInitAnsiString(&astrFullPathName, szKernelName);
  RtlAnsiStringToUnicodeString(&ustrFullPathName, &astrFullPathName, TRUE);

  ntStatus = ZwOpenFile(&hFile, FILE_EXECUTE | SYNCHRONIZE, &oa, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
  if (!NT_SUCCESS(ntStatus))
  {
    dprintf("Failed to ZwOpenFile. [0x%08X]\n", ntStatus);
    return;
  }

  oa.ObjectName = 0; 
  ntStatus = ZwCreateSection(&hSection, SECTION_ALL_ACCESS, &oa, 0, PAGE_EXECUTE, SEC_IMAGE, hFile);
  if (!NT_SUCCESS(ntStatus))
  {
    dprintf("Failed to ZwCreateSection. [0x%08X]\n", ntStatus);
    return;
  }

  ntStatus = ZwMapViewOfSection(hSection, NtCurrentProcess(), &pBaseAddress, 0, 1000, 0, &size, (SECTION_INHERIT)1, MEM_TOP_DOWN, PAGE_READWRITE);
  if (!NT_SUCCESS(ntStatus))
  {
    dprintf("Failed to ZwMapViewOfSection. [0x%08X]\n", ntStatus);
    return;
  }

  ntStatus = ZwClose(hFile);
  if (!NT_SUCCESS(ntStatus))
  {
    dprintf("Failed to ZwClose. [0x%08X]\n", ntStatus);
    return;
  }

  RtlFreeUnicodeString(&ustrFullPathName);

  hMod = pBaseAddress;
  pImageDosHeader = (IMAGE_DOS_HEADER *)hMod;
  pImageOptionalHeader = (IMAGE_OPTIONAL_HEADER *) ((BYTE*)hMod+pImageDosHeader->e_lfanew+24);  // 24是文件头的长度
  pBaseRelocation = (IMAGE_BASE_RELOCATION*)((BYTE*) hMod + pImageOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);

  dprintf("relocation address: [0x%08X], rva : [0x%08X]\n", pBaseRelocation, pImageOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
  
  XRelocate((ULONG)pBaseAddress, ntKernelBase-0x400000, (ULONG)pBaseRelocation);
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 53
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
准确的rva = pBaseRelocation->VirtualAddress + pSection[i] 这个我知道。

问题就是那个0x3XXX错的,有些情况下,开头的3也变成0了,而stud_pe看到的是正确的。

感觉在映射到内存的过程中,被改了。

代码中,0xDB000是 winxp下NtWriteVirtualMemory所在的块。

NtWriteVirtualMemory前5个字节需要被重定位,但是映射到内存中的这个重定位表中没有。文件上是有的。
2011-5-13 13:40
0
雪    币: 53
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
木有人理我,555~~~
2011-5-15 11:00
0
雪    币: 9
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
放个代码看看?
2011-5-21 01:34
0
游客
登录 | 注册 方可回帖
返回