| 1 |
package magic |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
|
| 7 |
"github.com/gabriel-vasile/mimetype/internal/cdf" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
func Xlsx(raw []byte, limit uint32) bool { |
| 12 |
return msoxml(raw, zipEntries{{ |
| 13 |
name: []byte("xl/"), |
| 14 |
dir: true, |
| 15 |
}}, 100) |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
func Docx(raw []byte, limit uint32) bool { |
| 20 |
return msoxml(raw, zipEntries{{ |
| 21 |
name: []byte("word/"), |
| 22 |
dir: true, |
| 23 |
}}, 100) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func Pptx(raw []byte, limit uint32) bool { |
| 28 |
return msoxml(raw, zipEntries{{ |
| 29 |
name: []byte("ppt/"), |
| 30 |
dir: true, |
| 31 |
}}, 100) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func Visio(raw []byte, limit uint32) bool { |
| 36 |
return msoxml(raw, zipEntries{{ |
| 37 |
name: []byte("visio/"), |
| 38 |
dir: true, |
| 39 |
}}, 100) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func Ole(raw []byte, limit uint32) bool { |
| 46 |
return bytes.HasPrefix(raw, []byte{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func Doc(raw []byte, _ uint32) bool { |
| 52 |
fromParsing := cdf.Detect(raw) |
| 53 |
if fromParsing == cdf.CDFTypeDoc { |
| 54 |
return true |
| 55 |
} |
| 56 |
if fromParsing != cdf.CDFTypeGeneric { |
| 57 |
return false |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
clsids := [][]byte{ |
| 62 |
|
| 63 |
{0x06, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, |
| 64 |
|
| 65 |
{0x00, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, |
| 66 |
|
| 67 |
{0x07, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, |
| 68 |
} |
| 69 |
for _, clsid := range clsids { |
| 70 |
if matchOleClsid(raw, clsid) { |
| 71 |
return true |
| 72 |
} |
| 73 |
} |
| 74 |
return false |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func Ppt(raw []byte, limit uint32) bool { |
| 79 |
fromParsing := cdf.Detect(raw) |
| 80 |
if fromParsing == cdf.CDFTypePpt { |
| 81 |
return true |
| 82 |
} |
| 83 |
if fromParsing != cdf.CDFTypeGeneric { |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if matchOleClsid(raw, []byte{ |
| 90 |
0x10, 0x8d, 0x81, 0x64, 0x9b, 0x4f, 0xcf, 0x11, |
| 91 |
0x86, 0xea, 0x00, 0xaa, 0x00, 0xb9, 0x29, 0xe8, |
| 92 |
}) || matchOleClsid(raw, []byte{ |
| 93 |
0x70, 0xae, 0x7b, 0xea, 0x3b, 0xfb, 0xcd, 0x11, |
| 94 |
0xa9, 0x03, 0x00, 0xaa, 0x00, 0x51, 0x0e, 0xa3, |
| 95 |
}) { |
| 96 |
return true |
| 97 |
} |
| 98 |
|
| 99 |
lin := len(raw) |
| 100 |
if lin < 520 { |
| 101 |
return false |
| 102 |
} |
| 103 |
pptSubHeaders := [][]byte{ |
| 104 |
{0xA0, 0x46, 0x1D, 0xF0}, |
| 105 |
{0x00, 0x6E, 0x1E, 0xF0}, |
| 106 |
{0x0F, 0x00, 0xE8, 0x03}, |
| 107 |
} |
| 108 |
for _, h := range pptSubHeaders { |
| 109 |
if bytes.HasPrefix(raw[512:], h) { |
| 110 |
return true |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)], |
| 115 |
[]byte("P\x00o\x00w\x00e\x00r\x00P\x00o\x00i\x00n\x00t\x00 D\x00o\x00c\x00u\x00m\x00e\x00n\x00t")) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func Xls(raw []byte, limit uint32) bool { |
| 120 |
fromParsing := cdf.Detect(raw) |
| 121 |
if fromParsing == cdf.CDFTypeXls { |
| 122 |
return true |
| 123 |
} |
| 124 |
if fromParsing != cdf.CDFTypeGeneric { |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if matchOleClsid(raw, []byte{ |
| 131 |
0x10, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 132 |
}) || matchOleClsid(raw, []byte{ |
| 133 |
0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 134 |
}) { |
| 135 |
return true |
| 136 |
} |
| 137 |
|
| 138 |
lin := len(raw) |
| 139 |
if lin < 520 { |
| 140 |
return false |
| 141 |
} |
| 142 |
xlsSubHeaders := [][]byte{ |
| 143 |
{0x09, 0x08, 0x10, 0x00, 0x00, 0x06, 0x05, 0x00}, |
| 144 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x10}, |
| 145 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x1F}, |
| 146 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x22}, |
| 147 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x23}, |
| 148 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x28}, |
| 149 |
{0xFD, 0xFF, 0xFF, 0xFF, 0x29}, |
| 150 |
} |
| 151 |
for _, h := range xlsSubHeaders { |
| 152 |
if bytes.HasPrefix(raw[512:], h) { |
| 153 |
return true |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)], |
| 158 |
[]byte("W\x00k\x00s\x00S\x00S\x00W\x00o\x00r\x00k\x00B\x00o\x00o\x00k")) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func Pub(raw []byte, limit uint32) bool { |
| 163 |
return matchOleClsid(raw, []byte{ |
| 164 |
0x01, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 165 |
0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
func Msg(raw []byte, limit uint32) bool { |
| 171 |
fromParsing := cdf.Detect(raw) |
| 172 |
if fromParsing == cdf.CDFTypeMsg { |
| 173 |
return true |
| 174 |
} |
| 175 |
if fromParsing != cdf.CDFTypeGeneric { |
| 176 |
return false |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
return matchOleClsid(raw, []byte{ |
| 181 |
0x0B, 0x0D, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 182 |
0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
func Msi(raw []byte, limit uint32) bool { |
| 189 |
return cdf.Detect(raw) == cdf.CDFTypeInstaller |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func One(raw []byte, limit uint32) bool { |
| 194 |
return bytes.HasPrefix(raw, []byte{ |
| 195 |
0xe4, 0x52, 0x5c, 0x7b, 0x8c, 0xd8, 0xa7, 0x4d, |
| 196 |
0xae, 0xb1, 0x53, 0x78, 0xd0, 0x29, 0x96, 0xd3, |
| 197 |
}) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
func matchOleClsid(in []byte, clsid []byte) bool { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
sectorLength := 512 |
| 208 |
if len(in) < sectorLength { |
| 209 |
return false |
| 210 |
} |
| 211 |
if in[26] == 0x04 && in[27] == 0x00 { |
| 212 |
sectorLength = 4096 |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
firstSecID := int(binary.LittleEndian.Uint32(in[48:52])) |
| 217 |
|
| 218 |
|
| 219 |
clsidOffset := sectorLength*(1+firstSecID) + 80 |
| 220 |
|
| 221 |
|
| 222 |
if len(in) <= clsidOffset+16 || clsidOffset < 0 { |
| 223 |
return false |
| 224 |
} |
| 225 |
|
| 226 |
return bytes.HasPrefix(in[clsidOffset:], clsid) |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
func WPD(raw []byte, _ uint32) bool { |
| 231 |
if len(raw) < 10 { |
| 232 |
return false |
| 233 |
} |
| 234 |
if !bytes.HasPrefix(raw, []byte("\xffWPC")) { |
| 235 |
return false |
| 236 |
} |
| 237 |
return raw[8] == 1 && raw[9] == 10 |
| 238 |
} |