首页
社区
课程
招聘
[求助]HalExamineMBR的问题
发表于: 2013-5-24 16:46 6175

[求助]HalExamineMBR的问题

2013-5-24 16:46
6175
无意中在WDK中看到这个函数,文档中给出的原型如下,捣鼓了一番怎么都不对,后来发现貌似最后一个参数MS写错了,应该是 OUT PVOID  *Buffer,而且调用方式是 FASTCALL。

VOID
  HalExamineMBR(
    IN PDEVICE_OBJECT  DeviceObject,
    IN ULONG  SectorSize,
    IN ULONG  MBRTypeIdentifier,
    OUT PVOID  Buffer, // 应该是 OUT PVOID  *Buffer
    );

拿IDA看了一下,有个地方不明白,问什么要拿MBRTypeIdentifier和0x54,0x55比较呢,那两个常量我查了一下,不知所云。而且我的机器上MBR中启动分区的类型是 0x7,所以我的驱动调用HalExamineMBR一直不成功,请教一下这个函数到底怎么用?

54h                                        OnTrack        Disk Manager 6        Dyamic Drive Overlay (DDO)
55h                                        MicroHouse / StorageSoft        EZ-Drive        EZ-Drive, Maxtor, MaxBlast, or DriveGuide INT 13h redirector volume

完整的分区类型列表在这里 http://en.wikipedia.org/wiki/Partition_type

.text:00415D6F 66 81 BE FE 01 00 00 55+                cmp     word ptr [esi+1FEh], 0AA55h
.text:00415D78 75 BD                                   jnz     short loc_415D37
.text:00415D7A 8A 86 C2 01 00 00                       mov     al, [esi+1C2h]
.text:00415D80 0F B6 C8                                movzx   ecx, al
.text:00415D83 3B 4D 08                                cmp     ecx, [ebp+arg_0]; arg_0 is MBRTypeIdentifier
.text:00415D86 75 AF                                   jnz     short loc_415D37
.text:00415D88 3C 54                                   cmp     al, 54h
.text:00415D8A 75 08                                   jnz     short loc_415D94
.text:00415D8C C7 06 3F 00 00 00                       mov     dword ptr [esi], 3Fh
.text:00415D92 EB 04                                   jmp     short loc_415D98
.text:00415D94                         ; ---------------------------------------------------------------------------
.text:00415D94
.text:00415D94                         loc_415D94:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+CCj
.text:00415D94 3C 55                                   cmp     al, 55h
.text:00415D96 75 05                                   jnz     short loc_415D9D
.text:00415D98
.text:00415D98                         loc_415D98:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+D4j
.text:00415D98 8B 45 0C                                mov     eax, [ebp+arg_4]
.text:00415D9B 89 30                                   mov     [eax], esi
...

完整的函数如下, OS为Windows XP SP3:

  public __fastcall HalExamineMBR(x, x, x, x)
