-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
security_test.go
250 lines (236 loc) · 8.21 KB
/
security_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2018 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package pe
import (
"crypto/x509"
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
"time"
)
type TestSecurityEntry struct {
Header WinCertificate
Certificates []Certificate
err error
}
func TestParseSecurityDirectory(t *testing.T) {
tests := []struct {
in string
out TestSecurityEntry
}{
{
getAbsoluteFilePath("test/putty.exe"),
TestSecurityEntry{
Header: WinCertificate{
Length: 0x3D90,
Revision: 0x200,
CertificateType: 0x2,
},
Certificates: []Certificate{
{
Info: CertInfo{
Issuer: "GB, Greater Manchester, Salford, COMODO CA Limited, COMODO RSA Code Signing CA",
Subject: "GB, Cambridgeshire, Cambridge, Simon Tatham, Simon Tatham",
NotBefore: time.Date(2018, time.November, 13, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2021, time.November, 8, 23, 59, 59, 0, time.UTC),
SerialNumber: "7c1118cbbadc95da3752c46e47a27438",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA1WithRSA,
},
Verified: true,
SignatureValid: true,
},
{
Info: CertInfo{
Issuer: "GB, Greater Manchester, Salford, COMODO CA Limited, COMODO RSA Code Signing CA",
Subject: "GB, Cambridgeshire, Cambridge, Simon Tatham, Simon Tatham",
NotBefore: time.Date(2018, time.November, 13, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2021, time.November, 8, 23, 59, 59, 0, time.UTC),
SerialNumber: "7c1118cbbadc95da3752c46e47a27438",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
},
Verified: true,
SignatureValid: true,
},
},
err: nil,
},
},
{
getAbsoluteFilePath("test/putty_modified.exe"),
TestSecurityEntry{
Header: WinCertificate{
Length: 0x3D90,
Revision: 0x200,
CertificateType: 0x2,
},
Certificates: []Certificate{
{
Info: CertInfo{
Issuer: "GB, Greater Manchester, Salford, COMODO CA Limited, COMODO RSA Code Signing CA",
Subject: "GB, Cambridgeshire, Cambridge, Simon Tatham, Simon Tatham",
NotBefore: time.Date(2018, time.November, 13, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2021, time.November, 8, 23, 59, 59, 0, time.UTC),
SerialNumber: "7c1118cbbadc95da3752c46e47a27438",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA1WithRSA,
},
Verified: true,
SignatureValid: false,
},
{
Info: CertInfo{
Issuer: "GB, Greater Manchester, Salford, COMODO CA Limited, COMODO RSA Code Signing CA",
Subject: "GB, Cambridgeshire, Cambridge, Simon Tatham, Simon Tatham",
NotBefore: time.Date(2018, time.November, 13, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2021, time.November, 8, 23, 59, 59, 0, time.UTC),
SerialNumber: "7c1118cbbadc95da3752c46e47a27438",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
},
Verified: true,
SignatureValid: false,
},
},
err: nil,
},
},
{
getAbsoluteFilePath("test/579fd8a0385482fb4c789561a30b09f25671e86422f40ef5cca2036b28f99648"),
TestSecurityEntry{
Header: WinCertificate{
Length: 0x3488,
Revision: 0x200,
CertificateType: 0x2,
},
Certificates: []Certificate{
{
Info: CertInfo{
Issuer: "US, VeriSign, Inc., VeriSign Class 3 Code Signing 2010 CA",
Subject: "US, California, Mountain View, Symantec Corporation, Symantec Corporation",
NotBefore: time.Date(2016, time.December, 16, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2017, time.December, 17, 23, 59, 59, 0, time.UTC),
SerialNumber: "0ebfea68d677b3e26cab41c33f3e69de",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA1WithRSA,
},
Verified: false,
SignatureValid: false,
},
{
Info: CertInfo{
Issuer: "US, Symantec Corporation, Symantec Class 3 SHA256 Code Signing CA - G2",
Subject: "US, California, Mountain View, Symantec Corporation, Symantec Corporation",
NotBefore: time.Date(2017, time.March, 15, 00, 00, 0, 0, time.UTC),
NotAfter: time.Date(2018, time.April, 13, 23, 59, 59, 0, time.UTC),
SerialNumber: "2e6be6bd11a8676e6c57909e9b0d5f57",
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
},
Verified: false,
SignatureValid: false,
},
},
err: nil,
},
},
{
getAbsoluteFilePath("test/00121dae38f26a33da2990987db58738c5a5966930126a42f606a3b40e014624"),
TestSecurityEntry{
err: ErrSecurityDataDirInvalid,
},
},
}
for _, tt := range tests {
t.Run(filepath.Base(tt.in), func(t *testing.T) {
ops := Options{Fast: true}
file, err := New(tt.in, &ops)
if err != nil {
t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
}
err = file.Parse()
if err != nil {
t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
}
var va, size uint32
if file.Is64 {
oh64 := file.NtHeader.OptionalHeader.(ImageOptionalHeader64)
dirEntry := oh64.DataDirectory[ImageDirectoryEntryCertificate]
va = dirEntry.VirtualAddress
size = dirEntry.Size
} else {
oh32 := file.NtHeader.OptionalHeader.(ImageOptionalHeader32)
dirEntry := oh32.DataDirectory[ImageDirectoryEntryCertificate]
va = dirEntry.VirtualAddress
size = dirEntry.Size
}
err = file.parseSecurityDirectory(va, size)
if err != tt.out.err {
t.Fatalf("parseSecurityDirectory(%s) failed, reason: %v", tt.in, err)
}
got := file.Certificates
if tt.out.err == nil {
if !reflect.DeepEqual(got.Header, tt.out.Header) {
t.Fatalf("certificate header assertion failed, got %v, want %v", got.Header, tt.out.Header)
}
}
if len(got.Certificates) != len(tt.out.Certificates) {
t.Fatalf("certificate count assertion failed, got %d, want %d", len(got.Certificates), len(tt.out.Certificates))
}
for i, cert := range got.Certificates {
expected := tt.out.Certificates[i]
if !reflect.DeepEqual(cert.Info, expected.Info) {
t.Fatalf("certificate info %d assertion failed, got %v, want %v", i, cert.Info, expected.Info)
}
if expected.SignatureValid != cert.SignatureValid {
t.Fatalf("signature verification %d failed, cert %v, want %v", i, cert.SignatureValid, expected.SignatureValid)
}
if runtime.GOOS == "linux" {
if expected.Verified != cert.Verified {
t.Fatalf("certificate verification %d failed, cert %v, want %v", i, cert.Verified, expected.Verified)
}
}
}
})
}
}
func TestAuthentihash(t *testing.T) {
tests := []struct {
in string
out string
}{
{getAbsoluteFilePath("test/putty.exe"),
"8be7d65593b0fff2e8b29004640261b8a0d4fcc651a14cd0b8b702b7928f8ee0"},
{getAbsoluteFilePath("test/mscorlib.dll"),
"a52bd7784efbf206dbda2db058f3928deaf15f6fedf2773affae56023e2f0edb"},
{getAbsoluteFilePath("test/liblzo2-2.dll"),
"ae603480b92c7ea3feca164010d2594f9a5282f8b732ecaa0aca29f3225835f6"},
{getAbsoluteFilePath("test/kernel32.dll"),
"595e4eb556587a1363ff297df9f354a377963ecac0bed19230992b9601426aae"},
{getAbsoluteFilePath("test/mfc40u.dll"),
"5c8acdf9b2c7854c6b8e22e973d2fbae9c68fc22513d24c68c8e8010b1663e67"},
{getAbsoluteFilePath("test/000057fd78f66e64e15f5070364c824a8923b6216bd8bcf6368857fb9674c483"),
""},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
file, err := New(tt.in, &Options{})
if err != nil {
t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
}
err = file.Parse()
if err != nil {
t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
}
hash := file.Authentihash()
got := fmt.Sprintf("%x", hash)
if string(got) != tt.out {
t.Errorf("Authentihash(%s) got %v, want %v", tt.in, got, tt.out)
}
})
}
}