Skip to content

Commit

Permalink
BREAKING: integrated proxychains_common into Hook DLL
Browse files Browse the repository at this point in the history
fix: race condition in StdWprintf
  • Loading branch information
shunf4 committed Mar 3, 2020
1 parent c619f33 commit dcc83de
Show file tree
Hide file tree
Showing 42 changed files with 590 additions and 662 deletions.
15 changes: 6 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"objbase.h": "c",
"shtypes.h": "c",
"cwchar": "cpp",
"common.h": "c",
"stdio.h": "c",
"unistd.h": "c",
"spawn.h": "c",
Expand All @@ -21,14 +20,12 @@
"socket.h": "c",
"winsock2.h": "c",
"_fd_types.h": "c",
"log.h": "c",
"ipc.h": "c",
"stdlib.h": "c",
"log_win32.h": "c",
"common_win32.h": "c",
"system_error": "cpp",
"ipc_generic.h": "c",
"ipc_win32.h": "c",
"hookdll_util_ipc_generic.h": "c",
"hookdll_util_ipc_win32.h": "c",
"remote_win32.h": "c",
"defines_win32.h": "c",
"log_generic.h": "c",
Expand All @@ -49,7 +46,6 @@
"minwindef.h": "c",
"proxy_core.h": "c",
"minhook.h": "c",
"common_generic.h": "c",
"sstream": "c",
"version.h": "c",
"tls_generic.h": "c",
Expand All @@ -59,8 +55,9 @@
"string_view": "c",
"initializer_list": "c",
"utility": "c",
"hookdll_interior_win32.h": "c",
"hookdll_interior_generic.h": "c",
"ut_helpers.h": "c"
"hookdll_util_win32.h": "c",
"hookdll_util_generic.h": "c",
"ut_helpers.h": "c",
"remote_func_bin_x64.h": "c"
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ programs). See "To-do and Known Issues". Perhaps solution based on
- [X] ~~Resolve remote thread exit code `0xc0000005` (Access Denied)
when calling `C:\ProgramData\chocolatey\tools\7z.exe`~~
Finally fixed in ~~0.4.3~~ ~~0.4.4~~ 0.4.5
- [X] ~~Resolve race condition in `StdWprintf()`~~ Fixed in 0.4.5

# Licensing

Expand Down
16 changes: 11 additions & 5 deletions cygwin_build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,16 @@ $(REMOTE_FUNC_BIN_HEADER_DEBUG) : $(HELPER_PATH_DEBUG)
$(REMOTE_FUNC_BIN_HEADER_RELEASE) : $(HELPER_PATH_RELEASE)
$(HELPER_PATH_RELEASE) --dump-remote-function > $@

$(DLL_PATH_DEBUG) : ../minhook/libMinHook.a dll/dllmain.o dll/hook_connect_win32.o dll/hook_connect_win32.o dll/hook_connect_cygwin.o dll/hook_createprocess_win32.o dll/hook_installer.o dll/ipc_client_and_child_data.o dll/dllutil_log_func.o dll/dllutil_wsock.o common.o common_wsock.o ipc_message.o log.o cygwin_strsafe/strsafe.o
dll/hookdll_main.o : MADE_REMOTE_FUNC_BIN_HEADER

MADE_REMOTE_FUNC_BIN_HEADER : proxychains_helper.o remote_function.o
$(MAKE) $(REMOTE_FUNC_BIN_HEADER)
touch MADE_REMOTE_FUNC_BIN_HEADER

$(DLL_PATH_DEBUG) : ../minhook/libMinHook.a dll/hookdll_main.o dll/hook_connect_win32.o dll/hook_connect_win32.o dll/hook_connect_cygwin.o dll/hook_createprocess_win32.o dll/hook_installer.o dll/ipc_client_and_child_data.o dll/hookdll_util.o dll/hookdll_util_wsock.o dll/hookdll_util_ipc_message.o cygwin_strsafe/strsafe.o
$(CC) -shared -o$@ -Wl,--out-implib=$(DLL_IMPLIB_PATH_DEBUG) $(LIB_DIR_LDFLAGS) $(filter %.c %.o %.a %.lib,$^) -Wl,-static -lMinHook -Wl,-Bdynamic -lntdllcrt -lws2_32 -lshlwapi

