967e0c70272cd8a18d72f34df8254fed7361d170
1 #![feature(macro_rules)]
4 extern crate serialize
;
9 use end_point
::{ Client
, Server
};
16 const PORT
: u16 = 4221;
20 r
"{} [genkey | tests | oracle-weak | oracle-fixed]
21 genkey: Generate a 256 bits key
22 tests: launch some tests between a client and a weak server
23 oracle-weak: launch a padding oracle attack against a weak server
24 oracle-fixed: launch a padding oracle attack against a fixed server",
29 fn do_oracle_attack(address
: &str, variant
: packet
::Variant
) {
30 // 16 bytes encrypted data from 'Packet::random_packet_data([4])'.
31 let cipher_block
: [u8, ..16] = [254, 9, 228, 149, 60, 42, 165, 34, 233, 75, 112, 57, 37, 9, 116, 103]; // Known by the attacker.
32 let xor_operand
: [u8, ..16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3]; // This is the IV or the previous 16 bytes cipherblock. In our case we took the IV.
34 let expected_clear_block
: [u8, ..16] = [44, 92, 31, 98, 220, 84, 226, 53, 58, 94, 45, 25, 242, 6, 199, 1]; // To be found by the attacker.
36 match oracle_machine
::decipher(address
, PORT
, &xor_operand
, &cipher_block
, variant
) {
37 Some(ref deciphered
) if deciphered
.as_slice() == expected_clear_block
=> {
38 println!("The oracle machine has found the clear block!:");
39 println!(" Expected block: {}", expected_clear_block
.to_vec());
40 println!(" Decrypted block: {}", deciphered
)
43 println!("The oracle machine hasn't found the clear block: {}", other
),
45 println!("The oracle machine hasn't found the clear block"),
59 let args
= os
::args();
61 if args
.iter().any(|a
| a
.as_slice() == "--help" || a
.as_slice() == "-h") {
68 match args
[1].as_slice() {
71 "oracle-weak" => OracleWeak
,
72 "oracle-fixed" => OracleFixed
,
82 Help
=> print_usage(),
84 match crypto
::generate_key(256 / 8) {
85 Ok(key
) => println!("key: {}", key
),
86 Err(e
) => println!("Unable to generate a key. Error: {}", e
)
90 println!("Starting server on [{}]:{}...", address
, PORT
);
92 match Server
::new(address
, PORT
, match mode
{ OracleFixed
=> packet
::Fixed
, _
=> packet
::Weak
}) {
94 println!("Server started");
97 Tests
=> Client
::start_tests(address
, PORT
, packet
::Weak
),
98 OracleWeak
=> do_oracle_attack(address
, packet
::Weak
),
99 OracleFixed
=> do_oracle_attack(address
, packet
::Fixed
),
101 println!("Press any key to quit");
102 io
::stdin().read_line().ok().expect("Failed to read line");
106 server
.close().ok().expect("Failed to close the server");
109 println!("Unable to create a new server. Error: {}", e
)