Skip to content

Commit

Permalink
fix: resolve race condition of formatting funcs in common.lib;
Browse files Browse the repository at this point in the history
really fixed x64->x86 injection (more tests and investigation needed);
resolve excess stack usage in GetAddrInfoW()
  • Loading branch information
shunf4 committed Mar 1, 2020
1 parent 49e6dcc commit 6de579a
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 147 deletions.
4 changes: 4 additions & 0 deletions Configure/Configure.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,22 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)win32_output\</OutDir>
<TargetName>$(ProjectName)_x86</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)win32_output\</OutDir>
<TargetName>$(ProjectName)_x64</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)win32_output\</OutDir>
<TargetName>$(ProjectName)_x86</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)win32_output\</OutDir>
<TargetName>$(ProjectName)_x64</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,17 @@ programs). See "To-do and Known Issues". Perhaps solution based on
- [X] ~~Add ".bat" etc. extension (PATHEXT) when SearchPath()~~ Fixed in 0.4
- [ ] ~~Fix 32-bit proxychains SearchPath(ssh) failure~~
(Windows Filesystem Redirection)
- [X] ~~Dynamic selection of 32-bit DLL and 64-bit DLL~~ Fixed in 0.4
- [X] ~~Dynamic selection of 32-bit DLL and 64-bit DLL~~ ~~Fixed in 0.4~~
Finally fixed in 0.4.3 (More tests needed)
- [ ] ~~Try to fix `proxychains git clone https://...` under Cygwin~~
Using `-q` in 0.4.1 reduces the probability to happen
- [X] ~~Try to fix `proxychains npm install` in a huge project~~
(may be caused by excess usage of stack in GetAddrInfoW, turning off
`proxy_dns` in 0.4 fixes this)
- [ ] Resolve race condition in `FormatHostPortToStr()`
- [ ] Resolve remote thread exit code `0xc0000005` (Access Denied)
when calling `C:\ProgramData\chocolatey\tools\7z.exe`
- [X] ~~Resolve race condition in `FormatHostPortToStr()`~~ (0.4.3)
- [X] ~~Resolve remote thread exit code `0xc0000005` (Access Denied)
when calling `C:\ProgramData\chocolatey\tools\7z.exe`~~
Finally fixed in 0.4.3 (More tests needed)

# Licensing

Expand Down
4 changes: 2 additions & 2 deletions cygwin-build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ clean :
dist :
PXCH_VERSION=$$(gcc -dM -E - < ../include/version.h | grep PXCH_VERSION | sort | awk '{print $$3}' | head -c -1 | tr '\n' '.') ./dist.sh

install :
distinstall :
PXCH_VERSION=$$(gcc -dM -E - < ../include/version.h | grep PXCH_VERSION | sort | awk '{print $$3}' | head -c -1 | tr '\n' '.') ./dist.sh --install

install_legacy:
distinstall_legacy:
make release
cp $(EXEC_PATH_RELEASE) $(PREFIX)/bin/proxychains.exe
cp $(EXEC_PATH_RELEASE) $(PREFIX)/bin/px.exe
Expand Down
14 changes: 13 additions & 1 deletion include/common_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@

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
extern wchar_t g_szDumpMemoryBuf_early[PXCH_MAX_DUMP_MEMORY_BUFSIZE];
extern wchar_t g_szErrorMessageBuf_early[PXCH_MAX_ERROR_MESSAGE_BUFSIZE];
extern wchar_t g_szFormatHostPortBuf_early[PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE];

// After the load of Hook DLL, they will be per-thread(in TLS), thread safe; before that they are assigned *_early
extern wchar_t* g_szDumpMemoryBuf;
extern wchar_t* g_szErrorMessageBuf;
extern wchar_t* g_szFormatHostPortBuf;

