2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_information.hpp"
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2021-09-03 02:33:15 +02:00
|
|
|
#include <hex/helpers/paths.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2021-09-06 16:15:05 +02:00
|
|
|
#include <hex/helpers/literals.hpp>
|
2020-11-12 09:38:52 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
2020-11-12 21:21:11 +01:00
|
|
|
#include <filesystem>
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <numeric>
|
2020-11-12 21:21:11 +01:00
|
|
|
#include <span>
|
2021-02-22 13:08:06 +01:00
|
|
|
#include <thread>
|
2020-11-12 21:21:11 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
#include <hex/helpers/magic.hpp>
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-03-02 13:49:45 +01:00
|
|
|
#include <implot.h>
|
2021-02-22 13:08:06 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
using namespace hex::literals;
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewInformation::ViewInformation() : View("hex.builtin.view.information.name") {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventDataChanged>(this, [this]() {
|
2020-11-22 19:43:35 +01:00
|
|
|
this->m_dataValid = false;
|
|
|
|
this->m_highestBlockEntropy = 0;
|
|
|
|
this->m_blockEntropy.clear();
|
|
|
|
this->m_averageEntropy = 0;
|
|
|
|
this->m_blockSize = 0;
|
|
|
|
this->m_valueCounts.fill(0x00);
|
|
|
|
this->m_mimeType = "";
|
|
|
|
this->m_fileDescription = "";
|
|
|
|
this->m_analyzedRegion = { 0, 0 };
|
2020-11-12 09:38:52 +01:00
|
|
|
});
|
2021-04-12 22:20:05 +02:00
|
|
|
|
|
|
|
EventManager::subscribe<EventRegionSelected>(this, [this](Region region) {
|
2021-04-13 21:50:24 +02:00
|
|
|
if (this->m_blockSize != 0)
|
|
|
|
this->m_entropyHandlePosition = region.address / this->m_blockSize;
|
2021-04-12 22:20:05 +02:00
|
|
|
});
|
2021-12-05 22:09:43 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
EventManager::subscribe<EventFileUnloaded>(this, [this] {
|
2021-12-05 22:09:43 +01:00
|
|
|
this->m_dataValid = false;
|
|
|
|
});
|
2022-01-13 14:34:19 +01:00
|
|
|
|
|
|
|
ContentRegistry::FileHandler::add({ ".mgc" }, [](const auto &path) {
|
|
|
|
for (auto &destPath : hex::getPath(ImHexPath::Magic)) {
|
|
|
|
std::error_code error;
|
|
|
|
if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) {
|
|
|
|
View::showMessagePopup("hex.builtin.view.information.magic_db_added"_lang);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewInformation::~ViewInformation() {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<EventDataChanged>(this);
|
2021-04-12 22:20:05 +02:00
|
|
|
EventManager::unsubscribe<EventRegionSelected>(this);
|
2021-12-05 22:09:43 +01:00
|
|
|
EventManager::unsubscribe<EventFileUnloaded>(this);
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
static float calculateEntropy(std::array<ImU64, 256> &valueCounts, size_t blockSize) {
|
2020-11-12 09:38:52 +01:00
|
|
|
float entropy = 0;
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
for (auto count : valueCounts) {
|
|
|
|
if (count == 0) continue;
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
float probability = static_cast<float>(count) / blockSize;
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
entropy += probability * std::log2(probability);
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return (-entropy) / 8; // log2(256) = 8
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
void ViewInformation::analyze() {
|
|
|
|
this->m_analyzing = true;
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::thread([this] {
|
2021-09-21 02:29:54 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2020-12-27 15:39:06 +01:00
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
auto task = ImHexApi::Tasks::createTask("hex.builtin.view.information.analyzing", provider->getSize());
|
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
this->m_analyzedRegion = { provider->getBaseAddress(), provider->getBaseAddress() + provider->getSize() };
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
{
|
|
|
|
magic::compile();
|
|
|
|
|
|
|
|
this->m_fileDescription = magic::getDescription(provider);
|
|
|
|
this->m_mimeType = magic::getMIMEType(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_dataValid = true;
|
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
{
|
2021-03-02 13:49:45 +01:00
|
|
|
this->m_blockSize = std::max<u32>(std::ceil(provider->getSize() / 2048.0F), 256);
|
2021-02-22 13:08:06 +01:00
|
|
|
std::vector<u8> buffer(this->m_blockSize, 0x00);
|
|
|
|
std::memset(this->m_valueCounts.data(), 0x00, this->m_valueCounts.size() * sizeof(u32));
|
|
|
|
this->m_blockEntropy.clear();
|
2021-05-25 23:47:38 +02:00
|
|
|
this->m_valueCounts.fill(0);
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
for (u64 i = 0; i < provider->getSize(); i += this->m_blockSize) {
|
2021-03-02 16:45:39 +01:00
|
|
|
std::array<ImU64, 256> blockValueCounts = { 0 };
|
2021-12-09 21:10:24 +01:00
|
|
|
provider->read(i + provider->getBaseAddress(), buffer.data(), std::min(u64(this->m_blockSize), provider->getSize() - i));
|
2020-11-12 21:21:11 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
for (size_t j = 0; j < this->m_blockSize; j++) {
|
|
|
|
blockValueCounts[buffer[j]]++;
|
|
|
|
this->m_valueCounts[buffer[j]]++;
|
|
|
|
}
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
this->m_blockEntropy.push_back(calculateEntropy(blockValueCounts, this->m_blockSize));
|
2021-12-16 23:48:52 +01:00
|
|
|
task.update(i);
|
2021-02-22 13:08:06 +01:00
|
|
|
}
|
2020-11-12 21:21:11 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
this->m_averageEntropy = calculateEntropy(this->m_valueCounts, provider->getSize());
|
|
|
|
this->m_highestBlockEntropy = *std::max_element(this->m_blockEntropy.begin(), this->m_blockEntropy.end());
|
|
|
|
}
|
2020-11-12 21:21:11 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
this->m_analyzing = false;
|
|
|
|
}).detach();
|
|
|
|
}
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-02-22 13:08:06 +01:00
|
|
|
void ViewInformation::drawContent() {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.builtin.view.information.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-08-28 14:22:02 +02:00
|
|
|
if (ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav)) {
|
2020-11-15 16:06:10 +01:00
|
|
|
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.control"_lang);
|
2021-02-22 13:08:06 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::Disabled([this] {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Button("hex.builtin.view.information.analyze"_lang))
|
2021-08-28 14:22:02 +02:00
|
|
|
this->analyze();
|
2022-01-24 20:53:17 +01:00
|
|
|
},
|
|
|
|
this->m_analyzing);
|
2020-11-22 19:43:35 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (this->m_analyzing) {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextSpinner("hex.builtin.view.information.analyzing"_lang);
|
2021-12-12 00:41:44 +01:00
|
|
|
} else {
|
|
|
|
ImGui::NewLine();
|
2021-08-28 14:22:02 +02:00
|
|
|
}
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.region"_lang);
|
|
|
|
ImGui::Separator();
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
for (auto &[name, value] : provider->getDataInformation()) {
|
|
|
|
ImGui::LabelText(name.c_str(), "%s", value.c_str());
|
|
|
|
}
|
2021-02-22 13:08:06 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
if (this->m_dataValid) {
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::LabelText("hex.builtin.view.information.region"_lang, "0x%llx - 0x%llx", this->m_analyzedRegion.first, this->m_analyzedRegion.second);
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (!this->m_fileDescription.empty() || !this->m_mimeType.empty()) {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.magic"_lang);
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::Separator();
|
|
|
|
}
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (!this->m_fileDescription.empty()) {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.description"_lang);
|
2022-01-15 14:14:53 +01:00
|
|
|
ImGui::TextFormattedWrapped("{}", this->m_fileDescription.c_str());
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (!this->m_mimeType.empty()) {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.mime"_lang);
|
2022-01-15 14:14:53 +01:00
|
|
|
ImGui::TextFormattedWrapped("{}", this->m_mimeType.c_str());
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2021-04-12 22:20:05 +02:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.info_analysis"_lang);
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::Separator();
|
2021-04-12 22:20:05 +02:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImGui::GetColorU32(ImGuiCol_WindowBg));
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.distribution"_lang);
|
2021-12-12 00:41:44 +01:00
|
|
|
ImPlot::SetNextPlotLimits(0, 256, 0.5, float(*std::max_element(this->m_valueCounts.begin(), this->m_valueCounts.end())) * 1.1F, ImGuiCond_Always);
|
2022-01-24 20:53:17 +01:00
|
|
|
if (ImPlot::BeginPlot("##distribution", "Address", "Count", ImVec2(-1, 0), ImPlotFlags_NoLegend | ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect, ImPlotAxisFlags_Lock, ImPlotAxisFlags_Lock | ImPlotAxisFlags_LogScale)) {
|
|
|
|
static auto x = [] {
|
|
|
|
std::array<ImU64, 256> result { 0 };
|
2021-08-28 14:22:02 +02:00
|
|
|
std::iota(result.begin(), result.end(), 0);
|
|
|
|
return result;
|
|
|
|
}();
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImPlot::PlotBars<ImU64>("##bytes", x.data(), this->m_valueCounts.data(), x.size(), 0.67);
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImPlot::EndPlot();
|
2021-04-12 22:20:05 +02:00
|
|
|
}
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.view.information.entropy"_lang);
|
2021-03-02 13:49:45 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImPlot::SetNextPlotLimits(0, this->m_blockEntropy.size(), -0.1, 1.1, ImGuiCond_Always);
|
2022-01-24 20:53:17 +01:00
|
|
|
if (ImPlot::BeginPlot("##entropy", "Address", "Entropy", ImVec2(-1, 0), ImPlotFlags_CanvasOnly | ImPlotFlags_AntiAliased, ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoTickLabels, ImPlotAxisFlags_Lock)) {
|
2021-08-28 14:22:02 +02:00
|
|
|
ImPlot::PlotLine("##entropy_line", this->m_blockEntropy.data(), this->m_blockEntropy.size());
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (ImPlot::DragLineX("Position", &this->m_entropyHandlePosition, false)) {
|
|
|
|
u64 address = u64(this->m_entropyHandlePosition * this->m_blockSize) + provider->getBaseAddress();
|
|
|
|
address = std::min(address, provider->getBaseAddress() + provider->getSize() - 1);
|
2022-01-24 20:53:17 +01:00
|
|
|
EventManager::post<RequestSelectionChange>(Region { address, 1 });
|
2021-08-28 14:22:02 +02:00
|
|
|
}
|
2020-11-22 19:43:35 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImPlot::EndPlot();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopStyleColor();
|
2020-11-22 19:43:35 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::LabelText("hex.builtin.view.information.block_size"_lang, "%s", hex::format("hex.builtin.view.information.block_size.desc"_lang, this->m_blockEntropy.size(), this->m_blockSize).c_str());
|
|
|
|
ImGui::LabelText("hex.builtin.view.information.file_entropy"_lang, "%.8f", this->m_averageEntropy);
|
|
|
|
ImGui::LabelText("hex.builtin.view.information.highest_entropy"_lang, "%.8f", this->m_highestBlockEntropy);
|
2021-08-28 14:22:02 +02:00
|
|
|
|
|
|
|
if (this->m_averageEntropy > 0.83 && this->m_highestBlockEntropy > 0.9) {
|
|
|
|
ImGui::NewLine();
|
2022-01-15 14:14:53 +01:00
|
|
|
ImGui::TextFormattedColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F), "{}", "hex.builtin.view.information.encrypted"_lang);
|
2021-08-28 14:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 12:01:13 +01:00
|
|
|
}
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
2021-08-28 16:02:53 +02:00
|
|
|
ImGui::EndChild();
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|