Skip to content

Commit

Permalink
Merge pull request #21 from lucas-engen/dev
Browse files Browse the repository at this point in the history
Improve x64 support
  • Loading branch information
lucasvmx authored Mar 5, 2021
2 parents 95b75cf + 5706f81 commit 0332639
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 173 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: configure cmake for library
- name: Configure CMake
run: cmake -G "MinGW Makefiles" -B build

- name: build library
- name: Generate revision information
shell: bash
run: |
curl https://raw.githubusercontent.com/Autorevision/autorevision/master/autorevision.sh -o autorevision
./autorevision -t h > autorevision.h
- name: Build libhack
run: make
working-directory: build

- name: configure cmake for examples
- name: Configure CMake (examples)
run: |
cp ../../build/*.dll libhack.dll
cp ../../build/*.a libhack.a
cmake -G "MinGW Makefiles" -B build
working-directory: src/examples

- name: build examples
- name: Build examples
run: make
working-directory: src/examples/build
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ CTestTestfile.cmake
_deps

# Autorevision generated file
#autorevision.h
autorevision*
6 changes: 6 additions & 0 deletions CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ add_library(hack SHARED
src/process.h
src/consts.h
src/init.h
src/logger.c
src/logger.h
)

# configure target properties
Expand All @@ -29,6 +31,10 @@ if(MINGW)
#set(CMAKE_C_FLAGS "-m32")
elseif(MSVC)
add_definitions(-DVISUAL_STUDIO)
if (NOT MSVC_VERSION LESS 1900)
message(AUTHOR_WARNING "Disabling spectre mitigation")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Qspectre-")
endif()
endif()


Expand Down
25 changes: 0 additions & 25 deletions src/autorevision.h

This file was deleted.

9 changes: 9 additions & 0 deletions src/examples/CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ target_link_libraries(write_addr ${LIBHACK})
target_link_libraries(dll_inject ${LIBHACK})
target_link_libraries(version_info ${LIBHACK})
target_link_libraries(submodule_addr ${LIBHACK})

# Set language standard
set_property(TARGET pid PROPERTY C_STANDARD 11)
set_property(TARGET read_addr PROPERTY C_STANDARD 11)
set_property(TARGET write_addr PROPERTY C_STANDARD 11)
set_property(TARGET dll_inject PROPERTY C_STANDARD 11)
set_property(TARGET version_info PROPERTY C_STANDARD 11)
set_property(TARGET submodule_addr PROPERTY C_STANDARD 11)

2 changes: 1 addition & 1 deletion src/examples/write_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main()
*/
libhack_write_int_to_addr(hack, baseAddr + 0x4c, value);

printf("Value readed from address %#x: %d\n", baseAddr + 0x4c, value);
printf("Value written to address %#x: %d\n", baseAddr + 0x4c, value);

/*
Cleanup resources used by library
Expand Down
36 changes: 20 additions & 16 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@
#include <psapi.h>
#include <string.h>
#include "init.h"
#include "autorevision.h"
#include "logger.h"
#include "../autorevision.h"

/**
* @brief Contains version number
*
*/
static char version[VERSION_NUMBER_LEN];

/**
* @brief Gets library version
*
* @return const char* The lib version in format MAJOR.MINOR.PATCH build BUILD
*/
LIBHACK_API const char *libhack_get_platform()
{
#if defined(__x86__)
return "32-bit";
#elif defined(__x64__)
return "64-bit";
#else
return "unknown";
#endif
}

LIBHACK_API const char *libhack_getversion()
{
RtlSecureZeroMemory(version, sizeof(version));
Expand All @@ -52,8 +59,8 @@ static BOOL libhack_check_version()
const char *uuid = libhack_getuuid();

if(strncmp(uuid, VCS_UUID, strlen(VCS_UUID)) != 0) {
libhack_debug("version mismatch: %s build %d != %s\n", VCS_TAG, VCS_NUM, libhack_getversion());
libhack_debug("this version has been built on %s\n", VCS_DATE);
libhack_debug("Version mismatch: %s build %d != %s\n", VCS_TAG, VCS_NUM, libhack_getversion());
libhack_debug("This version has been built on %s\n", VCS_DATE);
return FALSE;
}

Expand All @@ -80,18 +87,15 @@ struct libhack_handle *libhack_init(const char *process_name)
}

// Initialize memory
RtlSecureZeroMemory(lh->process_name, sizeof(lh->process_name));
RtlSecureZeroMemory(lh, sizeof(struct libhack_handle));

/* Copy process name to internal variable */
strncpy(lh->process_name, process_name, sizeof(lh->process_name) / sizeof(lh->process_name[0]));

lh->pid = 0;
lh->base_addr = 0;
lh->hProcess = NULL;
lh->hModule = NULL;
lh->bProcessIsOpen = FALSE;

libhack_debug("initialized libhack version %s\n", libhack_getversion());
// Transform process name to lowercase
strlwr(lh->process_name);