const wchar_t* FormatHostPortToStr(const void* pHostPort, int iAddrLen);
const wchar_t* DumpMemory(const void* p, int iLength);
void IndexToIp(const PROXYCHAINS_CONFIG* pPxchConfig, PXCH_IP_ADDRESS* pIp, PXCH_UINT32 iIndex);
void IpToIndex(const PROXYCHAINS_CONFIG* pPxchConfig, PXCH_UINT32* piIndex, const PXCH_IP_ADDRESS* pIp);
PXCH_UINT32 OpenConfigurationFile(PROXYCHAINS_CONFIG* pPxchConfig);
Expand All @@ -34,4 +45,5 @@ PXCH_UINT32 CloseHostsFile();
long ConfigurationTellPos();
void ConfigurationRewind();
long HostsTellPos();
void HostsRewind();
void HostsRewind();

4 changes: 0 additions & 4 deletions include/common_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
#include "defines_win32.h"
#include "common_generic.h"

#define PXCH_MAX_ERROR_MESSAGE_BUFSIZE 256

extern wchar_t szErrorMessage[PXCH_MAX_ERROR_MESSAGE_BUFSIZE];

PWCHAR FormatErrorToStr(DWORD dwError);
void StdWprintf(DWORD dwStdHandle, const WCHAR* fmt, ...);
void StdVwprintf(DWORD dwStdHandle, const WCHAR* fmt, va_list args);
Expand Down
2 changes: 2 additions & 0 deletions include/defines_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ typedef unsigned int PXCH_UINT_PTR;
#define PXCH_MAX_FILEMAPPING_BUFSIZE 256
#define PXCH_MAX_CONFIGURATION_LINE_BUFSIZE 512
#define PXCH_MAX_HOSTS_LINE_BUFSIZE 512
#define PXCH_MAX_ERROR_MESSAGE_BUFSIZE 256
#define PXCH_MAX_DUMP_MEMORY_BUFSIZE 1024
#define PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE 512
#define PXCH_MAX_ARRAY_IP_NUM 10
#define PXCH_MAX_PATHEXT_BUFSIZE 256

Expand Down
3 changes: 1 addition & 2 deletions include/hookdll_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,4 @@ PXCH_DLL_API int Ws2_32_DirectConnect(void* pTempData, PXCH_UINT_PTR s, const PX
PXCH_DLL_API int Ws2_32_Socks5Connect(void* pTempData, PXCH_UINT_PTR s, const PXCH_PROXY_DATA* pProxy /* Mostly myself */, const PXCH_HOST_PORT* pHostPort, int iAddrLen);
PXCH_DLL_API int Ws2_32_Socks5Handshake(void* pTempData, PXCH_UINT_PTR s, const PXCH_PROXY_DATA* pProxy /* Mostly myself */);

extern UT_array* g_arrHeapAllocatedPointers;
PXCH_DLL_API const wchar_t* DumpMemory(const void* p, int iLength);
extern UT_array* g_arrHeapAllocatedPointers;
3 changes: 3 additions & 0 deletions include/log_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
#pragma once
#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];

// After the load of Hook DLL, they will be per-thread(in TLS), thread safe
// TODO: make log_* pointers instead of macros, like what we do in common_generic.h
#define log_ods_buf (g_dwTlsIndex ? PXCH_TLS_PTR_LOG_ODS_BUF(g_dwTlsIndex) : log_ods_buf_early)

#define PXCH_LOG_LEVEL_VERBOSE 600
Expand Down
5 changes: 3 additions & 2 deletions include/log_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "log_generic.h"
#include "tls_win32.h"

// Per-process, will cause race condition, only used at early stages of DLL loading and hook initializing
// *_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 SYSTEMTIME log_time_early;
extern wchar_t log_szLogLine_early[PXCH_LOG_IPC_BUFSIZE];
extern PXCH_IPC_MSGBUF log_msg_early;
Expand Down Expand Up @@ -61,7 +61,8 @@ static void __attribute__((unused)) suppress_unused_variable(void)
}
#endif

