Skip to content

Commit

Permalink
extern and object table hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
pruzko committed Jan 31, 2019
1 parent a749f11 commit 212b06a
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 4 deletions.
15 changes: 15 additions & 0 deletions include/retdec/fileformat/types/visual_basic/visual_basic_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class VisualBasicInfo
// std::uint32_t typeLibCLSID; 16BYTES
std::uint32_t typeLibLCID;

std::string externTableHashCrc32;
std::string externTableHashMd5;
std::string externTableHashSha256;
std::string objectTableHashCrc32;
std::string objectTableHashMd5;
std::string objectTableHashSha256;

bool validLanguageDLLPrimaryLCID;
bool validLanguageDLLSecondaryLCID;
bool validProjectPrimaryLCID;
Expand Down Expand Up @@ -82,6 +89,12 @@ class VisualBasicInfo
std::size_t getNumberOfExterns() const;
// bool getTypeLibCLSID(std::uint32_t &res) const;
bool getTypeLibLCID(std::uint32_t &res) const;
const std::string &getExternTableHashCrc32() const;
const std::string &getExternTableHashMd5() const;
const std::string &getExternTableHashSha256() const;
const std::string &getObjectTableHashCrc32() const;
const std::string &getObjectTableHashMd5() const;
const std::string &getObjectTableHashSha256() const;
/// @}

/// @name Setters
Expand Down Expand Up @@ -110,6 +123,8 @@ class VisualBasicInfo
bool hasProjectDescription() const;
bool hasProjectHelpFile() const;
bool isPcode() const;
void computeExternTableHashes();
void computeObjectTableHashes();
/// @}
};

Expand Down
3 changes: 3 additions & 0 deletions src/fileformat/file_format/pe/pe_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ bool PeFormat::parseVisualBasicExternTable(std::size_t structureOffset, std::siz
visualBasicInfo.addExtern(std::move(ext));
}

visualBasicInfo.computeExternTableHashes();

return true;
}

Expand Down Expand Up @@ -1057,6 +1059,7 @@ bool PeFormat::parseVisualBasicObjectTable(std::size_t structureOffset, std::siz
parseVisualBasicObjects(objectDescriptorsOffset, baseAddress, vbot.nObjects);
}

visualBasicInfo.computeObjectTableHashes();
return true;
}

Expand Down
134 changes: 134 additions & 0 deletions src/fileformat/types/visual_basic/visual_basic_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/

#include "retdec/crypto/crypto.h"
#include "retdec/utils/string.h"
#include "retdec/fileformat/types/visual_basic/visual_basic_info.h"


using namespace retdec::utils;

namespace retdec {
namespace fileformat {

Expand Down Expand Up @@ -238,6 +243,60 @@ bool VisualBasicInfo::getTypeLibLCID(std::uint32_t &res) const
return true;
}

/**
* Get extern table hash as CRC32
* @return Extern table hash
*/
const std::string &VisualBasicInfo::getExternTableHashCrc32() const
{
return externTableHashCrc32;
}

/**
* Get extern table hash as MD5
* @return Extern table hash
*/
const std::string &VisualBasicInfo::getExternTableHashMd5() const
{
return externTableHashMd5;
}

/**
* Get extern table hash as SHA256
* @return Extern table hash
*/
const std::string &VisualBasicInfo::getExternTableHashSha256() const
{
return externTableHashSha256;
}

/**
* Get object table hash as CRC32
* @return Object table hash
*/
const std::string &VisualBasicInfo::getObjectTableHashCrc32() const
{
return objectTableHashCrc32;
}

/**
* Get object table hash as MD5
* @return Object table hash
*/
const std::string &VisualBasicInfo::getObjectTableHashMd5() const
{
return objectTableHashMd5;
}

/**
* Get object table hash as SHA256
* @return Object table hash
*/
const std::string &VisualBasicInfo::getObjectTableHashSha256() const
{
return objectTableHashSha256;
}

/**
* Set language DLL
* @param lDLL Language DLL to set
Expand Down Expand Up @@ -424,6 +483,81 @@ bool VisualBasicInfo::isPcode() const
return pcodeFlag;
}

/**
* Compute external table hashes - CRC32, MD5, SHA256.
*/
void VisualBasicInfo::computeExternTableHashes()
{
std::vector<std::uint8_t> hashBytes;

for (const auto& ext : externs)
{
auto moduleName = toLower(ext->getModuleName());
auto apiName = toLower(ext->getApiName());

if(apiName.empty() || moduleName.empty())
{
continue;
}

if(!hashBytes.empty())
{
hashBytes.push_back(static_cast<unsigned char>(','));
}

for(const auto c : std::string(apiName + "." + moduleName))
{
hashBytes.push_back(static_cast<unsigned char>(c));
}
}

externTableHashCrc32 = retdec::crypto::getCrc32(hashBytes.data(), hashBytes.size());
externTableHashMd5 = retdec::crypto::getMd5(hashBytes.data(), hashBytes.size());
externTableHashSha256 = retdec::crypto::getSha256(hashBytes.data(), hashBytes.size());
}

/**
* Compute object table hashes - CRC32, MD5, SHA256.
*/
void VisualBasicInfo::computeObjectTableHashes()
{
std::vector<std::uint8_t> hashBytes;

for (const auto& obj : objects)
{
auto objName = toLower(obj->getName());
if(objName.empty())
{
continue;
}

std::string methods = "";
for (const auto &method : obj->getMethods())
{
if (!methods.empty())
{
methods.push_back('.');
}

methods += method;
}

if(!hashBytes.empty())
{
hashBytes.push_back(static_cast<unsigned char>(','));
}

for(const auto c : std::string(objName + "." + methods))
{
hashBytes.push_back(static_cast<unsigned char>(c));
}
}

objectTableHashCrc32 = retdec::crypto::getCrc32(hashBytes.data(), hashBytes.size());
objectTableHashMd5 = retdec::crypto::getMd5(hashBytes.data(), hashBytes.size());
objectTableHashSha256 = retdec::crypto::getSha256(hashBytes.data(), hashBytes.size());
}


} // namespace fileformat
} // namespace retdec
54 changes: 54 additions & 0 deletions src/fileinfo/file_information/file_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,60 @@ std::string FileInformation::getVisualBasicTypeLibLCIDStr() const
return visualBasicInfo.getTypeLibLCIDStr();
}

/**
* Get visual basic extern table hash as Crc32
* @return Visual basic extern table hash
*/
std::string FileInformation::getVisualBasicExternTableHashCrc32() const
{
return visualBasicInfo.getExternTableHashCrc32();
}

/**
* Get visual basic extern table hash as Md5
* @return Visual basic extern table hash
*/
std::string FileInformation::getVisualBasicExternTableHashMd5() const
{
return visualBasicInfo.getExternTableHashMd5();
}

/**
* Get visual basic extern table hash as Sha256
* @return Visual basic extern table hash
*/
std::string FileInformation::getVisualBasicExternTableHashSha256() const
{
return visualBasicInfo.getExternTableHashSha256();
}

/**
* Get visual basic object table hash as Crc32
* @return Visual basic object table hash
*/
std::string FileInformation::getVisualBasicObjectTableHashCrc32() const
{
return visualBasicInfo.getObjectTableHashCrc32();
}

/**
* Get visual basic object table hash as Md5
* @return Visual basic object table hash
*/
std::string FileInformation::getVisualBasicObjectTableHashMd5() const
{
return visualBasicInfo.getObjectTableHashMd5();
}

/**
* Get visual basic object table hash as Sha256
* @return Visual basic object table hash
*/
std::string FileInformation::getVisualBasicObjectTableHashSha256() const
{
return visualBasicInfo.getObjectTableHashSha256();
}




Expand Down
7 changes: 7 additions & 0 deletions src/fileinfo/file_information/file_information.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ class FileInformation
std::string getVisualBasicExternModuleName(std::size_t position) const;
std::string getVisualBasicExternApiName(std::size_t position) const;
std::string getVisualBasicTypeLibLCIDStr() const;
std::string getExternTableHashCrc32() const;
std::string getVisualBasicExternTableHashCrc32() const;
std::string getVisualBasicExternTableHashMd5() const;
std::string getVisualBasicExternTableHashSha256() const;
std::string getVisualBasicObjectTableHashCrc32() const;
std::string getVisualBasicObjectTableHashMd5() const;
std::string getVisualBasicObjectTableHashSha256() const;
/// @}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,60 @@ std::string VisualBasicInfo::getTypeLibLCIDStr() const
return getNumberAsString(lcid);
}

/**
* Get extern table hash as CRC32
* @return Extern table hash
*/
std::string VisualBasicInfo::getExternTableHashCrc32() const
{
return visualBasicInfo ? visualBasicInfo->getExternTableHashCrc32() : "";
}

/**
* Get extern table hash as MD5
* @return Extern table hash
*/
std::string VisualBasicInfo::getExternTableHashMd5() const
{
return visualBasicInfo ? visualBasicInfo->getExternTableHashMd5() : "";
}

/**
* Get extern table hash as SHA256
* @return Extern table hash
*/
std::string VisualBasicInfo::getExternTableHashSha256() const
{
return visualBasicInfo ? visualBasicInfo->getExternTableHashSha256() : "";
}

/**
* Get object table hash as CRC32
* @return Object table hash
*/
std::string VisualBasicInfo::getObjectTableHashCrc32() const
{
return visualBasicInfo ? visualBasicInfo->getObjectTableHashCrc32() : "";
}

/**
* Get object table hash as MD5
* @return Object table hash
*/
std::string VisualBasicInfo::getObjectTableHashMd5() const
{
return visualBasicInfo ? visualBasicInfo->getObjectTableHashMd5() : "";
}

/**
* Get object table hash as SHA256
* @return Object table hash
*/
std::string VisualBasicInfo::getObjectTableHashSha256() const
{
return visualBasicInfo ? visualBasicInfo->getObjectTableHashSha256() : "";
}

/**
* Set visual basic information
* @param vbInfo Instance of class with original information about visual basic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class VisualBasicInfo
std::string getExternApiName(std::size_t position) const;
// std::string getTypeLibCLSIDStr() const;
std::string getTypeLibLCIDStr() const;
std::string getExternTableHashCrc32() const;
std::string getExternTableHashMd5() const;
std::string getExternTableHashSha256() const;
std::string getObjectTableHashCrc32() const;
std::string getObjectTableHashMd5() const;
std::string getObjectTableHashSha256() const;
/// @}

/// @name Setters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ std::size_t VisualBasicExternTablePlainGetter::getBasicInfo(std::size_t structIn
desc.clear();
info.clear();

desc.push_back("Number of visual basic externs: ");
desc.push_back("Number of externs: ");
desc.push_back("CRC32 : ");
desc.push_back("MD5 : ");
desc.push_back("SHA256 : ");
info.push_back(numToStr(fileinfo.getVisualBasicNumberOfExterns()));
info.push_back("TODO");
info.push_back("TODO");
info.push_back("TODO");
info.push_back(fileinfo.getVisualBasicExternTableHashCrc32());
info.push_back(fileinfo.getVisualBasicExternTableHashMd5());
info.push_back(fileinfo.getVisualBasicExternTableHashSha256());

return info.size();
}
Expand Down

0 comments on commit 212b06a

Please sign in to comment.