• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            沒畫完的畫

            喂馬 劈柴 BBQ~
            posts - 37, comments - 55, trackbacks - 0, articles - 0
              C++博客 ::  :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            關(guān)于 TrueCrypt 第三集

            Posted on 2008-09-06 08:55 沒畫完的畫 閱讀(1905) 評(píng)論(1)  編輯 收藏 引用 所屬分類: Windows Driver
            2008.09.04

            首先應(yīng)該了解下在 Windows 下面,應(yīng)用層(Ring3)跟內(nèi)核(Ring0)的通信是如何進(jìn)行的。
            先不管內(nèi)核(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” 這個(gè)設(shè)備,
            據(jù)說 PhysicalDrive0 這個(gè)設(shè)備就是“第一塊物理硬盤”了,至少是不是一定就是引導(dǎo)盤,不太清楚

            問題1:PhysicalDrive0  是不是一定就是引導(dǎo)盤??

            然后通過 DeviceIoControl() 與 設(shè)備交互
            最后 CloseHandle()

            打開設(shè)備就像打開文件一樣,如果把 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);
            }


            為了安全起見,在虛擬機(jī)下運(yùn)行,運(yùn)行結(jié)果如下:
            Error WriteFile: LastErrorCode = 5

            查了 ErrorLookUp
            5 的錯(cuò)誤是 Access is denied.
            哈哈~~~如果 WriteFile 成功不就見鬼了!!!

            從安全的角度來看,NT以上的系統(tǒng)好像是不能直接對(duì)硬盤的絕對(duì)扇區(qū)操作的,至于 WriteFile  PhysicalDrive0 失敗的具體原因,不太清楚。

            問題2: WriteFile  PhysicalDrive0 失敗的具體原因? (ReadFile沒試,理論上應(yīng)當(dāng)也會(huì)失敗)

            總結(jié)一下:
            1、關(guān)于 CreateFile 時(shí)所使用的設(shè)備名
                一些標(biāo)準(zhǔn)設(shè)備的設(shè)備名,微軟已經(jīng)定義好了,
               比如 PhysicalDrive0  這些,如果是自己的驅(qū)動(dòng)所創(chuàng)建的設(shè)備,當(dāng)然名字可以任由自己取

               在 CreateFile 打開設(shè)備時(shí),設(shè)備的名字通常為 \\.\DeviceName (在C++中的字符串則表示為
               \\\\.\\DeviceName)
              
               在驅(qū)動(dòng).sys 當(dāng)然需要做一些東西,才可以讓應(yīng)用層通過這個(gè)設(shè)備名來訪問(具體見下集分解)
              

            2、DeviceIoControl() 這個(gè)函數(shù)是與驅(qū)動(dòng)層通信的關(guān)鍵
               
                一個(gè)操作碼,一個(gè)輸入緩沖區(qū),一個(gè)輸出緩沖區(qū)
                具體做什么,可以當(dāng)然需要驅(qū)動(dòng)與應(yīng)用程序之間事先商量好的,
                微軟同樣定義了一些標(biāo)準(zhǔn)設(shè)備的操作碼,在 winioctl.h 文件中


            今天好累, 去睡覺了~~~~~
             
            問題列表:
            問題1:PhysicalDrive0  是不是一定就是引導(dǎo)的硬盤??
                       -- 未解決

            問題2:WriteFile  PhysicalDrive0 失敗的具體原因? (ReadFile沒試,理論上應(yīng)當(dāng)也會(huì)失敗)
                       -- 未解決

            問題3:如果驅(qū)動(dòng)層在響應(yīng)DeviceIoControl 時(shí)阻塞,那么應(yīng)用層在調(diào)用 DeviceIoContorl() 是不是也會(huì)阻塞?

                       -- 未解決

            Feedback

            # re: 關(guān)于 TrueCrypt 第三集  回復(fù)  更多評(píng)論   

            2008-09-08 00:48 by Bill Gates
            1. 不一定
            2. Administrator沒有權(quán)限寫MBR,不過有權(quán)限打開C:,寫c盤的引導(dǎo)區(qū)
            3. 不管是ring0還是ring3,DeviceIoControl的時(shí)候是一個(gè)線程,所以這個(gè)線程會(huì)block,ring0 block的后果是這個(gè)線程不能被terminate,造成所屬進(jìn)程也不能terminate(不過這個(gè)進(jìn)程其他線程會(huì)被terminate,所以無法用這個(gè)做不能殺死的進(jìn)程干壞事)。
            伊人久久精品影院| 日本精品久久久久中文字幕8 | 无码国内精品久久人妻蜜桃| 99久久亚洲综合精品网站| 精品国产乱码久久久久久郑州公司| 亚洲日本va午夜中文字幕久久| 久久免费国产精品| 日日狠狠久久偷偷色综合0| 精品欧美一区二区三区久久久| 久久久精品波多野结衣| 色99久久久久高潮综合影院| 漂亮人妻被中出中文字幕久久| 亚洲欧美一区二区三区久久| 久久综合亚洲色HEZYO社区| 亚洲乱码中文字幕久久孕妇黑人| 久久亚洲精品成人AV| 国产精品久久久久AV福利动漫| 狠狠色婷婷综合天天久久丁香 | 色综合久久久久无码专区| 久久影院综合精品| 狠狠色婷婷综合天天久久丁香 | 久久国产劲爆AV内射—百度| 亚洲AV无码成人网站久久精品大| 久久久久久无码Av成人影院| 日本福利片国产午夜久久| 亚洲国产精品成人AV无码久久综合影院| 一本一本久久a久久精品综合麻豆| 久久精品国产免费观看| 久久青青草原国产精品免费 | 大香伊人久久精品一区二区| 伊人久久大香线蕉综合Av| 97久久香蕉国产线看观看| 午夜精品久久久久久| 99国产欧美精品久久久蜜芽| 亚洲人AV永久一区二区三区久久| 久久婷婷五月综合国产尤物app| 国产精品一区二区久久精品无码 | 久久国产精品-国产精品| 亚洲国产成人精品无码久久久久久综合| 国产成人精品综合久久久久| 久久无码精品一区二区三区|