.text:00415CBE                         __fastcall HalExamineMBR(x, x, x, x) proc near
.text:00415CBE                                                                 ; CODE XREF: IoSetPartitionInformation(x,x,x,x)+2Cp
.text:00415CBE                                                                 ; IoWritePartitionTable(x,x,x,x,x)+46p ...
.text:00415CBE
.text:00415CBE                         Event           = _KEVENT ptr -24h
.text:00415CBE                         IoStatusBlock   = _IO_STATUS_BLOCK ptr -14h
.text:00415CBE                         StartingOffset  = LARGE_INTEGER ptr -0Ch
.text:00415CBE                         DeviceObject    = dword ptr -4
.text:00415CBE                         arg_0           = dword ptr  8
.text:00415CBE                         arg_4           = dword ptr  0Ch
.text:00415CBE
.text:00415CBE 8B FF                                   mov     edi, edi
.text:00415CC0 55                                      push    ebp
.text:00415CC1 8B EC                                   mov     ebp, esp
.text:00415CC3 83 EC 24                                sub     esp, 24h
.text:00415CC6 8B 45 0C                                mov     eax, [ebp+arg_4]
.text:00415CC9 53                                      push    ebx
.text:00415CCA 33 DB                                   xor     ebx, ebx
.text:00415CCC 89 18                                   mov     [eax], ebx
.text:00415CCE 56                                      push    esi
.text:00415CCF B8 00 02 00 00                          mov     eax, 200h
.text:00415CD4 3B D0                                   cmp     edx, eax
.text:00415CD6 57                                      push    edi
.text:00415CD7 89 4D FC                                mov     [ebp+DeviceObject], ecx
.text:00415CDA 8B FA                                   mov     edi, edx
.text:00415CDC 73 02                                   jnb     short loc_415CE0
.text:00415CDE 8B F8                                   mov     edi, eax
.text:00415CE0
.text:00415CE0                         loc_415CE0:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+1Ej
.text:00415CE0 B8 00 10 00 00                          mov     eax, 1000h
.text:00415CE5 3B F8                                   cmp     edi, eax
.text:00415CE7 89 5D F4                                mov     dword ptr [ebp+StartingOffset], ebx
.text:00415CEA 89 5D F8                                mov     dword ptr [ebp+StartingOffset+4], ebx
.text:00415CED 72 02                                   jb      short loc_415CF1
.text:00415CEF 8B C7                                   mov     eax, edi
.text:00415CF1
.text:00415CF1                         loc_415CF1:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+2Fj
.text:00415CF1 68 46 73 74 62                          push    'btsF'          ; Tag
.text:00415CF6 50                                      push    eax             ; NumberOfBytes
.text:00415CF7 6A 04                                   push    4               ; PoolType
.text:00415CF9 E8 82 73 05 00                          call    ExAllocatePoolWithTag(x,x,x)
.text:00415CFE 8B F0                                   mov     esi, eax
.text:00415D00 3B F3                                   cmp     esi, ebx
.text:00415D02 0F 84 95 00 00 00                       jz      loc_415D9D
.text:00415D08 8D 45 E4                                lea     eax, [ebp+Event.Header.WaitListHead]
.text:00415D0B 89 45 E8                                mov     [ebp+Event.Header.WaitListHead.Blink], eax
.text:00415D0E 89 45 E4                                mov     [ebp+Event.Header.WaitListHead.Flink], eax
.text:00415D11 8D 45 EC                                lea     eax, [ebp+IoStatusBlock]
.text:00415D14 50                                      push    eax             ; IoStatusBlock
.text:00415D15 8D 45 DC                                lea     eax, [ebp+Event]
.text:00415D18 50                                      push    eax             ; Event
.text:00415D19 8D 45 F4                                lea     eax, [ebp+StartingOffset]
.text:00415D1C 50                                      push    eax             ; StartingOffset
.text:00415D1D 57                                      push    edi             ; Length
.text:00415D1E 56                                      push    esi             ; Buffer
.text:00415D1F FF 75 FC                                push    [ebp+DeviceObject] ; DeviceObject
.text:00415D22 88 5D DC                                mov     [ebp+Event.Header.Type], bl
.text:00415D25 6A 03                                   push    3               ; MajorFunction
.text:00415D27 C6 45 DE 04                             mov     [ebp+Event.Header.Size], 4
.text:00415D2B 89 5D E0                                mov     [ebp+Event.Header.SignalState], ebx
.text:00415D2E E8 A3 34 00 00                          call    IoBuildSynchronousFsdRequest(x,x,x,x,x,x,x)
.text:00415D33 3B C3                                   cmp     eax, ebx
.text:00415D35 75 09                                   jnz     short loc_415D40
.text:00415D37
.text:00415D37                         loc_415D37:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+AFj
.text:00415D37                                                                 ; HalExamineMBR(x,x,x,x)+BAj ...
.text:00415D37 53                                      push    ebx             ; Tag
.text:00415D38 56                                      push    esi             ; P
.text:00415D39 E8 A8 6C 05 00                          call    ExFreePoolWithTag(x,x)
.text:00415D3E EB 5D                                   jmp     short loc_415D9D
.text:00415D40                         ; ---------------------------------------------------------------------------
.text:00415D40
.text:00415D40                         loc_415D40:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+77j
.text:00415D40 8B 48 60                                mov     ecx, [eax+60h]
.text:00415D43 80 49 DE 02                             or      byte ptr [ecx-22h], 2
.text:00415D47 83 E9 24                                sub     ecx, 24h
.text:00415D4A 8B 4D FC                                mov     ecx, [ebp+DeviceObject]
.text:00415D4D 8B D0                                   mov     edx, eax
.text:00415D4F E8 AC 10 00 00                          call    IofCallDriver(x,x)
.text:00415D54 3D 03 01 00 00                          cmp     eax, 103h
.text:00415D59 75 10                                   jnz     short loc_415D6B
.text:00415D5B 53                                      push    ebx             ; Timeout
.text:00415D5C 53                                      push    ebx             ; Alertable
.text:00415D5D 53                                      push    ebx             ; WaitMode
.text:00415D5E 53                                      push    ebx             ; WaitReason
.text:00415D5F 8D 45 DC                                lea     eax, [ebp+Event]
.text:00415D62 50                                      push    eax             ; Object
.text:00415D63 E8 94 CA 00 00                          call    KeWaitForSingleObject(x,x,x,x,x)
.text:00415D68 8B 45 EC                                mov     eax, dword ptr [ebp+IoStatusBlock.___u0]
.text:00415D6B
.text:00415D6B                         loc_415D6B:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+9Bj
.text:00415D6B 3B C3                                   cmp     eax, ebx
.text:00415D6D 7C C8                                   jl      short loc_415D37
.text:00415D6F 66 81 BE FE 01 00 00 55+                cmp     word ptr [esi+1FEh], 0AA55h
.text:00415D78 75 BD                                   jnz     short loc_415D37
.text:00415D7A 8A 86 C2 01 00 00                       mov     al, [esi+1C2h]
.text:00415D80 0F B6 C8                                movzx   ecx, al
.text:00415D83 3B 4D 08                                cmp     ecx, [ebp+arg_0]
.text:00415D86 75 AF                                   jnz     short loc_415D37
.text:00415D88 3C 54                                   cmp     al, 54h
.text:00415D8A 75 08                                   jnz     short loc_415D94
.text:00415D8C C7 06 3F 00 00 00                       mov     dword ptr [esi], 3Fh
.text:00415D92 EB 04                                   jmp     short loc_415D98
.text:00415D94                         ; ---------------------------------------------------------------------------
.text:00415D94
.text:00415D94                         loc_415D94:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+CCj
.text:00415D94 3C 55                                   cmp     al, 55h
.text:00415D96 75 05                                   jnz     short loc_415D9D
.text:00415D98
.text:00415D98                         loc_415D98:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+D4j
.text:00415D98 8B 45 0C                                mov     eax, [ebp+arg_4]
.text:00415D9B 89 30                                   mov     [eax], esi
.text:00415D9D
.text:00415D9D                         loc_415D9D:                             ; CODE XREF: HalExamineMBR(x,x,x,x)+44j
.text:00415D9D                                                                 ; HalExamineMBR(x,x,x,x)+80j ...
.text:00415D9D 5F                                      pop     edi
.text:00415D9E 5E                                      pop     esi
.text:00415D9F 5B                                      pop     ebx
.text:00415DA0 C9                                      leave
.text:00415DA1 C2 08 00                                retn    8
.text:00415DA1                         __fastcall HalExamineMBR(x, x, x, x) endp

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 9
活跃值: (598)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
在 WRK 里面找到了 HalExamineMBR 的代码,看起来必须是 0x54/0x55 才行,我的机器是 0x7,就会一直失败。 求解惑

