magic.man 14 KB

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