关于MmMapIoSpace函数(高手们进来帮帮忙,在线等)

digi01   2008-8-7 14:55 楼主
[code]
        ULONG SIZE = 16;
        //ULONG ADDRESS = 0x00000413;
        PHYSICAL_ADDRESS ioPhysicalBase = {0x0000050F,0x00000500};
        SECTOR_KEY =  (PUCHAR)MmMapIoSpace(ioPhysicalBase,SIZE,FALSE);
        DbgPrint("SECORT_KEY_007:\n");

        if(SECTOR_KEY==NULL)
        {
                DbgPrint("This is NULL!\n");
        }else{
                DbgPrint("%x\n",SECTOR_KEY);
                DbgPrint("%X\n",*SECTOR_KEY);
                DbgPrint("not null end\n");
        }
我在0x00000500这个位置写了三个字符"aut",但是用DbgPrint打印出来的消息并不时"aut",而是"FF"。不知道是不是我这个函数是否用对了,还望懂这个东西的高手们帮下忙。
小弟在这先谢过了

回复评论 (6)

小弟就是想在驱动程序中以字符串指针的方式读内存物理地址为0x00000500下的16个字符,或者有其它的方法。希望知道的高手们不吝赐教,最好能详细解释下或给段代码给我。
再谢过啦!!!
点赞  2008-8-7 15:02
MmMapIoSpace
The MmMapIoSpace routine maps the given physical address range to nonpaged system space.

PVOID
  MmMapIoSpace(
    IN PHYSICAL_ADDRESS  PhysicalAddress,
    IN ULONG  NumberOfBytes,
    IN MEMORY_CACHING_TYPE  CacheType
    );


Parameters
PhysicalAddress
Specifies the starting physical address of the I/O range to be mapped.
NumberOfBytes
Specifies a value greater than zero, indicating the number of bytes to be mapped.
CacheType
Specifies a MEMORY_CACHING_TYPE value, which indicates the permitted caching behavior when mapping the physical address range.

Return Value
MmMapIoSpace returns the base virtual address that maps the base physical address for the range. If space for mapping the range is insufficient, it returns NULL.

