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

Implemented computation of export hashes #321

Merged
merged 4 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions include/retdec/fileformat/file_format/file_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
void loadStrings(StringType type, std::size_t charSize);
void loadStrings(StringType type, std::size_t charSize, const SecSeg* secSeg);
void loadImpHash();
void loadExpHash();
bool isInValidState() const;
LoadFlags getLoadFlags() const;
/// @}
Expand Down
1 change: 1 addition & 0 deletions include/retdec/fileformat/types/export_table/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Export

/// @name Other methods
/// @{
virtual bool isUsedForExphash() const;
void invalidateOrdinalNumber();
bool hasEmptyName() const;
/// @}
Expand Down
9 changes: 8 additions & 1 deletion include/retdec/fileformat/types/export_table/export_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ class ExportTable
{
private:
using exportsIterator = std::vector<Export>::const_iterator;
std::vector<Export> exports; ///< stored exports
std::vector<Export> exports; ///< stored exports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use tabs only to indent code. If you want to add comments at the end of the line, align it with spaces please.

std::string expHashCrc32; ///< exphash CRC32
std::string expHashMd5; ///< exphash MD5
std::string expHashSha256; ///< exphash SHA256
public:
ExportTable();
~ExportTable();

/// @name Getters
/// @{
std::size_t getNumberOfExports() const;
const std::string& getExphashCrc32() const;
const std::string& getExphashMd5() const;
const std::string& getExphashSha256() const;
const Export* getExport(std::size_t exportIndex) const;
const Export* getExport(const std::string &name) const;
const Export* getExportOnAddress(unsigned long long address) const;
Expand All @@ -42,6 +48,7 @@ class ExportTable

/// @name Other methods
/// @{
void computeHashes();
void clear();
void addExport(Export &newExport);
bool hasExports() const;
Expand Down
7 changes: 3 additions & 4 deletions include/retdec/fileformat/types/import_table/import_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ImportTable
using importsIterator = std::vector<std::unique_ptr<Import>>::const_iterator;
std::vector<std::string> libraries; ///< name of libraries
std::vector<std::unique_ptr<Import>> imports; ///< stored imports
std::vector<unsigned char> impHashBytes; ///< bytes for calculation of imphash
std::string impHashCrc32; ///< imphash CRC32
std::string impHashMd5; ///< imphash MD5
std::string impHashSha256; ///< imphash SHA256
Expand All @@ -39,9 +38,9 @@ class ImportTable
std::size_t getNumberOfImportsInLibrary(std::size_t libraryIndex) const;
std::size_t getNumberOfImportsInLibrary(const std::string &name) const;
std::size_t getNumberOfImportsInLibraryCaseInsensitive(const std::string &name) const;
std::string getImphashCrc32() const;
std::string getImphashMd5() const;
std::string getImphashSha256() const;
const std::string& getImphashCrc32() const;
const std::string& getImphashMd5() const;
const std::string& getImphashSha256() const;
std::string getLibrary(std::size_t libraryIndex) const;
const Import* getImport(std::size_t importIndex) const;
const Import* getImport(const std::string &name) const;
Expand Down
13 changes: 13 additions & 0 deletions src/fileformat/file_format/file_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,19 @@ void FileFormat::loadImpHash()
importTable->computeHashes();
}

/**
* Loads exphash from export table.
*/
void FileFormat::loadExpHash()
{
if (!exportTable || (loadFlags & LoadFlags::NO_VERBOSE_HASHES))
{
return;
}

exportTable->computeHashes();
}