$(DLL_PATH_RELEASE) : ../minhook/libMinHook.a dll/dllmain.o dll/hook_connect_win32.o dll/hook_connect_win32.o dll/hook_connect_cygwin.o dll/hook_createprocess_win32.o dll/hook_installer.o dll/ipc_client_and_child_data.o dll/dllutil_log_func.o dll/dllutil_wsock.o common.o common_wsock.o ipc_message.o log.o cygwin_strsafe/strsafe.o
$(DLL_PATH_RELEASE) : ../minhook/libMinHook.a dll/hookdll_main.o dll/hook_connect_win32.o dll/hook_connect_win32.o dll/hook_connect_cygwin.o dll/hook_createprocess_win32.o dll/hook_installer.o dll/ipc_client_and_child_data.o dll/hookdll_util.o dll/hookdll_util_wsock.o dll/hookdll_util_ipc_message.o cygwin_strsafe/strsafe.o
$(CC) -shared -o$@ -Wl,--out-implib=$(DLL_IMPLIB_PATH_RELEASE) $(LIB_DIR_LDFLAGS) $(filter %.c %.o %.a %.lib,$^) -Wl,-static -lMinHook -Wl,-Bdynamic -lntdllcrt -lws2_32 -lshlwapi

# -Wl,--export-all-symbols \
Expand All @@ -144,16 +150,16 @@ $(DLL_PATH_RELEASE) : ../minhook/libMinHook.a dll/dllmain.o dll/hook_connect_win
$(DLL_IMPLIB_PATH_DEBUG) : $(DLL_PATH_DEBUG)
$(DLL_IMPLIB_PATH_RELEASE) : $(DLL_PATH_RELEASE)

$(EXEC_PATH_DEBUG) : ../minhook/libMinHook.a exe/main.o exe/args_and_config.o exe/ipc_proc_bookkeeping.o stdlib_config_reader.o common.o common_wsock.o ipc_message.o log.o cygwin_strsafe/strsafe.o $(DLL_IMPLIB_PATH_DEBUG)
$(EXEC_PATH_DEBUG) : ../minhook/libMinHook.a exe/main.o exe/args_and_config.o exe/ipc_proc_bookkeeping.o stdlib_config_reader.o cygwin_strsafe/strsafe.o $(DLL_IMPLIB_PATH_DEBUG)
$(CC) -o$@ $(filter %.c %.o %.a %.lib,$^) $(LIB_DIR_LDFLAGS) -Wl,-static -lMinHook -Wl,-Bdynamic -lntdllcrt -lkernel32 -lshlwapi -ladvapi32 -lcygwin -lws2_32

$(EXEC_PATH_RELEASE) : ../minhook/libMinHook.a exe/main.o exe/args_and_config.o exe/ipc_proc_bookkeeping.o stdlib_config_reader.o common.o common_wsock.o ipc_message.o log.o cygwin_strsafe/strsafe.o $(DLL_IMPLIB_PATH_RELEASE)
$(EXEC_PATH_RELEASE) : ../minhook/libMinHook.a exe/main.o exe/args_and_config.o exe/ipc_proc_bookkeeping.o stdlib_config_reader.o cygwin_strsafe/strsafe.o $(DLL_IMPLIB_PATH_RELEASE)
$(CC) -o$@ $(filter %.c %.o %.a %.lib,$^) $(LIB_DIR_LDFLAGS) -Wl,-static -lMinHook -Wl,-Bdynamic -lntdllcrt -lkernel32 -lshlwapi -ladvapi32 -lcygwin -lws2_32

.PHONY : clean dist pre_debug pre_release

clean :
$(RM) MAKING_DEBUG MAKING_RELEASE
$(RM) MAKING_DEBUG MAKING_RELEASE MADE_REMOTE_FUNC_BIN_HEADER
$(RM) $(ALL_EXECS_DEBUG) $(ALL_EXECS_RELEASE)
$(RM) $(ALL_SHARED_LIBS_DEBUG) $(ALL_SHARED_LIBS_RELEASE)
$(RM) $(ALL_IMPLIBS_DEBUG) $(ALL_IMPLIBS_RELEASE)
Expand Down
50 changes: 0 additions & 50 deletions include/common_generic.h

