| 1 |
package cdf |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"fmt" |
| 6 |
"hash/fnv" |
| 7 |
"math/bits" |
| 8 |
"math/rand/v2" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
const testSecSize = 512 |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
var testSecSizes = []int{512, 4096} |
| 17 |
|
| 18 |
|
| 19 |
func secP2(secSize int) uint16 { |
| 20 |
return uint16(bits.TrailingZeros(uint(secSize))) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func testHeader(secSize int, firstDirSec, firstSSAT int32, minStdStream uint32, masterSAT []int32) []byte { |
| 26 |
h := make([]byte, 512) |
| 27 |
binary.LittleEndian.PutUint64(h, cdfMagic) |
| 28 |
binary.LittleEndian.PutUint16(h[30:], secP2(secSize)) |
| 29 |
binary.LittleEndian.PutUint16(h[32:], 6) |
| 30 |
binary.LittleEndian.PutUint32(h[48:], uint32(firstDirSec)) |
| 31 |
binary.LittleEndian.PutUint32(h[56:], minStdStream) |
| 32 |
binary.LittleEndian.PutUint32(h[60:], uint32(firstSSAT)) |
| 33 |
binary.LittleEndian.PutUint32(h[68:], uint32(0xFFFFFFFE)) |
| 34 |
binary.LittleEndian.PutUint32(h[72:], 0) |
| 35 |
for i := 0; i < masterSATSize; i++ { |
| 36 |
v := int32(-1) |
| 37 |
if i < len(masterSAT) { |
| 38 |
v = masterSAT[i] |
| 39 |
} |
| 40 |
binary.LittleEndian.PutUint32(h[76+4*i:], uint32(v)) |
| 41 |
} |
| 42 |
return h |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
func idSector(secSize int, ids ...int32) []byte { |
| 48 |
s := make([]byte, secSize) |
| 49 |
for i := 0; i < secSize/4; i++ { |
| 50 |
v := int32(-1) |
| 51 |
if i < len(ids) { |
| 52 |
v = ids[i] |
| 53 |
} |
| 54 |
binary.LittleEndian.PutUint32(s[4*i:], uint32(v)) |
| 55 |
} |
| 56 |
return s |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func padSector(secSize int, b []byte) []byte { |
| 61 |
s := make([]byte, secSize) |
| 62 |
copy(s, b) |
| 63 |
return s |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func dirEntryBytes(name string, typ uint8, first int32, size uint32, uuid []byte) []byte { |
| 69 |
e := make([]byte, dirEntrySize) |
| 70 |
for i := 0; i < len(name); i++ { |
| 71 |
binary.LittleEndian.PutUint16(e[2*i:], uint16(name[i])) |
| 72 |
} |
| 73 |
binary.LittleEndian.PutUint16(e[64:], uint16(2*(len(name)+1))) |
| 74 |
e[66] = typ |
| 75 |
copy(e[80:96], uuid) |
| 76 |
binary.LittleEndian.PutUint32(e[116:], uint32(first)) |
| 77 |
binary.LittleEndian.PutUint32(e[120:], size) |
| 78 |
return e |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func summaryStream(appName string, wide bool) []byte { |
| 85 |
strType, step := uint32(typeStringASCII), 1 |
| 86 |
if wide { |
| 87 |
strType, step = typeStringWide, 2 |
| 88 |
} |
| 89 |
strData := make([]byte, (len(appName)+1)*step) |
| 90 |
for i := 0; i < len(appName); i++ { |
| 91 |
strData[i*step] = appName[i] |
| 92 |
} |
| 93 |
|
| 94 |
section := make([]byte, 24, 24+len(strData)) |
| 95 |
binary.LittleEndian.PutUint32(section[4:], 1) |
| 96 |
binary.LittleEndian.PutUint32(section[8:], propIDNameOfApplication) |
| 97 |
binary.LittleEndian.PutUint32(section[12:], 16) |
| 98 |
binary.LittleEndian.PutUint32(section[16:], strType) |
| 99 |
binary.LittleEndian.PutUint32(section[20:], uint32(len(appName)+1)) |
| 100 |
section = append(section, strData...) |
| 101 |
binary.LittleEndian.PutUint32(section, uint32(len(section))) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
stream := make([]byte, 48+len(section)) |
| 106 |
binary.LittleEndian.PutUint32(stream[44:], 48) |
| 107 |
copy(stream[48:], section) |
| 108 |
return stream |
| 109 |
} |
| 110 |
|
| 111 |
type entrySpec struct { |
| 112 |
name string |
| 113 |
typ uint8 |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func makeCDF(secSize int, rootUUID []byte, summaryName string, summary []byte, extras ...entrySpec) []byte { |
| 129 |
if len(summary) > secSize { |
| 130 |
panic("summary stream larger than one sector") |
| 131 |
} |
| 132 |
dir := dirEntryBytes("Root Entry", dirTypeRootStorage, 2, uint32(secSize), rootUUID) |
| 133 |
if summary != nil { |
| 134 |
dir = append(dir, dirEntryBytes(summaryName, dirTypeUserStream, 3, uint32(len(summary)), nil)...) |
| 135 |
} |
| 136 |
for _, e := range extras { |
| 137 |
dir = append(dir, dirEntryBytes(e.name, e.typ, -2, 0, nil)...) |
| 138 |
} |
| 139 |
if len(dir) > secSize { |
| 140 |
panic("directory larger than one sector") |
| 141 |
} |
| 142 |
|
| 143 |
out := padSector(secSize, testHeader(secSize, 1, -2, 0, []int32{0})) |
| 144 |
out = append(out, idSector(secSize, -3, -2, -2, -2)...) |
| 145 |
out = append(out, padSector(secSize, dir)...) |
| 146 |
out = append(out, make([]byte, secSize)...) |
| 147 |
out = append(out, padSector(secSize, summary)...) |
| 148 |
return out |
| 149 |
} |
| 150 |
|
| 151 |
func TestDetectInvalidInput(t *testing.T) { |
| 152 |
badSectorSize := testHeader(testSecSize, 1, -2, 0, nil) |
| 153 |
binary.LittleEndian.PutUint16(badSectorSize[30:], 21) |
| 154 |
|
| 155 |
tooSmallSector := testHeader(testSecSize, 1, -2, 0, nil) |
| 156 |
binary.LittleEndian.PutUint16(tooSmallSector[30:], 5) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
dirSecOverflow := testHeader(testSecSize, 1<<22-1, -2, 0, nil) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
secSize4K := 4096 |
| 166 |
truncatedSSAT := append( |
| 167 |
padSector(secSize4K, testHeader(secSize4K, -2, 0, 0, []int32{0})), |
| 168 |
0x00, 32 |
| 169 |
) |
| 170 |
|
| 171 |
tests := []struct { |
| 172 |
name string |
| 173 |
data []byte |
| 174 |
}{ |
| 175 |
{"nil", nil}, |
| 176 |
{"short", []byte{0xD0, 0xCF, 0x11, 0xE0}}, |
| 177 |
{"bad magic", make([]byte, 512)}, |
| 178 |
{"bad sector size", badSectorSize}, |
| 179 |
{"sector smaller than dir entry", tooSmallSector}, |
| 180 |
{"dir sector overflows int32", dirSecOverflow}, |
| 181 |
{"truncated sector in SSAT chain loops forever", truncatedSSAT}, |
| 182 |
} |
| 183 |
for _, tt := range tests { |
| 184 |
t.Run(tt.name, func(t *testing.T) { |
| 185 |
if got := Detect(tt.data); got != CDFTypeGeneric { |
| 186 |
t.Errorf("Detect() = %v, want CDFTypeGeneric", got) |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestDetectMSIRootCLSID(t *testing.T) { |
| 193 |
for _, secSize := range testSecSizes { |
| 194 |
data := makeCDF(secSize, msiCLSID, "", nil) |
| 195 |
if got := Detect(data); got != CDFTypeInstaller { |
| 196 |
t.Errorf("Detect(secSize=%d) = %v, want CDFTypeInstaller", secSize, got) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestDetectFromAppName(t *testing.T) { |
| 202 |
tests := []struct { |
| 203 |
app string |
| 204 |
want CDFType |
| 205 |
}{ |
| 206 |
{"Microsoft Office Word", CDFTypeDoc}, |
| 207 |
{"Microsoft Excel", CDFTypeXls}, |
| 208 |
{"Microsoft Powerpoint", CDFTypePpt}, |
| 209 |
{"Advanced Installer 19.0", CDFTypeInstaller}, |
| 210 |
{"InstallShield", CDFTypeInstaller}, |
| 211 |
{"Microsoft Patch Compiler", CDFTypeInstaller}, |
| 212 |
{"NAnt", CDFTypeInstaller}, |
| 213 |
{"Windows Installer XML Toolset", CDFTypeInstaller}, |
| 214 |
{"MICROSOFT WORD", CDFTypeDoc}, |
| 215 |
} |
| 216 |
summaryNames := []string{"\x05SummaryInformation", "\x05DocumentSummaryInformation"} |
| 217 |
for _, secSize := range testSecSizes { |
| 218 |
for _, summaryName := range summaryNames { |
| 219 |
for _, wide := range []bool{false, true} { |
| 220 |
for _, tt := range tests { |
| 221 |
t.Run(tt.app, func(t *testing.T) { |
| 222 |
data := makeCDF(secSize, nil, summaryName, summaryStream(tt.app, wide)) |
| 223 |
if got := Detect(data); got != tt.want { |
| 224 |
t.Errorf("Detect(secSize=%d, summary=%q, app=%q, wide=%v) = %v, want %v", |
| 225 |
secSize, summaryName, tt.app, wide, got, tt.want) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
func TestDetectFromSiblingNames(t *testing.T) { |
| 238 |
tests := []struct { |
| 239 |
sibling string |
| 240 |
want CDFType |
| 241 |
}{ |
| 242 |
{"Book", CDFTypeXls}, |
| 243 |
{"Workbook", CDFTypeXls}, |
| 244 |
{"WordDocument", CDFTypeDoc}, |
| 245 |
{"PowerPoint Document", CDFTypePpt}, |
| 246 |
{"\x05DigitalSignature", CDFTypeInstaller}, |
| 247 |
|
| 248 |
|
| 249 |
{"Contents", CDFTypeGeneric}, |
| 250 |
} |
| 251 |
for _, secSize := range testSecSizes { |
| 252 |
for _, tt := range tests { |
| 253 |
t.Run(tt.sibling, func(t *testing.T) { |
| 254 |
summary := summaryStream("Unknown Application", false) |
| 255 |
data := makeCDF(secSize, nil, "\x05SummaryInformation", summary, entrySpec{tt.sibling, dirTypeUserStream}) |
| 256 |
if got := Detect(data); got != tt.want { |
| 257 |
t.Errorf("Detect(secSize=%d) = %v, want %v", secSize, got, tt.want) |
| 258 |
} |
| 259 |
}) |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
func TestDetectFromSectionNames(t *testing.T) { |
| 267 |
tests := []struct { |
| 268 |
entry entrySpec |
| 269 |
want CDFType |
| 270 |
}{ |
| 271 |
{entrySpec{"EncryptedPackage", dirTypeUserStream}, CDFTypeGeneric}, |
| 272 |
{entrySpec{"EncryptedSummary", dirTypeUserStream}, CDFTypeGeneric}, |
| 273 |
{entrySpec{"Book", dirTypeUserStream}, CDFTypeXls}, |
| 274 |
{entrySpec{"Workbook", dirTypeUserStream}, CDFTypeXls}, |
| 275 |
{entrySpec{"WordDocument", dirTypeUserStream}, CDFTypeDoc}, |
| 276 |
{entrySpec{"PowerPoint Document", dirTypeUserStream}, CDFTypePpt}, |
| 277 |
{entrySpec{"__properties_version1.0", dirTypeUserStream}, CDFTypeMsg}, |
| 278 |
{entrySpec{"__recip_version1.0_#00000000", dirTypeUserStorage}, CDFTypeMsg}, |
| 279 |
|
| 280 |
{entrySpec{"Contents", dirTypeUserStream}, CDFTypeGeneric}, |
| 281 |
} |
| 282 |
for _, secSize := range testSecSizes { |
| 283 |
for _, tt := range tests { |
| 284 |
t.Run(tt.entry.name, func(t *testing.T) { |
| 285 |
data := makeCDF(secSize, nil, "", nil, tt.entry) |
| 286 |
if got := Detect(data); got != tt.want { |
| 287 |
t.Errorf("Detect(secSize=%d) = %v, want %v", secSize, got, tt.want) |
| 288 |
} |
| 289 |
}) |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
func TestDetectMasterSATExtension(t *testing.T) { |
| 299 |
const ( |
| 300 |
msatSec = 109 |
| 301 |
extSATSec = 110 |
| 302 |
dirSec = 13952 |
| 303 |
rootSec = 13953 |
| 304 |
summarySec = 13954 |
| 305 |
nSectors = summarySec + 1 |
| 306 |
) |
| 307 |
summary := summaryStream("Microsoft Excel", false) |
| 308 |
|
| 309 |
data := make([]byte, (1+nSectors)*testSecSize) |
| 310 |
sector := func(id int32) []byte { |
| 311 |
off := (1 + int(id)) * testSecSize |
| 312 |
return data[off : off+testSecSize] |
| 313 |
} |
| 314 |
|
| 315 |
masterSAT := make([]int32, masterSATSize) |
| 316 |
for i := range masterSAT { |
| 317 |
masterSAT[i] = int32(i) |
| 318 |
} |
| 319 |
copy(data, testHeader(testSecSize, dirSec, -2, 0, masterSAT)) |
| 320 |
binary.LittleEndian.PutUint32(data[68:], msatSec) |
| 321 |
binary.LittleEndian.PutUint32(data[72:], 1) |
| 322 |
|
| 323 |
|
| 324 |
for i := int32(0); i < masterSATSize; i++ { |
| 325 |
copy(sector(i), idSector(testSecSize)) |
| 326 |
} |
| 327 |
satIDs := make([]int32, testSecSize/4) |
| 328 |
for i := range satIDs { |
| 329 |
satIDs[i] = -1 |
| 330 |
} |
| 331 |
for i := 0; i <= extSATSec; i++ { |
| 332 |
satIDs[i] = -3 |
| 333 |
} |
| 334 |
copy(sector(0), idSector(testSecSize, satIDs...)) |
| 335 |
|
| 336 |
|
| 337 |
copy(sector(msatSec), idSector(testSecSize, extSATSec)) |
| 338 |
|
| 339 |
|
| 340 |
copy(sector(extSATSec), idSector(testSecSize, -2, -2, -2)) |
| 341 |
|
| 342 |
dir := dirEntryBytes("Root Entry", dirTypeRootStorage, rootSec, testSecSize, nil) |
| 343 |
dir = append(dir, dirEntryBytes("\x05SummaryInformation", dirTypeUserStream, summarySec, uint32(len(summary)), nil)...) |
| 344 |
copy(sector(dirSec), dir) |
| 345 |
copy(sector(summarySec), summary) |
| 346 |
|
| 347 |
if got := Detect(data); got != CDFTypeXls { |
| 348 |
t.Errorf("Detect() = %v, want CDFTypeXls", got) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
func TestDetectShortStreamSummary(t *testing.T) { |
| 355 |
summary := summaryStream("Microsoft Office Word", false) |
| 356 |
if len(summary) <= 64 || len(summary) > testSecSize { |
| 357 |
t.Fatalf("summary len %d, want >64 and <=%d to span short sectors", len(summary), testSecSize) |
| 358 |
} |
| 359 |
|
| 360 |
dir := dirEntryBytes("Root Entry", dirTypeRootStorage, 2, testSecSize, nil) |
| 361 |
|
| 362 |
dir = append(dir, dirEntryBytes("\x05SummaryInformation", dirTypeUserStream, 0, uint32(len(summary)), nil)...) |
| 363 |
|
| 364 |
|
| 365 |
data := testHeader(testSecSize, 1, 3, 4096, []int32{0}) |
| 366 |
data = append(data, idSector(testSecSize, -3, -2, -2, -2)...) |
| 367 |
data = append(data, padSector(testSecSize, dir)...) |
| 368 |
data = append(data, padSector(testSecSize, summary)...) |
| 369 |
nShort := int32((len(summary) + 63) / 64) |
| 370 |
ssat := make([]int32, nShort) |
| 371 |
for i := int32(0); i < nShort-1; i++ { |
| 372 |
ssat[i] = i + 1 |
| 373 |
} |
| 374 |
ssat[nShort-1] = -2 |
| 375 |
data = append(data, idSector(testSecSize, ssat...)...) |
| 376 |
|
| 377 |
if got := Detect(data); got != CDFTypeDoc { |
| 378 |
t.Errorf("Detect() = %v, want CDFTypeDoc", got) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
func TestDetectShortStreamMultiSectorSSAT(t *testing.T) { |
| 389 |
const ( |
| 390 |
shortSecSz = 64 |
| 391 |
perSec = testSecSize / 4 |
| 392 |
sstSectors = 17 |
| 393 |
firstSST = int32(2) |
| 394 |
lastSST = firstSST + sstSectors - 1 |
| 395 |
ssatSecA = lastSST + 1 |
| 396 |
ssatSecB = ssatSecA + 1 |
| 397 |
sidB0 = int32(perSec) |
| 398 |
) |
| 399 |
summary := summaryStream("Microsoft Office Word", false) |
| 400 |
if len(summary) > 2*shortSecSz { |
| 401 |
t.Fatalf("summary len %d, want <= %d so it fits in 2 short sectors", len(summary), 2*shortSecSz) |
| 402 |
} |
| 403 |
|
| 404 |
dir := dirEntryBytes("Root Entry", dirTypeRootStorage, firstSST, uint32(sstSectors*testSecSize), nil) |
| 405 |
dir = append(dir, dirEntryBytes("\x05SummaryInformation", dirTypeUserStream, sidB0, uint32(len(summary)), nil)...) |
| 406 |
|
| 407 |
|
| 408 |
sat := make([]int32, perSec) |
| 409 |
for i := range sat { |
| 410 |
sat[i] = -1 |
| 411 |
} |
| 412 |
sat[0] = -3 |
| 413 |
sat[1] = -2 |
| 414 |
for s := firstSST; s < lastSST; s++ { |
| 415 |
sat[s] = s + 1 |
| 416 |
} |
| 417 |
sat[lastSST] = -2 |
| 418 |
sat[ssatSecA] = ssatSecB |
| 419 |
sat[ssatSecB] = -2 |
| 420 |
|
| 421 |
data := testHeader(testSecSize, 1, ssatSecA, 4096, []int32{0}) |
| 422 |
data = append(data, idSector(testSecSize, sat...)...) |
| 423 |
data = append(data, padSector(testSecSize, dir)...) |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
sst := make([]byte, sstSectors*testSecSize) |
| 428 |
copy(sst[int(sidB0)*shortSecSz:], summary) |
| 429 |
data = append(data, sst...) |
| 430 |
|
| 431 |
|
| 432 |
data = append(data, idSector(testSecSize)...) |
| 433 |
|
| 434 |
rect |
| 435 |
|
| 436 |
nShort := int32((len(summary) + shortSecSz - 1) / shortSecSz) |
| 437 |
ssatB := make([]int32, perSec) |
| 438 |
for i := range ssatB { |
| 439 |
ssatB[i] = -1 |
| 440 |
} |
| 441 |
for i := int32(0); i < nShort-1; i++ { |
| 442 |
ssatB[i] = sidB0 + i + 1 |
| 443 |
} |
| 444 |
ssatB[nShort-1] = -2 |
| 445 |
data = append(data, idSector(testSecSize, ssatB...)...) |
| 446 |
|
| 447 |
if got := Detect(data); got != CDFTypeDoc { |
| 448 |
t.Errorf("Detect() = %v, want CDFTypeDoc (short stream in 2nd SSAT sector)", got) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
r is |
| 461 |
|
| 462 |
|
| 463 |
func makeFragmentedDirCDF(secSize int) []byte { |
| 464 |
dir1 := dirEntryBytes("Root Entry", dirTypeRootStorage, -1, 0, nil) |
| 465 |
dir2 := dirEntryBytes("WordDocument", dirTypeUserStream, -1, 0, nil) |
| 466 |
|
| 467 |
data := padSector(secSize, testHeader(secSize, 1, -1, 4096, []int32{0})) |
| 468 |
data = append(data, idSector(secSize, -3, 3, -1, -2)...) |
| 469 |
data = append(data, padSector(secSize, dir1)...) |
| 470 |
data = append(data, padSector(secSize, nil)...) |
| 471 |
data = append(data, padSector(secSize, dir2)...) |
| 472 |
return data |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
ut |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
func TestDetectFragmentedDirChain(t *testing.T) { |
| 482 |
for _, secSize := range testSecSizes { |
| 483 |
t.Run(fmt.Sprintf("sec%d", secSize), func(t *testing.T) { |
| 484 |
data := makeFragmentedDirCDF(secSize) |
| 485 |
if got := Detect(data); got != CDFTypeDoc { |
| 486 |
t.Errorf("Detect() = %v, want CDFTypeDoc (fragmented dir chain)", got) |
| 487 |
} |
| 488 |
}) |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func put16(b []byte, off int, v uint16) { binary.LittleEndian.PutUint16(b[off:], v) } |
| 493 |
func put32(b []byte, off int, v uint32) { binary.LittleEndian.PutUint32(b[off:], v) } |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
func TestBuildSATTruncatedMSATNoPanic(t *testing.T) { |
| 501 |
for _, tc := range []struct { |
| 502 |
name string |
| 503 |
secP2 uint16 |
| 504 |
secSize int |
| 505 |
}{ |
| 506 |
{"sector 512", 9, 512}, |
| 507 |
{"sector 4096", 12, 4096}, |
| 508 |
} { |
| 509 |
t.Run(tc.name, func(t *testing.T) { |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
data := make([]byte, tc.secSize+4) |
| 514 |
binary.LittleEndian.PutUint64(data, cdfMagic) |
| 515 |
put16(data, 30, tc.secP2) |
| 516 |
put16(data, 32, 6) |
| 517 |
put32(data, 48, 0xFFFFFFFF) |
| 518 |
put32(data, 56, 4096) |
| 519 |
put32(data, 60, 0xFFFFFFFF) |
| 520 |
put32(data, 68, 0) |
| 521 |
put32(data, 72, 1) |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
put32(data, 76, 100) |
| 526 |
for i := 1; i < masterSATSize; i++ { |
| 527 |
put32(data, 76+4*i, 0xFFFFFFFF) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
put32(data, tc.secSize, 0) |
| 532 |
|
| 533 |
if cdf := Detect(data); cdf != CDFTypeGeneric { |
| 534 |
t.Errorf("expected: %q, got: %q", CDFTypeGeneric, cdf) |
| 535 |
} |
| 536 |
}) |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
func TestCyclicChainBounded(t *testing.T) { |
| 547 |
const secP2 = 20 |
| 548 |
secSize := 1 << secP2 |
| 549 |
|
| 550 |
data := make([]byte, 3*secSize) |
| 551 |
binary.LittleEndian.PutUint64(data, cdfMagic) |
| 552 |
put16(data, 30, secP2) |
| 553 |
put16(data, 32, 6) |
| 554 |
put32(data, 48, 1) |
| 555 |
put32(data, 56, 4096) |
| 556 |
put32(data, 60, 0xFFFFFFFF) |
| 557 |
put32(data, 68, 0xFFFFFFFF) |
| 558 |
put32(data, 72, 0) |
| 559 |
|
| 560 |
put32(data, 76, 0) |
| 561 |
for i := 1; i < masterSATSize; i++ { |
| 562 |
put32(data, 76+4*i, 0xFFFFFFFF) |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
put32(data, secSize+4, 1) |
| 567 |
|
| 568 |
if cdf := Detect(data); cdf != CDFTypeGeneric { |
| 569 |
t.Errorf("expected: %q, got: %q", CDFTypeGeneric, cdf) |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
func TestSummaryAppNameNoPanic(t *testing.T) { |
| 579 |
for _, tc := range []struct { |
| 580 |
name string |
| 581 |
stream func() []byte |
| 582 |
want string |
| 583 |
}{{ |
| 584 |
|
| 585 |
name: "valid app name", |
| 586 |
stream: func() []byte { |
| 587 |
stream := make([]byte, 80) |
| 588 |
put32(stream, sectionDeclOffset+16, 48) |
| 589 |
sec := stream[48:] |
| 590 |
put32(sec, 0, 29) |
| 591 |
put32(sec, 4, 1) |
| 592 |
put32(sec, 8, propIDNameOfApplication) |
| 593 |
put32(sec, 12, 16) |
| 594 |
put32(sec, 16, typeStringASCII) |
| 595 |
put32(sec, 20, 5) |
| 596 |
copy(sec[24:], "Word\x00") |
| 597 |
return stream |
| 598 |
}, |
| 599 |
want: "Word", |
| 600 |
}, { |
| 601 |
|
| 602 |
|
| 603 |
name: "offset overflow", |
| 604 |
stream: func() []byte { |
| 605 |
stream := make([]byte, 64) |
| 606 |
put32(stream, sectionDeclOffset+16, 48) |
| 607 |
sec := stream[48:] |
| 608 |
put32(sec, 0, 16) |
| 609 |
put32(sec, 4, 1) |
| 610 |
put32(sec, 8, propIDNameOfApplication) |
| 611 |
put32(sec, 12, 0xFFFFFFFF) |
| 612 |
return stream |
| 613 |
}, |
| 614 |
want: "", |
| 615 |
}, { |
| 616 |
|
| 617 |
|
| 618 |
name: "length overflow", |
| 619 |
stream: func() []byte { |
| 620 |
stream := make([]byte, 80) |
| 621 |
put32(stream, sectionDeclOffset+16, 48) |
| 622 |
sec := stream[48:] |
| 623 |
put32(sec, 0, 32) |
| 624 |
put32(sec, 4, 1) |
| 625 |
put32(sec, 8, propIDNameOfApplication) |
| 626 |
put32(sec, 12, 16) |
| 627 |
put32(sec, 16, typeStringWide) |
| 628 |
put32(sec, 20, 0xFFFFFFFF) |
| 629 |
return stream |
| 630 |
}, |
| 631 |
want: "", |
| 632 |
}, { |
| 633 |
|
| 634 |
|
| 635 |
name: "sdOff negative on 386", |
| 636 |
stream: func() []byte { |
| 637 |
stream := make([]byte, 64) |
| 638 |
put32(stream, sectionDeclOffset+16, 0x80000000) |
| 639 |
return stream |
| 640 |
}, |
| 641 |
want: "", |
| 642 |
}} { |
| 643 |
t.Run(tc.name, func(t *testing.T) { |
| 644 |
if got := string(summaryAppName(tc.stream())); got != tc.want { |
| 645 |
t.Errorf("expected: %q, got: %q", tc.want, got) |
| 646 |
} |
| 647 |
}) |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
func BenchmarkDetect(b *testing.B) { |
| 652 |
cases := []struct { |
| 653 |
name string |
| 654 |
build func(secSize int) []byte |
| 655 |
}{{ |
| 656 |
name: "MSIRootCLSID", |
| 657 |
build: func(secSize int) []byte { |
| 658 |
return makeCDF(secSize, msiCLSID, "", nil) |
| 659 |
}, |
| 660 |
}, { |
| 661 |
name: "AppNameASCII", |
| 662 |
build: func(secSize int) []byte { |
| 663 |
return makeCDF(secSize, nil, "\x05SummaryInformation", |
| 664 |
summaryStream("Microsoft Office Word", false)) |
| 665 |
}, |
| 666 |
}, { |
| 667 |
name: "AppNameWide", |
| 668 |
build: func(secSize int) []byte { |
| 669 |
return makeCDF(secSize, nil, "\x05SummaryInformation", |
| 670 |
summaryStream("Microsoft Excel", true)) |
| 671 |
}, |
| 672 |
}, { |
| 673 |
name: "SiblingName", |
| 674 |
build: func(secSize int) []byte { |
| 675 |
return makeCDF(secSize, nil, "\x05SummaryInformation", |
| 676 |
summaryStream("Unknown Application", false), |
| 677 |
entrySpec{"WordDocument", dirTypeUserStream}) |
| 678 |
}, |
| 679 |
}, { |
| 680 |
name: "SectionName", |
| 681 |
build: func(secSize int) []byte { |
| 682 |
return makeCDF(secSize, nil, "", nil, |
| 683 |
entrySpec{"EncryptedPackage", dirTypeUserStream}) |
| 684 |
}, |
| 685 |
}, { |
| 686 |
name: "Generic", |
| 687 |
build: func(secSize int) []byte { |
| 688 |
return makeCDF(secSize, nil, "", nil, |
| 689 |
entrySpec{"Contents", dirTypeUserStream}) |
| 690 |
}, |
| 691 |
}, { |
| 692 |
name: "ShortStreamSummary", |
| 693 |
build: func(secSize int) []byte { |
| 694 |
summary := summaryStream("Microsoft Office Word", false) |
| 695 |
dir := dirEntryBytes("Root Entry", dirTypeRootStorage, 2, uint32(secSize), nil) |
| 696 |
dir = append(dir, dirEntryBytes("\x05SummaryInformation", dirTypeUserStream, 0, uint32(len(summary)), nil)...) |
| 697 |
|
| 698 |
data := testHeader(secSize, 1, 3, 4096, []int32{0}) |
| 699 |
data = append(data, idSector(secSize, -3, -2, -2, -2)...) |
| 700 |
data = append(data, padSector(secSize, dir)...) |
| 701 |
data = append(data, padSector(secSize, summary)...) |
| 702 |
nShort := int32((len(summary) + 63) / 64) |
| 703 |
ssat := make([]int32, nShort) |
| 704 |
for i := int32(0); i < nShort-1; i++ { |
| 705 |
ssat[i] = i + 1 |
| 706 |
} |
| 707 |
ssat[nShort-1] = -2 |
| 708 |
data = append(data, idSector(secSize, ssat...)...) |
| 709 |
return data |
| 710 |
}, |
| 711 |
}, { |
| 712 |
name: "FragmentedDir", |
| 713 |
build: makeFragmentedDirCDF, |
| 714 |
}} |
| 715 |
|
| 716 |
for _, secSize := range testSecSizes { |
| 717 |
for _, bc := range cases { |
| 718 |
data := bc.build(secSize) |
| 719 |
b.Run(fmt.Sprintf("%s/sec%d", bc.name, secSize), func(b *testing.B) { |
| 720 |
b.SetBytes(int64(len(data))) |
| 721 |
b.ReportAllocs() |
| 722 |
for b.Loop() { |
| 723 |
Detect(data) |
| 724 |
} |
| 725 |
}) |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
func FuzzDetect(f *testing.F) { |
| 731 |
for _, secSize := range testSecSizes { |
| 732 |
|
| 733 |
f.Add(makeCDF(secSize, msiCLSID, "", nil)) |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
for _, wide := range []bool{false, true} { |
| 738 |
f.Add(makeCDF(secSize, nil, "\x05SummaryInformation", |
| 739 |
summaryStream("Microsoft Office Word", wide))) |
| 740 |
f.Add(makeCDF(secSize, nil, "\x05DocumentSummaryInformation", |
| 741 |
summaryStream("Microsoft Excel", wide))) |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
f.Add(makeCDF(secSize, nil, "\x05SummaryInformation", |
| 746 |
summaryStream("Unknown Application", false), |
| 747 |
entrySpec{"WordDocument", dirTypeUserStream})) |
| 748 |
|
| 749 |
|
| 750 |
f.Add(makeCDF(secSize, nil, "\x05SummaryInformation", |
| 751 |
summaryStream("Unknown Application", false), |
| 752 |
entrySpec{"Contents", dirTypeUserStream})) |
| 753 |
|
| 754 |
|
| 755 |
f.Add(makeCDF(secSize, nil, "", nil, entrySpec{"EncryptedPackage", dirTypeUserStream})) |
| 756 |
f.Add(makeCDF(secSize, nil, "", nil, entrySpec{"Workbook", dirTypeUserStream})) |
| 757 |
f.Add(makeCDF(secSize, nil, "", nil, entrySpec{"PowerPoint Document", dirTypeUserStream})) |
| 758 |
f.Add(makeCDF(secSize, nil, "", nil, entrySpec{"__properties_version1.0", dirTypeUserStream})) |
| 759 |
|
| 760 |
|
| 761 |
f.Add(makeCDF(secSize, nil, "", nil, entrySpec{"Contents", dirTypeUserStream})) |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
f.Add(testHeader(512, 1, -2, 0, []int32{0})) |
| 766 |
|
| 767 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 768 |
if len(data) == 0 { |
| 769 |
return |
| 770 |
} |
| 771 |
|
| 772 |
h := fnv.New64a() |
| 773 |
h.Write(data) |
| 774 |
rng := rand.New(rand.NewPCG(h.Sum64(), 0)) |
| 775 |
|
| 776 |
|
| 777 |
for i := 0; i < 100; i++ { |
| 778 |
j := rng.IntN(len(data)) |
| 779 |
_ = Detect(data[:j]) |
| 780 |
} |
| 781 |
}) |
| 782 |
} |