/**
* Getter for state of instance
* @return @c true if all is OK, @c false otherwise
Expand Down
6 changes: 4 additions & 2 deletions src/fileformat/file_format/pe/pe_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ void PeFormat::loadImports()

loadImpHash();

for (auto&& addressRange : formatParser->getImportDirectoryOccupiedAddresses())
for(auto&& addressRange : formatParser->getImportDirectoryOccupiedAddresses())
{
nonDecodableRanges.addRange(std::move(addressRange));
}
Expand All @@ -733,7 +733,9 @@ void PeFormat::loadExports()
exportTable->addExport(newExport);
}

for (auto&& addressRange : formatParser->getExportDirectoryOccupiedAddresses())
loadExpHash();

for(auto&& addressRange : formatParser->getExportDirectoryOccupiedAddresses())
{
nonDecodableRanges.addRange(std::move(addressRange));
}
Expand Down
10 changes: 10 additions & 0 deletions src/fileformat/types/export_table/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ void Export::setOrdinalNumber(unsigned long long exportOrdinalNumber)
ordinalNumberIsValid = true;
}

/**
* Virtual method which indicates whether export should be used
* for calculating exphash.
* @return `true` if should be used, otherwise `false`.
*/
bool Export::isUsedForExphash() const
{
return true;
}

/**
* Invalidate ordinal number of export
*
Expand Down
83 changes: 83 additions & 0 deletions src/fileformat/types/export_table/export_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/

#include "retdec/crypto/crypto.h"
#include "retdec/utils/string.h"
#include "retdec/utils/conversion.h"
#include "retdec/fileformat/types/export_table/export_table.h"

Expand Down Expand Up @@ -37,6 +39,33 @@ std::size_t ExportTable::getNumberOfExports() const
return exports.size();
}

/**
* Get exphash as CRC32
* @return Exphash as CRC32
*/
const std::string& ExportTable::getExphashCrc32() const
{
return expHashCrc32;
}

/**
* Get exphash as MD5
* @return Exphash as MD5
*/
const std::string& ExportTable::getExphashMd5() const
{
return expHashMd5;
}

/**
* Get exphash as SHA256
* @return Exphash as SHA256
*/
const std::string& ExportTable::getExphashSha256() const
{
return expHashSha256;
}

/**
* Get selected export
* @param exportIndex Index of selected export (indexed from 0)
Expand Down Expand Up @@ -101,6 +130,60 @@ ExportTable::exportsIterator ExportTable::end() const
return exports.end();
}

/**
* Compute export hashes - CRC32, MD5, SHA256.
*/
void ExportTable::computeHashes()
{
std::vector<std::string> funcNames;
std::vector<std::uint8_t> expHashBytes;

for(const auto& newExport : exports)
{
if(!newExport.isUsedForExphash())
{
continue;
}

auto funcName = toLower(newExport.getName());

// convert ordinal to export name
if(funcName.empty())
{
unsigned long long ord;
if(newExport.getOrdinalNumber(ord))
{
funcName = toLower("ord" + std::to_string(ord));
}
}

if(!funcName.empty())
{
funcNames.push_back(funcName);
}
}

std::sort(funcNames.begin(), funcNames.end());

for(const auto& funcName : funcNames)
{
// Yara adds comma if there are multiple imports
if(!expHashBytes.empty())
{
expHashBytes.push_back(static_cast<unsigned char>(','));
}

for(const auto c : std::string(funcName))
{
expHashBytes.push_back(static_cast<unsigned char>(c));
}
}

expHashCrc32 = retdec::crypto::getCrc32(expHashBytes.data(), expHashBytes.size());
expHashMd5 = retdec::crypto::getMd5(expHashBytes.data(), expHashBytes.size());
expHashSha256 = retdec::crypto::getSha256(expHashBytes.data(), expHashBytes.size());
}