// Per-thread(in TLS), thread safe
// After the load of Hook DLL, they will be per-thread(in TLS), thread safe
// TODO: make log_* pointers instead of macros, like what we do in common_generic.h
#define log_time (*(g_dwTlsIndex ? PXCH_TLS_PTR_LOG_TIME(g_dwTlsIndex) : &log_time_early))
#define log_szLogLine (g_dwTlsIndex ? PXCH_TLS_PTR_LOG_SZLOGLINE(g_dwTlsIndex) : log_szLogLine_early)
#define log_msg (g_dwTlsIndex ? PXCH_TLS_PTR_LOG_MSG(g_dwTlsIndex) : log_msg_early)
Expand Down
17 changes: 14 additions & 3 deletions include/tls_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
#include "defines_generic.h"
#include "ipc_generic.h"

// Thread-local storage offsets and sizes

#define PXCH_TLS_OFFSET_W32HOSTENT 0
#define PXCH_TLS_W32HOSTENT_IP_NUM 16
#define PXCH_TLS_W32HOSTENT_ALIAS_NUM 16
#define PXCH_TLS_W32HOSTENT_ALIAS_BUFSIZE 64

extern PXCH_DLL_API PXCH_UINT32 g_dwTlsIndex;
extern PXCH_DLL_API const PXCH_UINT32 g_dwW32HostentSize;
extern PXCH_DLL_API const PXCH_UINT32 g_dwW32SystemTimeSize;

#define PXCH_TLS_OFFSET_W32HOSTENT_IP_PTR_LIST (PXCH_TLS_OFFSET_W32HOSTENT + g_dwW32HostentSize)
#define PXCH_TLS_OFFSET_W32HOSTENT_IP_BUF (PXCH_TLS_OFFSET_W32HOSTENT_IP_PTR_LIST + sizeof(PXCH_UINT32*[PXCH_TLS_W32HOSTENT_IP_NUM]))
#define PXCH_TLS_OFFSET_W32HOSTENT_ALIAS_PTR_LIST (PXCH_TLS_OFFSET_W32HOSTENT_IP_BUF + sizeof(PXCH_UINT32[PXCH_TLS_W32HOSTENT_IP_NUM]))
#define PXCH_TLS_OFFSET_W32HOSTENT_ALIAS_BUF (PXCH_TLS_OFFSET_W32HOSTENT_ALIAS_PTR_LIST + sizeof(char*[PXCH_TLS_W32HOSTENT_ALIAS_NUM]))
#define PXCH_TLS_OFFSET_W32HOSTENT_HOSTNAME_BUF (PXCH_TLS_OFFSET_W32HOSTENT_ALIAS_PTR_LIST + sizeof(char[PXCH_TLS_W32HOSTENT_ALIAS_NUM][PXCH_TLS_W32HOSTENT_ALIAS_BUFSIZE]))
#define PXCH_TLS_OFFSET_W32HOSTENT_HOSTNAME_BUF (PXCH_TLS_OFFSET_W32HOSTENT_ALIAS_BUF + sizeof(char[PXCH_TLS_W32HOSTENT_ALIAS_NUM][PXCH_TLS_W32HOSTENT_ALIAS_BUFSIZE]))

