Skip to content

Commit 5241ecb

Browse files
committed
Add hooking library to hook windows api with standard name (without the _ like fopen) - use include_next to include system header
1 parent b04b8d1 commit 5241ecb

18 files changed

+639
-17
lines changed

3rdparty/easyhook/bin/EasyHook32.dll

278 KB
Binary file not shown.

3rdparty/easyhook/bin/EasyHook64.dll

323 KB
Binary file not shown.

3rdparty/easyhook/include/easyhook.h

+444
Large diffs are not rendered by default.

3rdparty/easyhook/lib/EasyHook32.lib

13.6 KB
Binary file not shown.

3rdparty/easyhook/lib/EasyHook64.lib

12.9 KB
Binary file not shown.

CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@ list(REMOVE_ITEM lib_src ${CMAKE_CURRENT_SOURCE_DIR}/src/_tdirent.c)
1111

1212
include_directories(../windevblk/include)
1313

14+
15+
1416
add_library(
15-
posix4msvc
17+
posix4msvc SHARED
1618
${lib_src}
1719
)
1820

19-
21+
add_definitions(-DDLLEXPORTS)
2022

2123
target_include_directories(
2224
posix4msvc PUBLIC
2325
include
2426
include/bsd
2527
include/sys/arch
28+
3rdparty/easyhook/include
2629
src
2730
)
2831

32+
#../3rdparty/easyhook/lib/EasyHook32.lib
2933

3034
target_link_libraries(
3135
posix4msvc
3236
windevblk
37+
C:/Developer/msys64/home/Vincent/posix4win/util-linux/posix4msvc/3rdparty/easyhook/lib/EasyHook32.lib
3338
)

include/bsd/string.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _BSD_STRING_H_
33

44
//#include <sys/cdefs.h>
5-
5+
#include_next "string.h"
66
#include <stddef.h>
77

88
#ifdef __cplusplus

include/getopt.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#ifndef _P4MSVC_GETOPT_H_
33
#define _P4MSVC_GETOPT_H_
44

5+
#ifdef DLLEXPORTS
6+
#define P4W_API __declspec(dllexport)
7+
#else
8+
#define P4W_API __declspec(dllimport)
9+
#endif
510