This file was deleted.

27 changes: 0 additions & 27 deletions include/common_win32.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/defines_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,4 @@ static const wchar_t g_szMinHookDllFileName[] = L"MinHook.x64.dll";
static const wchar_t g_szMinHookDllFileName[] = L"MinHook.x86.dll";
#endif

extern PXCH_DLL_API PROXYCHAINS_CONFIG* g_pPxchConfig;
PXCH_DLL_API extern PROXYCHAINS_CONFIG* g_pPxchConfig;
4 changes: 2 additions & 2 deletions include/defines_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ typedef struct _PXCH_INJECT_REMOTE_DATA {
} PXCH_INJECT_REMOTE_DATA;
#pragma pack(pop)

extern PXCH_DLL_API BOOL g_bCurrentlyInWinapiCall;
extern PXCH_DLL_API DWORD g_dwCurrentProcessIdForVerify;
PXCH_DLL_API extern BOOL g_bCurrentlyInWinapiCall;
PXCH_DLL_API extern DWORD g_dwCurrentProcessIdForVerify;

3 changes: 2 additions & 1 deletion include/hookdll_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
extern FP_ORIGINAL_FUNC2(Cygwin1, connect);
DECLARE_HOOK_FUNC2(Cygwin1, connect);

PXCH_UINT32 RestoreChildData();
void Win32HookWs2_32(void);
void CygwinHook(void);
25 changes: 0 additions & 25 deletions include/hookdll_interior_generic.h

This file was deleted.

49 changes: 49 additions & 0 deletions include/hookdll_util_generic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* hookdll_util_generic.h
* Copyright (C) 2020 Feng Shun.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once

#include "defines_win32.h"
#include "tls_generic.h"
#include "hookdll_util_ipc_win32.h"

extern PXCH_INJECT_REMOTE_DATA* g_pRemoteData;
PXCH_DLL_API extern const wchar_t* g_szRuleTargetDesc[3];

// *_early are per-process instead of per-thread, which will cause race condition, and are only used at early stages of DLL loading and hook initializing
PXCH_DLL_API extern wchar_t g_szDumpMemoryBuf_early[PXCH_MAX_DUMP_MEMORY_BUFSIZE];
PXCH_DLL_API extern wchar_t g_szErrorMessageBuf_early[PXCH_MAX_ERROR_MESSAGE_BUFSIZE];
PXCH_DLL_API extern wchar_t g_szFormatHostPortBuf_early[PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE];

PXCH_DLL_API extern wchar_t g_szFwprintfWbuf_early[PXCH_MAX_FWPRINTF_BUFSIZE];
PXCH_DLL_API extern char g_szFwprintfBuf_early[PXCH_MAX_FWPRINTF_BUFSIZE];

// After the load of Hook DLL, they will be per-thread(in TLS), thread safe
#define g_szDumpMemoryBuf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_DUMP_MEMORY_BUF(g_dwTlsIndex) : g_szDumpMemoryBuf_early)
#define g_szErrorMessageBuf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_ERROR_MESSAGE_BUF(g_dwTlsIndex) : g_szErrorMessageBuf_early)
#define g_szFormatHostPortBuf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_FORMAT_HOST_PORT_BUF(g_dwTlsIndex) : g_szFormatHostPortBuf_early)
#define g_szFwprintfWbuf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_FORMAT_FWPRINTF_W_BUF(g_dwTlsIndex) : g_szFwprintfWbuf_early)
#define g_szFwprintfBuf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_FORMAT_FWPRINTF_BUF(g_dwTlsIndex) : g_szFwprintfBuf_early)


PXCH_DLL_API const wchar_t* FormatHostPortToStr(const void* pHostPort, int iAddrLen);
PXCH_DLL_API const wchar_t* DumpMemory(const void* p, int iLength);

