magic.man 13 KB

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