Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iuriimet committed Feb 14, 2022
1 parent c635e41 commit f0719a2
Show file tree
Hide file tree
Showing 22 changed files with 426 additions and 294 deletions.
93 changes: 64 additions & 29 deletions CMakeLists.txt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
cmake_minimum_required(VERSION 2.8)
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})

project(elfhook)

if(DEFINED DEBUG)
SET(CMAKE_BUILD_TYPE "Debug")
SET(CFLAGS " -O0 -g ")
SET(CXXFLAGS " -O0 -g ")
SET(CMAKE_C_FLAGS ${CFLAGS})
SET(CMAKE_CXX_FLAGS ${CXXFLAGS})
else()
SET(CMAKE_BUILD_TYPE "Release")
endif()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories(${CMAKE_SOURCE_DIR}/inc ${CMAKE_SOURCE_DIR}/lib)

FILE(GLOB SRCS src/*.c src/*.cpp)
add_executable(${PROJECT_NAME} ${SRCS})

add_library(TEST_LIB SHARED ${CMAKE_SOURCE_DIR}/lib/libtest.c)
set_property(TARGET TEST_LIB PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${PROJECT_NAME} pthread dl TEST_LIB)

message(STATUS "Configuring: " ${ProjectId})
message(STATUS "CMAKE_C_FLAGS: " ${CMAKE_C_FLAGS})
message(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET (PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR})
SET(CMAKE_VERBOSE_MAKEFILE ON)

IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "RELEASE")
ENDIF(NOT CMAKE_BUILD_TYPE)

IF(${CMAKE_BUILD_TYPE} STREQUAL "RELEASE")
SET(FORTITY_OPTIONS_COMPILER "-fstack-protector-strong -Wl,-z,relro -D_FORTIFY_SOURCE=2 -fPIE")
SET(FORTITY_OPTIONS_LINKER "-pie")
ELSE()
SET(FORTITY_OPTIONS_COMPILER " ")
SET(FORTITY_OPTIONS_LINKER " ")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wp,-U_FORTIFY_SOURCE")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wp,-U_FORTIFY_SOURCE")
ENDIF()

IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
SET(CXX_STD "c++0x")
ELSE()
SET(CXX_STD "c++11")
ENDIF()

SET(COMPILE_BASE_FLAGS "-Werror -Wall")

if (DEFINED __TIZEN__)
SET(COMPILE_BASE_FLAGS "${COMPILE_BASE_FLAGS} -D__TIZEN__ ")
endif (DEFINED __TIZEN__)

SET(CMAKE_C_FLAGS_DEBUG "${COMPILE_BASE_FLAGS} -O0 -ggdb")
SET(CMAKE_CXX_FLAGS_DEBUG "${COMPILE_BASE_FLAGS} -O0 -ggdb -std=${CXX_STD}")
SET(CMAKE_C_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG -g0")
SET(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG -g0 -std=${CXX_STD}")

INCLUDE(FindPkgConfig)

if (NOT DEFINED BIN_DIR)
SET (BIN_DIR "/usr/apps/elfhook")
endif (NOT DEFINED BIN_DIR)

if (NOT DEFINED LIB_DIR)
SET (LIB_DIR "/usr/local/lib")
endif (NOT DEFINED LIB_DIR)

if (NOT DEFINED MANIFESTDIR)
SET (MANIFESTDIR "/usr/share/packages")
endif (NOT DEFINED MANIFESTDIR)

# ZZZ
#macro(print_all_variables)
# message(STATUS "print_all_variables------------------------------------------{")
# get_cmake_property(_variableNames VARIABLES)
# foreach (_variableName ${_variableNames})
# message(STATUS "${_variableName}=${${_variableName}}")
# endforeach()
# message(STATUS "print_all_variables------------------------------------------}")
#endmacro()
#print_all_variables()

add_subdirectory(libtest)
add_subdirectory(libelfmem)
add_subdirectory(elfhook)
4 changes: 0 additions & 4 deletions README
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
mkdir build
cd build
cmake -D DEBUG=1 ..
make
6 changes: 4 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash

#gbs --conf gbs/gbs_tizen6.5.conf build -P tizen -A i586 --spec=elfhook.spec --incremental --clean --include-all
gbs --conf gbs/gbs_tizen6.5.conf build -P tizen6.5 -A i586 --incremental --clean --include-all
mkdir build
cd build
cmake -D DEBUG=1 ..
make
12 changes: 12 additions & 0 deletions build_tizen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

ARCH=i586
# ARCH=x86_64

BUILD_TYPE="DEBUG"

gbs --conf gbs/gbs_tizen6.5.conf build -P tizen6.5 -A $ARCH --incremental --clean --include-all --threads 1 \
--define '__debug_install_post %{nil}' \
--define 'debug_package %{nil}' \
--define 'build_type '$BUILD_TYPE

45 changes: 45 additions & 0 deletions common/inc/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef __LOGGER_H__
#define __LOGGER_H__

#if defined(__ANDROID__)

#include <android/log.h>

static const char* TAG = "ELFHOOK";

#define LOG_D(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOG_I(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOG_W(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define LOG_E(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)

#elif defined(__TIZEN__)

#include <dlog.h>

#ifndef TAG
#define TAG "ELFHOOK"
#endif

#define LOG_D(fmt, ...) dlog_print(DLOG_DEBUG, TAG, "[Debug] " fmt "\n", ##__VA_ARGS__)
#define LOG_I(fmt, ...) dlog_print(DLOG_INFO, TAG, "[Info] " fmt "\n", ##__VA_ARGS__)
#define LOG_W(fmt, ...) dlog_print(DLOG_WARN, TAG, "[Warning] " fmt "\n", ##__VA_ARGS__)
#define LOG_E(fmt, ...) dlog_print(DLOG_ERROR, TAG, "[Error] " fmt "\n", ##__VA_ARGS__)

#else

#include <stdio.h>
#include <string.h>

#define LOG_D(M, ...) \
fprintf(stdout, "[%s:%d] " M "\n", strrchr(__FILE__, '/') > 0 ? strrchr(__FILE__, '/') + 1 : __FILE__ , __LINE__, ##__VA_ARGS__); \
fflush(stdout);
#define LOG_I(M, ...) \
fprintf(stdout, "[%s:%d] " M "\n", strrchr(__FILE__, '/') > 0 ? strrchr(__FILE__, '/') + 1 : __FILE__ , __LINE__, ##__VA_ARGS__);
#define LOG_W(M, ...) \
fprintf(stdout, "[%s:%d] " M "\n", strrchr(__FILE__, '/') > 0 ? strrchr(__FILE__, '/') + 1 : __FILE__ , __LINE__, ##__VA_ARGS__);
#define LOG_E(M, ...) \
fprintf(stderr, "[%s:%d] " M " %s\n", strrchr(__FILE__, '/') > 0 ? strrchr(__FILE__, '/') + 1 : __FILE__ , __LINE__, ##__VA_ARGS__, strerror(errno));

#endif

#endif /* __LOGGER_H__ */
24 changes: 24 additions & 0 deletions elfhook/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required (VERSION 2.8)

project(elfhook C CXX)

file(GLOB APP_SRCS *.c *.cpp)

add_executable(${PROJECT_NAME} ${APP_SRCS})
add_dependencies(${PROJECT_NAME} elfmem)
add_dependencies(${PROJECT_NAME} test)

if (DEFINED __TIZEN__)
pkg_check_modules(APP_DEPS REQUIRED dlog)
endif (DEFINED __TIZEN__)

include_directories(SYSTEM ${APP_DEPS_INCLUDE_DIRS})
include_directories(. ../common/inc ../libtest ../libelfmem/inc)

target_link_libraries(${PROJECT_NAME} elfmem test ${APP_DEPS_LIBRARIES} pthread dl)

set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS ${FORTITY_OPTIONS_COMPILER})
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS ${FORTITY_OPTIONS_LINKER})

