 |
|
 |
I am looking to write a file filter for disk.sys that needs to attach to the creaton process before the operating system gains access to the disk/partition. However, I only need to read/write one-two bytes in the MBR nothing more and I am not very clear on the ASM/C required to do this.
Hopefully this example will clarify my intentions.
I need to layer the file filter for disk.sys so that when the Disk class driver creates the device objects representing the raw disks (any). \Device\Harddisk0\DR(0) --> Represents Raw Harddisk 0 it looks in the raw disk MBR at track 0, side 0, sector 1, offset 0x01BC (optionally 0x01BC-0x01BD) and reads the value there and or writes a value to the same location.
dkg0414, your program got me in the right area for conducting this, but I only want to deal with those two bytes not all 512. Any thoughts you can provide would be helpful.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hey Brian,
disk.sys is disk class, so filter would be a disk class filter driver. file filter are filters above the filesystems.
I wrote this thing for special cases in which there is some malware sitting and guarding some specific sectors/clusters on volume.
If you want to write to do some operations on MBR or Boot Sector, easy approach would be do a CreateFile on "\\.\\PhysicalDriveX" where X is the disk number (and not drive/partition number). Then read (ReadFile) out the first sector (which will be MBR) in a buffer, modify the bytes which concern you and do a WriteFile on offset 0.
I am not sure whether this approach will work on vista and later 64bit versions (I think it should work, since you are only modifying MBR or boot sector of partition)
I hope it will help.
Regards Deepak/dkg0414
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi. Thanks. It looks like you're doing everything right here, but oddly, I'm getting far different numbers than what this driver is returning. When I read sector 0, I get the main boot record which starts with 3 numbers and then the ASCII representation for NTFS. This software seems to be pulling data from a sector other than Logical Block Address 0.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi Deepak,
Thanks. Certainly. I'm just calling ReadFile() from a Win32 usermode app on Windows Vista. The subroutine is below. Maybe I'm missing something, but I can't see what could possibly be different. When I read sector 0 using this subroutine, I get the MBR of my drive. When I use your driver to read raw disk sector 0, I get an entirely different set of numbers. But it must be reading a sector somewhere, because I can see the sector fixup in the data that your driver returns. Odd. I'm still working on the problem. There's always a reason.
DWORD ReadSector(LPSTR volume, ULONGLONG sector, unsigned char *q) {
ULONGLONG ull, ull1; unsigned long ul, ul1; unsigned long bytesread = 0; unsigned long sectorsize = 512; char sstr[STRINGLENGTH], device[STRINGLENGTH];
HANDLE hDriver;
strcpy_s(device, volume); _strupr_s(device);
hDriver = CreateFile(device, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if(hDriver != INVALID_HANDLE_VALUE) { ull = sector * sectorsize; ul = (unsigned long) ull; ull >>= 32; ul1 = (unsigned long) ull; SetFilePointer(hDriver, ul, (PLONG) &ul1, FILE_BEGIN); if(!ReadFile(hDriver, q, sectorsize, &bytesread, NULL)) { sprintf_s(sstr, sizeof(sstr), "Error reading sector. Error = %d", GetLastError()); PleaseWait(sstr); } CloseHandle(hDriver); } else { sprintf_s(sstr, sizeof(sstr), "Error opening driver %s", volume); PleaseWait(sstr); }
return(bytesread);
}
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
What is the Volume name here, I mean the input to your ReadSector function. I think you are giving input here as \\.\PhysicalDriveX. And what are you giving inputs to the "DiskSector" utility. Is /disk or /partition? /disk is for the entire disk starting from sector 0 while /partition is from the starting of partition which will not be starting of the disk.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Deepak,
The volume names are \\.\PhysicalDriveX and \\.\X: They both work. I'm reading drives 0 and 1 ( c: and d: ). They both have an MBR on sector 0. (d: is a bootable drive as well). I can see the MBR's and the MFT's, etc. on both drives.
I run your software with the command line:
disksector /disk 1 /read 0
Totally different numbers on sector 0. It's really odd. As far as I can tell, everything looks correct in your source code. Can you see your MBR using this software? I have no idea yet what I could be doing wrong.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hmm ok. AFAIK, MBR is just one, it can't be present on both the drives. Infact it is not at all present on drives (partitions). I think you are talking about Boot Sector (or Boot Record) of specific drives (partitions). MBR stands for Master Boot Record and is present on the start of properly partitioned hard disk. MBR contains info about partitions on the hard drive in a Partition Table (Like start and end of partition, type of partition, etc). It contains other info also (I dont remember offhand right now). Typically after 64 starting sectors (it is a typical value, it can vary also), first partition area starts. So when you read \\.\PhysicalDriveX, you are reading from the starting of the harddisk (i.e MBR will be read). When you read drives i.e (\\.\C:\), then you are reading at a offset on the harddisk (typically 64 sectors as said above) and it's first sector will be a boot sector (if bootable partition). Ok So far so good. Now about the my utility. Below I have pasted the usage of utility again.
DiskSector {/disk | /partition} <rawdisk number | partition number> {/read | /write} <sectornumber> {/unload} Options Meanings :-- {/disk | /partition} <rawdisk number | partition number> Disk and Parition options are mutually exclusive Disk numbering starts from 0 while partition starts from 1 {/read | /write} <sectornumber> Read and Write options are mutually exclusive Sector numbering starts from 0 {/unload} This option simply unloads the support driver e.g "DiskSector /disk 0 /read 0" will read raw sector 0 of harddisk 0
From the above usage it is evident that I am referring /disk as the physicaldrive (i.e the hard disk) and it's numbering starts from 0 (and not 1) while /partition refers to partitions (i.e drives) and their numbering starts from 1 (and not 0).
I hope all above helped, please forgive me if all above was confusing 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks Deepak. Sorry. Yes, I meant Boot Sector. The command line I'm using is:
disksector /disk 1 /read 0
The response is this:
33C0 8ED0 BC00 7C8E C08E D8BE 007C BF00 06B9 0002 FCF3 A450 681C 06CB FBB9 0400 .... 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 55AA
When I use ReadFile() to read the same sector from the same drive the response is this:
EB52 904E 5446 5320 2020 2000 0208 0000 0000 0000 00F8 0000 3F00 FF00 0008 0000 .... 6F20 7265 7374 6172 740D 0A00 0000 0000 0000 0000 0000 0000 809D B2CA 0000 55AA
Note the fixups are the same, and the output from ReadFile() contains the NTFS designator after the first 3 bytes, which is correct. I can't figure out where the data from your utility is coming from.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hmm Are you intending to read the first disk attached (i.e PhysicalDrive0) to your system? If yes then your command line should be "disksector /disk 0 /read 0" I already told you that disk numbering starts from 0, 1, 2 and so on.
Let me know if you were intending to read first disk of your system.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks Deepak. No, I was reading drive D: "/disk 1". But I've tested both drives with both pieces of software. Same results on both drives.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Ok since you are reading a drive, it is a partition and not a physical disk. Even if it is on a different disk, you should specify it as partition. try "/partition 2".
See usage correctly, partition numbering starts as 1,2,3,... while disk numbering starts as 0,1,2,...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
There is a difference between disk and drives. With disk here I am talking about here as physical disks as exposed by the storage stack.
While partition are different partitions on the hard drive. If you intend to read a drive, you need to give /partition option. If you are reading a physical drive then you need to give /disk option.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I don't think you're understanding what I'm saying Deepak. I have two disk drives. \\.\PhysicalDrive0 is disk drive c: and \\.\PhysicalDrive1 is disk drive d:
I've tested everything thoroughly using the ReadFile() command. Everything corresponds to the literature regarding what an NTFS raw disk drive should look like. I'm reading sector 0, the Boot Sector from both drives. Let's stop the confusion here and talk about the first disk. \\.\PhysicalDrive0. What command do you use to read the Boot Sector, raw disk sector 0, on your 1st physical drive. And when you do read it, do you see the Boot Sector, or something else?
By Boot Sector, I mean this:
http://www.ntfs.com/ntfs-partition-boot-sector.htm
This should be in sector 0 of your first physical drive. This is what I see when I use the ReadFile() command.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I'd like to read sectors from my SD card and i have problem - it is recognized as disk but calling
DiskSector /disk 1 /read 0
returns "DeviceIoControl Failed and error code is 1167". it means that the resource may be out of sync. Is there sth. I can do about that? (sorry for my english)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
1>errors in directory c:\disksector\src\exe 1>link : error LNK2001: unresolved external symbol _mainCRTStartup 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _strncat referenced in function _cleanupDriver@0 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _printf referenced in function _uninstallDriver@0 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _memset referenced in function _uninstallDriver@0 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _free referenced in function _DoDeviceIoCtl@24 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _getc referenced in function _DoDeviceIoCtl@24 1>c:\disksector\src\exe\sector_io.obj : error LNK2001: unresolved external symbol __iob 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _malloc referenced in function _DoDeviceIoCtl@24 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol _strtoul referenced in function _main 1>c:\disksector\src\exe\sector_io.obj : error LNK2019: unresolved external symbol __strtoui64 referenced in function _main 1>c:\disksector\bin\fre_wxp_x86\i386\disksector.exe : error LNK1120: 10 unresolved externals
Can you recommend what to change to resolve the above link failure?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You need to point to the correct lib path. I have not used 7600 DDK yet, but looks like their lib path has changed.
Look into the SOURCES file and check out the LIBS, and try to find their location in 7600 DDK install path.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I would like to gain a read/write access to \\.\PhysicalDrive0 under Vista from within a virtual environment (for example vmware). I need this access on the physical sector level to do mainly read-operation in order to create backup-image of the whole device or partition.
Would your code-example help me to get this working?
Currently there is an access restriction under Vista, and any attempt to access \\.\PhysicalDrive0 ends up with access denied.
Thanks in advance.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
hmm vista 32bit or 64bit. How are you calling CreateFile. Use these flags FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE in dwShared param of CreateFile. I really don't see any problem in opening handle to \\.\PhysicalDrive0 on my vista 32bit machine. Or you might have UAC enabled on your vista machine, Try disabling that UAC option from control panel. You should be able to read the PhysicalDrives using CreateFile API.
Regards Deepak
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Dear Deepak,
thank you very much for your reply! It was a nifty hint! Now I can access \\.\PhysicalDrive0 at least in read-only mode.
But anyway, the question about read/write access is still open. The problem is, that I cannot control CreateFile() function because all this stuff runs within the VMware software. The only parameters that I can supply to VMware is the physical device "\\.\PhysicalDrive0" the geometry of this device and that the open mode should be "read-only" or "read-write". All other stuff I cannot control. In case of "read-write" mode I get "access denied" independently of any security setting.
I think about some sort of adapter or filter (or dispatcher) sitting some were on the way to physical device and help me to bypass the read/write requests direct to driver ignoring the Windows control mechanisms. Is it possible?
Sincerely.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If you will give dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE and dwDesiredAccess = GENERIC_READ|GENERIC_WRITE, then also it (CreateFile) will open the handle for you. Now you are saying that you can't control the CreateFile. Can you control the "dwShareMode" that you pass to the VM machine. Or tell the software in VMWare to open the files with SHARE_DELETE, READ and WRITE access. If this is not possible, then probably you would need writing a driver which will be running on the VMMachine as filter driver and will attach to the volumes (see file system filter devpment guide) on system. This particular example is just for tutorial purposes and may crash. A properly developed filter will help you if you can't control the dwShareMode.
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
 |
It looks like you'r constructing IRP's for disk driver which is nice. Two questions:
1. Does this allows you to overwrite files in use. Probably does?
2. Rootkits hooking driver's IRP still can filter your calls, right?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |