-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
275 lines (254 loc) · 9.95 KB
/
mod.rs
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
#[cfg(test)]
mod tests {
use crate::shared::aes::{cbc_decrypt, cbc_encrypt, ecb_decrypt, ecb_encrypt, is_ecb};
use crate::shared::conversion::base64_to_bytes;
use crate::shared::key_value::parse_key_value;
use crate::shared::padding::{pad_pkcs7, unpad_pkcs7};
use crate::shared::xor::xor;
use rand::{Rng, RngCore};
use std::fs::read_to_string;
#[test]
fn test_challenge_9() {
let bytes = b"YELLOW SUBMARINE";
assert_eq!(pad_pkcs7(bytes, 20), b"YELLOW SUBMARINE\x04\x04\x04\x04")
}
#[test]
fn test_challenge_10() {
let key = b"YELLOW SUBMARINE";
let iv = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
let ct = base64_to_bytes(
&read_to_string("src/set2/challenge10.txt")
.unwrap()
.replace("\n", ""),
)
.unwrap();
let mut pt = vec![0; ct.len()];
cbc_decrypt(key, iv, &ct, &mut pt);
assert!(String::from_utf8(pt)
.unwrap()
.starts_with("I'm back and I'm ringin' the bell \n"));
}
#[test]
fn test_challenge_11() {
let encrypt_ecb_or_cbc = |pt: &[u8]| {
let mut rng = rand::thread_rng();
let mut key = [0; 16];
rng.fill_bytes(&mut key);
let mut prefix = vec![0; rng.gen_range(5..=10)];
rng.fill_bytes(&mut prefix);
let mut suffix = vec![0; rng.gen_range(5..=10)];
rng.fill_bytes(&mut suffix);
let pt = [&prefix, pt, &suffix].concat();
let pt = pad_pkcs7(&pt, 16);
let mut ct = vec![0; pt.len()];
if rng.gen::<bool>() {
ecb_encrypt(&key, &pt, &mut ct);
(ct, true)
} else {
let mut iv = [0; 16];
rng.fill_bytes(&mut iv);
cbc_encrypt(&key, &iv, &pt, &mut ct);
(ct, false)
}
};
// 11 characters to fill the first block if the random prefix is only 5 bytes.
// 32 characters to get the next two blocks without random bytes.
let pt = b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let (ct, ecb) = encrypt_ecb_or_cbc(pt);
assert_eq!(is_ecb(&ct), ecb)
}
#[test]
fn test_challenge_12() {
let unknown = base64_to_bytes(
&read_to_string("src/set2/challenge12.txt")
.unwrap()
.replace("\n", ""),
)
.unwrap();
let mut rng = rand::thread_rng();
let mut key = [0; 16];
rng.fill_bytes(&mut key);
let ecb_oracle = |pt: &[u8]| {
let pt = pad_pkcs7(&[pt, &unknown].concat(), 16);
let mut ct = vec![0; pt.len()];
ecb_encrypt(&key, &pt, &mut ct);
ct
};
// Discovering block size.
let mut block_size = 1;
let mut pt = vec![0, 0];
let mut ct = ecb_oracle(&pt);
while ct[0..block_size] != ct[block_size..2 * block_size] {
block_size += 1;
pt.push(0);
pt.push(0);
ct = ecb_oracle(&pt);
}
assert_eq!(block_size, 16);
let mut recovered = Vec::new();
loop {
let padding_len = (block_size - 1) - (recovered.len() % block_size);
let mut pt = Vec::with_capacity(padding_len + recovered.len() + 1 + padding_len);
pt.extend_from_slice(&vec![0; padding_len]);
pt.extend_from_slice(&recovered);
pt.push(0);
pt.extend_from_slice(&vec![0; padding_len]);
let byte_index = padding_len + recovered.len();
let end1 = padding_len + recovered.len() + 1;
let end2 = end1 + padding_len + recovered.len() + 1;
let byte = (0..=255).find(|&b| {
pt[byte_index] = b;
let ct = ecb_oracle(&pt);
ct[end1 - block_size..end1] == ct[end2 - block_size..end2]
});
if byte.is_some() {
recovered.push(byte.unwrap());
continue;
}
// This last byte will always be a padding byte.
recovered.pop();
break;
}
let recovered = String::from_utf8(recovered).unwrap();
assert!(recovered.starts_with("Rollin' in my 5.0\n"));
assert!(recovered.ends_with("Did you stop? No, I just drove by\n"));
}
#[test]
fn test_challenge_13() {
let map = parse_key_value("foo=bar&baz=qux&zap=zazzle");
assert_eq!(map.len(), 3);
assert_eq!(map.get("foo").unwrap(), "bar");
assert_eq!(map.get("baz").unwrap(), "qux");
assert_eq!(map.get("zap").unwrap(), "zazzle");
let mut rng = rand::thread_rng();
let mut key = [0; 16];
rng.fill_bytes(&mut key);
let encrypt = |pt: &[u8]| {
let pt = pad_pkcs7(pt, 16);
let mut ct = vec![0; pt.len()];
ecb_encrypt(&key, &pt, &mut ct);
ct
};
let decrypt = |ct: &[u8]| {
let mut pt = vec![0; ct.len()];
ecb_decrypt(&key, ct, &mut pt);
let pt = unpad_pkcs7(&pt, 16).unwrap();
parse_key_value(&String::from_utf8(pt).unwrap())
};
let ct1 = encrypt(
b"email=AAAAAAAAAAadmin\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b&uid=10&role=user",
);
let ct2 = encrypt(b"[email protected]&uid=10&role=user");
let ct = [&ct2[0..16], &ct2[16..32], &ct1[16..32]].concat();
let map = decrypt(&ct);
assert_eq!(map.len(), 3);
assert_eq!(map.get("email").unwrap(), "[email protected]");
assert_eq!(map.get("uid").unwrap(), "10");
assert_eq!(map.get("role").unwrap(), "admin");
}
#[test]
fn test_challenge_14() {
let unknown = base64_to_bytes(
&read_to_string("src/set2/challenge12.txt")
.unwrap()
.replace("\n", ""),
)
.unwrap();
let mut rng = rand::thread_rng();
let mut key = [0; 16];
rng.fill_bytes(&mut key);
let mut random_prefix = vec![0; rng.gen_range(0..16)];
rng.fill_bytes(&mut random_prefix);
let ecb_oracle_harder = |pt: &[u8]| {
let pt = pad_pkcs7(&[&random_prefix, pt, &unknown].concat(), 16);
let mut ct = vec![0; pt.len()];
ecb_encrypt(&key, &pt, &mut ct);
ct
};
// Assuming block size is already known.
let block_size = 16;
// Discovering random prefix length.
let check: &[u8] = &vec![1; 2 * block_size];
let mut prefix_padding = vec![0; block_size];
while prefix_padding.len() > 0 {
let pt = [&prefix_padding, check].concat();
let ct = ecb_oracle_harder(&pt);
if ct[block_size..2 * block_size] == ct[2 * block_size..3 * block_size] {
break;
}
prefix_padding.pop();
}
assert_eq!(random_prefix.len() + prefix_padding.len(), block_size);
let mut recovered = Vec::new();
loop {
let padding_len = (block_size - 1) - (recovered.len() % block_size);
let mut pt = Vec::with_capacity(
prefix_padding.len() + padding_len + recovered.len() + 1 + padding_len,
);
pt.extend_from_slice(&prefix_padding);
pt.extend_from_slice(&vec![0; padding_len]);
pt.extend_from_slice(&recovered);
pt.push(0);
pt.extend_from_slice(&vec![0; padding_len]);
let byte_index = prefix_padding.len() + padding_len + recovered.len();
let end1 = block_size + padding_len + recovered.len() + 1;
let end2 = end1 + padding_len + recovered.len() + 1;
let byte = (0..=255).find(|&b| {
pt[byte_index] = b;
let ct = ecb_oracle_harder(&pt);
ct[end1 - block_size..end1] == ct[end2 - block_size..end2]
});
if byte.is_some() {
recovered.push(byte.unwrap());
continue;
}
// This last byte will always be a padding byte.
recovered.pop();
break;
}
let recovered = String::from_utf8(recovered).unwrap();
assert!(recovered.starts_with("Rollin' in my 5.0\n"));
assert!(recovered.ends_with("Did you stop? No, I just drove by\n"));
}
#[test]
fn test_challenge_15() {
assert_eq!(
unpad_pkcs7(b"ICE ICE BABY\x04\x04\x04\x04", 16),
Some(Vec::from("ICE ICE BABY"))
);
assert_eq!(unpad_pkcs7(b"ICE ICE BABY\x05\x05\x05\x05", 16), None);
assert_eq!(unpad_pkcs7(b"ICE ICE BABY\x01\x02\x03\x04", 16), None);
}
#[test]
fn test_challenge_16() {
let prefix = b"comment1=cooking%20MCs;userdata=";
let suffix = b";comment2=%20like%20a%20pound%20of%20bacon";
let mut rng = rand::thread_rng();
let mut key = [0; 16];
rng.fill_bytes(&mut key);
let mut iv = [0; 16];
rng.fill_bytes(&mut iv);
let encrypt = |pt: &[u8]| {
if pt.contains(&b';') || pt.contains(&b'=') {
return None;
}
let pt = pad_pkcs7(&[prefix, pt, suffix].concat(), 16);
let mut ct = vec![0; pt.len()];
cbc_encrypt(&key, &iv, &pt, &mut ct);
Some(ct)
};
let decrypt = |ct: &[u8]| {
let mut pt = vec![0; ct.len()];
cbc_decrypt(&key, &iv, &ct, &mut pt);
let pt = unpad_pkcs7(&pt, 16).unwrap();
// We need from_utf8_lossy here because the second block will be scrambled.
String::from_utf8_lossy(&pt).contains(";admin=true;")
};
let mut ct = encrypt(b"?admin?true").unwrap();
let actual_pt = b"?admin?true;comm";
let target_pt = b";admin=true;comm";
xor(&mut ct[16..16 + actual_pt.len()], actual_pt);
xor(&mut ct[16..16 + target_pt.len()], target_pt);
assert!(decrypt(&ct));
}
}