<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Théo Jehl on Medium]]></title>
        <description><![CDATA[Stories by Théo Jehl on Medium]]></description>
        <link>https://medium.com/@theomacx86?source=rss-349bd390cfa8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XFGcuhXMkRFjMuIfKjKqcw.jpeg</url>
            <title>Stories by Théo Jehl on Medium</title>
            <link>https://medium.com/@theomacx86?source=rss-349bd390cfa8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:02:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@theomacx86/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A taste of their own medicine: defeat self-debugging techniques with GDB]]></title>
            <link>https://medium.com/@theomacx86/a-taste-of-their-own-medicine-defeat-self-debugging-techniques-with-gdb-bbbd17bde3f7?source=rss-349bd390cfa8------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbbd17bde3f7</guid>
            <category><![CDATA[gdb]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <dc:creator><![CDATA[Théo Jehl]]></dc:creator>
            <pubDate>Thu, 20 Jul 2023 13:37:33 GMT</pubDate>
            <atom:updated>2023-07-20T13:37:33.385Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m9aSYXfpNaeKmlxOMlYD0g.png" /><figcaption>GDB Welcome screen</figcaption></figure><p>When launching a malware campaign, attackers take into account the moment before the attack, the attack itself, but what about the aftermath?</p><p>Slowing down researchers and reverse-engineering efforts can make the campaign last longer, self-debugging is one of the techniques that can slow down a researcher trying to debug the malware in a live environment.</p><p>Some other legitimate programs can use this technique for example to protect a game against cheaters using a tool like CheatEngine to modify the game memory while it is running.</p><p>During my time at the Lille’s University, I wrote a script using GDB and GDB’s Python API to automate self-debugging program analysis easily and efficiently.</p><p>What’s a self-debugging program someone might say? Excellent question, before answering it, how debugging works?</p><p>To debug itself, a program uses a system call ptrace. Ptrace allows a program to control another one, this is exactly what GDB uses to debug programs. Ptrace allows a program to read/write another program’s memory, registers, and control its execution flow. An important property of this system call is: A program can be debugged by one program at a time, debugging an already debugged program will fail.</p><p>We can already infer how one might achieve self-debugging, let’s take an example, we have a program with a main process that will fork itself, taking one of two paths.</p><ul><li>Child: Enters an infinite loop, in the code this loop is followed by the main program logic</li><li>Parent: fork, then tries to debug its child with ptrace(PTRACE_ATTACH,…) request. If the request is a success, it edits the child to exit the loop and enter the main logic. If it fails however, exit.</li></ul><p>And now the interesting part is, what if a debugger is in the way..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*qvnyUtKitN8FVWtrmGVavQ.png" /><figcaption>GDB default configuration for a parent with a child</figcaption></figure><p>In the default configuration, GDB follows the fork and thread, and the self-debugging protection will work. This is a simple concept, real-world programs with this kind of protection uses sophisticated self-debugging measures alongside other protections.</p><p>Disabling it will make the child process untracked, escaping analyst control. Another option of GDB is system call interception, <em>catchpoints. </em>This allows the analyst to intercept a system call and pause the debugger. At this point two paths are possible: emulate the system calls by re-implementing the logic within the script, which is long, time-consuming, and might break compatibility.</p><p>What if we let the program do its system calls and debug its child? In a controlled way, of course, to achieve this, some GDB scripts can help us automate this.</p><p>The only emulated ptrace call is the ptrace_attach, we are in the same configuration as before, being GDB having control over the two processes. So the ptrace_attach will fail, the script simply puts “0” in the register holding the return value, making it seems like the call was a success.</p><p>For the technique itself, the program needs to be modified first, at attach time, the current instruction is saved and replaced by a syscall. This allows us to execute a memmap syscall, some memory is allocated within the debugged program space, allowing us to load code in it.</p><p>When a ptrace call is intercepted, GDB puts the child into an infinite loop and detaches itself from it. It will allow the parent to attach to its child, during this timeframe, the infinite loop will prevent the child from executing code outside of GDB’s control.</p><p>The parent will execute 3 instructions that were injected:</p><ul><li>Pthread attach: the parent gain back control over it’s child</li><li>Original ptrace call: the ptrace call the program intended to do in the first place</li><li>Pthread detach: The process gives back control to GDB</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*j-IqbmJuy3-u5wOLDSTNGw.png" /></figure><p>This was made possible using GDB’s Python API, but this API is not complete and not fully working. The script works on GDB 9, and playing with ptrace on Linux might not work OOB because of ptrace_scope, a security setting enforced by the kernel. An analyst might want to use a VM with <strong>/proc/sys/kernel/yama/ptrace_scope </strong>set to 0.</p><blockquote>The source code is still internal to this day and will be released soon, this article will be updated once sources are available<br>Little warning while I’m at it: I’m no expert in “Pythonic” code, I mainly use it as a scripting and prototyping language, the syntax might blind some of you and I’m sorry for this.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbbd17bde3f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>