1 .\" $File: magic.man,v 1.38 2007/01/27 00:52:08 ljt Exp $
5 .\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
8 .Nd file command's magic number 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 begins with a certain
22 specifies what magic numbers are to be tested for,
23 what message to print if a particular magic number 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 1-byte, 2-byte, or 4-byte numeric value or
30 If the test succeeds, a message is printed.
31 The line consists of the following fields:
32 .Bl -tag -width ".Dv message"
34 A number specifying the offset, in bytes, into the file of the data
35 which is to be tested.
37 The type of the data to be tested.
38 The possible values are:
39 .Bl -tag -width ".Dv lestring16"
43 A two-byte value (on most systems) in this machine's native byte order.
45 A four-byte value (on most systems) in this machine's native byte order.
47 An eight-byte value (on most systems) in this machine's native byte order.
50 The string type specification can be optionally followed
54 flag compacts whitespace in the target, which must
55 contain at least one whitespace character.
58 consecutive blanks, the target needs at least
60 consecutive blanks to match.
63 flag treats every blank in the target as an optional blank.
66 flag, specifies case insensitive matching: lowercase
67 characters in the magic match both lower and upper case characters in the
68 targer, whereas upper case characters in the magic, only much uppercase
69 characters in the target.
71 A pascal style string where the first byte is interpreted as the an
73 The string is not NUL terminated.
75 A four-byte value interpreted as a UNIX date.
77 A eight-byte value interpreted as a UNIX date.
79 A four-byte value interpreted as a UNIX-style date, but interpreted as
80 local time rather than UTC.
82 An eight-byte value interpreted as a UNIX-style date, but interpreted as
83 local time rather than UTC.
85 A two-byte value (on most systems) in big-endian byte order.
87 A four-byte value (on most systems) in big-endian byte order.
89 An eight-byte value (on most systems) in big-endian byte order.
91 A four-byte value (on most systems) in big-endian byte order,
92 interpreted as a Unix date.
94 An eight-byte value (on most systems) in big-endian byte order,
95 interpreted as a Unix date.
97 A four-byte value (on most systems) in big-endian byte order,
98 interpreted as a UNIX-style date, but interpreted as local time rather
101 An eight-byte value (on most systems) in big-endian byte order,
102 interpreted as a UNIX-style date, but interpreted as local time rather
105 A two-byte unicode (UCS16) string in big-endian byte order.
107 A two-byte value (on most systems) in little-endian byte order.
109 A four-byte value (on most systems) in little-endian byte order.
111 An eight-byte value (on most systems) in little-endian byte order.
113 A four-byte value (on most systems) in little-endian byte order,
114 interpreted as a UNIX date.
116 An eight-byte value (on most systems) in little-endian byte order,
117 interpreted as a UNIX date.
119 A four-byte value (on most systems) in little-endian byte order,
120 interpreted as a UNIX-style date, but interpreted as local time rather
123 An eight-byte value (on most systems) in little-endian byte order,
124 interpreted as a UNIX-style date, but interpreted as local time rather
127 A two-byte unicode (UCS16) string in little-endian byte order.
129 A four-byte value (on most systems) in middle-endian (PDP-11) byte order.
131 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
132 interpreted as a UNIX date.
134 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
135 interpreted as a UNIX-style date, but interpreted as local time rather
138 A regular expression match in extended POSIX regular expression syntax
140 The type specification can be optionally followed by /[cse]*.
143 flag makes the match case insensitive, while the
147 flags update the offset to the starting or ending offsets of the
148 match (only one should be used).
149 By default, regex does not update the offset.
150 The regular expression is always tested against the first
154 is the given offset, thus it
155 is only useful for (single-byte encoded) text.
159 will match the beginning and end of individual lines, respectively,
160 not beginning and end of file.
162 A literal string search starting at the given offset.
163 It must be followed by
164 .Dv \*[Lt]number\*[Gt]
165 which specifies how many matches shall be attempted (the range).
166 This is suitable for searching larger binary expressions with variable
169 escapes for special characters.
171 This is intended to be used with the text
173 (which is always true) and a message that is to be used if there are
178 The numeric types may optionally be followed by
181 to specify that the value is to be AND'ed with the
182 numeric value before any comparisons are done.
185 to the type indicates that ordered comparisons should be unsigned.
186 .Bl -tag -width ".Dv message"
188 The value to be compared with the value from the file.
191 is specified in C form; if it is a string, it is specified as a C string
192 with the usual escapes permitted (e.g. \en for new-line).
195 may be preceded by a character indicating the operation to be performed.
198 to specify that the value from the file must equal the specified value,
200 to specify that the value from the file must be less than the specified
203 to specify that the value from the file must be greater than the specified
206 to specify that the value from the file must have set all of the bits
207 that are set in the specified value,
209 to specify that the value from the file must have clear any of the bits
210 that are set in the specified value, or
212 the value specified after is negated before tested.
214 to specify that any value will match.
215 If the character is omitted, it is assumed to be
223 specifies that the line matches if the test does
227 Numeric values are specified in C form; e.g.
235 For string values, the byte string from the
236 file must match the specified byte string.
244 can be applied to strings.
245 The length used for matching is that of the string argument
247 This means that a line can match any string, and
248 then presumably print that string, by doing
250 (because all strings are greater than the null string).
254 always evaluates to true.
256 The message to be printed if the comparison succeeds.
257 If the string contains a
259 format specification, the value from the file (with any specified masking
260 performed) is printed using the message as the format string.
261 If the string begins with ``\\b'', the message printed is the
262 remainder of the string with no whitespace added before it: multiple
263 matches are normally separated by a single space.
266 Some file formats contain additional information which is to be printed
267 along with the file type or need additional tests to determine the true
269 These additional tests are introduced by one or more
271 characters preceding the offset.
274 on the line indicates the level of the test; a line with no
276 at the beginning is considered to be at level 0.
277 Tests are arranged in a tree-like hierarchy:
278 If a the test on a line at level
280 succeeds, all following tests at level
282 are performed, and the messages printed if the tests succeed, untile a line
286 For more complex files, one can use empty messages to get just the
287 "if/then" effect, in the following way:
288 .Bd -literal -offset indent
290 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
291 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
294 Offsets do not need to be constant, but can also be read from the file
296 If the first character following the last
300 then the string after the parenthesis is interpreted as an indirect offset.
301 That means that the number after the parenthesis is used as an offset in
303 The value at that offset is read, and is used again as an offset
305 Indirect offsets are of the form:
306 .Em (( x [.[bslBSL]][+\-][ y ]) .
309 is used as an offset in the file.
310 A byte, short or long is read at that offset depending on the
313 The capitalized types interpret the number as a big endian
314 value, whereas the small letter versions interpret the number as a little
318 type interprets the number as a middle endian (PDP-11) value.
319 To that number the value of
321 is added and the result is used as an offset in the file.
322 The default type if one is not specified is long.
324 That way variable length structures can be examined:
325 .Bd -literal -offset indent
326 # MS Windows executables are also valid MS-DOS executables
328 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
329 # skip the whole block below if it is not an extended executable
330 \*[Gt]0x18 leshort \*[Gt]0x3f
331 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
332 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
335 This strategy of examining has one drawback: You must make sure that
336 you eventually print something, or users may get empty output (like, when
337 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
339 If this indirect offset cannot be used as-is, there are simple calculations
341 .Em [+-*/%\*[Am]|^]\*[Lt]number\*[Gt]
342 inside parentheses allows one to modify
343 the value read from the file before it is used as an offset:
344 .Bd -literal -offset indent
345 # MS Windows executables are also valid MS-DOS executables
347 # sometimes, the value at 0x18 is less that 0x40 but there's still an
348 # extended executable, simply appended to the file
349 \*[Gt]0x18 leshort \*[Lt]0x40
350 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
351 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
354 Sometimes you do not know the exact offset as this depends on the length or
355 position (when indirection was used before) of preceding fields.
356 You can specify an offset relative to the end of the last up-level
359 as a prefix to the offset:
360 .Bd -literal -offset indent
362 \*[Gt]0x18 leshort \*[Gt]0x3f
363 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
364 # immediately following the PE signature is the CPU type
365 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
366 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
369 Indirect and relative offsets can be combined:
370 .Bd -literal -offset indent
372 \*[Gt]0x18 leshort \*[Lt]0x40
373 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
374 # if it's not COFF, go back 512 bytes and add the offset taken
375 # from byte 2/3, which is yet another way of finding the start
376 # of the extended executable
377 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
380 Or the other way around:
381 .Bd -literal -offset indent
383 \*[Gt]0x18 leshort \*[Gt]0x3f
384 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
385 # at offset 0x80 (-4, since relative offsets start at the end
386 # of the up-level match) inside the LE header, we find the absolute
387 # offset to the code area, where we look for a specific signature
388 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
392 .Bd -literal -offset indent
394 \*[Gt]0x18 leshort \*[Gt]0x3f
395 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
396 # at offset 0x58 inside the LE header, we find the relative offset
397 # to a data area where we look for a specific signature
398 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
401 Finally, if you have to deal with offset/length pairs in your file, even the
402 second value in a parenthesized expression can be taken from the file itself,
403 using another set of parentheses.
404 Note that this additional indirect offset is always relative to the
405 start of the main indirect offset.
406 .Bd -literal -offset indent
408 \*[Gt]0x18 leshort \*[Gt]0x3f
409 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
410 # search for the PE section called ".idata"...
411 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
412 # ...and go to the end of it, calculated from start+length;
413 # these are located 14 and 10 bytes after the section name
414 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
417 .Xr file __CSECTION__
418 \- the command that reads this file.
436 are system-dependent; perhaps they should be specified as a number
437 of bytes (2B, 4B, etc),
438 since the files being recognized typically come from
439 a system on which the lengths are invariant.
441 .\" From: guy@sun.uucp (Guy Harris)
442 .\" Newsgroups: net.bugs.usg
443 .\" Subject: /etc/magic's format isn't well documented
444 .\" Message-ID: <2752@sun.uucp>
445 .\" Date: 3 Sep 85 08:19:07 GMT
446 .\" Organization: Sun Microsystems, Inc.
449 .\" Here's a manual page for the format accepted by the "file" made by adding
450 .\" the changes I posted to the S5R2 version.
452 .\" Modified for Ian Darwin's version of the file command.
453 .\" @(#)$Id: magic.man,v 1.38 2007/01/27 00:52:08 ljt Exp $