1.\" $OpenBSD: magic.5,v 1.19 2020/11/14 10:35:58 jmc Exp $ 2.\" 3.\" @(#)$FreeBSD: src/usr.bin/file/magic.5,v 1.11 2000/03/01 12:19:39 sheldonh Exp $ 4.\" 5.\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems. 6.\" 7.\" Copyright (c) Ian F. Darwin 1986-1995. 8.\" Software written by Ian F. Darwin and others; 9.\" maintained 1995-present by Christos Zoulas and others. 10.\" 11.\" Redistribution and use in source and binary forms, with or without 12.\" modification, are permitted provided that the following conditions 13.\" are met: 14.\" 1. Redistributions of source code must retain the above copyright 15.\" notice immediately at the beginning of the file, without modification, 16.\" this list of conditions, and the following disclaimer. 17.\" 2. Redistributions in binary form must reproduce the above copyright 18.\" notice, this list of conditions and the following disclaimer in the 19.\" documentation and/or other materials provided with the distribution. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 25.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.Dd $Mdocdate: November 14 2020 $ 34.Dt MAGIC 5 35.Os 36.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems. 37.Sh NAME 38.Nm magic 39.Nd file command's magic pattern file 40.Sh DESCRIPTION 41This manual page documents the format of the magic file as 42used by the 43.Xr file 1 44command. 45.Xr file 1 46identifies the type of a file using, 47among other tests, 48a test for whether the file contains certain 49.Dq "magic patterns" . 50The file 51.Pa /etc/magic 52specifies what magic numbers are to be tested for, 53what message to print if a particular magic number is found, 54and additional information to extract from the file. 55.Pp 56Each line of the file specifies a test to be performed. 57A test compares the data starting at a particular offset 58in the file with a byte value, a string or a numeric value. 59If the test succeeds, a message is printed. 60The line consists of the following fields: 61.Bl -tag -width "message" 62.It Dv offset 63A number specifying the offset, in bytes, into the file of the data 64which is to be tested. 65.It Dv type 66The type of the data to be tested. 67The possible values are: 68.Bl -tag -width "lestring16" 69.It Dv byte 70A one-byte value. 71.It Dv short 72A two-byte value in this machine's native byte order. 73.It Dv long 74A four-byte value in this machine's native byte order. 75.It Dv quad 76An eight-byte value in this machine's native byte order. 77.It Dv float 78A 32-bit single precision IEEE floating point number in this machine's native byte order. 79.It Dv double 80A 64-bit double precision IEEE floating point number in this machine's native byte order. 81.It Dv string 82A string of bytes. 83The string type specification can be optionally followed 84by /[Bbc]*. 85The 86.Dq B 87flag compacts whitespace in the target, which must 88contain at least one whitespace character. 89If the magic has 90.Dv n 91consecutive blanks, the target needs at least 92.Dv n 93consecutive blanks to match. 94The 95.Dq b 96flag treats every blank in the target as an optional blank. 97Finally the 98.Dq c 99flag, specifies case insensitive matching: lowercase 100characters in the magic match both lower and upper case characters in the 101target, whereas upper case characters in the magic only match uppercase 102characters in the target. 103.It Dv pstring 104A Pascal-style string where the first byte is interpreted as the an 105unsigned length. 106The string is not NUL terminated. 107.It Dv date 108A four-byte value interpreted as a UNIX date. 109.It Dv qdate 110An eight-byte value interpreted as a UNIX date. 111.It Dv ldate 112A four-byte value interpreted as a UNIX-style date, but interpreted as 113local time rather than UTC. 114.It Dv qldate 115An eight-byte value interpreted as a UNIX-style date, but interpreted as 116local time rather than UTC. 117.It Dv beshort 118A two-byte value in big-endian byte order. 119.It Dv belong 120A four-byte value in big-endian byte order. 121.It Dv bequad 122An eight-byte value in big-endian byte order. 123.It Dv befloat 124A 32-bit single precision IEEE floating point number in big-endian byte order. 125.It Dv bedouble 126A 64-bit double precision IEEE floating point number in big-endian byte order. 127.It Dv bedate 128A four-byte value in big-endian byte order, 129interpreted as a Unix date. 130.It Dv beqdate 131An eight-byte value in big-endian byte order, 132interpreted as a Unix date. 133.It Dv beldate 134A four-byte value in big-endian byte order, 135interpreted as a UNIX-style date, but interpreted as local time rather 136than UTC. 137.It Dv beqldate 138An eight-byte value in big-endian byte order, 139interpreted as a UNIX-style date, but interpreted as local time rather 140than UTC. 141.It Dv bestring16 142A two-byte unicode (UCS16) string in big-endian byte order. 143.It Dv leshort 144A two-byte value in little-endian byte order. 145.It Dv lelong 146A four-byte value in little-endian byte order. 147.It Dv lequad 148An eight-byte value in little-endian byte order. 149.It Dv lefloat 150A 32-bit single precision IEEE floating point number in little-endian byte order. 151.It Dv ledouble 152A 64-bit double precision IEEE floating point number in little-endian byte order. 153.It Dv ledate 154A four-byte value in little-endian byte order, 155interpreted as a UNIX date. 156.It Dv leqdate 157An eight-byte value in little-endian byte order, 158interpreted as a UNIX date. 159.It Dv leldate 160A four-byte value in little-endian byte order, 161interpreted as a UNIX-style date, but interpreted as local time rather 162than UTC. 163.It Dv leqldate 164An eight-byte value in little-endian byte order, 165interpreted as a UNIX-style date, but interpreted as local time rather 166than UTC. 167.It Dv lestring16 168A two-byte unicode (UCS16) string in little-endian byte order. 169.It Dv melong 170A four-byte value in middle-endian (PDP-11) byte order. 171.It Dv medate 172A four-byte value in middle-endian (PDP-11) byte order, 173interpreted as a UNIX date. 174.It Dv meldate 175A four-byte value in middle-endian (PDP-11) byte order, 176interpreted as a UNIX-style date, but interpreted as local time rather 177than UTC. 178.It Dv regex 179A regular expression match in extended POSIX regular expression syntax 180(like egrep). 181Regular expressions can take exponential time to process, 182and their performance is hard to predict, so their use is discouraged. 183When used in production environments, 184their performance should be carefully checked. 185The type specification can be optionally followed by 186.Dv /[c][s] . 187The 188.Dq c 189flag makes the match case insensitive, while the 190.Dq s 191flag update the offset to the start offset of the match, rather than the end. 192The regular expression is tested against line 193.Dv N + 1 194onwards, where 195.Dv N 196is the given offset. 197Line endings are assumed to be in the machine's native format. 198.Dv ^ 199and 200.Dv $ 201match the beginning and end of individual lines, respectively, 202not beginning and end of file. 203.It Dv search 204A literal string search starting at the given offset. 205The same modifier flags can be used as for string patterns. 206The modifier flags (if any) must be followed by 207.Dv /number 208the range, that is, the number of positions at which the match will be 209attempted, starting from the start offset. 210This is suitable for searching larger binary expressions 211with variable offsets, using 212.Dv \e 213escapes for special characters. 214The offset works as for regex. 215.It Dv default 216This is intended to be used with the test 217.Em x 218(which is always true) and a message that is to be used if there are 219no other matches. 220.It Dv clear 221This test is always true and clears the match flag for that level. 222It is intended to be used with the default test. 223.It Dv name 224Define a named magic instance that can be called from another 225.Dv use 226magic entry, like a subroutine call. 227Named instance direct magic offsets are relative to the offset of the 228previous matched entry, but indirect offsets are relative to the 229beginning of the file as usual. 230Named magic entries always match. 231.It Dv use 232Recursively call the named magic starting from the current offset. 233If the name of the referenced instance begins with a 234.Dv ^ 235then the endianness of the magic is switched; if the magic mentioned 236.Dv leshort 237for example, 238it is treated as 239.Dv beshort 240and vice versa. 241This is useful to avoid duplicating the rules for different endianness. 242.El 243.Pp 244Each top-level magic pattern (see below for an explanation of levels) 245is classified as text or binary according to the types used. 246Types 247.Dq regex 248and 249.Dq search 250are classified as text tests, unless non-printable characters are used 251in the pattern. 252All other tests are classified as binary. 253A top-level pattern is considered to be a test text 254when all its patterns are text 255patterns; otherwise, it is considered to be a binary pattern. 256When matching a file, binary patterns are tried first; if no match is 257found, and the file looks like text, then its encoding is determined 258and the text patterns are tried. 259.Pp 260The numeric types may optionally be followed by 261.Dv & 262and a numeric value, 263to specify that the value is to be AND'ed with the 264numeric value before any comparisons are done. 265Prepending a 266.Dv u 267to the type indicates that ordered comparisons should be unsigned. 268.It Dv test 269The value to be compared with the value from the file. 270If the type is 271numeric, this value 272is specified in C form; if it is a string, it is specified as a C string 273with the usual escapes permitted (e.g. \en for new-line). 274.Pp 275Numeric values 276may be preceded by a character indicating the operation to be performed. 277It may be 278.Dv = , 279to specify that the value from the file must equal the specified value, 280.Dv \*(Lt , 281to specify that the value from the file must be less than the specified 282value, 283.Dv \*(Gt , 284to specify that the value from the file must be greater than the specified 285value, 286.Dv & , 287to specify that the value from the file must have set all of the bits 288that are set in the specified value, 289.Dv ^ , 290to specify that the value from the file must have clear any of the bits 291that are set in the specified value, or 292.Dv ~ , 293the value specified after is negated before tested. 294.Dv x , 295to specify that any value will match. 296If the character is omitted, it is assumed to be 297.Dv = . 298Operators 299.Dv & , 300.Dv ^ , 301and 302.Dv ~ 303don't work with floats and doubles. 304The operator 305.Dv !\& 306specifies that the line matches if the test does 307.Em not 308succeed. 309.Pp 310Numeric values are specified in C form; e.g. 311.Dv 13 312is decimal, 313.Dv 013 314is octal, and 315.Dv 0x13 316is hexadecimal. 317.Pp 318For string values, the string from the 319file must match the specified string. 320The operators 321.Dv = , 322.Dv \*(Lt 323and 324.Dv \*(Gt 325(but not 326.Dv & ) 327can be applied to strings. 328The length used for matching is that of the string argument 329in the magic file. 330This means that a line can match any non-empty string (usually used to 331then print the string), with 332.Em \*(Gt\e0 333(because all non-empty strings are greater than the empty string). 334.Pp 335The special test 336.Em x 337always evaluates to true. 338.It Dv message 339The message to be printed if the comparison succeeds. 340If the string contains a 341.Xr printf 3 342format specification, the value from the file (with any specified masking 343performed) is printed using the message as the format string. 344If the string begins with 345.Dq \eb , 346the message printed is the remainder of the string with no whitespace 347added before it: multiple matches are normally separated by a single 348space. 349.El 350.Pp 351A MIME type is given on a separate line, which must be the next 352non-blank or comment line after the magic line that identifies the 353file type, and has the following format: 354.Bd -literal -offset indent 355!:mime MIMETYPE 356.Ed 357.Pp 358i.e. the literal string 359.Dq !:mime 360followed by the MIME type. 361.Pp 362Some file formats contain additional information which is to be printed 363along with the file type or need additional tests to determine the true 364file type. 365These additional tests are introduced by one or more 366.Em \*(Gt 367characters preceding the offset. 368The number of 369.Em \*(Gt 370on the line indicates the level of the test; a line with no 371.Em \*(Gt 372at the beginning is considered to be at level 0. 373Tests are arranged in a tree-like hierarchy: 374If a test on a line at level 375.Em n 376succeeds, all following tests at level 377.Em n+1 378are performed, and the messages printed if the tests succeed, until a line 379with level 380.Em n 381(or less) appears. 382For more complex files, one can use empty messages to get just the 383"if/then" effect, in the following way: 384.Bd -literal -offset indent 3850 string MZ 386\*(Gt0x18 leshort \*(Lt0x40 MS-DOS executable 387\*(Gt0x18 leshort \*(Gt0x3f extended PC executable (e.g., MS Windows) 388.Ed 389.Pp 390Offsets do not need to be constant, but can also be read from the file 391being examined. 392If the first character following the last 393.Em \*(Gt 394is a 395.Em \&( 396then the string after the parenthesis is interpreted as an indirect offset. 397That means that the number after the parenthesis is used as an offset in 398the file. 399The value at that offset is read, and is used again as an offset 400in the file. 401Indirect offsets are of the form: 402.Em (( x [.[bslBSL]][+\-][ y ]) . 403The value of 404.Em x 405is used as an offset in the file. 406A byte, short or long is read at that offset depending on the 407.Op bslBSLm 408type specifier. 409The capitalized types interpret the number as a big endian 410value, whereas the small letter versions interpret the number as a little 411endian value; 412the 413.Em m 414type interprets the number as a middle endian (PDP-11) value. 415To that number the value of 416.Em y 417is added and the result is used as an offset in the file. 418The default type if one is not specified is long. 419.Pp 420That way variable length structures can be examined: 421.Bd -literal -offset indent 422# MS Windows executables are also valid MS-DOS executables 4230 string MZ 424\*(Gt0x18 leshort \*(Lt0x40 MZ executable (MS-DOS) 425# skip the whole block below if it is not an extended executable 426\*(Gt0x18 leshort \*(Gt0x3f 427\*(Gt\*(Gt(0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 428\*(Gt\*(Gt(0x3c.l) string LX\e0\e0 LX executable (OS/2) 429.Ed 430.Pp 431This strategy of examining has a drawback: You must make sure that 432you eventually print something, or users may get empty output (like, when 433there is neither PE\e0\e0 nor LE\e0\e0 in the above example) 434.Pp 435If this indirect offset cannot be used directly, simple calculations are 436possible: appending 437.Em [+-*/%&|^]number 438inside parentheses allows one to modify 439the value read from the file before it is used as an offset: 440.Bd -literal -offset indent 441# MS Windows executables are also valid MS-DOS executables 4420 string MZ 443# sometimes, the value at 0x18 is less that 0x40 but there's still an 444# extended executable, simply appended to the file 445\*(Gt0x18 leshort \*(Lt0x40 446\*(Gt\*(Gt(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP) 447\*(Gt\*(Gt(4.s*512) leshort !0x014c MZ executable (MS-DOS) 448.Ed 449.Pp 450Sometimes you do not know the exact offset as this depends on the length or 451position (when indirection was used before) of preceding fields. 452You can specify an offset relative to the end of the last up-level 453field using 454.Sq & 455as a prefix to the offset: 456.Bd -literal -offset indent 4570 string MZ 458\*(Gt0x18 leshort \*(Gt0x3f 459\*(Gt\*(Gt(0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 460# immediately following the PE signature is the CPU type 461\*(Gt\*(Gt\*(Gt&0 leshort 0x14c for Intel 80386 462\*(Gt\*(Gt\*(Gt&0 leshort 0x184 for DEC Alpha 463.Ed 464.Pp 465Indirect and relative offsets can be combined: 466.Bd -literal -offset indent 4670 string MZ 468\*(Gt0x18 leshort \*(Lt0x40 469\*(Gt\*(Gt(4.s*512) leshort !0x014c MZ executable (MS-DOS) 470# if it's not COFF, go back 512 bytes and add the offset taken 471# from byte 2/3, which is yet another way of finding the start 472# of the extended executable 473\*(Gt\*(Gt\*(Gt&(2.s-514) string LE LE executable (MS Windows VxD driver) 474.Ed 475.Pp 476Or the other way around: 477.Bd -literal -offset indent 4780 string MZ 479\*(Gt0x18 leshort \*(Gt0x3f 480\*(Gt\*(Gt(0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 481# at offset 0x80 (-4, since relative offsets start at the end 482# of the up-level match) inside the LE header, we find the absolute 483# offset to the code area, where we look for a specific signature 484\*(Gt\*(Gt\*(Gt(&0x7c.l+0x26) string UPX \eb, UPX compressed 485.Ed 486.Pp 487Or even both! 488.Bd -literal -offset indent 4890 string MZ 490\*(Gt0x18 leshort \*(Gt0x3f 491\*(Gt\*(Gt(0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 492# at offset 0x58 inside the LE header, we find the relative offset 493# to a data area where we look for a specific signature 494\*(Gt\*(Gt\*(Gt&(&0x54.l-3) string UNACE \eb, ACE self-extracting archive 495.Ed 496.Pp 497Finally, if you have to deal with offset/length pairs in your file, even the 498second value in a parenthesized expression can be taken from the file itself, 499using another set of parentheses. 500Note that this additional indirect offset is always relative to the 501start of the main indirect offset. 502.Bd -literal -offset indent 5030 string MZ 504\*(Gt0x18 leshort \*(Gt0x3f 505\*(Gt\*(Gt(0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 506# search for the PE section called ".idata"... 507\*(Gt\*(Gt\*(Gt&0xf4 search/0x140 .idata 508# ...and go to the end of it, calculated from start+length; 509# these are located 14 and 10 bytes after the section name 510\*(Gt\*(Gt\*(Gt\*(Gt(&0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive 511.Ed 512.Sh SEE ALSO 513.Xr file 1 514\- the command that reads this file. 515.Sh BUGS 516The formats 517.Dv long , 518.Dv belong , 519.Dv lelong , 520.Dv melong , 521.Dv short , 522.Dv beshort , 523.Dv leshort , 524.Dv date , 525.Dv bedate , 526.Dv medate , 527.Dv ledate , 528.Dv beldate , 529.Dv leldate , 530and 531.Dv meldate 532are system-dependent; perhaps they should be specified as a number 533of bytes (2B, 4B, etc), 534since the files being recognized typically come from 535a system on which the lengths are invariant. 536.\" 537.\" From: guy@sun.uucp (Guy Harris) 538.\" Newsgroups: net.bugs.usg 539.\" Subject: /etc/magic's format isn't well documented 540.\" Message-ID: <2752@sun.uucp> 541.\" Date: 3 Sep 85 08:19:07 GMT 542.\" Organization: Sun Microsystems, Inc. 543.\" Lines: 136 544.\" 545.\" Here's a manual page for the format accepted by the "file" made by adding 546.\" the changes I posted to the S5R2 version. 547.\" 548.\" Modified for Ian Darwin's version of the file command. 549