commit a50bae058041cc0b1eeddb4757b66a8696ad231c
parent 8bfebfb05cf44da058a4dbcd684c84f27f41ed81
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 30 Sep 2023 18:04:41 +0200
Add a way to scan string and add missing libssl dependency to CI
Diffstat:
3 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml
@@ -43,7 +43,7 @@ jobs:
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian
build: |-
set -e &&
- sudo apt-get install -y automake libtool make gcc pkg-config libmagic-dev &&
+ sudo apt-get install -y automake libtool make gcc pkg-config libmagic-dev libssl-dev &&
make yara &&
yarn build --target x86_64-unknown-linux-gnu &&
strip *.node
@@ -63,7 +63,7 @@ jobs:
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64
build: |-
set -e &&
- sudo apt-get install -y automake libtool make gcc pkg-config libmagic-dev &&
+ sudo apt-get install -y automake libtool make gcc pkg-config libmagic-dev libssl-dev &&
make yara &&
yarn build --target aarch64-unknown-linux-gnu &&
aarch64-unknown-linux-gnu-strip *.node
diff --git a/index.d.ts b/index.d.ts
@@ -22,6 +22,7 @@ export interface YaraVariable {
export class YaraScanner {
constructor(rules: Array<YaraRule>, variables: Array<YaraVariable>)
scanBuffer(buffer: Buffer, timeout: number): Array<YaraRuleResult>
+ scanString(input: string, timeout: number): Array<YaraRuleResult>
}
export class YaraRuleResult {
/** Name of the rule. */
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,7 +1,7 @@
#![deny(clippy::all)]
use napi::{anyhow::Context, bindgen_prelude::Buffer, Result};
-use yara::{Compiler, Rules};
+use yara::{Compiler, Rule as ExtYaraRule, Rules};
#[macro_use]
extern crate napi_derive;
@@ -134,15 +134,8 @@ impl YaraScanner {
Ok(YaraScanner { rules })
}
- #[napi]
- pub fn scan_buffer(&self, buffer: Buffer, timeout: i32) -> Result<Vec<YaraRuleResult>> {
- let buf: Vec<u8> = buffer.into();
- let results = self
- .rules
- .scan_mem(&buf, timeout)
- .context("Failed to scan buffer")?;
-
- let napi_results = results
+ fn convert_yara_results(&self, rules: Vec<ExtYaraRule>) -> Vec<YaraRuleResult> {
+ rules
.iter()
.map(|rule| YaraRuleResult {
identifier: rule.identifier.to_string(),
@@ -163,8 +156,28 @@ impl YaraScanner {
})
.collect(),
})
- .collect();
+ .collect()
+ }
+
+ #[napi]
+ pub fn scan_buffer(&self, buffer: Buffer, timeout: i32) -> Result<Vec<YaraRuleResult>> {
+ let buf: Vec<u8> = buffer.into();
+ let results = self
+ .rules
+ .scan_mem(&buf, timeout)
+ .context("Failed to scan buffer")?;
+
+ Ok(self.convert_yara_results(results))
+ }
+
+ #[napi]
+ pub fn scan_string(&self, input: String, timeout: i32) -> Result<Vec<YaraRuleResult>> {
+ let buf: &[u8] = input.as_bytes();
+ let results = self
+ .rules
+ .scan_mem(buf, timeout)
+ .context("Failed to scan buffer")?;
- Ok(napi_results)
+ Ok(self.convert_yara_results(results))
}
}