Skip to content

Commit 4631b97

Browse files
added windows support
1 parent c1db181 commit 4631b97

File tree

10 files changed

+107
-16
lines changed

10 files changed

+107
-16
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
name: CI
22
on:
33
push:
4+
branches: [ master ]
45
pull_request:
6+
branches: [ master ]
57
workflow_dispatch:
68

79
jobs:
8-
build:
9-
if: "!contains(github.event.head_commit.message, '[ci skip]')"
10+
linux-build:
1011
runs-on: ubuntu-latest
1112

1213
steps:
13-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1415

1516
- name: Build project
1617
run: ./build.sh
18+
19+
windows-build:
20+
runs-on: windows-latest
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Build project
26+
run: build.bat
27+
shell: cmd

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.idea
22
build
3-
cmake-build-*
3+
cmake-build-debug
4+
cmake-build-minsizerel
5+
cmake-build-release
6+
cmake-build-relwithdebinfo

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.11)
1+
cmake_minimum_required(VERSION 3.14)
22
project(function-interceptor)
33

4-
set(CMAKE_CXX_STANDARD 20)
4+
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
77

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ This project demonstrates how to hook functions through the [funchook](https://g
66

77
### Requirements
88

9-
- g++ compiler
9+
- g++ / msvc compiler
1010
- cmake
1111
- python3
1212
- frida: https://frida.re
1313

14-
### Preparing
14+
### Preparing (Linux)
1515

1616
```bash
1717
# install dependencies
@@ -24,7 +24,17 @@ sudo sysctl kernel.yama.ptrace_scope=0
2424
./build.sh
2525
```
2626

27-
### Running
27+
### Preparing (Windows)
28+
29+
```bat
30+
REM install dependencies
31+
pip install frida
32+
33+
REM build c++ project
34+
build.bat
35+
```
36+
37+
### Running (Linux)
2838

2939
```bash
3040
# run program (it is assumed that in different terminals)
@@ -34,6 +44,21 @@ LD_LIBRARY_PATH=./build/bin ./build/bin/program
3444
./scripts/loader.py $(pidof program) ./build/bin/libhook.so
3545
```
3646

47+
### Running (Windows)
48+
49+
```bat
50+
REM run program (it is assumed that in different terminals)
51+
.\build\bin\program.exe
52+
53+
REM find the PID
54+
frida-ps
55+
56+
REM attach .\build\bin\hook.dll to PID
57+
python .\scripts\loader.py PID .\build\bin\hook.dll
58+
```
59+
60+
### Output
61+
3762
```
3863
exampleFunction(a=1, b=2, c=3)
3964
ret = 48

build.bat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@echo off
2+
3+
set CMAKE_BUILD_TYPE="Release"
4+
5+
if not exist build mkdir build
6+
cd build
7+
8+
set vswhere="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
9+
set cmakeLookup=call %vswhere% -latest -requires Microsoft.VisualStudio.Component.VC.CMake.Project -find Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe
10+
11+
for /f "tokens=*" %%i in ('%cmakeLookup%') do set cmake="%%i"
12+
13+
%cmake% -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON ..
14+
%cmake% --build . --config %CMAKE_BUILD_TYPE% --target INSTALL
15+
16+
cd ..

clean.bat

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@echo off
2+
3+
setlocal ENABLEDELAYEDEXPANSION
4+
5+
for /f "tokens=*" %%i in (.gitignore) do (
6+
set filename=%%i
7+
set filename=!filename:/=\!
8+
if exist !filename!\* (
9+
for /d %%d in (!filename!) do (
10+
del /s /f /q %%d\*.*
11+
for /f %%f in ('dir /ad /b %%d\') do rd /s /q %%d\%%f
12+
rd %%d
13+
)
14+
) else if exist !filename! (
15+
del /f /q !filename!
16+
)
17+
)

demo/module/include/module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef MODULE_H
22
#define MODULE_H
33

4-
#if defined(WIN32)
4+
#ifdef _WIN32
55
#define DLL_EXPORT __declspec(dllexport)
6-
#elif defined(__GNUC__)
6+
#else
77
#define DLL_EXPORT __attribute__((visibility("default")))
88
#endif
99

demo/program/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include <module.h>
22
#include <cstdio>
3-
#include <unistd.h>
3+
#include <thread>
4+
#include <chrono>
5+
6+
using namespace std::chrono_literals;
47

58
int main() {
69
for (int i = 0; i < 30; ++i) {
710
int ret = exampleFunction(1, 2, 3);
811
printf("ret = %d\n", ret);
9-
sleep(1);
12+
std::this_thread::sleep_for(1s);
1013
}
1114
return 0;
1215
}

hook/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ project(hook)
22

33
include(FetchContent)
44

5-
FetchContent_Declare(funchook GIT_REPOSITORY https://github.com/kubo/funchook)
5+
set(FUNCHOOK_BUILD_SHARED OFF CACHE BOOL "")
6+
set(FUNCHOOK_BUILD_STATIC ON CACHE BOOL "")
7+
set(FUNCHOOK_BUILD_TESTS OFF CACHE BOOL "")
8+
set(FUNCHOOK_INSTALL OFF CACHE BOOL "")
9+
FetchContent_Declare(funchook GIT_REPOSITORY https://github.com/StackOverflowExcept1on/funchook)
610
FetchContent_MakeAvailable(funchook)
711

812
add_library(${PROJECT_NAME} SHARED library.cpp)

hook/library.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include <funchook.h>
22

33
#include <cstdio>
4+
#ifdef _WIN32
5+
#include <windows.h>
6+
#else
47
#include <dlfcn.h>
8+
#endif
59

6-
#if defined(WIN32)
10+
#ifdef _WIN32
711
#define DLL_EXPORT __declspec(dllexport)
8-
#elif defined(__GNUC__)
12+
#else
913
#define DLL_EXPORT __attribute__((visibility("default")))
1014
#endif
1115

@@ -24,9 +28,17 @@ extern "C" DLL_EXPORT void onStartup() {
2428
funchook_t *funchook = funchook_create();
2529
int ret;
2630

31+
#ifdef _WIN32
32+
void *handle = GetModuleHandleA("module.dll");
33+
#else
2734
void *handle = dlopen("libmodule.so", RTLD_NOW);
35+
#endif
2836
printf("handle = %p\n", handle);
37+
#ifdef _WIN32
38+
exampleFunction_original = (exampleFunction_fptr_t) GetProcAddress((HMODULE) handle, "exampleFunction");
39+
#else
2940
exampleFunction_original = (exampleFunction_fptr_t) dlsym(handle, "exampleFunction");
41+
#endif
3042
printf("exampleFunction_original = %p\n", (void *) exampleFunction_original);
3143

3244
ret = funchook_prepare(funchook, (void **) &exampleFunction_original, (void *) exampleFunction_hook);

0 commit comments

Comments
 (0)