install(TARGETS ${PROJECT_NAME} DESTINATION ${BIN_DIR})
install(FILES ${PROJECT_NAME}.manifest DESTINATION ${MANIFESTDIR})
File renamed without changes.
54 changes: 54 additions & 0 deletions elfhook/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>

#include "libtest.h"

#include "elfmem_def.h"
#include "elfmem.h"
#include "logger.h"

int hooked_puts_1(const char* s)
{
puts(s);
puts("!!! HOOKED 111 !!!");
return 0;
}

int hooked_puts_2(const char* s)
{
puts(s);
puts("!!! HOOKED 222 !!!");
return 0;
}

//void hooked_libtest()
//{
// LOG_D("!!! libtest HOOKED !!!");
//}

int main()
{
ElfMem elf;

// original call
libtest();

// hook 1
const void* orig_addr = elf.soHookRel("libtest.so", "puts", (const void*)hooked_puts_1);
LOG_D("Orig Addr %p : Hook Addr %p", (const void*)orig_addr, (const void*)hooked_puts_1);
libtest();

// hook 2
const void* hook_addr_1 = elf.soHookRel("libtest.so", "puts", (const void*)hooked_puts_2);
LOG_D("Hook 1 Addr %p : Hook 2 Addr %p ", (const void*)hook_addr_1, (const void*)hooked_puts_2);
libtest();

// restore original
const void* hook_addr_2 = elf.soHookRel("libtest.so", "puts", (const void*)orig_addr);
LOG_D("Hook 2 Addr %p : Orig Addr %p ", (const void*)hook_addr_2, (const void*)orig_addr);
libtest();

// elf.soHookSym("libtest.so", "libtest", (const void*)hooked_libtest);
// libtest();

return 0;
}
8 changes: 1 addition & 7 deletions gbs/gbs_tizen6.5.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
profile=profile.tizen6.5

