Skip to content

Commit

Permalink
OrcLib: FuzzyHash: add preprocessor ORC_BUILD_TLSH
Browse files Browse the repository at this point in the history
Maintainer choosed to restrict Windows support to mingw and the tlsh
feature is not being used. So it will be disable by default and removed
on any breaking change.
  • Loading branch information
fabienfl-orc committed Feb 10, 2021
1 parent 2018813 commit 3c625eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ option(ORC_BUILD_COMMAND "Build any OrcCommand based command" ON)
option(ORC_BUILD_FASTFIND "Build FastFind binary" ON)
option(ORC_BUILD_ORC "Build Orc binary" ON)
option(ORC_BUILD_PARQUET "Build Parquet module" OFF)
option(ORC_BUILD_TLSH "Build with tlsh support" OFF)
option(ORC_BUILD_SSDEEP "Build with ssdeep support" OFF)
option(ORC_BUILD_JSON "Build with JSON StructuredOutput enabled" ON)
option(ORC_BUILD_BOOST_STACKTRACE "Build with stack backtrace enabled" ON)
Expand Down Expand Up @@ -83,7 +84,6 @@ if(ORC_VCPKG_ROOT)
boost-scope-exit
fmt
spdlog
tlsh
yara
rapidjson
)
Expand All @@ -98,6 +98,11 @@ if(ORC_VCPKG_ROOT)
list(APPEND _PACKAGES boost-stacktrace)
endif()

if(ORC_BUILD_TLSH)
add_definitions(-DORC_BUILD_TLSH)
list(APPEND _PACKAGES tlsh)
endif()

if(ORC_BUILD_APACHE_ORC)
if("${CMAKE_GENERATOR_TOOLSET}" STREQUAL "v141_xp")
message(FATAL_ERROR "Apache Orc requires Seven or later")
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -57,6 +57,7 @@ cmake --build . --config MinSizeRel -- -maxcpucount
| ORC_BUILD_ORC | ON | Build Orc binary |
| ORC_BUILD_PARQUET | OFF | Build Parquet module (x64) |
| ORC_BUILD_SSDEEP | OFF | Build with ssdeep support |
| ORC_BUILD_TLSH | OFF | Build with tslh support |
| ORC_BUILD_JSON | ON | Build with JSON enabled |
| ORC_USE_STATIC_CRT | ON | Use static runtime |
| ORC_VCPKG_ROOT | ${ORC}/external/vcpkg | VCPKG root directory |
Expand Down
7 changes: 5 additions & 2 deletions src/OrcLib/CMakeLists.txt
Expand Up @@ -13,7 +13,6 @@ orc_add_compile_options()
find_package(7zip CONFIG REQUIRED)
find_package(Boost REQUIRED)
find_package(fmt REQUIRED)
find_package(tlsh CONFIG REQUIRED)
find_package(VisualStudio REQUIRED)
find_package(Yara REQUIRED)

Expand Down Expand Up @@ -864,7 +863,6 @@ target_link_libraries(OrcLib
Wintrust.lib
Crypt32.lib
fmt::fmt-header-only
tlsh::tlsh
VisualStudio::CppUnitTest
yara::yara
ws2_32.lib
Expand All @@ -874,6 +872,11 @@ target_link_libraries(OrcLib
find_package(RapidJSON CONFIG REQUIRED)
target_include_directories(OrcLib PRIVATE ${RAPIDJSON_INCLUDE_DIRS})

if(ORC_BUILD_TLSH)
find_package(tlsh CONFIG REQUIRED)
target_link_libraries(OrcLib PUBLIC tlsh::tlsh)
endif()

if(ORC_BUILD_SSDEEP)
find_package(ssdeep CONFIG REQUIRED)
target_link_libraries(OrcLib PUBLIC ssdeep::fuzzy)
Expand Down
33 changes: 27 additions & 6 deletions src/OrcLib/FuzzyHashStream.cpp
Expand Up @@ -11,7 +11,9 @@
#include "WideAnsi.h"
#include "BinaryBuffer.h"

#include "tlsh/tlsh.h"
#ifdef ORC_BUILD_TLSH
# include "tlsh/tlsh.h"
#endif // ORC_BUILD_TLSH

#ifdef ORC_BUILD_SSDEEP
# include "ssdeep/fuzzy.h"
Expand All @@ -27,10 +29,14 @@ FuzzyHashStream::Algorithm FuzzyHashStream::GetSupportedAlgorithm(LPCWSTR szAlgo
return Algorithm::SSDeep;
}
#endif // ORC_BUILD_SSDEEP

#ifdef ORC_BUILD_TSLH
if (!_wcsnicmp(szAlgo, L"tlsh", wcslen(L"tlsh")))
{
return Algorithm::TLSH;
}
#endif // ORC_BUILD_TLSH

return Algorithm::Undefined;
}

Expand All @@ -46,13 +52,17 @@ std::wstring FuzzyHashStream::GetSupportedAlgorithm(Algorithm algs)
retval.append(L"SSDeep");
}
#endif // ORC_BUILD_SSDEEP

