summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-04-06 01:26:20 -0400
committerJesse Luehrs <doy@tozt.net>2015-04-06 01:26:20 -0400
commit82fde31b637e72d381857e3e2cb669298073edd1 (patch)
tree69153e3b49c06584c15e895787e15c6e8c8b71d8
parent3dbdba1a7f5f0094c1d3075b3ca91b07196fcbf2 (diff)
downloadmatasano-82fde31b637e72d381857e3e2cb669298073edd1.tar.gz
matasano-82fde31b637e72d381857e3e2cb669298073edd1.zip
real problem 22
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--src/crack.rs15
-rw-r--r--src/lib.rs2
-rw-r--r--tests/lib.rs12
5 files changed, 39 insertions, 1 deletions
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 <doy@tozt.net>"]
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<u8>>) -> Vec<Vec<u8>>
return plaintext_lines;
}
+pub fn recover_mersenne_twister_seed_from_time (output: u32) -> Option<u32> {
+ 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: 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;
@@ -475,6 +476,17 @@ fn problem_21 () {
}
#[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();
let outputs: Vec<u32> = mt.gen_iter().take(624).collect();