起因:
要对处理图片文件组件做fuzz,github搜到了一篇文章(如何使用Fuzzing挖掘ImageMagick的漏洞),对ImageMagick做fuzz,看到了有magic这个宏定义,于是kimi搜了下,原来是每个文件都有一个魔数。
什么是魔数?
魔数是文件的前几位,它唯一地标识了文件的类型。这使得编程更容易,因为不需要搜索复杂的文件结构来识别文件类型。
例如,jpeg文件以ffd8 ffe0 0010 4a46 4946 0001 0101 0047......JFIF........G开头,ffd8表示它是JPEG文件,ffe0标识JFIF类型结构。有一个ascii编码“JFIF”,它在长度代码之后出现,但这不是识别文件所必需的。前4个字节唯一地做到这一点。
这给出了一个正在进行的文件类型魔法数列表。
Image Files
File type | Typical extension | Hex digits xx = variable | Ascii digits . = not an ascii char |
---|---|---|---|
Bitmap format | .bmp | 42 4d | BM |
FITS format | .fits | 53 49 4d 50 4c 45 | SIMPLE |
GIF format | .gif | 47 49 46 38 | GIF8 |
Graphics Kernel System | .gks | 47 4b 53 4d | GKSM |
IRIS rgb format | .rgb | 01 da | .. |
ITC (CMU WM) format | .itc | f1 00 40 bb | .... |
JPEG File Interchange Format | .jpg | ff d8 ff e0 | .... |
NIFF (Navy TIFF) | .nif | 49 49 4e 31 | IIN1 |
PM format | .pm | 56 49 45 57 | VIEW |
PNG format | .png | 89 50 4e 47 | .PNG |
Postscript format | .[e]ps | 25 21 | %! |
Sun Rasterfile | .ras | 59 a6 6a 95 | Y.j. |
Targa format | .tga | xx xx xx | ... |
TIFF format (Motorola - big endian) | .tif | 4d 4d 00 2a | MM.* |
TIFF format (Intel - little endian) | .tif | 49 49 2a 00 | II*. |
X11 Bitmap format | .xbm | xx xx | |
XCF Gimp file structure | .xcf | 67 69 6d 70 20 78 63 66 20 76 | gimp xcf |
Xfig format | .fig | 23 46 49 47 | #FIG |
XPM format | .xpm | 2f 2a 20 58 50 4d 20 2a 2f | /* XPM */ |
Compressed files
File type | Typical extension | Hex digits xx = variable | Ascii digits . = not an ascii char |
---|---|---|---|
Bzip | .bz | 42 5a | BZ |
Compress | .Z | 1f 9d | .. |
gzip format | .gz | 1f 8b | .. |
pkzip format | .zip | 50 4b 03 04 | PK.. |
参考: