Created
January 9, 2023 12:54
-
-
Save unfo/1a9c2ec02ea9acba4c98f0eaaf949c7a to your computer and use it in GitHub Desktop.
brute-force-counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use counter::Counter; | |
use itertools::Itertools; | |
fn main() { | |
let pw = String::from("lW2jYRI02ZKDBb9VtQBU1f6eDRo6WEj9"); | |
let lower = (b'a' ..= b'z').map(char::from).collect::<Vec<_>>(); | |
let upper = (b'A' ..= b'Z').map(char::from).collect::<Vec<_>>(); | |
let num = (b'0' ..= b'9').map(char::from).collect::<Vec<_>>(); | |
let mut pw_space = lower.clone(); | |
pw_space.extend(upper); | |
pw_space.extend(num); | |
let freqs = pw.chars().collect::<Counter<_>>(); | |
let freq_map = freqs.into_map(); | |
// dbg!(&freq_map); | |
let mut freq_counter = freq_map.clone(); | |
let keyspace = freq_map.keys().clone().sorted(); | |
const PASSWORD_LENGTH: usize = 32; | |
// naive case | |
let mut counter = 0; | |
let mut found_pw: Vec<char> = vec![' '; PASSWORD_LENGTH]; | |
for perm in pw_space.iter() | |
.combinations_with_replacement(PASSWORD_LENGTH) { | |
counter += 1; | |
if counter > 1_000_000 { | |
println!("Took more than 1M tries, skipping for now, adjust code if needed"); | |
break; | |
} | |
if counter % 100_000 == 0 { | |
println!("{}: {}", counter, perm.clone().into_iter().collect::<String>()); | |
} | |
let mut found = true; | |
for i in 0..PASSWORD_LENGTH { | |
found = found & (*perm[i] == pw.chars().nth(i).unwrap()); | |
} | |
if found { | |
for (i, c) in perm.iter().enumerate() { | |
found_pw[i] = **c; | |
} | |
println!("FOUND THE PASSWORD"); | |
break; | |
} | |
} | |
let mut pw_fmt = String::from_iter(found_pw); | |
println!("Naive: {counter} requests to find the password {pw_fmt}"); | |
counter = 0; | |
found_pw = vec![' '; PASSWORD_LENGTH]; | |
for (index, c) in pw.chars().enumerate() { | |
for pw in &pw_space { | |
counter += 1; | |
if c == *pw { | |
found_pw[index] = *pw; | |
break; | |
} | |
} | |
} | |
pw_fmt = String::from_iter(found_pw); | |
println!("Char-by-char: {counter} requests to find the password {pw_fmt}"); | |
counter = 0; | |
found_pw = vec![' '; PASSWORD_LENGTH]; | |
for (index, c) in pw.chars().enumerate() { | |
for pw in keyspace.clone() { | |
counter += 1; | |
if c == *pw { | |
found_pw[index] = *pw; | |
break; | |
} | |
} | |
} | |
pw_fmt = String::from_iter(found_pw); | |
println!("PW Space: {counter} requests to find the password {pw_fmt}"); | |
counter = 0; | |
found_pw = vec![' '; PASSWORD_LENGTH]; | |
for (index, c) in pw.chars().enumerate() { | |
for pw in keyspace.clone() { | |
if *freq_counter.get(pw).unwrap() > 0 { | |
counter += 1; | |
if c == *pw { | |
found_pw[index] = *pw; | |
freq_counter.entry(*pw).and_modify(|k| (*k) -= 1 ); | |
break; | |
} | |
} | |
} | |
} | |
pw_fmt = String::from_iter(found_pw); | |
println!("PW Space with freq: {counter} requests to find the password {pw_fmt}"); | |
counter = 0; | |
found_pw = vec![' '; PASSWORD_LENGTH]; | |
freq_counter = freq_map.clone(); | |
for pc in keyspace.clone() { | |
for (index, c) in pw.chars().enumerate() { | |
if *freq_counter.get(pc).unwrap() > 0 { | |
if found_pw[index] != ' ' { | |
continue; | |
} | |
counter += 1; | |
if c == *pc { | |
found_pw[index] = (*pc).clone(); | |
freq_counter.entry(*pc).and_modify(|k| (*k) -= 1 ); | |
} | |
} | |
} | |
} | |
pw_fmt = String::from_iter(found_pw); | |
println!("PW Space with freq by index: {counter} requests to find the password {pw_fmt}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment