1.\" $File: magic.man,v 1.60 2009/05/08 23:02:44 christos Exp $ 2.Dd August 30, 2008 3.Dt MAGIC __FSECTION__ 4.Os 5.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems. 6.Sh NAME 7.Nm magic 8.Nd file command's magic pattern file 9.Sh DESCRIPTION 10This manual page documents the format of the magic file as 11used by the 12.Xr file __CSECTION__ 13command, version __VERSION__. 14The 15.Xr file __CSECTION__ 16command identifies the type of a file using, 17among other tests, 18a test for whether the file contains certain 19.Dq "magic patterns" . 20The file 21.Pa __MAGIC__ 22specifies what patterns are to be tested for, what message or 23MIME type to print if a particular pattern is found, 24and additional information to extract from the file. 25.Pp 26Each line of the file specifies a test to be performed. 27A test compares the data starting at a particular offset 28in the file with a byte value, a string or a numeric value. 29If the test succeeds, a message is printed. 30The line consists of the following fields: 31.Bl -tag -width ".Dv message" 32.It Dv offset 33A number specifying the offset, in bytes, into the file of the data 34which is to be tested. 35.It Dv type 36The type of the data to be tested. 37The possible values are: 38.Bl -tag -width ".Dv lestring16" 39.It Dv byte 40A one-byte value. 41.It Dv short 42A two-byte value in this machine's native byte order. 43.It Dv long 44A four-byte value in this machine's native byte order. 45.It Dv quad 46An eight-byte value in this machine's native byte order. 47.It Dv float 48A 32-bit single precision IEEE floating point number in this machine's native byte order. 49.It Dv double 50A 64-bit double precision IEEE floating point number in this machine's native byte order. 51.It Dv string 52A string of bytes. 53The string type specification can be optionally followed 54by /[Bbc]*. 55The 56.Dq B 57flag compacts whitespace in the target, which must 58contain at least one whitespace character. 59If the magic has 60.Dv n 61consecutive blanks, the target needs at least 62.Dv n 63consecutive blanks to match. 64The 65.Dq b 66flag treats every blank in the target as an optional blank. 67Finally the 68.Dq c 69flag, specifies case insensitive matching: lowercase 70characters in the magic match both lower and upper case characters in the 71target, whereas upper case characters in the magic only match uppercase 72characters in the target. 73.It Dv pstring 74A Pascal-style string where the first byte is interpreted as the an 75unsigned length. 76The string is not NUL terminated. 77.It Dv date 78A four-byte value interpreted as a UNIX date. 79.It Dv qdate 80A eight-byte value interpreted as a UNIX date. 81.It Dv ldate 82A four-byte value interpreted as a UNIX-style date, but interpreted as 83local time rather than UTC. 84.It Dv qldate 85An eight-byte value interpreted as a UNIX-style date, but interpreted as 86local time rather than UTC. 87.It Dv beid3 88A 32-bit ID3 length in big-endian byte order. 89.It Dv beshort 90A two-byte value in big-endian byte order. 91.It Dv belong 92A four-byte value in big-endian byte order. 93.It Dv bequad 94An eight-byte value in big-endian byte order. 95.It Dv befloat 96A 32-bit single precision IEEE floating point number in big-endian byte order. 97.It Dv bedouble 98A 64-bit double precision IEEE floating point number in big-endian byte order. 99.It Dv bedate 100A four-byte value in big-endian byte order, 101interpreted as a Unix date. 102.It Dv beqdate 103An eight-byte value in big-endian byte order, 104interpreted as a Unix date. 105.It Dv beldate 106A four-byte value in big-endian byte order, 107interpreted as a UNIX-style date, but interpreted as local time rather 108than UTC. 109.It Dv beqldate 110An eight-byte value in big-endian byte order, 111interpreted as a UNIX-style date, but interpreted as local time rather 112than UTC. 113.It Dv bestring16 114A two-byte unicode (UCS16) string in big-endian byte order. 115.It Dv leid3 116A 32-bit ID3 length in little-endian byte order. 117.It Dv leshort 118A two-byte value in little-endian byte order. 119.It Dv lelong 120A four-byte value in little-endian byte order. 121.It Dv lequad 122An eight-byte value in little-endian byte order. 123.It Dv lefloat 124A 32-bit single precision IEEE floating point number in little-endian byte order. 125.It Dv ledouble 126A 64-bit double precision IEEE floating point number in little-endian byte order. 127.It Dv ledate 128A four-byte value in little-endian byte order, 129interpreted as a UNIX date. 130.It Dv leqdate 131An eight-byte value in little-endian byte order, 132interpreted as a UNIX date. 133.It Dv leldate 134A four-byte value in little-endian byte order, 135interpreted as a UNIX-style date, but interpreted as local time rather 136than UTC. 137.It Dv leqldate 138An eight-byte value in little-endian byte order, 139interpreted as a UNIX-style date, but interpreted as local time rather 140than UTC. 141.It Dv lestring16 142A two-byte unicode (UCS16) string in little-endian byte order. 143.It Dv melong 144A four-byte value in middle-endian (PDP-11) byte order. 145.It Dv medate 146A four-byte value in middle-endian (PDP-11) byte order, 147interpreted as a UNIX date. 148.It Dv meldate 149A four-byte value in middle-endian (PDP-11) byte order, 150interpreted as a UNIX-style date, but interpreted as local time rather 151than UTC. 152.It Dv indirect 153Starting at the given offset, consult the magic database again. 154.It Dv regex 155A regular expression match in extended POSIX regular expression syntax 156(like egrep). Regular expressions can take exponential time to 157process, and their performance is hard to predict, so their use is 158discouraged. When used in production environments, their performance 159should be carefully checked. The type specification can be optionally 160followed by 161.Dv /[c][s] . 162The 163.Dq c 164flag makes the match case insensitive, while the 165.Dq s 166flag update the offset to the start offset of the match, rather than the end. 167The regular expression is tested against line 168.Dv N + 1 169onwards, where 170.Dv N 171is the given offset. 172Line endings are assumed to be in the machine's native format. 173.Dv ^ 174and 175.Dv $ 176match the beginning and end of individual lines, respectively, 177not beginning and end of file. 178.It Dv search 179A literal string search starting at the given offset. The same 180modifier flags can be used as for string patterns. The modifier flags 181(if any) must be followed by 182.Dv /number 183the range, that is, the number of positions at which the match will be 184attempted, starting from the start offset. This is suitable for 185searching larger binary expressions with variable offsets, using 186.Dv \e 187escapes for special characters. The offset works as for regex. 188.It Dv default 189This is intended to be used with the test 190.Em x 191(which is always true) and a message that is to be used if there are 192no other matches. 193.El 194.Pp 195Each top-level magic pattern (see below for an explanation of levels) 196is classified as text or binary according to the types used. Types 197.Dq regex 198and 199.Dq search 200are classified as text tests, unless non-printable characters are used 201in the pattern. All other tests are classified as binary. A top-level 202pattern is considered to be a test text when all its patterns are text 203patterns; otherwise, it is considered to be a binary pattern. When 204matching a file, binary patterns are tried first; if no match is 205found, and the file looks like text, then its encoding is determined 206and the text patterns are tried. 207.Pp 208The numeric types may optionally be followed by 209.Dv \*[Am] 210and a numeric value, 211to specify that the value is to be AND'ed with the 212numeric value before any comparisons are done. 213Prepending a 214.Dv u 215to the type indicates that ordered comparisons should be unsigned. 216.It Dv test 217The value to be compared with the value from the file. 218If the type is 219numeric, this value 220is specified in C form; if it is a string, it is specified as a C string 221with the usual escapes permitted (e.g. \en for new-line). 222.Pp 223Numeric values 224may be preceded by a character indicating the operation to be performed. 225It may be 226.Dv = , 227to specify that the value from the file must equal the specified value, 228.Dv \*[Lt] , 229to specify that the value from the file must be less than the specified 230value, 231.Dv \*[Gt] , 232to specify that the value from the file must be greater than the specified 233value, 234.Dv \*[Am] , 235to specify that the value from the file must have set all of the bits 236that are set in the specified value, 237.Dv ^ , 238to specify that the value from the file must have clear any of the bits 239that are set in the specified value, or 240.Dv ~ , 241the value specified after is negated before tested. 242.Dv x , 243to specify that any value will match. 244If the character is omitted, it is assumed to be 245.Dv = . 246Operators 247.Dv \*[Am] , 248.Dv ^ , 249and 250.Dv ~ 251don't work with floats and doubles. 252The operator 253.Dv !\& 254specifies that the line matches if the test does 255.Em not 256succeed. 257.Pp 258Numeric values are specified in C form; e.g. 259.Dv 13 260is decimal, 261.Dv 013 262is octal, and 263.Dv 0x13 264is hexadecimal. 265.Pp 266For string values, the string from the 267file must match the specified string. 268The operators 269.Dv = , 270.Dv \*[Lt] 271and 272.Dv \*[Gt] 273(but not 274.Dv \*[Am] ) 275can be applied to strings. 276The length used for matching is that of the string argument 277in the magic file. 278This means that a line can match any non-empty string (usually used to 279then print the string), with 280.Em \*[Gt]\e0 281(because all non-empty strings are greater than the empty string). 282.Pp 283The special test 284.Em x 285always evaluates to true. 286.Dv message 287The message to be printed if the comparison succeeds. 288If the string contains a 289.Xr printf 3 290format specification, the value from the file (with any specified masking 291performed) is printed using the message as the format string. 292If the string begins with 293.Dq \eb , 294the message printed is the remainder of the string with no whitespace 295added before it: multiple matches are normally separated by a single 296space. 297.El 298.Pp 299An APPLE 4+4 character APPLE creator and type can be specified as: 300.Bd -literal -offset indent 301!:apple CREATYPE 302.Ed 303.Pp 304A MIME type is given on a separate line, which must be the next 305non-blank or comment line after the magic line that identifies the 306file type, and has the following format: 307.Bd -literal -offset indent 308!:mime MIMETYPE 309.Ed 310.Pp 311i.e. the literal string 312.Dq !:mime 313followed by the MIME type. 314.Pp 315An optional strength can be supplied on a separate line which refers to 316the current magic description using the following format: 317.Bd -literal -offset indent 318!:strength OP VALUE 319.Ed 320.Pp 321The operand 322.Dv OP 323can be: 324.Dv + , 325.Dv - , 326.Dv * , 327or 328.Dv / 329and 330.Dv VALUE 331is a constant between 0 and 255. 332This constant is applied using the specified operand 333to the currently computed default magic strength. 334.Pp 335Some file formats contain additional information which is to be printed 336along with the file type or need additional tests to determine the true 337file type. 338These additional tests are introduced by one or more 339.Em \*[Gt] 340characters preceding the offset. 341The number of 342.Em \*[Gt] 343on the line indicates the level of the test; a line with no 344.Em \*[Gt] 345at the beginning is considered to be at level 0. 346Tests are arranged in a tree-like hierarchy: 347If a the test on a line at level 348.Em n 349succeeds, all following tests at level 350.Em n+1 351are performed, and the messages printed if the tests succeed, untile a line 352with level 353.Em n 354(or less) appears. 355For more complex files, one can use empty messages to get just the 356"if/then" effect, in the following way: 357.Bd -literal -offset indent 3580 string MZ 359\*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable 360\*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows) 361.Ed 362.Pp 363Offsets do not need to be constant, but can also be read from the file 364being examined. 365If the first character following the last 366.Em \*[Gt] 367is a 368.Em ( 369then the string after the parenthesis is interpreted as an indirect offset. 370That means that the number after the parenthesis is used as an offset in 371the file. 372The value at that offset is read, and is used again as an offset 373in the file. 374Indirect offsets are of the form: 375.Em (( x [.[bislBISL]][+\-][ y ]) . 376The value of 377.Em x 378is used as an offset in the file. 379A byte, id3 length, short or long is read at that offset depending on the 380.Em [bislBISLm] 381type specifier. 382The capitalized types interpret the number as a big endian 383value, whereas the small letter versions interpret the number as a little 384endian value; 385the 386.Em m 387type interprets the number as a middle endian (PDP-11) value. 388To that number the value of 389.Em y 390is added and the result is used as an offset in the file. 391The default type if one is not specified is long. 392.Pp 393That way variable length structures can be examined: 394.Bd -literal -offset indent 395# MS Windows executables are also valid MS-DOS executables 3960 string MZ 397\*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS) 398# skip the whole block below if it is not an extended executable 399\*[Gt]0x18 leshort \*[Gt]0x3f 400\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 401\*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2) 402.Ed 403.Pp 404This strategy of examining has a drawback: You must make sure that 405you eventually print something, or users may get empty output (like, when 406there is neither PE\e0\e0 nor LE\e0\e0 in the above example) 407.Pp 408If this indirect offset cannot be used directly, simple calculations are 409possible: appending 410.Em [+-*/%\*[Am]|^]number 411inside parentheses allows one to modify 412the value read from the file before it is used as an offset: 413.Bd -literal -offset indent 414# MS Windows executables are also valid MS-DOS executables 4150 string MZ 416# sometimes, the value at 0x18 is less that 0x40 but there's still an 417# extended executable, simply appended to the file 418\*[Gt]0x18 leshort \*[Lt]0x40 419\*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP) 420\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS) 421.Ed 422.Pp 423Sometimes you do not know the exact offset as this depends on the length or 424position (when indirection was used before) of preceding fields. 425You can specify an offset relative to the end of the last up-level 426field using 427.Sq \*[Am] 428as a prefix to the offset: 429.Bd -literal -offset indent 4300 string MZ 431\*[Gt]0x18 leshort \*[Gt]0x3f 432\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 433# immediately following the PE signature is the CPU type 434\*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386 435\*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha 436.Ed 437.Pp 438Indirect and relative offsets can be combined: 439.Bd -literal -offset indent 4400 string MZ 441\*[Gt]0x18 leshort \*[Lt]0x40 442\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS) 443# if it's not COFF, go back 512 bytes and add the offset taken 444# from byte 2/3, which is yet another way of finding the start 445# of the extended executable 446\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver) 447.Ed 448.Pp 449Or the other way around: 450.Bd -literal -offset indent 4510 string MZ 452\*[Gt]0x18 leshort \*[Gt]0x3f 453\*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 454# at offset 0x80 (-4, since relative offsets start at the end 455# of the up-level match) inside the LE header, we find the absolute 456# offset to the code area, where we look for a specific signature 457\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed 458.Ed 459.Pp 460Or even both! 461.Bd -literal -offset indent 4620 string MZ 463\*[Gt]0x18 leshort \*[Gt]0x3f 464\*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 465# at offset 0x58 inside the LE header, we find the relative offset 466# to a data area where we look for a specific signature 467\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive 468.Ed 469.Pp 470Finally, if you have to deal with offset/length pairs in your file, even the 471second value in a parenthesized expression can be taken from the file itself, 472using another set of parentheses. 473Note that this additional indirect offset is always relative to the 474start of the main indirect offset. 475.Bd -literal -offset indent 4760 string MZ 477\*[Gt]0x18 leshort \*[Gt]0x3f 478\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 479# search for the PE section called ".idata"... 480\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata 481# ...and go to the end of it, calculated from start+length; 482# these are located 14 and 10 bytes after the section name 483\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive 484.Ed 485.Sh SEE ALSO 486.Xr file __CSECTION__ 487\- the command that reads this file. 488.Sh BUGS 489The formats 490.Dv long , 491.Dv belong , 492.Dv lelong , 493.Dv melong , 494.Dv short , 495.Dv beshort , 496.Dv leshort , 497.Dv date , 498.Dv bedate , 499.Dv medate , 500.Dv ledate , 501.Dv beldate , 502.Dv leldate , 503and 504.Dv meldate 505are system-dependent; perhaps they should be specified as a number 506of bytes (2B, 4B, etc), 507since the files being recognized typically come from 508a system on which the lengths are invariant. 509.\" 510.\" From: guy@sun.uucp (Guy Harris) 511.\" Newsgroups: net.bugs.usg 512.\" Subject: /etc/magic's format isn't well documented 513.\" Message-ID: <2752@sun.uucp> 514.\" Date: 3 Sep 85 08:19:07 GMT 515.\" Organization: Sun Microsystems, Inc. 516.\" Lines: 136 517.\" 518.\" Here's a manual page for the format accepted by the "file" made by adding 519.\" the changes I posted to the S5R2 version. 520.\" 521.\" Modified for Ian Darwin's version of the file command. 522