magic.man 15 KB

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