xref: /openbsd/usr.bin/file/magic.5 (revision cca36db2)
1.\" $OpenBSD: magic.5,v 1.14 2010/07/15 21:54:20 jmc Exp $
2.\"
3.\" @(#)$FreeBSD: src/usr.bin/file/magic.5,v 1.11 2000/03/01 12:19:39 sheldonh Exp $
4.\"
5.\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
6.\"
7.\" Copyright (c) Ian F. Darwin 1986-1995.
8.\" Software written by Ian F. Darwin and others;
9.\" maintained 1995-present by Christos Zoulas and others.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice immediately at the beginning of the file, without modification,
16.\"    this list of conditions, and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.Dd $Mdocdate: July 15 2010 $
34.Dt MAGIC 5
35.Os
36.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
37.Sh NAME
38.Nm magic
39.Nd file command's magic pattern file
40.Sh DESCRIPTION
41This manual page documents the format of the magic file as
42used by the
43.Xr file 1
44command, version 4.24.
45The
46.Xr file 1
47command identifies the type of a file using,
48among other tests,
49a test for whether the file contains certain
50.Dq "magic patterns" .
51The file
52.Pa /etc/magic
53specifies what magic numbers are to be tested for,
54what message to print if a particular magic number is found,
55and additional information to extract from the file.
56.Pp
57Each line of the file specifies a test to be performed.
58A test compares the data starting at a particular offset
59in the file with a byte value, a string or a numeric value.
60If the test succeeds, a message is printed.
61The line consists of the following fields:
62.Bl -tag -width ".Dv message"
63.It Dv offset
64A number specifying the offset, in bytes, into the file of the data
65which is to be tested.
66.It Dv type
67The type of the data to be tested.
68The possible values are:
69.Bl -tag -width ".Dv lestring16"
70.It Dv byte
71A one-byte value.
72.It Dv short
73A two-byte value in this machine's native byte order.
74.It Dv long
75A four-byte value in this machine's native byte order.
76.It Dv quad
77An eight-byte value in this machine's native byte order.
78.It Dv float
79A 32-bit single precision IEEE floating point number in this machine's native byte order.
80.It Dv double
81A 64-bit double precision IEEE floating point number in this machine's native byte order.
82.It Dv string
83A string of bytes.
84The string type specification can be optionally followed
85by /[Bbc]*.
86The
87.Dq B
88flag compacts whitespace in the target, which must
89contain at least one whitespace character.
90If the magic has
91.Dv n
92consecutive blanks, the target needs at least
93.Dv n
94consecutive blanks to match.
95The
96.Dq b
97flag treats every blank in the target as an optional blank.
98Finally the
99.Dq c
100flag, specifies case insensitive matching: lowercase
101characters in the magic match both lower and upper case characters in the
102target, whereas upper case characters in the magic only match uppercase
103characters in the target.
104.It Dv pstring
105A Pascal-style string where the first byte is interpreted as the an
106unsigned length.
107The string is not NUL terminated.
108.It Dv date
109A four-byte value interpreted as a UNIX date.
110.It Dv qdate
111An eight-byte value interpreted as a UNIX date.
112.It Dv ldate
113A four-byte value interpreted as a UNIX-style date, but interpreted as
114local time rather than UTC.
115.It Dv qldate
116An eight-byte value interpreted as a UNIX-style date, but interpreted as
117local time rather than UTC.
118.It Dv beshort
119A two-byte value in big-endian byte order.
120.It Dv belong
121A four-byte value in big-endian byte order.
122.It Dv bequad
123An eight-byte value in big-endian byte order.
124.It Dv befloat
125A 32-bit single precision IEEE floating point number in big-endian byte order.
126.It Dv bedouble
127A 64-bit double precision IEEE floating point number in big-endian byte order.
128.It Dv bedate
129A four-byte value in big-endian byte order,
130interpreted as a Unix date.
131.It Dv beqdate
132An eight-byte value in big-endian byte order,
133interpreted as a Unix date.
134.It Dv beldate
135A four-byte value in big-endian byte order,
136interpreted as a UNIX-style date, but interpreted as local time rather
137than UTC.
138.It Dv beqldate
139An eight-byte value in big-endian byte order,
140interpreted as a UNIX-style date, but interpreted as local time rather
141than UTC.
142.It Dv bestring16
143A two-byte unicode (UCS16) string in big-endian byte order.
144.It Dv leshort
145A two-byte value in little-endian byte order.
146.It Dv lelong
147A four-byte value in little-endian byte order.
148.It Dv lequad
149An eight-byte value in little-endian byte order.
150.It Dv lefloat
151A 32-bit single precision IEEE floating point number in little-endian byte order.
152.It Dv ledouble
153A 64-bit double precision IEEE floating point number in little-endian byte order.
154.It Dv ledate
155A four-byte value in little-endian byte order,
156interpreted as a UNIX date.
157.It Dv leqdate
158An eight-byte value in little-endian byte order,
159interpreted as a UNIX date.
160.It Dv leldate
161A four-byte value in little-endian byte order,
162interpreted as a UNIX-style date, but interpreted as local time rather
163than UTC.
164.It Dv leqldate
165An eight-byte value in little-endian byte order,
166interpreted as a UNIX-style date, but interpreted as local time rather
167than UTC.
168.It Dv lestring16
169A two-byte unicode (UCS16) string in little-endian byte order.
170.It Dv melong
171A four-byte value in middle-endian (PDP-11) byte order.
172.It Dv medate
173A four-byte value in middle-endian (PDP-11) byte order,
174interpreted as a UNIX date.
175.It Dv meldate
176A four-byte value in middle-endian (PDP-11) byte order,
177interpreted as a UNIX-style date, but interpreted as local time rather
178than UTC.
179.It Dv regex
180A regular expression match in extended POSIX regular expression syntax
181(like egrep).
182Regular expressions can take exponential time to process,
183and their performance is hard to predict, so their use is discouraged.
184When used in production environments,
185their performance should be carefully checked.
186The type specification can be optionally followed by
187.Dv /[c][s] .
188The
189.Dq c
190flag makes the match case insensitive, while the
191.Dq s
192flag update the offset to the start offset of the match, rather than the end.
193The regular expression is tested against line
194.Dv N + 1
195onwards, where
196.Dv N
197is the given offset.
198Line endings are assumed to be in the machine's native format.
199.Dv ^
200and
201.Dv $
202match the beginning and end of individual lines, respectively,
203not beginning and end of file.
204.It Dv search
205A literal string search starting at the given offset.
206The same modifier flags can be used as for string patterns.
207The modifier flags (if any) must be followed by
208.Dv /number
209the range, that is, the number of positions at which the match will be
210attempted, starting from the start offset.
211This is suitable for searching larger binary expressions
212with variable offsets, using
213.Dv \e
214escapes for special characters.
215The offset works as for regex.
216.It Dv default
217This is intended to be used with the test
218.Em x
219(which is always true) and a message that is to be used if there are
220no other matches.
221.El
222.Pp
223Each top-level magic pattern (see below for an explanation of levels)
224is classified as text or binary according to the types used.
225Types
226.Dq regex
227and
228.Dq search
229are classified as text tests, unless non-printable characters are used
230in the pattern.
231All other tests are classified as binary.
232A top-level pattern is considered to be a test text
233when all its patterns are text
234patterns; otherwise, it is considered to be a binary pattern.
235When matching a file, binary patterns are tried first; if no match is
236found, and the file looks like text, then its encoding is determined
237and the text patterns are tried.
238.Pp
239The numeric types may optionally be followed by
240.Dv &
241and a numeric value,
242to specify that the value is to be AND'ed with the
243numeric value before any comparisons are done.
244Prepending a
245.Dv u
246to the type indicates that ordered comparisons should be unsigned.
247.It Dv test
248The value to be compared with the value from the file.
249If the type is
250numeric, this value
251is specified in C form; if it is a string, it is specified as a C string
252with the usual escapes permitted (e.g. \en for new-line).
253.Pp
254Numeric values
255may be preceded by a character indicating the operation to be performed.
256It may be
257.Dv = ,
258to specify that the value from the file must equal the specified value,
259.Dv \*(Lt ,
260to specify that the value from the file must be less than the specified
261value,
262.Dv \*(Gt ,
263to specify that the value from the file must be greater than the specified
264value,
265.Dv & ,
266to specify that the value from the file must have set all of the bits
267that are set in the specified value,
268.Dv ^ ,
269to specify that the value from the file must have clear any of the bits
270that are set in the specified value, or
271.Dv ~ ,
272the value specified after is negated before tested.
273.Dv x ,
274to specify that any value will match.
275If the character is omitted, it is assumed to be
276.Dv = .
277Operators
278.Dv & ,
279.Dv ^ ,
280and
281.Dv ~
282don't work with floats and doubles.
283The operator
284.Dv !\&
285specifies that the line matches if the test does
286.Em not
287succeed.
288.Pp
289Numeric values are specified in C form; e.g.
290.Dv 13
291is decimal,
292.Dv 013
293is octal, and
294.Dv 0x13
295is hexadecimal.
296.Pp
297For string values, the string from the
298file must match the specified string.
299The operators
300.Dv = ,
301.Dv \*(Lt
302and
303.Dv \*(Gt
304(but not
305.Dv & )
306can be applied to strings.
307The length used for matching is that of the string argument
308in the magic file.
309This means that a line can match any non-empty string (usually used to
310then print the string), with
311.Em \*(Gt\e0
312(because all non-empty strings are greater than the empty string).
313.Pp
314The special test
315.Em x
316always evaluates to true.
317.Dv message
318The message to be printed if the comparison succeeds.
319If the string contains a
320.Xr printf 3
321format specification, the value from the file (with any specified masking
322performed) is printed using the message as the format string.
323If the string begins with
324.Dq \eb ,
325the message printed is the remainder of the string with no whitespace
326added before it: multiple matches are normally separated by a single
327space.
328.El
329.Pp
330A MIME type is given on a separate line, which must be the next
331non-blank or comment line after the magic line that identifies the
332file type, and has the following format:
333.Bd -literal -offset indent
334!:mime	MIMETYPE
335.Ed
336.Pp
337i.e. the literal string
338.Dq !:mime
339followed by the MIME type.
340.Pp
341Some file formats contain additional information which is to be printed
342along with the file type or need additional tests to determine the true
343file type.
344These additional tests are introduced by one or more
345.Em \*(Gt
346characters preceding the offset.
347The number of
348.Em \*(Gt
349on the line indicates the level of the test; a line with no
350.Em \*(Gt
351at the beginning is considered to be at level 0.
352Tests are arranged in a tree-like hierarchy:
353If a the test on a line at level
354.Em n
355succeeds, all following tests at level
356.Em n+1
357are performed, and the messages printed if the tests succeed, untile a line
358with level
359.Em n
360(or less) appears.
361For more complex files, one can use empty messages to get just the
362"if/then" effect, in the following way:
363.Bd -literal -offset indent
3640      string   MZ
365\*(Gt0x18  leshort  \*(Lt0x40   MS-DOS executable
366\*(Gt0x18  leshort  \*(Gt0x3f   extended PC executable (e.g., MS Windows)
367.Ed
368.Pp
369Offsets do not need to be constant, but can also be read from the file
370being examined.
371If the first character following the last
372.Em \*(Gt
373is a
374.Em \&(
375then the string after the parenthesis is interpreted as an indirect offset.
376That means that the number after the parenthesis is used as an offset in
377the file.
378The value at that offset is read, and is used again as an offset
379in the file.
380Indirect offsets are of the form:
381.Em (( x [.[bslBSL]][+\-][ y ]) .
382The value of
383.Em x
384is used as an offset in the file.
385A byte, short or long is read at that offset depending on the
386.Op bslBSLm
387type specifier.
388The capitalized types interpret the number as a big endian
389value, whereas the small letter versions interpret the number as a little
390endian value;
391the
392.Em m
393type interprets the number as a middle endian (PDP-11) value.
394To that number the value of
395.Em y
396is added and the result is used as an offset in the file.
397The default type if one is not specified is long.
398.Pp
399That way variable length structures can be examined:
400.Bd -literal -offset indent
401# MS Windows executables are also valid MS-DOS executables
4020           string  MZ
403\*(Gt0x18       leshort \*(Lt0x40   MZ executable (MS-DOS)
404# skip the whole block below if it is not an extended executable
405\*(Gt0x18       leshort \*(Gt0x3f
406\*(Gt\*(Gt(0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
407\*(Gt\*(Gt(0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
408.Ed
409.Pp
410This strategy of examining has a drawback: You must make sure that
411you eventually print something, or users may get empty output (like, when
412there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
413.Pp
414If this indirect offset cannot be used directly, simple calculations are
415possible: appending
416.Em [+-*/%&|^]number
417inside parentheses allows one to modify
418the value read from the file before it is used as an offset:
419.Bd -literal -offset indent
420# MS Windows executables are also valid MS-DOS executables
4210           string  MZ
422# sometimes, the value at 0x18 is less that 0x40 but there's still an
423# extended executable, simply appended to the file
424\*(Gt0x18       leshort \*(Lt0x40
425\*(Gt\*(Gt(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
426\*(Gt\*(Gt(4.s*512) leshort !0x014c MZ executable (MS-DOS)
427.Ed
428.Pp
429Sometimes you do not know the exact offset as this depends on the length or
430position (when indirection was used before) of preceding fields.
431You can specify an offset relative to the end of the last up-level
432field using
433.Sq &
434as a prefix to the offset:
435.Bd -literal -offset indent
4360           string  MZ
437\*(Gt0x18       leshort \*(Gt0x3f
438\*(Gt\*(Gt(0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
439# immediately following the PE signature is the CPU type
440\*(Gt\*(Gt\*(Gt&0       leshort 0x14c     for Intel 80386
441\*(Gt\*(Gt\*(Gt&0       leshort 0x184     for DEC Alpha
442.Ed
443.Pp
444Indirect and relative offsets can be combined:
445.Bd -literal -offset indent
4460             string  MZ
447\*(Gt0x18         leshort \*(Lt0x40
448\*(Gt\*(Gt(4.s*512)   leshort !0x014c MZ executable (MS-DOS)
449# if it's not COFF, go back 512 bytes and add the offset taken
450# from byte 2/3, which is yet another way of finding the start
451# of the extended executable
452\*(Gt\*(Gt\*(Gt&(2.s-514) string  LE      LE executable (MS Windows VxD driver)
453.Ed
454.Pp
455Or the other way around:
456.Bd -literal -offset indent
4570                 string  MZ
458\*(Gt0x18             leshort \*(Gt0x3f
459\*(Gt\*(Gt(0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
460# at offset 0x80 (-4, since relative offsets start at the end
461# of the up-level match) inside the LE header, we find the absolute
462# offset to the code area, where we look for a specific signature
463\*(Gt\*(Gt\*(Gt(&0x7c.l+0x26) string  UPX     \eb, UPX compressed
464.Ed
465.Pp
466Or even both!
467.Bd -literal -offset indent
4680                string  MZ
469\*(Gt0x18            leshort \*(Gt0x3f
470\*(Gt\*(Gt(0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
471# at offset 0x58 inside the LE header, we find the relative offset
472# to a data area where we look for a specific signature
473\*(Gt\*(Gt\*(Gt&(&0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
474.Ed
475.Pp
476Finally, if you have to deal with offset/length pairs in your file, even the
477second value in a parenthesized expression can be taken from the file itself,
478using another set of parentheses.
479Note that this additional indirect offset is always relative to the
480start of the main indirect offset.
481.Bd -literal -offset indent
4820                 string       MZ
483\*(Gt0x18             leshort      \*(Gt0x3f
484\*(Gt\*(Gt(0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
485# search for the PE section called ".idata"...
486\*(Gt\*(Gt\*(Gt&0xf4          search/0x140 .idata
487# ...and go to the end of it, calculated from start+length;
488# these are located 14 and 10 bytes after the section name
489\*(Gt\*(Gt\*(Gt\*(Gt(&0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
490.Ed
491.Sh SEE ALSO
492.Xr file 1
493\- the command that reads this file.
494.Sh BUGS
495The formats
496.Dv long ,
497.Dv belong ,
498.Dv lelong ,
499.Dv melong ,
500.Dv short ,
501.Dv beshort ,
502.Dv leshort ,
503.Dv date ,
504.Dv bedate ,
505.Dv medate ,
506.Dv ledate ,
507.Dv beldate ,
508.Dv leldate ,
509and
510.Dv meldate
511are system-dependent; perhaps they should be specified as a number
512of bytes (2B, 4B, etc),
513since the files being recognized typically come from
514a system on which the lengths are invariant.
515.\"
516.\" From: guy@sun.uucp (Guy Harris)
517.\" Newsgroups: net.bugs.usg
518.\" Subject: /etc/magic's format isn't well documented
519.\" Message-ID: <2752@sun.uucp>
520.\" Date: 3 Sep 85 08:19:07 GMT
521.\" Organization: Sun Microsystems, Inc.
522.\" Lines: 136
523.\"
524.\" Here's a manual page for the format accepted by the "file" made by adding
525.\" the changes I posted to the S5R2 version.
526.\"
527.\" Modified for Ian Darwin's version of the file command.
528