BanMe
June 18th, 2010, 14:17
well i am back.. be it for better or worse... I owe a little something to someone first.. but that will be dealt with..
so im still working on this little toy and the newest feature i am adding to it is remote veh takeover... with a dynamic hook handler (page_guard,or int3hooks) I know I got a bit of work left in the int3 part of it adding the function to write a int3 for instance but w/e, i am after just opinions on this approach....
its basicly write HANDLER to process reference HANDLER in injected code to install VEH by locating the VehListHead in RtlAddVectoredExceptionHandler and using InsertHeadList..ive redone basicly everything and am nearing release of v3...
so im still working on this little toy and the newest feature i am adding to it is remote veh takeover... with a dynamic hook handler (page_guard,or int3hooks) I know I got a bit of work left in the int3 part of it adding the function to write a int3 for instance but w/e, i am after just opinions on this approach....
its basicly write HANDLER to process reference HANDLER in injected code to install VEH by locating the VehListHead in RtlAddVectoredExceptionHandler and using InsertHeadList..ive redone basicly everything and am nearing release of v3...
Code:
typedef struct _SIN_REQUEST_MESSAGE
{
PORT_MESSAGE Header;
SIN_REQUEST_TYPE ReqType;
}SIN_REQUEST_MESSAGE,*PSIN_REQUEST_MESSAGE;
typedef struct _SIN_REQUEST_VEH_TAKEOVER
{
SIN_REQUEST_MESSAGE Header;
ULONG HandleExceptionType;
ULONG TargetFunc
ULONG HookFunc
ANSI_STRING ProcessName;
}SIN_REQUEST_VEH_TAKEOVER,*PSIN_REQUEST_VEH_TAKEOVER;
typedef struct _VEH_HIJACK_SETUP
{
RTLALLOCATEHEAP Alloc;
RTLENCODEPOINTER Encode;
RTLENTERCRITICALSECTION CriticalEnter;
RTLLEAVECRITICALSECTION CriticalLeave;
ULONG RtlpVectoredExceptionLock;
ULONG VehListHead;
void *VectoredHandler;
}VEH_HIJACK_SETUP,*PVEH_HIJACK_SETUP;
...
case HK_REMOTE_VEH_TAKEOVER:
{
ReqVehHook = (SIN_REQUEST_VEH_TAKEOVER*)ReqMessage;
if(SinSrvSetupVeHijack(&HijackInfo))
{
if(ReqVehHook->HandleExceptionType == 0)
{
if(NT_SUCCESS(RtlAnsiStringToUnicodeString(&Unicode,&ReqVehHook->ProcessName,false)))
{
ClientId.UniqueProcess = GetPidByName(Unicode.Buffer);
ClientId.UniqueThread = 0;
if(NT_SUCCESS(NtOpenProcess(&TargetProc,PROCESS_ALL_ACCESS,0,&ClientId)))
{
Size = GetFunctionLength(HookHandler);
if(NT_SUCCESS(NtAllocateVirtualMemory(TargetProc,&HookLocation,0,(SIZE_T*)Size,MEM_COMMIT,PAGE_EXECUTE_READWRITE)))
{
if(NT_SUCCESS(NtWriteVirtualMemory(TargetProc,HookLocation,HookHandler,Size,&Address)))
{
if(NT_SUCCESS(NtWriteVirtualMemory(TargetProc,HookLocation,ReqVehHook->TargetFunc,sizeof(ULONG),&Address)))
{
if(NT_SUCCESS(NtWriteVirtualMemory(TargetProc,((PVOID)((ULONG)HookLocation+sizeof(ULONG)*10)),ReqVeh Hook->HookFunc,&Address)))
{
Size = GetFunctionLength(SinSrvSetupVeHijack);
if(NT_SUCCESS(NtAllocateVirtualMemory(TargetProc,&HandlerLocation,0,(SIZE_T*)Size,MEM_COMMIT,PAGE_EXECUTE_READWRITE)))
{
if(NT_SUCCESS(NtWriteVirtualMemory(TargetProc,HandlerLocation,SinSrvSetupVeHijack)))
{
HijackInfo.VectoredHandler = HandlerLocation;
if(NT_SUCCESS(RtlCreateUserThread(TargetProc,0,false,0,0,0,(PUSER_THREAD_START_ROUTINE)HandlerLocati on,&HijackInfo,TargetThread,&ClientId)))
{
break;
}
}
}
}
}
}
}
}
}
break;
}
else
{
//int3installer
}
}
break;
....
bool SinSrvSetupVeHijack(__out VEH_HIJACK_SETUP*HijackSetup)
{
ULONG Address = 0;
BYTE FindThis[] = {0x89, 0x06, 0x00, 0x04};
BYTE FindThis2[] = {0x89 ,0x46, 0x0, 0x5};
HijackSetup->Alloc = (RTLALLOCATEHEAP)Native_GetApi(L"ntdll.dll","RtlAllocateHeap";
HijackSetup->CriticalEnter = (RTLENTERCRITICALSECTION)Native_GetApi(L"ntdll.dll","RtlEnterCriticalSection";
HijackSetup->Encode = (RTLENCODEPOINTER)Native_GetApi(L"ntdll.dll","RtlEncodePointer";
HijackSetup->CriticalLeave = (RTLLEAVECRITICALSECTION)Native_GetApi(L"ntdll.dll","RtlLeaveCriticalSection";
Address = (ULONG)Native_GetApi(L"ntdll.dll","RtlAddVectoredExceptionHandler";
HijackSetup->VehListHead = (ULONG_PTR)FindCode((ULONG)Address,(BYTE*)&FindThis);
HijackSetup->RtlpVectoredExceptionLock = FindCode(Address,(BYTE*)&FindThis2);
if(!HijackSetup->Alloc && !HijackSetup->CriticalEnter && !HijackSetup->Encode && !HijackSetup->CriticalLeave && !HijackSetup->VehListHead && !HijackSetup->RtlpVectoredExceptionLock)
{
return false;
}
return true;
}
void SinSrvHijackVEH(VEH_HIJACK_SETUP*HijackSetup)
{
__asm
{
RTL_HEAP:
nop
nop
nop
nop
RTL_ENCODED:
nop
nop
nop
nop
}
HijackSetup->Alloc(NtCurrentTeb()->ProcessEnvironmentBlock->ProcessHeap,0,sizeof(RTL_VECTORED_EXCEPTION_HANDLER));
__asm
{
lea ecx, RTL_HEAP
mov dword ptr[ecx],eax
}
if(NT_SUCCESS(HijackSetup->CriticalEnter((PRTL_CRITICAL_SECTION)HijackSetup->RtlpVectoredExceptionLock)))
{
__asm
{
lea edx,HijackSetup
push eax
push edx
push [edx]HijackSetup.VectoredHandler
call [edx]HijackSetup.Encode
cmp eax, 0
je Fail
mov ecx,eax
pop edx
pop eax
add eax,8
mov dword ptr[eax],ecx
mov ecx,dword ptr[edx]HijackSetup.VehListHead
push ecx
lea eax,RTL_HEAP
push eax
call InsertHeadList
}
if(NT_SUCCESS(HijackSetup->CriticalLeave((PRTL_CRITICAL_SECTION)HijackSetup->RtlpVectoredExceptionLock)))
{
return;
}
Fail:
return;
}
}
__declspec(naked) ULONG HookHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
__asm
{
TargetFuncs:
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
HookFuncs:
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
Int3Buffer:
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
xor ecx,ecx
mov ebx,dword ptr [ExceptionInfo]
mov eax,dword ptr[ebx]ExceptionInfo.ExceptionRecord.ExceptionCode
cmp eax,0x80000001
je FindGuardPoint
mov eax,dword ptr[ebx]ExceptionInfo.ExceptionRecord.ExceptionCode
cmp eax,0x80000003
je FindBreakPoint
FindGuardPoint:
cmp ecx,10
je ContinueSearch
mov eax,4
imul eax,ecx
lea edx,TargetFuncs
mov esi,dword ptr[edx+eax]
cmp byte ptr [esi],0x90
je ContinueSearch
cmp esi,[ebx]ExceptionInfo.ContextRecord.Eip
je FoundGp
inc ecx
jmp FindGuardPoint
FoundGp:
lea esi,HookFuncs
mov esi,dword ptr [esi+eax]
cmp byte ptr[esi],0x90
mov dword ptr[ebx]ExceptionInfo.ContextRecord.Eip,esi
mov eax,-1
ret
FindBreakPoint:
cmp ecx,10
je ContinueSearch
mov eax,4
imul eax,ecx
lea esi,Int3Buffer
mov edx,dword ptr[esi+eax]
cmp byte ptr[edx],0x90
je ContinueSearch
cmp edx,dword ptr[ebx]ExceptionInfo.ContextRecord.Eip
je FoundBp
inc ecx
inc ecx
jmp FindBreakPoint
FoundBp:
inc ecx
mov eax,4
imul eax,ecx
mov ecx,dword ptr[esi+eax]
cmp byte ptr[ecx],0x90
je ContinueSearch
mov ebx,[ebx]ExceptionInfo.ContextRecord.Eip
mov dword ptr [ebx],ecx
mov eax,-1
ret
ContinueSearch:
mov eax,0
ret
}
}