| 1 |
package magic |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"debug/macho" |
| 6 |
"encoding/binary" |
| 7 |
"slices" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
func Lnk(raw []byte, _ uint32) bool { |
| 12 |
return bytes.HasPrefix(raw, []byte{0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00}) |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
func Wasm(raw []byte, _ uint32) bool { |
| 17 |
return bytes.HasPrefix(raw, []byte{0x00, 0x61, 0x73, 0x6D}) |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
func Exe(raw []byte, _ uint32) bool { |
| 22 |
return len(raw) > 1 && raw[0] == 0x4D && raw[1] == 0x5A |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func Elf(raw []byte, _ uint32) bool { |
| 27 |
return bytes.HasPrefix(raw, []byte{0x7F, 0x45, 0x4C, 0x46}) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func Nes(raw []byte, _ uint32) bool { |
| 32 |
return bytes.HasPrefix(raw, []byte{0x4E, 0x45, 0x53, 0x1A}) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 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 |
|
| 43 |
func Torrent(raw []byte, _ uint32) bool { |
| 44 |
return bytes.HasPrefix(raw, []byte("d8:announce")) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func Par1(raw []byte, _ uint32) bool { |
| 49 |
return bytes.HasPrefix(raw, []byte{0x50, 0x41, 0x52, 0x31}) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func CBOR(raw []byte, _ uint32) bool { |
| 54 |
return bytes.HasPrefix(raw, []byte{0xD9, 0xD9, 0xF7}) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func classOrMachOFat(in []byte) bool { |
| 60 |
|
| 61 |
|
| 62 |
if len(in) < 8 { |
| 63 |
return false |
| 64 |
} |
| 65 |
|
| 66 |
return binary.BigEndian.Uint32(in) == macho.MagicFat |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func Class(raw []byte, limit uint32) bool { |
| 71 |
return classOrMachOFat(raw) && raw[7] > 30 |
| 72 |
} |
| 73 |
|
| 74 |
|
| 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 |
|
| 94 |
|
| 95 |
func Dbf(raw []byte, limit uint32) bool { |
| 96 |
if len(raw) < 68 { |
| 97 |
return false |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if raw[2] == 0 || raw[2] > 12 || raw[3] == 0 || raw[3] > 31 { |
| 102 |
return false |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if raw[12] != 0x00 || raw[13] != 0x00 || raw[30] != 0x00 || raw[31] != 0x00 { |
| 107 |
return false |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if raw[28] > 0x01 { |
| 113 |
return false |
| 114 |
} |
| 115 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 155 |
func Marc(raw []byte, limit uint32) bool { |
| 156 |
|
| 157 |
if len(raw) < 24 { |
| 158 |
return false |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if !bytes.Equal(raw[20:24], []byte("4500")) { |
| 163 |
return false |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
for i := 0; i < 5; i++ { |
| 168 |
if raw[i] < '0' || raw[i] > '9' { |
| 169 |
return false |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
return bytes.Contains(raw[:min(2048, len(raw))], []byte{0x1E}) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
func TzIf(raw []byte, limit uint32) bool { |
| 210 |
|
| 211 |
if len(raw) < 44 { |
| 212 |
return false |
| 213 |
} |
| 214 |
|
| 215 |
if !bytes.HasPrefix(raw, []byte("TZif")) { |
| 216 |
return false |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if binary.BigEndian.Uint32(raw[36:40]) == 0 { |
| 221 |
return false |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
return raw[4] == 0x00 || raw[4] == 0x32 || raw[4] == 0x33 |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
func Pyc(raw []byte, limit uint32) bool { |
| 231 |
if len(raw) < 8 { |
| 232 |
return false |
| 233 |
} |
| 234 |
|
| 235 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 277 |
|
| 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 |
} |