-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `hash` module implements hashing functions `md5`, `sha1` and `sha256`.
- Loading branch information
Showing
7 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use std::cell::RefCell; | ||
|
||
use md5 as md5_hash; | ||
use rustc_hash::FxHashMap; | ||
use sha1 as sha1_hash; | ||
use sha1::Digest; | ||
use sha256::digest as sha256_digest; | ||
|
||
use crate::modules::prelude::*; | ||
use crate::modules::protos::hash::*; | ||
|
||
thread_local!( | ||
static SHA256_CACHE: RefCell<FxHashMap<(i64, i64), String>> = | ||
RefCell::new(FxHashMap::default()); | ||
|
||
static SHA1_CACHE: RefCell<FxHashMap<(i64, i64), String>> = | ||
RefCell::new(FxHashMap::default()); | ||
|
||
static MD5_CACHE: RefCell<FxHashMap<(i64, i64), String>> = | ||
RefCell::new(FxHashMap::default()); | ||
); | ||
|
||
#[module_main] | ||
fn main(_ctx: &ScanContext) -> Hash { | ||
// With every scanned file the cache must be cleared. | ||
SHA256_CACHE.with(|cache| cache.borrow_mut().clear()); | ||
SHA1_CACHE.with(|cache| cache.borrow_mut().clear()); | ||
MD5_CACHE.with(|cache| cache.borrow_mut().clear()); | ||
|
||
Hash::new() | ||
} | ||
|
||
#[module_export(name = "md5")] | ||
fn md5( | ||
ctx: &mut ScanContext, | ||
offset: i64, | ||
size: i64, | ||
) -> Option<RuntimeString> { | ||
let cached = MD5_CACHE.with(|cache| -> Option<RuntimeString> { | ||
Some(RuntimeString::from_bytes( | ||
ctx, | ||
cache.borrow().get(&(offset, size))?, | ||
)) | ||
}); | ||
|
||
if cached.is_some() { | ||
return cached; | ||
} | ||
|
||
let range = offset.try_into().ok()?..(offset + size).try_into().ok()?; | ||
let data = ctx.scanned_data().get(range)?; | ||
let digest = format!("{:x}", md5_hash::compute(data)); | ||
let result = RuntimeString::from_bytes(ctx, digest.as_bytes()); | ||
|
||
MD5_CACHE.with(|cache| { | ||
cache.borrow_mut().insert((offset, size), digest); | ||
}); | ||
|
||
Some(result) | ||
} | ||
|
||
#[module_export(name = "md5")] | ||
fn md5_str(ctx: &mut ScanContext, s: RuntimeString) -> Option<RuntimeString> { | ||
Some(RuntimeString::from_bytes( | ||
ctx, | ||
format!("{:x}", md5_hash::compute(s.as_bstr(ctx))), | ||
)) | ||
} | ||
|
||
#[module_export(name = "sha1")] | ||
fn sha1( | ||
ctx: &mut ScanContext, | ||
offset: i64, | ||
size: i64, | ||
) -> Option<RuntimeString> { | ||
let cached = SHA1_CACHE.with(|cache| -> Option<RuntimeString> { | ||
Some(RuntimeString::from_bytes( | ||
ctx, | ||
cache.borrow().get(&(offset, size))?, | ||
)) | ||
}); | ||
|
||
if cached.is_some() { | ||
return cached; | ||
} | ||
|
||
let range = offset.try_into().ok()?..(offset + size).try_into().ok()?; | ||
let data = ctx.scanned_data().get(range)?; | ||
let mut hasher = sha1_hash::Sha1::new(); | ||
|
||
hasher.update(data); | ||
|
||
let digest = format!("{:x}", hasher.finalize()); | ||
let result = RuntimeString::from_bytes(ctx, digest.as_bytes()); | ||
|
||
SHA1_CACHE.with(|cache| { | ||
cache.borrow_mut().insert((offset, size), digest); | ||
}); | ||
|
||
Some(result) | ||
} | ||
|
||
#[module_export(name = "sha1")] | ||
fn sha1_str(ctx: &mut ScanContext, s: RuntimeString) -> Option<RuntimeString> { | ||
let mut hasher = sha1_hash::Sha1::new(); | ||
hasher.update(s.as_bstr(ctx)); | ||
|
||
Some(RuntimeString::from_bytes(ctx, format!("{:x}", hasher.finalize()))) | ||
} | ||
|
||
#[module_export(name = "sha256")] | ||
fn sha256( | ||
ctx: &mut ScanContext, | ||
offset: i64, | ||
size: i64, | ||
) -> Option<RuntimeString> { | ||
let cached = SHA256_CACHE.with(|cache| -> Option<RuntimeString> { | ||
Some(RuntimeString::from_bytes( | ||
ctx, | ||
cache.borrow().get(&(offset, size))?, | ||
)) | ||
}); | ||
|
||
if cached.is_some() { | ||
return cached; | ||
} | ||
|
||
let range = offset.try_into().ok()?..(offset + size).try_into().ok()?; | ||
let data = ctx.scanned_data().get(range)?; | ||
let digest = sha256_digest(data); | ||
let result = RuntimeString::from_bytes(ctx, digest.as_bytes()); | ||
|
||
SHA256_CACHE.with(|cache| { | ||
cache.borrow_mut().insert((offset, size), digest); | ||
}); | ||
|
||
Some(result) | ||
} | ||
|
||
#[module_export(name = "sha256")] | ||
fn sha256_str( | ||
ctx: &mut ScanContext, | ||
s: RuntimeString, | ||
) -> Option<RuntimeString> { | ||
Some(RuntimeString::from_bytes( | ||
ctx, | ||
sha256_digest(s.as_bstr(ctx).as_bytes()), | ||
)) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto2"; | ||
|
||
import "yara.proto"; | ||
|
||
option (yara.module_options) = { | ||
name : "hash" | ||
root_message: "Hash" | ||
rust_module: "hash" | ||
}; | ||
|
||
message Hash { | ||
// This module contains only exported functions, and doesn't return any data | ||
} |
This file contains 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