-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
217 additions
and
29 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
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,112 @@ | ||
use crate::modules::prelude::*; | ||
use crate::modules::protos::console::*; | ||
|
||
#[module_main] | ||
fn main(_data: &[u8]) -> Console { | ||
// Nothing to do, but we have to return our protobuf | ||
Console::new() | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_str(ctx: &mut ScanContext, string: RuntimeString) -> bool { | ||
ctx.console_log(format!("{}", string.as_bstr(ctx))); | ||
true | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_msg_str( | ||
ctx: &mut ScanContext, | ||
message: RuntimeString, | ||
string: RuntimeString, | ||
) -> bool { | ||
ctx.console_log(format!( | ||
"{}{}", | ||
message.as_bstr(ctx), | ||
string.as_bstr(ctx) | ||
)); | ||
true | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_int(ctx: &mut ScanContext, i: i64) -> bool { | ||
ctx.console_log(format!("{}", i)); | ||
true | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_msg_int(ctx: &mut ScanContext, message: RuntimeString, i: i64) -> bool { | ||
ctx.console_log(format!("{}{}", message.as_bstr(ctx), i)); | ||
true | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_float(ctx: &mut ScanContext, f: f64) -> bool { | ||
ctx.console_log(format!("{}", f)); | ||
true | ||
} | ||
|
||
#[module_export(name = "log")] | ||
fn log_msg_float( | ||
ctx: &mut ScanContext, | ||
message: RuntimeString, | ||
f: f64, | ||
) -> bool { | ||
ctx.console_log(format!("{}{}", message.as_bstr(ctx), f)); | ||
true | ||
} | ||
|
||
#[module_export(name = "hex")] | ||
fn log_hex(ctx: &mut ScanContext, i: i64) -> bool { | ||
ctx.console_log(format!("0x{:x}", i)); | ||
true | ||
} | ||
|
||
#[module_export(name = "hex")] | ||
fn log_msg_hex(ctx: &mut ScanContext, message: RuntimeString, i: i64) -> bool { | ||
ctx.console_log(format!("{}0x{:x}", message.as_bstr(ctx), i)); | ||
true | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
#[test] | ||
fn log() { | ||
let rules = crate::compile( | ||
r#" | ||
import "console" | ||
rule test { | ||
condition: | ||
console.log("foo") and | ||
console.log("bar: ", 1) and | ||
console.log("baz: ", 3.14) and | ||
console.log(10) and | ||
console.log(6.28) and | ||
console.hex(10) and | ||
console.hex("qux: ", 255) | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
let mut messages = vec![]; | ||
|
||
crate::scanner::Scanner::new(&rules) | ||
.console_log(|message| messages.push(message)) | ||
.scan(b"") | ||
.expect("scan should not fail"); | ||
|
||
assert_eq!( | ||
messages, | ||
vec![ | ||
"foo", | ||
"bar: 1", | ||
"baz: 3.14", | ||
"10", | ||
"6.28", | ||
"0xa", | ||
"qux: 0xff" | ||
] | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,25 +1,27 @@ | ||
// File generated automatically by build.rs. Do not edit. | ||
#[cfg(feature = "test_proto2-module")] | ||
mod test_proto2; | ||
#[cfg(feature = "string-module")] | ||
mod string; | ||
#[cfg(feature = "macho-module")] | ||
mod macho; | ||
#[cfg(feature = "pe-module")] | ||
mod pe; | ||
#[cfg(feature = "elf-module")] | ||
mod elf; | ||
#[cfg(feature = "text-module")] | ||
mod text; | ||
#[cfg(feature = "dotnet-module")] | ||
mod dotnet; | ||
#[cfg(feature = "lnk-module")] | ||
mod lnk; | ||
#[cfg(feature = "hash-module")] | ||
mod hash; | ||
#[cfg(feature = "text-module")] | ||
mod text; | ||
#[cfg(feature = "math-module")] | ||
mod math; | ||
#[cfg(feature = "test_proto2-module")] | ||
mod test_proto2; | ||
#[cfg(feature = "time-module")] | ||
mod time; | ||
#[cfg(feature = "dotnet-module")] | ||
mod dotnet; | ||
#[cfg(feature = "test_proto3-module")] | ||
mod test_proto3; | ||
#[cfg(feature = "pe-module")] | ||
mod pe; | ||
#[cfg(feature = "string-module")] | ||
mod string; | ||
#[cfg(feature = "elf-module")] | ||
mod elf; | ||
#[cfg(feature = "math-module")] | ||
mod math; | ||
#[cfg(feature = "console-module")] | ||
mod console; |
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,14 @@ | ||
syntax = "proto2"; | ||
import "yara.proto"; | ||
|
||
package console; | ||
|
||
option (yara.module_options) = { | ||
name : "console" | ||
root_message: "console.Console" | ||
rust_module: "console" | ||
}; | ||
|
||
message Console { | ||
// 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
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