Skip to content

Commit

Permalink
Merge pull request ayoubfaouzi#200 from LordNoteworthy/Noteworthy
Browse files Browse the repository at this point in the history
add trap flag anti debug
  • Loading branch information
ayoubfaouzi authored Feb 3, 2020
2 parents 6e66d44 + d7fd5d8 commit 654b266
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#### 0.80

- Anti anti-debug trick: trap flag.
- Add check for well known names used by malware sandboxes.
- Improve ProcessDebugObject anti-debug check thanks to @Mattiwatti

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Please, if you encounter any of the anti-analysis tricks which you have seen in
- Memory Breakpoints (PAGE_GUARD)
- Interrupt 0x2d
- Interrupt 1
- Trap Flag
- Parent Process (Explorer.exe)
- SeDebugPrivilege (Csrss.exe)
- NtYieldExecution / SwitchToThread
Expand Down
3 changes: 2 additions & 1 deletion al-khaser/Al-khaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(void)
BOOL ENABLE_DUMPING_CHECK = TRUE;
BOOL ENABLE_ANALYSIS_TOOLS_CHECK = TRUE;
BOOL ENABLE_ANTI_DISASSM_CHECKS = TRUE;

/* Resize the console window for better visibility */
resize_console_window();

Expand Down Expand Up @@ -70,6 +70,7 @@ int main(void)
exec_check(&SoftwareBreakpoints, TEXT("Checking Software Breakpoints "));
exec_check(&Interrupt_0x2d, TEXT("Checking Interupt 0x2d "));
exec_check(&Interrupt_3, TEXT("Checking Interupt 1 "));
exec_check(&TrapFlag, TEXT("Checking trap flag"));
exec_check(&MemoryBreakpoints_PageGuard, TEXT("Checking Memory Breakpoints PAGE GUARD "));
exec_check(&IsParentExplorerExe, TEXT("Checking If Parent Process is explorer.exe "));
exec_check(&CanOpenCsrss, TEXT("Checking SeDebugPrivilege "));
Expand Down
57 changes: 57 additions & 0 deletions al-khaser/AntiDebug/TrapFlag.cpp
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;
}
3 changes: 3 additions & 0 deletions al-khaser/AntiDebug/TrapFlag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

BOOL TrapFlag();
3 changes: 3 additions & 0 deletions al-khaser/al-khaser.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<ClInclude Include="AntiDebug\SharedUserData_KernelDebugger.h" />
<ClInclude Include="AntiDebug\SoftwareBreakpoints.h" />
<ClInclude Include="AntiDebug\TLS_callbacks.h" />
<ClInclude Include="AntiDebug\TrapFlag.h" />
<ClInclude Include="AntiDebug\UnhandledExceptionFilter_Handler.h" />
<ClInclude Include="AntiDebug\WriteWatch.h" />
<ClInclude Include="AntiDebug\WUDF_IsDebuggerPresent.h" />
Expand Down Expand Up @@ -269,6 +270,7 @@
<ClCompile Include="AntiDebug\SharedUserData_KernelDebugger.cpp" />
<ClCompile Include="AntiDebug\SoftwareBreakpoints.cpp" />
<ClCompile Include="AntiDebug\TLS_callbacks.cpp" />
<ClCompile Include="AntiDebug\TrapFlag.cpp" />
<ClCompile Include="AntiDebug\UnhandledExceptionFilter_Handler.cpp" />
<ClCompile Include="AntiDebug\WriteWatch.cpp" />
<ClCompile Include="AntiDebug\WUDF_IsDebuggerPresent.cpp" />
Expand Down Expand Up @@ -322,6 +324,7 @@
<MASM Include="AntiDisassm\AntiDisassm_x86.asm">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
<UseSafeExceptionHandlers Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</UseSafeExceptionHandlers>
</MASM>
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions al-khaser/al-khaser.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@
<ClInclude Include="AntiDisassm\pch.h">
<Filter>AntiDisassm\Header</Filter>
</ClInclude>
<ClInclude Include="AntiDebug\TrapFlag.h">
<Filter>AntiDebug\Header</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="al-khaser.cpp" />
Expand Down Expand Up @@ -458,6 +461,9 @@
<ClCompile Include="AntiDisassm\AntiDisassm.cpp">
<Filter>AntiDisassm\Source</Filter>
</ClCompile>
<ClCompile Include="AntiDebug\TrapFlag.cpp">
<Filter>AntiDebug\Source</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="AntiDebug\int2d_x86.asm">
Expand Down
Binary file modified al-khaser/pch.h
Binary file not shown.

0 comments on commit 654b266

Please sign in to comment.