-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockCipherAttacks_test.go
288 lines (229 loc) · 6.7 KB
/
blockCipherAttacks_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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package matasano
import (
"bufio"
"bytes"
cryptorand "crypto/rand"
"encoding/base64"
"encoding/hex"
"os"
"reflect"
"strings"
"testing"
)
func TestOracleEncryptionModeDetector(t *testing.T) {
mode := OracleEncryptionModeDetector(ByteAtATimeECBEncryptor)
if mode != ECB {
t.Errorf("guessed the wrong mode for the ByteAtATimeECBEncryptor")
}
}
func TestEncryptionModeDetector(t *testing.T) {
detectEcbFile, err := os.Open("detectEcb.txt")
if err != nil {
t.Errorf("error reading test file: %v", err)
}
defer detectEcbFile.Close()
var lines []string
scanner := bufio.NewScanner(detectEcbFile)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
var ctBytes []byte
var mode int
found := false
expectedIndex := 132
for index, element := range lines {
ctBytes, _ = hex.DecodeString(element)
mode = EncryptionModeDetector(ctBytes)
if mode == ECB {
found = true
if index != expectedIndex {
t.Errorf("found ecb at unexpected index: %v", index)
}
}
}
if !found {
t.Errorf("didn't find anything encrypted with ecb")
}
}
func TestDiscoverBlockSizeOfEncryptionOracle(t *testing.T) {
size := DiscoverBlockSizeOfEncryptionOracle(ByteAtATimeECBEncryptor)
if size != 16 {
t.Errorf("got %v", size)
}
}
func TestByteAtATime(t *testing.T) {
targetBytes, _ := base64.StdEncoding.DecodeString(targetB64PlainText)
var empytyPrefix []byte
pt := DecryptTarget(ByteAtATimeECBEncryptor, empytyPrefix, len(targetBytes))
if bytes.Compare(pt, targetBytes) != 0 {
t.Errorf("got %v, expected %v", pt, targetBytes)
}
}
func TestByteAtATimeTricky(t *testing.T) {
targetBytes, _ := base64.StdEncoding.DecodeString(targetB64PlainText)
prefix, _ := base64.StdEncoding.DecodeString(prefixB64PlainText)
pt := DecryptTarget(ByteAtATimeECBEncryptorTricky, prefix, len(targetBytes))
if bytes.Compare(pt, targetBytes) != 0 {
t.Errorf("got %v, expected %v", pt, targetBytes)
}
}
func TestParseParamString(t *testing.T) {
testString := "foo=bar&baz=qux&zap=zazzle"
expected := make(map[string]string)
expected["foo"] = "bar"
expected["baz"] = "qux"
expected["zap"] = "zazzle"
actual := parseParamString(testString)
eq := reflect.DeepEqual(expected, actual)
if !eq {
t.Errorf("got %v", actual)
}
}
func TestUserEncode(t *testing.T) {
u := User{"[email protected]", "user", 10}
if u.Encode() != "[email protected]&uid=10&role=user" {
t.Errorf("got %s", u.Encode())
}
}
func TestCreateAdminProfileCipherText(t *testing.T) {
ct := CreateAdminProfileCipherText()
user, parseError := DecryptAndParseProfile(ct)
if parseError != nil {
t.Errorf("error parsing profile: %v", parseError)
}
if user.role != "admin" {
t.Errorf("got %v", user.role)
}
}
func TestPKCS7PaddingStripper(t *testing.T) {
iceBytes := []byte("ICE ICE BABY\x04\x04\x04\x04")
stripped, err := StripPKCS7Padding(iceBytes)
expected := []byte("ICE ICE BABY")
if err != nil {
t.Errorf("got an error while stripping valid pkcs7 padding: %v", err)
}
if bytes.Compare(expected, stripped) != 0 {
t.Errorf("expected %v, but got %v when stripping padding", expected, stripped)
}
}
func TestcbcAdminBitFlipperRemovesAdminString(t *testing.T) {
adminString := ";admin=true;"
ct, iv := cbcBitFlipStringEncrypt(adminString)
containsAdmin, err := CbcBitFlipIsAdmin(ct, iv)
if err != nil {
t.Errorf("got an unexpected error: %v", err)
}
if containsAdmin {
t.Errorf("was able to sneak in ';admin=true;'")
}
}
func TestForgeAdminCiphertext(t *testing.T) {
ct, iv := ForgeAdminCiphertext()
isAdmin, err := CbcBitFlipIsAdmin(ct, iv)
if err != nil {
t.Errorf("got an unexpected error: %v", err)
}
if !isAdmin {
t.Errorf("failed to get admin=true. Here's the ct: %v", ct)
}
}
func TestForgeAdminCiphertextCtr(t *testing.T) {
ct, err := ForgeAdminCiphertextCtr()
if err != nil {
t.Errorf("got unexpected error: %v", err)
}
isAdmin, err := CtrBitFlipIsAdmin(ct)
if err != nil {
t.Errorf("got an unexpected error: %v", err)
}
if !isAdmin {
t.Errorf("failed to get admin=true. Here's the ct: %v", ct)
}
}
func CbcBitFlipIsAdmin(ct, iv []byte) (bool, error) {
key, _ := hex.DecodeString(bitFlipKey)
cbcEnc := CbcEncryptor{key, iv}
pt, _ := cbcEnc.CbcDecrypt(ct)
stripped, err := StripPKCS7Padding(pt)
if err != nil {
return false, err
}
return strings.Contains(string(stripped), ";admin=true;"), nil
}
func CtrBitFlipIsAdmin(ct []byte) (bool, error) {
keyBytes, _ := hex.DecodeString(bitFlipKey)
ctrEnc := CtrEncryptor{keyBytes, 0}
pt, decryptErr := ctrEnc.CtrDecrypt(ct)
if decryptErr != nil {
return false, decryptErr
}
stripped, paddingErr := StripPKCS7Padding(pt)
if paddingErr != nil {
return false, paddingErr
}
return strings.Contains(string(stripped), ";admin=true;"), nil
}
func TestPaddingOracleEncryptRandomPlaintextPadding(t *testing.T) {
iv, ct, err := PaddingOracleEncryptRandomPlaintext()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
validPadding, decryptError := CiphertextHasValidPadding(iv, ct)
if decryptError != nil {
t.Errorf("Unexpected error during decryption: %v", decryptError)
}
if !validPadding {
t.Errorf("Plaintext has invalid padding! IV and ciphertext: %v, %v", iv, ct)
}
}
func TestPaddingOracleAttack(t *testing.T) {
iv, ct, err := PaddingOracleEncryptRandomPlaintext()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
pt, paddingOracleError := PaddingOracleAttack(iv, ct)
if paddingOracleError != nil {
t.Errorf("error while performing padding oracle attack: %v", paddingOracleError)
}
key, _ := hex.DecodeString(paddingOracleKeyString)
cbcEnc := CbcEncryptor{key, iv}
expectedPt, decryptErr := cbcEnc.CbcDecrypt(ct)
if decryptErr != nil {
t.Errorf("error decrypting: %v", decryptErr)
}
if bytes.Compare(expectedPt, pt) != 0 {
t.Errorf("expected %v, but got %v", expectedPt, pt)
}
}
func TestAttackRandomWriteReEncrypt(t *testing.T) {
pt, ptDecodeErr := ReadB64File("ecbPlaintext.txt")
if ptDecodeErr != nil {
t.Errorf("error decoding plaintext: %v", ptDecodeErr)
}
key := make([]byte, 16)
cryptorand.Read(key)
enc := CtrEncryptor{key, 0}
ct, encErr := enc.CtrEncrypt(pt)
if encErr != nil {
t.Errorf("error encrypting: %v", encErr)
}
guessedPt, attackErr := enc.AttackRandomWriteReEncrypt(ct)
if attackErr != nil {
t.Errorf("error performing ctr random re-encrypt: %v", attackErr)
}
if bytes.Compare(guessedPt, pt) != 0 {
t.Errorf("guessed %v, but expected %v", guessedPt, pt)
}
}
func TestAttackIvEqualsKeyCbc(t *testing.T) {
key := make([]byte, 16)
cryptorand.Read(key)
cbcEnc := CbcEncryptor{key, key}
guessedKey, err := cbcEnc.AttackIvEqualsKeyCbc()
if err != nil {
t.Errorf("error guessing key: %v", err)
}
if bytes.Compare(guessedKey, key) != 0 {
t.Errorf("guessed %v, but expected %v", guessedKey, key)
}
}