Skip to content
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

Calculate Rich header hash #945

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/retdec/fileformat/types/rich_header/rich_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class RichHeader
bool isValidStructure = false; ///< @c true if header has valid structure
bool isSuspicious = false; ///< @c true if content of header is suspicious
std::vector<std::uint8_t> bytes; ///< decrypted content of rich header
/// hashes of decrypted rich header
std::string sha256;
std::string md5;
std::string crc32;
public:
/// @name Getters
/// @{
Expand All @@ -43,6 +47,9 @@ class RichHeader
const LinkerInfo* getLastRecord() const;
bool getValidStructure() const;
bool getSuspicious() const;
std::string getSha256() const;
std::string getCrc32() const;
std::string getMd5() const;
const std::vector<std::uint8_t>& getBytes() const;
/// @}

Expand All @@ -54,6 +61,9 @@ class RichHeader
void setValidStructure(bool richValidStructure);
void setSuspicious(bool richSuspicious);
void setBytes(const std::vector<std::uint8_t>& richHeaderBytes);
void setSha256(const std::string& sha256);
void setCrc32(const std::string& crc32);
void setMd5(const std::string& md5);
/// @}

/// @name Iterators
Expand Down
12 changes: 11 additions & 1 deletion src/fileformat/file_format/pe/pe_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,17 @@ void PeFormat::loadRichHeader()

richHeader->setKey(header.getKey());
richHeader->setSignature(signature);
richHeader->setBytes(header.getDecryptedHeaderBytes());

auto decrypted_bytes = header.getDecryptedHeaderBytes();
richHeader->setBytes(decrypted_bytes);

auto crc32 = retdec::fileformat::getCrc32(decrypted_bytes.data(), decrypted_bytes.size());
auto md5 = retdec::fileformat::getMd5(decrypted_bytes.data(), decrypted_bytes.size());
auto sha256 = retdec::fileformat::getSha256(decrypted_bytes.data(), decrypted_bytes.size());

richHeader->setCrc32(crc32);
richHeader->setMd5(md5);
richHeader->setSha256(sha256);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/fileformat/types/rich_header/rich_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ bool RichHeader::getSuspicious() const
return isSuspicious;
}

std::string RichHeader::getSha256() const
{
return sha256;
}
std::string RichHeader::getCrc32() const
{
return crc32;
}
std::string RichHeader::getMd5() const
{
return md5;
}

/**
* Returns the decrypted bytes of the rich header.
* @return Decrypted bytes of rich header.
Expand Down Expand Up @@ -167,6 +180,19 @@ void RichHeader::setSuspicious(bool richSuspicious)
isSuspicious = richSuspicious;
}

void RichHeader::setSha256(const std::string& sha256)
{
this->sha256 = sha256;
}
void RichHeader::setCrc32(const std::string& crc32)
{
this->crc32 = crc32;
}
void RichHeader::setMd5(const std::string& md5)
{
this->md5 = md5;
}

/**
* Sets the decrypted bytes of the rich header.
* @param richHeaderBytes Rich header bytes of the signature.
Expand Down
13 changes: 13 additions & 0 deletions src/fileinfo/file_information/file_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,19 @@ bool FileInformation::hasRichHeaderRecords() const
return richHeader.hasRecords();
}

std::string FileInformation::getRichHeaderSha256() const
{
return richHeader.getSha256();
}
std::string FileInformation::getRichHeaderCrc32() const
{
return richHeader.getCrc32();
}
std::string FileInformation::getRichHeaderMd5() const
{
return richHeader.getMd5();
}

/**
* Check whether visual basic informations are used.
* @return @c true if it is used, otherwise @c false/
Expand Down
3 changes: 3 additions & 0 deletions src/fileinfo/file_information/file_information.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class FileInformation
std::string getRichHeaderRecordProductNameStr(std::size_t position) const;
std::string getRichHeaderRecordVisualStudioNameStr(std::size_t position) const;
std::string getRichHeaderRawBytesStr() const;
std::string getRichHeaderSha256() const;
std::string getRichHeaderCrc32() const;
std::string getRichHeaderMd5() const;
bool hasRichHeaderRecords() const;
/// @}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ std::vector<std::uint8_t> RichHeader::getRawBytes() const
return header ? header->getBytes() : std::vector<std::uint8_t>{};
}

std::string RichHeader::getSha256() const
{
return header ? header->getSha256() : "";
}
std::string RichHeader::getCrc32() const
{
return header ? header->getCrc32() : "";
}
std::string RichHeader::getMd5() const
{
return header ? header->getMd5() : "";
}

/**
* Set rich header data
* @param richHeader Instance of class with original information about rich header
Expand All @@ -132,5 +145,7 @@ bool RichHeader::hasRecords() const
return header ? header->hasRecords() : false;
}



} // namespace fileinfo
} // namespace retdec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class RichHeader
std::string getRecordNumberOfUsesStr(std::size_t position) const;
std::string getRecordProductNameStr(std::size_t position) const;
std::string getRecordVisualStudioNameStr(std::size_t position) const;
std::string getSha256() const;
std::string getCrc32() const;
std::string getMd5() const;
std::vector<std::uint8_t> getRawBytes() const;
/// @}

Expand Down
9 changes: 9 additions & 0 deletions src/fileinfo/file_presentation/json_presentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ void JsonPresentation::presentRichHeader(Writer& writer) const
serializeString(writer, "offset", offset);
serializeString(writer, "key", key);
serializeString(writer, "signature", sig);

auto crc32 = fileinfo.getRichHeaderCrc32();
auto md5 = fileinfo.getRichHeaderMd5();
auto sha256 = fileinfo.getRichHeaderSha256();

serializeString(writer, "crc32", crc32);
serializeString(writer, "md5", md5);
serializeString(writer, "sha256", sha256);

writer.EndObject();
}

Expand Down
13 changes: 13 additions & 0 deletions src/fileinfo/file_presentation/plain_presentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,19 @@ void PlainPresentation::presentRichHeader() const
Log::info() << (i ? std::string(signDesc.length(), ' ') : signDesc) << sig.substr(i, signLineLen) << "\n";
}
}
auto crc32 = fileinfo.getRichHeaderCrc32();
auto md5 = fileinfo.getRichHeaderMd5();
auto sha256 = fileinfo.getRichHeaderSha256();

if (!crc32.empty()) {
Log::info() << "Rich header CRC32 : " << crc32 << "\n";
}
if (!md5.empty()) {
Log::info() << "Rich header MD5 : " << md5 << "\n";
}
if (!sha256.empty()) {
Log::info() << "Rich header SHA256 : " << sha256 << "\n";
}
}

/**
Expand Down