-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc_attack_test.clj
executable file
·72 lines (53 loc) · 1.66 KB
/
cbc_attack_test.clj
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
(ns set2.cbc-attack-test
(:require [clojure
[string :as str]
[test :refer :all]]
[set2.cbc-attack :as sut]
[util
[aes :as aes]
[random :as rand]
[tools :as u]]))
;; -------------------------
;; Utilities
;; -------------------------
(def random-cipher-key (rand/byte-lst 16))
(def random-iv (rand/byte-lst 16))
(def msg-prefix "comment1=cooking%20MCs;userdata=")
(def msg-suffix ";comment2=%20like%20a%20pound%20of%20bacon")
(defn encrypt-cookie
[userdata]
(when (some #(or (= \; %) (= \= %)) userdata)
(throw (Exception. "Invalid userdata. Should not contain ; or =")))
(aes/encrypt (map int (concat msg-prefix userdata msg-suffix))
random-cipher-key
:cbc random-iv))
(defn decrypt-cookie
[ciphertext]
(u/bytes->str (aes/decrypt ciphertext random-cipher-key :cbc random-iv)))
(defn parse-cookie
[cookie]
(reduce #(conj %1 (str/split %2 #"=")) {}
(filter #(not= % "") (str/split cookie #";"))))
(defn oracle-encrypt
[text]
(encrypt-cookie (u/bytes->str text)))
(defn is-admin?
[cookie]
(= "true" (get (parse-cookie cookie) "admin")))
;; -------------------------
;; Tests
;; -------------------------
(deftest cbc-attack-test
(testing "Failed attacking CBC"
(is (-> ";admin=true;"
(sut/attack-cbc oracle-encrypt)
decrypt-cookie
is-admin?))))
(deftest cbc-attack-test2
(testing "Falied attacking CBC"
(is (-> ";priv=root;"
(sut/attack-cbc oracle-encrypt)
decrypt-cookie
parse-cookie
(get "priv")
(= "root")))))