#ifdef ORC_BUILD_TLSH
if (HasFlag(algs, FuzzyHashStream::Algorithm::TLSH))
{
if (retval.empty())
retval.append(L"TLSH");
else
retval.append(L",TLSH");
}
#endif // ORC_BUILD_TLSH

return retval;
}

Expand Down Expand Up @@ -100,21 +110,18 @@ HRESULT FuzzyHashStream::OpenToWrite(FuzzyHashStream::Algorithm algs, const std:

STDMETHODIMP FuzzyHashStream::Close()
{
#ifdef ORC_BUILD_TLSH
if (m_tlsh)
{
m_tlsh->final();
}
#endif // ORC_BUILD_TLSH

return HashStream::Close();
}

HRESULT FuzzyHashStream::ResetHash(bool bContinue)
{
if (m_tlsh)
{
m_tlsh->reset();
}

#ifdef ORC_BUILD_SSDEEP
if (m_ssdeep)
{
Expand All @@ -128,20 +135,30 @@ HRESULT FuzzyHashStream::ResetHash(bool bContinue)
}
#endif // ORC_BUILD_SSDEEP

#ifdef ORC_BUILD_TLSH
if (m_tlsh)
{
m_tlsh->reset();
}

if (HasFlag(m_Algorithms, FuzzyHashStream::Algorithm::TLSH))
{
m_tlsh = std::make_unique<Tlsh>();
}
#endif // ORC_BUILD_TLSH

m_bHashIsValid = true;
return S_OK;
}

HRESULT FuzzyHashStream::HashData(LPBYTE pBuffer, DWORD dwBytesToHash)
{
#ifdef ORC_BUILD_TLSH
if (m_tlsh)
{
m_tlsh->update(pBuffer, dwBytesToHash);
}
#endif // ORC_BUILD_TLSH

#ifdef ORC_BUILD_SSDEEP
if (m_ssdeep)
Expand Down Expand Up @@ -173,6 +190,7 @@ HRESULT FuzzyHashStream::GetHash(FuzzyHashStream::Algorithm alg, CBinaryBuffer&
#endif // ORC_BUILD_SSDEEP
break;
case FuzzyHashStream::Algorithm::TLSH:
#ifdef ORC_BUILD_TLSH
if (HasFlag(m_Algorithms, FuzzyHashStream::Algorithm::TLSH) && m_tlsh)
{
if (!m_tlsh->isValid())
Expand All @@ -187,6 +205,7 @@ HRESULT FuzzyHashStream::GetHash(FuzzyHashStream::Algorithm alg, CBinaryBuffer&
}
return S_OK;
}
#endif // ORC_BUILD_TLSH
break;
default:
return E_INVALIDARG;
Expand Down Expand Up @@ -222,5 +241,7 @@ FuzzyHashStream::~FuzzyHashStream()
}
#endif // ORC_BUILD_SSDEEP

#ifdef ORC_BUILD_TLSH
m_tlsh.reset();
#endif
}
4 changes: 4 additions & 0 deletions src/OrcLib/FuzzyHashStream.h
Expand Up @@ -50,7 +50,11 @@ class FuzzyHashStream : public HashStream

protected:
Algorithm m_Algorithms = Algorithm::Undefined;

#ifdef ORC_BUILD_TLSH
std::unique_ptr<Tlsh> m_tlsh;
#endif // ORC_BUILD_TLSH

struct fuzzy_state* m_ssdeep = nullptr;
};

Expand Down

0 comments on commit 3c625eb

Please sign in to comment.