| 1 |
// Copyright 2020 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
package attest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/binary" |
| 20 |
"encoding/hex" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"strings" |
| 25 |
"unicode/utf16" |
| 26 |
|
| 27 |
"github.com/google/go-attestation/attest/internal" |
| 28 |
) |
| 29 |
|
| 30 |
type windowsEvent uint32 |
| 31 |
|
| 32 |
// SIPA event types |
| 33 |
const ( |
| 34 |
sipaTypeMask windowsEvent = 0x000f0000 |
| 35 |
sipaContainer windowsEvent = 0x00010000 |
| 36 |
sipaInformation windowsEvent = 0x00020000 |
| 37 |
sipaError windowsEvent = 0x00030000 |
| 38 |
sipaPreOsParameter windowsEvent = 0x00040000 |
| 39 |
sipaOSParameter windowsEvent = 0x00050000 |
| 40 |
sipaAuthority windowsEvent = 0x00060000 |
| 41 |
sipaLoadedModule windowsEvent = 0x00070000 |
| 42 |
sipaTrustPoint windowsEvent = 0x00080000 |
| 43 |
sipaELAM windowsEvent = 0x00090000 |
| 44 |
sipaVBS windowsEvent = 0x000a0000 |
| 45 |
trustBoundary windowsEvent = 0x40010001 |
| 46 |
elamAggregation windowsEvent = 0x40010002 |
| 47 |
loadedModuleAggregation windowsEvent = 0x40010003 |
| 48 |
trustpointAggregation windowsEvent = 0xC0010004 |
| 49 |
ksrAggregation windowsEvent = 0x40010005 |
| 50 |
ksrSignedMeasurementAggregation windowsEvent = 0x40010006 |
| 51 |
information windowsEvent = 0x00020001 |
| 52 |
bootCounter windowsEvent = 0x00020002 |
| 53 |
transferControl windowsEvent = 0x00020003 |
| 54 |
applicationReturn windowsEvent = 0x00020004 |
| 55 |
bitlockerUnlock windowsEvent = 0x00020005 |
| 56 |
eventCounter windowsEvent = 0x00020006 |
| 57 |
counterID windowsEvent = 0x00020007 |
| 58 |
morBitNotCancelable windowsEvent = 0x00020008 |
| 59 |
applicationSVN windowsEvent = 0x00020009 |
| 60 |
svnChainStatus windowsEvent = 0x0002000A |
| 61 |
morBitAPIStatus windowsEvent = 0x0002000B |
| 62 |
bootDebugging windowsEvent = 0x00040001 |
| 63 |
bootRevocationList windowsEvent = 0x00040002 |
| 64 |
osKernelDebug windowsEvent = 0x00050001 |
| 65 |
codeIntegrity windowsEvent = 0x00050002 |
| 66 |
testSigning windowsEvent = 0x00050003 |
| 67 |
dataExecutionPrevention windowsEvent = 0x00050004 |
| 68 |
safeMode windowsEvent = 0x00050005 |
| 69 |
winPE windowsEvent = 0x00050006 |
| 70 |
physicalAddressExtension windowsEvent = 0x00050007 |
| 71 |
osDevice windowsEvent = 0x00050008 |
| 72 |
systemRoot windowsEvent = 0x00050009 |
| 73 |
hypervisorLaunchType windowsEvent = 0x0005000A |
| 74 |
hypervisorPath windowsEvent = 0x0005000B |
| 75 |
hypervisorIOMMUPolicy windowsEvent = 0x0005000C |
| 76 |
hypervisorDebug windowsEvent = 0x0005000D |
| 77 |
driverLoadPolicy windowsEvent = 0x0005000E |
| 78 |
siPolicy windowsEvent = 0x0005000F |
| 79 |
hypervisorMMIONXPolicy windowsEvent = 0x00050010 |
| 80 |
hypervisorMSRFilterPolicy windowsEvent = 0x00050011 |
| 81 |
vsmLaunchType windowsEvent = 0x00050012 |
| 82 |
osRevocationList windowsEvent = 0x00050013 |
| 83 |
vsmIDKInfo windowsEvent = 0x00050020 |
| 84 |
flightSigning windowsEvent = 0x00050021 |
| 85 |
pagefileEncryptionEnabled windowsEvent = 0x00050022 |
| 86 |
vsmIDKSInfo windowsEvent = 0x00050023 |
| 87 |
hibernationDisabled windowsEvent = 0x00050024 |
| 88 |
dumpsDisabled windowsEvent = 0x00050025 |
| 89 |
dumpEncryptionEnabled windowsEvent = 0x00050026 |
| 90 |
dumpEncryptionKeyDigest windowsEvent = 0x00050027 |
| 91 |
lsaISOConfig windowsEvent = 0x00050028 |
| 92 |
noAuthority windowsEvent = 0x00060001 |
| 93 |
authorityPubKey windowsEvent = 0x00060002 |
| 94 |
filePath windowsEvent = 0x00070001 |
| 95 |
imageSize windowsEvent = 0x00070002 |
| 96 |
hashAlgorithmID windowsEvent = 0x00070003 |
| 97 |
authenticodeHash windowsEvent = 0x00070004 |
| 98 |
authorityIssuer windowsEvent = 0x00070005 |
| 99 |
authoritySerial windowsEvent = 0x00070006 |
| 100 |
imageBase windowsEvent = 0x00070007 |
| 101 |
authorityPublisher windowsEvent = 0x00070008 |
| 102 |
authoritySHA1Thumbprint windowsEvent = 0x00070009 |
| 103 |
imageValidated windowsEvent = 0x0007000A |
| 104 |
moduleSVN windowsEvent = 0x0007000B |
| 105 |
quote windowsEvent = 0x80080001 |
| 106 |
quoteSignature windowsEvent = 0x80080002 |
| 107 |
aikID windowsEvent = 0x80080003 |
| 108 |
aikPubDigest windowsEvent = 0x80080004 |
| 109 |
elamKeyname windowsEvent = 0x00090001 |
| 110 |
elamConfiguration windowsEvent = 0x00090002 |
| 111 |
elamPolicy windowsEvent = 0x00090003 |
| 112 |
elamMeasured windowsEvent = 0x00090004 |
| 113 |
vbsVSMRequired windowsEvent = 0x000A0001 |
| 114 |
vbsSecurebootRequired windowsEvent = 0x000A0002 |
| 115 |
vbsIOMMURequired windowsEvent = 0x000A0003 |
| 116 |
vbsNXRequired windowsEvent = 0x000A0004 |
| 117 |
vbsMSRFilteringRequired windowsEvent = 0x000A0005 |
| 118 |
vbsMandatoryEnforcement windowsEvent = 0x000A0006 |
| 119 |
vbsHVCIPolicy windowsEvent = 0x000A0007 |
| 120 |
vbsMicrosoftBootChainRequired windowsEvent = 0x000A0008 |
| 121 |
ksrSignature windowsEvent = 0x000B0001 |
| 122 |
) |
| 123 |
|
| 124 |
// WinCSPAlg describes the hash algorithm used by a Windows CSP. |
| 125 |
type WinCSPAlg uint32 |
| 126 |
|
| 127 |
// Valid CSP Algorithm IDs. |
| 128 |
const ( |
| 129 |
WinAlgMD4 WinCSPAlg = 0x02 |
| 130 |
WinAlgMD5 WinCSPAlg = 0x03 |
| 131 |
WinAlgSHA1 WinCSPAlg = 0x04 |
| 132 |
WinAlgSHA256 WinCSPAlg = 0x0c |
| 133 |
WinAlgSHA384 WinCSPAlg = 0x0d |
| 134 |
WinAlgSHA512 WinCSPAlg = 0x0e |
| 135 |
) |
| 136 |
|
| 137 |
// BitlockerStatus describes the status of BitLocker on a Windows system. |
| 138 |
type BitlockerStatus uint8 |
| 139 |
|
| 140 |
// Valid BitlockerStatus values. |
| 141 |
const ( |
| 142 |
BitlockerStatusCached = 0x01 |
| 143 |
BitlockerStatusMedia = 0x02 |
| 144 |
BitlockerStatusTPM = 0x04 |
| 145 |
BitlockerStatusPin = 0x10 |
| 146 |
BitlockerStatusExternal = 0x20 |
| 147 |
BitlockerStatusRecovery = 0x40 |
| 148 |
) |
| 149 |
|
| 150 |
// Ternary describes a boolean value that can additionally be unknown. |
| 151 |
type Ternary uint8 |
| 152 |
|
| 153 |
// Valid Ternary values. |
| 154 |
const ( |
| 155 |
TernaryUnknown Ternary = iota |
| 156 |
TernaryTrue |
| 157 |
TernaryFalse |
| 158 |
) |
| 159 |
|
| 160 |
// WinEvents describes information from the event log recorded during |
| 161 |
// bootup of Microsoft Windows. |
| 162 |
type WinEvents struct { |
| 163 |
// ColdBoot is set to true if the system was not resuming from hibernation. |
| 164 |
ColdBoot bool |
| 165 |
// BootCount contains the value of the monotonic boot counter. This |
| 166 |
// value is not set for TPM 1.2 devices and some TPMs with buggy |
| 167 |
// implementations of monotonic counters. |
| 168 |
BootCount uint64 |
| 169 |
// LoadedModules contains authenticode hashes for binaries which |
| 170 |
// were loaded during boot. |
| 171 |
LoadedModules map[string]WinModuleLoad |
| 172 |
// ELAM describes the configuration of each Early Launch AntiMalware driver, |
| 173 |
// for each AV Vendor key. |
| 174 |
ELAM map[string]WinELAM |
| 175 |
// BootDebuggingEnabled is true if boot debugging was ever reported |
| 176 |
// as enabled. |
| 177 |
BootDebuggingEnabled bool |
| 178 |
// KernelDebugEnabled is true if kernel debugging was recorded as |
| 179 |
// enabled at any point during boot. |
| 180 |
KernelDebugEnabled bool |
| 181 |
// DEPEnabled is true if NX (Data Execution Prevention) was consistently |
| 182 |
// reported as enabled. |
| 183 |
DEPEnabled Ternary |
| 184 |
// CodeIntegrityEnabled is true if code integrity was consistently |
| 185 |
// reported as enabled. |
| 186 |
CodeIntegrityEnabled Ternary |
| 187 |
// TestSigningEnabled is true if test-mode signature verification was |
| 188 |
// ever reported as enabled. |
| 189 |
TestSigningEnabled bool |
| 190 |
// BitlockerUnlocks reports the bitlocker status for every instance of |
| 191 |
// a disk unlock, where bitlocker was used to secure the disk. |
| 192 |
BitlockerUnlocks []BitlockerStatus |
| 193 |
} |
| 194 |
|
| 195 |
// WinModuleLoad describes a module which was loaded while |
| 196 |
// Windows booted. |
| 197 |
type WinModuleLoad struct { |
| 198 |
// FilePath represents the path from which the module was loaded. This |
| 199 |
// information is not always present. |
| 200 |
FilePath string |
| 201 |
// AuthenticodeHash contains the authenticode hash of the binary |
| 202 |
// blob which was loaded. |
| 203 |
AuthenticodeHash []byte |
| 204 |
// ImageBase describes all the addresses to which the the blob was loaded. |
| 205 |
ImageBase []uint64 |
| 206 |
// ImageSize describes the size of the image in bytes. This information |
| 207 |
// is not always present. |
| 208 |
ImageSize uint64 |
| 209 |
// HashAlgorithm describes the hash algorithm used. |
| 210 |
HashAlgorithm WinCSPAlg |
| 211 |
// ImageValidated is set if the post-boot loader validated the image. |
| 212 |
ImageValidated bool |
| 213 |
|
| 214 |
// AuthorityIssuer identifies the issuer of the certificate which certifies |
| 215 |
// the signature on this module. |
| 216 |
AuthorityIssuer string |
| 217 |
// AuthorityPublisher identifies the publisher of the certificate which |
| 218 |
// certifies the signature on this module. |
| 219 |
AuthorityPublisher string |
| 220 |
// AuthoritySerial contains the serial of the certificate certifying this |
| 221 |
// module. |
| 222 |
AuthoritySerial []byte |
| 223 |
// AuthoritySHA1 is the SHA1 hash of the certificate thumbprint. |
| 224 |
AuthoritySHA1 []byte |
| 225 |
} |
| 226 |
|
| 227 |
// WinELAM describes the configuration of an Early Launch AntiMalware driver. |
| 228 |
// These values represent the 3 measured registry values stored in the ELAM |
| 229 |
// hive for the driver. |
| 230 |
type WinELAM struct { |
| 231 |
Measured []byte |
| 232 |
Config []byte |
| 233 |
Policy []byte |
| 234 |
} |
| 235 |
|
| 236 |
// ParseWinEvents parses a series of events to extract information about |
| 237 |
// the bringup of Microsoft Windows. This information is not trustworthy |
| 238 |
// unless the integrity of platform & bootloader events has already been |
| 239 |
// established. |
| 240 |
func ParseWinEvents(events []Event) (*WinEvents, error) { |
| 241 |
var ( |
| 242 |
out = WinEvents{ |
| 243 |
LoadedModules: map[string]WinModuleLoad{}, |
| 244 |
ELAM: map[string]WinELAM{}, |
| 245 |
} |
| 246 |
seenSeparator struct { |
| 247 |
PCR12 bool |
| 248 |
PCR13 bool |
| 249 |
} |
| 250 |
) |
| 251 |
|
| 252 |
for _, e := range events { |
| 253 |
if e.Index != 12 && e.Index != 13 { |
| 254 |
continue |
| 255 |
} |
| 256 |
|
| 257 |
et, err := internal.UntrustedParseEventType(uint32(e.Type)) |
| 258 |
if err != nil { |
| 259 |
return nil, fmt.Errorf("unrecognised event type: %v", err) |
| 260 |
} |
| 261 |
|
| 262 |
digestVerify := e.digestEquals(e.Data) |
| 263 |
|
| 264 |
switch e.Index { |
| 265 |
case 12: // 'early boot' events |
| 266 |
switch et { |
| 267 |
case internal.EventTag: |
| 268 |
if seenSeparator.PCR12 { |
| 269 |
continue |
| 270 |
} |
| 271 |
s, err := internal.ParseTaggedEventData(e.Data) |
| 272 |
if err != nil { |
| 273 |
return nil, fmt.Errorf("invalid tagged event structure at event %d: %w", e.sequence, err) |
| 274 |
} |
| 275 |
if digestVerify != nil { |
| 276 |
return nil, fmt.Errorf("invalid digest for tagged event %d: %w", e.sequence, digestVerify) |
| 277 |
} |
| 278 |
if err := out.readWinEventBlock(s, e.Index); err != nil { |
| 279 |
return nil, fmt.Errorf("invalid SIPA events in event %d: %w", e.sequence, err) |
| 280 |
} |
| 281 |
case internal.Separator: |
| 282 |
if seenSeparator.PCR12 { |
| 283 |
return nil, fmt.Errorf("duplicate WBCL separator at event %d", e.sequence) |
| 284 |
} |
| 285 |
seenSeparator.PCR12 = true |
| 286 |
if !bytes.Equal(e.Data, []byte("WBCL")) { |
| 287 |
return nil, fmt.Errorf("invalid WBCL separator data at event %d: %v", e.sequence, e.Data) |
| 288 |
} |
| 289 |
if digestVerify != nil { |
| 290 |
return nil, fmt.Errorf("invalid separator digest at event %d: %v", e.sequence, digestVerify) |
| 291 |
} |
| 292 |
|
| 293 |
default: |
| 294 |
return nil, fmt.Errorf("unexpected (PCR12) event type: %v", et) |
| 295 |
} |
| 296 |
case 13: // Post 'early boot' events |
| 297 |
switch et { |
| 298 |
case internal.EventTag: |
| 299 |
if seenSeparator.PCR13 { |
| 300 |
continue |
| 301 |
} |
| 302 |
s, err := internal.ParseTaggedEventData(e.Data) |
| 303 |
if err != nil { |
| 304 |
return nil, fmt.Errorf("invalid tagged event structure at event %d: %w", e.sequence, err) |
| 305 |
} |
| 306 |
if digestVerify != nil { |
| 307 |
return nil, fmt.Errorf("invalid digest for tagged event %d: %w", e.sequence, digestVerify) |
| 308 |
} |
| 309 |
if err := out.readWinEventBlock(s, e.Index); err != nil { |
| 310 |
return nil, fmt.Errorf("invalid SIPA events in event %d: %w", e.sequence, err) |
| 311 |
} |
| 312 |
case internal.Separator: |
| 313 |
if seenSeparator.PCR13 { |
| 314 |
return nil, fmt.Errorf("duplicate WBCL separator at event %d", e.sequence) |
| 315 |
} |
| 316 |
seenSeparator.PCR13 = true |
| 317 |
if !bytes.Equal(e.Data, []byte("WBCL")) { |
| 318 |
return nil, fmt.Errorf("invalid WBCL separator data at event %d: %v", e.sequence, e.Data) |
| 319 |
} |
| 320 |
if digestVerify != nil { |
| 321 |
return nil, fmt.Errorf("invalid separator digest at event %d: %v", e.sequence, digestVerify) |
| 322 |
} |
| 323 |
|
| 324 |
default: |
| 325 |
return nil, fmt.Errorf("unexpected (PCR13) event type: %v", et) |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
return &out, nil |
| 330 |
} |
| 331 |
|
| 332 |
type microsoftEventHeader struct { |
| 333 |
Type windowsEvent |
| 334 |
Size uint32 |
| 335 |
} |
| 336 |
|
| 337 |
// unknownSIPAEvent is returned by parseSIPAEvent if the event type is |
| 338 |
// not handled. Unlike other events in the TCG log, it is safe to skip |
| 339 |
// unhandled SIPA events, as they are embedded within EventTag structures, |
| 340 |
// and these structures should match the event digest. |
| 341 |
var errUnknownSIPAEvent = errors.New("unknown event") |
| 342 |
|
| 343 |
func (w *WinEvents) readBooleanInt64Event(header microsoftEventHeader, r *bytes.Reader) error { |
| 344 |
if header.Size != 8 { |
| 345 |
return fmt.Errorf("payload was %d bytes, want 8", header.Size) |
| 346 |
} |
| 347 |
var num uint64 |
| 348 |
if err := binary.Read(r, binary.LittleEndian, &num); err != nil { |
| 349 |
return fmt.Errorf("reading u64: %w", err) |
| 350 |
} |
| 351 |
isSet := num != 0 |
| 352 |
|
| 353 |
switch header.Type { |
| 354 |
// Boolean signals that latch off if the are ever false (ie: attributes |
| 355 |
// that represent a stronger security state when set). |
| 356 |
case dataExecutionPrevention: |
| 357 |
if isSet && w.DEPEnabled == TernaryUnknown { |
| 358 |
w.DEPEnabled = TernaryTrue |
| 359 |
} else if !isSet { |
| 360 |
w.DEPEnabled = TernaryFalse |
| 361 |
} |
| 362 |
} |
| 363 |
return nil |
| 364 |
} |
| 365 |
|
| 366 |
func (w *WinEvents) readBooleanByteEvent(header microsoftEventHeader, r *bytes.Reader) error { |
| 367 |
if header.Size != 1 { |
| 368 |
return fmt.Errorf("payload was %d bytes, want 1", header.Size) |
| 369 |
} |
| 370 |
var b byte |
| 371 |
if err := binary.Read(r, binary.LittleEndian, &b); err != nil { |
| 372 |
return fmt.Errorf("reading byte: %w", err) |
| 373 |
} |
| 374 |
isSet := b != 0 |
| 375 |
|
| 376 |
switch header.Type { |
| 377 |
// Boolean signals that latch on if they are ever true (ie: attributes |
| 378 |
// that represent a weaker security state when set). |
| 379 |
case osKernelDebug: |
| 380 |
w.KernelDebugEnabled = w.KernelDebugEnabled || isSet |
| 381 |
case bootDebugging: |
| 382 |
w.BootDebuggingEnabled = w.BootDebuggingEnabled || isSet |
| 383 |
case testSigning: |
| 384 |
w.TestSigningEnabled = w.TestSigningEnabled || isSet |
| 385 |
|
| 386 |
// Boolean signals that latch off if the are ever false (ie: attributes |
| 387 |
// that represent a stronger security state when set). |
| 388 |
case codeIntegrity: |
| 389 |
if isSet && w.CodeIntegrityEnabled == TernaryUnknown { |
| 390 |
w.CodeIntegrityEnabled = TernaryTrue |
| 391 |
} else if !isSet { |
| 392 |
w.CodeIntegrityEnabled = TernaryFalse |
| 393 |
} |
| 394 |
} |
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
func (w *WinEvents) readUint32(header microsoftEventHeader, r io.Reader) (uint32, error) { |
| 399 |
if header.Size != 4 { |
| 400 |
return 0, fmt.Errorf("integer size not uint32 (%d bytes)", header.Size) |
| 401 |
} |
| 402 |
|
| 403 |
data := make([]uint8, header.Size) |
| 404 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 405 |
return 0, fmt.Errorf("reading u32: %w", err) |
| 406 |
} |
| 407 |
i := binary.LittleEndian.Uint32(data) |
| 408 |
|
| 409 |
return i, nil |
| 410 |
} |
| 411 |
|
| 412 |
func (w *WinEvents) readUint64(header microsoftEventHeader, r io.Reader) (uint64, error) { |
| 413 |
if header.Size != 8 { |
| 414 |
return 0, fmt.Errorf("integer size not uint64 (%d bytes)", header.Size) |
| 415 |
} |
| 416 |
|
| 417 |
data := make([]uint8, header.Size) |
| 418 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 419 |
return 0, fmt.Errorf("reading u64: %w", err) |
| 420 |
} |
| 421 |
i := binary.LittleEndian.Uint64(data) |
| 422 |
|
| 423 |
return i, nil |
| 424 |
} |
| 425 |
|
| 426 |
func (w *WinEvents) readBootCounter(header microsoftEventHeader, r *bytes.Reader) error { |
| 427 |
i, err := w.readUint64(header, r) |
| 428 |
if err != nil { |
| 429 |
return fmt.Errorf("boot counter: %v", err) |
| 430 |
} |
| 431 |
|
| 432 |
if w.BootCount > 0 && w.BootCount != i { |
| 433 |
return fmt.Errorf("conflicting values for boot counter: %d != %d", i, w.BootCount) |
| 434 |
} |
| 435 |
w.BootCount = i |
| 436 |
return nil |
| 437 |
} |
| 438 |
|
| 439 |
func (w *WinEvents) readTransferControl(header microsoftEventHeader, r *bytes.Reader) error { |
| 440 |
i, err := w.readUint32(header, r) |
| 441 |
if err != nil { |
| 442 |
return fmt.Errorf("transfer control: %v", err) |
| 443 |
} |
| 444 |
|
| 445 |
// A transferControl event with a value of 1 indicates that bootmngr |
| 446 |
// launched WinLoad. A different (unknown) value is set if WinResume |
| 447 |
// is launched. |
| 448 |
w.ColdBoot = i == 0x1 |
| 449 |
return nil |
| 450 |
} |
| 451 |
|
| 452 |
func (w *WinEvents) readBitlockerUnlock(header microsoftEventHeader, r *bytes.Reader, pcr int) error { |
| 453 |
if header.Size > 8 { |
| 454 |
return fmt.Errorf("bitlocker data too large (%d bytes)", header.Size) |
| 455 |
} |
| 456 |
data := make([]uint8, header.Size) |
| 457 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 458 |
return fmt.Errorf("reading u%d: %w", header.Size<<8, err) |
| 459 |
} |
| 460 |
i, n := binary.Uvarint(data) |
| 461 |
if n <= 0 { |
| 462 |
return fmt.Errorf("reading u%d: invalid varint", header.Size<<8) |
| 463 |
} |
| 464 |
|
| 465 |
if pcr == 13 { |
| 466 |
// The bitlocker status is duplicated across both PCRs. As such, |
| 467 |
// we prefer the earlier one, and bail here to prevent duplicate |
| 468 |
// records. |
| 469 |
return nil |
| 470 |
} |
| 471 |
|
| 472 |
w.BitlockerUnlocks = append(w.BitlockerUnlocks, BitlockerStatus(i)) |
| 473 |
return nil |
| 474 |
} |
| 475 |
|
| 476 |
func (w *WinEvents) parseImageValidated(header microsoftEventHeader, r io.Reader) (bool, error) { |
| 477 |
if header.Size != 1 { |
| 478 |
return false, fmt.Errorf("payload was %d bytes, want 1", header.Size) |
| 479 |
} |
| 480 |
var num byte |
| 481 |
if err := binary.Read(r, binary.LittleEndian, &num); err != nil { |
| 482 |
return false, fmt.Errorf("reading u8: %w", err) |
| 483 |
} |
| 484 |
return num == 1, nil |
| 485 |
} |
| 486 |
|
| 487 |
func (w *WinEvents) parseHashAlgID(header microsoftEventHeader, r io.Reader) (WinCSPAlg, error) { |
| 488 |
i, err := w.readUint32(header, r) |
| 489 |
if err != nil { |
| 490 |
return 0, fmt.Errorf("hash algorithm ID: %v", err) |
| 491 |
} |
| 492 |
|
| 493 |
switch alg := WinCSPAlg(i & 0xff); alg { |
| 494 |
case WinAlgMD4, WinAlgMD5, WinAlgSHA1, WinAlgSHA256, WinAlgSHA384, WinAlgSHA512: |
| 495 |
return alg, nil |
| 496 |
default: |
| 497 |
return 0, fmt.Errorf("unknown algorithm ID: %x", i) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
func (w *WinEvents) parseAuthoritySerial(header microsoftEventHeader, r io.Reader) ([]byte, error) { |
| 502 |
if header.Size > 128 { |
| 503 |
return nil, fmt.Errorf("authority serial is too long (%d bytes)", header.Size) |
| 504 |
} |
| 505 |
data := make([]byte, header.Size) |
| 506 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 507 |
return nil, fmt.Errorf("reading bytes: %w", err) |
| 508 |
} |
| 509 |
return data, nil |
| 510 |
} |
| 511 |
|
| 512 |
func (w *WinEvents) parseAuthoritySHA1(header microsoftEventHeader, r io.Reader) ([]byte, error) { |
| 513 |
if header.Size > 20 { |
| 514 |
return nil, fmt.Errorf("authority thumbprint is too long (%d bytes)", header.Size) |
| 515 |
} |
| 516 |
data := make([]byte, header.Size) |
| 517 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 518 |
return nil, fmt.Errorf("reading bytes: %w", err) |
| 519 |
} |
| 520 |
return data, nil |
| 521 |
} |
| 522 |
|
| 523 |
func (w *WinEvents) parseImageBase(header microsoftEventHeader, r io.Reader) (uint64, error) { |
| 524 |
if header.Size != 8 { |
| 525 |
return 0, fmt.Errorf("payload was %d bytes, want 8", header.Size) |
| 526 |
} |
| 527 |
var num uint64 |
| 528 |
if err := binary.Read(r, binary.LittleEndian, &num); err != nil { |
| 529 |
return 0, fmt.Errorf("reading u64: %w", err) |
| 530 |
} |
| 531 |
return num, nil |
| 532 |
} |
| 533 |
|
| 534 |
func (w *WinEvents) parseAuthenticodeHash(header microsoftEventHeader, r io.Reader) ([]byte, error) { |
| 535 |
if header.Size > 32 { |
| 536 |
return nil, fmt.Errorf("authenticode hash data exceeds the size of any valid hash (%d bytes)", header.Size) |
| 537 |
} |
| 538 |
data := make([]byte, header.Size) |
| 539 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 540 |
return nil, fmt.Errorf("reading bytes: %w", err) |
| 541 |
} |
| 542 |
return data, nil |
| 543 |
} |
| 544 |
|
| 545 |
func (w *WinEvents) readLoadedModuleAggregation(rdr *bytes.Reader, header microsoftEventHeader) error { |
| 546 |
if available := int64(rdr.Len()); int64(header.Size) > available { |
| 547 |
return fmt.Errorf("LMA event data (%d bytes) larger than available data (%d bytes)", header.Size, available) |
| 548 |
} |
| 549 |
|
| 550 |
var ( |
| 551 |
r = &io.LimitedReader{R: rdr, N: int64(header.Size)} |
| 552 |
codeHash []byte |
| 553 |
imgBase, imgSize uint64 |
| 554 |
fPath string |
| 555 |
algID WinCSPAlg |
| 556 |
imgValidated bool |
| 557 |
aIssuer, aPublisher string |
| 558 |
aSerial, aSHA1 []byte |
| 559 |
) |
| 560 |
|
| 561 |
for r.N > 0 { |
| 562 |
var h microsoftEventHeader |
| 563 |
if err := binary.Read(r, binary.LittleEndian, &h); err != nil { |
| 564 |
return fmt.Errorf("parsing LMA sub-event: %v", err) |
| 565 |
} |
| 566 |
if int64(h.Size) > r.N { |
| 567 |
return fmt.Errorf("LMA sub-event is larger than available data: %d > %d", h.Size, r.N) |
| 568 |
} |
| 569 |
|
| 570 |
var err error |
| 571 |
switch h.Type { |
| 572 |
case imageBase: |
| 573 |
if imgBase != 0 { |
| 574 |
return errors.New("duplicate image base data in LMA event") |
| 575 |
} |
| 576 |
if imgBase, err = w.parseImageBase(h, r); err != nil { |
| 577 |
return err |
| 578 |
} |
| 579 |
case authenticodeHash: |
| 580 |
if codeHash != nil { |
| 581 |
return errors.New("duplicate authenticode hash structure in LMA event") |
| 582 |
} |
| 583 |
if codeHash, err = w.parseAuthenticodeHash(h, r); err != nil { |
| 584 |
return err |
| 585 |
} |
| 586 |
case filePath: |
| 587 |
if fPath != "" { |
| 588 |
return errors.New("duplicate file path in LMA event") |
| 589 |
} |
| 590 |
if fPath, err = w.parseUTF16(h, r); err != nil { |
| 591 |
return err |
| 592 |
} |
| 593 |
case imageSize: |
| 594 |
if imgSize != 0 { |
| 595 |
return errors.New("duplicate image size in LMA event") |
| 596 |
} |
| 597 |
if imgSize, err = w.readUint64(h, r); err != nil { |
| 598 |
return err |
| 599 |
} |
| 600 |
case hashAlgorithmID: |
| 601 |
if algID != 0 { |
| 602 |
return errors.New("duplicate hash algorithm ID in LMA event") |
| 603 |
} |
| 604 |
if algID, err = w.parseHashAlgID(h, r); err != nil { |
| 605 |
return err |
| 606 |
} |
| 607 |
case imageValidated: |
| 608 |
if imgValidated { |
| 609 |
return errors.New("duplicate image validated field in LMA event") |
| 610 |
} |
| 611 |
if imgValidated, err = w.parseImageValidated(h, r); err != nil { |
| 612 |
return err |
| 613 |
} |
| 614 |
case authorityIssuer: |
| 615 |
if aIssuer != "" { |
| 616 |
return errors.New("duplicate authority issuer in LMA event") |
| 617 |
} |
| 618 |
if aIssuer, err = w.parseUTF16(h, r); err != nil { |
| 619 |
return err |
| 620 |
} |
| 621 |
case authorityPublisher: |
| 622 |
if aPublisher != "" { |
| 623 |
return errors.New("duplicate authority publisher in LMA event") |
| 624 |
} |
| 625 |
if aPublisher, err = w.parseUTF16(h, r); err != nil { |
| 626 |
return err |
| 627 |
} |
| 628 |
case authoritySerial: |
| 629 |
if aSerial != nil { |
| 630 |
return errors.New("duplicate authority serial in LMA event") |
| 631 |
} |
| 632 |
if aSerial, err = w.parseAuthoritySerial(h, r); err != nil { |
| 633 |
return err |
| 634 |
} |
| 635 |
case authoritySHA1Thumbprint: |
| 636 |
if aSHA1 != nil { |
| 637 |
return errors.New("duplicate authority SHA1 thumbprint in LMA event") |
| 638 |
} |
| 639 |
if aSHA1, err = w.parseAuthoritySHA1(h, r); err != nil { |
| 640 |
return err |
| 641 |
} |
| 642 |
case moduleSVN: |
| 643 |
// Ignore - consume value. |
| 644 |
b := make([]byte, h.Size) |
| 645 |
if err := binary.Read(r, binary.LittleEndian, &b); err != nil { |
| 646 |
return err |
| 647 |
} |
| 648 |
default: |
| 649 |
return fmt.Errorf("unknown event in LMA aggregation: %v", h.Type) |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
var iBase []uint64 |
| 654 |
if imgBase != 0 { |
| 655 |
iBase = []uint64{imgBase} |
| 656 |
} |
| 657 |
|
| 658 |
l := WinModuleLoad{ |
| 659 |
FilePath: fPath, |
| 660 |
AuthenticodeHash: codeHash, |
| 661 |
ImageBase: iBase, |
| 662 |
ImageSize: imgSize, |
| 663 |
ImageValidated: imgValidated, |
| 664 |
HashAlgorithm: algID, |
| 665 |
AuthorityIssuer: aIssuer, |
| 666 |
AuthorityPublisher: aPublisher, |
| 667 |
AuthoritySerial: aSerial, |
| 668 |
AuthoritySHA1: aSHA1, |
| 669 |
} |
| 670 |
hashHex := hex.EncodeToString(l.AuthenticodeHash) |
| 671 |
l.ImageBase = append(l.ImageBase, w.LoadedModules[hashHex].ImageBase...) |
| 672 |
w.LoadedModules[hashHex] = l |
| 673 |
return nil |
| 674 |
} |
| 675 |
|
| 676 |
// parseUTF16 decodes data representing a UTF16 string. It is assumed the |
| 677 |
// caller has validated that the data size is within allowable bounds. |
| 678 |
func (w *WinEvents) parseUTF16(header microsoftEventHeader, r io.Reader) (string, error) { |
| 679 |
data := make([]uint16, header.Size/2) |
| 680 |
if err := binary.Read(r, binary.LittleEndian, &data); err != nil { |
| 681 |
return "", err |
| 682 |
} |
| 683 |
return strings.TrimSuffix(string(utf16.Decode(data)), "\x00"), nil |
| 684 |
} |
| 685 |
|
| 686 |
func (w *WinEvents) readELAMAggregation(rdr io.Reader, header microsoftEventHeader) error { |
| 687 |
if br, ok := rdr.(*bytes.Reader); ok { |
| 688 |
if available := int64(br.Len()); int64(header.Size) > available { |
| 689 |
return fmt.Errorf("ELAM aggregation event data (%d bytes) larger than available data (%d bytes)", header.Size, available) |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
var ( |
| 694 |
r = &io.LimitedReader{R: rdr, N: int64(header.Size)} |
| 695 |
driverName string |
| 696 |
measured []byte |
| 697 |
policy []byte |
| 698 |
config []byte |
| 699 |
) |
| 700 |
|
| 701 |
for r.N > 0 { |
| 702 |
var h microsoftEventHeader |
| 703 |
if err := binary.Read(r, binary.LittleEndian, &h); err != nil { |
| 704 |
return fmt.Errorf("parsing ELAM aggregation sub-event: %v", err) |
| 705 |
} |
| 706 |
if int64(h.Size) > r.N { |
| 707 |
return fmt.Errorf("ELAM aggregation sub-event is larger than available data: %d > %d", h.Size, r.N) |
| 708 |
} |
| 709 |
|
| 710 |
var err error |
| 711 |
switch h.Type { |
| 712 |
case elamAggregation: |
| 713 |
w.readELAMAggregation(r, h) |
| 714 |
if r.N == 0 { |
| 715 |
return nil |
| 716 |
} |
| 717 |
case elamKeyname: |
| 718 |
if driverName != "" { |
| 719 |
return errors.New("duplicate driver name in ELAM aggregation event") |
| 720 |
} |
| 721 |
if driverName, err = w.parseUTF16(h, r); err != nil { |
| 722 |
return fmt.Errorf("parsing ELAM driver name: %v", err) |
| 723 |
} |
| 724 |
case elamMeasured: |
| 725 |
if measured != nil { |
| 726 |
return errors.New("duplicate measured data in ELAM aggregation event") |
| 727 |
} |
| 728 |
measured = make([]byte, h.Size) |
| 729 |
if err := binary.Read(r, binary.LittleEndian, &measured); err != nil { |
| 730 |
return fmt.Errorf("reading ELAM measured value: %v", err) |
| 731 |
} |
| 732 |
case elamPolicy: |
| 733 |
if policy != nil { |
| 734 |
return errors.New("duplicate policy data in ELAM aggregation event") |
| 735 |
} |
| 736 |
policy = make([]byte, h.Size) |
| 737 |
if err := binary.Read(r, binary.LittleEndian, &policy); err != nil { |
| 738 |
return fmt.Errorf("reading ELAM policy value: %v", err) |
| 739 |
} |
| 740 |
case elamConfiguration: |
| 741 |
if config != nil { |
| 742 |
return errors.New("duplicate config data in ELAM aggregation event") |
| 743 |
} |
| 744 |
config = make([]byte, h.Size) |
| 745 |
if err := binary.Read(r, binary.LittleEndian, &config); err != nil { |
| 746 |
return fmt.Errorf("reading ELAM config value: %v", err) |
| 747 |
} |
| 748 |
default: |
| 749 |
return fmt.Errorf("unknown event in LMA aggregation: %v", h.Type) |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
if driverName == "" { |
| 754 |
return errors.New("ELAM driver name not specified") |
| 755 |
} |
| 756 |
w.ELAM[driverName] = WinELAM{ |
| 757 |
Measured: measured, |
| 758 |
Config: config, |
| 759 |
Policy: policy, |
| 760 |
} |
| 761 |
return nil |
| 762 |
} |
| 763 |
|
| 764 |
func (w *WinEvents) readSIPAEvent(r *bytes.Reader, pcr int) error { |
| 765 |
var header microsoftEventHeader |
| 766 |
if err := binary.Read(r, binary.LittleEndian, &header); err != nil { |
| 767 |
return err |
| 768 |
} |
| 769 |
|
| 770 |
switch header.Type { |
| 771 |
case elamAggregation: |
| 772 |
return w.readELAMAggregation(r, header) |
| 773 |
case loadedModuleAggregation: |
| 774 |
return w.readLoadedModuleAggregation(r, header) |
| 775 |
case bootCounter: |
| 776 |
return w.readBootCounter(header, r) |
| 777 |
case bitlockerUnlock: |
| 778 |
return w.readBitlockerUnlock(header, r, pcr) |
| 779 |
case transferControl: |
| 780 |
return w.readTransferControl(header, r) |
| 781 |
|
| 782 |
case osKernelDebug, codeIntegrity, bootDebugging, testSigning: // Parse boolean values. |
| 783 |
return w.readBooleanByteEvent(header, r) |
| 784 |
case dataExecutionPrevention: // Parse booleans represented as uint64's. |
| 785 |
return w.readBooleanInt64Event(header, r) |
| 786 |
|
| 787 |
default: |
| 788 |
// Event type was not handled, consume the data. |
| 789 |
if int(header.Size) > r.Len() { |
| 790 |
return fmt.Errorf("event data len (%d bytes) larger than event length (%d bytes)", header.Size, r.Len()) |
| 791 |
} |
| 792 |
tmp := make([]byte, header.Size) |
| 793 |
if err := binary.Read(r, binary.LittleEndian, &tmp); err != nil { |
| 794 |
return fmt.Errorf("reading unknown data section of length %d: %w", header.Size, err) |
| 795 |
} |
| 796 |
|
| 797 |
return errUnknownSIPAEvent |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
// readWinEventBlock extracts boot configuration from SIPA events contained in |
| 802 |
// the given tagged event. |
| 803 |
func (w *WinEvents) readWinEventBlock(evt *internal.TaggedEventData, pcr int) error { |
| 804 |
r := bytes.NewReader(evt.Data) |
| 805 |
|
| 806 |
// All windows information should be sub events in an enclosing SIPA |
| 807 |
// container event. |
| 808 |
if (windowsEvent(evt.ID) & sipaTypeMask) != sipaContainer { |
| 809 |
return fmt.Errorf("expected container event, got %v", windowsEvent(evt.ID)) |
| 810 |
} |
| 811 |
|
| 812 |
for r.Len() > 0 { |
| 813 |
if err := w.readSIPAEvent(r, pcr); err != nil { |
| 814 |
if errors.Is(err, errUnknownSIPAEvent) { |
| 815 |
// Unknown SIPA events are okay as all TCG events are verifiable. |
| 816 |
continue |
| 817 |
} |
| 818 |
return err |
| 819 |
} |
| 820 |
} |
| 821 |
return nil |
| 822 |
} |