]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/file/magic.5
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / file / magic.5
1 .\"
2 .\" $FreeBSD$
3 .\"
4 .\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
5 .\"
6 .Dd February 19, 2006
7 .Dt MAGIC 5 "Public Domain"
8 .Os
9 .Sh NAME
10 .Nm magic
11 .Nd file command's magic number file
12 .Sh DESCRIPTION
13 This manual page documents the format of the magic file as
14 used by the
15 .Nm
16 command, version 4.21.
17 The
18 .Nm file
19 command identifies the type of a file using,
20 among other tests,
21 a test for whether the file begins with a certain
22 .Em "magic number" .
23 The file
24 .Pa /usr/share/misc/magic
25 specifies what magic numbers are to be tested for,
26 what message to print if a particular magic number is found,
27 and additional information to extract from the file.
28 .Pp
29 Each line of the file specifies a test to be performed.
30 A test compares the data starting at a particular offset
31 in the file with a 1-byte, 2-byte, or 4-byte numeric value or
32 a string.
33 If the test succeeds, a message is printed.
34 The line consists of the following fields:
35 .Bl -tag -width indent
36 .It offset
37 A number specifying the offset, in bytes, into the file of the data
38 which is to be tested.
39 .It type
40 The type of the data to be tested.
41 The possible values are:
42 .Bl -tag -width indent
43 .It byte
44 A one-byte value.
45 .It short
46 A two-byte value (on most systems) in this machine's native byte order.
47 .It long
48 A four-byte value (on most systems) in this machine's native byte order.
49 .It string
50 A string of bytes.
51 The string type specification can be optionally followed
52 by /[Bbc]*.
53 The
54 .Dq B
55 flag compacts whitespace in the target, which must contain
56 at least one whitespace character.
57 If the magic has
58 .Ar n
59 consecutive blanks, the target needs at least
60 .Ar n
61 consecutive blanks to match.
62 The
63 .Dq b
64 flag treats every blank in the target as an optional blank.
65 Finally the
66 .Dq c
67 flag, specifies case insensitive matching: lowercase characters
68 in the magic match both lower and upper case characters in the
69 targer, whereas upper case characters in the magic, only much
70 uppercase characters in the target.
71 .It pstring
72 A pascal style string where the first byte is interpreted as the an
73 unsigned length.
74 The string is not
75 .Dv NUL
76 terminated.
77 .It date
78 A four-byte value interpreted as a
79 .Ux
80 date.
81 .It ldate
82 A four-byte value interpreted as a
83 .Ux Ns -style
84 date, but interpreted as
85 local time rather than UTC.
86 .It beshort
87 A two-byte value (on most systems) in big-endian byte order.
88 .It belong
89 A four-byte value (on most systems) in big-endian byte order.
90 .It bedate
91 A four-byte value (on most systems) in big-endian byte order,
92 interpreted as a
93 .Ux
94 date.
95 .It beldate
96 A four-byte value (on most systems) in big-endian byte order,
97 interpreted as a
98 .Ux Ns -style
99 date, but interpreted as local time rather
100 than UTC.
101 .It bestring16
102 A two-byte unicode (UCS16) string in big-endian byte order.
103 .It leshort
104 A two-byte value (on most systems) in little-endian byte order.
105 .It lelong
106 A four-byte value (on most systems) in little-endian byte order.
107 .It ledate
108 A four-byte value (on most systems) in little-endian byte order,
109 interpreted as a
110 .Ux
111 date.
112 .It leldate
113 A four-byte value (on most systems) in little-endian byte order,
114 interpreted as a
115 .Ux Ns -style
116 date, but interpreted as local time rather
117 than UTC.
118 .It lestring16
119 A two-byte unicode (UCS16) string in little-endian byte order.
120 .It melong
121 A four-byte value (on most systems) in middle-endian (PDP-11) byte order.
122 .It medate
123 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
124 interpreted as a
125 .Ux
126 date.
127 .It meldate
128 A four-byte value (on most systems) in middle-endian (PDP-11) byte order,
129 interpreted as a
130 .Ux Ns -style
131 date, but interpreted as local time rather
132 than UTC.
133 .It regex
134 A regular expression match in extended
135 .Tn POSIX
136 regular expression syntax
137 (much like egrep).
138 The type specification can be optionally followed by
139 .Ql /c
140 for case-insensitive matches.
141 The regular expression is always
142 tested against the first
143 .Ar N
144 lines, where
145 .Ar N
146 is the given offset, thus it
147 is only useful for (single-byte encoded) text.
148 .Ql ^
149 and
150 .Ql $
151 will match the beginning and end of individual lines, respectively,
152 not beginning and end of file.
153 .It search
154 A literal string search starting at the given offset.
155 It must be followed by
156 .Li / Ns Aq Ar number
157 which specifies how many matches shall be attempted (the range).
158 This is suitable for searching larger binary expressions with variable
159 offsets, using
160 .Ql \e
161 escapes for special characters.
162 .El
163 .El
164 .Pp
165 The numeric types may optionally be followed by
166 .Em &
167 and a numeric value,
168 to specify that the value is to be AND'ed with the
169 numeric value before any comparisons are done.
170 Prepending a
171 .Em u
172 to the type indicates that ordered comparisons should be unsigned.
173 .Bl -tag -width indent
174 .It test
175 The value to be compared with the value from the file.
176 If the type is
177 numeric, this value
178 is specified in C form; if it is a string, it is specified as a C string
179 with the usual escapes permitted (e.g.\& \en for new-line).
180 .It ""
181 Numeric values
182 may be preceded by a character indicating the operation to be performed.
183 It may be
184 .Em = ,
185 to specify that the value from the file must equal the specified value,
186 .Em < ,
187 to specify that the value from the file must be less than the specified
188 value,
189 .Em > ,
190 to specify that the value from the file must be greater than the specified
191 value,
192 .Em & ,
193 to specify that the value from the file must have set all of the bits
194 that are set in the specified value,
195 .Em ^ ,
196 to specify that the value from the file must have clear any of the bits
197 that are set in the specified value, or
198 .Em ~ ,
199 the value specified after is negated before tested, or
200 .Em x ,
201 to specify that any value will match.
202 If the character is omitted,
203 it is assumed to be
204 .Em = .
205 For all tests except
206 .Dq string
207 and
208 .Dq regex ,
209 operation
210 .Em !\&
211 specifies that the line matches if the test does
212 .Em not
213 succeed.
214 .It ""
215 Numeric values are specified in C form; e.g.\&
216 .Em 13
217 is decimal,
218 .Em 013
219 is octal, and
220 .Em 0x13
221 is hexadecimal.
222 .It ""
223 For string values, the byte string from the
224 file must match the specified byte string.
225 The operators
226 .Em = ,
227 .Em <
228 and
229 .Em >
230 (but not
231 .Em & )
232 can be applied to strings.
233 The length used for matching is that of the string argument
234 in the magic file.
235 This means that a line can match any string, and
236 then presumably print that string, by doing
237 .Em >\e0
238 (because all strings are greater than the null string).
239 .It message
240 The message to be printed if the comparison succeeds.
241 If the string
242 contains a
243 .Xr printf 3
244 format specification, the value from the file (with any specified masking
245 performed) is printed using the message as the format string.
246 .El
247 .Pp
248 Some file formats contain additional information which is to be printed
249 along with the file type or need additional tests to determine the true
250 file type.
251 These additional tests are introduced by one or more
252 .Em >
253 characters preceding the offset.
254 The number of
255 .Em >
256 on the line indicates the level of the test; a line with no
257 .Em >
258 at the beginning is considered to be at level 0.
259 Tests are arranged in a tree-like hierarchy:
260 If a the test on a line at level
261 .Em n
262 succeeds, all following tests at level
263 .Em n+1
264 are performed, and the messages printed if the tests succeed, until a line
265 with level
266 .Em n
267 (or less) appears.
268 For more complex files, one can use empty messages to get just the
269 "if/then" effect, in the following way:
270 .Bd -literal -offset indent
271 0      string   MZ
272 >0x18  leshort  <0x40   MS-DOS executable
273 >0x18  leshort  >0x3f   extended PC executable (e.g., MS Windows)
274 .Ed
275 .Pp
276 Offsets do not need to be constant, but can also be read from the file
277 being examined.
278 If the first character following the last
279 .Em >
280 is a
281 .Em \&(
282 then the string after the parenthesis is interpreted as an indirect offset.
283 That means that the number after the parenthesis is used as an offset in
284 the file.
285 The value at that offset is read, and is used again as an offset
286 in the file.
287 Indirect offsets are of the form:
288 .Em (x[.[bslBSL]][+\-][y]) .
289 The value of
290 .Em x
291 is used as an offset in the file.
292 A byte, short or long is read at that offset
293 depending on the
294 .Em [bslBSLm]
295 type specifier.
296 The capitalized types interpret the number as a big endian value, whereas
297 a small letter versions interpret the number as a little endian value;
298 the
299 .Em m
300 type interprets the number as a middle endian (PDP-11) value.
301 To that number the value of
302 .Em y
303 is added and the result is used as an offset in the file.
304 The default type
305 if one is not specified is long.
306 .Pp
307 That way variable length structures can be examined:
308 .Bd -literal -offset indent
309 # MS Windows executables are also valid MS-DOS executables
310 0           string  MZ
311 >0x18       leshort <0x40   MZ executable (MS-DOS)
312 # skip the whole block below if it is not an extended executable
313 >0x18       leshort >0x3f
314 >>(0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
315 >>(0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
316 .Ed
317 .Pp
318 This strategy of examining has one drawback: You must make sure that
319 you eventually print something, or users may get empty output (like, when
320 there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
321 .Pp
322 If this indirect offset cannot be used as-is, there are simple calculations
323 possible: appending
324 .Em [+-*/%&|^]<number>
325 inside parentheses allows one to modify
326 the value read from the file before it is used as an offset:
327 .Bd -literal -offset indent
328 # MS Windows executables are also valid MS-DOS executables
329 0           string  MZ
330 # sometimes, the value at 0x18 is less that 0x40 but there's still an
331 # extended executable, simply appended to the file
332 >0x18       leshort <0x40
333 >>(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
334 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
335 .Ed
336 .Pp
337 Sometimes you do not know the exact offset as this depends on the length or
338 position (when indirection was used before) of preceding fields.
339 You can
340 specify an offset relative to the end of the last uplevel field using
341 .Em &
342 as a prefix to the offset:
343 .Bd -literal -offset indent
344 0           string  MZ
345 >0x18       leshort >0x3f
346 >>(0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
347 # immediately following the PE signature is the CPU type
348 >>>&0       leshort 0x14c     for Intel 80386
349 >>>&0       leshort 0x184     for DEC Alpha
350 .Ed
351 .Pp
352 Indirect and relative offsets can be combined:
353 .Bd -literal -offset indent
354 0             string  MZ
355 >0x18         leshort <0x40
356 >>(4.s*512)   leshort !0x014c MZ executable (MS-DOS)
357 # if it's not COFF, go back 512 bytes and add the offset taken
358 # from byte 2/3, which is yet another way of finding the start
359 # of the extended executable
360 >>>&(2.s-514) string  LE      LE executable (MS Windows VxD driver)
361 .Ed
362 .Pp
363 Or the other way around:
364 .Bd -literal -offset indent
365 0                 string  MZ
366 >0x18             leshort >0x3f
367 >>(0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
368 # at offset 0x80 (-4, since relative offsets start at the end
369 # of the uplevel match) inside the LE header, we find the absolute
370 # offset to the code area, where we look for a specific signature
371 >>>(&0x7c.l+0x26) string  UPX     \eb, UPX compressed
372 .Ed
373 .Pp
374 Or even both!
375 .Bd -literal -offset indent
376 0                string  MZ
377 >0x18            leshort >0x3f
378 >>(0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
379 # at offset 0x58 inside the LE header, we find the relative offset
380 # to a data area where we look for a specific signature
381 >>>&(&0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
382 .Ed
383 .Pp
384 Finally, if you have to deal with offset/length pairs in your file, even the
385 second value in a parenthesed expression can be taken from the file itself,
386 using another set of parentheses.
387 Note that this additional indirect offset
388 is always relative to the start of the main indirect offset.
389 .Bd -literal -offset indent
390 0                 string       MZ
391 >0x18             leshort      >0x3f
392 >>(0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
393 # search for the PE section called ".idata"...
394 >>>&0xf4          search/0x140 .idata
395 # ...and go to the end of it, calculated from start+length;
396 # these are located 14 and 10 bytes after the section name
397 >>>>(&0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
398 .Ed
399 .Sh BUGS
400 The formats
401 .Em long ,
402 .Em belong ,
403 .Em lelong ,
404 .Em melong ,
405 .Em short ,
406 .Em beshort ,
407 .Em leshort ,
408 .Em date ,
409 .Em bedate ,
410 .Em medate ,
411 .Em ledate ,
412 .Em beldate ,
413 .Em leldate ,
414 and
415 .Em meldate
416 are system-dependent; perhaps they should be specified as a number
417 of bytes (2B, 4B, etc),
418 since the files being recognized typically come from
419 a system on which the lengths are invariant.
420 .Pp
421 If
422 .Pa /usr/share/misc/magic
423 is newer than
424 .Pa /usr/share/misc/magic.mgc
425 it is not used.
426 Use the command:
427 .Dq Li "cd /usr/share/misc && file -C -m magic"
428 to rebuild.
429 .Sh SEE ALSO
430 .Xr file 1
431 .\"
432 .\" From: guy@sun.uucp (Guy Harris)
433 .\" Newsgroups: net.bugs.usg
434 .\" Subject: /etc/magic's format isn't well documented
435 .\" Message-ID: <2752@sun.uucp>
436 .\" Date: 3 Sep 85 08:19:07 GMT
437 .\" Organization: Sun Microsystems, Inc.
438 .\" Lines: 136
439 .\"
440 .\" Here's a manual page for the format accepted by the "file" made by adding
441 .\" the changes I posted to the S5R2 version.
442 .\"
443 .\" Modified for Ian Darwin's version of the file command.
444 .\" @(#)$Id: magic.man,v 1.30 2006/02/19 18:16:03 christos Exp $