PXCH_DLL_API void IndexToIp(const PROXYCHAINS_CONFIG* pPxchConfig, PXCH_IP_ADDRESS* pIp, PXCH_UINT32 iIndex);
PXCH_DLL_API void IpToIndex(const PROXYCHAINS_CONFIG* pPxchConfig, PXCH_UINT32* piIndex, const PXCH_IP_ADDRESS* pIp);

6 changes: 3 additions & 3 deletions include/ipc_generic.h → include/hookdll_util_ipc_generic.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* ipc_generic.h
/* hookdll_util_ipc_generic.h
* Copyright (C) 2020 Feng Shun.
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -89,6 +89,6 @@ typedef struct _IPC_MSGHDR_HOSTNAMEANDIPS {

PXCH_UINT32 IpcCommunicateWithServer(const PXCH_IPC_MSGBUF sendMessage, PXCH_UINT32 cbSendMessageSize, PXCH_IPC_MSGBUF responseMessage, PXCH_UINT32* pcbResponseMessageSize);

PXCH_UINT32 WstrToMessage(PXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32* pcbMessageSize, const wchar_t* szWstr);
PXCH_UINT32 MessageToWstr(wchar_t* wstr, CPXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32 cbMessageSize);
PXCH_DLL_API PXCH_UINT32 WstrToMessage(PXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32* pcbMessageSize, const wchar_t* szWstr);
PXCH_DLL_API PXCH_UINT32 MessageToWstr(wchar_t* wstr, CPXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32 cbMessageSize);

16 changes: 8 additions & 8 deletions include/ipc_win32.h → include/hookdll_util_ipc_win32.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* ipc_win32.h
/* hookdll_util_ipc_win32.h
* Copyright (C) 2020 Feng Shun.
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
#pragma once

#include "defines_win32.h"
#include "ipc_generic.h"
#include "hookdll_util_ipc_generic.h"

#pragma pack(push, 1)
typedef struct _REPORTED_CHILD_DATA {
Expand All @@ -43,11 +43,11 @@ typedef struct _IPC_MSGHDR_QUERYSTORAGE {
} PXCH_IPC_MSGHDR_QUERYSTORAGE;
#pragma pack(pop)

DWORD ChildDataToMessage(PXCH_IPC_MSGBUF chMessageBuf, DWORD* pcbMessageSize, const REPORTED_CHILD_DATA* pChildData);
DWORD MessageToChildData(REPORTED_CHILD_DATA* pChildData, CPXCH_IPC_MSGBUF chMessageBuf, DWORD cbMessageSize);
PXCH_DLL_API DWORD ChildDataToMessage(PXCH_IPC_MSGBUF chMessageBuf, DWORD* pcbMessageSize, const REPORTED_CHILD_DATA* pChildData);
PXCH_DLL_API DWORD MessageToChildData(REPORTED_CHILD_DATA* pChildData, CPXCH_IPC_MSGBUF chMessageBuf, DWORD cbMessageSize);

DWORD QueryStorageToMessage(PXCH_IPC_MSGBUF chMessageBuf, DWORD* pcbMessageSize, DWORD dwChildPid);
DWORD MessageToQueryStorage(DWORD* pdwChildPid, CPXCH_IPC_MSGBUF chMessageBuf, DWORD cbMessageSize);
PXCH_DLL_API DWORD QueryStorageToMessage(PXCH_IPC_MSGBUF chMessageBuf, DWORD* pcbMessageSize, DWORD dwChildPid);
PXCH_DLL_API DWORD MessageToQueryStorage(DWORD* pdwChildPid, CPXCH_IPC_MSGBUF chMessageBuf, DWORD cbMessageSize);

PXCH_UINT32 HostnameAndIpsToMessage(PXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32* pcbMessageSize, PXCH_UINT32 dwPid, const PXCH_HOSTNAME* Hostname, BOOL bWillMapResolvedIpToHost, PXCH_UINT32 dwIpNum, const PXCH_IP_ADDRESS* Ips, PXCH_UINT32 dwTarget);
PXCH_UINT32 MessageToHostnameAndIps(PXCH_UINT32* pdwPid, PXCH_HOSTNAME* pHostname, BOOL* pbWillMapResolvedIpToHost, PXCH_UINT32* pdwIpNum, PXCH_IP_ADDRESS* Ips, PXCH_UINT32* pdwTarget, CPXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32 cbMessageSize);
PXCH_DLL_API PXCH_UINT32 HostnameAndIpsToMessage(PXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32* pcbMessageSize, PXCH_UINT32 dwPid, const PXCH_HOSTNAME* Hostname, BOOL bWillMapResolvedIpToHost, PXCH_UINT32 dwIpNum, const PXCH_IP_ADDRESS* Ips, PXCH_UINT32 dwTarget);
PXCH_DLL_API PXCH_UINT32 MessageToHostnameAndIps(PXCH_UINT32* pdwPid, PXCH_HOSTNAME* pHostname, BOOL* pbWillMapResolvedIpToHost, PXCH_UINT32* pdwIpNum, PXCH_IP_ADDRESS* Ips, PXCH_UINT32* pdwTarget, CPXCH_IPC_MSGBUF chMessageBuf, PXCH_UINT32 cbMessageSize);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* hookdll_interior_win32.h
/* hookdll_util_win32.h
* Copyright (C) 2020 Feng Shun.
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -18,16 +18,19 @@
*/
#pragma once

