[关闭]
@zoand 2015-06-21T04:37:56.000000Z 字数 2929 阅读 3830

[整理]一份很标准的MmGetSystemRoutineAddressEx(兼容32/64)

amd64驱动


获得任意模块任意导出函数的地址,不再受限于MmGetSystemRoutineAddress的鸡肋功能。
(一般 Kixxx 都是未导出的 API,而 Kexxx 则是导出的 API)

  1. //传入:导出函数所属模块的基址,函数名称(跟GetProcAddress一样)
  2. PVOID MmGetSystemRoutineAddressEx(SIZE_T uModBase, CHAR *cSearchFnName)
  3. {
  4. IMAGE_DOS_HEADER *doshdr;
  5. #ifdef AMD64
  6. IMAGE_OPTIONAL_HEADER64 *opthdr;
  7. #else
  8. IMAGE_OPTIONAL_HEADER32 *opthdr;
  9. #endif
  10. IMAGE_EXPORT_DIRECTORY *pExportTable;
  11. ULONG *dwAddrFns, *dwAddrNames;
  12. USHORT *dwAddrNameOrdinals;
  13. ULONG dwFnOrdinal,i;
  14. SIZE_T uFnAddr=0;
  15. char *cFunName;
  16. doshdr = (IMAGE_DOS_HEADER *)uModBase;
  17. if (NULL == doshdr)
  18. {
  19. goto __exit;
  20. }
  21. #ifdef AMD64
  22. opthdr = (IMAGE_OPTIONAL_HEADER64 *)(uModBase + doshdr->e_lfanew + sizeof(ULONG)+sizeof(IMAGE_FILE_HEADER));
  23. #else
  24. opthdr = (IMAGE_OPTIONAL_HEADER32 *)(uModBase + doshdr->e_lfanew + sizeof(ULONG)+sizeof(IMAGE_FILE_HEADER));
  25. #endif
  26. if (NULL == opthdr)
  27. {
  28. goto __exit;
  29. }
  30. pExportTable = (IMAGE_EXPORT_DIRECTORY *)(uModBase + opthdr->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  31. if (NULL == pExportTable)
  32. {
  33. goto __exit;
  34. }
  35. dwAddrFns = (ULONG *)(uModBase + pExportTable->AddressOfFunctions);
  36. dwAddrNames = (ULONG *)(uModBase + pExportTable->AddressOfNames);
  37. dwAddrNameOrdinals = (USHORT *)(uModBase + pExportTable->AddressOfNameOrdinals);
  38. for (i = 0; i < pExportTable->NumberOfNames; ++i)
  39. {
  40. cFunName = (char *)(uModBase + dwAddrNames[i]);
  41. if (!_strnicmp(cSearchFnName, cFunName, strlen(cSearchFnName)))
  42. {
  43. dwFnOrdinal = pExportTable->Base + dwAddrNameOrdinals[i] - 1;
  44. uFnAddr = uModBase + dwAddrFns[dwFnOrdinal];
  45. break;
  46. }
  47. }
  48. __exit:
  49. return (PVOID)uFnAddr;
  50. }

来源:http://www.vbasm.com/thread-8053-1-1.html

另外一份,功能一样:

  1. ULONG GetFunctionAddress
  2. (
  3. IN ULONG FirstFeature,
  4. IN ULONG SecondFeature,
  5. IN ULONG ThirdFeature,
  6. IN ULONG FourthFeature
  7. )
  8. {
  9. NTSTATUS NtStatus=STATUS_SEVERITY_SUCCESS;
  10. ULONG SystemInformationLength=0;
  11. ULONG Index=0;
  12. ULONG Loop=0;
  13. ULONG ModuleBegin=0;
  14. ULONG ModuleFinish=0;
  15. PULONG SystemInformationBuffer=NULL;
  16. PSYSTEM_MODULE_INFORMATION SystemModulePointer=NULL;
  17. ULONG Value=0;
  18. ZwQuerySystemInformation(SystemModuleInformation,NULL,0,&SystemInformationLength);
  19. SystemInformationBuffer=ExAllocatePool(PagedPool,SystemInformationLength);
  20. if (SystemInformationBuffer==NULL)
  21. {
  22. return NtStatus;
  23. }
  24. NtStatus=ZwQuerySystemInformation
  25. (
  26. SystemModuleInformation,
  27. SystemInformationBuffer,
  28. SystemInformationLength,
  29. NULL
  30. );
  31. if (!NT_SUCCESS(NtStatus))
  32. {
  33. ExFreePool(SystemInformationBuffer);
  34. return NtStatus;
  35. }
  36. if (MmIsAddressValid(SystemInformationBuffer)==False)
  37. {
  38. ExFreePool(SystemInformationBuffer);
  39. return NtStatus;
  40. }
  41. SystemModulePointer=(PSYSTEM_MODULE_INFORMATION)(SystemInformationBuffer+1);
  42. for (Index=0;Index<*(ULONG*)SystemInformationBuffer;Index++)
  43. {
  44. ModuleBegin=(ULONG)SystemModulePointer[Index].Base;
  45. ModuleFinish=(ULONG)SystemModulePointer[Index].Base+SystemModulePointer[Index].Size;
  46. for (Loop=ModuleBeginAddress;Loop<ModuleFinishAddress;Loop++)
  47. {
  48. if
  49. (
  50. *(ULONG*)(Loop+0)==FirstFeature&&
  51. *(ULONG*)(Loop+4)==SecondFeature&&
  52. *(ULONG*)(Loop+8)==ThirdFeature&&
  53. *(ULONG*)(Loop+12)==FourthFeature
  54. )
  55. {
  56. Value=Loop;
  57. }
  58. }
  59. }
  60. ExFreePool(SystemInformationBuffer);
  61. return Value;
  62. }

来源:http://blog.csdn.net/dormancy_elife/article/details/6072842

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注