2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_yara.hpp"
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-01-28 14:15:15 +01:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
#include "content/helpers/provider_extra_data.hpp"
|
|
|
|
|
2023-01-04 14:03:09 +01:00
|
|
|
// <yara/types.h>'s RE type has a zero-sized array, which is not allowed in ISO C++.
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
2021-02-26 13:35:19 +01:00
|
|
|
#include <yara.h>
|
2023-01-04 14:03:09 +01:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2021-02-26 13:35:19 +01:00
|
|
|
#include <filesystem>
|
|
|
|
#include <thread>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
#include <wolv/io/fs.hpp>
|
|
|
|
#include <wolv/utils/guards.hpp>
|
2023-03-16 13:35:09 +01:00
|
|
|
#include <wolv/literals.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-03-16 13:35:09 +01:00
|
|
|
using namespace wolv::literals;
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewYara::ViewYara() : View("hex.builtin.view.yara.name") {
|
2021-02-26 13:35:19 +01:00
|
|
|
yr_initialize();
|
|
|
|
|
2023-01-28 00:01:53 +01:00
|
|
|
ContentRegistry::FileHandler::add({ ".yar", ".yara" }, [](const auto &path) {
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &destPath : fs::getDefaultPaths(fs::ImHexPath::Yara)) {
|
2023-03-12 18:27:29 +01:00
|
|
|
if (wolv::io::fs::copyFile(path, destPath / path.filename(), std::fs::copy_options::overwrite_existing)) {
|
2022-07-02 17:53:13 +02:00
|
|
|
View::showInfoPopup("hex.builtin.view.yara.rule_added"_lang);
|
2022-01-13 14:34:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2023-01-28 14:15:15 +01:00
|
|
|
|
|
|
|
ProjectFile::registerPerProviderHandler({
|
|
|
|
.basePath = "yara.json",
|
|
|
|
.required = false,
|
|
|
|
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto fileContent = tar.readString(basePath);
|
2023-01-28 14:15:15 +01:00
|
|
|
if (fileContent.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto data = nlohmann::json::parse(fileContent.begin(), fileContent.end());
|
|
|
|
|
|
|
|
if (!data.contains("rules"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &rules = data["rules"];
|
|
|
|
if (!rules.is_array())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &extraData = ProviderExtraData::get(provider).yara;
|
|
|
|
extraData.matches.clear();
|
|
|
|
|
|
|
|
for (auto &rule : rules) {
|
|
|
|
if (!rule.contains("name") || !rule.contains("path"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &name = rule["name"];
|
|
|
|
auto &path = rule["path"];
|
|
|
|
|
|
|
|
if (!name.is_string() || !path.is_string())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
extraData.rules.emplace_back(std::fs::path(name.get<std::string>()), std::fs::path(path.get<std::string>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
|
|
|
|
nlohmann::json data;
|
|
|
|
|
|
|
|
data["rules"] = nlohmann::json::array();
|
|
|
|
|
|
|
|
auto &extraData = ProviderExtraData::get(provider).yara;
|
|
|
|
for (auto &[name, path] : extraData.rules) {
|
|
|
|
data["rules"].push_back({
|
2023-03-12 18:43:05 +01:00
|
|
|
{ "name", wolv::util::toUTF8String(name) },
|
|
|
|
{ "path", wolv::util::toUTF8String(path) }
|
2023-01-28 14:15:15 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
tar.writeString(basePath, data.dump(4));
|
2023-01-28 14:15:15 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewYara::~ViewYara() {
|
|
|
|
yr_finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewYara::drawContent() {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.builtin.view.yara.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.yara.header.rules"_lang);
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
auto &extraData = ProviderExtraData::getCurrent().yara;
|
|
|
|
auto &rules = extraData.rules;
|
|
|
|
auto &matches = extraData.matches;
|
2023-03-16 13:35:09 +01:00
|
|
|
auto &sortedMatches = extraData.sortedMatches;
|
2023-01-28 14:15:15 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
if (ImGui::BeginListBox("##rules", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 5))) {
|
2023-01-28 14:15:15 +01:00
|
|
|
for (u32 i = 0; i < rules.size(); i++) {
|
2023-01-27 12:17:10 +01:00
|
|
|
const bool selected = (this->m_selectedRule == i);
|
2023-03-12 18:43:05 +01:00
|
|
|
if (ImGui::Selectable(wolv::util::toUTF8String(rules[i].first).c_str(), selected)) {
|
2023-01-27 12:17:10 +01:00
|
|
|
this->m_selectedRule = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::IconButton(ICON_VS_ADD, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
|
|
|
|
std::vector<std::fs::path> paths;
|
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Yara)) {
|
|
|
|
std::error_code error;
|
|
|
|
for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) {
|
|
|
|
if (!entry.is_regular_file()) continue;
|
|
|
|
if (entry.path().extension() != ".yara" && entry.path().extension() != ".yar") continue;
|
|
|
|
|
|
|
|
paths.push_back(entry);
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-28 00:01:53 +01:00
|
|
|
View::showFileChooserPopup(paths, { { "Yara File", "yara" }, { "Yara File", "yar" } }, true,
|
2023-01-28 14:15:15 +01:00
|
|
|
[&](const auto &path) {
|
|
|
|
rules.push_back({ path.filename(), path });
|
2023-01-28 00:01:53 +01:00
|
|
|
});
|
2023-01-27 12:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::IconButton(ICON_VS_REMOVE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
|
2023-01-28 14:15:15 +01:00
|
|
|
if (this->m_selectedRule < rules.size()) {
|
|
|
|
rules.erase(rules.begin() + this->m_selectedRule);
|
|
|
|
this->m_selectedRule = std::min(this->m_selectedRule, (u32)rules.size() - 1);
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
if (ImGui::Button("hex.builtin.view.yara.match"_lang)) this->applyRules();
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (this->m_matcherTask.isRunning()) {
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextSpinner("hex.builtin.view.yara.matching"_lang);
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::NewLine();
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.yara.header.matches"_lang);
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2022-03-15 23:48:49 +01:00
|
|
|
auto matchesTableSize = ImGui::GetContentRegionAvail();
|
|
|
|
matchesTableSize.y *= 3.75 / 5.0;
|
|
|
|
matchesTableSize.y -= ImGui::GetTextLineHeightWithSpacing();
|
|
|
|
|
|
|
|
if (ImGui::BeginTable("matches", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, matchesTableSize)) {
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2023-03-16 13:35:09 +01:00
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.yara.matches.identifier"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("identifier"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.yara.matches.variable"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("variable"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.address"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("address"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.size"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("size"));
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2023-03-16 13:35:09 +01:00
|
|
|
auto sortSpecs = ImGui::TableGetSortSpecs();
|
|
|
|
if (!matches.empty() && (sortSpecs->SpecsDirty || sortedMatches.empty())) {
|
|
|
|
sortedMatches.clear();
|
|
|
|
std::transform(matches.begin(), matches.end(), std::back_inserter(sortedMatches), [](auto &match) {
|
|
|
|
return &match;
|
|
|
|
});
|
|
|
|
|
|
|
|
std::sort(sortedMatches.begin(), sortedMatches.end(), [&sortSpecs](ProviderExtraData::Data::Yara::YaraMatch *left, ProviderExtraData::Data::Yara::YaraMatch *right) -> bool {
|
|
|
|
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("identifier"))
|
|
|
|
return left->identifier < right->identifier;
|
|
|
|
else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("variable"))
|
|
|
|
return left->variable < right->variable;
|
|
|
|
else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("address"))
|
|
|
|
return left->address < right->address;
|
|
|
|
else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size"))
|
|
|
|
return left->size < right->size;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Descending)
|
|
|
|
std::reverse(sortedMatches.begin(), sortedMatches.end());
|
|
|
|
|
|
|
|
sortSpecs->SpecsDirty = false;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
if (!this->m_matcherTask.isRunning()) {
|
2022-03-15 23:48:49 +01:00
|
|
|
ImGuiListClipper clipper;
|
2023-03-16 13:35:09 +01:00
|
|
|
clipper.Begin(sortedMatches.size());
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2022-03-15 23:48:49 +01:00
|
|
|
while (clipper.Step()) {
|
2022-03-27 00:01:28 +01:00
|
|
|
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
2023-03-16 13:35:09 +01:00
|
|
|
auto &[identifier, variableName, address, size, wholeDataMatch, highlightId, tooltipId] = *sortedMatches[i];
|
2022-03-15 23:48:49 +01:00
|
|
|
ImGui::TableNextRow();
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::TableNextColumn();
|
2022-03-15 23:48:49 +01:00
|
|
|
ImGui::PushID(i);
|
|
|
|
if (ImGui::Selectable("match", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
|
|
|
|
ImHexApi::HexEditor::setSelection(address, size);
|
|
|
|
}
|
|
|
|
ImGui::PopID();
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextUnformatted(identifier.c_str());
|
2021-02-26 13:35:19 +01:00
|
|
|
ImGui::TableNextColumn();
|
2022-03-15 23:48:49 +01:00
|
|
|
ImGui::TextUnformatted(variableName.c_str());
|
|
|
|
|
|
|
|
if (!wholeDataMatch) {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("0x{0:X} : 0x{1:X}", address, address + size - 1);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("0x{0:X}", size);
|
|
|
|
} else {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormattedColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F), "{}", "hex.builtin.view.yara.whole_data"_lang);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted("");
|
|
|
|
}
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:48:49 +01:00
|
|
|
clipper.End();
|
|
|
|
}
|
2021-02-26 13:35:19 +01:00
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
2022-03-15 23:48:49 +01:00
|
|
|
|
|
|
|
auto consoleSize = ImGui::GetContentRegionAvail();
|
|
|
|
|
|
|
|
if (ImGui::BeginChild("##console", consoleSize, true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_HorizontalScrollbar)) {
|
2022-08-03 23:32:34 +02:00
|
|
|
ImGuiListClipper clipper;
|
|
|
|
|
|
|
|
clipper.Begin(this->m_consoleMessages.size());
|
2022-03-15 23:48:49 +01:00
|
|
|
while (clipper.Step())
|
2022-03-27 00:01:28 +01:00
|
|
|
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
2022-03-15 23:48:49 +01:00
|
|
|
const auto &message = this->m_consoleMessages[i];
|
|
|
|
|
|
|
|
if (ImGui::Selectable(message.c_str()))
|
|
|
|
ImGui::SetClipboardText(message.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndChild();
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:48:49 +01:00
|
|
|
void ViewYara::clearResult() {
|
2023-01-28 14:15:15 +01:00
|
|
|
auto &matches = ProviderExtraData::getCurrent().yara.matches;
|
|
|
|
|
|
|
|
for (const auto &match : matches) {
|
2022-05-27 20:42:07 +02:00
|
|
|
ImHexApi::HexEditor::removeBackgroundHighlight(match.highlightId);
|
|
|
|
ImHexApi::HexEditor::removeTooltip(match.tooltipId);
|
|
|
|
}
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
matches.clear();
|
2022-03-15 23:48:49 +01:00
|
|
|
this->m_consoleMessages.clear();
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:35:19 +01:00
|
|
|
void ViewYara::applyRules() {
|
2023-01-28 14:15:15 +01:00
|
|
|
this->clearResult();
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_matcherTask = TaskManager::createTask("hex.builtin.view.yara.matching", 0, [this](auto &task) {
|
2021-12-16 23:48:52 +01:00
|
|
|
if (!ImHexApi::Provider::isValid()) return;
|
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
auto &extraData = ProviderExtraData::getCurrent().yara;
|
|
|
|
auto &rules = extraData.rules;
|
|
|
|
auto &matches = extraData.matches;
|
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
struct ResultContext {
|
|
|
|
Task *task = nullptr;
|
2023-01-28 14:15:15 +01:00
|
|
|
std::vector<ProviderExtraData::Data::Yara::YaraMatch> newMatches;
|
2023-01-27 12:17:10 +01:00
|
|
|
std::vector<std::string> consoleMessages;
|
2021-09-23 22:57:19 +02:00
|
|
|
};
|
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
ResultContext resultContext;
|
|
|
|
resultContext.task = &task;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
for (const auto &[fileName, filePath] : rules) {
|
2023-01-27 12:17:10 +01:00
|
|
|
YR_COMPILER *compiler = nullptr;
|
|
|
|
yr_compiler_create(&compiler);
|
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
yr_compiler_destroy(compiler);
|
|
|
|
};
|
2021-09-23 22:57:19 +02:00
|
|
|
|
2023-03-12 18:43:05 +01:00
|
|
|
auto currFilePath = wolv::util::toUTF8String(wolv::io::fs::toShortPath(filePath));
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
yr_compiler_set_include_callback(
|
|
|
|
compiler,
|
|
|
|
[](const char *includeName, const char *, const char *, void *userData) -> const char * {
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::File file(std::fs::path(static_cast<const char *>(userData)).parent_path() / includeName, wolv::io::File::Mode::Read);
|
2023-01-27 12:17:10 +01:00
|
|
|
if (!file.isValid())
|
|
|
|
return nullptr;
|
2021-09-23 22:57:19 +02:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
auto size = file.getSize();
|
|
|
|
char *buffer = new char[size + 1];
|
|
|
|
file.readBuffer(reinterpret_cast<u8 *>(buffer), size);
|
|
|
|
buffer[size] = 0x00;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return buffer;
|
|
|
|
},
|
|
|
|
[](const char *ptr, void *userData) {
|
|
|
|
hex::unused(userData);
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
delete[] ptr;
|
|
|
|
},
|
|
|
|
currFilePath.data()
|
|
|
|
);
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::File file(rules[this->m_selectedRule].second, wolv::io::File::Mode::Read);
|
2023-01-27 12:17:10 +01:00
|
|
|
if (!file.isValid()) return;
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
if (yr_compiler_add_file(compiler, file.getHandle(), nullptr, nullptr) != 0) {
|
|
|
|
std::string errorMessage(0xFFFF, '\x00');
|
|
|
|
yr_compiler_get_error_message(compiler, errorMessage.data(), errorMessage.size());
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-03-13 08:58:08 +01:00
|
|
|
TaskManager::doLater([this, errorMessage = wolv::util::trim(errorMessage)] {
|
2023-01-27 12:17:10 +01:00
|
|
|
this->clearResult();
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
this->m_consoleMessages.push_back("Error: " + errorMessage);
|
|
|
|
});
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
YR_RULES *yaraRules;
|
|
|
|
yr_compiler_get_rules(compiler, &yaraRules);
|
|
|
|
ON_SCOPE_EXIT { yr_rules_destroy(yaraRules); };
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
YR_MEMORY_BLOCK_ITERATOR iterator;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
struct ScanContext {
|
|
|
|
Task *task = nullptr;
|
|
|
|
std::vector<u8> buffer;
|
|
|
|
YR_MEMORY_BLOCK currBlock = {};
|
|
|
|
};
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
ScanContext context;
|
|
|
|
context.task = &task;
|
|
|
|
context.currBlock.base = 0;
|
|
|
|
context.currBlock.fetch_data = [](auto *block) -> const u8 * {
|
|
|
|
auto &context = *static_cast<ScanContext *>(block->context);
|
|
|
|
auto provider = ImHexApi::Provider::get();
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
context.buffer.resize(context.currBlock.size);
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
if (context.buffer.empty())
|
|
|
|
return nullptr;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
block->size = context.currBlock.size;
|
|
|
|
provider->read(context.currBlock.base + provider->getBaseAddress(), context.buffer.data(), context.buffer.size());
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return context.buffer.data();
|
|
|
|
};
|
|
|
|
iterator.file_size = [](auto *iterator) -> u64 {
|
|
|
|
hex::unused(iterator);
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return ImHexApi::Provider::get()->getActualSize();
|
|
|
|
};
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
iterator.context = &context;
|
|
|
|
iterator.first = [](YR_MEMORY_BLOCK_ITERATOR *iterator) -> YR_MEMORY_BLOCK *{
|
|
|
|
auto &context = *static_cast<ScanContext *>(iterator->context);
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
context.currBlock.base = 0;
|
|
|
|
context.currBlock.size = 0;
|
|
|
|
context.buffer.clear();
|
|
|
|
iterator->last_error = ERROR_SUCCESS;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return iterator->next(iterator);
|
|
|
|
};
|
|
|
|
iterator.next = [](YR_MEMORY_BLOCK_ITERATOR *iterator) -> YR_MEMORY_BLOCK * {
|
|
|
|
auto &context = *static_cast<ScanContext *>(iterator->context);
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
u64 address = context.currBlock.base + context.currBlock.size;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
iterator->last_error = ERROR_SUCCESS;
|
|
|
|
context.currBlock.base = address;
|
2023-03-16 13:35:09 +01:00
|
|
|
context.currBlock.size = std::min<size_t>(ImHexApi::Provider::get()->getActualSize() - address, 10_MiB);
|
2023-01-27 12:17:10 +01:00
|
|
|
context.currBlock.context = &context;
|
|
|
|
context.task->update(address);
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
if (context.currBlock.size == 0) return nullptr;
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return &context.currBlock;
|
|
|
|
};
|
2021-02-26 13:35:19 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
yr_rules_scan_mem_blocks(
|
2023-01-28 14:15:15 +01:00
|
|
|
yaraRules, &iterator, 0, [](YR_SCAN_CONTEXT *context, int message, void *data, void *userData) -> int {
|
2023-01-27 12:17:10 +01:00
|
|
|
auto &results = *static_cast<ResultContext *>(userData);
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
switch (message) {
|
|
|
|
case CALLBACK_MSG_RULE_MATCHING:
|
|
|
|
{
|
|
|
|
auto rule = static_cast<YR_RULE *>(data);
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
YR_STRING *string;
|
|
|
|
YR_MATCH *match;
|
2022-03-15 23:48:49 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
if (rule->strings != nullptr) {
|
|
|
|
yr_rule_strings_foreach(rule, string) {
|
|
|
|
yr_string_matches_foreach(context, string, match) {
|
|
|
|
results.newMatches.push_back({ rule->identifier, string->identifier, u64(match->offset), size_t(match->match_length), false, 0, 0 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
results.newMatches.push_back({ rule->identifier, "", 0, 0, true, 0, 0 });
|
2022-03-15 23:48:49 +01:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2023-01-27 12:17:10 +01:00
|
|
|
break;
|
|
|
|
case CALLBACK_MSG_CONSOLE_LOG:
|
|
|
|
{
|
|
|
|
results.consoleMessages.emplace_back(static_cast<const char *>(data));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
return results.task->shouldInterrupt() ? CALLBACK_ABORT : CALLBACK_CONTINUE;
|
|
|
|
},
|
|
|
|
&resultContext,
|
|
|
|
0);
|
2022-02-02 21:08:46 +01:00
|
|
|
|
2023-01-27 12:17:10 +01:00
|
|
|
}
|
2023-01-28 14:15:15 +01:00
|
|
|
TaskManager::doLater([this, &matches, resultContext] {
|
|
|
|
for (const auto &match : matches) {
|
2022-10-28 14:32:56 +02:00
|
|
|
ImHexApi::HexEditor::removeBackgroundHighlight(match.highlightId);
|
|
|
|
ImHexApi::HexEditor::removeTooltip(match.tooltipId);
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:48:49 +01:00
|
|
|
this->m_consoleMessages = resultContext.consoleMessages;
|
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
std::move(resultContext.newMatches.begin(), resultContext.newMatches.end(), std::back_inserter(matches));
|
2022-10-28 14:32:56 +02:00
|
|
|
|
2023-01-28 14:15:15 +01:00
|
|
|
auto uniques = std::set(matches.begin(), matches.end(), [](const auto &l, const auto &r) {
|
2023-03-14 17:02:59 +01:00
|
|
|
return std::tie(l.address, l.size, l.wholeDataMatch, l.identifier, l.variable) <
|
|
|
|
std::tie(r.address, r.size, r.wholeDataMatch, r.identifier, r.variable);
|
2022-10-28 14:32:56 +02:00
|
|
|
});
|
2023-01-28 14:15:15 +01:00
|
|
|
matches.clear();
|
|
|
|
std::move(uniques.begin(), uniques.end(), std::back_inserter(matches));
|
2022-10-28 14:32:56 +02:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
constexpr static color_t YaraColor = 0x70B4771F;
|
2023-01-28 14:27:51 +01:00
|
|
|
for (auto &match : uniques) {
|
2022-05-27 20:42:07 +02:00
|
|
|
match.highlightId = ImHexApi::HexEditor::addBackgroundHighlight({ match.address, match.size }, YaraColor);
|
|
|
|
match.tooltipId = ImHexApi::HexEditor::addTooltip({ match. address, match.size }, hex::format("{0} [{1}]", match.identifier, match.variable), YaraColor);
|
2022-03-15 23:48:49 +01:00
|
|
|
}
|
|
|
|
});
|
2022-08-17 16:15:36 +02:00
|
|
|
});
|
2021-02-26 13:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|