/**
* Delete all records from table
*/
Expand Down
7 changes: 3 additions & 4 deletions src/fileformat/types/import_table/import_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ std::size_t ImportTable::getNumberOfImportsInLibraryCaseInsensitive(const std::s
* Get imphash as CRC32
* @return Imphash as CRC32
*/
std::string ImportTable::getImphashCrc32() const
const std::string& ImportTable::getImphashCrc32() const
{
return impHashCrc32;
}
Expand All @@ -677,7 +677,7 @@ std::string ImportTable::getImphashCrc32() const
* Get imphash as MD5
* @return Imphash as MD5
*/
std::string ImportTable::getImphashMd5() const
const std::string& ImportTable::getImphashMd5() const
{
return impHashMd5;
}
Expand All @@ -686,7 +686,7 @@ std::string ImportTable::getImphashMd5() const
* Get imphash as SHA256
* @return Imphash as SHA256
*/
std::string ImportTable::getImphashSha256() const
const std::string& ImportTable::getImphashSha256() const
{
return impHashSha256;
}
Expand Down Expand Up @@ -826,7 +826,6 @@ void ImportTable::computeHashes()
*/
void ImportTable::clear()
{
impHashBytes.clear();
libraries.clear();
imports.clear();
impHashCrc32.clear();
Expand Down
27 changes: 27 additions & 0 deletions src/fileinfo/file_information/file_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,33 @@ std::size_t FileInformation::getNumberOfStoredExports() const
return exportTable.getNumberOfExports();
}

/**
* Get exphash as CRC32
* @return Exphash as CRC32
*/
std::string FileInformation::getExphashCrc32() const
{
return exportTable.getExphashCrc32();
}

/**
* Get exphash as MD5
* @return Exphash as MD5
*/
std::string FileInformation::getExphashMd5() const
{
return exportTable.getExphashMd5();
}

/**
* Get exphash as SHA256
* @return Exphash as SHA256
*/
std::string FileInformation::getExphashSha256() const
{
return exportTable.getExphashSha256();
}

/**
* Get export name
* @param position Index of selected export (indexed from 0)
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 @@ -179,6 +179,9 @@ class FileInformation
/// @name Getters of @a exportTable
/// @{
std::size_t getNumberOfStoredExports() const;
std::string getExphashCrc32() const;
std::string getExphashMd5() const;
std::string getExphashSha256() const;
std::string getExportName(std::size_t position) const;
std::string getExportAddressStr(std::size_t position, std::ios_base &(* format)(std::ios_base &)) const;
std::string getExportOrdinalNumberStr(std::size_t position, std::ios_base &(* format)(std::ios_base &)) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ std::size_t ExportTable::getNumberOfExports() const
return table ? table->getNumberOfExports() : 0;
}

/**
* Get exphash as CRC32
* @return Exphash as CRC32
*/
std::string ExportTable::getExphashCrc32() const
{
return table ? table->getExphashCrc32() : "";
}

/**
* Get exphash as MD5
* @return Exphash as MD5
*/
std::string ExportTable::getExphashMd5() const
{
return table ? table->getExphashMd5() : "";
}

/**
* Get exphash as SHA256
* @return Exphash as SHA256
*/
std::string ExportTable::getExphashSha256() const
{
return table ? table->getExphashSha256() : "";
}

/**
* Get export name
* @param position Index of selected export from table (indexed from 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ExportTable
/// @name Getters
/// @{
std::size_t getNumberOfExports() const;
std::string getExphashCrc32() const;
std::string getExphashMd5() const;
std::string getExphashSha256() const;
std::string getExportName(std::size_t position) const;
std::string getExportAddressStr(std::size_t position, std::ios_base &(* format)(std::ios_base &)) const;
std::string getExportOrdinalNumberStr(std::size_t position, std::ios_base &(* format)(std::ios_base &)) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ std::size_t ExportTablePlainGetter::getBasicInfo(std::size_t structIndex, std::v
info.clear();

desc.push_back("Number of exports: ");
desc.push_back("CRC32 : ");
desc.push_back("MD5 : ");
desc.push_back("SHA256 : ");
info.push_back(numToStr(fileinfo.getNumberOfStoredExports()));
info.push_back(fileinfo.getExphashCrc32());
info.push_back(fileinfo.getExphashMd5());
info.push_back(fileinfo.getExphashSha256());

return info.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ std::size_t ExportTableJsonGetter::getBasicInfo(std::size_t structIndex, std::ve
info.clear();

desc.push_back("numberOfExports");
desc.push_back("crc32");
desc.push_back("md5");
desc.push_back("sha256");
info.push_back(numToStr(fileinfo.getNumberOfStoredExports()));
info.push_back(fileinfo.getImphashCrc32());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be exphash, not imphash. Same for all three of them.

info.push_back(fileinfo.getImphashMd5());
info.push_back(fileinfo.getImphashSha256());

return info.size();
}
Expand Down