forked from ayoubfaouzi/al-khaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ayoubfaouzi#200 from LordNoteworthy/Noteworthy
add trap flag anti debug
- Loading branch information
Showing
8 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "pch.h" | ||
|
||
#include "TrapFlag.h" | ||
|
||
/* | ||
This technique is similar to exceptions based debugger detections. | ||
You enable the trap flag in the current process and check whether | ||
an exception is raised or not. If an exception is not raised, you | ||
can assume that a debugger has “swallowed” the exception for us, | ||
and that the program is being traced. The beauty of this approach | ||
is that it detects every debugger, user mode or kernel mode, | ||
because they all use the trap flag for tracing a program. | ||
Vectored Exception Handling is used here because SEH is an | ||
anti-debug trick in itself. | ||
*/ | ||
|
||
static BOOL SwallowedException = TRUE; | ||
|
||
static LONG CALLBACK VectoredHandler( | ||
_In_ PEXCEPTION_POINTERS ExceptionInfo | ||
) | ||
{ | ||
SwallowedException = FALSE; | ||
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) | ||
{ | ||
//Increase EIP/RIP to continue execution. | ||
#ifdef _WIN64 | ||
ExceptionInfo->ContextRecord->Rip++; | ||
#else | ||
ExceptionInfo->ContextRecord->Eip++; | ||
#endif | ||
return EXCEPTION_CONTINUE_EXECUTION; | ||
} | ||
return EXCEPTION_CONTINUE_SEARCH; | ||
} | ||
|
||
|
||
|
||
BOOL TrapFlag() | ||
{ | ||
PVOID Handle = AddVectoredExceptionHandler(1, VectoredHandler); | ||
SwallowedException = TRUE; | ||
|
||
#ifdef _WIN64 | ||
UINT64 eflags = __readeflags(); | ||
#else | ||
UINT eflags = __readeflags(); | ||
#endif | ||
|
||
// Set the trap flag | ||
eflags |= 0x100; | ||
__writeeflags(eflags); | ||
|
||
RemoveVectoredExceptionHandler(Handle); | ||
return SwallowedException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
BOOL TrapFlag(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.