2021-02-26 13:35:19 +01:00
|
|
|
#include "views/view_yara.hpp"
|
|
|
|
|
|
|
|
#include <hex/providers/provider.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2021-09-23 22:57:19 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
#include <yara.h>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <imgui_imhex_extensions.h>
|
2021-09-23 22:57:19 +02:00
|
|
|
#include <hex/helpers/paths.hpp>
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-09-23 22:57:19 +02:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2021-03-03 22:26:17 +01:00
|
|
|
ViewYara::ViewYara() : View("hex.view.yara.name") {
|
2021-02-26 13:35:19 +01:00
|
|
|
yr_initialize();
|
|
|
|
|
|
|
|
this->reloadRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewYara::~ViewYara() {
|
|
|
|
yr_finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewYara::drawContent() {
|
2021-03-03 22:26:17 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.view.yara.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
if (!this->m_matching && !this->m_errorMessage.empty()) {
|
|
|
|
View::showErrorPopup("hex.view.yara.error"_lang + this->m_errorMessage.data());
|
|
|
|
this->m_errorMessage.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("hex.view.yara.header.rules"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (this->m_rules.empty()) {
|
2021-03-03 22:26:17 +01:00
|
|
|
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F), "%s", static_cast<const char*>("hex.view.yara.no_rules"_lang));
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
if (ImGui::Button("hex.view.yara.reload"_lang)) this->reloadRules();
|
|
|
|
} else {
|
|
|
|
ImGui::Disabled([this]{
|
2021-09-23 22:57:19 +02:00
|
|
|
if (ImGui::BeginCombo("hex.view.yara.header.rules"_lang, this->m_rules[this->m_selectedRule].first.c_str())) {
|
2021-02-26 13:35:19 +01:00
|
|
|
for (u32 i = 0; i < this->m_rules.size(); i++) {
|
|
|
|
const bool selected = (this->m_selectedRule == i);
|
2021-09-23 22:57:19 +02:00
|
|
|
if (ImGui::Selectable(this->m_rules[i].first.c_str(), selected))
|
2021-02-26 13:35:19 +01:00
|
|
|
this->m_selectedRule = i;
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
ImGui::SetItemDefaultFocus();
|
|
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("hex.view.yara.reload"_lang)) this->reloadRules();
|
|
|
|
if (ImGui::Button("hex.view.yara.match"_lang)) this->applyRules();
|
|
|
|
}, this->m_matching);
|
|
|
|
|
|
|
|
if (this->m_matching) {
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextSpinner("hex.view.yara.matching"_lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.yara.header.matches"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::BeginTable("matches", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
|
|
|
ImGui::TableSetupColumn("hex.view.yara.matches.identifier"_lang);
|
|
|
|
ImGui::TableSetupColumn("hex.common.address"_lang);
|
|
|
|
ImGui::TableSetupColumn("hex.common.size"_lang);
|
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
|
|
|
ImGuiListClipper clipper;
|
|
|
|
clipper.Begin(this->m_matches.size());
|
|
|
|
|
|
|
|
while (clipper.Step()) {
|
|
|
|
for (u32 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
|
|
|
auto &[identifier, address, size, wholeDataMatch] = this->m_matches[i];
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::PushID(i);
|
|
|
|
if (ImGui::Selectable("match", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::post<RequestSelectionChange>(Region { u64(address), size_t(size) });
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
ImGui::PopID();
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextUnformatted(identifier.c_str());
|
|
|
|
|
|
|
|
if (!wholeDataMatch) {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Text("0x%llX : 0x%llX", address, address + size - 1);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Text("0x%lX", size);
|
|
|
|
} else {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F), "%s", static_cast<const char*>("hex.view.yara.whole_data"_lang));
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clipper.End();
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewYara::drawMenu() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewYara::reloadRules() {
|
|
|
|
this->m_rules.clear();
|
|
|
|
|
2021-09-23 22:57:19 +02:00
|
|
|
for (auto path : hex::getPath(ImHexPath::Yara)) {
|
|
|
|
if (!fs::exists(path))
|
|
|
|
continue;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2021-09-23 22:57:19 +02:00
|
|
|
for (const auto &entry : fs::recursive_directory_iterator(path)) {
|
|
|
|
if (entry.is_regular_file() && entry.path().extension() == ".yar") {
|
|
|
|
this->m_rules.push_back({ fs::relative(entry.path(), fs::path(path)).string(), entry.path().string() });
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewYara::applyRules() {
|
|
|
|
this->m_matches.clear();
|
|
|
|
this->m_errorMessage.clear();
|
|
|
|
this->m_matching = true;
|
|
|
|
|
|
|
|
std::thread([this] {
|
|
|
|
|
|
|
|
YR_COMPILER *compiler = nullptr;
|
|
|
|
yr_compiler_create(&compiler);
|
2021-09-23 22:57:19 +02:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
yr_compiler_destroy(compiler);
|
|
|
|
this->m_matching = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
yr_compiler_set_include_callback(
|
|
|
|
compiler,
|
|
|
|
[](const char *includeName, const char *callingRuleFileName, const char *callingRuleNamespace, void *userData) -> const char * {
|
|
|
|
auto currFilePath = static_cast<const char*>(userData);
|
|
|
|
|
|
|
|
File file((fs::path(currFilePath).parent_path() / includeName).string(), File::Mode::Read);
|
|
|
|
if (!file.isValid())
|
|
|
|
return nullptr;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2021-09-23 22:57:19 +02:00
|
|
|
auto size = file.getSize();
|
|
|
|
char *buffer = new char[size + 1];
|
|
|
|
file.readBuffer(reinterpret_cast<u8*>(buffer), size);
|
|
|
|
buffer[size] = 0x00;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
},
|
|
|
|
[](const char *ptr, void *userData) {
|
|
|
|
delete[] ptr;
|
|
|
|
},
|
|
|
|
this->m_rules[this->m_selectedRule].second.data());
|
|
|
|
|
|
|
|
|
|
|
|
File file(this->m_rules[this->m_selectedRule].second, File::Mode::Read);
|
2021-09-08 15:18:24 +02:00
|
|
|
if (!file.isValid()) return;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
if (yr_compiler_add_file(compiler, file.getHandle(), nullptr, nullptr) != 0) {
|
2021-02-26 13:35:19 +01:00
|
|
|
this->m_errorMessage.resize(0xFFFF);
|
|
|
|
yr_compiler_get_error_message(compiler, this->m_errorMessage.data(), this->m_errorMessage.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
YR_RULES *rules;
|
|
|
|
yr_compiler_get_rules(compiler, &rules);
|
2021-09-23 22:57:19 +02:00
|
|
|
ON_SCOPE_EXIT { yr_rules_destroy(rules); };
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
std::vector<YaraMatch> newMatches;
|
|
|
|
|
|
|
|
YR_MEMORY_BLOCK_ITERATOR iterator;
|
|
|
|
|
|
|
|
struct ScanContext {
|
|
|
|
std::vector<u8> buffer;
|
|
|
|
YR_MEMORY_BLOCK currBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
ScanContext context;
|
|
|
|
context.currBlock.base = 0;
|
|
|
|
context.currBlock.fetch_data = [](auto *block) -> const u8* {
|
|
|
|
auto &context = *static_cast<ScanContext*>(block->context);
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
context.buffer.resize(std::min<u64>(0xF'FFFF, provider->getSize() - context.currBlock.base));
|
|
|
|
|
|
|
|
if (context.buffer.empty()) return nullptr;
|
|
|
|
|
2021-04-16 21:50:15 +02:00
|
|
|
provider->readRelative(context.currBlock.base, context.buffer.data(), context.buffer.size());
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
return context.buffer.data();
|
|
|
|
};
|
|
|
|
iterator.file_size = [](auto *iterator) -> u64 {
|
2021-09-21 02:29:54 +02:00
|
|
|
return ImHexApi::Provider::get()->getSize();
|
2021-02-26 13:35:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
iterator.context = &context;
|
|
|
|
iterator.first = [](YR_MEMORY_BLOCK_ITERATOR* iterator) -> YR_MEMORY_BLOCK* {
|
|
|
|
auto &context = *static_cast<ScanContext*>(iterator->context);
|
|
|
|
|
|
|
|
context.currBlock.base = 0;
|
|
|
|
context.currBlock.size = 0;
|
|
|
|
context.buffer.clear();
|
|
|
|
iterator->last_error = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
return iterator->next(iterator);
|
|
|
|
};
|
|
|
|
iterator.next = [](YR_MEMORY_BLOCK_ITERATOR* iterator) -> YR_MEMORY_BLOCK* {
|
|
|
|
auto &context = *static_cast<ScanContext*>(iterator->context);
|
|
|
|
|
|
|
|
u64 address = context.currBlock.base + context.currBlock.size;
|
|
|
|
|
|
|
|
iterator->last_error = ERROR_SUCCESS;
|
|
|
|
context.currBlock.base = address;
|
2021-09-21 02:29:54 +02:00
|
|
|
context.currBlock.size = std::min<u64>(0xF'FFFF, ImHexApi::Provider::get()->getSize() - address);
|
2021-02-26 13:35:19 +01:00
|
|
|
context.currBlock.context = &context;
|
|
|
|
|
|
|
|
if (context.currBlock.size == 0) return nullptr;
|
|
|
|
|
|
|
|
return &context.currBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
yr_rules_scan_mem_blocks(rules, &iterator, 0, [](YR_SCAN_CONTEXT* context, int message, void *data, void *userData) -> int {
|
|
|
|
if (message == CALLBACK_MSG_RULE_MATCHING) {
|
|
|
|
auto &newMatches = *static_cast<std::vector<YaraMatch>*>(userData);
|
|
|
|
auto rule = static_cast<YR_RULE*>(data);
|
|
|
|
|
|
|
|
YR_STRING *string;
|
|
|
|
YR_MATCH *match;
|
|
|
|
|
|
|
|
if (rule->strings != nullptr) {
|
|
|
|
yr_rule_strings_foreach(rule, string) {
|
|
|
|
yr_string_matches_foreach(context, string, match) {
|
|
|
|
newMatches.push_back({ rule->identifier, match->offset, match->match_length, false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newMatches.push_back({ rule->identifier, 0, 0, true });
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return CALLBACK_CONTINUE;
|
|
|
|
}, &newMatches, 0);
|
|
|
|
|
|
|
|
std::copy(newMatches.begin(), newMatches.end(), std::back_inserter(this->m_matches));
|
|
|
|
}).detach();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|