Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRules method to Scanner #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
var util = require("util")
var yara = require ("./build/Release/yara");

function _parseMetadata(rule) {
for (var i = 0; i < rule.metas.length; i++) {
var fields = rule.metas[i].split(":")

var type = parseInt(fields.shift())
var id = fields.shift()

var meta = {
type: type,
id: id,
value: fields.join(":")
}

if (meta.type == yara.MetaType.Integer)
meta.value = parseInt(meta.value)
else if (meta.type == yara.MetaType.Boolean)
meta.value = (meta.value == "true") ? true : false

rule.metas[i] = meta
}

return rule;
}

function _expandConstantObject(object) {
var keys = []
for (var key in object)
Expand All @@ -23,6 +47,15 @@ function Scanner(options) {
this.yara = new yara.ScannerWrap()
}

Scanner.prototype.getRules = function() {
var result = this.yara.getRules();
result.rules.forEach(function(rule) {
_parseMetadata(rule);
});

return result;
}

Scanner.prototype.configure = function(options, cb) {
return this.yara.configure(options, function(error, warnings) {
if (warnings) {
Expand Down Expand Up @@ -73,25 +106,7 @@ Scanner.prototype.scan = function(req, cb) {
cb(error)
} else {
result.rules.forEach(function(rule) {
for (var i = 0; i < rule.metas.length; i++) {
var fields = rule.metas[i].split(":")

var type = parseInt(fields.shift())
var id = fields.shift()

var meta = {
type: type,
id: id,
value: fields.join(":")
}

if (meta.type == yara.MetaType.Integer)
meta.value = parseInt(meta.value)
else if (meta.type == yara.MetaType.Boolean)
meta.value = (meta.value == "true") ? true : false

rule.metas[i] = meta
}
rule = _parseMetadata(rule);

for (var i = 0; i < rule.matches.length; i++) {
var fields = rule.matches[i].split(":")
Expand Down
97 changes: 95 additions & 2 deletions src/yara.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void ScannerWrap::Init(Local<Object> exports) {

Nan::SetPrototypeMethod(tpl, "configure", Configure);
Nan::SetPrototypeMethod(tpl, "scan", Scan);
Nan::SetPrototypeMethod(tpl, "getRules", GetRules);

ScannerWrap_constructor.Reset(tpl);
Nan::Set(exports, Nan::New("ScannerWrap").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
Expand Down Expand Up @@ -456,7 +457,7 @@ class AsyncConfigure : public Nan::AsyncWorker {
);
}
}

if (error_count == 0) {
rc = yr_compiler_get_rules(scanner_->compiler, &scanner_->rules);
if (rc != ERROR_SUCCESS)
Expand Down Expand Up @@ -692,6 +693,14 @@ struct MatchData {
uint32_t length;
};

struct CompiledRule {
std::string id;
std::list<std::string> tags;
std::list<std::string> metas;
};

typedef std::list<CompiledRule*> CompiledRuleList;

struct ScanRuleMatch {
std::string id;
std::list<std::string> tags;
Expand Down Expand Up @@ -943,6 +952,90 @@ int scanCallback(int message, void* data, void* param) {
return CALLBACK_CONTINUE;
}

NAN_METHOD(ScannerWrap::GetRules) {
Nan::HandleScope scope;

CompiledRuleList compiled_rules;
CompiledRuleList::iterator compiled_rules_it;
YR_RULE* rule;

ScannerWrap* scanner = ScannerWrap::Unwrap<ScannerWrap>(info.This());

yr_rules_foreach(scanner->rules, rule) {
CompiledRule* compiled_rule;
YR_META* meta;
YR_STRING* rule_string;
const char* tag;

compiled_rule = new CompiledRule();

compiled_rule->id = rule->identifier;

yr_rule_tags_foreach(rule, tag) {
compiled_rule->tags.push_back(std::string(tag));
}

yr_rule_metas_foreach(rule, meta) {
std::ostringstream oss;
oss << meta->type << ":" << meta->identifier << ":";

if (meta->type == META_TYPE_INTEGER)
oss << meta->integer;
else if (meta->type == META_TYPE_BOOLEAN)
oss << (meta->integer ? "true" : "false");
else
oss << meta->string;

compiled_rule->metas.push_back(oss.str());
}

compiled_rules.push_back(compiled_rule);
}

Local<Object> res = Nan::New<Object>();

Local<Array> rules = Nan::New<Array>();
int rules_index = 0;

for (CompiledRuleList::iterator compiled_rules_it = compiled_rules.begin();
compiled_rules_it != compiled_rules.end();
compiled_rules_it++) {
CompiledRule* compiled_rule = *compiled_rules_it;

Local<Object> rule = Nan::New<Object>();

Local<Array> tags = Nan::New<Array>();
int tags_index = 0;

for (std::list<std::string>::iterator tags_it = compiled_rule->tags.begin();
tags_it != compiled_rule->tags.end();
tags_it++) {
Local<String> tag = Nan::New((*tags_it).c_str()).ToLocalChecked();
Nan::Set(tags, tags_index++, tag);
}

Local<Array> metas = Nan::New<Array>();
int metas_index = 0;

for (std::list<std::string>::iterator metas_it = compiled_rule->metas.begin();
metas_it != compiled_rule->metas.end();
metas_it++) {
Local<String> meta = Nan::New((*metas_it).c_str()).ToLocalChecked();
Nan::Set(metas, metas_index++, meta);
}

Nan::Set(rule, Nan::New("id").ToLocalChecked(), Nan::New(compiled_rule->id.c_str()).ToLocalChecked());
Nan::Set(rule, Nan::New("tags").ToLocalChecked(), tags);
Nan::Set(rule, Nan::New("metas").ToLocalChecked(), metas);

Nan::Set(rules, rules_index++, rule);
}

Nan::Set(res, Nan::New("rules").ToLocalChecked(), rules);

info.GetReturnValue().Set(res);
}

NAN_METHOD(ScannerWrap::Scan) {
Nan::HandleScope scope;

Expand Down Expand Up @@ -1084,7 +1177,7 @@ NAN_METHOD(ScannerWrap::Scan) {
scan_req,
callback
);

async_scan->matched_bytes = matched_bytes;

Nan::AsyncQueueWorker(async_scan);
Expand Down
1 change: 1 addition & 0 deletions src/yara.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ScannerWrap : public Nan::ObjectWrap {
static NAN_METHOD(New);
static NAN_METHOD(Configure);
static NAN_METHOD(Scan);
static NAN_METHOD(GetRules);

pthread_rwlock_t lock;
};
Expand Down
67 changes: 67 additions & 0 deletions test/unit_index.js_scanner.getRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

var assert = require("assert")

var yara = require ("../")

var scanner;

before(function(done) {
yara.initialize(function(error) {
assert.ifError(error)

scanner = yara.createScanner()

scanner.configure({
rules: [
{string: "rule is_stephen : human man {\nmeta:\nm1 = \"m1\"\nm2 = true\nm3 = 123\n\nstrings:\n$s1 = \"stephen\"\ncondition:\nany of them\n}"},
{string: "rule is_either : human man woman {\nstrings:\n$s1 = \"stephen\"\n$s2 = \"silvia\"\ncondition:\nany of them\n}"},
]
}, function(error) {
assert.ifError(error)
done()
})
})
})

describe("index.js", function() {
describe("Scanner.getRules()", function() {
it("returns rule metadata", function(done) {
var result = scanner.getRules()

var expected = {
"rules": [
{
"id": "is_stephen",
"tags": ["human", "man"],
"metas": [
{type: 2, id: "m1", value: "m1"},
{type: 3, id: "m2", value: true},
{type: 1, id: "m3", value: 123}
]
},
{
"id": "is_either",
"tags": ["human", "man", "woman"],
"metas": []
}
]
}

assert.deepEqual(result, expected)
done()
})

it("returns empty array if no rules are configured", function(done) {
var scanner = yara.createScanner()
scanner.configure({
rules: [
{}
]
}, function() {
var result = scanner.getRules();
assert.deepEqual(result, { rules: []})
done()
})
})
})
})