From 82fde31b637e72d381857e3e2cb669298073edd1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 6 Apr 2015 01:26:20 -0400 Subject: real problem 22 --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/crack.rs | 15 ++++++++++++++- src/lib.rs | 2 ++ tests/lib.rs | 12 ++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a8904f1..c4ca95f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,7 @@ dependencies = [ "openssl 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -89,3 +90,12 @@ name = "rustc-serialize" version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "time" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + diff --git a/Cargo.toml b/Cargo.toml index 544ff50..bc2f6cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ authors = ["Jesse Luehrs "] rustc-serialize = "0.3" openssl = "0.5" rand = "0.3" +time = "0.1" diff --git a/src/crack.rs b/src/crack.rs index b351249..396eb6b 100644 --- a/src/crack.rs +++ b/src/crack.rs @@ -1,7 +1,7 @@ use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::collections::{HashMap, HashSet}; -use rand::SeedableRng; +use rand::{Rng, SeedableRng}; use data::ENGLISH_FREQUENCIES; use primitives::{fixed_xor, unpad_pkcs7, hamming, repeating_key_xor}; @@ -357,6 +357,19 @@ pub fn crack_fixed_nonce_ctr_statistically (input: Vec>) -> Vec> return plaintext_lines; } +pub fn recover_mersenne_twister_seed_from_time (output: u32) -> Option { + let now = ::time::now().to_timespec().sec as u32; + for i in -10000..10000i32 { + let seed = (now as i32).wrapping_add(i) as u32; + let mut mt = MersenneTwister::from_seed(seed); + let test_output: u32 = mt.gen(); + if test_output == output { + return Some(seed); + } + } + return None; +} + pub fn clone_mersenne_twister_from_output (outputs: &[u32]) -> MersenneTwister { fn untemper (val: u32) -> u32 { fn unxorshift (f: F, mut y: u32, n: usize, mask: u32) -> u32 where F: Fn(u32, usize) -> u32 { diff --git a/src/lib.rs b/src/lib.rs index bbaf93f..21bac75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate rustc_serialize as serialize; extern crate openssl; extern crate rand; +extern crate time; mod aes; mod base64; @@ -36,5 +37,6 @@ pub use crack::find_single_byte_xor_encrypted_string; pub use crack::crack_single_byte_xor; pub use crack::crack_repeating_key_xor; pub use crack::crack_fixed_nonce_ctr_statistically; +pub use crack::recover_mersenne_twister_seed_from_time; pub use crack::clone_mersenne_twister_from_output; pub use crack::recover_16_bit_mt19937_key; diff --git a/tests/lib.rs b/tests/lib.rs index da8516d..d38e3e3 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,6 +1,7 @@ extern crate matasano; extern crate rustc_serialize as serialize; extern crate rand; +extern crate time; use std::ascii::AsciiExt; use std::borrow::ToOwned; @@ -474,6 +475,17 @@ fn problem_21 () { assert_eq!(&got[..], &expected[..]); } +#[test] +fn problem_22 () { + // std::thread::sleep_ms(rand::thread_rng().gen_range(40, 1000) * 1000); + let seed = time::now().to_timespec().sec as u32; + let mut mt = matasano::MersenneTwister::from_seed(seed); + // std::thread::sleep_ms(rand::thread_rng().gen_range(40, 1000) * 1000); + let output: u32 = mt.gen(); + let got = matasano::recover_mersenne_twister_seed_from_time(output).unwrap(); + assert_eq!(got, seed); +} + #[test] fn problem_23 () { let mut mt: matasano::MersenneTwister = rand::thread_rng().gen(); -- cgit v1.2.3-54-g00ecf