Skip to content

Commit

Permalink
Fix empty input file hashes and filenames on IDA 7.5 and lower
Browse files Browse the repository at this point in the history
IDA 7.6 changed the way IDB metadata is stored. 7.5 and lower used to store
the data in the `root_node` netnode. Instead, newer versions implement this
using the `getinf_buf()` API.

This should address [b/179181035](https://issuetracker.google.com/179181035).

PiperOrigin-RevId: 372540288
Change-Id: I2e1b21b1ac98c274b5ba8a3d561255c14b21b770
  • Loading branch information
cblichmann authored and copybara-github committed May 7, 2021
1 parent 00c77be commit 933e158
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 14 additions & 4 deletions ida/digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// clang-format off
#include "third_party/zynamics/binexport/ida/begin_idasdk.inc" // NOLINT
#include <nalt.hpp> // NOLINT
#include <netnode.hpp> // NOLINT
#include "third_party/zynamics/binexport/ida/end_idasdk.inc" // NOLINT
// clang-format on

Expand All @@ -27,16 +28,25 @@
namespace security::binexport {

absl::StatusOr<std::string> GetInputFileSha256() {
std::string hash(32, '\0');
if (!retrieve_input_file_sha256(reinterpret_cast<uchar*>(&hash[0]))) {
constexpr int kNumSha256Bytes = 32;
std::string hash(kNumSha256Bytes, '\0');
auto* hash_uchar = reinterpret_cast<uchar*>(&hash[0]);
if (!retrieve_input_file_sha256(hash_uchar) &&
// b/186782665: IDA 7.5 and lower use the root_node instead.
(root_node.supval(RIDX_SHA256, hash_uchar, kNumSha256Bytes) !=
kNumSha256Bytes)) {
return absl::InternalError("Failed to load SHA256 hash of input file");
}
return absl::AsciiStrToLower(absl::BytesToHexString(hash));
}

absl::StatusOr<std::string> GetInputFileMd5() {
std::string hash(32, '\0');
if (!retrieve_input_file_md5(reinterpret_cast<uchar*>(&hash[0]))) {
constexpr int kNumMd5Bytes = 16;
std::string hash(kNumMd5Bytes, '\0');
auto* hash_uchar = reinterpret_cast<uchar*>(&hash[0]);
if (!retrieve_input_file_md5(hash_uchar) &&
// b/186782665: IDA 7.5 and lower use the root_node instead.
(root_node.supval(RIDX_MD5, hash_uchar, kNumMd5Bytes) != kNumMd5Bytes)) {
return absl::InternalError("Failed to load MD5 hash of input file");
}
return absl::AsciiStrToLower(absl::BytesToHexString(hash));
Expand Down
12 changes: 9 additions & 3 deletions ida/names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <ida.hpp> // NOLINT
#include <lines.hpp> // NOLINT
#include <name.hpp> // NOLINT
#include <nalt.hpp> // NOLINT
#include <netnode.hpp> // NOLINT
#include <segment.hpp> // NOLINT
#include <struct.hpp> // NOLINT
#include <typeinf.hpp> // NOLINT
Expand Down Expand Up @@ -116,9 +118,13 @@ absl::optional<std::string> GetArchitectureName() {
int GetArchitectureBitness() { return inf_is_64bit() ? 64 : 32; }

std::string GetModuleName() {
char path_buffer[QMAXPATH] = {0};
get_input_file_path(path_buffer, QMAXPATH);
return Basename(path_buffer);
std::string path(QMAXPATH, '\0');
if (get_input_file_path(&path[0], QMAXPATH) == 0) {
// b/186782665: IDA 7.5 and lower use the root_node instead.
root_node.valstr(&path[0], QMAXPATH);
}
path.resize(std::strlen(path.data()));
return Basename(path);
}

int GetOriginalIdaLine(const Address address, std::string* line) {
Expand Down

0 comments on commit 933e158

Please sign in to comment.