magic.man 19 KB

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