#define PXCH_TLS_OFFSET_LOG_TIME (PXCH_TLS_OFFSET_W32HOSTENT_HOSTNAME_BUF + sizeof(char[PXCH_MAX_HOSTNAME_BUFSIZE]))
#define PXCH_TLS_OFFSET_LOG_SZLOGLINE (PXCH_TLS_OFFSET_LOG_TIME + g_dwW32SystemTimeSize)
Expand All @@ -44,8 +47,12 @@ extern PXCH_DLL_API const PXCH_UINT32 g_dwW32SystemTimeSize;
#define PXCH_TLS_OFFSET_LOG_TID (PXCH_TLS_OFFSET_LOG_CYGPID + sizeof(PXCH_UINT32))
#define PXCH_TLS_OFFSET_LOG_ODS_BUF (PXCH_TLS_OFFSET_LOG_TID + sizeof(PXCH_UINT32))
#define PXCH_TLS_OFFSET_DUMP_MEMORY_BUF (PXCH_TLS_OFFSET_LOG_ODS_BUF + sizeof(wchar_t[PXCH_LOG_ODS_BUFSIZE]))
#define PXCH_TLS_OFFSET_ERROR_MESSAGE_BUF (PXCH_TLS_OFFSET_DUMP_MEMORY_BUF + sizeof(wchar_t[PXCH_MAX_DUMP_MEMORY_BUFSIZE]))
#define PXCH_TLS_OFFSET_FORMAT_HOST_PORT_BUF (PXCH_TLS_OFFSET_ERROR_MESSAGE_BUF + sizeof(wchar_t[PXCH_MAX_ERROR_MESSAGE_BUFSIZE]))



#define PXCH_TLS_TOTAL_SIZE (PXCH_TLS_OFFSET_DUMP_MEMORY_BUF + sizeof(wchar_t[PXCH_MAX_DUMP_MEMORY_BUFSIZE]))
#define PXCH_TLS_TOTAL_SIZE (PXCH_TLS_OFFSET_FORMAT_HOST_PORT_BUF + sizeof(wchar_t[PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE]))


#define PXCH_TLS_PTR_LOG_SZLOGLINE_BY_BASE(base) ((wchar_t*)((char*)base + PXCH_TLS_OFFSET_LOG_SZLOGLINE))
Expand All @@ -58,6 +65,8 @@ extern PXCH_DLL_API const PXCH_UINT32 g_dwW32SystemTimeSize;
#define PXCH_TLS_PTR_LOG_TID_BY_BASE(base) ((PXCH_UINT32*)((char*)base + PXCH_TLS_OFFSET_LOG_TID))
#define PXCH_TLS_PTR_LOG_ODS_BUF_BY_BASE(base) ((wchar_t*)((char*)base + PXCH_TLS_OFFSET_LOG_ODS_BUF))
#define PXCH_TLS_PTR_DUMP_MEMORY_BUF_BY_BASE(base) ((wchar_t*)((char*)base + PXCH_TLS_OFFSET_DUMP_MEMORY_BUF))
#define PXCH_TLS_PTR_ERROR_MESSAGE_BUF_BY_BASE(base) ((wchar_t*)((char*)base + PXCH_TLS_OFFSET_ERROR_MESSAGE_BUF))
#define PXCH_TLS_PTR_FORMAT_HOST_PORT_BUF_BY_BASE(base) ((wchar_t*)((char*)base + PXCH_TLS_OFFSET_FORMAT_HOST_PORT_BUF))