点赞  2008-8-7 15:39
0x00000500是实际的物理地址吧?
点赞  2008-8-7 15:47
已经弄出来了!发个大家共享下!

  1. #include
  2. #include
  3. //#include
  4. #include

  5. #define BYTESPERLINE 16
  6. #define LINESPERSCREEN 25
  7. #define OBJ_CASE_INSENSITIVE 0x00000040L
  8. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  9. #define InitializeObjectAttributes( p, n, a, r, s ) { \
  10.         (p)->Length = sizeof( OBJECT_ATTRIBUTES ); \
  11.         (p)->RootDirectory = r; \
  12.         (p)->Attributes = a; \
  13.         (p)->ObjectName = n; \
  14.         (p)->SecurityDescriptor = s; \
  15.         (p)->SecurityQualityOfService = NULL; \
  16. }

  17. typedef struct _UNICODE_STRING
  18. {
  19.     USHORT Length;
  20.     USHORT MaximumLength;
  21. #ifdef MIDL_PASS
  22.     [size_is(MaximumLength / 2), length_is((Length) / 2) ] USHORT * Buffer;
  23. #else // MIDL_PASS
  24.     PWSTR Buffer;
  25. #endif // MIDL_PASS
  26. } UNICODE_STRING;

  27. typedef LARGE_INTEGER PHYSICAL_ADDRESS, *PPHYSICAL_ADDRESS; // windbgkd
  28. typedef LONG NTSTATUS;
  29. typedef UNICODE_STRING *PUNICODE_STRING;
  30. typedef enum _SECTION_INHERIT{ ViewShare = 1 , ViewUnmap = 2 } SECTION_INHERIT;

  31. //*************************************************************************************************
  32. typedef struct _OBJECT_ATTRIBUTES
  33. {
  34.     ULONG Length;
  35.     HANDLE RootDirectory;
  36.     PUNICODE_STRING ObjectName;
  37.     ULONG Attributes;
  38.     PVOID SecurityDescriptor; // Points to type SECURITY_DESCRIPTOR
  39.     PVOID SecurityQualityOfService; // Points to type SECURITY_QUALITY_OF_SERVICE
  40. } OBJECT_ATTRIBUTES;
  41. typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;

  42. //************************************************************************
  43. NTSTATUS (__stdcall *NtUnmapViewOfSection)
  44. (
  45. IN HANDLE ProcessHandle,
  46. IN PVOID BaseAddress
  47. );
  48. NTSTATUS (__stdcall *NtOpenSection)
  49. (
  50. OUT PHANDLE SectionHandle,
  51. IN ACCESS_MASK DesiredAccess,
  52. IN POBJECT_ATTRIBUTES ObjectAttributes
  53. );
  54. NTSTATUS (__stdcall *NtMapViewOfSection)
  55. (
  56. IN HANDLE SectionHandle,
  57. IN HANDLE ProcessHandle,
  58. IN OUT PVOID *BaseAddress,
  59. IN ULONG ZeroBits,
  60. IN ULONG CommitSize,
  61. IN OUT PLARGE_INTEGER SectionOffset, /* optional */
  62. IN OUT PULONG ViewSize,
  63. IN SECTION_INHERIT InheritDisposition,
  64. IN ULONG AllocationType,
  65. IN ULONG Protect
  66. );
  67. VOID (__stdcall *RtlInitUnicodeString)
  68. (
  69. IN OUT PUNICODE_STRING DestinationString,
  70. IN PCWSTR SourceString
  71. );
  72. ULONG (__stdcall *RtlNtStatusToDosError)
  73. (
  74. IN NTSTATUS Status
  75. );
  76. VOID UnmapPhysicalMemory( DWORD Address )
  77. {
  78.         NTSTATUS status;
  79.         status = NtUnmapViewOfSection( (HANDLE) -1, (PVOID) Address );
  80. }
  81. //************************************************************************
  82. //--------------------------------------------------------
  83. //
  84. // MapPhysicalMemory
  85. //
  86. // Maps a view of a section.
  87. //
  88. //--------------------------------------------------------
  89. BOOLEAN MapPhysicalMemory(        HANDLE PhysicalMemory,
  90.                                                         PDWORD Address,
  91.                                                         PDWORD Length,
  92.                                                         PDWORD VirtualAddress )
  93. {
  94.     NTSTATUS ntStatus;
  95.     PHYSICAL_ADDRESS viewBase;
  96.     //char error[256];
  97.     *VirtualAddress = 0;
  98.     viewBase.QuadPart = (ULONGLONG) (*Address);
  99.     ntStatus = NtMapViewOfSection (PhysicalMemory,
  100.         (HANDLE) -1,
  101.         (PVOID *) VirtualAddress,
  102.         0L,
  103.         *Length,
  104.         &viewBase,
  105.         Length,
  106.         ViewShare,
  107.         0,
  108.         PAGE_READONLY );
  109.     *Address = viewBase.LowPart;
  110.     return TRUE;
  111. }
  112. //--------------------------------------------------------
  113. //
  114. // OpensPhysicalMemory
  115. //
  116. // This function opens the physical memory device. It
  117. // uses the native API since
  118. //
  119. //--------------------------------------------------------
  120. HANDLE OpenPhysicalMemory()
  121. {
  122.     NTSTATUS status;
  123.     HANDLE physmem;
  124.     UNICODE_STRING physmemString;
  125.     OBJECT_ATTRIBUTES attributes;
  126.     WCHAR physmemName[] = L"\\device\\physicalmemory";
  127.     RtlInitUnicodeString( &physmemString, physmemName );
  128.     InitializeObjectAttributes( &attributes, &physmemString,
  129.     OBJ_CASE_INSENSITIVE, NULL, NULL );
  130.     status = NtOpenSection( &physmem, SECTION_MAP_READ, &attributes );
  131.     return physmem;
  132. }
  133. //--------------------------------------------------------
  134. //
  135. // LocateNtdllEntryPoints
  136. //
  137. // Finds the entry points for all the functions we
  138. // need within NTDLL.DLL.
  139. //
  140. //--------------------------------------------------------
  141. BOOLEAN LocateNtdllEntryPoints()
  142. {
  143.     if( !(RtlInitUnicodeString = (void (__stdcall *)(PUNICODE_STRING,PCWSTR)) GetProcAddress( GetModuleHandle("ntdll.dll"),"RtlInitUnicodeString" )) )
  144.     {
  145.         return FALSE;
  146.     }
  147.     if( !(NtUnmapViewOfSection = (NTSTATUS (__stdcall *)(HANDLE,PVOID)) GetProcAddress( GetModuleHandle("ntdll.dll"),"NtUnmapViewOfSection" )) )
  148.     {
  149.         return FALSE;
  150.     }
  151.     if( !(NtOpenSection = (NTSTATUS (__stdcall *)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES)) GetProcAddress( GetModuleHandle("ntdll.dll"),"NtOpenSection" )) )
  152.     {
  153.         return FALSE;
  154.     }
  155.     if( !(NtMapViewOfSection = (NTSTATUS (__stdcall *)(IN HANDLE,
  156.                                                        IN HANDLE,
  157.                                                        IN OUT PVOID *,
  158.                                                        IN ULONG ,
  159.                                                        IN ULONG ,
  160.                                                        IN OUT PLARGE_INTEGER ,
  161.                                                        IN OUT PULONG,
  162.                                                        IN SECTION_INHERIT,
  163.                                                        IN ULONG ,
  164.                                                        IN ULONG )) GetProcAddress( GetModuleHandle("ntdll.dll"),"NtMapViewOfSection" )) )
  165.     {
  166.         return FALSE;
  167.     }
  168.     if( !(RtlNtStatusToDosError = (ULONG (__stdcall *)(NTSTATUS)) GetProcAddress( GetModuleHandle("ntdll.dll"),"RtlNtStatusToDosError" )) )
  169.     {
  170.         return FALSE;
  171.     }
  172.     return TRUE;
  173. }

  174. //--------------------------------------------------------
  175. //
  176. //Memory_Call
  177. //
  178. //
  179. //--------------------------------------------------------
  180. void Memory_Call()
  181. {
  182.         HANDLE physmem;
  183.         DWORD vaddress, paddress, length;
  184.         char key[16];
  185.         char input[256];
  186.         char mk[] = "12";
  187.         char jk[] = "12";

  188.         //
  189.         // Load NTDLL entry points
  190.         LocateNtdllEntryPoints();
  191.         physmem = OpenPhysicalMemory();
  192.        
  193.         paddress = 0x00;
  194.         length = 0x00;
  195.         while(1)
  196.         {
  197.                 memcpy(input,mk,3);
  198.                 sscanf( input, "%x", &paddress );
  199.                 memcpy(input,jk,3);
  200.                 sscanf( input, "%x", &length );
  201.                 if( !MapPhysicalMemory( physmem, &paddress, &length,&vaddress ))
  202.                         continue;
  203.                 memcpy(key,(char *)vaddress+1280,16);
  204.                 printf(key);
  205.                 printf("\n");
  206.                 break;
  207.                 UnmapPhysicalMemory( vaddress );
  208.         }
  209.        

  210.         CloseHandle( physmem );
  211. }
  212. //--------------------------------------------------------
  213. //
  214. // Main
  215. //
  216. // This program drives the command loop
  217. //
  218. //--------------------------------------------------------
  219. int main( int argc, char *argv[] )
  220. {
  221.         Memory_Call();
  222.         _getch();
  223.         return 0;
  224. }

点赞  2008-8-7 20:31
贴出来的代码没有注释,不好理解,感觉和读取0x00000500下的16个字符无关。

MmMapIoSpace函数调用错在这里:
PHYSICAL_ADDRESS ioPhysicalBase = {0x0000050F,0x00000500};
物理地址是64位的地址,这样写的意思是访问0x5000000050F,而不是从0x00000500到0x0000050F。
修改如下:
PHYSICAL_ADDRESS ioPhysicalBase = {0x0000050F,0};
点赞  2008-9-17 12:46
标记下
点赞  2010-1-5 08:30
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复