magic.man 17 KB

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