免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

关于DbgkpProcessDebugPortMutex 处理

上节课说了对DbgkpProcessDebugPortMutex的修改会导致调试器卡死的问题。由于时间原因导致了我没有写代码实现如何处理它,那么本帖就详细介绍下这个东西该如何处理才好。

首先我的思路是替换掉这个内核变量,也就是说只要引用这个变量的位置的地方我都要替换掉。
内核中引用DbgkpProcessDebugPortMutex这个内核变量的地方很多,win7下面大概有十几处,这就给替换带来了麻烦。如果自己搜索所有特征码进行定位的话工作量是比较繁琐的。
为了节省代码量,我想到一个有趣的办法。
大家应该还记得当初重载内核时候写的重定位基址的功能吧。对了,说到这里可能有人就明白怎么回事儿了。因为DbgkpProcessDebugPortMutex 也是要重定位的一个东西,所以我们就可以在重定位基址那个函数里面获取到内核中有多少个地方的代码要重定位DbgkpProcessDebugPortMutex ,那么就能确定内核中所有访问DbgkpProcessDebugPortMutex 的代码了。
好了,下面我们看看代码把。

首先是初始化函数:
  1. BOOLEAN InitCreateNewFastMutex(PDRIVER_OBJECT pDriver)
  2. {
  3.         ULONG        u_address;
  4.         PLDR_DATA_TABLE_ENTRY pkernel_module;

  5. //这是我随便找的一点定位DbgkpProcessDebugPortMutex的特征码
  6.         /*
  7.         83e67ac1 83bf9000000000  cmp     dword ptr [edi+90h],0
  8.         83e67ac8 0f848c010000    je      nt!DbgkCopyProcessDebugPort+0x1ba (83e67c5a)
  9.         83e67ace b101            mov     cl,1
  10.         83e67ad0 ff155c61c183    call    dword ptr [nt!_imp_KfRaiseIrql (83c1615c)]
  11.         83e67ad6 bea0c9d783      mov     esi,offset nt!DbgkpProcessDebugPortMutex (83d7c9a0)
  12.         */
  13.         SignatureInfo Signature[5] = {{0xBF,20},{0x0F,14},{0xB1, 8},{0xFF, 6},{0xBE, 0}};

  14.         pkernel_module = SearchDriver(pDriver,L"ntoskrnl.exe");
  15.         if (!MmIsAddressValid(pkernel_module)){
  16.                 return FALSE;
  17.         }

  18. //SearchAddress函数是自己写的一个定位特征码的函数
  19.         u_address = SearchAddress(\
  20.                 (ULONG)pkernel_module->DllBase,\
  21.                 pkernel_module->SizeOfImage,\
  22.                 Signature);

  23.         if (!MmIsAddressValid((PVOID)u_address))
  24.         {
  25. //得到了原始的DbgkpProcessDebugPortMutex
  26.                 g_DbgkpProcessDebugPortMutex = *(ULONG*)(u_address+1);
  27.         }

  28. //我们这里复制原始DbgkpProcessDebugPortMutex的内容到新定义的DbgkpProcessDebugPortMutex
  29.         RtlCopyMemory(g_new_fast_mutex,(PVOID)g_DbgkpProcessDebugPortMutex,0x20);
  30.         return TRUE;
  31. }
