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

TuxHook2: cross-platform NetHook2 port #1082

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Resources/TuxHook2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include/
deps/
build/
CMakeLists.txt.user
167 changes: 167 additions & 0 deletions Resources/TuxHook2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
cmake_minimum_required(VERSION 3.15)

project(tuxhook2 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)

# steam client is still a 32bit app
if(UNIX)
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)
elseif(WIN32)
set(CMAKE_C_FLAGS "/MD")
set(CMAKE_CXX_FLAGS "/MD")

find_library(PSAPI psapi)
if(PSAPI MATCHES "PSAPI-NOTFOUND")
message(FATAL_ERROR "Psapi not found!")
endif()
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE true)

set(PROTOBUF_VERSION 3.15.6)
set(FUNCHOOK_VERSION 1.1.0)

set(TuxHook_ROOT ${CMAKE_CURRENT_LIST_DIR})
set(TuxHook_SRC ${CMAKE_CURRENT_LIST_DIR}/TuxHook)

include(ExternalProject)
ExternalProject_Add(protobuf
DOWNLOAD_DIR ${TuxHook_ROOT}/deps/downloads
SOURCE_DIR ${TuxHook_ROOT}/deps/srcs/protobuf
SOURCE_SUBDIR ./cmake
BINARY_DIR ${TuxHook_ROOT}/deps/builds/protobuf
INSTALL_DIR ${TuxHook_ROOT}/include/protobuf
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-Dprotobuf_BUILD_TESTS=false
-Dprotobuf_MSVC_STATIC_RUNTIME=false
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_POSITION_INDEPENDENT_CODE=true
URL https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/protobuf-cpp-${PROTOBUF_VERSION}.zip
EXCLUDE_FROM_ALL true
)
if(UNIX AND NOT APPLE)
set(protobuf_LIB ${TuxHook_ROOT}/include/protobuf/lib/libprotobuf.a)
elseif(WIN32)
set(protobuf_LIB ${TuxHook_ROOT}/include/protobuf/lib/libprotobuf.lib)
endif()
set(protobuf_INCLUDE ${TuxHook_ROOT}/include/protobuf/include)


# funchook build seem to be messy with MSVC
set(FUNCHOOK_C_FLAGS "${CMAKE_C_FLAGS}")
if(WIN32)
# somebody forgot to use standard defined _WIN32 flag?
# fine can also define WIN32
set(FUNCHOOK_C_FLAGS " -DWIN32=1 ${FUNCHOOK_C_FLAGS}")
endif()
ExternalProject_Add(funchook
DOWNLOAD_DIR ${TuxHook_ROOT}/deps/downloads
SOURCE_DIR ${TuxHook_ROOT}/deps/srcs/funchook
BINARY_DIR ${TuxHook_ROOT}/deps/builds/funchook
INSTALL_DIR ${TuxHook_ROOT}/include/funchook
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DFUNCHOOK_BUILD_STATIC=true
-DFUNCHOOK_BUILD_TESTS=false
-DFUNCHOOK_BUILD_SHARED=false
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_C_FLAGS="${FUNCHOOK_C_FLAGS}"
-DCMAKE_POSITION_INDEPENDENT_CODE=true
URL https://github.com/kubo/funchook/releases/download/v${FUNCHOOK_VERSION}/funchook-${FUNCHOOK_VERSION}.zip
EXCLUDE_FROM_ALL true
)
# libdistorm is not copied to funchook libs during shared build install
# might just grub a copy from build artifacts ...
if(UNIX AND NOT APPLE)
set(funchook_LIB ${TuxHook_ROOT}/include/funchook/lib/libfunchook.a)
set(funchook_DISTORM_LIB ${TuxHook_ROOT}/deps/builds/funchook/libdistorm.a)
elseif(WIN32)
set(funchook_LIB ${TuxHook_ROOT}/include/funchook/lib/funchook.lib)
set(funchook_DISTORM_LIB ${TuxHook_ROOT}/deps/builds/funchook/Release/distorm.lib)
endif()
set(funchook_INCLUDE ${TuxHook_ROOT}/include/funchook/include)

ExternalProject_Add(zlib
DOWNLOAD_DIR ${TuxHook_ROOT}/deps/downloads
SOURCE_DIR ${TuxHook_ROOT}/deps/srcs/zlib
BINARY_DIR ${TuxHook_ROOT}/deps/builds/zlib
INSTALL_DIR ${TuxHook_ROOT}/include/zlib
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DBUILD_SHARED_LIBS=false
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_POSITION_INDEPENDENT_CODE=true
URL http://zlib.net/zlib1212.zip
EXCLUDE_FROM_ALL true
)
if(UNIX AND NOT APPLE)
set(zlib_LIB ${TuxHook_ROOT}/include/zlib/lib/libz.a)
elseif(WIN32)
set(zlib_LIB ${TuxHook_ROOT}/include/zlib/lib/zlibstatic.lib)
endif()
set(zlib_INCLUDE ${TuxHook_ROOT}/include/zlib/include)

add_custom_target(deps DEPENDS zlib funchook protobuf)
set_target_properties(deps PROPERTIES EXCLUDE_FROM_ALL true)

