1 .\" $File: magic.man,v 1.59 2008/11/06 23:22:53 christos Exp $
5 .\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
8 .Nd file command's magic pattern file
10 This manual page documents the format of the magic file as
13 command, version __VERSION__.
16 command identifies the type of a file using,
18 a test for whether the file contains certain
19 .Dq "magic patterns" .
22 specifies what patterns are to be tested for, what message or
23 MIME type to print if a particular pattern is found,
24 and additional information to extract from the file.
26 Each line of the file specifies a test to be performed.
27 A test compares the data starting at a particular offset
28 in the file with a byte value, a string or a numeric value.
29 If the test succeeds, a message is printed.
30 The line consists of the following fields:
31 .Bl -tag -width ".Dv message"
33 A number specifying the offset, in bytes, into the file of the data
34 which is to be tested.
36 The type of the data to be tested.
37 The possible values are:
38 .Bl -tag -width ".Dv lestring16"
42 A two-byte value in this machine's native byte order.
44 A four-byte value in this machine's native byte order.
46 An eight-byte value in this machine's native byte order.
48 A 32-bit single precision IEEE floating point number in this machine's native byte order.
50 A 64-bit double precision IEEE floating point number in this machine's native byte order.
53 The string type specification can be optionally followed
57 flag compacts whitespace in the target, which must
58 contain at least one whitespace character.
61 consecutive blanks, the target needs at least
63 consecutive blanks to match.
66 flag treats every blank in the target as an optional blank.
69 flag, specifies case insensitive matching: lowercase
70 characters in the magic match both lower and upper case characters in the
71 target, whereas upper case characters in the magic only match uppercase
72 characters in the target.
74 A Pascal-style string where the first byte is interpreted as the an
76 The string is not NUL terminated.
78 A four-byte value interpreted as a UNIX date.
80 A eight-byte value interpreted as a UNIX date.
82 A four-byte value interpreted as a UNIX-style date, but interpreted as
83 local time rather than UTC.
85 An eight-byte value interpreted as a UNIX-style date, but interpreted as
86 local time rather than UTC.
88 A 32-bit ID3 length in big-endian byte order.
90 A two-byte value in big-endian byte order.
92 A four-byte value in big-endian byte order.
94 An eight-byte value in big-endian byte order.
96 A 32-bit single precision IEEE floating point number in big-endian byte order.
98 A 64-bit double precision IEEE floating point number in big-endian byte order.
100 A four-byte value in big-endian byte order,
101 interpreted as a Unix date.
103 An eight-byte value in big-endian byte order,
104 interpreted as a Unix date.
106 A four-byte value in big-endian byte order,
107 interpreted as a UNIX-style date, but interpreted as local time rather
110 An eight-byte value in big-endian byte order,
111 interpreted as a UNIX-style date, but interpreted as local time rather
114 A two-byte unicode (UCS16) string in big-endian byte order.
116 A 32-bit ID3 length in little-endian byte order.
118 A two-byte value in little-endian byte order.
120 A four-byte value in little-endian byte order.
122 An eight-byte value in little-endian byte order.
124 A 32-bit single precision IEEE floating point number in little-endian byte order.
126 A 64-bit double precision IEEE floating point number in little-endian byte order.
128 A four-byte value in little-endian byte order,
129 interpreted as a UNIX date.
131 An eight-byte value in little-endian byte order,
132 interpreted as a UNIX date.
134 A four-byte value in little-endian byte order,
135 interpreted as a UNIX-style date, but interpreted as local time rather
138 An eight-byte value in little-endian byte order,
139 interpreted as a UNIX-style date, but interpreted as local time rather
142 A two-byte unicode (UCS16) string in little-endian byte order.
144 A four-byte value in middle-endian (PDP-11) byte order.
146 A four-byte value in middle-endian (PDP-11) byte order,
147 interpreted as a UNIX date.
149 A four-byte value in middle-endian (PDP-11) byte order,
150 interpreted as a UNIX-style date, but interpreted as local time rather
153 Starting at the given offset, consult the magic database again.
155 A regular expression match in extended POSIX regular expression syntax
156 (like egrep). Regular expressions can take exponential time to
157 process, and their performance is hard to predict, so their use is
158 discouraged. When used in production environments, their performance
159 should be carefully checked. The type specification can be optionally
164 flag makes the match case insensitive, while the
166 flag update the offset to the start offset of the match, rather than the end.
167 The regular expression is tested against line
172 Line endings are assumed to be in the machine's native format.
176 match the beginning and end of individual lines, respectively,
177 not beginning and end of file.
179 A literal string search starting at the given offset. The same
180 modifier flags can be used as for string patterns. The modifier flags
181 (if any) must be followed by
183 the range, that is, the number of positions at which the match will be
184 attempted, starting from the start offset. This is suitable for
185 searching larger binary expressions with variable offsets, using
187 escapes for special characters. The offset works as for regex.
189 This is intended to be used with the test
191 (which is always true) and a message that is to be used if there are
195 Each top-level magic pattern (see below for an explanation of levels)
196 is classified as text or binary according to the types used. Types
200 are classified as text tests, unless non-printable characters are used
201 in the pattern. All other tests are classified as binary. A top-level
202 pattern is considered to be a test text when all its patterns are text
203 patterns; otherwise, it is considered to be a binary pattern. When
204 matching a file, binary patterns are tried first; if no match is
205 found, and the file looks like text, then its encoding is determined
206 and the text patterns are tried.
208 The numeric types may optionally be followed by
211 to specify that the value is to be AND'ed with the
212 numeric value before any comparisons are done.
215 to the type indicates that ordered comparisons should be unsigned.
217 The value to be compared with the value from the file.
220 is specified in C form; if it is a string, it is specified as a C string
221 with the usual escapes permitted (e.g. \en for new-line).
224 may be preceded by a character indicating the operation to be performed.
227 to specify that the value from the file must equal the specified value,
229 to specify that the value from the file must be less than the specified
232 to specify that the value from the file must be greater than the specified
235 to specify that the value from the file must have set all of the bits
236 that are set in the specified value,
238 to specify that the value from the file must have clear any of the bits
239 that are set in the specified value, or
241 the value specified after is negated before tested.
243 to specify that any value will match.
244 If the character is omitted, it is assumed to be
251 don't work with floats and doubles.
254 specifies that the line matches if the test does
258 Numeric values are specified in C form; e.g.
266 For string values, the string from the
267 file must match the specified string.
275 can be applied to strings.
276 The length used for matching is that of the string argument
278 This means that a line can match any non-empty string (usually used to
279 then print the string), with
281 (because all non-empty strings are greater than the empty string).
285 always evaluates to true.
287 The message to be printed if the comparison succeeds.
288 If the string contains a
290 format specification, the value from the file (with any specified masking
291 performed) is printed using the message as the format string.
292 If the string begins with
294 the message printed is the remainder of the string with no whitespace
295 added before it: multiple matches are normally separated by a single
299 An APPLE 4+4 character APPLE creator and type can be specified as:
300 .Bd -literal -offset indent
304 A MIME type is given on a separate line, which must be the next
305 non-blank or comment line after the magic line that identifies the
306 file type, and has the following format:
307 .Bd -literal -offset indent
311 i.e. the literal string
313 followed by the MIME type.
315 An optional strength can be supplied on a separate line which refers to
316 the current magic description using the following format:
317 .Bd -literal -offset indent
331 is a constant between 0 and 255.
332 This constant is applied using the specified operand
333 to the currently computed default magic strength.
335 Some file formats contain additional information which is to be printed
336 along with the file type or need additional tests to determine the true
338 These additional tests are introduced by one or more
340 characters preceding the offset.
343 on the line indicates the level of the test; a line with no
345 at the beginning is considered to be at level 0.
346 Tests are arranged in a tree-like hierarchy:
347 If a the test on a line at level
349 succeeds, all following tests at level
351 are performed, and the messages printed if the tests succeed, untile a line
355 For 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
359 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
360 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
363 Offsets do not need to be constant, but can also be read from the file
365 If the first character following the last
369 then the string after the parenthesis is interpreted as an indirect offset.
370 That means that the number after the parenthesis is used as an offset in
372 The value at that offset is read, and is used again as an offset
374 Indirect offsets are of the form:
375 .Em (( x [.[bislBISL]][+\-][ y ]) .
378 is used as an offset in the file.
379 A byte, id3 length, short or long is read at that offset depending on the
382 The capitalized types interpret the number as a big endian
383 value, whereas the small letter versions interpret the number as a little
387 type interprets the number as a middle endian (PDP-11) value.
388 To that number the value of
390 is added and the result is used as an offset in the file.
391 The default type if one is not specified is long.
393 That way variable length structures can be examined:
394 .Bd -literal -offset indent
395 # MS Windows executables are also valid MS-DOS executables
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)
404 This strategy of examining has a drawback: You must make sure that
405 you eventually print something, or users may get empty output (like, when
406 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
408 If this indirect offset cannot be used directly, simple calculations are
410 .Em [+-*/%\*[Am]|^]number
411 inside parentheses allows one to modify
412 the 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
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)
423 Sometimes you do not know the exact offset as this depends on the length or
424 position (when indirection was used before) of preceding fields.
425 You can specify an offset relative to the end of the last up-level
428 as a prefix to the offset:
429 .Bd -literal -offset indent
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
438 Indirect and relative offsets can be combined:
439 .Bd -literal -offset indent
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)
449 Or the other way around:
450 .Bd -literal -offset indent
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
461 .Bd -literal -offset indent
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
470 Finally, if you have to deal with offset/length pairs in your file, even the
471 second value in a parenthesized expression can be taken from the file itself,
472 using another set of parentheses.
473 Note that this additional indirect offset is always relative to the
474 start of the main indirect offset.
475 .Bd -literal -offset indent
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
486 .Xr file __CSECTION__
487 \- the command that reads this file.
505 are system-dependent; perhaps they should be specified as a number
506 of bytes (2B, 4B, etc),
507 since the files being recognized typically come from
508 a system on which the lengths are invariant.
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.
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.
521 .\" Modified for Ian Darwin's version of the file command.