-
Notifications
You must be signed in to change notification settings - Fork 218
Run yara-x rules against small blocks of sbufs #487
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
Draft
jonstewart
wants to merge
6
commits into
main
Choose a base branch
from
yara-x
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8e6941
configure should check for existence of yara_x_capi and link against …
jonstewart f079e26
create a stub scanner for scan_yarax, with the config symbol being HA…
jonstewart af88d0e
basic scanning of sbufs with yara_x implemented. This uses a hard-cod…
jonstewart bbd5cdf
Create a yara-x feature recorder and write the names of matching rule…
jonstewart 7f33588
Report on the block size offset (4KB-aligned) of a rule match, not ju…
jonstewart 6293ef4
Added a new --yara_x_rules CLI option for specifying a directory of .…
jonstewart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
| /** | ||
| * Plugin: scan_yarax | ||
| * Purpose: Run yara-x rules against raw pages | ||
| * Reference: https://virustotal.github.io/yara-x/ | ||
| **/ | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include "config.h" | ||
| #include "be20_api/scanner_params.h" | ||
|
|
||
| #ifdef HAVE_YARAX | ||
| extern "C" { | ||
| #include <yara_x.h> | ||
|
|
||
| void scan_yarax(scanner_params &sp); | ||
| } | ||
|
|
||
| auto YARA_RULE = R"( | ||
| rule test_rule { | ||
| strings: | ||
| $a = "test" | ||
| condition: | ||
| $a | ||
| } | ||
| )"; | ||
|
|
||
| using rules_ptr = std::unique_ptr<YRX_RULES, decltype(&yrx_rules_destroy)>; | ||
|
|
||
| rules_ptr& getYaraRules() { | ||
| static rules_ptr rules(nullptr, yrx_rules_destroy); | ||
| return rules; | ||
| } | ||
|
|
||
| struct YaraCallbackData { | ||
| feature_recorder &Recorder; | ||
| pos0_t Pos; | ||
| }; | ||
|
|
||
| void yara_callback(const YRX_RULE* rule, void* userData) { | ||
| const uint8_t* ruleID = nullptr; | ||
| size_t idLen = 0; | ||
| if (yrx_rule_identifier(rule, &ruleID, &idLen) != SUCCESS) { | ||
| std::cerr << "Failed to get rule ID in yara_callback" << std::endl; | ||
| return; | ||
| } | ||
| std::string_view ruleIDView(reinterpret_cast<const char*>(ruleID), idLen); | ||
| const YaraCallbackData* data = static_cast<YaraCallbackData*>(userData); | ||
| data->Recorder.write(data->Pos, std::string(ruleIDView), ""); | ||
| } | ||
|
|
||
| void scan_yarax(scanner_params &sp) { | ||
| sp.check_version(); | ||
| if (sp.phase == scanner_params::PHASE_INIT){ | ||
| sp.info->set_name("yarax"); | ||
| sp.info->author = "Jon Stewart"; | ||
| sp.info->description = "Scans for yara-x rule matches in raw pages"; | ||
| sp.info->scanner_version = "1.0"; | ||
| sp.info->feature_defs.push_back(feature_recorder_def("yara-x")); | ||
| return; | ||
| } | ||
| else if (sp.phase == scanner_params::PHASE_INIT2) { | ||
| YRX_RULES* rules = nullptr; | ||
| YRX_RESULT result = yrx_compile(YARA_RULE, &rules); | ||
| if (result != SUCCESS) { | ||
| std::cerr << "Failed to compile yara-x rule: " << yrx_last_error() << std::endl; | ||
| return; | ||
| } | ||
| else { | ||
| getYaraRules().reset(rules); | ||
| } | ||
| } | ||
| else if (sp.phase == scanner_params::PHASE_SCAN) { | ||
| rules_ptr& rules = getYaraRules(); | ||
| if (!rules) { | ||
| return; | ||
| } | ||
| YRX_SCANNER* scannerRawPtr = nullptr; | ||
| YRX_RESULT result = yrx_scanner_create(rules.get(), &scannerRawPtr); | ||
| if (result != SUCCESS) { | ||
| std::cerr << "Failed to create yara-x scanner: " << yrx_last_error() << std::endl; | ||
| return; | ||
| } | ||
| std::unique_ptr<YRX_SCANNER, decltype(&yrx_scanner_destroy)> scanner(scannerRawPtr, yrx_scanner_destroy); | ||
|
|
||
| YaraCallbackData callbackData{sp.named_feature_recorder("yara-x"), sp.sbuf->pos0}; | ||
|
|
||
| result = yrx_scanner_on_matching_rule(scanner.get(), yara_callback, &callbackData); | ||
| if (result != SUCCESS) { | ||
| std::cerr << "Failed to set yara-x callback: " << yrx_last_error() << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| // Iterate the sbuf 4KB at a time and scan these pages individually | ||
| const sbuf_t *sbuf = sp.sbuf; | ||
| const size_t blockSize = 4096; | ||
| const uint8_t* curBlock = sbuf->get_buf(); | ||
| const uint8_t* end = curBlock + sbuf->pagesize; | ||
| while (curBlock < end) { | ||
| const size_t len = std::min(blockSize, static_cast<size_t>(end - curBlock)); | ||
| result = yrx_scanner_scan(scanner.get(), curBlock, len); | ||
| if (result != SUCCESS) { | ||
| std::cerr << "Failed to scan yara-x: " << yrx_last_error() << std::endl; | ||
| return; | ||
| } | ||
| curBlock += len; | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to consolidate this pkg-config block with the one above it for lightgrep to avoid duplication.