1MAGIC(5) BSD File Formats Manual MAGIC(5)
2
4 magic — file command's magic number file
5
7 This manual page documents the format of the magic file as used by the
8 file(1) command, version 4.21. The file(1) command identifies the type
9 of a file using, among other tests, a test for whether the file begins
10 with a certain “magic number”. The file /usr/share/file/magic specifies
11 what magic numbers are to be tested for, what message to print if a par‐
12 ticular magic number is found, and additional information to extract from
13 the file.
14
15 Each line of the file specifies a test to be performed. A test compares
16 the data starting at a particular offset in the file with a 1-byte,
17 2-byte, or 4-byte numeric value or a string. If the test succeeds, a
18 message is printed. The line consists of the following fields:
19
20 offset A number specifying the offset, in bytes, into the file of the
21 data which is to be tested.
22
23 type The type of the data to be tested. The possible values are:
24
25 byte A one-byte value.
26
27 short A two-byte value (on most systems) in this machine's
28 native byte order.
29
30 long A four-byte value (on most systems) in this
31 machine's native byte order.
32
33 quad An eight-byte value (on most systems) in this
34 machine's native byte order.
35
36 string A string of bytes. The string type specification
37 can be optionally followed by /[Bbc]*. The “B” flag
38 compacts whitespace in the target, which must con‐
39 tain at least one whitespace character. If the
40 magic has n consecutive blanks, the target needs at
41 least n consecutive blanks to match. The “b” flag
42 treats every blank in the target as an optional
43 blank. Finally the “c” flag, specifies case insen‐
44 sitive matching: lowercase characters in the magic
45 match both lower and upper case characters in the
46 targer, whereas upper case characters in the magic,
47 only much uppercase characters in the target.
48
49 pstring A pascal style string where the first byte is inter‐
50 preted as the an unsigned length. The string is not
51 NUL terminated.
52
53 date A four-byte value interpreted as a UNIX date.
54
55 qdate A eight-byte value interpreted as a UNIX date.
56
57 ldate A four-byte value interpreted as a UNIX-style date,
58 but interpreted as local time rather than UTC.
59
60 qldate An eight-byte value interpreted as a UNIX-style
61 date, but interpreted as local time rather than UTC.
62
63 beshort A two-byte value (on most systems) in big-endian
64 byte order.
65
66 belong A four-byte value (on most systems) in big-endian
67 byte order.
68
69 bequad An eight-byte value (on most systems) in big-endian
70 byte order.
71
72 bedate A four-byte value (on most systems) in big-endian
73 byte order, interpreted as a Unix date.
74
75 beqdate An eight-byte value (on most systems) in big-endian
76 byte order, interpreted as a Unix date.
77
78 beldate A four-byte value (on most systems) in big-endian
79 byte order, interpreted as a UNIX-style date, but
80 interpreted as local time rather than UTC.
81
82 beqldate An eight-byte value (on most systems) in big-endian
83 byte order, interpreted as a UNIX-style date, but
84 interpreted as local time rather than UTC.
85
86 bestring16 A two-byte unicode (UCS16) string in big-endian byte
87 order.
88
89 leshort A two-byte value (on most systems) in little-endian
90 byte order.
91
92 lelong A four-byte value (on most systems) in little-endian
93 byte order.
94
95 lequad An eight-byte value (on most systems) in little-
96 endian byte order.
97
98 ledate A four-byte value (on most systems) in little-endian
99 byte order, interpreted as a UNIX date.
100
101 leqdate An eight-byte value (on most systems) in little-
102 endian byte order, interpreted as a UNIX date.
103
104 leldate A four-byte value (on most systems) in little-endian
105 byte order, interpreted as a UNIX-style date, but
106 interpreted as local time rather than UTC.
107
108 leqldate An eight-byte value (on most systems) in little-
109 endian byte order, interpreted as a UNIX-style date,
110 but interpreted as local time rather than UTC.
111
112 lestring16 A two-byte unicode (UCS16) string in little-endian
113 byte order.
114
115 melong A four-byte value (on most systems) in middle-endian
116 (PDP-11) byte order.
117
118 medate A four-byte value (on most systems) in middle-endian
119 (PDP-11) byte order, interpreted as a UNIX date.
120
121 meldate A four-byte value (on most systems) in middle-endian
122 (PDP-11) byte order, interpreted as a UNIX-style
123 date, but interpreted as local time rather than UTC.
124
125 regex A regular expression match in extended POSIX regular
126 expression syntax (much like egrep). The type spec‐
127 ification can be optionally followed by /[cse]*.
128 The “c” flag makes the match case insensitive, while
129 the “s” or “e” flags update the offset to the start‐
130 ing or ending offsets of the match (only one should
131 be used). By default, regex does not update the
132 offset. The regular expression is always tested
133 against the first N lines, where N is the given off‐
134 set, thus it is only useful for (single-byte
135 encoded) text. ^ and $ will match the beginning and
136 end of individual lines, respectively, not beginning
137 and end of file.
138
139 search A literal string search starting at the given off‐
140 set. It must be followed by <number> which speci‐
141 fies how many matches shall be attempted (the
142 range). This is suitable for searching larger
143 binary expressions with variable offsets, using \
144 escapes for special characters.
145
146 default This is intended to be used with the text x (which
147 is always true) and a message that is to be used if
148 there are no other matches.
149
150 The numeric types may optionally be followed by & and a numeric value, to
151 specify that the value is to be AND'ed with the numeric value before any
152 comparisons are done. Prepending a u to the type indicates that ordered
153 comparisons should be unsigned.
154
155 test The value to be compared with the value from the file. If the
156 type is numeric, this value is specified in C form; if it is a
157 string, it is specified as a C string with the usual escapes
158 permitted (e.g. \n for new-line).
159
160 Numeric values may be preceded by a character indicating the
161 operation to be performed. It may be =, to specify that the
162 value from the file must equal the specified value, <, to spec‐
163 ify that the value from the file must be less than the specified
164 value, >, to specify that the value from the file must be
165 greater than the specified value, &, to specify that the value
166 from the file must have set all of the bits that are set in the
167 specified value, ^, to specify that the value from the file must
168 have clear any of the bits that are set in the specified value,
169 or ~, the value specified after is negated before tested. x, to
170 specify that any value will match. If the character is omitted,
171 it is assumed to be =. For all tests except string and regex,
172 operation ! specifies that the line matches if the test does
173 not succeed.
174
175 Numeric values are specified in C form; e.g. 13 is decimal, 013
176 is octal, and 0x13 is hexadecimal.
177
178 For string values, the byte string from the file must match the
179 specified byte string. The operators =, < and > (but not &) can
180 be applied to strings. The length used for matching is that of
181 the string argument in the magic file. This means that a line
182 can match any string, and then presumably print that string, by
183 doing >\0 (because all strings are greater than the null
184 string).
185
186 The special test x always evaluates to true. message The mes‐
187 sage to be printed if the comparison succeeds. If the string
188 contains a printf(3) format specification, the value from the
189 file (with any specified masking performed) is printed using the
190 message as the format string. If the string begins with ``\b'',
191 the message printed is the remainder of the string with no
192 whitespace added before it: multiple matches are normally sepa‐
193 rated by a single space.
194
195 Some file formats contain additional information which is to be printed
196 along with the file type or need additional tests to determine the true
197 file type. These additional tests are introduced by one or more > char‐
198 acters preceding the offset. The number of > on the line indicates the
199 level of the test; a line with no > at the beginning is considered to be
200 at level 0. Tests are arranged in a tree-like hierarchy: If a the test
201 on a line at level n succeeds, all following tests at level n+1 are per‐
202 formed, and the messages printed if the tests succeed, untile a line with
203 level n (or less) appears. For more complex files, one can use empty
204 messages to get just the "if/then" effect, in the following way:
205
206 0 string MZ
207 >0x18 leshort <0x40 MS-DOS executable
208 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
209
210 Offsets do not need to be constant, but can also be read from the file
211 being examined. If the first character following the last > is a ( then
212 the string after the parenthesis is interpreted as an indirect offset.
213 That means that the number after the parenthesis is used as an offset in
214 the file. The value at that offset is read, and is used again as an off‐
215 set in the file. Indirect offsets are of the form: (( x [.[bslBSL]][+-][
216 y ]). The value of x is used as an offset in the file. A byte, short or
217 long is read at that offset depending on the [bslBSLm] type specifier.
218 The capitalized types interpret the number as a big endian value, whereas
219 the small letter versions interpret the number as a little endian value;
220 the m type interprets the number as a middle endian (PDP-11) value. To
221 that number the value of y is added and the result is used as an offset
222 in the file. The default type if one is not specified is long.
223
224 That way variable length structures can be examined:
225
226 # MS Windows executables are also valid MS-DOS executables
227 0 string MZ
228 >0x18 leshort <0x40 MZ executable (MS-DOS)
229 # skip the whole block below if it is not an extended executable
230 >0x18 leshort >0x3f
231 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
232 >>(0x3c.l) string LX\0\0 LX executable (OS/2)
233
234 This strategy of examining has one drawback: You must make sure that you
235 eventually print something, or users may get empty output (like, when
236 there is neither PE\0\0 nor LE\0\0 in the above example)
237
238 If this indirect offset cannot be used as-is, there are simple calcula‐
239 tions possible: appending [+-*/%&|^]<number> inside parentheses allows
240 one to modify the value read from the file before it is used as an off‐
241 set:
242
243 # MS Windows executables are also valid MS-DOS executables
244 0 string MZ
245 # sometimes, the value at 0x18 is less that 0x40 but there's still an
246 # extended executable, simply appended to the file
247 >0x18 leshort <0x40
248 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
249 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
250
251 Sometimes you do not know the exact offset as this depends on the length
252 or position (when indirection was used before) of preceding fields. You
253 can specify an offset relative to the end of the last up-level field
254 using ‘&’ as a prefix to the offset:
255
256 0 string MZ
257 >0x18 leshort >0x3f
258 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
259 # immediately following the PE signature is the CPU type
260 >>>&0 leshort 0x14c for Intel 80386
261 >>>&0 leshort 0x184 for DEC Alpha
262
263 Indirect and relative offsets can be combined:
264
265 0 string MZ
266 >0x18 leshort <0x40
267 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
268 # if it's not COFF, go back 512 bytes and add the offset taken
269 # from byte 2/3, which is yet another way of finding the start
270 # of the extended executable
271 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
272
273 Or the other way around:
274
275 0 string MZ
276 >0x18 leshort >0x3f
277 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
278 # at offset 0x80 (-4, since relative offsets start at the end
279 # of the up-level match) inside the LE header, we find the absolute
280 # offset to the code area, where we look for a specific signature
281 >>>(&0x7c.l+0x26) string UPX \b, UPX compressed
282
283 Or even both!
284
285 0 string MZ
286 >0x18 leshort >0x3f
287 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
288 # at offset 0x58 inside the LE header, we find the relative offset
289 # to a data area where we look for a specific signature
290 >>>&(&0x54.l-3) string UNACE \b, ACE self-extracting archive
291
292 Finally, if you have to deal with offset/length pairs in your file, even
293 the second value in a parenthesized expression can be taken from the file
294 itself, using another set of parentheses. Note that this additional
295 indirect offset is always relative to the start of the main indirect off‐
296 set.
297
298 0 string MZ
299 >0x18 leshort >0x3f
300 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
301 # search for the PE section called ".idata"...
302 >>>&0xf4 search/0x140 .idata
303 # ...and go to the end of it, calculated from start+length;
304 # these are located 14 and 10 bytes after the section name
305 >>>>(&0xe.l+(-4)) string PK\3\4 \b, ZIP self-extracting archive
306
308 file(1) - the command that reads this file.
309
311 The formats long, belong, lelong, melong, short, beshort, leshort, date,
312 bedate, medate, ledate, beldate, leldate, and meldate are system-depen‐
313 dent; perhaps they should be specified as a number of bytes (2B, 4B,
314 etc), since the files being recognized typically come from a system on
315 which the lengths are invariant.
316
317BSD January 10, 2007 BSD