复制代码
那么怎么搜索所有的DbgkpProcessDebugPortMutex呢,我们看下面的代码:
  1. void RelocModule(PVOID pNewImage,PVOID pOrigImage)
  2. {
  3.         ULONG                                        uIndex,uCount;
  4.         ULONG                                        uRelocTableSize;
  5.         USHORT                                        TypeValue;
  6.         USHORT                                        *pwOffsetArrayAddress;
  7.         ULONG                                        uTypeOffsetArraySize;

  8.         ULONG                                        uRelocOffset;

  9.         ULONG                                        uRelocAddress;

  10.         PIMAGE_DOS_HEADER                pImageDosHeader;
  11.         PIMAGE_NT_HEADERS                pImageNtHeader;
  12.         IMAGE_DATA_DIRECTORY        ImageDataDirectory;
  13.         IMAGE_BASE_RELOCATION        *pImageBaseRelocation;

  14.         pImageDosHeader = (PIMAGE_DOS_HEADER)pNewImage;
  15.         pImageNtHeader = (PIMAGE_NT_HEADERS)((ULONG)pNewImage + pImageDosHeader->e_lfanew);

  16.         uRelocOffset = (ULONG)pOrigImage - pImageNtHeader->OptionalHeader.ImageBase;

  17.         ImageDataDirectory = pImageNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];

  18.         pImageBaseRelocation = (PIMAGE_BASE_RELOCATION)(ImageDataDirectory.VirtualAddress + (ULONG)pNewImage);
  19.         uRelocTableSize = ImageDataDirectory.Size;

  20.         uCount = 0;

  21.         while(uRelocTableSize)
  22.         {
  23.                 uTypeOffsetArraySize = (pImageBaseRelocation->SizeOfBlock - sizeof(ULONG)*2) / sizeof(USHORT);

  24.                 pwOffsetArrayAddress = pImageBaseRelocation->TypeOffset;
  25.                 for (uIndex = 0;uIndex<uTypeOffsetArraySize;uIndex++)
  26.                 {
  27.                         TypeValue = pwOffsetArrayAddress[uIndex];
  28.                         if (TypeValue>>12==IMAGE_REL_BASED_HIGHLOW)
  29.                         {
  30.                                 uRelocAddress = (TypeValue&0xfff)+pImageBaseRelocation->VirtualAddress + (ULONG)pNewImage;
  31.                                 if (!MmIsAddressValid((PVOID)uRelocAddress))
  32.                                 {
  33.                                         continue;
  34.                                 }

  35.                                 *(ULONG*)uRelocAddress += uRelocOffset;
  36. //g_fast_mutex_pointer是个数组,每个元素代表要修改的位置,我系统中所有用到DbgkpProcessDebugPortMutex不超过12个
  37.                                 if (*(ULONG*)uRelocAddress==g_DbgkpProcessDebugPortMutex&&uCount<12)
  38.                                 {
  39.                                         g_fast_mutex_pointer[uCount] = uRelocAddress;
  40.                                         uCount++;
  41.                                 }
  42.                         }
  43.                 }

  44.                 uRelocTableSize -= pImageBaseRelocation->SizeOfBlock;
  45.                 pImageBaseRelocation = (IMAGE_BASE_RELOCATION *)(\
  46.                         (ULONG)pImageBaseRelocation + pImageBaseRelocation->SizeOfBlock);
  47.         }
  48. }