611
#ifdef __cplusplus
712
extern "C" {
@@ -11,8 +16,8 @@ extern const int no_argument;
1116
extern const int required_argument;
1217
extern const int optional_argument;
1318

14-
extern char* optarg;
15-
extern int optind, opterr, optopt;
19+
P4W_API extern char* optarg;
20+
P4W_API extern int optind, opterr, optopt;
1621

1722
struct option {
1823
const char* name;

include/stdio.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#ifndef _P4MSVC_STDIO_H_
3+
#define _P4MSVC_STDIO_H_
4+
5+
#include_next <stdio.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
FILE *__cdecl posix_fopen(const char *path, const char *mode);
12+
13+
//FILE *fdopen(int fd, const char *mode);
14+
15+
//FILE *freopen(const char *path, const char *mode, FILE *stream);
16+
17+
18+
#ifdef __cplusplus
19+
}
20+
#endif
21+
22+
23+
#endif /*_P4MSVC_STDIO_H_*/

include/stdlib_ex.h renamed to include/stdlib.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define _STDLIB_EX_H_
1111

1212
//#include <sys/cdefs.h>
13-
#include <stdlib.h>
13+
#include_next <stdlib.h>
1414

1515
#ifdef __cplusplus
1616
extern "C" {

include/unistd.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include <sys/types.h>
1414
#include <sys/types_ex.h>
1515
#include <strings.h>
16-
#include <stdio.h>
17-
#include <stdlib_ex.h>
16+
#include "stdio.h"
17+
#include "stdlib.h"
1818

1919
/***********************************************************************/
2020
// Since sys/stat.h is already defined by microsoft we include missing definitions

src/FILEio.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
#include <windows.h>
3+
#include "stdio.h"
4+
#include <errno.h>
5+
6+
#include <get_osfhandle-nothrow.h>
7+
#include "windevblk.h"
8+
9+
10+
FILE *__cdecl
11+
posix_fopen(const char *path, const char *mode)
12+
{
13+
return fopen(path, mode);
14+
}

src/dllmain.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
#include <windows.h>
3+
4+
5+
HINSTANCE g_hInstance = NULL;
6+
void init(void);
7+
void cleanup(void);
8+
9+
10+
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
11+
{
12+
switch (ul_reason_for_call)
13+
{
14+
case DLL_PROCESS_ATTACH:
15+
GetModuleHandleEx(0, NULL, &g_hInstance);
16+
init();
17+
break;
18+
case DLL_THREAD_ATTACH:
19+
break;
20+
21+
case DLL_THREAD_DETACH:
22+
break;
23+
24+
case DLL_PROCESS_DETACH:
25+
cleanup();
26+
break;
27+
}
28+
return TRUE;
29+
}

src/env.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdlib_ex.h>
1+
#include "stdlib.h"
22

33

44
int __cdecl

src/init.c

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
#include <windows.h>
3+
#include "stdio.h"
4+
5+
#include "easyhook.h"
6+
7+
static BOOL g_isInited = FALSE;
8+
static CRITICAL_SECTION g_critSec;
9+
10+
static HOOK_TRACE_INFO g_hHook;
11+
12+
void init(void);
13+
void cleanup(void);
14+
void init_hooking(void);
15+
void cleanup_hooking(void);
16+
17+
// Some useful macros
18+
#define PPCAT_NX(A, B) A ## B
19+
#define PPCAT(A, B) PPCAT_NX(A, B)
20+
#define STRINGIZE_NX(A) #A
21+
#define STRINGIZE(A) STRINGIZE_NX(A)
22+
23+
#ifndef ARRAY_SIZE
24+
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
25+
#endif
26+
27+
#ifdef _DEBUG
28+
#define REL_OR_DBG_SUFFIX d
29+
#else
30+
#define REL_OR_DBG_SUFFIX
31+
#endif
32+
#define DEBUG_OR_RELEASE(x) STRINGIZE(PPCAT(T1, REL_OR_DBG_SUFFIX))
33+
34+
typedef struct _HOOKING_INFO
35+
{
36+
TCHAR ModuleName[MAX_PATH];
37+
TCHAR FuncName[MAX_PATH];
38+
void* InHookProc;
39+
} HOOKING_INFO, *PHOOKING_INFO;
40+
41+
static HOOKING_INFO g_HookingInfo[] =
42+
{
43+
{ TEXT("ucrtbased"), TEXT("fopen"), posix_fopen },
44+
};
45+
46+
void init(void)
47+
{
48+
InitializeCriticalSection(&g_critSec);
49+
50+
if(!g_isInited)
51+
{
52+
init_hooking();
53+
g_isInited = TRUE;
54+
}
55+
DeleteCriticalSection(&g_critSec);
56+
}
57+
58+
void cleanup(void)
59+
{
60+
61+
cleanup_hooking();
62+
}
63+
64+
void init_hooking(void)
65+
{
66+
NTSTATUS result;
67+
ULONG ACLEntries[1] = { 0 };
68+
ZeroMemory(&g_hHook, sizeof(HOOK_TRACE_INFO));
69+
70+
#ifndef LHINSTALLHOOK
71+
# define LHINSTALLHOOK(a,b,c) (LhInstallHook(GetProcAddress(GetModuleHandle(a), b), c, NULL, &g_hHook))
72+
#endif
73+
74+
for (size_t i = 0; i < ARRAY_SIZE(g_HookingInfo); i++)
75+
{
76+
PHOOKING_INFO pHookInfo = &g_HookingInfo[i];
77+
result = LHINSTALLHOOK(pHookInfo->ModuleName, pHookInfo->FuncName, pHookInfo->InHookProc);
78+
if (FAILED(result))
79+
break;
80+
}
81+
82+
if (SUCCEEDED(result))
83+
{
84+
LhSetInclusiveACL(ACLEntries, 1, &g_hHook);
85+
}
86+
87+
#undef LHINSTALLHOOK;
88+
}
89+
90+
void cleanup_hooking(void)
91+
{
92+
LhUninstallHook(&g_hHook);
93+
LhWaitForPendingRemovals();
94+
}

src/io.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ lseek(int fd, long offset, int origin)
6060
HDEVBLK hDevBlk = DevBlkFromDiskHandle(handle);
6161
if (hDevBlk)
6262
{
63-
liOffset.QuadPart = offset;
64-
if (DevBlkSetPointerEx(handle, liOffset, &liNewOffset, origin))
65-
{
66-
lret = liNewOffset.LowPart;
67-
}
63+
lret = DevBlkSetPointer(hDevBlk, offset, (void*)0, origin);
6864
}
6965
else
7066
{
@@ -130,7 +126,7 @@ read(int fd, void *buf, size_t count)
130126
HDEVBLK hDevBlk = DevBlkFromDiskHandle(handle);
131127
if (hDevBlk)
132128
{
133-
if (DevBlkRead(hDevBlk, buf, count, &dwReadLen))
129+
if (DevBlkRead(hDevBlk, buf, count, &dwReadLen, NULL))
134130
{
135131
size = dwReadLen;
136132
}

src/posix4msvc.def

+13-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ fdopendir
2929
faccessat
3030
basename
3131
lseek64
32-
mkstemp
32+
mkstemp
33+
getopt
34+
geteuid
35+
getuid
36+
getgid
37+
getegid
38+
seteuid
39+
setegid
40+
ttyname
41+
ttyname_r
42+
fchmod
43+
link
44+
realpath

src/strlcat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <bsd/string.h>
1+
//#include <bsd/string.h>
22
#include <string.h>
33

44
#if !HAVE_STRLCAT

0 commit comments

Comments
 (0)