libhack_debug("Initialized libhack version %s (%s)\n", libhack_getversion(), libhack_get_platform());

return lh;
}
Expand Down
53 changes: 30 additions & 23 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define LIBHACK_H

#include "consts.h"
#include "platform.h"
#include <stdio.h>
#include <windows.h>

Expand All @@ -29,39 +30,25 @@ extern "C" {
#endif

/**
* @brief Obtém o tamanho do vetor
* @brief Gets the number of elements in a vector
*
*/
#define arraySize(x) (sizeof(x)/sizeof(x[0]))

/**
* @brief Shows a message if the program is being debugged
*
*/
#if defined(DEBUG) || !defined(NDEBUG)
#define libhack_debug(...) fprintf(stdout, "[libhack] " __VA_ARGS__)
#else
#ifdef _MSC_VER
#define libhack_debug(...)
#else
#define libhack_debug(...) asm("nop\n\t")
#endif
#endif

#define libhack_assert_or_exit(condition, exit_code) \
if(!condition) { \
fprintf(stdout, "libhack assert failure on %s line %d\n", __FILE__, __LINE__); \
if(!(condition)) { \
fprintf(stdout, "[LIBHACK] warn: assert failure on %s line %d\n", __FILE__, __LINE__); \
exit(exit_code); \
}

#define libhack_assert_or_warn(condition) \
if(!condition) { \
fprintf(stdout, "warn: assert failure on %s line %d\n", __FILE__, __LINE__); \
if(!(condition)) { \
fprintf(stdout, "[LIBHACK] warn: assert failure on %s line %d\n", __FILE__, __LINE__); \
}

#define libhack_assert_or_return(condition, retval) \
if(!condition) { \
fprintf(stdout, "warn: assert failure on %s line %d\n", __FILE__, __LINE__); \
if(!(condition)) { \
fprintf(stdout, "[LIBHACK] warn: assert failure on %s line %d\n", __FILE__, __LINE__); \
return retval; \
}

Expand All @@ -81,13 +68,20 @@ struct libhack_handle
* @brief Process identifier
*
*/
DWORD64 pid;

DWORD pid;
#if defined(__x64__)
/**
* @brief Process base address
*
*/
DWORD64 base_addr;
#else
/**
* @brief Process base address
*
*/
DWORD base_addr;
#endif

/**
* @brief Process handle
Expand All @@ -106,8 +100,21 @@ struct libhack_handle
*
*/
BOOL bProcessIsOpen;

/**
* @brief Flag to check if process is a x64 process
*
*/
BOOL b64BitProcess;
};

/**
* @brief Gets the target platform of library
*
* @return LIBHACK_API const* platform string
*/
LIBHACK_API const char *libhack_get_platform();

/**
* @brief Gets the program version and return a string
*
Expand Down
50 changes: 50 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#include <stdio.h>
#include <stdarg.h>
#include "logger.h"

#if defined(DEBUG) || !defined(NDEBUG)
void libhack_debug(const char *msg, ...)
{
va_list list;

va_start(list, msg);
fprintf(stdout, "[LIBHACK DEBUG] ");
vfprintf(stdout, msg, list);
fprintf(stdout, "\n");
va_end(list);
}

void libhack_warn(const char *msg, ...)
{
va_list list;

va_start(list, msg);
fprintf(stdout, "[LIBHACK WARNING] ");
vfprintf(stdout, msg, list);
fprintf(stdout, "\n");
va_end(list);
}

void libhack_notice(const char *msg, ...)
{
va_list list;

va_start(list, msg);
fprintf(stdout, "[LIBHACK INFO] ");
vfprintf(stdout, msg, list);
fprintf(stdout, "\n");
va_end(list);
}

void libhack_err(const char *msg, ...)
{
va_list list;

va_start(list, msg);
fprintf(stdout, "[LIBHACK ERROR] ");
vfprintf(stdout, msg, list);
fprintf(stdout, "\n");
va_end(list);
}
#endif
21 changes: 21 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef LOGGER_H
#define LOGGER_H

/**
* @brief Shows a message if the program is being debugged
*
*/

#if defined(DEBUG) || !defined(NDEBUG)
extern void libhack_debug(const char *msg, ...);
extern void libhack_warn(const char *msg, ...);
extern void libhack_notice(const char *msg, ...);
extern void libhack_err(const char *msg, ...);
#else
#define libhack_debug(...)
#define libhack_warn(...)
#define libhack_notice(...)
#define libhack_err(...)
#endif

#endif
13 changes: 13 additions & 0 deletions src/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PLATFORM_H
#define PLATFORM_H


#if defined(__MINGW64__) || defined(_WIN64)
#define __x64__
#elif __MINGW32__ || defined(_WIN32)
#define __x86__
#else
#error "Unknown platform detected"
#endif

#endif // PLATFORM_H
Loading

0 comments on commit 0332639

Please sign in to comment.