partitionTableEntry = (PPARTITION_DESCRIPTOR) &(((PUSHORT) readBuffer)[PARTITION_TABLE_OFFSET]);

    if (partitionTableEntry->PartitionType != MBRTypeIdentifier) {

        //
        // The partition type isn't what the caller cares about.
        //
        ExFreePool(readBuffer);

    } else {

        if (partitionTableEntry->PartitionType == 0x54) {

            //
            // Rather than allocate a new piece of memory to return
            // the data - just use the memory allocated for the buffer.
            // We can assume the caller will delete this shortly.
            //

            ((PULONG)readBuffer)[0] = 63;
            *Buffer = readBuffer;

        } else if (partitionTableEntry->PartitionType == 0x55) {

            //
            // EzDrive Partition.  Simply return the pointer to non-null
            // There is no skewing here.
            //

            *Buffer = readBuffer;

        } else {

            ASSERT(partitionTableEntry->PartitionType == 0x55);

        }
2013-5-24 18:14
0
雪    币: 402
活跃值: (342)
能力值: ( LV2,RANK:140 )
在线值:
发帖
回帖
粉丝
3
Partition type 0x55 is used by a driver which hooks the BIOS disk
interrupt and translates disk geometry so that DOS can use the
entire disk.  The driver keeps your partitions inside the 0x55
partition.  Linux detects the situation and uses them from there.
Apparently Linux also fools fdisk into reading and writing the
virtual MBR inside the 0x55 partition.

这段代码是用来判断是不是EZDrive类型的Disk的,在IoReadPartitionTable里用到这个函数了,如果是EZDrive貌似有特殊的处理..
2013-5-30 15:42
0
雪    币: 9
活跃值: (598)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
感谢关注,问题是我的物理机器Win7和虚拟机上的MBR这个位置都是0x7,所以调用此函数会失败
2013-5-30 17:16
0
雪    币: 402
活跃值: (342)
能力值: ( LV2,RANK:140 )
在线值:
发帖
回帖
粉丝
5
对呀.0x7说明你的是ntfs的呀不是EZDrive的,...这个函数就是用来检查你这个盘是不是EZDrive的类型的.不是的话当然不会返回了.我表达不清楚??
2013-5-31 11:02
0
雪    币: 9
活跃值: (598)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
谢谢,你说的很清楚,这个函数就是用来检查你这个盘是不是EZDrive的类型的, 只是WDK上说的是可以用 HalExamineMBR 这个函数获得MBR的内容,这正是我想要的:

The HalExamineMBR routine reads the master boot record (MBR) of a disk and returns data from the MBR if the MBR is of the type specified by the caller.

获得的方式是:
*Buffer = readBuffer;

但是该函数的代码是把MBR读取到readBuffer,再获取第一个分区表的PartitionType项(我的机器上是0x7),如果是0x55 才会 *Buffer = readBuffer;

在我的机器上我这么调用:
int *p = NULL;
HalExamineMBR(pDO, 512, 0x7, (PVOID *)&p);

因为比较的是PartitionType(0x7),所以在我的机器上永远不能成功,这个才是我不明白的地方,难道是MS故意做的限制?

其实在调试的时候发现MBR已经读出来了,在readBuffer里面,只是没有赋给Buffer(我传进去的参数)

-------------------------------
VOID
  HalExamineMBR(
    IN PDEVICE_OBJECT  DeviceObject,
    IN ULONG  SectorSize,
    IN ULONG  MBRTypeIdentifier,
    OUT PVOID * Buffer,
    );

Parameters
DeviceObject
Pointer to the device object for the device being examined.

SectorSize
Specifies the minimum number of bytes that an I/O operation can fetch from the device being examined. If this value is less than 512, HalExamineMBR reads 512 bytes to ensure that it reads an entire partition table.

MBRTypeIdentifier
Specifies the type of MBR that may be on the disk.

Buffer
Pointer to a buffer that returns data from the MBR. The layout of the buffer depends on the MBRTypeIdentifier. The caller must deallocate this buffer as soon as possible with ExFreePool. This routine returns NULL in Buffer if the MBRTypeIdentifier of the disk does not match that specified by the caller or if there is an error.
2013-5-31 14:43
0
雪    币: 402
活跃值: (342)
能力值: ( LV2,RANK:140 )
在线值:
发帖
回帖
粉丝
7
很明显,WDk写的不对...印象中MSDN中有不少错的地方
2013-5-31 15:19
0
雪    币: 8865
活跃值: (2379)
能力值: ( LV12,RANK:760 )
在线值:
发帖
回帖
粉丝
8
真相是:写MSDN的这个条目,和开发该接口的人不是一个人。
2013-6-1 11:49
0
游客
登录 | 注册 方可回帖
返回
//