#define PXCH_TLS_PTR_LOG_SZLOGLINE(dwTlsIndex) PXCH_TLS_PTR_LOG_SZLOGLINE_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_LOG_MSG(dwTlsIndex) PXCH_TLS_PTR_LOG_MSG_BY_BASE(TlsGetValue(dwTlsIndex))
Expand All @@ -68,4 +77,6 @@ extern PXCH_DLL_API const PXCH_UINT32 g_dwW32SystemTimeSize;
#define PXCH_TLS_PTR_LOG_CYGPID(dwTlsIndex) PXCH_TLS_PTR_LOG_CYGPID_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_LOG_TID(dwTlsIndex) PXCH_TLS_PTR_LOG_TID_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_LOG_ODS_BUF(dwTlsIndex) PXCH_TLS_PTR_LOG_ODS_BUF_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_DUMP_MEMORY_BUF(dwTlsIndex) PXCH_TLS_PTR_DUMP_MEMORY_BUF_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_DUMP_MEMORY_BUF(dwTlsIndex) PXCH_TLS_PTR_DUMP_MEMORY_BUF_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_ERROR_MESSAGE_BUF(dwTlsIndex) PXCH_TLS_PTR_ERROR_MESSAGE_BUF_BY_BASE(TlsGetValue(dwTlsIndex))
#define PXCH_TLS_PTR_FORMAT_HOST_PORT_BUF(dwTlsIndex) PXCH_TLS_PTR_FORMAT_HOST_PORT_BUF_BY_BASE(TlsGetValue(dwTlsIndex))
2 changes: 1 addition & 1 deletion include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
#define PXCH_VERSION_MINOR 4
#endif
#ifndef PXCH_VERSION_PATCH
#define PXCH_VERSION_PATCH 2
#define PXCH_VERSION_PATCH 3
#endif
8 changes: 4 additions & 4 deletions proxychains_hook.dll/proxychains_hook.dll.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</PostBuildEvent>
<PreBuildEvent>
<Command>cd /d $(SolutionDir)win32_output
.\configure.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
.\configure_x86.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand All @@ -142,7 +142,7 @@
</PostBuildEvent>
<PreBuildEvent>
<Command>cd /d $(SolutionDir)win32_output
.\configure.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
.\configure_x86.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -168,7 +168,7 @@
</PostBuildEvent>
<PreBuildEvent>
<Command>cd /d $(SolutionDir)win32_output
.\configure.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
.\configure_x86.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -194,7 +194,7 @@
</PostBuildEvent>
<PreBuildEvent>
<Command>cd /d $(SolutionDir)win32_output
.\configure.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
.\configure_x86.exe &gt; $(SolutionDir)include\function_pointers_configured.h</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
25 changes: 21 additions & 4 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
#include "common_win32.h"
#include "tls_generic.h"

WCHAR szErrorMessage[PXCH_MAX_ERROR_MESSAGE_BUFSIZE];
wchar_t g_szDumpMemoryBuf_early[PXCH_MAX_DUMP_MEMORY_BUFSIZE];
wchar_t g_szErrorMessageBuf_early[PXCH_MAX_ERROR_MESSAGE_BUFSIZE];

wchar_t* g_szDumpMemoryBuf = g_szDumpMemoryBuf_early;
wchar_t* g_szErrorMessageBuf = g_szErrorMessageBuf_early;

static WCHAR szFwprintfWbuf[PXCH_MAX_FWPRINTF_BUFSIZE];
static CHAR szFwprintfBuf[PXCH_MAX_FWPRINTF_BUFSIZE];

Expand Down Expand Up @@ -99,11 +104,23 @@ PWCHAR FormatErrorToStr(DWORD dwError)
if (buf[dwCb - 2] == L'\r') {
buf[dwCb - 2] = L'\0';
}
StringCchPrintfW(szErrorMessage, PXCH_MAX_ERROR_MESSAGE_BUFSIZE, L"%ls(" WPRDW L")", buf, dwError);
StringCchPrintfW(g_szErrorMessageBuf, PXCH_MAX_ERROR_MESSAGE_BUFSIZE, L"%ls(" WPRDW L")", buf, dwError);
LocalFree(hLocalBuffer);
}
else {
StringCchPrintfW(szErrorMessage, PXCH_MAX_ERROR_MESSAGE_BUFSIZE, L"(" WPRDW L")", dwError);
StringCchPrintfW(g_szErrorMessageBuf, PXCH_MAX_ERROR_MESSAGE_BUFSIZE, L"(" WPRDW L")", dwError);
}
return g_szErrorMessageBuf;
}

const wchar_t* DumpMemory(const void* p, int iLength)
{
int i;
wchar_t* pDumpMemoryBuf = g_szDumpMemoryBuf;

if (iLength == 0) iLength = 64;
for (i = 0; i < iLength; i++) {
StringCchPrintfExW(pDumpMemoryBuf, PXCH_MAX_DUMP_MEMORY_BUFSIZE - (pDumpMemoryBuf - g_szDumpMemoryBuf), &pDumpMemoryBuf, NULL, 0, L"%02x ", (unsigned int)*((const unsigned char*)p + i));
}
return szErrorMessage;
return g_szDumpMemoryBuf;
}
15 changes: 8 additions & 7 deletions src/common_wsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
#include "common_win32.h"
#include "log_generic.h"

