1 .TH MAGIC __FSECTION__ "Public Domain"
2 .\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
4 magic \- file command's magic number file
6 This manual page documents the format of the magic file as
8 .BR file (__CSECTION__)
9 command, version __VERSION__.
12 command identifies the type of a file using,
14 a test for whether the file begins with a certain
18 specifies what magic numbers are to be tested for,
19 what message to print if a particular magic number is found,
20 and additional information to extract from the file.
22 Each line of the file specifies a test to be performed.
23 A test compares the data starting at a particular offset
24 in the file with a 1-byte, 2-byte, or 4-byte numeric value or
26 If the test succeeds, a message is printed.
27 The line consists of the following fields:
28 .IP offset \w'message'u+2n
29 A number specifying the offset, in bytes, into the file of the data
30 which is to be tested.
32 The type of the data to be tested.
33 The possible values are:
35 .IP byte \w'message'u+2n
38 A two-byte value (on most systems) in this machine's native byte order.
40 A four-byte value (on most systems) in this machine's native byte order.
42 An eight-byte value (on most systems) in this machine's native byte order.
45 The string type specification can be optionally followed
47 The ``B'' flag compacts whitespace in the target, which must
48 contain at least one whitespace character.
51 consecutive blanks, the target needs at least
53 consecutive blanks to match.
54 The ``b'' flag treats every blank in the target as an optional blank.
55 Finally the ``c'' flag, specifies case insensitive matching: lowercase
56 characters in the magic match both lower and upper case characters in the
57 targer, whereas upper case characters in the magic, only much uppercase
58 characters in the target.
60 A pascal style string where the first byte is interpreted as the an
61 unsigned length. The string is not NUL terminated.
63 A four-byte value interpreted as a UNIX date.
65 A eight-byte value interpreted as a UNIX date.
67 A four-byte value interpreted as a UNIX-style date, but interpreted as
68 local time rather than UTC.
70 An eight-byte value interpreted as a UNIX-style date, but interpreted as
71 local time rather than UTC.
73 A two-byte value (on most systems) in big-endian byte order.
75 A four-byte value (on most systems) in big-endian byte order.
77 An eight-byte value (on most systems) in big-endian byte order.
79 A four-byte value (on most systems) in big-endian byte order,
80 interpreted as a Unix date.
82 An eight-byte value (on most systems) in big-endian byte order,
83 interpreted as a Unix date.
85 A four-byte value (on most systems) in big-endian byte order,
86 interpreted as a UNIX-style date, but interpreted as local time rather
89 An eight-byte value (on most systems) in big-endian byte order,
90 interpreted as a UNIX-style date, but interpreted as local time rather
93 A two-byte unicode (UCS16) string in big-endian byte order.
95 A two-byte value (on most systems) in little-endian byte order.
97 A four-byte value (on most systems) in little-endian byte order.
99 An eight-byte value (on most systems) in little-endian byte order.
101 A four-byte value (on most systems) in little-endian byte order,
102 interpreted as a UNIX date.
104 An eight-byte value (on most systems) in little-endian byte order,
105 interpreted as a UNIX date.
107 A four-byte value (on most systems) in little-endian byte order,
108 interpreted as a UNIX-style date, but interpreted as local time rather
111 An eight-byte value (on most systems) in little-endian byte order,
112 interpreted as a UNIX-style date, but interpreted as local time rather
115 A two-byte unicode (UCS16) string in little-endian byte order.
117 A four-byte value (on most systems) in middle-endian (PDP-11) byte order.
119 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
120 interpreted as a UNIX date.
122 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
123 interpreted as a UNIX-style date, but interpreted as local time rather
126 A regular expression match in extended POSIX regular expression syntax
128 The type specification can be optionally followed by
130 for case-insensitive matches.
131 The regular expression is always
132 tested against the first
136 is the given offset, thus it
137 is only useful for (single-byte encoded) text.
141 will match the beginning and end of individual lines, respectively,
142 not beginning and end of file.
144 A literal string search starting at the given offset. It must be followed by
146 which specifies how many matches shall be attempted (the range).
147 This is suitable for searching larger binary expressions with variable
150 escapes for special characters.
153 The numeric types may optionally be followed by
156 to specify that the value is to be AND'ed with the
157 numeric value before any comparisons are done.
160 to the type indicates that ordered comparisons should be unsigned.
162 The value to be compared with the value from the file.
165 is specified in C form; if it is a string, it is specified as a C string
166 with the usual escapes permitted (e.g. \en for new-line).
169 may be preceded by a character indicating the operation to be performed.
172 to specify that the value from the file must equal the specified value,
174 to specify that the value from the file must be less than the specified
177 to specify that the value from the file must be greater than the specified
180 to specify that the value from the file must have set all of the bits
181 that are set in the specified value,
183 to specify that the value from the file must have clear any of the bits
184 that are set in the specified value, or
186 the value specified after is negated before tested.
188 to specify that any value will match.
189 If the character is omitted, it is assumed to be
197 specifies that the line matches if the test does
201 Numeric values are specified in C form; e.g.
209 For string values, the byte string from the
210 file must match the specified byte string.
218 can be applied to strings.
219 The length used for matching is that of the string argument
221 This means that a line can match any string, and
222 then presumably print that string, by doing
224 (because all strings are greater than the null string).
226 The message to be printed if the comparison succeeds. If the string
229 format specification, the value from the file (with any specified masking
230 performed) is printed using the message as the format string.
232 Some file formats contain additional information which is to be printed
233 along with the file type or need additional tests to determine the true
235 These additional tests are introduced by one or more
237 characters preceding the offset.
240 on the line indicates the level of the test; a line with no
242 at the beginning is considered to be at level 0.
243 Tests are arranged in a tree-like hierarchy:
244 If a the test on a line at level
246 succeeds, all following tests at level
248 are performed, and the messages printed if the tests succeed, untile a line
252 For more complex files, one can use empty messages to get just the
253 "if/then" effect, in the following way:
257 >0x18 leshort <0x40 MS-DOS executable
258 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
261 Offsets do not need to be constant, but can also be read from the file
263 If the first character following the last
267 then the string after the parenthesis is interpreted as an indirect offset.
268 That means that the number after the parenthesis is used as an offset in
270 The value at that offset is read, and is used again as an offset
272 Indirect offsets are of the form:
273 .BI (( x [.[bslBSL]][+\-][ y ]).
276 is used as an offset in the file. A byte, short or long is read at that offset
280 The capitalized types interpret the number as a big endian
281 value, whereas the small letter versions interpret the number as a little
285 type interprets the number as a middle endian (PDP-11) value.
286 To that number the value of
288 is added and the result is used as an offset in the file.
289 The default type if one is not specified is long.
291 That way variable length structures can be examined:
294 # MS Windows executables are also valid MS-DOS executables
296 >0x18 leshort <0x40 MZ executable (MS-DOS)
297 # skip the whole block below if it is not an extended executable
299 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
300 >>(0x3c.l) string LX\e0\e0 LX executable (OS/2)
303 This strategy of examining has one drawback: You must make sure that
304 you eventually print something, or users may get empty output (like, when
305 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
307 If this indirect offset cannot be used as-is, there are simple calculations
309 .BI [+-*/%&|^]<number>
310 inside parentheses allows one to modify
311 the value read from the file before it is used as an offset:
314 # MS Windows executables are also valid MS-DOS executables
316 # sometimes, the value at 0x18 is less that 0x40 but there's still an
317 # extended executable, simply appended to the file
319 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
320 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
323 Sometimes you do not know the exact offset as this depends on the length or
324 position (when indirection was used before) of preceding fields. You can
325 specify an offset relative to the end of the last up-level field using
327 as a prefix to the offset:
332 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
333 # immediately following the PE signature is the CPU type
334 >>>&0 leshort 0x14c for Intel 80386
335 >>>&0 leshort 0x184 for DEC Alpha
338 Indirect and relative offsets can be combined:
343 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
344 # if it's not COFF, go back 512 bytes and add the offset taken
345 # from byte 2/3, which is yet another way of finding the start
346 # of the extended executable
347 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
350 Or the other way around:
355 >>(0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
356 # at offset 0x80 (-4, since relative offsets start at the end
357 # of the up-level match) inside the LE header, we find the absolute
358 # offset to the code area, where we look for a specific signature
359 >>>(&0x7c.l+0x26) string UPX \eb, UPX compressed
367 >>(0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
368 # at offset 0x58 inside the LE header, we find the relative offset
369 # to a data area where we look for a specific signature
370 >>>&(&0x54.l-3) string UNACE \eb, ACE self-extracting archive
373 Finally, if you have to deal with offset/length pairs in your file, even the
374 second value in a parenthesized expression can be taken from the file itself,
375 using another set of parentheses. Note that this additional indirect offset
376 is always relative to the start of the main indirect offset.
381 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
382 # search for the PE section called ".idata"...
383 >>>&0xf4 search/0x140 .idata
384 # ...and go to the end of it, calculated from start+length;
385 # these are located 14 and 10 bytes after the section name
386 >>>>(&0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
405 are system-dependent; perhaps they should be specified as a number
406 of bytes (2B, 4B, etc),
407 since the files being recognized typically come from
408 a system on which the lengths are invariant.
410 .BR file (__CSECTION__)
411 \- the command that reads this file.
413 .\" From: guy@sun.uucp (Guy Harris)
414 .\" Newsgroups: net.bugs.usg
415 .\" Subject: /etc/magic's format isn't well documented
416 .\" Message-ID: <2752@sun.uucp>
417 .\" Date: 3 Sep 85 08:19:07 GMT
418 .\" Organization: Sun Microsystems, Inc.
421 .\" Here's a manual page for the format accepted by the "file" made by adding
422 .\" the changes I posted to the S5R2 version.
424 .\" Modified for Ian Darwin's version of the file command.
425 .\" @(#)$Id: magic.man,v 1.33 2006/10/31 19:37:16 christos Exp $