-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
426 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
mkdir build | ||
cd build | ||
cmake -D DEBUG=1 .. | ||
make | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.