magic.man 14 KB

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