没画完的画

喂马 劈柴 BBQ~
posts - 37, comments - 55, trackbacks - 0, articles - 0
  C++博客 ::  :: 新随笔 :: 联系 :: 聚合  :: 管理

关于 TrueCrypt 第三集

Posted on 2008-09-06 08:55 没画完的画 阅读(1880) 评论(1)  编辑 收藏 引用 所属分类: Windows Driver
2008.09.04

首先应该了解下在 Windows 下面,应用层(Ring3)跟内核(Ring0)的通信是如何进行的。
先不管内核(Ring0),先把 Ring3 弄明白再说
Google 到了下面一段代码

/* The code of interest is in the subroutine GetDriveGeometry. The
   code in main shows how to interpret the results of the IOCTL call. 
*/

  
#include 
<windows.h>
#include 
<winioctl.h>
#include 
<stdio.h>
  
BOOL GetDriveGeometry(DISK_GEOMETRY 
*pdg)
{
    HANDLE hDevice;               
// handle to the drive to be examined
    BOOL bResult;                 // results flag
    DWORD junk;                   // discard results
  
    hDevice 
= CreateFile("\\\\.\\PhysicalDrive0",  // drive to open
                    0,                // no access to the drive
                    FILE_SHARE_READ | // share mode
                    FILE_SHARE_WRITE,
                    NULL,             
// default security attributes
                    OPEN_EXISTING,    // disposition
                    0,                // file attributes
                    NULL);            // do not copy file attributes
  
    
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    {
        
return (FALSE);
    }

  
    bResult 
= DeviceIoControl(hDevice,     // device to be queried
        IOCTL_DISK_GET_DRIVE_GEOMETRY,     // operation to perform
                    NULL, 0,               // no input buffer
                    pdg, sizeof(*pdg),     // output buffer
                    &junk,                 // # bytes returned
                    (LPOVERLAPPED) NULL);  // synchronous I/O

    CloseHandle(hDevice);
  
    
return (bResult);
}

  
int main(int argc, char *argv[])
{
    DISK_GEOMETRY pdg;            
// disk drive geometry structure
    BOOL bResult;                 // generic results flag
    ULONGLONG DiskSize;           // size of the drive, in bytes
  
    bResult 
= GetDriveGeometry (&pdg);
  
    
if (bResult)
    
{
        printf(
"Cylinders = %I64d\n", pdg.Cylinders);
        printf(
"Tracks per cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);
        printf(
"Sectors per track = %ld\n", (ULONG) pdg.SectorsPerTrack);
        printf(
"Bytes per sector = %ld\n", (ULONG) pdg.BytesPerSector);
  
        DiskSize 
= pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
            (ULONG)pdg.SectorsPerTrack 
* (ULONG)pdg.BytesPerSector;
        printf(
"Disk size = %I64d (Bytes) = %I64d (Mb)\n", DiskSize,
            DiskSize 
/ (1024 * 1024));
    }

    
else
    
{
        printf(
"GetDriveGeometry failed. Error %ld.\n", GetLastError());
    }

  
    
return ((int)bResult);
}




上述的程序,用 CreateFile 打开 “\\\\.\\PhysicalDrive0” 这个设备,
据说 PhysicalDrive0 这个设备就是“第一块物理硬盘”了,至少是不是一定就是引导盘,不太清楚

问题1:PhysicalDrive0  是不是一定就是引导盘??

然后通过 DeviceIoControl() 与 设备交互
最后 CloseHandle()

打开设备就像打开文件一样,如果把 DeviceIoControl() 换成 WriteFile 呢, 哈哈哈,,试一下先

BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
    HANDLE hDevice;               
// handle to the drive to be examined
    BOOL bResult;                 // results flag
    DWORD junk;                   // discard results
  
    hDevice 
= CreateFile("\\\\.\\PhysicalDrive0",  // drive to open
                    0,                // no access to the drive
                    FILE_SHARE_READ | // share mode
                    FILE_SHARE_WRITE,
                    NULL,             
// default security attributes
                    OPEN_EXISTING,    // disposition
                    0,                // file attributes
                    NULL);            // do not copy file attributes
  
    
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    {
        
return (FALSE);
    }

  
    DWORD dwRet 
= 0 ;

 
if( WriteFile(hDevice, "abc"3&dwRet, NULL) == FALSE)
 
{
  printf(
"Error WriteFile: LastErrorCode = %d \n", ::GetLastError());
  
return FALSE; 
 }
;

    CloseHandle(hDevice);
  
    
return (bResult);
}


为了安全起见,在虚拟机下运行,运行结果如下:
Error WriteFile: LastErrorCode = 5

查了 ErrorLookUp
5 的错误是 Access is denied.
哈哈~~~如果 WriteFile 成功不就见鬼了!!!

从安全的角度来看,NT以上的系统好像是不能直接对硬盘的绝对扇区操作的,至于 WriteFile  PhysicalDrive0 失败的具体原因,不太清楚。

问题2: WriteFile  PhysicalDrive0 失败的具体原因? (ReadFile没试,理论上应当也会失败)

总结一下:
1、关于 CreateFile 时所使用的设备名
    一些标准设备的设备名,微软已经定义好了,
   比如 PhysicalDrive0  这些,如果是自己的驱动所创建的设备,当然名字可以任由自己取

   在 CreateFile 打开设备时,设备的名字通常为 \\.\DeviceName (在C++中的字符串则表示为
   \\\\.\\DeviceName)
  
   在驱动.sys 当然需要做一些东西,才可以让应用层通过这个设备名来访问(具体见下集分解)
  

2、DeviceIoControl() 这个函数是与驱动层通信的关键
   
    一个操作码,一个输入缓冲区,一个输出缓冲区
    具体做什么,可以当然需要驱动与应用程序之间事先商量好的,
    微软同样定义了一些标准设备的操作码,在 winioctl.h 文件中


今天好累, 去睡觉了~~~~~
 
问题列表:
问题1:PhysicalDrive0  是不是一定就是引导的硬盘??
           -- 未解决

问题2:WriteFile  PhysicalDrive0 失败的具体原因? (ReadFile没试,理论上应当也会失败)
           -- 未解决

问题3:如果驱动层在响应DeviceIoControl 时阻塞,那么应用层在调用 DeviceIoContorl() 是不是也会阻塞?

           -- 未解决

Feedback

# re: 关于 TrueCrypt 第三集  回复  更多评论   

2008-09-08 00:48 by Bill Gates
1. 不一定
2. Administrator没有权限写MBR,不过有权限打开C:,写c盘的引导区
3. 不管是ring0还是ring3,DeviceIoControl的时候是一个线程,所以这个线程会block,ring0 block的后果是这个线程不能被terminate,造成所属进程也不能terminate(不过这个进程其他线程会被terminate,所以无法用这个做不能杀死的进程干坏事)。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理