您的位置:首页 > 其它

FAT32文件系统格式实例浅析

2010-06-01 17:51 549 查看
A FAT file system volume is composed of four basic regions
which are laid out in this order on the volume:
0 – Reserved Region
1 – FAT Region
2 – Root Directory Region (doesn’t exist on FAT32 volumes)
3 – File and Directory Data Region
DBR就是我们的Boot扇区:
DBR的格式:
jmp $+$BPB_FAT32_END;
_emit NOP;
BPB label byte
......
BPB end
FAT32 label byte
.....
FAT32 end

$BPB_FAT32_END:
xor ax, ax
mov ss, ax
mov sp, 7c00h
....
检查int13/int13扩展中断是否可用
读取BPB/FAT32信息
通过int13/int13扩展中断读取ntldr文件描述信息
再读取ntldr文件数据
jmp ntldr-OEP
org 510
dw 55h,AAh
BPB/FAT32中的主要信息(本机):
1个簇有 16个扇区(8192个字节)
1个磁道有 63个扇区
保留扇区 32个扇区(for DBR)
FAT 10235个扇区

所以根目录起始扇区 10235*2+32 = 20502, 起始簇号 2
/* FAT12/16中 数据区的开始簇号是2,
/* FAT32中的没有根目录的概念就在此,
/* 只是把根目录算做数据区了
/* int13中断只能读取到8.4GB的数据, 再大就
/* 读不到了,int13扩展中断就是解决这个问题

[FAT 32 Byte Directory Entry Structure]
Name Offset (byte) Size (bytes) Description
DIR_Name 0 11 Short name.
DIR_Attr 11(0BH) 1 File attributes:
ATTR_READ_ONLY 0x01
ATTR_HIDDEN 0x02
ATTR_SYSTEM 0x04
ATTR_VOLUME_ID 0x08
ATTR_DIRECTORY 0x10
ATTR_ARCHIVE 0x20
ATTR_LONG_NAME |ATTR_READ_ONLY | ATTR_HIDDEN |
ATTR_SYSTEM | ATTR_VOLUME_ID The upper two bits
of the attribute byte are reserved and should
always be set to 0 when a file is created and
never modified or looked at after that.
DIR_NTRes 12(0CH) 1 Reserved for use by Windows NT. Set value to 0 when
a file is created and never modify or look at it
after that.
DIR_CrtTimeTenth 13(0DH) 1 Millisecond stamp at file creation time. This field
actually contains a count of tenths of a second.
The granularity of the seconds part of DIR_CrtTime is 2
seconds so this field is a count of tenths of a second
and its valid value range is 0-199 inclusive.
DIR_CrtTime 14(0EH) 2 Time file was created.
DIR_CrtDate 16(10H) 2 Date file was created.
DIR_LstAccDate 18(12H) 2 Last access date. Note that there is no last access time,
only a date. This is the date of last read or write.
In the case of a write, this should be set to the same
date as DIR_WrtDate.
DIR_FstClusHI 20(14H) 2 High word of this entry’s first cluster number
(always 0 for a FAT12 or FAT16 volume).
DIR_WrtTime 22(16H) 2 Time of last write. Note that file creation is
considered a write.
DIR_WrtDate 24(18H) 2 Date of last write. Note that file creation is
considered a write.
DIR_FstClusLO 26(1AH) 2 Low word of this entry’s first cluster number.
DIR_FileSize 28(1CH) 4 32-bit DWORD holding this file’s size in bytes.
[公式:
1 根目录的位置 = 保留扇区 + FAT表所占的扇区 * FAT表个数 (单位=扇区)
2 指定簇所在的位置 = 根目录所在扇区 + (簇号 - 根目录所在簇号)*(扇数/簇) (单位=扇区)
3 指定簇在FAT表中的位置 = 保留扇区 * (字节/扇区) + 簇号*(簇在FAT表中描述所占的字节数) (单位=字节)
]
--------------------------------------------------
BPB/FAT32中的主要信息(本机):
1个簇有 16个扇区(8192个字节)
1个磁道有 63个扇区
保留扇区 32个扇区(for DBR)
FAT 10235个扇区
1个扇区 512字节
---------------------------------------------------------------------------
(下面表述中 簇号用10进制; 扇区号用10进制; 逻辑偏移用16进制; 工具WINHEX)
eg: 查找c:/windows/system32/ntoskrnl.exe的过程:
1) 定位根目录的位置: FAT*2 + Reserved = 10235*2+32 = 20502(扇区) ------ 0x000A02C00(逻辑偏移20502*512)
2) 读根目录中的数据(INT13),找到windows目录的描述
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
000A036A0 57 49 4E 44 4F 57 53 20 20 20 20 10 00 46 F7 B1 WINDOWS ..F鞅
000A036B0 21 37 2B 37 00 00 F8 B1 21 37 03 14 00 00 00 00 !7+7..!7......
/* 对照上表,0BH位是0x10----是一个目录
/* 我感兴趣的只有它的簇号(14H & 1AH):0x00001403

3) 找到Windows目录中的目录项所在的第一个簇号: 0x00001403 = 5123(簇)
对应扇区号: 20502 + (5123 - 2) * 16 = 102438(扇区) ------ 0x003204C00(逻辑偏移102438*512)
为啥要减2? 因为20502对应的2号簇!

对应FAT表中的位置: Reserved*512 + 5123*4 = 0x0900C(逻辑偏移)
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
000009000 64 46 00 00 dF..
/* Windows目录表中的目录项所在的下个簇号: 0x00004664 = 18020(簇)

下个簇号对应FAT表中的位置: Reserved*512 + 18020*4 = 0x015990(逻辑偏移)
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
000015990 FF FF FF 0F .
/* 再下一下簇号是? 0x0FFFFFFF表示最后一个簇了

/* 在上述两个簇中找System32的描述(运气真好,在第一簇(5123)中就找到了)
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
003204C40 53 59 53 54 45 4D 33 32 20 20 20 10 08 49 F7 B1 SYSTEM32 ..I鞅
003204C50 21 37 33 37 00 00 F8 B1 21 37 04 14 00 00 00 00 !737..!7......
/* 对照上表,0BH位是0x10----是一个目录
/* 我感兴趣的只有它的簇号(14H & 1AH): 0x00001404 = 5124(簇)

4) 找到System32目录中的目录项所在第一个簇号: 0x00001404 = 5124(簇)
对应扇区号: 20502 + (5124 - 2) * 16 = 102454(扇区) ------ 0x003206C00(逻辑偏移102454*512)

对应FAT表中的位置: Reserved*512 + 5124*4 = 0x09010(逻辑偏移)
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
000009010 7A 46 00 00 zF..
/* System32目录表中的目录项所在的下个簇号: 0x0000467A = 18042(簇)

在FAT表中继续找下去......
System32目录表中的目录项所在的所有簇是:5124 18042 24684 47193 55087 62856 65459 54377 5884 5166

5) 我们在18042簇中找到了Ntosrkrnl.exe的文件描述信息
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
01F510700 4E 54 4F 53 4B 52 4E 4C 45 58 45 20 18 00 00 80 NTOSKRNLEXE ...€
01F510710 9E 33 32 37 07 00 3C 00 61 36 5E 8F 00 96 20 00 ?27..<.a6^??.
/* 对照上表,0BH位是0x20----是一个存档文件
/* 我感兴趣的还有它的簇号(14H & 1AH):0x00078F5E

6) 找到Ntoskrnl.exe文件数据所在的第一个簇号: 0x00078F5E = 495454(簇)
对应扇区号: 20502 + (495454 - 2) * 16 = 7947734(扇区) ------ 0x0F28BAC00(逻辑偏移)
对应FAT表中的位置: Reserved*512 + 495454*4 = 0x1E7D78(逻辑偏移)
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
0001E7D70 5F 8F 07 00 60 8F 07 00 _?.`?.
0001E7D80 61 8F 07 00 62 8F 07 00 63 8F 07 00 64 8F 07 00 a?.b?.c?.d?.
/* Ntosrkrnl.exe文件数据所在的下个簇号: 0x00078F5F = 495455(簇)
在FAT表中继续找下去......

我们来看一下Ntoskrnl.exe第一个簇中的数据:
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
0F28BAC00 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 MZ?..........
0F28BAC10 B8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ?......@.......
0F28BAC20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0F28BAC30 00 00 00 00 00 00 00 00 00 00 00 00 D8 00 00 00 ............?..
0F28BAC40 0E 1F BA 0E 00 B4 09 CD 21 B8 01 4C CD 21 54 68 ..?.???L?Th
0F28BAC50 69 73 20 70 72 6F 67 72 61 6D 20 63 61 6E 6E 6F is program canno
0F28BAC60 74 20 62 65 20 72 75 6E 20 69 6E 20 44 4F 53 20 t be run in DOS
0F28BAC70 6D 6F 64 65 2E 0D 0D 0A 24 00 00 00 00 00 00 00 mode....$.......
0F28BAC80 FE 3C 69 99 BA 5D 07 CA BA 5D 07 CA BA 5D 07 CA ?i櫤].屎].屎].?
0F28BAC90 79 52 5A CA BD 5D 07 CA BA 5D 06 CA ED 5D 07 CA yRZ式].屎].薯].?
0F28BACA0 79 52 08 CA ED 5D 07 CA 79 52 59 CA BB 5D 07 CA yR.薯].蕐RY驶].?
0F28BACB0 79 52 58 CA 3D 5F 07 CA 79 52 5B CA BB 5D 07 CA yRX?_.蕐R[驶].?
0F28BACC0 79 52 5D CA BB 5D 07 CA 52 69 63 68 BA 5D 07 CA yR]驶].蔙ich篯.?
0F28BACD0 00 00 00 00 00 00 00 00 50 45 00 00 4C 01 15 00 ........PE..L...
7) 很熟悉吧? 以上六步所有数据结果都是通过计算器算出来了,不是把WINHEX中的东西直接贴出来的

8) 现在知道为什么把ntldr放在系统根目录下的原因了吧?
快速定位,要在512个字节(其实远小于这个啦 BPB&FAT描述信息&出错提示信息 也要占空间的)内搞定这些!!

(PS: 前段时间在网在看到一人说自己写了一个Rootkit,可以绕过所有anti-Rootkit软件的检测
在别的系统下也看不到这个Rootkit, 只实现了FAT32系统下的, 估计就用到"隐藏扇区"的
技术, 把此RootKit所在的簇在FAT表中的描述改为坏道——0x0FFFFFF7; 没拿到bin, 没
有Disasm, 不敢断言)
术语:
DBR(Dos Boot Record) 操作系统引导记录区
FAT(File Allocation Table) 文件分配表
参考资料 《FAT32规范》
《细说IDE硬盘的容量限制》

洗澡睡觉~ windowssky by 20070920 02:17
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: