Debian Codegraph @

internal/magic/binary.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "debug/macho"
6 "encoding/binary"
7 "slices"
8 )
9
10 // Lnk matches Microsoft lnk binary format.
11 func Lnk(raw []byte, _ uint32) bool {
12 return bytes.HasPrefix(raw, []byte{0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00})
13 }
14
15 // Wasm matches a web assembly File Format file.
16 func Wasm(raw []byte, _ uint32) bool {
17 return bytes.HasPrefix(raw, []byte{0x00, 0x61, 0x73, 0x6D})
18 }
19
20 // Exe matches a Windows/DOS executable file.
21 func Exe(raw []byte, _ uint32) bool {
22 return len(raw) > 1 && raw[0] == 0x4D && raw[1] == 0x5A
23 }
24
25 // Elf matches an Executable and Linkable Format file.
26 func Elf(raw []byte, _ uint32) bool {
27 return bytes.HasPrefix(raw, []byte{0x7F, 0x45, 0x4C, 0x46})
28 }
29
30 // Nes matches a Nintendo Entertainment system ROM file.
31 func Nes(raw []byte, _ uint32) bool {
32 return bytes.HasPrefix(raw, []byte{0x4E, 0x45, 0x53, 0x1A})
33 }
34
35 // SWF matches an Adobe Flash swf file.
36 func SWF(raw []byte, _ uint32) bool {
37 return bytes.HasPrefix(raw, []byte("CWS")) ||
38 bytes.HasPrefix(raw, []byte("FWS")) ||
39 bytes.HasPrefix(raw, []byte("ZWS"))
40 }
41
42 // Torrent has bencoded text in the beginning.
43 func Torrent(raw []byte, _ uint32) bool {
44 return bytes.HasPrefix(raw, []byte("d8:announce"))
45 }
46
47 // PAR1 matches a parquet file.
48 func Par1(raw []byte, _ uint32) bool {
49 return bytes.HasPrefix(raw, []byte{0x50, 0x41, 0x52, 0x31})
50 }
51
52 // CBOR matches a Concise Binary Object Representation https://cbor.io/
53 func CBOR(raw []byte, _ uint32) bool {
54 return bytes.HasPrefix(raw, []byte{0xD9, 0xD9, 0xF7})
55 }
56
57 // Java bytecode and Mach-O binaries share the same magic number.
58 // More info here https://github.com/threatstack/libmagic/blob/master/magic/Magdir/cafebabe
59 func classOrMachOFat(in []byte) bool {
60 // There should be at least 8 bytes for both of them because the only way to
61 // quickly distinguish them is by comparing byte at position 7
62 if len(in) < 8 {
63 return false
64 }
65
66 return binary.BigEndian.Uint32(in) == macho.MagicFat
67 }
68
69 // Class matches a java class file.
70 func Class(raw []byte, limit uint32) bool {
71 return classOrMachOFat(raw) && raw[7] > 30
72 }
73
74 // MachO matches Mach-O binaries format.
75 func MachO(raw []byte, limit uint32) bool {
76 if classOrMachOFat(raw) && raw[7] < 0x14 {
77 return true
78 }
79
80 if len(raw) < 4 {
81 return false
82 }
83
84 be := binary.BigEndian.Uint32(raw)
85 le := binary.LittleEndian.Uint32(raw)
86
87 return be == macho.Magic32 ||
88 le == macho.Magic32 ||
89 be == macho.Magic64 ||
90 le == macho.Magic64
91 }
92
93 // Dbf matches a dBase file.
94 // https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
95 func Dbf(raw []byte, limit uint32) bool {
96 if len(raw) < 68 {
97 return false
98 }
99
100 // 3rd and 4th bytes contain the last update month and day of month.
101 if raw[2] == 0 || raw[2] > 12 || raw[3] == 0 || raw[3] > 31 {
102 return false
103 }
104
105 // 12, 13, 30, 31 are reserved bytes and always filled with 0x00.
106 if raw[12] != 0x00 || raw[13] != 0x00 || raw[30] != 0x00 || raw[31] != 0x00 {
107 return false
108 }
109 // Production MDX flag;
110 // 0x01 if a production .MDX file exists for this table;
111 // 0x00 if no .MDX file exists.
112 if raw[28] > 0x01 {
113 return false
114 }
115
116 // dbf type is dictated by the first byte.
117 dbfTypes := []byte{
118 0x02, 0x03, 0x04, 0x05, 0x30, 0x31, 0x32, 0x42, 0x62, 0x7B, 0x82,
119 0x83, 0x87, 0x8A, 0x8B, 0x8E, 0xB3, 0xCB, 0xE5, 0xF5, 0xF4, 0xFB,
120 }
121 return slices.Contains(dbfTypes, raw[0])
122 }
123
124 // ElfObj matches an object file.
125 func ElfObj(raw []byte, limit uint32) bool {
126 return len(raw) > 17 && ((raw[16] == 0x01 && raw[17] == 0x00) ||
127 (raw[16] == 0x00 && raw[17] == 0x01))
128 }
129
130 // ElfExe matches an executable file.
131 func ElfExe(raw []byte, limit uint32) bool {
132 return len(raw) > 17 && ((raw[16] == 0x02 && raw[17] == 0x00) ||
133 (raw[16] == 0x00 && raw[17] == 0x02))
134 }
135
136 // ElfLib matches a shared library file.
137 func ElfLib(raw []byte, limit uint32) bool {
138 return len(raw) > 17 && ((raw[16] == 0x03 && raw[17] == 0x00) ||
139 (raw[16] == 0x00 && raw[17] == 0x03))
140 }
141
142 // ElfDump matches a core dump file.
143 func ElfDump(raw []byte, limit uint32) bool {
144 return len(raw) > 17 && ((raw[16] == 0x04 && raw[17] == 0x00) ||
145 (raw[16] == 0x00 && raw[17] == 0x04))
146 }
147
148 // Dcm matches a DICOM medical format file.
149 func Dcm(raw []byte, limit uint32) bool {
150 return len(raw) > 131 &&
151 bytes.Equal(raw[128:132], []byte{0x44, 0x49, 0x43, 0x4D})
152 }
153
154 // Marc matches a MARC21 (MAchine-Readable Cataloging) file.
155 func Marc(raw []byte, limit uint32) bool {
156 // File is at least 24 bytes ("leader" field size).
157 if len(raw) < 24 {
158 return false
159 }
160
161 // Fixed bytes at offset 20.
162 if !bytes.Equal(raw[20:24], []byte("4500")) {
163 return false
164 }
165
166 // First 5 bytes are ASCII digits.
167 for i := 0; i < 5; i++ {
168 if raw[i] < '0' || raw[i] > '9' {
169 return false
170 }
171 }
172
173 // Field terminator is present in first 2048 bytes.
174 return bytes.Contains(raw[:min(2048, len(raw))], []byte{0x1E})
175 }
176
177 // GLB matches a glTF model format file.
178 // GLB is the binary file format representation of 3D models saved in
179 // the GL transmission Format (glTF).
180 // GLB uses little endian and its header structure is as follows:
181 //
182 // <-- 12-byte header -->
183 // | magic | version | length |
184 // | (uint32) | (uint32) | (uint32) |
185 // | \x67\x6C\x54\x46 | \x01\x00\x00\x00 | ... |
186 // | g l T F | 1 | ... |
187 //
188 // Visit [glTF specification] and [IANA glTF entry] for more details.
189 //
190 // [glTF specification]: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
191 // [IANA glTF entry]: https://www.iana.org/assignments/media-types/model/gltf-binary
192 func GLB(raw []byte, _ uint32) bool {
193 return bytes.HasPrefix(raw, []byte("\x67\x6C\x54\x46\x02\x00\x00\x00")) ||
194 bytes.HasPrefix(raw, []byte("\x67\x6C\x54\x46\x01\x00\x00\x00"))
195 }
196
197 // TzIf matches a Time Zone Information Format (TZif) file.
198 // See more: https://tools.ietf.org/id/draft-murchison-tzdist-tzif-00.html#rfc.section.3
199 // Its header structure is shown below:
200 //
201 // +---------------+---+
202 // | magic (4) | <-+-- version (1)
203 // +---------------+---+---------------------------------------+
204 // | [unused - reserved for future use] (15) |
205 // +---------------+---------------+---------------+-----------+
206 // | isutccnt (4) | isstdcnt (4) | leapcnt (4) |
207 // +---------------+---------------+---------------+
208 // | timecnt (4) | typecnt (4) | charcnt (4) |
209 func TzIf(raw []byte, limit uint32) bool {
210 // File is at least 44 bytes (header size).
211 if len(raw) < 44 {
212 return false
213 }
214
215 if !bytes.HasPrefix(raw, []byte("TZif")) {
216 return false
217 }
218
219 // Field "typecnt" MUST not be zero.
220 if binary.BigEndian.Uint32(raw[36:40]) == 0 {
221 return false
222 }
223
224 // Version has to be NUL (0x00), '2' (0x32) or '3' (0x33).
225 return raw[4] == 0x00 || raw[4] == 0x32 || raw[4] == 0x33
226 }
227
228 // Pyc matches a Python compiled file.
229 // The signatures are sourced from libmagic v5.47
230 func Pyc(raw []byte, limit uint32) bool {
231 if len(raw) < 8 {
232 return false
233 }
234
235 // python 1.0 through 3.7 signatures, magic/Magdir/python:13:190
236 pycMagic := []uint32{
237 0x02099900, 0x03099900, 0x892e0d0a, 0x04170d0a, 0x994e0d0a, 0xfcc40d0a,
238 0xfdc40d0a, 0x87c60d0a, 0x88c60d0a, 0x2aeb0d0a, 0x2beb0d0a, 0x2ded0d0a,
239 0x2eed0d0a, 0x3bf20d0a, 0x3cf20d0a, 0x45f20d0a, 0x59f20d0a, 0x63f20d0a,
240 0x6df20d0a, 0x6ef20d0a, 0x77f20d0a, 0x81f20d0a, 0x8bf20d0a, 0x8cf20d0a,
241 0x95f20d0a, 0x9ff20d0a, 0xa9f20d0a, 0xb3f20d0a, 0xb4f20d0a, 0xc7f20d0a,
242 0xd1f20d0a, 0xd2f20d0a, 0xdbf20d0a, 0xe5f20d0a, 0xeff20d0a, 0xf9f20d0a,
243 0x03f30d0a, 0x04f30d0a, 0x0af30d0a, 0xb80b0d0a, 0xc20b0d0a, 0xcc0b0d0a,
244 0xd60b0d0a, 0xe00b0d0a, 0xea0b0d0a, 0xf40b0d0a, 0xf50b0d0a, 0xff0b0d0a,
245 0x090c0d0a, 0x130c0d0a, 0x1d0c0d0a, 0x1f0c0d0a, 0x270c0d0a, 0x3b0c0d0a,
246 0x450c0d0a, 0x4f0c0d0a, 0x580c0d0a, 0x620c0d0a, 0x6c0c0d0a, 0x760c0d0a,
247 0x800c0d0a, 0x8a0c0d0a, 0x940c0d0a, 0x9e0c0d0a, 0xb20c0d0a, 0xbc0c0d0a,
248 0xc60c0d0a, 0xd00c0d0a, 0xda0c0d0a, 0xe40c0d0a, 0xee0c0d0a, 0xf80c0d0a,
249 0x020d0d0a, 0x0c0d0d0a, 0x160d0d0a, 0x170d0d0a, 0x200d0d0a, 0x210d0d0a,
250 0x2a0d0d0a, 0x2b0d0d0a, 0x2c0d0d0a, 0x2d0d0d0a, 0x2f0d0d0a, 0x300d0d0a,
251 0x310d0d0a, 0x320d0d0a, 0x330d0d0a, 0x3e0d0d0a, 0x3f0d0d0a,
252 }
253
254 n := binary.BigEndian.Uint32(raw)
255
256 if slices.Contains(pycMagic, n) {
257 return true
258 }
259
260 if raw[2] == 0x0d && raw[3] == 0x0a {
261 // Only two bits of flag field are currently used.
262 if l := binary.LittleEndian.Uint32(raw[4:]); l > 3 {
263 return false
264 }
265 if raw[1] == 0x0d || raw[1] == 0x0e {
266 return true
267 }
268 // PyPy magic numbers, magic/Magdir/python:233
269 n := binary.LittleEndian.Uint16(raw)
270 return n == 240 || n == 256 || n == 336 || n == 384 || n == 416
271 }
272
273 return false
274 }
275
276 // Pcap identifies "libpcap" capture files.
277 // https://www.tcpdump.org/manpages/pcap-savefile.5.html
278 func Pcap(raw []byte, _ uint32) bool {
279 if len(raw) < 4 {
280 return false
281 }
282 be := binary.BigEndian.Uint32(raw)
283 le := binary.LittleEndian.Uint32(raw)
284 return be == 0xa1b2c3d4 || be == 0xa1b23c4d ||
285 le == 0xa1b2c3d4 || le == 0xa1b23c4d
286 }