Skip to content

Commit

Permalink
feat: handle CALLBACK_MSG_CONSOLE_LOG callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
vthib committed Dec 8, 2023
1 parent e0d788a commit 6cdad52
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/internals/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum CallbackMsg<'r> {
ModuleImported(YrObject<'r>),
TooManyMatches(YrString<'r>),
ScanFinished,
ConsoleLog(&'r CStr),
UnknownMsg,
}

Expand Down Expand Up @@ -50,6 +51,10 @@ impl<'r> CallbackMsg<'r> {
TooManyMatches(YrString::from((context, yr_string)))
}
yara_sys::CALLBACK_MSG_SCAN_FINISHED => ScanFinished,
yara_sys::CALLBACK_MSG_CONSOLE_LOG => {
let msg = unsafe { CStr::from_ptr(message_data as *const i8) };
ConsoleLog(msg)
}
_ => UnknownMsg,
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,28 @@ fn test_scan_mem_blocks_sized() {
assert_eq!(1, result.len());
}

#[test]
fn test_scan_mem_console_log() {
let rule = r#"
import "console"
rule log {
condition:
console.log("value: ", 12)
}"#;
let rules = compile(rule);
let mut logs = Vec::new();
let callback = |message| {
if let CallbackMsg::ConsoleLog(log) = message {
logs.push(log.to_string_lossy().to_string());
}
CallbackReturn::Continue
};

let result = rules.scan_mem_callback(b"", 10, callback);
assert!(result.is_ok());
assert_eq!(&logs, &["value: 12"]);
}

#[test]
fn test_scan_fast_mode() {
let test_mem = b"
Expand Down

0 comments on commit 6cdad52

Please sign in to comment.