static WCHAR g_HostPrintBuf[100];
wchar_t g_szFormatHostPortBuf_early[PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE];
wchar_t* g_szFormatHostPortBuf = g_szFormatHostPortBuf_early;

const wchar_t* FormatHostPortToStr(const void* pHostPort, int iAddrLen)
{
DWORD dwLen;
dwLen = _countof(g_HostPrintBuf);
g_HostPrintBuf[0] = L'\0';
dwLen = PXCH_MAX_FORMAT_HOST_PORT_BUFSIZE;
g_szFormatHostPortBuf[0] = L'\0';

if (HostIsType(HOSTNAME, *(PXCH_HOST*)pHostPort)) {
if (((PXCH_HOSTNAME*)pHostPort)->wPort) {
StringCchPrintfW(g_HostPrintBuf, dwLen, L"%ls:%hu", ((PXCH_HOSTNAME*)pHostPort)->szValue, ntohs(((PXCH_HOSTNAME*)pHostPort)->wPort));
StringCchPrintfW(g_szFormatHostPortBuf, dwLen, L"%ls:%hu", ((PXCH_HOSTNAME*)pHostPort)->szValue, ntohs(((PXCH_HOSTNAME*)pHostPort)->wPort));
} else {
StringCchPrintfW(g_HostPrintBuf, dwLen, L"%ls", ((PXCH_HOSTNAME*)pHostPort)->szValue);
StringCchPrintfW(g_szFormatHostPortBuf, dwLen, L"%ls", ((PXCH_HOSTNAME*)pHostPort)->szValue);
}
} else {
WSAAddressToStringW((struct sockaddr*)(pHostPort), iAddrLen, NULL, g_HostPrintBuf, &dwLen);
WSAAddressToStringW((struct sockaddr*)(pHostPort), iAddrLen, NULL, g_szFormatHostPortBuf, &dwLen);
}
return g_HostPrintBuf;
return g_szFormatHostPortBuf;
}

