1MAGIC(5) BSD File Formats Manual MAGIC(5)
2
4 magic — file command's magic pattern file
5
7 This manual page documents the format of magic files as used by the
8 file(1) command, version 5.34. The file(1) command identifies the type
9 of a file using, among other tests, a test for whether the file contains
10 certain “magic patterns”. The database of these “magic patterns” is usu‐
11 ally located in a binary file in /usr/share/misc/magic.mgc or a directory
12 of source text magic pattern fragment files in /usr/share/misc/magic.
13 The database specifies what patterns are to be tested for, what message
14 or MIME type to print if a particular pattern is found, and additional
15 information to extract from the file.
16
17 The format of the source fragment files that are used to build this data‐
18 base is as follows: Each line of a fragment file specifies a test to be
19 performed. A test compares the data starting at a particular offset in
20 the file with a byte value, a string or a numeric value. If the test
21 succeeds, a message is printed. The line consists of the following
22 fields:
23
24 offset A number specifying the offset (in bytes) into the file of the
25 data which is to be tested. This offset can be a negative num‐
26 ber if it is:
27 · The first direct offset of the magic entry (at continuation
28 level 0), in which case it is interpreted an offset from end
29 end of the file going backwards. This works only when a
30 file descriptor to the file is a available and it is a regu‐
31 lar file.
32 · A continuation offset relative to the end of the last up-
33 level field (&).
34
35 type The type of the data to be tested. The possible values are:
36
37 byte A one-byte value.
38
39 short A two-byte value in this machine's native byte
40 order.
41
42 long A four-byte value in this machine's native byte
43 order.
44
45 quad An eight-byte value in this machine's native byte
46 order.
47
48 float A 32-bit single precision IEEE floating point number
49 in this machine's native byte order.
50
51 double A 64-bit double precision IEEE floating point number
52 in this machine's native byte order.
53
54 string A string of bytes. The string type specification
55 can be optionally followed by /[WwcCtbT]*. The “W”
56 flag compacts whitespace in the target, which must
57 contain at least one whitespace character. If the
58 magic has n consecutive blanks, the target needs at
59 least n consecutive blanks to match. The “w” flag
60 treats every blank in the magic as an optional
61 blank. The “c” flag specifies case insensitive
62 matching: lower case characters in the magic match
63 both lower and upper case characters in the target,
64 whereas upper case characters in the magic only
65 match upper case characters in the target. The “C”
66 flag specifies case insensitive matching: upper case
67 characters in the magic match both lower and upper
68 case characters in the target, whereas lower case
69 characters in the magic only match upper case char‐
70 acters in the target. To do a complete case insen‐
71 sitive match, specify both “c” and “C”. The “t”
72 flag forces the test to be done for text files,
73 while the “b” flag forces the test to be done for
74 binary files. The “T” flag causes the string to be
75 trimmed, i.e. leading and trailing whitespace is
76 deleted before the string is printed.
77
78 pstring A Pascal-style string where the first byte/short/int
79 is interpreted as the unsigned length. The length
80 defaults to byte and can be specified as a modifier.
81 The following modifiers are supported:
82 B A byte length (default).
83 H A 4 byte big endian length.
84 h A 2 byte big endian length.
85 L A 4 byte little endian length.
86 l A 2 byte little endian length.
87 J The length includes itself in its count.
88 The string is not NUL terminated. “J” is used
89 rather than the more valuable “I” because this type
90 of length is a feature of the JPEG format.
91
92 date A four-byte value interpreted as a UNIX date.
93
94 qdate A eight-byte value interpreted as a UNIX date.
95
96 ldate A four-byte value interpreted as a UNIX-style date,
97 but interpreted as local time rather than UTC.
98
99 qldate An eight-byte value interpreted as a UNIX-style
100 date, but interpreted as local time rather than UTC.
101
102 qwdate An eight-byte value interpreted as a Windows-style
103 date.
104
105 beid3 A 32-bit ID3 length in big-endian byte order.
106
107 beshort A two-byte value in big-endian byte order.
108
109 belong A four-byte value in big-endian byte order.
110
111 bequad An eight-byte value in big-endian byte order.
112
113 befloat A 32-bit single precision IEEE floating point number
114 in big-endian byte order.
115
116 bedouble A 64-bit double precision IEEE floating point number
117 in big-endian byte order.
118
119 bedate A four-byte value in big-endian byte order, inter‐
120 preted as a Unix date.
121
122 beqdate An eight-byte value in big-endian byte order, inter‐
123 preted as a Unix date.
124
125 beldate A four-byte value in big-endian byte order, inter‐
126 preted as a UNIX-style date, but interpreted as
127 local time rather than UTC.
128
129 beqldate An eight-byte value in big-endian byte order, inter‐
130 preted as a UNIX-style date, but interpreted as
131 local time rather than UTC.
132
133 beqwdate An eight-byte value in big-endian byte order, inter‐
134 preted as a Windows-style date.
135
136 bestring16 A two-byte unicode (UCS16) string in big-endian byte
137 order.
138
139 leid3 A 32-bit ID3 length in little-endian byte order.
140
141 leshort A two-byte value in little-endian byte order.
142
143 lelong A four-byte value in little-endian byte order.
144
145 lequad An eight-byte value in little-endian byte order.
146
147 lefloat A 32-bit single precision IEEE floating point number
148 in little-endian byte order.
149
150 ledouble A 64-bit double precision IEEE floating point number
151 in little-endian byte order.
152
153 ledate A four-byte value in little-endian byte order,
154 interpreted as a UNIX date.
155
156 leqdate An eight-byte value in little-endian byte order,
157 interpreted as a UNIX date.
158
159 leldate A four-byte value in little-endian byte order,
160 interpreted as a UNIX-style date, but interpreted as
161 local time rather than UTC.
162
163 leqldate An eight-byte value in little-endian byte order,
164 interpreted as a UNIX-style date, but interpreted as
165 local time rather than UTC.
166
167 leqwdate An eight-byte value in little-endian byte order,
168 interpreted as a Windows-style date.
169
170 lestring16 A two-byte unicode (UCS16) string in little-endian
171 byte order.
172
173 melong A four-byte value in middle-endian (PDP-11) byte
174 order.
175
176 medate A four-byte value in middle-endian (PDP-11) byte
177 order, interpreted as a UNIX date.
178
179 meldate A four-byte value in middle-endian (PDP-11) byte
180 order, interpreted as a UNIX-style date, but inter‐
181 preted as local time rather than UTC.
182
183 indirect Starting at the given offset, consult the magic
184 database again. The offset of the indirect magic is
185 by default absolute in the file, but one can specify
186 /r to indicate that the offset is relative from the
187 beginning of the entry.
188
189 name Define a “named” magic instance that can be called
190 from another use magic entry, like a subroutine
191 call. Named instance direct magic offsets are rela‐
192 tive to the offset of the previous matched entry,
193 but indirect offsets are relative to the beginning
194 of the file as usual. Named magic entries always
195 match.
196
197 use Recursively call the named magic starting from the
198 current offset. If the name of the referenced
199 begins with a ^ then the endianness of the magic is
200 switched; if the magic mentioned leshort for exam‐
201 ple, it is treated as beshort and vice versa. This
202 is useful to avoid duplicating the rules for differ‐
203 ent endianness.
204
205 regex A regular expression match in extended POSIX regular
206 expression syntax (like egrep). Regular expressions
207 can take exponential time to process, and their per‐
208 formance is hard to predict, so their use is dis‐
209 couraged. When used in production environments,
210 their performance should be carefully checked. The
211 size of the string to search should also be limited
212 by specifying /<length>, to avoid performance issues
213 scanning long files. The type specification can
214 also be optionally followed by /[c][s][l]. The “c”
215 flag makes the match case insensitive, while the “s”
216 flag update the offset to the start offset of the
217 match, rather than the end. The “l” modifier,
218 changes the limit of length to mean number of lines
219 instead of a byte count. Lines are delimited by the
220 platforms native line delimiter. When a line count
221 is specified, an implicit byte count also computed
222 assuming each line is 80 characters long. If nei‐
223 ther a byte or line count is specified, the search
224 is limited automatically to 8KiB. ^ and $ match the
225 beginning and end of individual lines, respectively,
226 not beginning and end of file.
227
228 search A literal string search starting at the given off‐
229 set. The same modifier flags can be used as for
230 string patterns. The search expression must contain
231 the range in the form /number, that is the number of
232 positions at which the match will be attempted,
233 starting from the start offset. This is suitable
234 for searching larger binary expressions with vari‐
235 able offsets, using \ escapes for special charac‐
236 ters. The order of modifier and number is not rele‐
237 vant.
238
239 default This is intended to be used with the test x (which
240 is always true) and it has no type. It matches when
241 no other test at that continuation level has matched
242 before. Clearing that matched tests for a continua‐
243 tion level, can be done using the clear test.
244
245 clear This test is always true and clears the match flag
246 for that continuation level. It is intended to be
247 used with the default test.
248
249 For compatibility with the Single UNIX Standard, the type speci‐
250 fiers dC and d1 are equivalent to byte, the type specifiers uC
251 and u1 are equivalent to ubyte, the type specifiers dS and d2
252 are equivalent to short, the type specifiers uS and u2 are
253 equivalent to ushort, the type specifiers dI, dL, and d4 are
254 equivalent to long, the type specifiers uI, uL, and u4 are
255 equivalent to ulong, the type specifier d8 is equivalent to
256 quad, the type specifier u8 is equivalent to uquad, and the type
257 specifier s is equivalent to string. In addition, the type
258 specifier dQ is equivalent to quad and the type specifier uQ is
259 equivalent to uquad.
260
261 Each top-level magic pattern (see below for an explanation of
262 levels) is classified as text or binary according to the types
263 used. Types “regex” and “search” are classified as text tests,
264 unless non-printable characters are used in the pattern. All
265 other tests are classified as binary. A top-level pattern is
266 considered to be a test text when all its patterns are text pat‐
267 terns; otherwise, it is considered to be a binary pattern. When
268 matching a file, binary patterns are tried first; if no match is
269 found, and the file looks like text, then its encoding is deter‐
270 mined and the text patterns are tried.
271
272 The numeric types may optionally be followed by & and a numeric
273 value, to specify that the value is to be AND'ed with the
274 numeric value before any comparisons are done. Prepending a u
275 to the type indicates that ordered comparisons should be
276 unsigned.
277
278 test The value to be compared with the value from the file. If the
279 type is numeric, this value is specified in C form; if it is a
280 string, it is specified as a C string with the usual escapes
281 permitted (e.g. \n for new-line).
282
283 Numeric values may be preceded by a character indicating the
284 operation to be performed. It may be =, to specify that the
285 value from the file must equal the specified value, <, to spec‐
286 ify that the value from the file must be less than the specified
287 value, >, to specify that the value from the file must be
288 greater than the specified value, &, to specify that the value
289 from the file must have set all of the bits that are set in the
290 specified value, ^, to specify that the value from the file must
291 have clear any of the bits that are set in the specified value,
292 or ~, the value specified after is negated before tested. x, to
293 specify that any value will match. If the character is omitted,
294 it is assumed to be =. Operators &, ^, and ~ don't work with
295 floats and doubles. The operator ! specifies that the line
296 matches if the test does not succeed.
297
298 Numeric values are specified in C form; e.g. 13 is decimal, 013
299 is octal, and 0x13 is hexadecimal.
300
301 Numeric operations are not performed on date types, instead the
302 numeric value is interpreted as an offset.
303
304 For string values, the string from the file must match the spec‐
305 ified string. The operators =, < and > (but not &) can be
306 applied to strings. The length used for matching is that of the
307 string argument in the magic file. This means that a line can
308 match any non-empty string (usually used to then print the
309 string), with >\0 (because all non-empty strings are greater
310 than the empty string).
311
312 Dates are treated as numerical values in the respective internal
313 representation.
314
315 The special test x always evaluates to true.
316
317 message The message to be printed if the comparison succeeds. If the
318 string contains a printf(3) format specification, the value from
319 the file (with any specified masking performed) is printed using
320 the message as the format string. If the string begins with
321 “\b”, the message printed is the remainder of the string with no
322 whitespace added before it: multiple matches are normally sepa‐
323 rated by a single space.
324
325 An APPLE 4+4 character APPLE creator and type can be specified as:
326
327 !:apple CREATYPE
328
329 A MIME type is given on a separate line, which must be the next non-blank
330 or comment line after the magic line that identifies the file type, and
331 has the following format:
332
333 !:mime MIMETYPE
334
335 i.e. the literal string “!:mime” followed by the MIME type.
336
337 An optional strength can be supplied on a separate line which refers to
338 the current magic description using the following format:
339
340 !:strength OP VALUE
341
342 The operand OP can be: +, -, *, or / and VALUE is a constant between 0
343 and 255. This constant is applied using the specified operand to the
344 currently computed default magic strength.
345
346 Some file formats contain additional information which is to be printed
347 along with the file type or need additional tests to determine the true
348 file type. These additional tests are introduced by one or more > char‐
349 acters preceding the offset. The number of > on the line indicates the
350 level of the test; a line with no > at the beginning is considered to be
351 at level 0. Tests are arranged in a tree-like hierarchy: if the test on
352 a line at level n succeeds, all following tests at level n+1 are per‐
353 formed, and the messages printed if the tests succeed, until a line with
354 level n (or less) appears. For more complex files, one can use empty
355 messages to get just the "if/then" effect, in the following way:
356
357 0 string MZ
358 >0x18 leshort <0x40 MS-DOS executable
359 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
360
361 Offsets do not need to be constant, but can also be read from the file
362 being examined. If the first character following the last > is a ( then
363 the string after the parenthesis is interpreted as an indirect offset.
364 That means that the number after the parenthesis is used as an offset in
365 the file. The value at that offset is read, and is used again as an off‐
366 set in the file. Indirect offsets are of the form: (( x
367 [[.,][bBcCeEfFgGhHiIlmsSqQ]][+-][ y ]). The value of x is used as an
368 offset in the file. A byte, id3 length, short or long is read at that
369 offset depending on the [bBcCeEfFgGhHiIlmsSqQ] type specifier. The value
370 is treated as signed if “”, is specified or unsigned if “”. is speci‐
371 fied. The capitalized types interpret the number as a big endian value,
372 whereas the small letter versions interpret the number as a little endian
373 value; the m type interprets the number as a middle endian (PDP-11)
374 value. To that number the value of y is added and the result is used as
375 an offset in the file. The default type if one is not specified is long.
376 The following types are recognized:
377
378 Type Sy Mnemonic Sy Endian Sy Size
379 bcBc Byte/Char N/A 1
380 efg Double Little 8
381 EFG Double Big 8
382 hs Half/Short Little 2
383 HS Half/Short Big 2
384 i ID3 Little 4
385 I ID3 Big 4
386 m Middle Middle 4
387 q Quad Little 8
388 Q Quad Big 8
389
390 That way variable length structures can be examined:
391
392 # MS Windows executables are also valid MS-DOS executables
393 0 string MZ
394 >0x18 leshort <0x40 MZ executable (MS-DOS)
395 # skip the whole block below if it is not an extended executable
396 >0x18 leshort >0x3f
397 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
398 >>(0x3c.l) string LX\0\0 LX executable (OS/2)
399
400 This strategy of examining has a drawback: you must make sure that you
401 eventually print something, or users may get empty output (such as when
402 there is neither PE\0\0 nor LE\0\0 in the above example).
403
404 If this indirect offset cannot be used directly, simple calculations are
405 possible: appending [+-*/%&|^]number inside parentheses allows one to
406 modify the value read from the file before it is used as an offset:
407
408 # MS Windows executables are also valid MS-DOS executables
409 0 string MZ
410 # sometimes, the value at 0x18 is less that 0x40 but there's still an
411 # extended executable, simply appended to the file
412 >0x18 leshort <0x40
413 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
414 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
415
416 Sometimes you do not know the exact offset as this depends on the length
417 or position (when indirection was used before) of preceding fields. You
418 can specify an offset relative to the end of the last up-level field
419 using ‘&’ as a prefix to the offset:
420
421 0 string MZ
422 >0x18 leshort >0x3f
423 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
424 # immediately following the PE signature is the CPU type
425 >>>&0 leshort 0x14c for Intel 80386
426 >>>&0 leshort 0x184 for DEC Alpha
427
428 Indirect and relative offsets can be combined:
429
430 0 string MZ
431 >0x18 leshort <0x40
432 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
433 # if it's not COFF, go back 512 bytes and add the offset taken
434 # from byte 2/3, which is yet another way of finding the start
435 # of the extended executable
436 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
437
438 Or the other way around:
439
440 0 string MZ
441 >0x18 leshort >0x3f
442 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
443 # at offset 0x80 (-4, since relative offsets start at the end
444 # of the up-level match) inside the LE header, we find the absolute
445 # offset to the code area, where we look for a specific signature
446 >>>(&0x7c.l+0x26) string UPX \b, UPX compressed
447
448 Or even both!
449
450 0 string MZ
451 >0x18 leshort >0x3f
452 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
453 # at offset 0x58 inside the LE header, we find the relative offset
454 # to a data area where we look for a specific signature
455 >>>&(&0x54.l-3) string UNACE \b, ACE self-extracting archive
456
457 If you have to deal with offset/length pairs in your file, even the sec‐
458 ond value in a parenthesized expression can be taken from the file
459 itself, using another set of parentheses. Note that this additional
460 indirect offset is always relative to the start of the main indirect off‐
461 set.
462
463 0 string MZ
464 >0x18 leshort >0x3f
465 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
466 # search for the PE section called ".idata"...
467 >>>&0xf4 search/0x140 .idata
468 # ...and go to the end of it, calculated from start+length;
469 # these are located 14 and 10 bytes after the section name
470 >>>>(&0xe.l+(-4)) string PK\3\4 \b, ZIP self-extracting archive
471
472 If you have a list of known values at a particular continuation level,
473 and you want to provide a switch-like default case:
474
475 # clear that continuation level match
476 >18 clear
477 >18 lelong 1 one
478 >18 lelong 2 two
479 >18 default x
480 # print default match
481 >>18 lelong x unmatched 0x%x
482
484 file(1) - the command that reads this file.
485
487 The formats long, belong, lelong, melong, short, beshort, and leshort do
488 not depend on the length of the C data types short and long on the plat‐
489 form, even though the Single UNIX Specification implies that they do.
490 However, as OS X Mountain Lion has passed the Single UNIX Specification
491 validation suite, and supplies a version of file(1) in which they do not
492 depend on the sizes of the C data types and that is built for a 64-bit
493 environment in which long is 8 bytes rather than 4 bytes, presumably the
494 validation suite does not test whether, for example long refers to an
495 item with the same size as the C data type long. There should probably
496 be type names int8, uint8, int16, uint16, int32, uint32, int64, and
497 uint64, and specified-byte-order variants of them, to make it clearer
498 that those types have specified widths.
499
500BSD June 22, 2018 BSD