Debian Codegraph @

lualib/lua_magic/patterns.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]] --
16
17 --[[[
18 -- @module lua_magic/patterns
19 -- This module contains most common patterns
20 --]]
21
22 local heuristics = require "lua_magic/heuristics"
23
24 local patterns = {
25 pdf = {
26 -- These are alternatives
27 matches = {
28 {
29 string = [[%PDF-[12]\.\d]],
30 position = { '<=', 1024 },
31 weight = 60,
32 heuristic = heuristics.pdf_format_heuristic
33 },
34 {
35 string = [[%FDF-[12]\.\d]],
36 position = { '<=', 1024 },
37 weight = 60,
38 heuristic = heuristics.pdf_format_heuristic
39 },
40 },
41 },
42 ps = {
43 matches = {
44 {
45 string = [[%!PS-Adobe]],
46 relative_position = 0,
47 weight = 60,
48 },
49 },
50 },
51 -- RTF document
52 rtf = {
53 matches = {
54 {
55 string = [[^{\\rt]],
56 position = 4,
57 weight = 60,
58 }
59 }
60 },
61 chm = {
62 matches = {
63 {
64 string = [[ITSF]],
65 relative_position = 0,
66 weight = 60,
67 }
68 }
69 },
70 djvu = {
71 matches = {
72 {
73 string = [[AT&TFORM]],
74 relative_position = 0,
75 weight = 60,
76 },
77 {
78 string = [[DJVM]],
79 relative_position = 0x0c,
80 weight = 60,
81 }
82 }
83 },
84 -- MS Office format, needs heuristic
85 ole = {
86 matches = {
87 {
88 hex = [[d0cf11e0a1b11ae1]],
89 relative_position = 0,
90 weight = 60,
91 heuristic = heuristics.ole_format_heuristic
92 }
93 }
94 },
95 -- MS Exe file
96 exe = {
97 matches = {
98 {
99 string = [[MZ]],
100 relative_position = 0,
101 weight = 15,
102 },
103 -- PE part
104 {
105 string = [[PE\x{00}\x{00}]],
106 position = { '>=', 0x3c + 4 },
107 weight = 15,
108 heuristic = heuristics.pe_part_heuristic,
109 }
110 }
111 },
112 elf = {
113 matches = {
114 {
115 hex = [[7f454c46]],
116 relative_position = 0,
117 weight = 60,
118 },
119 }
120 },
121 lnk = {
122 matches = {
123 {
124 hex = [[4C0000000114020000000000C000000000000046]],
125 relative_position = 0,
126 weight = 60,
127 },
128 }
129 },
130 bat = {
131 matches = {
132 {
133 string = [[(?i)@\s*ECHO\s+OFF]],
134 position = { '>=', 0 },
135 weight = 60,
136 },
137 }
138 },
139 class = {
140 -- Technically, this also matches MachO files, but I don't care about
141 -- Apple and their mental health problems here: just consider Java files,
142 -- Mach object files and all other cafe babes as bad and block them!
143 matches = {
144 {
145 hex = [[cafebabe]],
146 relative_position = 0,
147 weight = 60,
148 },
149 }
150 },
151 ics = {
152 matches = {
153 {
154 string = [[BEGIN:VCALENDAR]],
155 weight = 60,
156 relative_position = 0,
157 }
158 }
159 },
160 vcf = {
161 matches = {
162 {
163 string = [[BEGIN:VCARD]],
164 weight = 60,
165 relative_position = 0,
166 }
167 }
168 },
169 -- Archives
170 arj = {
171 matches = {
172 {
173 hex = '60EA',
174 relative_position = 0,
175 weight = 60,
176 },
177 }
178 },
179 ace = {
180 matches = {
181 {
182 string = [[\*\*ACE\*\*]],
183 position = 14,
184 weight = 60,
185 },
186 }
187 },
188 cab = {
189 matches = {
190 {
191 hex = [[4d53434600000000]], -- Can be anywhere for SFX :(
192 position = { '>=', 8 },
193 weight = 60,
194 },
195 }
196 },
197 tar = {
198 matches = {
199 {
200 string = [[ustar]],
201 relative_position = 257,
202 weight = 60,
203 },
204 }
205 },
206 bz2 = {
207 matches = {
208 {
209 string = "^BZ[h0]",
210 position = 3,
211 weight = 60,
212 },
213 }
214 },
215 lz4 = {
216 matches = {
217 {
218 hex = "04224d18",
219 relative_position = 0,
220 weight = 60,
221 },
222 {
223 hex = "03214c18",
224 relative_position = 0,
225 weight = 60,
226 },
227 {
228 hex = "02214c18",
229 relative_position = 0,
230 weight = 60,
231 },
232 {
233 -- MozLZ4
234 hex = '6d6f7a4c7a343000',
235 relative_position = 0,
236 weight = 60,
237 }
238 }
239 },
240 zst = {
241 matches = {
242 {
243 string = [[^[\x{22}-\x{40}]\x{B5}\x{2F}\x{FD}]],
244 position = 4,
245 weight = 60,
246 },
247 }
248 },
249 zoo = {
250 matches = {
251 {
252 hex = [[dca7c4fd]],
253 relative_position = 20,
254 weight = 60,
255 },
256 }
257 },
258 zip = {
259 matches = {
260 {
261 hex = [[504b0304]], -- PK\x03\x04
262 relative_position = 0,
263 weight = 60,
264 },
265 }
266 },
267 rar = {
268 matches = {
269 {
270 hex = [[526172211a0700]], -- RAR4
271 relative_position = 0,
272 weight = 60,
273 },
274 {
275 hex = [[526172211a070100]], -- RAR5
276 relative_position = 0,
277 weight = 60,
278 },
279 }
280 },
281 ['7z'] = {
282 matches = {
283 {
284 hex = [[377abcaf271c]], -- 7z signature
285 relative_position = 0,
286 weight = 60,
287 },
288 }
289 },
290 gz = {
291 matches = {
292 {
293 string = [[^\x{1f}\x{8b}\x{08}]], -- gzip with deflate method
294 position = 3,
295 weight = 60,
296 },
297 }
298 },
299 xar = {
300 matches = {
301 {
302 string = [[xar!]],
303 relative_position = 0,
304 weight = 60,
305 },
306 }
307 },
308 iso = {
309 matches = {
310 {
311 string = [[\x{01}CD001\x{01}]],
312 position = { '>=', 0x8000 + 7 }, -- first 32k is unused
313 weight = 60,
314 },
315 }
316 },
317 egg = {
318 -- ALZip egg
319 matches = {
320 {
321 string = [[EGGA]],
322 weight = 60,
323 relative_position = 0,
324 },
325 }
326 },
327 alz = {
328 -- ALZip alz
329 matches = {
330 {
331 string = [[ALZ\x{01}]],
332 weight = 60,
333 relative_position = 0,
334 },
335 }
336 },
337 -- Apple is a 'special' child: this needs to be matched at the data tail...
338 dmg = {
339 matches = {
340 {
341 string = [[koly\x{00}\x{00}\x{00}\x{04}]],
342 position = -512 + 8,
343 weight = 61,
344 tail = 512,
345 },
346 }
347 },
348 szdd = {
349 matches = {
350 {
351 hex = [[535a4444]],
352 relative_position = 0,
353 weight = 60,
354 },
355 }
356 },
357 xz = {
358 matches = {
359 {
360 hex = [[FD377A585A00]],
361 relative_position = 0,
362 weight = 60,
363 },
364 }
365 },
366 -- Images
367 psd = {
368 matches = {
369 {
370 string = [[8BPS]],
371 relative_position = 0,
372 weight = 60,
373 },
374 }
375 },
376 ico = {
377 matches = {
378 {
379 hex = [[00000100]],
380 relative_position = 0,
381 weight = 60,
382 },
383 }
384 },
385 pcx = {
386 matches = {
387 {
388 hex = [[0A050108]],
389 relative_position = 0,
390 weight = 60,
391 },
392 }
393 },
394 pic = {
395 matches = {
396 {
397 hex = [[FF80C9C71A00]],
398 relative_position = 0,
399 weight = 60,
400 },
401 }
402 },
403 swf = {
404 matches = {
405 {
406 hex = [[5a5753]], -- LZMA
407 relative_position = 0,
408 weight = 60,
409 },
410 {
411 hex = [[435753]], -- Zlib
412 relative_position = 0,
413 weight = 60,
414 },
415 {
416 hex = [[465753]], -- Uncompressed
417 relative_position = 0,
418 weight = 60,
419 },
420 }
421 },
422 tiff = {
423 matches = {
424 {
425 hex = [[49492a00]], -- LE encoded
426 relative_position = 0,
427 weight = 60,
428 },
429 {
430 hex = [[4d4d]], -- BE tiff
431 relative_position = 0,
432 weight = 60,
433 },
434 }
435 },
436 webp = {
437 matches = {
438 {
439 -- RIFF....WEBP
440 string = [[^RIFF....WEBP]],
441 position = 12,
442 weight = 60,
443 },
444 }
445 },
446 svg = {
447 matches = {
448 {
449 -- Case-insensitive <svg ...> in the first chunk
450 -- Use heuristic to avoid misdetecting HTML with embedded SVG
451 string = [[(?i)<svg\b]],
452 position = { '<=', 4096 },
453 weight = 40,
454 heuristic = heuristics.svg_format_heuristic
455 },
456 {
457 -- Case-insensitive <!DOCTYPE svg ...> within the first 4KiB
458 -- DOCTYPE svg is unambiguous - no heuristic needed
459 string = [[(?i)<!doctype\s+svg]],
460 position = { '<=', 4096 },
461 weight = 40,
462 },
463 }
464 },
465 -- Other
466 pgp = {
467 matches = {
468 {
469 hex = [[A803504750]],
470 relative_position = 0,
471 weight = 60,
472 },
473 {
474 hex = [[2D424547494E20504750204D4553534147452D]],
475 relative_position = 0,
476 weight = 60,
477 },
478 }
479 },
480 uue = {
481 matches = {
482 {
483 hex = [[626567696e20]],
484 relative_position = 0,
485 weight = 60,
486 },
487 }
488 },
489 dwg = {
490 matches = {
491 {
492 string = '^AC10[12][2-9]',
493 position = 6,
494 weight = 60,
495 }
496 }
497 },
498 jpg = {
499 matches = {
500 { -- JPEG2000
501 hex = [[0000000c6a5020200d0a870a]],
502 relative_position = 0,
503 weight = 60,
504 },
505 {
506 string = [[^\x{ff}\x{d8}\x{ff}]],
507 weight = 60,
508 position = 3,
509 },
510 },
511 },
512 png = {
513 matches = {
514 {
515 string = [[^\x{89}PNG\x{0d}\x{0a}\x{1a}\x{0a}]],
516 position = 8,
517 weight = 60,
518 },
519 }
520 },
521 gif = {
522 matches = {
523 {
524 string = [[^GIF8\d]],
525 position = 5,
526 weight = 60,
527 },
528 }
529 },
530 bmp = {
531 matches = {
532 {
533 string = [[^BM...\x{00}\x{00}\x{00}\x{00}]],
534 position = 9,
535 weight = 60,
536 },
537 }
538 },
539 heic = {
540 matches = {
541 {
542 -- HEIC/HEIF file format signature
543 -- Starts with ftyp followed by specific brand identifiers
544 string = "^....ftyphe[im][cs]",
545 position = 12,
546 weight = 60,
547 },
548 {
549 -- Alternative signature for HEIC/HEIF
550 string = [[^....ftypmif1]],
551 position = 12,
552 weight = 60,
553 },
554 }
555 },
556 }
557
558 return patterns