-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to set module data in scan callback
Some modules in YARA need to be fed data to be usable, notably the cuckoo module. This works by setting the module data in the "import module" callback, as can be seen here: <https://github.com/VirusTotal/yara/blob/923368eab/cli/yara.c#L1200> This MR adds bindings to be able to do exactly this: the object related to this callback msg is wrapped in a YrModuleImport object, which exposes two functions: - one to retrieve the module name - one to set the module data This makes the code looks like this: ```rust let res = yara_scanner.scan_mem_callback(b"", |msg| { if let yara::CallbackMsg::ImportModule(mut module) = msg { if module.name() == Some(b"cuckoo") { // Safety: report is alive for longer than the scan. unsafe { module.set_module_data( report.as_mut_ptr().cast(), report.len() as u64, ); } } } yara::CallbackReturn::Continue }); ``` I haven't added a test for it, because the only module that uses this is the cuckoo module, and to use it, the module-cuckoo feature must be enabled and the libjansson-dev needs to be installed. If you prefer to have a test, I can try to update the CI to have a test like this working.
- Loading branch information
Showing
9 changed files
with
313 additions
and
2 deletions.
There are no files selected for viewing
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,45 @@ | ||
use std::ffi::{c_void, CStr}; | ||
use std::fmt::Debug; | ||
|
||
/// Details about a module being imported. | ||
pub struct YrModuleImport<'a>(&'a mut yara_sys::YR_MODULE_IMPORT); | ||
|
||
impl Debug for YrModuleImport<'_> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
Debug::fmt("YrModuleImport", f) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a mut yara_sys::YR_MODULE_IMPORT> for YrModuleImport<'a> { | ||
fn from(value: &'a mut yara_sys::YR_MODULE_IMPORT) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl YrModuleImport<'_> { | ||
/// Get the name of the module. | ||
pub fn name(&self) -> Option<&[u8]> { | ||
let ptr = self.0.module_name; | ||
if ptr.is_null() { | ||
None | ||
} else { | ||
// Safety: | ||
// - ptr is not null, and is guaranteed by libyara to be nul-terminated | ||
// - returned slice is valid for as long as self, guaranteeing the ptr to stay valid. | ||
let cstr = unsafe { CStr::from_ptr(ptr) }; | ||
Some(cstr.to_bytes()) | ||
} | ||
} | ||
|
||
/// Set the module data to be used by the module. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The caller must guarantee that: | ||
/// - `ptr` is valid for reads of `size` bytes. | ||
/// - `ptr` stays valid for the full duration of the scan. | ||
pub unsafe fn set_module_data(&mut self, ptr: *mut c_void, size: usize) { | ||
self.0.module_data = ptr; | ||
self.0.module_data_size = size as _; | ||
} | ||
} |
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
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
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