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.
43 The string type specification can be optionally followed
45 The ``B'' flag compacts whitespace in the target, which must
46 contain at least one whitespace character.
49 consecutive blanks, the target needs at least
51 consecutive blanks to match.
52 The ``b'' flag treats every blank in the target as an optional blank.
53 Finally the ``c'' flag, specifies case insensitive matching: lowercase
54 characters in the magic match both lower and upper case characters in the
55 targer, whereas upper case characters in the magic, only much uppercase
56 characters in the target.
58 A pascal style string where the first byte is interpreted as the an
59 unsigned length. The string is not NUL terminated.
61 A four-byte value interpreted as a UNIX date.
63 A four-byte value interpreted as a UNIX-style date, but interpreted as
64 local time rather than UTC.
66 A two-byte value (on most systems) in big-endian byte order.
68 A four-byte value (on most systems) in big-endian byte order.
70 A four-byte value (on most systems) in big-endian byte order,
71 interpreted as a Unix date.
73 A four-byte value (on most systems) in big-endian byte order,
74 interpreted as a UNIX-style date, but interpreted as local time rather
77 A two-byte unicode (UCS16) string in big-endian byte order.
79 A two-byte value (on most systems) in little-endian byte order.
81 A four-byte value (on most systems) in little-endian byte order.
83 A four-byte value (on most systems) in little-endian byte order,
84 interpreted as a UNIX date.
86 A four-byte value (on most systems) in little-endian byte order,
87 interpreted as a UNIX-style date, but interpreted as local time rather
90 A two-byte unicode (UCS16) string in little-endian byte order.
92 A four-byte value (on most systems) in middle-endian (PDP-11) byte order.
94 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
95 interpreted as a UNIX date.
97 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
98 interpreted as a UNIX-style date, but interpreted as local time rather
101 A regular expression match in extended POSIX regular expression syntax
103 The type specification can be optionally followed by
105 for case-insensitive matches.
106 The regular expression is always
107 tested against the first
111 is the given offset, thus it
112 is only useful for (single-byte encoded) text.
116 will match the beginning and end of individual lines, respectively,
117 not beginning and end of file.
119 A literal string search starting at the given offset. It must be followed by
121 which specifies how many matches shall be attempted (the range).
122 This is suitable for searching larger binary expressions with variable
125 escapes for special characters.
128 The numeric types may optionally be followed by
131 to specify that the value is to be AND'ed with the
132 numeric value before any comparisons are done.
135 to the type indicates that ordered comparisons should be unsigned.
137 The value to be compared with the value from the file.
140 is specified in C form; if it is a string, it is specified as a C string
141 with the usual escapes permitted (e.g. \en for new-line).
144 may be preceded by a character indicating the operation to be performed.
147 to specify that the value from the file must equal the specified value,
149 to specify that the value from the file must be less than the specified
152 to specify that the value from the file must be greater than the specified
155 to specify that the value from the file must have set all of the bits
156 that are set in the specified value,
158 to specify that the value from the file must have clear any of the bits
159 that are set in the specified value, or
161 the value specified after is negated before tested.
163 to specify that any value will match.
164 If the character is omitted, it is assumed to be
172 specifies that the line matches if the test does
176 Numeric values are specified in C form; e.g.
184 For string values, the byte string from the
185 file must match the specified byte string.
193 can be applied to strings.
194 The length used for matching is that of the string argument
196 This means that a line can match any string, and
197 then presumably print that string, by doing
199 (because all strings are greater than the null string).
201 The message to be printed if the comparison succeeds. If the string
204 format specification, the value from the file (with any specified masking
205 performed) is printed using the message as the format string.
207 Some file formats contain additional information which is to be printed
208 along with the file type or need additional tests to determine the true
210 These additional tests are introduced by one or more
212 characters preceding the offset.
215 on the line indicates the level of the test; a line with no
217 at the beginning is considered to be at level 0.
218 Tests are arranged in a tree-like hierarchy:
219 If a the test on a line at level
221 succeeds, all following tests at level
223 are performed, and the messages printed if the tests succeed, untile a line
227 For more complex files, one can use empty messages to get just the
228 "if/then" effect, in the following way:
232 >0x18 leshort <0x40 MS-DOS executable
233 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
236 Offsets do not need to be constant, but can also be read from the file
238 If the first character following the last
242 then the string after the parenthesis is interpreted as an indirect offset.
243 That means that the number after the parenthesis is used as an offset in
245 The value at that offset is read, and is used again as an offset
247 Indirect offsets are of the form:
248 .BI (( x [.[bslBSL]][+\-][ y ]).
251 is used as an offset in the file. A byte, short or long is read at that offset
255 The capitalized types interpret the number as a big endian
256 value, whereas the small letter versions interpret the number as a little
260 type interprets the number as a middle endian (PDP-11) value.
261 To that number the value of
263 is added and the result is used as an offset in the file.
264 The default type if one is not specified is long.
266 That way variable length structures can be examined:
269 # MS Windows executables are also valid MS-DOS executables
271 >0x18 leshort <0x40 MZ executable (MS-DOS)
272 # skip the whole block below if it is not an extended executable
274 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
275 >>(0x3c.l) string LX\e0\e0 LX executable (OS/2)
278 This strategy of examining has one drawback: You must make sure that
279 you eventually print something, or users may get empty output (like, when
280 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
282 If this indirect offset cannot be used as-is, there are simple calculations
284 .BI [+-*/%&|^]<number>
285 inside parentheses allows one to modify
286 the value read from the file before it is used as an offset:
289 # MS Windows executables are also valid MS-DOS executables
291 # sometimes, the value at 0x18 is less that 0x40 but there's still an
292 # extended executable, simply appended to the file
294 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
295 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
298 Sometimes you do not know the exact offset as this depends on the length or
299 position (when indirection was used before) of preceding fields. You can
300 specify an offset relative to the end of the last uplevel field using
302 as a prefix to the offset:
307 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
308 # immediately following the PE signature is the CPU type
309 >>>&0 leshort 0x14c for Intel 80386
310 >>>&0 leshort 0x184 for DEC Alpha
313 Indirect and relative offsets can be combined:
318 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
319 # if it's not COFF, go back 512 bytes and add the offset taken
320 # from byte 2/3, which is yet another way of finding the start
321 # of the extended executable
322 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
325 Or the other way around:
330 >>(0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
331 # at offset 0x80 (-4, since relative offsets start at the end
332 # of the uplevel match) inside the LE header, we find the absolute
333 # offset to the code area, where we look for a specific signature
334 >>>(&0x7c.l+0x26) string UPX \eb, UPX compressed
342 >>(0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
343 # at offset 0x58 inside the LE header, we find the relative offset
344 # to a data area where we look for a specific signature
345 >>>&(&0x54.l-3) string UNACE \eb, ACE self-extracting archive
348 Finally, if you have to deal with offset/length pairs in your file, even the
349 second value in a parenthesed expression can be taken from the file itself,
350 using another set of parentheses. Note that this additional indirect offset
351 is always relative to the start of the main indirect offset.
356 >>(0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
357 # search for the PE section called ".idata"...
358 >>>&0xf4 search/0x140 .idata
359 # ...and go to the end of it, calculated from start+length;
360 # these are located 14 and 10 bytes after the section name
361 >>>>(&0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
380 are system-dependent; perhaps they should be specified as a number
381 of bytes (2B, 4B, etc),
382 since the files being recognized typically come from
383 a system on which the lengths are invariant.
385 .BR file (__CSECTION__)
386 \- the command that reads this file.
388 .\" From: guy@sun.uucp (Guy Harris)
389 .\" Newsgroups: net.bugs.usg
390 .\" Subject: /etc/magic's format isn't well documented
391 .\" Message-ID: <2752@sun.uucp>
392 .\" Date: 3 Sep 85 08:19:07 GMT
393 .\" Organization: Sun Microsystems, Inc.
396 .\" Here's a manual page for the format accepted by the "file" made by adding
397 .\" the changes I posted to the S5R2 version.
399 .\" Modified for Ian Darwin's version of the file command.
400 .\" @(#)$Id: magic.man,v 1.30 2006/02/19 18:16:03 christos Exp $