set(LIBSRC
${TuxHook_SRC}/tuxhook.cpp
${TuxHook_SRC}/clientmodule.h
${TuxHook_SRC}/clientmodule.cpp
${TuxHook_SRC}/utils.h
${TuxHook_SRC}/csimpledetour.cpp
${TuxHook_SRC}/csimpledetour.h
${TuxHook_SRC}/binaryreader.cpp
${TuxHook_SRC}/binaryreader.h
${TuxHook_SRC}/crypto.h
${TuxHook_SRC}/net.h
${TuxHook_SRC}/signscan.h
${TuxHook_SRC}/signscan.cpp
${TuxHook_SRC}/zip.cpp
${TuxHook_SRC}/zip.h
${TuxHook_SRC}/version.h
${TuxHook_SRC}/steammessages_base.pb.cc
${TuxHook_SRC}/logger.cpp
${TuxHook_SRC}/logger.h
)
if(UNIX AND NOT APPLE)
list(APPEND LIBSRC
${TuxHook_SRC}/utils.cpp
${TuxHook_SRC}/crypto.cpp
${TuxHook_SRC}/net.cpp
)
elseif(WIN32)
list(APPEND LIBSRC
${TuxHook_SRC}/nh2_string.h
${TuxHook_SRC}/string.cpp
${TuxHook_SRC}/sedebug.h
${TuxHook_SRC}/sedebug.cpp
${TuxHook_SRC}/utils_win32.cpp
${TuxHook_SRC}/crypto_win32.cpp
${TuxHook_SRC}/net_win32.cpp
${TuxHook_SRC}/injector.cpp
)
endif()

add_library(tuxhook2 SHARED ${LIBSRC})
set_property(TARGET tuxhook2 PROPERTY POSITION_INDEPENDENT_CODE true)
target_include_directories(tuxhook2 PUBLIC ${zlib_INCLUDE} ${protobuf_INCLUDE} ${funchook_INCLUDE})
target_link_libraries(tuxhook2 ${CMAKE_DL_LIBS} ${zlib_LIB} ${protobuf_LIB} ${funchook_LIB} ${funchook_DISTORM_LIB})
if(WIN32)
target_link_libraries(tuxhook2 ${PSAPI})
endif()

if(UNIX AND NOT APPLE)
add_custom_command(TARGET tuxhook2 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/inject.sh ${CMAKE_CURRENT_BINARY_DIR}/inject.sh)
# tiny payload lib
add_library(tuxhookldr SHARED ${CMAKE_CURRENT_LIST_DIR}/TuxHook/tuxhookldr.cpp)
set_property(TARGET tuxhookldr PROPERTY POSITION_INDEPENDENT_CODE true)
target_link_libraries(tuxhookldr ${CMAKE_DL_LIBS})
endif()
14 changes: 14 additions & 0 deletions Resources/TuxHook2/TuxHook/binaryreader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "binaryreader.h"

namespace NetHook
{

CBinaryReader::CBinaryReader( uint8 *pData, uint32 cubData )
{
m_pData = pData;
m_cubData = cubData;

m_Position = 0;
}

}
47 changes: 47 additions & 0 deletions Resources/TuxHook2/TuxHook/binaryreader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef NETHOOK_BINARYREADER_H_
#define NETHOOK_BINARYREADER_H_

#include "steam/steamtypes.h"

namespace NetHook
{

class CBinaryReader
{

public:
CBinaryReader( uint8 *pData, uint32 cubData );

uint32 GetPosition() noexcept { return m_Position; }
void SetPosition( uint32 pos ) noexcept { m_Position = pos; }
void SeekRelative( uint32 pos ) noexcept { m_Position += pos; }

uint32 GetSizeLeft() noexcept { return m_cubData - m_Position; }

template<typename T>
T Read() noexcept
{
T readData = *(T *)( m_pData + m_Position );
m_Position += sizeof( T );

return readData;
}

uint8 *ReadBytes( uint32 len ) noexcept
{
uint8 *ret = ( m_pData + m_Position );
m_Position += len;

return ret;
}

private:
uint8 *m_pData;
uint32 m_cubData;

uint32 m_Position;
};

}

#endif // !NETHOOK_BINARYREADER_H_
47 changes: 47 additions & 0 deletions Resources/TuxHook2/TuxHook/clientmodule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include <fstream>
#include "clientmodule.h"
#include "signscan.h"

namespace NetHook
{

ClientModule::ClientModule(const ModuleInfo& modInfo) noexcept:
m_moduleInfo(modInfo)
{
}

ClientModule::~ClientModule()
{
}

std::string ClientModule::GetDirectory()
{
#ifdef __linux__
return m_moduleInfo.m_modulePath.substr(0, m_moduleInfo.m_modulePath.find_last_of("/"));
#elif _WIN32
return m_moduleInfo.m_modulePath.substr(0, m_moduleInfo.m_modulePath.find_last_of("\\"));
#endif
}

std::string ClientModule::GetFullPath()
{
return m_moduleInfo.m_modulePath;
}

std::string ClientModule::GetName()
{
return m_moduleInfo.m_moduleName;
}

bool ClientModule::FindSignature(const char *sig, const char *mask, void **func, const char *prev) noexcept
{
const char* base = m_moduleInfo.m_base;
const char* end = base + m_moduleInfo.m_size;

return NetHook::FindSignature(base, end, sig, mask, func, prev);
}

}
41 changes: 41 additions & 0 deletions Resources/TuxHook2/TuxHook/clientmodule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef CLIENTMODULE_H
#define CLIENTMODULE_H

#include <string>

namespace NetHook
{

struct ModuleInfo
{
std::string m_moduleName;
std::string m_modulePath;
const char* m_base = nullptr;
size_t m_size = 0;
};

class ClientModule
{
public:
explicit ClientModule(const ModuleInfo& modInfo) noexcept;
~ClientModule();

std::string GetFullPath();
std::string GetDirectory();
std::string GetName();

bool FindSignature(const char *sig, const char *mask, void **func, const char *prev) noexcept;

private:
ClientModule();
ClientModule(const ClientModule&);
ClientModule& operator=(const ClientModule&);

ModuleInfo m_moduleInfo;
};

}

extern NetHook::ClientModule *g_pClientModule;

#endif // CLIENTMODULE_H
Loading