-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some suppot for Themida v3, remove a couple MSVC-isms
- Loading branch information
Showing
9 changed files
with
593 additions
and
245 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,119 @@ | ||
unit Tracer; | ||
|
||
interface | ||
|
||
uses Windows, SysUtils, Utils; | ||
|
||
type | ||
TTracePredicate = function(const C: TContext): Boolean of object; | ||
|
||
TTracer = class | ||
private | ||
FProcessID, FThreadID: Cardinal; | ||
FThreadHandle: THandle; | ||
FPredicate: TTracePredicate; | ||
FCounter, FLimit: Cardinal; | ||
FLimitReached: Boolean; | ||
Log: TLogProc; | ||
|
||
function OnSingleStep(const Ev: TDebugEvent): Cardinal; | ||
public | ||
constructor Create(AProcessID, AThreadID: Cardinal; AThreadHandle: THandle; | ||
APredicate: TTracePredicate; ALog: TLogProc); | ||
|
||
procedure Trace(AAddress: NativeUInt; ALimit: Cardinal); | ||
|
||
property Counter: Cardinal read FCounter; | ||
property LimitReached: Boolean read FLimitReached; | ||
end; | ||
|
||
implementation | ||
|
||
{ TTracer } | ||
|
||
constructor TTracer.Create(AProcessID, AThreadID: Cardinal; AThreadHandle: THandle; | ||
APredicate: TTracePredicate; ALog: TLogProc); | ||
begin | ||
FProcessID := AProcessID; | ||
FThreadID := AThreadID; | ||
FThreadHandle := AThreadHandle; | ||
FPredicate := APredicate; | ||
Log := ALog; | ||
end; | ||
|
||
procedure TTracer.Trace(AAddress: NativeUInt; ALimit: Cardinal); | ||
var | ||
C: TContext; | ||
Ev: TDebugEvent; | ||
Status: Cardinal; | ||
begin | ||
FCounter := 0; | ||
FLimit := ALimit; | ||
FLimitReached := False; | ||
|
||
C.ContextFlags := CONTEXT_CONTROL; | ||
if not GetThreadContext(FThreadHandle, C) then | ||
RaiseLastOSError; | ||
|
||
C.Eip := AAddress; | ||
C.EFlags := C.EFlags or $100; // Trap | ||
if not SetThreadContext(FThreadHandle, C) then | ||
RaiseLastOSError; | ||
|
||
if not ContinueDebugEvent(FProcessID, FThreadID, DBG_CONTINUE) then | ||
Exit; | ||
|
||
Status := DBG_EXCEPTION_NOT_HANDLED; | ||
while WaitForDebugEvent(Ev, INFINITE) do | ||
begin | ||
case Ev.dwDebugEventCode of | ||
EXCEPTION_DEBUG_EVENT: | ||
begin | ||
if Ev.Exception.ExceptionRecord.ExceptionCode = EXCEPTION_SINGLE_STEP then | ||
begin | ||
Status := OnSingleStep(Ev); | ||
if Status = DBG_CONTROL_BREAK then | ||
Break; | ||
end | ||
else | ||
begin | ||
Log(ltFatal, 'Unexpected exception during tracing: ' + IntToHex(Ev.Exception.ExceptionRecord.ExceptionCode, 8)); | ||
Exit; | ||
end; | ||
end; | ||
|
||
else | ||
Status := DBG_CONTINUE; | ||
end; | ||
|
||
ContinueDebugEvent(Ev.dwProcessId, Ev.dwThreadId, Status); | ||
end; | ||
end; | ||
|
||
function TTracer.OnSingleStep(const Ev: TDebugEvent): Cardinal; | ||
var | ||
C: TContext; | ||
begin | ||
Inc(FCounter); | ||
if (FLimit <> 0) and (FCounter > FLimit) then | ||
begin | ||
FLimitReached := True; | ||
Log(ltInfo, 'Giving up trace due to instruction limit'); | ||
Exit(DBG_CONTROL_BREAK); | ||
end; | ||
|
||
C.ContextFlags := CONTEXT_CONTROL; | ||
if not GetThreadContext(FThreadHandle, C) then | ||
RaiseLastOSError; | ||
|
||
C.EFlags := C.EFlags or $100; | ||
if not SetThreadContext(FThreadHandle, C) then | ||
RaiseLastOSError; | ||
|
||
if FPredicate(C) then | ||
Result := DBG_CONTROL_BREAK | ||
else | ||
Result := DBG_CONTINUE; | ||
end; | ||
|
||
end. |
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,46 @@ | ||
[SETTINGS] | ||
CurrentProfile=Magicmida | ||
[Magicmida] | ||
DLLNormal=1 | ||
DLLStealth=0 | ||
DLLUnload=0 | ||
GetLocalTimeHook=0 | ||
GetSystemTimeHook=0 | ||
GetTickCount64Hook=0 | ||
GetTickCountHook=0 | ||
KiUserExceptionDispatcherHook=1 | ||
NtCloseHook=0 | ||
NtContinueHook=1 | ||
NtCreateThreadExHook=0 | ||
NtGetContextThreadHook=1 | ||
NtQueryInformationProcessHook=1 | ||
NtQueryObjectHook=0 | ||
NtQueryPerformanceCounterHook=0 | ||
NtQuerySystemInformationHook=1 | ||
NtQuerySystemTimeHook=0 | ||
NtSetContextThreadHook=0 | ||
NtSetDebugFilterStateHook=0 | ||
NtSetInformationThreadHook=1 | ||
NtSetInformationProcessHook=0 | ||
NtUserBlockInputHook=0 | ||
NtUserBuildHwndListHook=0 | ||
NtUserFindWindowExHook=0 | ||
NtUserQueryWindowHook=0 | ||
NtUserGetForegroundWindowHook=1 | ||
NtYieldExecutionHook=0 | ||
OutputDebugStringHook=0 | ||
PebBeingDebugged=1 | ||
PebHeapFlags=1 | ||
PebNtGlobalFlag=1 | ||
PebStartupInfo=0 | ||
PebOsBuildNumber=0 | ||
PreventThreadCreation=0 | ||
RemoveDebugPrivileges=0 | ||
AutostartServer=1 | ||
ServerPort=1337 | ||
BreakOnTLS=1 | ||
FixOllyBugs=1 | ||
RemoveEPBreak=0 | ||
SkipEPOutsideCode=1 | ||
X64Fix=1 | ||
WindowTitle=Themida |