void IndexToIp(const PROXYCHAINS_CONFIG* pPxchConfig, PXCH_IP_ADDRESS* pIp, PXCH_UINT32 iIndex)
Expand Down
24 changes: 12 additions & 12 deletions src/configure.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@
// #error "Only compile it to x86 program"
int main()
{
wprintf(L"#define PXCH_ADDRESS_FreeLibrary " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetModuleHandleW " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetProcAddress " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_LoadLibraryW " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetLastError " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_OutputDebugStringA " PREFIX_ZERO_X "%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_FreeLibrary " PREFIX_ZERO_X L"%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetModuleHandleW " PREFIX_ZERO_X L"%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetProcAddress " PREFIX_ZERO_X L"%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_LoadLibraryW " PREFIX_ZERO_X L"%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_GetLastError " PREFIX_ZERO_X L"%p\n", NULL);
wprintf(L"#define PXCH_ADDRESS_OutputDebugStringA " PREFIX_ZERO_X L"%p\n", NULL);
return 0;
}
#else
int main()
{
wprintf(L"#define PXCH_ADDRESS_FreeLibrary " PREFIX_ZERO_X "%p\n", &FreeLibrary);
wprintf(L"#define PXCH_ADDRESS_GetModuleHandleW " PREFIX_ZERO_X "%p\n", &GetModuleHandleW);
wprintf(L"#define PXCH_ADDRESS_GetProcAddress " PREFIX_ZERO_X "%p\n", &GetProcAddress);
wprintf(L"#define PXCH_ADDRESS_LoadLibraryW " PREFIX_ZERO_X "%p\n", &LoadLibraryW);
wprintf(L"#define PXCH_ADDRESS_GetLastError " PREFIX_ZERO_X "%p\n", &GetLastError);
wprintf(L"#define PXCH_ADDRESS_OutputDebugStringA " PREFIX_ZERO_X "%p\n", &OutputDebugStringA);
wprintf(L"#define PXCH_ADDRESS_FreeLibrary " PREFIX_ZERO_X L"%p\n", &FreeLibrary);
wprintf(L"#define PXCH_ADDRESS_GetModuleHandleW " PREFIX_ZERO_X L"%p\n", &GetModuleHandleW);
wprintf(L"#define PXCH_ADDRESS_GetProcAddress " PREFIX_ZERO_X L"%p\n", &GetProcAddress);
wprintf(L"#define PXCH_ADDRESS_LoadLibraryW " PREFIX_ZERO_X L"%p\n", &LoadLibraryW);
wprintf(L"#define PXCH_ADDRESS_GetLastError " PREFIX_ZERO_X L"%p\n", &GetLastError);
wprintf(L"#define PXCH_ADDRESS_OutputDebugStringA " PREFIX_ZERO_X L"%p\n", &OutputDebugStringA);
return 0;
}
#endif
9 changes: 8 additions & 1 deletion src/dll/dllmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ PXCH_DLL_API BOOL DumpRemoteFunction(void)
if (f == NULL) return FALSE;

if (*(BYTE*)pCode == 0xE9) {
LOGV(L"Function body is a JMP instruction! This is usually caused by \"incremental linking\". Try to disable that.");
LOGV(L"Function body is a JMP instruction! This is usually caused by \"incremental linking\". Although I will handle that in a right way, but there might be problems in the future. Try to disable that.");
pCode = (void*)((char*)pCode + *(DWORD*)((char*)pCode + 1) + 5);
}

Expand Down Expand Up @@ -393,6 +393,13 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
pvData = HeapAlloc(GetProcessHeap(), 0, PXCH_TLS_TOTAL_SIZE);
TlsSetValue(g_dwTlsIndex, pvData);
ODBGSTRLOG(L"Initialized TLS: g_dwTlsIndex = " WPRDW, g_dwTlsIndex);

g_szDumpMemoryBuf = PXCH_TLS_PTR_DUMP_MEMORY_BUF_BY_BASE(pvData);
g_szErrorMessageBuf = PXCH_TLS_PTR_ERROR_MESSAGE_BUF_BY_BASE(pvData);
g_szFormatHostPortBuf = PXCH_TLS_PTR_FORMAT_HOST_PORT_BUF_BY_BASE(pvData);

// TODO: initialize log_* here after they are made as pointers rather than macros

break;
case DLL_THREAD_DETACH:
pvData = TlsGetValue(g_dwTlsIndex);
Expand Down
13 changes: 0 additions & 13 deletions src/dll/dllutil_log_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,3 @@ void pxchlog_ipc_func(const wchar_t* prefix_fmt, const wchar_t* ipc_prefix_fmt,
IpcCommunicateWithServer(log_msg, log_cbMsgSize, log_respMsg, &log_cbRespMsgSize);
}
}

const wchar_t* DumpMemory(const void* p, int iLength)
{
int i;
wchar_t* szDumpMemoryBuf = PXCH_TLS_PTR_DUMP_MEMORY_BUF(g_dwTlsIndex);
wchar_t* pDumpMemoryBuf = szDumpMemoryBuf;

if (iLength == 0) iLength = 64;
for (i = 0; i < iLength; i++) {
StringCchPrintfExW(pDumpMemoryBuf, PXCH_MAX_DUMP_MEMORY_BUFSIZE - (pDumpMemoryBuf - szDumpMemoryBuf), &pDumpMemoryBuf, NULL, 0, L"%02x ", (unsigned int)*((const unsigned char*)p + i));
}
return szDumpMemoryBuf;
}
Loading

0 comments on commit 6de579a

Please sign in to comment.