Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1c09935
合并3.0 Build_343中的修改,但是detours.cpp中ARM及ARM64中detour_find_jmp_bounds函数的…
sonyps5201314 Feb 9, 2020
7a6f330
加入VS解决方案
sonyps5201314 Feb 9, 2020
167f7f0
git忽略文件修改
sonyps5201314 Feb 9, 2020
10247b1
合并我们目前最新的3.0 Build_343_src_mod中的修改到目前官方最新版本
sonyps5201314 Feb 9, 2020
74bb4f1
执行完成相关任务后删除该负载以防止任务被反复执行及防止内存泄露
sonyps5201314 Mar 6, 2020
0cb41bb
删除.detourd可读写数据节和.detourc只读数据节
sonyps5201314 Mar 6, 2020
55755c3
Windows SDK 版本更新
sonyps5201314 Apr 29, 2020
f18b2a8
Merge branch 'master' of https://github.com/Microsoft/Detours
sonyps5201314 Jul 1, 2020
ff48786
从SocksCapEx2的Detours修改版中移植进detour_skip_all_sequential_jmps函数
sonyps5201314 Jul 1, 2020
e23122f
加入新的API函数DetourSetNeedClosePendingThreadHandles用于控制是否在删除DetourThread对…
sonyps5201314 Aug 2, 2020
5849ff4
[code from mhook][performance] CreateToolhelp32Snapshot -> ZwQuerySys…
sonyps5201314 Aug 3, 2020
5a9297b
移除我们新加的API函数DetourSetNeedClosePendingThreadHandles,完善DetourUpdateAllO…
sonyps5201314 Aug 3, 2020
c4a2915
DetourTransactionBegin加入一个fWait参数用于支持等待其他Detour会话结束
sonyps5201314 Aug 4, 2020
d00c138
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Aug 22, 2020
ade5b6e
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Aug 27, 2020
c493cf9
fix dynamic_alloc to can build without cxx11
sonyps5201314 Aug 27, 2020
cb5a80c
vs2008 and earlier version's vc's rc.exe don`t have 'nologo' parameter
sonyps5201314 Aug 27, 2020
3416d7b
update for can build all samples with updated APIs in VS IDE
sonyps5201314 Aug 27, 2020
2bd77a0
add english comments of the chinese comments by google translator, th…
sonyps5201314 Aug 27, 2020
bd317cc
re save as UTF8-BOM for fix build warning, but it's not compat with o…
sonyps5201314 Aug 27, 2020
39871e1
fix for online build
sonyps5201314 Aug 27, 2020
4503268
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Aug 28, 2020
76fe979
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Aug 28, 2020
5cfea74
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Aug 31, 2020
89d7e33
Merge branch 'master' of https://github.com/Microsoft/Detours into ma…
sonyps5201314 Sep 2, 2020
146bdf2
fix for online build
sonyps5201314 Sep 2, 2020
025fe0a
fix for online build
sonyps5201314 Sep 2, 2020
85bf59f
switch vc project file ConfigurationType to MAKEFILE, because Utility…
sonyps5201314 Sep 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ include/
obj.*
*.ipdb
*.iobj
*.tlog
*.log
*.obj
*.user
*.recipe
/bin.*
*.vcxproj.FileListAbsolute.txt
3 changes: 3 additions & 0 deletions CREDITS.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ The following individuals have helped identify specific bugs and improvements
in Detours. The entire Detours community has benefited from their help.
==============================================================================

* Jay Krell: Identified error in DetourFindPayload that caused a
incorrect failure when pcbData is NULL. (Build_342)

* Jay Krell: Identified issue with VirtualSize == 0 files created in
NT 3.1 images. (Build_339)

Expand Down
12 changes: 6 additions & 6 deletions samples/comeasy/wrotei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ int WINAPI TimedEntryPoint(VOID)
CreateStreamOnHGlobal(NULL, TRUE, &pStream);

// Apply the detour to the vtable.
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
if (pStream != NULL) {
RealIStreamWrite = pStream->lpVtbl->Write;
DetourAttach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
Expand Down Expand Up @@ -132,8 +132,8 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
TrueEntryPoint = (int (WINAPI *)(VOID))DetourGetEntryPoint(NULL);
RawEntryPoint = TrueEntryPoint;

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)TrueEntryPoint, TimedEntryPoint);
error = DetourTransactionCommit();

Expand All @@ -147,8 +147,8 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
}
}
else if (dwReason == DLL_PROCESS_DETACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
if (RealIStreamWrite != NULL) {
DetourDetach(&(PVOID&)RealIStreamWrite, (PVOID)MineIStreamWrite);
}
Expand Down
8 changes: 4 additions & 4 deletions samples/commem/commem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ int main(int argc, char **argv)
li.QuadPart = 0;
hr = pStream->lpVtbl->Write(pStream, &ul, sizeof(ul), NULL);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
DetourTransactionCommit();

Expand All @@ -94,8 +94,8 @@ int main(int argc, char **argv)
li.QuadPart = 1;
hr = pStream->lpVtbl->Write(pStream, &li, sizeof(li), NULL);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourDetach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
DetourTransactionCommit();

Expand Down
4 changes: 2 additions & 2 deletions samples/common.mak
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CLIB=/MT
!ENDIF

AFLAGS=/nologo /Zi /c /Fl
CFLAGS=/nologo /Zi $(CLIB) /Gm- /W4 /WX /we4777 /we4800 /Od
CFLAGS=/nologo /Zi $(CLIB) /Gm- /W4 /WX /we4777 /we4800 /Od /D__AUTO_CREATE_DETOUR_HEAP__

!IF $(DETOURS_SOURCE_BROWSING)==1
CFLAGS=$(CFLAGS) /FR
Expand Down Expand Up @@ -80,7 +80,7 @@ LIBS = $(DEPS)
!endif

.rc{$(OBJD)}.res:
rc /nologo /DDETOURS_BITS=$(DETOURS_BITS) /fo$(@) /i$(INCD) $(*B).rc
rc /DDETOURS_BITS=$(DETOURS_BITS) /fo$(@) /i$(INCD) $(*B).rc

##
################################################################# End of File.
2 changes: 1 addition & 1 deletion samples/cping/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ CPPFLAGS=
!endif

.rc{$(OBJD)}.res:
rc /nologo /Fo$@ .\$(*B).rc
rc /Fo$@ .\$(*B).rc

##############################################################################
##
Expand Down
4 changes: 2 additions & 2 deletions samples/cping/cping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,8 @@ LONG RerouteEntryPoints(VOID)
ULONG))
DetourFindFunction("ntdll.dll", "NtDeviceIoControlFile"));

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();

DetourAttach(&(PVOID&)Real_NtWaitForSingleObject,
Catch_NtWaitForSingleObject);
Expand Down
28 changes: 14 additions & 14 deletions samples/dtest/dtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,16 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
printf("Calling LocalTarget1 w/o detour\n");
LocalTarget1(1);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_LocalTarget1, MyLocalTarget1);
DetourTransactionCommit();

printf("Calling LocalTarget1 w/ detour\n");
LocalTarget1(2);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_Target0, MyTarget0);
DetourTransactionCommit();

Expand All @@ -523,8 +523,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
//dprintf("- Target0 :: %p\n", Target0);
Target0();

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_Target1, MyTarget1);
DetourAttach(&(PVOID&)Trampoline_Target2, MyTarget2);
DetourAttach(&(PVOID&)Trampoline_Target3, MyTarget3);
Expand Down Expand Up @@ -595,8 +595,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
printf("Calling Target0 again with 1 detour.\n");
Target0();

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_Target0_1, MyTarget0_1);
DetourTransactionCommit();

Expand All @@ -605,8 +605,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
printf("Calling Target0 again with 2 detours.\n");
Target0();

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_Target0_2, MyTarget0_2);
DetourTransactionCommit();

Expand All @@ -616,8 +616,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
Target0();


DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Trampoline_Target0_3, MyTarget0_3);
DetourTransactionCommit();

Expand All @@ -626,8 +626,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmd
printf("Calling Target0 again with 4 detours.\n");
Target0();

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourDetach(&(PVOID&)Trampoline_Target0, MyTarget0);
DetourDetach(&(PVOID&)Trampoline_Target1, MyTarget1);
DetourDetach(&(PVOID&)Trampoline_Target2, MyTarget2);
Expand Down
108 changes: 66 additions & 42 deletions samples/dynamic_alloc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
#include <windows.h>
#include <detours.h>

#if _MSC_VER <= 1600
#define nullptr NULL
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
#endif

extern "C" {
void *CodeTemplate();
void *CodeTemplate_End();
Expand All @@ -45,14 +51,17 @@ void *target_function() {

// Helper function to sandwich a given function between `DetourTransactionBegin`
// and `DetourTransactionCommit`/`DetourTransactionAbort`.
bool DetourTransaction(std::function<bool()> callback) {
LONG status = DetourTransactionBegin();
typedef bool (*callback_type)(PVOID* pdetour_target, PVOID detour_destination);
bool DetourTransaction(callback_type callback, PVOID* pdetour_target, PVOID detour_destination) {
DetourCreateHeap(TRUE);
LONG status = DetourTransactionBegin(TRUE);
if (status != NO_ERROR) {
Log("DetourTransactionBegin failed with %08x\n", status);
return status == NO_ERROR;
}
DetourUpdateAllOtherThreads();

if (callback()) {
if (callback(pdetour_target, detour_destination)) {
status = DetourTransactionCommit();
if (status != NO_ERROR) {
Log("DetourTransactionCommit failed with %08x\n", status);
Expand All @@ -73,7 +82,7 @@ bool DetourTransaction(std::function<bool()> callback) {
// This class manages one dynamically-allocated region that is allocated by
// the Detours API `DetourAllocateRegionWithinJumpBounds`, to which we can
// push binary data sequentially to use it as a detour function.
class CodeRegionFactory final {
class CodeRegionFactory /*final*/ {
template <typename T>
static const T *at(const void *base, uint32_t offset) {
return
Expand All @@ -88,14 +97,24 @@ class CodeRegionFactory final {
reinterpret_cast<uint8_t*>(base) + offset);
}

void *region_ = nullptr;
uint8_t *current_ = nullptr,
*current_end_ = nullptr;
void *region_;
uint8_t *current_,*current_end_;

public:
void Init()
{
region_ = nullptr;
current_ = nullptr;
current_end_ = nullptr;
}
CodeRegionFactory()
{
Init();
}
CodeRegionFactory(const void *source) {
Init();
DWORD new_region_size = 0;
auto new_region_address =
PVOID new_region_address =
DetourAllocateRegionWithinJumpBounds(source, &new_region_size);
if (new_region_address) {
region_ = current_ = at<uint8_t>(new_region_address, 0);
Expand All @@ -117,55 +136,67 @@ class CodeRegionFactory final {
// the start address of a copy in the region if succeeded.
void *PushTemplate(const void *start,
const void *end) {
auto diff = at<uint8_t>(end, 0) - at<uint8_t>(start, 0);
INT_PTR diff = at<uint8_t>(end, 0) - at<uint8_t>(start, 0);
if (diff < 0 || current_ + diff > current_end_)
return nullptr;
auto start_pos = current_;
uint8_t* start_pos = current_;
memcpy(start_pos, start, diff);
current_ += diff;
return start_pos;
}
};

static bool is_detoured = false;
bool callback_attach(PVOID* pdetour_target, PVOID detour_destination) {
PDETOUR_TRAMPOLINE trampoline = nullptr;
void* target = nullptr,
* detour = nullptr;
LONG status = DetourAttachEx(pdetour_target,
detour_destination,
&trampoline,
&target,
&detour);
if (status != NO_ERROR) {
Log("DetourAttachEx failed - %08x\n", status);
return false;
}
is_detoured = true;
std::cout
<< "detour: " << target << " --> " << detour
<< " (trampoline: " << trampoline << " )"
<< std::endl;
return true;
}

bool callback_detach(PVOID* pdetour_target, PVOID detour_destination) {
LONG status = DetourDetach(pdetour_target, detour_destination);
if (status != NO_ERROR) {
Log("DetourDetach failed - %08x\n", status);
return false;
}
return true;
}

int main(int, char**) {
std::cout << "1. target_function() without Detour" << std::endl;
auto ret = target_function();
void* ret = target_function();
std::cout << ret << std::endl;
assert(!ret);

CodeRegionFactory factory(target_function);

void *detour_destination,
void *detour_destination = nullptr,
*detour_target = reinterpret_cast<void*>(target_function);

// Fill the allocated page with as many instances as possible of the code
// template, and pick the last instance
while (auto p = factory.PushTemplate(CodeTemplate,
while (void* p = factory.PushTemplate(CodeTemplate,
CodeTemplate_End)) {
detour_destination = p;
}

bool is_detoured = false;
DetourTransaction([&]() {
PDETOUR_TRAMPOLINE trampoline = nullptr;
void *target = nullptr,
*detour = nullptr;
auto status = DetourAttachEx(&detour_target,
detour_destination,
&trampoline,
&target,
&detour);
if (status != NO_ERROR) {
Log("DetourAttachEx failed - %08x\n", status);
return false;
}
is_detoured = true;
std::cout
<< "detour: " << target << " --> " << detour
<< " (trampoline: " << trampoline << " )"
<< std::endl;
return true;
});
DetourCreateHeap(TRUE);
DetourTransaction(callback_attach, &detour_target, detour_destination);

// Attach failed for some reason. Bail out.
if (!is_detoured)
Expand All @@ -176,14 +207,7 @@ int main(int, char**) {
std::cout << ret << std::endl;
assert(ret); // The return value is cracked by the detour function

DetourTransaction([&]() {
auto status = DetourDetach(&detour_target, detour_destination);
if (status != NO_ERROR) {
Log("DetourDetach failed - %08x\n", status);
return false;
}
return true;
});
DetourTransaction(callback_detach, &detour_target, detour_destination);

std::cout << "3. target_function() without Detour" << std::endl;
ret = target_function();
Expand Down
8 changes: 4 additions & 4 deletions samples/echo/echofx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
" Starting.\n");
fflush(stdout);

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourAttach(&(PVOID&)Real_Echo, Mine_Echo);
error = DetourTransactionCommit();

Expand All @@ -47,8 +47,8 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
}
}
else if (dwReason == DLL_PROCESS_DETACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourTransactionBegin(TRUE);
DetourUpdateAllOtherThreads();
DetourDetach(&(PVOID&)Real_Echo, Mine_Echo);
error = DetourTransactionCommit();

Expand Down
Loading