Red Team/My 프로젝트

maldev 모듈 81 완료 후 복기 및 방법론 정리

wonder12 2023. 8. 29. 20:03

Thread Hijacking - Local Thread Creation

그러니까 local을 실행하면서 suspened state dummy therad(아무것도 기능안함) 만들고 modify pointer to payload's base address , resume
revshell 이라 network connection

 

 

Thread Hijacking - Remote Thread Creation

#remote process
그냥 target process 열고, 
notepad.exe, > enum process, target handle 가졌으니> Createthread도 가능. suspended state 
아무래도 local보단 낫고
일반 process  dll, shellcode injection보단 CreateThread대신(payload contents 숨겨야되서) thread hijacking이 낫지


CreateProess > suspended state process  = all thread state
> CreateRemoteThread

CreaetPRocessA
lpApplicationName  - full path cmd.exe or null
lpCommandLine - /k whoami or full path+ command
dwCreationFlags - CREATE_SUSPENDED
return PROCESS_INFORMATION structure- hProcess, hThread, PID, TID
거기서 thread hijaking 하면 됨

GetEnvironmentVairableA
processname 넣으면 %WINDIR%\\System32\\processname로 full path  만들도록

clean structs setting value to 0


remote니까
VirtualAllocEx
WriteProcessMemory
VirtualProtectEx




int main() {


//get hProcess, hThread, PID, TID
CreateSuspendedProcess("notepad.exe", &dwPid, &hProcess,, )

//store payload to target process  
if(!InjectShellcodeToRemoteProcess(hProcess, Payload, , &pAddress)){ 
return -1; 


//suspedned thread hijacking
HijackThread(hThread, dummyfunction?)
ㄴㄴ 그냥 CreaetThread 만들필요없이 기존 TID 아무거나 쓰면 change pointer 할 수 있음
}

notepad 열리진 않는데 뜨네 process

 

 

Thread Hijacking - Local Thread Enumeration

#local thread enum
CreateToolhelp32Snapshot > enum system's running threads 
enum process대신 

dwFlags diff value - TH32CS_SNAPTHREAD
return THREADENTRY32 structure - TID, TID 의 PID
identify parent PID 
if match, open(retreive) handle 

OpenThread = handle
GetCUrrentProcessId  = local PID

os create worker threads in process  = valid target for thread hijacking
ntdll.dll!EtwNotificationRegister+0x2d0 > to run EtwNotificationRegister : notify os system when event occurs in process 


input MainthreadID = to avoid main thread of local process
return targetTID, hThread

local process 안에서 생성되는거임

TID, handle to thread 얻었으면
여기에 hijacking해야지

payload를 in momory에 저장하려면 VirtualAlloc, virtualprotect , memcopy 필요할텐데? ㅇㅇ> return pAddress
굳이 CreateThread안해도 suspended state로 만들 수 있음  
SuspendThread(hThread);
그다음 hijacking : Get CONTEXT > modify pointer 
ResumeThread
//실행될때까지 기다림 
WaitForSingleObject(hThread, INFINITE);


unsinged char Payload[] = {};

int main() {
//dwMainThreadId = GetCurrentThreadId();
GetLocalThreadHandle(dwMainThreadId, &TID, &hTHread);


InjectShellcodeToLocalProcess(Payload )

HijackThread(hThread, pAddress)
}

 

Thread Hijacking - Remote Thread Enumeration

#remote thread enum
아까는 in current process에서 mainthread피하고 (CreateToolhelp32Snapshot = enum thread) system work thread에 handle얻어서 suspend 상태만들고 modify CONTEXT ->pointer 했음


enum , find process 한다음 = notepad.exe 에서 (hProcess)
system work thread 있을테니 handle 얻어서 memory remote process에 payload wrtie한다음 suspend만들고 modify context



//RemoteThreadHijacking.exe notepad.exe(argv[1]) 로 사용하려면 argc , wchar_t* argv[] 해줘야함 
int wmain(int argc, wchar_t* argv[]) {

CreateToolhelp32Snapshot > enum process > return PID, (OpenProcess - hProcess)

GetRemoteThreadhandle(dwPid, &ThreadID, &hThread )[CreateToolhelp32Snapshot >  process's running thread - TID, OpenThread- hThread]

//remote process handle 필요
RemoteShellcodeInjection(hProcess, Payload, &pAddress)

HijackThread(hThread, pAddress)


//CloseHandle hThread, hProcess해줘야됨
}

main thread가 아니라 좀오래걸릴 수 있음