From a377ed0d62a6bc8e93423ca00c3f556f29ea37cc Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 17:54:19 +0100 Subject: [PATCH 01/11] Added hashdecoder using our own AWS database, sends a GET request with the text as it's JSON payload (hash) and gets returned the approprite plaintext or None if its not in the database --- Cargo.toml | 4 + src/decoders/hash_decoder.rs | 149 +++++++++++++++++++++++++++++++++++ src/decoders/mod.rs | 1 + 3 files changed, 154 insertions(+) create mode 100644 src/decoders/hash_decoder.rs diff --git a/Cargo.toml b/Cargo.toml index 9fd094f8..300ee3c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,10 @@ lemmeknow = { version = "0.5", default-features = false } include_dir = "0.7.2" once_cell = "1.13.0" text_io = "0.1.10" +reqwest = { version = "0.11", features = ["blocking", "json"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +syn = "1.0.40" [profile.release] lto = "fat" diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs new file mode 100644 index 00000000..f9e40f6e --- /dev/null +++ b/src/decoders/hash_decoder.rs @@ -0,0 +1,149 @@ +use std::collections::HashMap; +use serde_json::{Result, Value}; +use log::{debug, info, trace}; + +use super::interface::Decoder; +use super::interface::Crack; + +use crate::decoders::interface::check_string_success; + +use serde::Serialize; +use serde::Deserialize; + +pub struct HashDecoder; + +impl Crack for Decoder{ + fn new() -> Decoder { + Decoder { + name: "hash", + description: "XXXXXXXXXXXXXXXX", + link: "XXXXXXXXXXXXXXXXXXXX", + tags: vec!["hash", "decoder"], + expected_runtime: 0.01, + expected_success: 1.0, + failure_runtime: 0.01, + normalised_entropy: vec![1.0, 10.0], + popularity: 1.0, + phantom: std::marker::PhantomData, + } + } + + fn crack(&self, text: &str) -> Option { + trace!("Trying hash with text {:?}", text); + + let decoded_text = decode_hash_no_error_handling(text); + + if decoded_text.is_none() { + debug!("Failed to decode hash because HashDecoder::decode_hash_no_error_handling returned None"); + return None; + } + + println!("{}", decoded_text.as_ref().unwrap()); + + decoded_text + } +} + +fn decode_hash_no_error_handling(text: &str) -> Option { + // Runs the code to decode hash + // Doesn't perform error handling + + #[derive(Serialize)] + #[serde(rename_all = "PascalCase")] + struct PostRequest<'a> { + hash: &'a str, + } + + // .json(&PostRequest{ hash: text }) + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "camelCase")] + struct Data { + status_code: u16, + body: std::collections::HashMap, + } + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "PascalCase")] + struct Body { + #[serde(rename = "Type")] + type_: String, + plaintext: String, + hash: String, + verified: bool, + } + + let mut data = HashMap::new(); + data.insert("Hash", [&text]); + + let client = reqwest::blocking::Client::new(); + let resp = client + .get("https://av5b81zg3k.execute-api.us-east-2.amazonaws.com/prod/lookup") + .json(&data) + .send() + .unwrap(); + + println!("{resp:?}"); + + return match resp.status() { + reqwest::StatusCode::OK => { + let mut hash_out: Data = resp.json().ok()?; + + if let Some(result) = hash_out.body.remove(text) { + Some(result.plaintext) + } else { + None + } + }, + _ => { + None + } + } +} + + +#[cfg(test)] +mod tests { + use super::HashDecoder; + use crate::decoders::interface::{Crack, Decoder}; + + #[test] + fn successful_decoding() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("098f6bcd4621d373cade4e832627b4f6").unwrap(); + + assert_eq!(result, "test"); + } + + #[test] + fn hash_decode_handles_panics() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("hello my name is panicky mc panic face!"); + if result.is_some() { + panic!("Decode_base64 did not return an option with Some.") + } else { + // If we get here, the test passed + // Because the base64_decoder.crack function returned None + // as it should do for the input + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_empty_string() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack(""); + if result.is_some() { + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_emoji() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("😂"); + if result.is_some() { + assert_eq!(true, true); + } + } +} \ No newline at end of file diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index 58758395..3a599b86 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -8,3 +8,4 @@ pub mod base64_decoder; pub mod interface; pub mod reverse_decoder; +pub mod hash_decoder; \ No newline at end of file From f8a0456b7fb01f5e38f509aa05fbaebe0682d18d Mon Sep 17 00:00:00 2001 From: Jayy001 <56607897+Jayy001@users.noreply.github.com> Date: Sun, 24 Jul 2022 17:58:56 +0100 Subject: [PATCH 02/11] Removed irrelevant return statement --- src/decoders/hash_decoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index f9e40f6e..baa1fba6 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -85,7 +85,7 @@ fn decode_hash_no_error_handling(text: &str) -> Option { println!("{resp:?}"); - return match resp.status() { + match resp.status() { reqwest::StatusCode::OK => { let mut hash_out: Data = resp.json().ok()?; @@ -146,4 +146,4 @@ mod tests { assert_eq!(true, true); } } -} \ No newline at end of file +} From 951a5706ccd5b67ab3de0665df0449605b941eb6 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 17:54:19 +0100 Subject: [PATCH 03/11] Cleanedup code with clippy and fmt, also added description and link --- Cargo.toml | 4 + src/decoders/hash_decoder.rs | 148 +++++++++++++++++++++++++++++++++++ src/decoders/mod.rs | 1 + 3 files changed, 153 insertions(+) create mode 100644 src/decoders/hash_decoder.rs diff --git a/Cargo.toml b/Cargo.toml index 9fd094f8..300ee3c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,10 @@ lemmeknow = { version = "0.5", default-features = false } include_dir = "0.7.2" once_cell = "1.13.0" text_io = "0.1.10" +reqwest = { version = "0.11", features = ["blocking", "json"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +syn = "1.0.40" [profile.release] lto = "fat" diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs new file mode 100644 index 00000000..704c5fd0 --- /dev/null +++ b/src/decoders/hash_decoder.rs @@ -0,0 +1,148 @@ +use log::{debug, info, trace}; +use serde_json::{Result, Value}; +use std::collections::HashMap; + +use super::interface::Crack; +use super::interface::Decoder; + +use crate::decoders::interface::check_string_success; + +use serde::Deserialize; +use serde::Serialize; + +pub struct HashDecoder; + +impl Crack for Decoder { + fn new() -> Decoder { + Decoder { + name: "hash", + description: "A cryptographic hash function is a hash function which takes an input (or 'message') and returns a fixed-size string of bytes. The string is called the 'hash value', 'message digest', 'digital fingerprint', 'digest' or 'checksum'.", + link: "https://en.wikipedia.org/wiki/Hash_function", + tags: vec!["hash", "decoder"], + expected_runtime: 0.01, + expected_success: 1.0, + failure_runtime: 0.01, + normalised_entropy: vec![1.0, 10.0], + popularity: 1.0, + phantom: std::marker::PhantomData, + } + } + + fn crack(&self, text: &str) -> Option { + trace!("Trying hash with text {:?}", text); + + let decoded_text = decode_hash_no_error_handling(text); + + if decoded_text.is_none() { + debug!("Failed to decode hash because HashDecoder::decode_hash_no_error_handling returned None"); + return None; + } + + println!("{}", decoded_text.as_ref().unwrap()); + + decoded_text + } +} + +fn decode_hash_no_error_handling(text: &str) -> Option { + // Runs the code to decode hash + // Doesn't perform error handling + + #[derive(Serialize)] + #[serde(rename_all = "PascalCase")] + struct PostRequest<'a> { + hash: &'a str, + } + + // .json(&PostRequest{ hash: text }) + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "camelCase")] + struct Data { + status_code: u16, + body: std::collections::HashMap, + } + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "PascalCase")] + struct Body { + #[serde(rename = "Type")] + type_: String, + plaintext: String, + hash: String, + verified: bool, + } + + let mut data = HashMap::new(); + data.insert("Hash", [&text]); + + let client = reqwest::blocking::Client::new(); + let resp = client + .get("https://av5b81zg3k.execute-api.us-east-2.amazonaws.com/prod/lookup") + .json(&data) + .send() + .unwrap(); + + println!("{resp:?}"); + + match resp.status() { + reqwest::StatusCode::OK => { + let mut hash_out: Data = resp.json().ok()?; + + if let Some(result) = hash_out.body.remove(text) { + Some(result.plaintext) + } else { + None + } + } + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::HashDecoder; + use crate::decoders::interface::{Crack, Decoder}; + + #[test] + fn successful_decoding() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder + .crack("098f6bcd4621d373cade4e832627b4f6") + .unwrap(); + + assert_eq!(result, "test"); + } + + #[test] + fn hash_decode_handles_panics() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("hello my name is panicky mc panic face!"); + if result.is_some() { + panic!("Decode_base64 did not return an option with Some.") + } else { + // If we get here, the test passed + // Because the base64_decoder.crack function returned None + // as it should do for the input + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_empty_string() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack(""); + if result.is_some() { + assert_eq!(true, true); + } + } + + #[test] + fn hash_handle_panic_if_emoji() { + let hash_decoder = Decoder::::new(); + let result = hash_decoder.crack("😂"); + if result.is_some() { + assert_eq!(true, true); + } + } +} diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index 58758395..191b976d 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -6,5 +6,6 @@ //! you will also need to make it a public module in this file. pub mod base64_decoder; +pub mod hash_decoder; pub mod interface; pub mod reverse_decoder; From 2cce455711fc4af311e5d59ea24ccbe8daffca64 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 18:17:52 +0100 Subject: [PATCH 04/11] Remove duplicate `pub mod` line in `mods.rs` --- src/decoders/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index 67505298..bf5afe0d 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -8,5 +8,4 @@ pub mod base64_decoder; pub mod hash_decoder; pub mod interface; -pub mod reverse_decoder; -pub mod hash_decoder; \ No newline at end of file +pub mod reverse_decoder; \ No newline at end of file From de8590319a12cc0a3a2dd80fd8ee2f7327e1b72a Mon Sep 17 00:00:00 2001 From: bee Date: Sun, 24 Jul 2022 20:03:57 +0100 Subject: [PATCH 05/11] Ran cargo fmt --- src/decoders/mod.rs | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index bf5afe0d..191b976d 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -8,4 +8,4 @@ pub mod base64_decoder; pub mod hash_decoder; pub mod interface; -pub mod reverse_decoder; \ No newline at end of file +pub mod reverse_decoder; diff --git a/src/lib.rs b/src/lib.rs index d278c366..5999928e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ //! Ares is an automatic decoding and cracking tool. mod checkers; -mod decoders; +pub mod decoders; mod filtration_system; mod searchers; mod storage; From 6f2e65facfc8906f971391adeaaaaa9430bff36d Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 20:05:09 +0100 Subject: [PATCH 06/11] Remove duplicate `pub mod` line in `mods.rs` --- src/decoders/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index bf5afe0d..191b976d 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -8,4 +8,4 @@ pub mod base64_decoder; pub mod hash_decoder; pub mod interface; -pub mod reverse_decoder; \ No newline at end of file +pub mod reverse_decoder; From ff974bace54430721b2bdf9d32050f92f67d2e83 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 20:08:57 +0100 Subject: [PATCH 07/11] Updated runtime values --- src/decoders/hash_decoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index 704c5fd0..233dd319 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -19,9 +19,9 @@ impl Crack for Decoder { description: "A cryptographic hash function is a hash function which takes an input (or 'message') and returns a fixed-size string of bytes. The string is called the 'hash value', 'message digest', 'digital fingerprint', 'digest' or 'checksum'.", link: "https://en.wikipedia.org/wiki/Hash_function", tags: vec!["hash", "decoder"], - expected_runtime: 0.01, + expected_runtime: 1.0, expected_success: 1.0, - failure_runtime: 0.01, + failure_runtime: 1.0, normalised_entropy: vec![1.0, 10.0], popularity: 1.0, phantom: std::marker::PhantomData, From 75d8dcc45ca5a7925b1527a6456ffcd911f739de Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 20:15:29 +0100 Subject: [PATCH 08/11] Fixed clippy --- src/decoders/hash_decoder.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index 233dd319..f41fdff9 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -1,11 +1,11 @@ -use log::{debug, info, trace}; -use serde_json::{Result, Value}; +use log::{debug, trace}; + use std::collections::HashMap; use super::interface::Crack; use super::interface::Decoder; -use crate::decoders::interface::check_string_success; + use serde::Deserialize; use serde::Serialize; @@ -56,21 +56,21 @@ fn decode_hash_no_error_handling(text: &str) -> Option { // .json(&PostRequest{ hash: text }) - #[derive(Deserialize, Debug)] + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Data { - status_code: u16, + // status_code: u16, body: std::collections::HashMap, } - #[derive(Deserialize, Debug)] + #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct Body { #[serde(rename = "Type")] - type_: String, + // type_: String, plaintext: String, - hash: String, - verified: bool, + // hash: String, + // verified: bool, } let mut data = HashMap::new(); From 94997ad1da061dc8fa0a624755da9316c8ca1d46 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 24 Jul 2022 20:40:28 +0100 Subject: [PATCH 09/11] Fixed clippy errors --- src/decoders/hash_decoder.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index f41fdff9..c3c5bca2 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -5,8 +5,6 @@ use std::collections::HashMap; use super::interface::Crack; use super::interface::Decoder; - - use serde::Deserialize; use serde::Serialize; @@ -59,18 +57,23 @@ fn decode_hash_no_error_handling(text: &str) -> Option { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Data { - // status_code: u16, + #[allow(unused)] + status_code: u16, body: std::collections::HashMap, } #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] + #[allow(unused)] struct Body { #[serde(rename = "Type")] - // type_: String, + #[allow(unused)] + r#type: String, plaintext: String, - // hash: String, - // verified: bool, + #[allow(unused)] + hash: String, + #[allow(unused)] + verified: bool, } let mut data = HashMap::new(); From 086e8340965d4a7d238c79c173c665fbd465d53d Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 25 Jul 2022 10:11:48 +0100 Subject: [PATCH 10/11] Cleaned up code --- src/decoders/hash_decoder.rs | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index c3c5bca2..51ff2eeb 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -6,7 +6,6 @@ use super::interface::Crack; use super::interface::Decoder; use serde::Deserialize; -use serde::Serialize; pub struct HashDecoder; @@ -46,34 +45,21 @@ fn decode_hash_no_error_handling(text: &str) -> Option { // Runs the code to decode hash // Doesn't perform error handling - #[derive(Serialize)] - #[serde(rename_all = "PascalCase")] - struct PostRequest<'a> { - hash: &'a str, - } - - // .json(&PostRequest{ hash: text }) - #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Data { - #[allow(unused)] - status_code: u16, + // status_code: u16, body: std::collections::HashMap, } #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] - #[allow(unused)] struct Body { - #[serde(rename = "Type")] - #[allow(unused)] - r#type: String, + // #[serde(rename = "Type")] + // r#type: String, plaintext: String, - #[allow(unused)] - hash: String, - #[allow(unused)] - verified: bool, + // hash: String, + // verified: bool, } let mut data = HashMap::new(); From 8f7ad2405625c3b8bcf6af7bd29c12b216ae64a2 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 25 Jul 2022 10:37:30 +0100 Subject: [PATCH 11/11] Remove irrelevant comments --- src/decoders/hash_decoder.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/decoders/hash_decoder.rs b/src/decoders/hash_decoder.rs index 51ff2eeb..06b11f5a 100644 --- a/src/decoders/hash_decoder.rs +++ b/src/decoders/hash_decoder.rs @@ -16,11 +16,11 @@ impl Crack for Decoder { description: "A cryptographic hash function is a hash function which takes an input (or 'message') and returns a fixed-size string of bytes. The string is called the 'hash value', 'message digest', 'digital fingerprint', 'digest' or 'checksum'.", link: "https://en.wikipedia.org/wiki/Hash_function", tags: vec!["hash", "decoder"], - expected_runtime: 1.0, - expected_success: 1.0, - failure_runtime: 1.0, + expected_runtime: 0.8, + expected_success: 0.8, + failure_runtime: 0.8, normalised_entropy: vec![1.0, 10.0], - popularity: 1.0, + popularity: 0.4, phantom: std::marker::PhantomData, } } @@ -48,18 +48,13 @@ fn decode_hash_no_error_handling(text: &str) -> Option { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct Data { - // status_code: u16, body: std::collections::HashMap, } #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct Body { - // #[serde(rename = "Type")] - // r#type: String, plaintext: String, - // hash: String, - // verified: bool, } let mut data = HashMap::new();