复制代码
接下来就是修改和恢复了。我上所有代码吧。
  1. #include "ntddk.h"
  2. #include "ntimage.h"

  3. #define        __Max(a,b)        a>b?a:b

  4. typedef struct _LDR_DATA_TABLE_ENTRY {
  5.         LIST_ENTRY InLoadOrderLinks;
  6.         LIST_ENTRY InMemoryOrderLinks;
  7.         LIST_ENTRY InInitializationOrderLinks;
  8.         PVOID DllBase;
  9.         PVOID EntryPoint;
  10.         ULONG SizeOfImage;
  11.         UNICODE_STRING FullDllName;
  12.         UNICODE_STRING BaseDllName;
  13.         ULONG Flags;
  14.         USHORT LoadCount;
  15.         USHORT TlsIndex;
  16.         union {
  17.                 LIST_ENTRY HashLinks;
  18.                 struct {
  19.                         PVOID SectionPointer;
  20.                         ULONG CheckSum;
  21.                 };
  22.         };
  23.         union {
  24.                 struct {
  25.                         ULONG TimeDateStamp;
  26.                 };
  27.                 struct {
  28.                         PVOID LoadedImports;
  29.                 };
  30.         };
  31. } LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;

  32. typedef struct _SignatureInfo{
  33.         UCHAR        cSingature;
  34.         int                Offset;
  35. }SignatureInfo,*pSignatureInfo;

  36. //global
  37. ULONG g_DbgkpProcessDebugPortMutex;
  38. UCHAR g_new_fast_mutex[0x20];
  39. ULONG g_fast_mutex_pointer[12];

  40. PVOID g_new_kernel_pointer;

  41. ULONG g_kernel_increment;

  42. void PageProtectOn()
  43. {
  44.         __asm{//恢复内存保护  
  45.                 mov  eax,cr0
  46.                 or   eax,10000h
  47.                 mov  cr0,eax
  48.                 sti
  49.         }
  50. }

  51. void PageProtectOff()
  52. {
  53.         __asm{//去掉内存保护
  54.                 cli
  55.                 mov  eax,cr0
  56.                 and  eax,not 10000h
  57.                 mov  cr0,eax
  58.         }
  59. }

  60. PLDR_DATA_TABLE_ENTRY SearchDriver(PDRIVER_OBJECT pDriverObject,wchar_t *strDriverName)
  61. {
  62.         LDR_DATA_TABLE_ENTRY        *pDataTableEntry,*pTempDataTableEntry;
  63.         PLIST_ENTRY                                pList;
  64.         UNICODE_STRING                        usModuleName;

  65.         RtlInitUnicodeString(&usModuleName,strDriverName);

  66.         pDataTableEntry = (LDR_DATA_TABLE_ENTRY*)pDriverObject->DriverSection;
  67.         if (!pDataTableEntry)
  68.         {
  69.                 return 0;
  70.         }

  71.         pList = pDataTableEntry->InLoadOrderLinks.Flink;

  72.         while(pList!= &pDataTableEntry->InLoadOrderLinks)
  73.         {
  74.                 pTempDataTableEntry = (LDR_DATA_TABLE_ENTRY *)pList;

  75.                 KdPrint(("%wZ",&pTempDataTableEntry->BaseDllName));
  76.                 if (0==RtlCompareUnicodeString(&pTempDataTableEntry->BaseDllName,&usModuleName,FALSE))
  77.                 {
  78.                         return pTempDataTableEntry;
  79.                 }

  80.                 pList = pList->Flink;
  81.         }

  82.         return 0;
  83. }

  84. ULONG SearchAddress(ULONG uStartBase,ULONG uSearchLength,SignatureInfo DebugProtInfo[5])
  85. {
  86.         UCHAR *p;
  87.         ULONG uIndex1,uIndex2;

  88.         //ULONG uIndex;
  89.         PIMAGE_DOS_HEADER pDosHeader;
  90.         PIMAGE_NT_HEADERS pNtHeader;
  91.         PIMAGE_SECTION_HEADER pSecHeader;

  92.         if(!MmIsAddressValid((PVOID)uStartBase))
  93.         {        return 0;        }

  94.         pDosHeader = (PIMAGE_DOS_HEADER)uStartBase;
  95.         pNtHeader = (PIMAGE_NT_HEADERS)((ULONG)uStartBase+pDosHeader->e_lfanew);
  96.         pSecHeader = (PIMAGE_SECTION_HEADER)((ULONG)pNtHeader+sizeof(IMAGE_NT_HEADERS));

  97.         for (uIndex1 = 0;uIndex1<pNtHeader->FileHeader.NumberOfSections;uIndex1++)
  98.         {
  99.                 if (pSecHeader[uIndex1].Characteristics&0x60000000)
  100.                 {
  101.                         //可读可写的段
  102.                         //DbgPrint("SectionName:%s----0x%X----0x%X",pSecHeader[uIndex1].Name,\
  103.                         //        pSecHeader[uIndex1].Misc.VirtualSize,uStartBase+pSecHeader[uIndex1].VirtualAddress);
  104.                         p = (UCHAR*)uStartBase + pSecHeader[uIndex1].VirtualAddress;
  105.                         for (uIndex2 = 0;uIndex2<pSecHeader[uIndex1].Misc.VirtualSize;uIndex2++)
  106.                         {
  107.                                 if (!MmIsAddressValid((p-DebugProtInfo[0].Offset))||
  108.                                         !MmIsAddressValid((p-DebugProtInfo[4].Offset)))
  109.                                 {
  110.                                         p++;
  111.                                         continue;
  112.                                 }
  113.                                 __try{
  114.                                         if (*(p-DebugProtInfo[0].Offset)==DebugProtInfo[0].cSingature&&
  115.                                                 *(p-DebugProtInfo[1].Offset)==DebugProtInfo[1].cSingature&&
  116.                                                 *(p-DebugProtInfo[2].Offset)==DebugProtInfo[2].cSingature&&
  117.                                                 *(p-DebugProtInfo[3].Offset)==DebugProtInfo[3].cSingature&&
  118.                                                 *(p-DebugProtInfo[4].Offset)==DebugProtInfo[4].cSingature)
  119.                                         {
  120.                                                 return (ULONG)p;
  121.                                         }

  122.                                 }__except(EXCEPTION_EXECUTE_HANDLER){
  123.                                         DbgPrint("11111111111111111111111111111111");
  124.                                 }
  125.                                 p++;
  126.                         }
  127.                 }
  128.         }

  129.         return 0;
  130. }

  131. BOOLEAN InitCreateNewFastMutex(PDRIVER_OBJECT pDriver)
  132. {
  133.         ULONG        u_address;
  134.         PLDR_DATA_TABLE_ENTRY pkernel_module;

  135.         /*
  136.         83e67ac1 83bf9000000000  cmp     dword ptr [edi+90h],0
  137.         83e67ac8 0f848c010000    je      nt!DbgkCopyProcessDebugPort+0x1ba (83e67c5a)
  138.         83e67ace b101            mov     cl,1
  139.         83e67ad0 ff155c61c183    call    dword ptr [nt!_imp_KfRaiseIrql (83c1615c)]
  140.         83e67ad6 bea0c9d783      mov     esi,offset nt!DbgkpProcessDebugPortMutex (83d7c9a0)
  141.         */
  142.         SignatureInfo Signature[5] = {{0xBF,20},{0x0F,14},{0xB1, 8},{0xFF, 6},{0xBE, 0}};

  143.         pkernel_module = SearchDriver(pDriver,L"ntoskrnl.exe");
  144.         if (!MmIsAddressValid(pkernel_module)){
  145.                 return FALSE;
  146.         }

  147.         u_address = SearchAddress(\
  148.                 (ULONG)pkernel_module->DllBase,\
  149.                 pkernel_module->SizeOfImage,\
  150.                 Signature);

  151.         if (!MmIsAddressValid((PVOID)u_address))
  152.         {
  153.                 g_DbgkpProcessDebugPortMutex = *(ULONG*)(u_address+1);
  154.         }

  155.         RtlCopyMemory(g_new_fast_mutex,(PVOID)g_DbgkpProcessDebugPortMutex,0x20);
  156.         return TRUE;
  157. }

  158. VOID SetNewFastMutex(PDRIVER_OBJECT pDriver)
  159. {
  160.         ULONG        Index;
  161.         if (InitCreateNewFastMutex(pDriver))
  162.         {
  163.                 PageProtectOff();

  164.                 for (Index = 0;Index<11;Index++)
  165.                 {
  166.                         *(ULONG*)(g_fast_mutex_pointer[Index]) = (ULONG)g_new_fast_mutex;
  167.                         *(ULONG*)(g_fast_mutex_pointer[Index] - g_kernel_increment) = (ULONG)g_new_fast_mutex;
  168.                 }

  169.                 PageProtectOn();
  170.         }
  171. }

  172. VOID ResOldFastMutex()
  173. {
  174.         ULONG        Index;
  175.         if (g_DbgkpProcessDebugPortMutex)
  176.         {
  177.                 PageProtectOff();

  178.                 for (Index = 0;Index<11;Index++)
  179.                 {
  180.                         *(ULONG*)(g_fast_mutex_pointer[Index]) = g_DbgkpProcessDebugPortMutex;
  181.                         *(ULONG*)(g_fast_mutex_pointer[Index] - g_kernel_increment) = g_DbgkpProcessDebugPortMutex;
  182.                 }

  183.                 PageProtectOn();
  184.         }
  185. }

  186. //////////////////////////////////////////////////////////////////////////
  187. void RelocModule(PVOID pNewImage,PVOID pOrigImage)
  188. {
  189.         ULONG                                        uIndex,uCount;
  190.         ULONG                                        uRelocTableSize;
  191.         USHORT                                        TypeValue;
  192.         USHORT                                        *pwOffsetArrayAddress;
  193.         ULONG                                        uTypeOffsetArraySize;

  194.         ULONG                                        uRelocOffset;

  195.         ULONG                                        uRelocAddress;

  196.         PIMAGE_DOS_HEADER                pImageDosHeader;
  197.         PIMAGE_NT_HEADERS                pImageNtHeader;
  198.         IMAGE_DATA_DIRECTORY        ImageDataDirectory;
  199.         IMAGE_BASE_RELOCATION        *pImageBaseRelocation;

  200.         pImageDosHeader = (PIMAGE_DOS_HEADER)pNewImage;
  201.         pImageNtHeader = (PIMAGE_NT_HEADERS)((ULONG)pNewImage + pImageDosHeader->e_lfanew);

  202.         uRelocOffset = (ULONG)pOrigImage - pImageNtHeader->OptionalHeader.ImageBase;

  203.         ImageDataDirectory = pImageNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];

  204.         pImageBaseRelocation = (PIMAGE_BASE_RELOCATION)(ImageDataDirectory.VirtualAddress + (ULONG)pNewImage);
  205.         uRelocTableSize = ImageDataDirectory.Size;

  206.         uCount = 0;

  207.         while(uRelocTableSize)
  208.         {
  209.                 uTypeOffsetArraySize = (pImageBaseRelocation->SizeOfBlock - sizeof(ULONG)*2) / sizeof(USHORT);

  210.                 pwOffsetArrayAddress = pImageBaseRelocation->TypeOffset;
  211.                 for (uIndex = 0;uIndex<uTypeOffsetArraySize;uIndex++)
  212.                 {
  213.                         TypeValue = pwOffsetArrayAddress[uIndex];
  214.                         if (TypeValue>>12==IMAGE_REL_BASED_HIGHLOW)
  215.                         {
  216.                                 uRelocAddress = (TypeValue&0xfff)+pImageBaseRelocation->VirtualAddress + (ULONG)pNewImage;
  217.                                 if (!MmIsAddressValid((PVOID)uRelocAddress))
  218.                                 {
  219.                                         continue;
  220.                                 }

  221.                                 *(ULONG*)uRelocAddress += uRelocOffset;
  222.                                 if (*(ULONG*)uRelocAddress==g_DbgkpProcessDebugPortMutex&&uCount<12)
  223.                                 {
  224.                                         g_fast_mutex_pointer[uCount] = uRelocAddress;
  225.                                         uCount++;
  226.                                 }
  227.                         }
  228.                 }

  229.                 uRelocTableSize -= pImageBaseRelocation->SizeOfBlock;
  230.                 pImageBaseRelocation = (IMAGE_BASE_RELOCATION *)(\
  231.                         (ULONG)pImageBaseRelocation + pImageBaseRelocation->SizeOfBlock);
  232.         }
  233. }

  234. NTSTATUS ReadFileToMemory(wchar_t *strFileName,PVOID *lpVirtualAddress,PVOID pOrigImage)
  235. {
  236.         NTSTATUS                                Status;
  237.         HANDLE                                        hFile;
  238.         LARGE_INTEGER                        FileOffset;
  239.         UNICODE_STRING                        usFileName;
  240.         OBJECT_ATTRIBUTES                ObjAttr;
  241.         IO_STATUS_BLOCK                        IoStatusBlock;

  242.         IMAGE_DOS_HEADER                ImageDosHeader;
  243.         IMAGE_NT_HEADERS                ImageNtHeader;
  244.         IMAGE_SECTION_HEADER        *pImageSectionHeader;

  245.         ULONG                                        uIndex;
  246.         PVOID                                        lpVirtualPointer;
  247.         ULONG                                        SecVirtualAddress,SizeOfSection;
  248.         ULONG                                        PointerToRawData;

  249.         if (!MmIsAddressValid(strFileName))
  250.         {
  251.                 return STATUS_UNSUCCESSFUL;
  252.         }

  253.         RtlInitUnicodeString(&usFileName,strFileName);

  254.         InitializeObjectAttributes(\
  255.                 &ObjAttr,\
  256.                 &usFileName,\
  257.                 OBJ_CASE_INSENSITIVE,\
  258.                 NULL,\
  259.                 NULL);

  260.         Status = ZwCreateFile(\
  261.                 &hFile,\
  262.                 FILE_ALL_ACCESS,\
  263.                 &ObjAttr,\
  264.                 &IoStatusBlock,\
  265.                 NULL,\
  266.                 FILE_ATTRIBUTE_NORMAL,\
  267.                 FILE_SHARE_READ,\
  268.                 FILE_OPEN,\
  269.                 FILE_NON_DIRECTORY_FILE,\
  270.                 NULL,\
  271.                 0);
  272.         if (!NT_SUCCESS(Status))
  273.         {
  274.                 KdPrint(("ZwCreateFile failed:%X",Status));
  275.                 return Status;
  276.         }

  277.         FileOffset.QuadPart = 0;
  278.         Status = ZwReadFile(\
  279.                 hFile,\
  280.                 NULL,\
  281.                 NULL,\
  282.                 NULL,\
  283.                 &IoStatusBlock,\
  284.                 &ImageDosHeader,\
  285.                 sizeof(IMAGE_DOS_HEADER),\
  286.                 &FileOffset,\
  287.                 NULL);
  288.         if (!NT_SUCCESS(Status))
  289.         {
  290.                 KdPrint(("read iamge_dos_header failed:%X",Status));
  291.                 ZwClose(hFile);
  292.                 return Status;
  293.         }

  294.         FileOffset.QuadPart = ImageDosHeader.e_lfanew;
  295.         Status = ZwReadFile(\
  296.                 hFile,\
  297.                 NULL,\
  298.                 NULL,\
  299.                 NULL,\
  300.                 &IoStatusBlock,\
  301.                 &ImageNtHeader,\
  302.                 sizeof(IMAGE_NT_HEADERS),\
  303.                 &FileOffset,\
  304.                 NULL);
  305.         if (!NT_SUCCESS(Status))
  306.         {
  307.                 KdPrint(("read IMAGE_NT_HEADERS failed:%X",Status));
  308.                 ZwClose(hFile);
  309.                 return Status;
  310.         }

  311.         pImageSectionHeader = ExAllocatePool(\
  312.                 NonPagedPool,\
  313.                 sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader.FileHeader.NumberOfSections);
  314.         if (pImageSectionHeader==0)
  315.         {
  316.                 KdPrint(("pImageSectionHeader is null."));
  317.                 ZwClose(hFile);
  318.                 return STATUS_UNSUCCESSFUL;
  319.         }

  320.         FileOffset.QuadPart += sizeof(IMAGE_NT_HEADERS);
  321.         Status = ZwReadFile(\
  322.                 hFile,\
  323.                 NULL,\
  324.                 NULL,\
  325.                 NULL,\
  326.                 &IoStatusBlock,\
  327.                 pImageSectionHeader,\
  328.                 sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader.FileHeader.NumberOfSections,\
  329.                 &FileOffset,\
  330.                 NULL);
  331.         if (!NT_SUCCESS(Status))
  332.         {
  333.                 KdPrint(("read IMAGE_SECTION_HEADER failed:%X",Status));
  334.                 ExFreePool(pImageSectionHeader);
  335.                 ZwClose(hFile);
  336.                 return Status;
  337.         }

  338.         lpVirtualPointer = ExAllocatePool(NonPagedPool,ImageNtHeader.OptionalHeader.SizeOfImage);
  339.         if (lpVirtualPointer==0)
  340.         {
  341.                 KdPrint(("lpVirtualPointer is null"));
  342.                 ExFreePool(pImageSectionHeader);
  343.                 ZwClose(hFile);
  344.                 return STATUS_UNSUCCESSFUL;
  345.         }

  346.         memset(lpVirtualPointer,0,ImageNtHeader.OptionalHeader.SizeOfImage);
  347.         RtlCopyMemory(\
  348.                 lpVirtualPointer,\
  349.                 &ImageDosHeader,\
  350.                 sizeof(IMAGE_DOS_HEADER));
  351.         RtlCopyMemory(\
  352.                 (PVOID)((ULONG)lpVirtualPointer+ImageDosHeader.e_lfanew),\
  353.                 &ImageNtHeader,\
  354.                 sizeof(IMAGE_NT_HEADERS));
  355.         RtlCopyMemory(\
  356.                 (PVOID)((ULONG)lpVirtualPointer+ImageDosHeader.e_lfanew+sizeof(IMAGE_NT_HEADERS)),\
  357.                 pImageSectionHeader,
  358.                 sizeof(IMAGE_SECTION_HEADER)*ImageNtHeader.FileHeader.NumberOfSections);

  359.         for (uIndex = 0;uIndex<ImageNtHeader.FileHeader.NumberOfSections;uIndex++)
  360.         {
  361.                 SecVirtualAddress = pImageSectionHeader[uIndex].VirtualAddress;
  362.                 SizeOfSection = __Max(pImageSectionHeader[uIndex].SizeOfRawData,\
  363.                         pImageSectionHeader[uIndex].Misc.VirtualSize);

  364.                 PointerToRawData = pImageSectionHeader[uIndex].PointerToRawData;

  365.                 FileOffset.QuadPart = PointerToRawData;
  366.                 Status = ZwReadFile(\
  367.                         hFile,\
  368.                         NULL,\
  369.                         NULL,\
  370.                         NULL,\
  371.                         &IoStatusBlock,\
  372.                         (PVOID)((ULONG)lpVirtualPointer+SecVirtualAddress),\
  373.                         SizeOfSection,\
  374.                         &FileOffset,\
  375.                         NULL);
  376.                 if (!NT_SUCCESS(Status))
  377.                 {
  378.                         KdPrint(("read failed is pImageSectionHeader[%d]",uIndex));
  379.                         ExFreePool(pImageSectionHeader);
  380.                         ExFreePool(lpVirtualPointer);
  381.                         ZwClose(hFile);
  382.                         return Status;
  383.                 }
  384.         }

  385.         RelocModule(lpVirtualPointer,pOrigImage);

  386.         KdPrint(("ok!"));

  387.         ExFreePool(pImageSectionHeader);
  388.         *lpVirtualAddress = lpVirtualPointer;
  389.         ZwClose(hFile);
  390.         return STATUS_SUCCESS;
  391. }

  392. //////////////////////////////////////////////////////////////////////////

  393. VOID MyUnload(PDRIVER_OBJECT        pDriver)
  394. {
  395.         ResOldFastMutex();
  396.         if (g_new_kernel_pointer)
  397.         {
  398.                 ExFreePool(g_new_kernel_pointer);
  399.         }
  400. }

  401. NTSTATUS DriverEntry(PDRIVER_OBJECT        pDriver,PUNICODE_STRING Reg_Path)
  402. {
  403.         LDR_DATA_TABLE_ENTRY        *pLdrDataTableEntry;

  404.         pLdrDataTableEntry = SearchDriver(pDriver,L"ntoskrnl.exe");
  405.         if (pLdrDataTableEntry)
  406.         {
  407.                 ReadFileToMemory(L"\\??\\C:\\Windows\\System32\\ntkrnlpa.exe",&g_new_kernel_pointer,pLdrDataTableEntry->DllBase);
  408.                 g_kernel_increment = (ULONG)g_new_kernel_pointer - (ULONG)pLdrDataTableEntry->DllBase;
  409.         }
  410.          
  411.         SetNewFastMutex(pDriver);

  412.         pDriver->DriverUnload = MyUnload;
  413.         return STATUS_SUCCESS;
  414. }
复制代码
代码逻辑基本如此,我倒是没试验,因为之前写的一份儿和这个有些不同,大家自己测试吧,方法绝对可行!

实际这种方法麻烦了些,处理这个目前我想到了三种方法,这是其中一种。还有一种是对SwapContext函数做手脚,SwapContext函数好好的利用可以做很多有趣的事情哟。
还有一种是通过异常机制,这种方法比较难处理但是威力很不错,比如debugport通过它可以轻而易举搞定。

返回列表