#include "hookdll_interior_generic.h"
#include "defines_win32.h"
#include "hookdll_util_generic.h"

PXCH_DLL_API PWCHAR FormatErrorToStr(DWORD dwError);
PXCH_DLL_API void StdWprintf(DWORD dwStdHandle, const WCHAR* fmt, ...);
PXCH_DLL_API void StdVwprintf(DWORD dwStdHandle, const WCHAR* fmt, va_list args);
PXCH_DLL_API void StdFlush(DWORD dwStdHandle);

DWORD IpcClientRegisterChildProcess();
PXCH_UINT32 RestoreChildData();

DWORD InjectTargetProcess(const PROCESS_INFORMATION* pPi);

void Win32HookWs2_32(void);
void CygwinHook(void);

#ifdef PXCH_INCLUDE_WINSOCK_UTIL
#include <WinSock2.h>
#include <Ws2Tcpip.h>
Expand Down
6 changes: 4 additions & 2 deletions include/log_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include "tls_generic.h"

// *_early are per-process instead of per-thread, which will cause race condition, and are only used at early stages of DLL loading and hook initializing
extern wchar_t log_ods_buf_early[PXCH_LOG_ODS_BUFSIZE];
PXCH_DLL_API extern wchar_t log_ods_buf_early[PXCH_LOG_ODS_BUFSIZE];

// After the load of Hook DLL, they will be per-thread(in TLS), thread safe
#define log_ods_buf (g_dwTlsIndex ? PXCH_TLS_PTR_LOG_ODS_BUF(g_dwTlsIndex) : log_ods_buf_early)
#define log_ods_buf ((g_dwTlsIndex != TLS_OUT_OF_INDEXES) ? PXCH_TLS_PTR_LOG_ODS_BUF(g_dwTlsIndex) : log_ods_buf_early)

#define PXCH_LOG_LEVEL_VERBOSE 600
#define PXCH_LOG_LEVEL_DEBUG 500
Expand Down Expand Up @@ -58,6 +58,8 @@ extern wchar_t log_ods_buf_early[PXCH_LOG_ODS_BUFSIZE];
#define PXCH_LOG_IPC_PID_PREFIX PXCH_LOG_IPC_PID_PREFIX_WIN
#endif

#define ODBGSTRLOG_FORCE_WITH_EARLY_BUF(fmt, ...) do { StringCchPrintfW(log_ods_buf_early, PXCH_LOG_ODS_BUFSIZE, fmt, ##__VA_ARGS__); OutputDebugStringW(log_ods_buf_early); } while(0)

#if PXCH_LOG_LEVEL_ENABLED >= PXCH_LOG_LEVEL_DEBUG
#define ODBGSTRLOG(fmt, ...) do { StringCchPrintfW(log_ods_buf, PXCH_LOG_ODS_BUFSIZE, fmt, ##__VA_ARGS__); OutputDebugStringW(log_ods_buf); } while(0)
#else
Expand Down
Loading

0 comments on commit dcc83de

Please sign in to comment.