| 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 |
"crypto/sha256" |
| 19 |
"encoding/binary" |
| 20 |
"encoding/json" |
| 21 |
"os" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 26 |
) |
| 27 |
|
| 28 |
func oversizedSIPAEvent(t *testing.T, sipaType windowsEvent) Event { |
| 29 |
t.Helper() |
| 30 |
inner := make([]byte, 8) |
| 31 |
binary.LittleEndian.PutUint32(inner[0:4], uint32(sipaType)) |
| 32 |
binary.LittleEndian.PutUint32(inner[4:8], 1<<30) // 1 GiB claimed size |
| 33 |
|
| 34 |
data := make([]byte, 8+len(inner)) |
| 35 |
binary.LittleEndian.PutUint32(data[0:4], uint32(sipaContainer)) |
| 36 |
binary.LittleEndian.PutUint32(data[4:8], uint32(len(inner))) |
| 37 |
copy(data[8:], inner) |
| 38 |
|
| 39 |
digest := sha256.Sum256(data) |
| 40 |
return Event{ |
| 41 |
Index: 12, |
| 42 |
Type: EventType(0x00000006), // EV_EVENT_TAG |
| 43 |
Data: data, |
| 44 |
Digest: digest[:], |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestParseWinEvents(t *testing.T) { |
| 49 |
want := &WinEvents{ |
| 50 |
ColdBoot: true, |
| 51 |
BootCount: 4, |
| 52 |
DEPEnabled: TernaryTrue, |
| 53 |
CodeIntegrityEnabled: TernaryTrue, |
| 54 |
BitlockerUnlocks: []BitlockerStatus{0, 0}, |
| 55 |
LoadedModules: map[string]WinModuleLoad{ |
| 56 |
"0fdce7d71936f79445e7d2c84cbeb97c948d3730e0b839166b0a4e625c2d4547": { |
| 57 |
FilePath: `\Windows\System32\drivers\vioscsi.sys`, |
| 58 |
ImageBase: []uint64{81416192}, |
| 59 |
ImageSize: uint64(86016), |
| 60 |
HashAlgorithm: WinAlgSHA256, |
| 61 |
ImageValidated: true, |
| 62 |
AuthorityIssuer: "Microsoft Windows Third Party Component CA 2014", |
| 63 |
AuthorityPublisher: "Microsoft Windows Hardware Compatibility Publisher", |
| 64 |
AuthoritySerial: []uint8{ |
| 65 |
0x33, 0x00, 0x00, // |3..| |
| 66 |
0x00, 0x25, 0x3a, 0x27, 0x38, 0x69, 0x0a, 0x34, 0x51, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, // -|.%:'8i.4Q......%| |
| 67 |
}, |
| 68 |
AuthoritySHA1: []uint8{ |
| 69 |
0x26, 0x29, 0xe8, 0x6a, 0xae, 0x6e, 0xb9, 0xc9, 0xad, 0xcc, 0x1c, 0x54, 0x8d, 0x60, 0x1a, 0x50, |
| 70 |
0xfd, 0x96, 0x92, 0x7a, |
| 71 |
}, |
| 72 |
AuthenticodeHash: []byte{15, 220, 231, 215, 25, 54, 247, 148, 69, 231, 210, 200, 76, 190, 185, 124, 148, 141, 55, 48, 224, 184, 57, 22, 107, 10, 78, 98, 92, 45, 69, 71}, |
| 73 |
}, |
| 74 |
"055a36a9921b98cc04042ca95249c7eca655536868dafcec7508947ebe5e71f4": { |
| 75 |
FilePath: `\Windows\System32\Drivers\ksecpkg.sys`, |
| 76 |
ImageBase: []uint64{82952192}, |
| 77 |
ImageSize: uint64(204800), |
| 78 |
HashAlgorithm: WinAlgSHA256, |
| 79 |
ImageValidated: true, |
| 80 |
AuthorityIssuer: "Microsoft Windows Production PCA 2011", |
| 81 |
AuthorityPublisher: "Microsoft Windows", |
| 82 |
AuthoritySerial: []uint8{ |
| 83 |
0x33, 0x00, 0x00, 0x01, 0xc4, 0x22, 0xb2, 0xf7, 0x9b, 0x79, 0x3d, 0xac, 0xb2, 0x00, 0x00, 0x00, |
| 84 |
0x00, 0x01, 0xc4, |
| 85 |
}, |
| 86 |
AuthoritySHA1: []uint8{ |
| 87 |
0xae, 0x9c, 0x1a, 0xe5, 0x47, 0x63, 0x82, 0x2e, 0xec, 0x42, 0x47, 0x49, 0x83, 0xd8, 0xb6, 0x35, |
| 88 |
0x11, 0x6c, 0x84, 0x52, |
| 89 |
}, |
| 90 |
AuthenticodeHash: []byte{5, 90, 54, 169, 146, 27, 152, 204, 4, 4, 44, 169, 82, 73, 199, 236, 166, 85, 83, 104, 104, 218, 252, 236, 117, 8, 148, 126, 190, 94, 113, 244}, |
| 91 |
}, |
| 92 |
"2bedd1589410b6fa13c82f35db735025b6a160595922750248771f5abd0fee58": { |
| 93 |
FilePath: `\Windows\System32\drivers\volmgrx.sys`, |
| 94 |
ImageBase: []uint64{80875520}, |
| 95 |
ImageSize: uint64(405504), |
| 96 |
HashAlgorithm: WinAlgSHA256, |
| 97 |
ImageValidated: true, |
| 98 |
AuthorityIssuer: "Microsoft Windows Production PCA 2011", |
| 99 |
AuthorityPublisher: "Microsoft Windows", |
| 100 |
AuthoritySerial: []uint8{ |
| 101 |
0x33, 0x00, 0x00, 0x01, 0xc4, 0x22, 0xb2, 0xf7, 0x9b, 0x79, 0x3d, 0xac, 0xb2, 0x00, 0x00, 0x00, |
| 102 |
0x00, 0x01, 0xc4, |
| 103 |
}, |
| 104 |
AuthoritySHA1: []uint8{ |
| 105 |
0xae, 0x9c, 0x1a, 0xe5, 0x47, 0x63, 0x82, 0x2e, 0xec, 0x42, 0x47, 0x49, 0x83, 0xd8, 0xb6, 0x35, |
| 106 |
0x11, 0x6c, 0x84, 0x52, |
| 107 |
}, |
| 108 |
AuthenticodeHash: []byte{43, 237, 209, 88, 148, 16, 182, 250, 19, 200, 47, 53, 219, 115, 80, 37, 182, 161, 96, 89, 89, 34, 117, 2, 72, 119, 31, 90, 189, 15, 238, 88}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
ELAM: map[string]WinELAM{ |
| 112 |
"Windows Defender": {Measured: []byte{0x06, 0x7d, 0x5b, 0x9d, 0xc5, 0x62, 0x7f, 0x97, 0xdc, 0xf3, 0xfe, 0xff, 0x60, 0x2a, 0x34, 0x2e, 0xd6, 0x98, 0xd2, 0xcc}}, |
| 113 |
}, |
| 114 |
} |
| 115 |
|
| 116 |
data, err := os.ReadFile("testdata/windows_gcp_shielded_vm.json") |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("reading test data: %v", err) |
| 119 |
} |
| 120 |
var dump Dump |
| 121 |
if err := json.Unmarshal(data, &dump); err != nil { |
| 122 |
t.Fatalf("parsing test data: %v", err) |
| 123 |
} |
| 124 |
|
| 125 |
el, err := ParseEventLog(dump.Log.Raw) |
| 126 |
if err != nil { |
| 127 |
t.Fatalf("parsing event log: %v", err) |
| 128 |
} |
| 129 |
events, err := el.Verify(dump.Log.PCRs) |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("validating event log: %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
winState, err := ParseWinEvents(events) |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("ExtractSecurebootState() failed: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
// Theres way too many modules to cross-check by hand, so we filter it down |
| 140 |
// to a manageable number. |
| 141 |
keep := map[string]bool{ |
| 142 |
"0fdce7d71936f79445e7d2c84cbeb97c948d3730e0b839166b0a4e625c2d4547": true, |
| 143 |
"055a36a9921b98cc04042ca95249c7eca655536868dafcec7508947ebe5e71f4": true, |
| 144 |
"2bedd1589410b6fa13c82f35db735025b6a160595922750248771f5abd0fee58": true, |
| 145 |
} |
| 146 |
for k := range winState.LoadedModules { |
| 147 |
if _, keep := keep[k]; !keep { |
| 148 |
delete(winState.LoadedModules, k) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if diff := cmp.Diff(winState, want, cmpopts.IgnoreUnexported(WinEvents{})); diff != "" { |
| 153 |
t.Errorf("Unexpected WinEvents (+got, -want):\n%s", diff) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestParseWinEvents_Failures(t *testing.T) { |
| 158 |
t.Run("oversized LMA header", func(t *testing.T) { |
| 159 |
if _, err := ParseWinEvents([]Event{oversizedSIPAEvent(t, loadedModuleAggregation)}); err == nil { |
| 160 |
t.Error("expected error for oversized LMA header, got nil") |
| 161 |
} |
| 162 |
}) |
| 163 |
|
| 164 |
t.Run("oversized ELAM aggregation header", func(t *testing.T) { |
| 165 |
if _, err := ParseWinEvents([]Event{oversizedSIPAEvent(t, elamAggregation)}); err == nil { |
| 166 |
t.Error("expected error for oversized ELAM aggregation header, got nil") |
| 167 |
} |
| 168 |
}) |
| 169 |
|
| 170 |
t.Run("deeply nested ELAM aggregation headers", func(t *testing.T) { |
| 171 |
if _, err := ParseWinEvents([]Event{nestedELAMSIPAEvent(t, 100)}); err == nil { |
| 172 |
t.Error("expected error for deeply nested ELAM aggregation headers, got nil") |
| 173 |
} |
| 174 |
}) |
| 175 |
} |
| 176 |
|
| 177 |
func nestedELAMSIPAEvent(t *testing.T, depth int) Event { |
| 178 |
t.Helper() |
| 179 |
total := depth * 8 |
| 180 |
inner := make([]byte, total) |
| 181 |
for i := 0; i < depth; i++ { |
| 182 |
off := i * 8 |
| 183 |
binary.LittleEndian.PutUint32(inner[off:], uint32(elamAggregation)) |
| 184 |
binary.LittleEndian.PutUint32(inner[off+4:], uint32(total-off-8)) |
| 185 |
} |
| 186 |
|
| 187 |
data := make([]byte, 8+len(inner)) |
| 188 |
binary.LittleEndian.PutUint32(data[0:4], uint32(sipaContainer)) |
| 189 |
binary.LittleEndian.PutUint32(data[4:8], uint32(len(inner))) |
| 190 |
copy(data[8:], inner) |
| 191 |
|
| 192 |
digest := sha256.Sum256(data) |
| 193 |
return Event{ |
| 194 |
Index: 12, |
| 195 |
Type: EventType(0x00000006), // EV_EVENT_TAG |
| 196 |
Data: data, |
| 197 |
Digest: digest[:], |
| 198 |
} |
| 199 |
} |