-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (85 loc) · 2.01 KB
/
main.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
package main
import (
"os"
"fmt"
"github.com/adriansr/cryptopals-challenge/util"
"crypto/aes"
"github.com/adriansr/cryptopals-challenge/crypto"
"strings"
"github.com/adriansr/cryptopals-challenge/text/ascii"
"bytes"
"net/url"
"github.com/adriansr/cryptopals-challenge/crypto/xor"
)
var origKey = util.RandomBytes(aes.BlockSize)
func encryptWithKeyAsIV(data string, key []byte) []byte {
bc, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
cbc := crypto.NewCBCBlockMode(key, bc)
return cbc.Encrypt(strings.NewReader(data))
}
func encrypt(data string) []byte {
if !ascii.IsValid([]byte(data)) {
panic(data)
}
return encryptWithKeyAsIV(data, origKey)
}
func check(ciphertext []byte) (bool, error) {
bc, err := aes.NewCipher(origKey)
if err != nil {
panic(err)
}
cbc := crypto.NewCBCBlockMode(origKey, bc)
plaintext := cbc.Decrypt(bytes.NewReader(ciphertext))
if !ascii.IsValid(plaintext) {
return false, fmt.Errorf("input has invalid characters: %s",
url.QueryEscape(string(plaintext)))
}
return bytes.Contains(plaintext, []byte(";admin=true;")), nil
}
func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "Usage: %s\n", os.Args[0])
os.Exit(1)
}
ciphertext := encrypt("AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC")
isAdmin, err := check(ciphertext)
if err != nil {
panic(err)
}
if isAdmin {
panic(isAdmin)
}
copy(ciphertext[32:], ciphertext[:16])
copy(ciphertext[16:32], make([]byte, 16))
isAdmin, err = check(ciphertext)
if err == nil {
panic(err)
}
if isAdmin {
panic(isAdmin)
}
fmt.Fprintf(os.Stderr, "got expected error='%v'\n", err)
msg := err.Error()
pos := strings.Index(msg, ": ")
rawS, err := url.QueryUnescape(msg[pos+2:])
if err != nil {
panic(err)
}
raw := []byte(rawS)
if len(raw) != len(ciphertext) {
panic(len(raw))
}
xor.XORBlocks(raw[:16], raw[32:])
rKey := raw[:16]
isAdmin, err = check(encryptWithKeyAsIV("a;admin=true;bxx", rKey))
if err != nil {
panic(err)
}
if !isAdmin {
panic(isAdmin)
}
fmt.Fprintf(os.Stderr, "ADMIN!\n")
}