-
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
1 parent
c2e9703
commit 8d0a143
Showing
4 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto2"; | ||
|
||
import "yara.proto"; | ||
|
||
option (yara.module_options) = { | ||
name : "time" | ||
root_message: "Time" | ||
rust_module: "time" | ||
}; | ||
|
||
message Time { | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::modules::prelude::*; | ||
use crate::modules::protos::time::*; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
#[module_main] | ||
fn main(_ctx: &ScanContext) -> Time { | ||
// Nothing to do, but we have to return our protobuf | ||
Time::new() | ||
} | ||
|
||
#[module_export] | ||
fn now(ctx: &ScanContext) -> Option<i64> { | ||
Some(SystemTime::now().duration_since(UNIX_EPOCH).ok()?.as_secs() as i64) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn end2end() { | ||
let rules = crate::compiler::Compiler::new() | ||
.add_source( | ||
r#"import "time" | ||
rule rule_1 { condition: time.now() >= 0 } | ||
rule rule_2 { condition: time.now() <= 0 } | ||
rule rule_3 { condition: time.now() != 0 } | ||
rule rule_4 { condition: time.now() == 0 } | ||
"#, | ||
) | ||
.unwrap() | ||
.build() | ||
.unwrap(); | ||
|
||
let mut scanner = crate::scanner::Scanner::new(&rules); | ||
|
||
assert_eq!(scanner.scan(&[]).num_matching_rules(), 2); | ||
} | ||
} |