Skip to content

Instantly share code, notes, and snippets.

@Barakat
Last active July 17, 2018 00:04
Show Gist options
  • Save Barakat/8aee21bf4e015f8d8ddc99dd80ae1162 to your computer and use it in GitHub Desktop.
Save Barakat/8aee21bf4e015f8d8ddc99dd80ae1162 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <cassert>
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
// التعليمات مولّدة من هذا الكود:
//
// #include <Windows.h>
//
// DWORD WINAPI
// ThreadProc(LPVOID lpParameter)
// {
// (void)lpParameter;
// return 0xbaaaaaad;
// }
static const unsigned char instructions[] = {
0x55, // push %rbp
0x48, 0x89, 0xe5, // mov %rsp, %rbp
0x48, 0x89, 0x4d, 0x10, // mov %rcx, 0x10(%rbp)
0xb8, 0xad, 0xaa, 0xaa, 0xba, // mov $0xbaaaaaad, %eax
0x5d, // pop %rbp
0xc3 // retq
};
// رقم العملية التي تريد حقنها
DWORD process_id = 7012;
// الحصول على مقبض لكائن العملية
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id);
assert(process != nullptr);
// إنشاء صفحة ذاكرة بصلاحية القراءة والكتابة
auto remote_base_address = VirtualAllocEx(process, nullptr, sizeof(instructions), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
assert(remote_base_address != nullptr);
// نكتب الكود فيها، ثم تغيير صلاحيتها لتكون قابلة للقراءة والتنفيذ
WriteProcessMemory(process, remote_base_address, instructions, sizeof(instructions), nullptr);
DWORD old_page_protection;
VirtualProtectEx(process, remote_base_address, sizeof(instructions), PAGE_EXECUTE_READ, &old_page_protection);
(void)old_page_protection;
// إنشاء خيط معالجة يبدأ تنفيذه من بداية الشل كود
auto remote_thread = CreateRemoteThread(process,
nullptr,
0,
reinterpret_cast<LPTHREAD_START_ROUTINE>(remote_base_address),
nullptr,
0,
nullptr);
// انتظار انتهاء تنفيذ خيط المعالجة
WaitForSingleObject(remote_thread, INFINITE);
// قراءة حالة الانتهاء للتأكد من نجاح الحقن
DWORD remote_thread_exit_code;
GetExitCodeThread(remote_thread, &remote_thread_exit_code);
assert(remote_thread_exit_code == 0xbaaaaaad);
// إغلاق مقبض خيط المعالجة
CloseHandle(remote_thread);
// تحرير ذاكرة الصفحة المحجوزة
VirtualFreeEx(process, remote_base_address, sizeof(instructions), MEM_RELEASE);
// إغلاق مقبض العملية
CloseHandle(process);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment