diff --git a/include/retdec/fileformat/types/visual_basic/visual_basic_info.h b/include/retdec/fileformat/types/visual_basic/visual_basic_info.h index dd14154e8c..962e8e3673 100644 --- a/include/retdec/fileformat/types/visual_basic/visual_basic_info.h +++ b/include/retdec/fileformat/types/visual_basic/visual_basic_info.h @@ -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; @@ -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 @@ -110,6 +123,8 @@ class VisualBasicInfo bool hasProjectDescription() const; bool hasProjectHelpFile() const; bool isPcode() const; + void computeExternTableHashes(); + void computeObjectTableHashes(); /// @} }; diff --git a/src/fileformat/file_format/pe/pe_format.cpp b/src/fileformat/file_format/pe/pe_format.cpp index 4031e09f8b..a76a1decbb 100644 --- a/src/fileformat/file_format/pe/pe_format.cpp +++ b/src/fileformat/file_format/pe/pe_format.cpp @@ -972,6 +972,8 @@ bool PeFormat::parseVisualBasicExternTable(std::size_t structureOffset, std::siz visualBasicInfo.addExtern(std::move(ext)); } + visualBasicInfo.computeExternTableHashes(); + return true; } @@ -1057,6 +1059,7 @@ bool PeFormat::parseVisualBasicObjectTable(std::size_t structureOffset, std::siz parseVisualBasicObjects(objectDescriptorsOffset, baseAddress, vbot.nObjects); } + visualBasicInfo.computeObjectTableHashes(); return true; } diff --git a/src/fileformat/types/visual_basic/visual_basic_info.cpp b/src/fileformat/types/visual_basic/visual_basic_info.cpp index c554fa6688..6169c8eaf4 100644 --- a/src/fileformat/types/visual_basic/visual_basic_info.cpp +++ b/src/fileformat/types/visual_basic/visual_basic_info.cpp @@ -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 { @@ -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 @@ -424,6 +483,81 @@ bool VisualBasicInfo::isPcode() const return pcodeFlag; } +/** + * Compute external table hashes - CRC32, MD5, SHA256. + */ +void VisualBasicInfo::computeExternTableHashes() +{ + std::vector 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(',')); + } + + for(const auto c : std::string(apiName + "." + moduleName)) + { + hashBytes.push_back(static_cast(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 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(',')); + } + + for(const auto c : std::string(objName + "." + methods)) + { + hashBytes.push_back(static_cast(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 diff --git a/src/fileinfo/file_information/file_information.cpp b/src/fileinfo/file_information/file_information.cpp index b5e31797a2..3962e81c92 100644 --- a/src/fileinfo/file_information/file_information.cpp +++ b/src/fileinfo/file_information/file_information.cpp @@ -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(); +} + diff --git a/src/fileinfo/file_information/file_information.h b/src/fileinfo/file_information/file_information.h index 955305f93f..79f4cb45c6 100644 --- a/src/fileinfo/file_information/file_information.h +++ b/src/fileinfo/file_information/file_information.h @@ -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; /// @} diff --git a/src/fileinfo/file_information/file_information_types/visual_basic_info.cpp b/src/fileinfo/file_information/file_information_types/visual_basic_info.cpp index 08ebf97854..51bdb8f2d0 100644 --- a/src/fileinfo/file_information/file_information_types/visual_basic_info.cpp +++ b/src/fileinfo/file_information/file_information_types/visual_basic_info.cpp @@ -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 diff --git a/src/fileinfo/file_information/file_information_types/visual_basic_info.h b/src/fileinfo/file_information/file_information_types/visual_basic_info.h index d94479470e..45e936b32b 100644 --- a/src/fileinfo/file_information/file_information_types/visual_basic_info.h +++ b/src/fileinfo/file_information/file_information_types/visual_basic_info.h @@ -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 diff --git a/src/fileinfo/file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp b/src/fileinfo/file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp index 7e2833b73c..e87425223d 100644 --- a/src/fileinfo/file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp +++ b/src/fileinfo/file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp @@ -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(); }