From 9680ed596db87636c33001bd5a205c44d38117e1 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 31 Jan 2025 09:17:29 +0100 Subject: ADD: port debugger to Mac OS X. Signed-off-by: Amlal El Mahrouss --- dev/LibDebugger/.private | 0 dev/LibDebugger/Debugger.h | 102 --------------------------------------- dev/LibDebugger/IDebugger.h | 113 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 102 deletions(-) create mode 100644 dev/LibDebugger/.private delete mode 100644 dev/LibDebugger/Debugger.h create mode 100644 dev/LibDebugger/IDebugger.h (limited to 'dev/LibDebugger') diff --git a/dev/LibDebugger/.private b/dev/LibDebugger/.private new file mode 100644 index 0000000..e69de29 diff --git a/dev/LibDebugger/Debugger.h b/dev/LibDebugger/Debugger.h deleted file mode 100644 index d99d8ac..0000000 --- a/dev/LibDebugger/Debugger.h +++ /dev/null @@ -1,102 +0,0 @@ -/*** - (C) 2025 Amlal El Mahrouss - */ - -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace LibDebugger -{ - /// \brief Debugger interface class in C++ - /// \author Amlal El Mahrouss - class IDebugger final - { - public: - IDebugger() = default; - ~IDebugger() = default; - - IDebugger& operator=(const IDebugger&) = default; - IDebugger(const IDebugger&) = default; - - public: - void Attach(pid_t pid) - { - this->m_pid = pid; - - if (ptrace(PTRACE_ATTACH, this->m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Attach"); - return; - } - - waitpid(m_pid, nullptr, 0); - - std::cout << "[+] Attached to process: " << m_pid << std::endl; - } - - void SetBreakpoint(void* addr) - { - long original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, nullptr); - if (original_data == -1) - { - perror("dbg: Peek"); - return; - } - - long data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) - if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) - { - perror("dbg: Poke"); - return; - } - - std::cout << "[+] Breakpoint set at: " << addr << std::endl; - - m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data - } - - void ContinueExecution() - { - if (ptrace(PTRACE_CONT, m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Cont"); - return; - } - - int status; - waitpid(m_pid, &status, 0); - - if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) - { - std::cout << "[!] Breakpoint hit." << std::endl; - } - } - - void Detach() - { - if (ptrace(PTRACE_DETACH, m_pid, nullptr, nullptr) == -1) - { - perror("dbg: Detach"); - return; - } - - std::cout << "[-] Detached from process: " << m_pid << std::endl; - } - - std::unordered_map& Breakpoints() - { - return m_breakpoints; - } - - private: - pid_t m_pid; - std::unordered_map m_breakpoints; - }; -} // namespace LibDebugger diff --git a/dev/LibDebugger/IDebugger.h b/dev/LibDebugger/IDebugger.h new file mode 100644 index 0000000..957cea9 --- /dev/null +++ b/dev/LibDebugger/IDebugger.h @@ -0,0 +1,113 @@ +/*** + (C) 2025 Amlal El Mahrouss + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#define PTRACE_ATTACH PT_ATTACH +#define PTRACE_DETACH PT_DETACH +#define PTRACE_POKETEXT PT_WRITE_I +#define PTRACE_CONT PT_CONTINUE +#define PTRACE_PEEKTEXT PT_READ_I +#endif + +namespace LibDebugger +{ + typedef char* VmAddress; + + /// \brief Debugger interface class in C++ + /// \author Amlal El Mahrouss + class IDebugger final + { + public: + IDebugger() = default; + ~IDebugger() = default; + + IDebugger& operator=(const IDebugger&) = default; + IDebugger(const IDebugger&) = default; + + public: + void Attach(pid_t pid) + { + this->m_pid = pid; + + if (ptrace(PTRACE_ATTACH, this->m_pid, nullptr, 0) == -1) + { + perror("dbg: Attach"); + return; + } + + waitpid(m_pid, nullptr, 0); + + std::cout << "[+] Attached to process: " << m_pid << std::endl; + } + + void SetBreakpoint(VmAddress addr) + { + long original_data = ptrace(PTRACE_PEEKTEXT, m_pid, addr, 0); + + if (original_data == -1) + { + perror("dbg: Peek"); + return; + } + + long data_with_int3 = (original_data & ~0xFF) | 0xCC; // Insert INT3 (0xCC) + if (ptrace(PTRACE_POKETEXT, m_pid, addr, data_with_int3) == -1) + { + perror("dbg: Poke"); + return; + } + + std::cout << "[+] Breakpoint set at: " << addr << std::endl; + + m_breakpoints[reinterpret_cast(addr)] = original_data; // Store original data + } + + void ContinueExecution() + { + if (ptrace(PTRACE_CONT, m_pid, nullptr, 0) == -1) + { + perror("dbg: Cont"); + return; + } + + int status; + waitpid(m_pid, &status, 0); + + if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) + { + std::cout << "[!] Breakpoint hit." << std::endl; + } + } + + void Detach() + { + if (ptrace(PTRACE_DETACH, m_pid, nullptr, 0) == -1) + { + perror("dbg: Detach"); + return; + } + + std::cout << "[-] Detached from process: " << m_pid << std::endl; + } + + std::unordered_map& Breakpoints() + { + return m_breakpoints; + } + + private: + pid_t m_pid; + std::unordered_map m_breakpoints; + }; +} // namespace LibDebugger -- cgit v1.2.3