[profile.tizen6.5]
repos = repo.base-standard, repo.unified-standard, repo.base-standard-debug, repo.unified_standard_debug
repos = repo.base-standard, repo.unified-standard

[repo.base-standard]
url=https://download.tizen.org/snapshots/tizen/6.5-base/latest/repos/standard/packages/

[repo.unified-standard]
url=https://download.tizen.org/snapshots/tizen/6.5-unified/latest/repos/standard/packages/

[repo.base-standard-debug]
url=https://download.tizen.org/snapshots/tizen/6.5-base/latest/repos/standard/debug/

[repo.unified_standard_debug]
url=https://download.tizen.org/snapshots/tizen/6.5-unified/latest/repos/standard/debug/
32 changes: 0 additions & 32 deletions inc/logger.h

This file was deleted.

16 changes: 16 additions & 0 deletions libelfmem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 2.8)

project(elfmem C CXX)

file(GLOB LIB_SRCS src/*.c src/*.cpp)

add_library (${PROJECT_NAME} STATIC ${LIB_SRCS})

if (DEFINED __TIZEN__)
pkg_check_modules(LIB_DEPS REQUIRED dlog)
endif (DEFINED __TIZEN__)

include_directories(SYSTEM ${LIB_DEPS_INCLUDE_DIRS})
include_directories(inc ../common/inc)

set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS ${FORTITY_OPTIONS_COMPILER})
7 changes: 4 additions & 3 deletions inc/elfmem.h → libelfmem/inc/elfmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ElfMem
private:

template <typename RELT>
const void* hookRelTab(const RELT* reltab, int relcnt, int reltype,
const void* hookRelTab(const RELT* reltab, int relcnt, uint64_t reltype,
const char* proc_name, const void* subst_addr) const {
assert(reltab);
assert(proc_name);
Expand All @@ -48,8 +48,9 @@ class ElfMem

if (strcmp((const char*)(m_strings + sym->st_name), proc_name) == 0) {
off_t off = m_ehdr->e_type == ET_DYN ? (off_t)m_ehdr : 0;
res = (const void*)(off + reltab->r_offset);
*(uintptr_t*)(res) = (uintptr_t)subst_addr;
const void* ptr = (const void*)(off + reltab->r_offset);
res = (const void*)(*(uintptr_t*)(ptr));
*(uintptr_t*)(ptr) = (uintptr_t)subst_addr;
}
}

Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions inc/elfutils.h → libelfmem/inc/elfutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ class ElfUtils
return (strncmp((const char*)res->e_ident, ELFMAG, SELFMAG) == 0) ? res : nullptr;
}

static const ELF_PHDR_T* findPHDR(const ELF_EHDR_T* ehdr, int type);
static const ELF_PHDR_T* findPHDR(const ELF_EHDR_T* ehdr, uint32_t type);

static const ELF_DYN_T* findDynTAB(const ELF_EHDR_T* ehdr, const ELF_PHDR_T* phdr, int type);

static void printMaps();
// static void printMaps();

static void printEHDR(const ELF_EHDR_T* ehdr);
// static void printEHDR(const ELF_EHDR_T* ehdr);

static void printPHDR(const ELF_PHDR_T* phdr);
// static void printPHDR(const ELF_PHDR_T* phdr);

static void printDynTAB(const ELF_DYN_T* dyn);
// static void printDynTAB(const ELF_DYN_T* dyn);

static void printSymTAB(const ELF_SYM_T* sym);
// static void printSymTAB(const ELF_SYM_T* sym);

static void printRelTAB(const ELF_REL_T* rel);
static void printRelaTAB(const ELF_RELA_T* rela);
// static void printRelTAB(const ELF_REL_T* rel);
// static void printRelaTAB(const ELF_RELA_T* rela);
};

#endif // __ELFUTILS_H__
Loading

0 comments on commit f0719a2

Please sign in to comment.