From 359f3a9b3ad5bb9c38cc4cf652773fd87c7abbe6 Mon Sep 17 00:00:00 2001 From: YANNIS Date: Tue, 27 May 2025 16:13:41 +0200 Subject: [PATCH] ajout de console de debogage --- .Debugger.cpp.swp | Bin 0 -> 12288 bytes Breakpoint.cpp | 14 +++++++++++- Breakpoint.hpp | 18 ++++++++++----- Debugger.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++- Debugger.hpp | 11 +++++++++- 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 .Debugger.cpp.swp diff --git a/.Debugger.cpp.swp b/.Debugger.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..be818c585cb25d67c8c6a30f78e9db8b077430f3 GIT binary patch literal 12288 zcmeI2&2Jk;7{;fFuKP>BK;4m|IEuaj1#z@gGu{mJp{e7y6# z&%Bc=?oJ&&c!)pRoMw3KV(k1E`v&iRc@H~RXUvTpN7(e>@~@=dt?NhR^aLth*WR#k)3SsgVR$q$)4J7 ze%GBVS3jQ~sTI%)Xa%$aS^=$qRzNGD70?P?a|$Sb2YU@sUzbL{n!fKCc~3v;H?4qH zKr5gX&<4x5*G|U%02je;;5+ak2*F-34W_^@z`zf; zG4>XC6TA*igQtN3CcvM!GWI*T2)+fMfKAW?FM=1q8n_=Y@arvz6(|sZW1t0o8Ds1$ z*aWA*QSbz~7o5GBu}{Hs;1GBUJPDfMr<<^6@HsdGUIATj6zl`{feSY>_7T_wD_|G+ z85;ixJ^=57_du-sHRu~1vQ|JV@V_arvB5ZA8*B{!=%-NG?Y8AbiXS+@Yb)-$>z#19 z; zhUAOkxvzqbil6YAB!W_tPZly0XVdJ5M$E{yLwT z;k6{1T0M&{6zV?@?V*1d+TJ8Igi^#+$klOVsEqCR7jKE4j|_wuB^Bac;-pM_#Yyc8 zJn1o9`ip@K+|j?@&7Kw6~R&n8OxR;OBhV2v-j zO2qw=d1-{>d47{0IpqsJL2*uS$31ED?8|!X_QK-a%F@B*C4&}cxn)OI znq1?{9xr6%Juh-?ZYItWVVK%C9ySms$-m@AdXL*|VHKXHJr=eeSqht)%iYZrhh6nb zV@1eeQv_xx45=EWjiA-FjXLE?s>xYfhGy3hWDp9H(k>O>J+sXS^88qQ)c6Jsrw@Tp zQQ-2f--IJNBmks=vN$WT8sp>iOokod(lS^$9!N{^)%*yr;lR0}D#<{@GIN*_(uttb z)0`Fr*)z&p?TWs14V;{EW4zM_8!^Dvih@8;GU9OJ+*GPa$tIUg9M0dru}3*>KMDt( zu4%0sP$5YSR7@nyGBsCNrt2HIl;}RP-H{S?rF%sQuWYyL=hlX-P7+nm3#eF9?kG!? zHQp|#Aj@H8s#+0y*xv4l7Fwj$t2n9EiKrmV({;f8z_VaIPUm0P$-!sjvJ<9M*mL>T z7!Q@HqOifIQgLw!n3gaOA9?oi`NhusGs{O%&x%_nzbDckFe;w9s3d8_*5I3pOYYh6c&69|b)4Dq zM6pHEC%7W_NY`>ATkr#+?7ODZ9L$#5A{W0u*wwc1RM;X9Z*5I1RBe|YA`<4XG;1x3 RyE*Xv8f&$3J>oCh{|%4@mU;jH literal 0 HcmV?d00001 diff --git a/Breakpoint.cpp b/Breakpoint.cpp index 77568b3..ef07b6c 100644 --- a/Breakpoint.cpp +++ b/Breakpoint.cpp @@ -9,7 +9,14 @@ #include Breakpoint::Breakpoint(pid_t pid, std::intptr_t addr) - : m_pid(pid), m_addr(addr) {} + : m_pid(pid), m_addr(addr) { + std::cout << "Creating breakpoint at address: " << std::hex << addr << std::dec << " for process: " << pid << std::endl; + if (addr % 4 != 0) { + throw std::runtime_error("[!] Breakpoint address is not 4-byte aligned (AArch64 requires alignment)."); + } + m_enabled = false; + m_saved_data = 0; +} void Breakpoint::enable() { if (m_addr % 4 != 0) { @@ -63,3 +70,8 @@ bool Breakpoint::is_enabled() const { return m_enabled; } +std::ostream& operator<<(std::ostream& os, const Breakpoint& bp) { + os << "pid: " << bp.m_pid << "Breakpoint at " << std::hex << bp.m_addr << (bp.m_enabled ? " (enabled)" : " (disabled)"); + return os; +} + diff --git a/Breakpoint.hpp b/Breakpoint.hpp index cb98cb5..6b30417 100644 --- a/Breakpoint.hpp +++ b/Breakpoint.hpp @@ -2,11 +2,18 @@ #pragma once #include #include +#include +#include class Breakpoint { +private: + pid_t m_pid; + std::intptr_t m_addr; + bool m_enabled = false; + uint8_t m_saved_data; public: - Breakpoint(pid_t pid, std::intptr_t addr); + Breakpoint(pid_t pid, std::intptr_t addr); void enable(); void disable(); @@ -14,10 +21,9 @@ public: bool is_enabled() const; -private: - pid_t m_pid; - std::intptr_t m_addr; - bool m_enabled = false; - uint8_t m_saved_data; + friend std::ostream& operator<<(std::ostream& os, const Breakpoint& bp); + + }; + diff --git a/Debugger.cpp b/Debugger.cpp index 39d629e..461e95b 100644 --- a/Debugger.cpp +++ b/Debugger.cpp @@ -6,7 +6,9 @@ #include #include -Debugger::Debugger(const std::string& prog_name) : program_name(prog_name) {} +Debugger::Debugger(const std::string& prog_name) : program_name(prog_name) { + std::cout << "[+] Debugger initialized for program: " << program_name << "\n"; +} void Debugger::run() { child_pid = fork(); @@ -30,9 +32,60 @@ void Debugger::run_debugger() { waitpid(child_pid, &status, 0); std::cout << "[+] Debugger attached to PID: " << child_pid << "\n"; + DEBUGING(); + ptrace(PTRACE_CONT, child_pid, nullptr, nullptr); waitpid(child_pid, &status, 0); std::cout << "[+] Child process exited.\n"; } +void Debugger::breakpoint_list() { + std::cout << "Breakpoints:\n"; + for (const auto& bp : breakpoints) { + std::cout << bp << std::endl; + } +} + +void Debugger::set_breakpoint(pid_t pid, std::intptr_t addr) { + Breakpoint bp(pid, addr); + bp.enable(); + breakpoints.push_back(bp); + std::cout << "[+] Breakpoint set at address: " << std::hex << addr << std::dec << "\n"; +} + +void Debugger::remove_breakpoint(pid_t pid, std::intptr_t addr) { + auto it = std::remove_if(breakpoints.begin(), breakpoints.end(), + [pid, addr](const Breakpoint& bp) { + return bp.get_address() == addr && bp.is_enabled(); + }); + if (it != breakpoints.end()) { + it->disable(); + breakpoints.erase(it, breakpoints.end()); + std::cout << "[+] Breakpoint removed at address: " << std::hex << addr << std::dec << "\n"; + } else { + std::cout << "[-] No breakpoint found at address: " << std::hex << addr << std::dec << "\n"; + } +} + +void Debugger::DEBUGING() { + std::cout << "[+] Entering debugging loop. Type 'exit' to quit.\n"; + std::string command; + while (true) { + std::cout << "debugger> "; + std::getline(std::cin, command); + if (command == "exit") { + break; + } else if (command == "list") { + breakpoint_list(); + } else if (command.substr(0, 3) == "set") { + std::intptr_t addr = std::stol(command.substr(4), nullptr, 16); + set_breakpoint(child_pid, addr); + } else if (command.substr(0, 6) == "remove") { + std::intptr_t addr = std::stol(command.substr(7), nullptr, 16); + remove_breakpoint(child_pid, addr); + } else { + std::cout << "Unknown command: " << command << "\n"; + } + } +} diff --git a/Debugger.hpp b/Debugger.hpp index 99e9a05..2a89ba3 100644 --- a/Debugger.hpp +++ b/Debugger.hpp @@ -2,17 +2,26 @@ #pragma once #include #include +#include +#include +#include "Breakpoint.hpp" class Debugger { public: Debugger(const std::string& prog_name); + ~Debugger() {std::cout << "Debugger destroyed." << std::endl;} + void breakpoint_list(); + void set_breakpoint(pid_t pid, std::intptr_t addr); + void remove_breakpoint(pid_t pid, std::intptr_t addr); void run(); + void DEBUGING(void); private: void run_target(); void run_debugger(); - + std::string program_name; pid_t child_pid; + std::vector breakpoints; };