Reformat Code

master
trivernis 5 years ago
parent d2eccf2aff
commit ca6cb3b21d

@ -1,14 +1,14 @@
use rand::Rng; use crate::lib::hash::{sha_checksum, PassKey};
use cfb_mode::stream_cipher::{NewStreamCipher, StreamCipher};
use cfb_mode::Cfb; use cfb_mode::Cfb;
use des::Des; use des::Des;
use cfb_mode::stream_cipher::{NewStreamCipher, StreamCipher}; use rand::Rng;
use rayon::prelude::*; use rayon::prelude::*;
use crate::lib::hash::{PassKey, sha_checksum};
type DesCfb = Cfb<Des>; type DesCfb = Cfb<Des>;
pub struct EncryptionKeys { pub struct EncryptionKeys {
current: u64 current: u64,
} }
impl Iterator for EncryptionKeys { impl Iterator for EncryptionKeys {
@ -27,9 +27,7 @@ impl Iterator for EncryptionKeys {
impl EncryptionKeys { impl EncryptionKeys {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self { current: 0 }
current: 0
}
} }
} }
@ -54,7 +52,11 @@ pub fn decrypt_data(data: &[u8], key: &[u8]) -> Vec<u8> {
} }
/// Decrypts data using a dictionary /// Decrypts data using a dictionary
pub fn decrypt_with_dictionary(data: &[u8], dict: Vec<PassKey>, checksum: &[u8]) -> Option<Vec<u8>> { pub fn decrypt_with_dictionary(
data: &[u8],
dict: Vec<PassKey>,
checksum: &[u8],
) -> Option<Vec<u8>> {
let pass = dict.par_iter().find_first(|(_pw, key)| { let pass = dict.par_iter().find_first(|(_pw, key)| {
let decrypted_data = decrypt_data(&data, key); let decrypted_data = decrypt_data(&data, key);
let decr_check = sha_checksum(&decrypted_data); let decr_check = sha_checksum(&decrypted_data);
@ -75,17 +77,19 @@ pub fn decrypt_with_dictionary(data: &[u8], dict: Vec<PassKey>, checksum: &[u8])
/// Decrypts data by generating all possible keys /// Decrypts data by generating all possible keys
pub fn decrypt_brute_brute_force(data: &[u8], checksum: &[u8]) -> Option<Vec<u8>> { pub fn decrypt_brute_brute_force(data: &[u8], checksum: &[u8]) -> Option<Vec<u8>> {
let encryption_key = (0u64..std::u64::MAX).into_par_iter().find_first(|num: &u64| { let encryption_key = (0u64..std::u64::MAX)
let key: &[u8] = &num.to_le_bytes(); .into_par_iter()
let decrypted_data = decrypt_data(&data, key); .find_first(|num: &u64| {
let decr_check = sha_checksum(&decrypted_data); let key: &[u8] = &num.to_le_bytes();
let decrypted_data = decrypt_data(&data, key);
let decr_check = sha_checksum(&decrypted_data);
if decr_check == checksum { if decr_check == checksum {
true true
} else { } else {
false false
} }
}); });
if let Some(num) = encryption_key { if let Some(num) = encryption_key {
let key: &[u8] = &num.to_le_bytes(); let key: &[u8] = &num.to_le_bytes();
println!("Key found: {:?}", key); println!("Key found: {:?}", key);

@ -1,5 +1,5 @@
use sha2::{Sha256, Digest};
use rayon::prelude::*; use rayon::prelude::*;
use sha2::{Digest, Sha256};
pub type PassKey = (String, Vec<u8>); pub type PassKey = (String, Vec<u8>);
@ -14,10 +14,13 @@ pub fn create_key(pw: String) -> Vec<u8> {
/// Maps a list of passwords to keys and returns a vector of pairs /// Maps a list of passwords to keys and returns a vector of pairs
pub fn map_to_keys(passwords: Vec<&String>) -> Vec<PassKey> { pub fn map_to_keys(passwords: Vec<&String>) -> Vec<PassKey> {
return passwords.par_iter().map(|pw| { return passwords
let pw_str = (*pw).clone(); .par_iter()
(pw_str.clone(), create_key(pw_str)) .map(|pw| {
}).collect(); let pw_str = (*pw).clone();
(pw_str.clone(), create_key(pw_str))
})
.collect();
} }
/// Creates a sha256 hashsum from the input data /// Creates a sha256 hashsum from the input data

@ -1,13 +1,16 @@
pub mod lib; pub mod lib;
use structopt::StructOpt;
use std::fs::File; use crate::lib::crypt::{
use std::io::{Read, Write}; decrypt_brute_brute_force, decrypt_data, decrypt_with_dictionary, encrypt_data,
use crate::lib::crypt::{encrypt_data, decrypt_data, decrypt_with_dictionary, decrypt_brute_brute_force}; };
use rpassword;
use rpassword::{read_password_from_tty};
use crate::lib::hash::{create_key, map_to_keys, sha_checksum, PassKey}; use crate::lib::hash::{create_key, map_to_keys, sha_checksum, PassKey};
use rayon::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use rayon::prelude::*;
use rpassword;
use rpassword::read_password_from_tty;
use std::fs::File;
use std::io::{Read, Write};
use structopt::StructOpt;
#[derive(StructOpt, Clone)] #[derive(StructOpt, Clone)]
#[structopt(name = "destools", version = "1.0", author = "Julius R.")] #[structopt(name = "destools", version = "1.0", author = "Julius R.")]
@ -18,7 +21,6 @@ struct Opts {
#[derive(StructOpt, Clone)] #[derive(StructOpt, Clone)]
enum SubCommand { enum SubCommand {
/// Encrypt a file with des /// Encrypt a file with des
#[structopt(name = "encrypt")] #[structopt(name = "encrypt")]
Encrypt(Encrypt), Encrypt(Encrypt),
@ -29,7 +31,7 @@ enum SubCommand {
/// Create a dictionary rainbow-table from a txt file /// Create a dictionary rainbow-table from a txt file
#[structopt(name = "create-dictionary")] #[structopt(name = "create-dictionary")]
CreateDictionary(CreateDictionary) CreateDictionary(CreateDictionary),
} }
#[derive(StructOpt, Clone)] #[derive(StructOpt, Clone)]
@ -65,7 +67,7 @@ struct Decrypt {
/// The file needs to be in a csv format with calculated password hashes. /// The file needs to be in a csv format with calculated password hashes.
/// The hashes can be calculated with the create-dictionary subcommand from a txt file. /// The hashes can be calculated with the create-dictionary subcommand from a txt file.
#[structopt(short = "d", long = "dictionary")] #[structopt(short = "d", long = "dictionary")]
dictionary: Option<String> dictionary: Option<String>,
} }
#[derive(StructOpt, Clone)] #[derive(StructOpt, Clone)]
@ -122,13 +124,16 @@ fn decrypt(_opts: &Opts, args: &Decrypt) {
let dictionary = read_file(dict); let dictionary = read_file(dict);
let lines = dictionary.lines().collect::<Vec<&str>>(); let lines = dictionary.lines().collect::<Vec<&str>>();
let pw_table: Vec<PassKey> = lines.par_iter().map(|line| { let pw_table: Vec<PassKey> = lines
let parts: Vec<&str> = line.split(",").collect::<Vec<&str>>(); .par_iter()
let pw = parts[0].parse().unwrap(); .map(|line| {
let key_str: String = parts[1].parse().unwrap(); let parts: Vec<&str> = line.split(",").collect::<Vec<&str>>();
let key = base64::decode(&key_str).unwrap(); let pw = parts[0].parse().unwrap();
(pw, key) let key_str: String = parts[1].parse().unwrap();
}).collect(); let key = base64::decode(&key_str).unwrap();
(pw, key)
})
.collect();
println!("Starting multithreaded decryption..."); println!("Starting multithreaded decryption...");
if let Some(dec_data) = decrypt_with_dictionary(&data, pw_table, &data_checksum) { if let Some(dec_data) = decrypt_with_dictionary(&data, pw_table, &data_checksum) {
@ -156,14 +161,15 @@ fn decrypt(_opts: &Opts, args: &Decrypt) {
/// Creates a dictionary from an input file and writes it to the output file /// Creates a dictionary from an input file and writes it to the output file
fn create_dictionary(_opts: &Opts, args: &CreateDictionary) { fn create_dictionary(_opts: &Opts, args: &CreateDictionary) {
let input = (*args.input).parse().unwrap(); let input = (*args.input).parse().unwrap();
let contents = read_file(input); let contents = read_file(input);
let lines = contents.lines().collect::<Vec<&str>>(); let lines = contents.lines().collect::<Vec<&str>>();
println!("Parsing {} passwords...", lines.len()); println!("Parsing {} passwords...", lines.len());
let pws: Vec<String> = lines.par_iter().map(| s | -> String { let pws: Vec<String> = lines
s.parse().unwrap() .par_iter()
}).collect(); .map(|s| -> String { s.parse().unwrap() })
.collect();
println!("Removing duplicates..."); println!("Removing duplicates...");
let passwords = pws.iter().unique().collect_vec(); let passwords = pws.iter().unique().collect_vec();
println!("Mapping passwords to keys..."); println!("Mapping passwords to keys...");
@ -190,7 +196,7 @@ fn read_file_binary(filename: String) -> Vec<u8> {
/// Reads a file to the end and returns the contents as a string /// Reads a file to the end and returns the contents as a string
fn read_file(filename: String) -> String { fn read_file(filename: String) -> String {
let mut fin = File::open(filename).unwrap(); let mut fin = File::open(filename).unwrap();
let mut contents= String::new(); let mut contents = String::new();
fin.read_to_string(&mut contents).unwrap(); fin.read_to_string(&mut contents).unwrap();
return contents; return contents;
} }

Loading…
Cancel
Save