<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://pwn2nice.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://pwn2nice.github.io/" rel="alternate" type="text/html" /><updated>2026-11-24T01:38:35+00:00</updated><id>https://pwn2nice.github.io/feed.xml</id><title type="html">pwn2nice’s blog</title><subtitle>Security research on iOS and Windows internals, exploit development, and low-level tooling.</subtitle><author><name>Yasser</name></author><entry><title type="html">Autonomous stack spoofing, using callback execution</title><link href="https://pwn2nice.github.io/2026/11/23/cb-exec.html" rel="alternate" type="text/html" title="Autonomous stack spoofing, using callback execution" /><published>2026-11-23T00:00:00+00:00</published><updated>2026-11-23T00:00:00+00:00</updated><id>https://pwn2nice.github.io/2026/11/23/cb-exec</id><content type="html" xml:base="https://pwn2nice.github.io/2026/11/23/cb-exec.html"><![CDATA[<p>Automating an arguably uncomplicated process to make our lives easier</p>

<p>Stack spoofing, the manual version, goes like this: open a dll, scroll, find a function that calls a pointer it was handed, check which argument holds the pointer, test it, it works (or not…), and then start again with the next one. It is slow in a way that feels weird, because the pattern you are looking for is the same every time. A function takes a pointer in, and somewhere inside it, it calls that pointer.</p>

<p>So I wrote <code class="language-plaintext highlighter-rouge">cbexec</code> to automate that tedious scrolling for me. Give it a folder of DLLs (system32 is a good start…) and it lists every function that runs a callback it received as an argument. You still have to test the sites yourself of course, but direct callbacks work almost every time. At least you are starting from a list instead of from an empty disassembler.</p>

<p>It builds a CFG for each function and it runs taint analysis over it. That is the whole method.</p>

<p>This post covers what a hit is, how the scanner finds one, the CFG part that took the most work, and how to read the output without drowning in it.</p>

<h2 id="why-is-this-useful-for-anti-analysis">Why is this useful for anti-analysis?</h2>

<p>Upon entering a suspicious function like interacting with memory protection on a region and more, EDRs walk your thread’s stack to understand where that call came from. Odds are, if your thread’s top frame point to an unbacked, unsigned, R(W)X region… I’ve got some bad news for your dynamic resilience against EDRs. Each return address within that stack points into a module. When every frame points into a signed DLL, the stack looks normal. That is the entire problem, really. You can have clean code and a clean implant and still get caught by the stack, because the stack is a list of where you came from.</p>

<p>A callback gets around that without faking anything. A legitimate function within a signed module takes your function pointer as an argument and calls it. The return address it pushes sits inside that function, inside a legitimate module. Your code runs, and the stack says a legitimate DLL called something.</p>

<pre><code class="language-mermaid">flowchart LR
    A[Your code] --&gt;|passes a function pointer| B[Legit export in a signed DLL]
    B --&gt;|calls the pointer| C[Your callback]
    C --&gt;|return address on the stack| B
    B -.-&gt;|stack walk sees| D[Signed DLL frame]
</code></pre>

<p><em>Your code hands a pointer to a legit export. The export calls it back. The return address on the stack belongs to the export.</em></p>

<p>For evasion: you do not need a gadget that fakes a stack. That works but it’s very complicated to do well and will probably generate more suspicious behavior than not. You need a real function, in a real DLL, that calls the pointer you hand it.</p>

<h2 id="what-counts-as-a-hit">What counts as a hit</h2>

<p>A hit is a function with an indirect <code class="language-plaintext highlighter-rouge">call</code> or <code class="language-plaintext highlighter-rouge">jmp</code> whose target came from that function incoming parameter. That is the definition the code uses, and it is worth staying close to it, because everything else follows from it.</p>

<p>According to the x64 Windows ABI, the first four arguments arrive in <code class="language-plaintext highlighter-rouge">rcx</code>, <code class="language-plaintext highlighter-rouge">rdx</code>, <code class="language-plaintext highlighter-rouge">r8</code>, and <code class="language-plaintext highlighter-rouge">r9</code>. The rest is pushed on the stack, starting at <code class="language-plaintext highlighter-rouge">[rsp+0x28]</code>. <code class="language-plaintext highlighter-rouge">cbexec</code> seeds taint from all of them. The four registers, their home slots in the shadow space, and stack arguments 4 through 11. Then it watches where that taint goes. If it reaches the target of an indirect transfer, that transfer is a hit.</p>

<p>There are two tiers of hits, and the difference matters when you go to use one!</p>

<p><code class="language-plaintext highlighter-rouge">Direct</code> means the argument value itself is the target. The function calls the pointer it received, with no load in between. <code class="language-plaintext highlighter-rouge">Derived</code> means the target was loaded or computed through the argument, from a struct field, a vtable entry, or an address built from the argument. The call still runs code the caller supplied, just one step removed. I rank direct first in the report, because those are the ones you know should work. A derived site can work just as well, it just takes more reading to be sure what it will call.</p>

<h2 id="how-it-finds-them">How it finds them</h2>

<p>Function bounds come from your DLL’s <code class="language-plaintext highlighter-rouge">.pdata</code>, the <code class="language-plaintext highlighter-rouge">RUNTIME_FUNCTION</code> table. I use that because the bounds are exact. No guessing where a function starts from its prologue, no pattern matching for <code class="language-plaintext highlighter-rouge">push rbp</code> and hoping the next function starts after it!</p>

<p>Each function gets Capstone disassembly and a CFG, and then the scan runs in two phases. I did it this way because running full taint on every function in System32 would take forever, and most functions never call anything indirectly at all.</p>

<pre><code class="language-mermaid">flowchart TD
    PE[PE file] --&gt; PDATA[.pdata function bounds]
    PDATA --&gt; DIS[Capstone disassembly and CFG]
    DIS --&gt; SYN[Step 1: reachable indirect transfer?]
    SYN --&gt;|No| SKIP[Skip the function]
    SYN --&gt;|Yes| TAINT[Step 2: taint fixpoint]
    TAINT --&gt; HIT[Hit: tainted transfer target]
    HIT --&gt; OUT[Ranked report]
</code></pre>

<p>Step one is syntactic, it walks reachability from the entry, computes CFG leaders, classifies transfers, and checks whether the function contains a reachable indirect transfer. If it does not, the function is skipped. Step two is taint, and it only runs where phase one found a candidate. It is a fixpoint over basic blocks, seeded at the entry with the argument registers and stack slots. Taint moves through <code class="language-plaintext highlighter-rouge">mov</code>, <code class="language-plaintext highlighter-rouge">lea</code>, and ALU ops.</p>

<p>The part I spent the most time getting right was the frame tracking, a stack argument is easy to lose track of, because the prologue moves <code class="language-plaintext highlighter-rouge">rsp</code> around and then the code reads the argument through <code class="language-plaintext highlighter-rouge">rbp</code> or through a saved pointer register. The analysis tracks <code class="language-plaintext highlighter-rouge">rsp</code> and <code class="language-plaintext highlighter-rouge">rbp</code> frame deltas, so a stack argument stays readable after all of that. It also tracks the shape MSVC emits when it does <code class="language-plaintext highlighter-rouge">mov rax, rsp</code> and then <code class="language-plaintext highlighter-rouge">lea rbp, [rax-0x57]</code>. If you do not handle that, you lose a lot of stack arguments for no good reason.</p>

<p>Calls clear the volatile registers. <code class="language-plaintext highlighter-rouge">rax</code>, <code class="language-plaintext highlighter-rouge">rcx</code>, <code class="language-plaintext highlighter-rouge">rdx</code>, <code class="language-plaintext highlighter-rouge">r8</code>, <code class="language-plaintext highlighter-rouge">r9</code>, <code class="language-plaintext highlighter-rouge">r10</code>, and <code class="language-plaintext highlighter-rouge">r11</code>. If a callback pointer only lives in a volatile across a call, it is gone after that call, and the analysis treats it that way. That is conservative, and it will miss some sites, but the alternative is worse, ending up reporting calls that cannot happen, because the register held something else by the time the transfer ran.</p>

<p>There is one rule in here that I want to call out, because it kills a whole class of false positives and it is easy to get wrong: a value loaded from memory counts as derived through an argument only when the base address came from a parameter. A parameter used as an index into static data does not count on its own. Why? A switch jump table and an image thunk both select image content. They are not caller supplied pointers, even when the index came from an argument. Index taint only counts together with a tainted base. I kept tripping over jump tables in early runs, and this was the fix. It is a small rule, and it does most of the filtering.</p>

<h2 id="control-flow-guard-or-why-a-naive-scanner-misses-most-of-it">Control Flow Guard, or why a naive scanner misses most of it</h2>

<p>This is the part that broke a simpler version of the tool, and it is the reason the hit count looks the way it does.</p>

<p>MSVC with <code class="language-plaintext highlighter-rouge">/guard:cf</code> rewrites <code class="language-plaintext highlighter-rouge">call reg</code> into a direct call to a shared dispatch thunk:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>call rel32 -&gt;  thunk: jmp rel32 ... -&gt; jmp qword ptr [rip+slot]
slot (.rdata / .00cfg): &lt;stored VA of a landing pad&gt;
landing pad:           jmp rax
</code></pre></div></div>

<p>Look at that chain for a second. There is no <code class="language-plaintext highlighter-rouge">call reg</code> left at the call site. There is a direct call to a thunk, and the real target rides in the landing pad register at the call site, in <code class="language-plaintext highlighter-rouge">rax</code> in the example above. A scanner that only looks for <code class="language-plaintext highlighter-rouge">call reg</code> misses every one of these sites. On a modern System32 build, that is most of them. I mean that literally. In the run below, 122389 of the 123647 indirect calls are CFG dispatches.</p>

<pre><code class="language-mermaid">flowchart LR
    CALL[call thunk] --&gt; THUNK[thunk: jmp rel32]
    THUNK --&gt; SLOT["jmp qword ptr [rip + slot]"]
    SLOT --&gt; PAD["landing pad: jmp rax"]
    PAD --&gt; TARGET[Target in rax at the call site]
</code></pre>

<p><code class="language-plaintext highlighter-rouge">cbexec</code> resolves the thunk chain. It follows the jumps, reads the slot, checks that the landing pad ends in <code class="language-plaintext highlighter-rouge">jmp reg</code>, and reads that register taint at the call site. Those hits carry <code class="language-plaintext highlighter-rouge">; cfg -&gt; reg</code> in the instruction text and <code class="language-plaintext highlighter-rouge">via=cfg</code> in the JSON. If you see <code class="language-plaintext highlighter-rouge">via=cfg</code> in the output, that is what it means. The call site looked direct, and the transfer was indirect one hop later.</p>

<p>Imports look similar at first glance, so the resolver has to tell them apart. An import or GOT slot holds 0, a foreign VA, or a 32-bit RVA. None of those point into the executable section of the image. A CFG slot points at a landing pad inside the image. A stored value that points at an ordinary function fails the <code class="language-plaintext highlighter-rouge">jmp reg</code> pad test, so it is not mistaken for a dispatch. That check matters, because without it you would start flagging every import call as a callback site, and the list would be useless.</p>

<h2 id="reading-the-output">Reading the output</h2>

<p>Here is a run, straight from the README:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python3 find_cb_exec.py /path/to/dlls -r -j 8 -o /tmp/out/cbexec
[1/112] acmigration.dll  funcs=1273 icalls=351 hits=146
...
[112/112] wpx.dll  funcs=1552 icalls=6529 hits=2403
CBEXEC_DONE files=112 funcs=114672 icalls=123647 cfg=122389 hits=69735 out=/tmp/out/cbexec
</code></pre></div></div>

<p>112 DLLs, 114672 functions, 123647 indirect calls, 69735 hits. Yes, 69735, it looks like a bug but surprisingly… it is not! Callback shaped functions are everywhere in Windows. That is also why the report ranks the list. Direct tier first, then named functions, then the lowest argument index. Without the ranking, a list that big is just noise.</p>

<p>Take one hit and walk through it, because the JSON is easier to read once you have done that once:</p>

<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">{</span><span class="w">
  </span><span class="nl">"call"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0x1800011b9"</span><span class="p">,</span><span class="w"> </span><span class="nl">"kind"</span><span class="p">:</span><span class="w"> </span><span class="s2">"call"</span><span class="p">,</span><span class="w"> </span><span class="nl">"form"</span><span class="p">:</span><span class="w"> </span><span class="s2">"reg"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"tier"</span><span class="p">:</span><span class="w"> </span><span class="s2">"derived"</span><span class="p">,</span><span class="w"> </span><span class="nl">"args"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="mi">6</span><span class="p">],</span><span class="w"> </span><span class="nl">"insn"</span><span class="p">:</span><span class="w"> </span><span class="s2">"call 0x180064010  ; cfg -&gt; rax"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"bytes"</span><span class="p">:</span><span class="w"> </span><span class="s2">"e8522e0600"</span><span class="p">,</span><span class="w"> </span><span class="nl">"via"</span><span class="p">:</span><span class="w"> </span><span class="s2">"cfg"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"func"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0x180001150"</span><span class="p">,</span><span class="w"> </span><span class="nl">"fname"</span><span class="p">:</span><span class="w"> </span><span class="s2">"sub_180001150"</span><span class="p">,</span><span class="w"> </span><span class="nl">"fsize"</span><span class="p">:</span><span class="w"> </span><span class="mi">116</span><span class="p">,</span><span class="w">
  </span><span class="nl">"offset"</span><span class="p">:</span><span class="w"> </span><span class="mi">105</span><span class="p">,</span><span class="w"> </span><span class="nl">"is_named"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>The callback sits at <code class="language-plaintext highlighter-rouge">0x1800011b9</code>, inside function <code class="language-plaintext highlighter-rouge">0x180001150</code>. That function is 116 bytes long, and the callback is 105 bytes in, so it is near the end. Argument index 6 is the third stack argument. Remember the numbering: 0 is <code class="language-plaintext highlighter-rouge">rcx</code>, 1 is <code class="language-plaintext highlighter-rouge">rdx</code>, 2 is <code class="language-plaintext highlighter-rouge">r8</code>, 3 is <code class="language-plaintext highlighter-rouge">r9</code>, 4 is the first stack argument, so 6 is the third stack argument. The tier is derived, and the transfer goes through a CFG dispatch thunk, with the target in <code class="language-plaintext highlighter-rouge">rax</code> at the call site.</p>

<p>Two fields are there for direct use. <code class="language-plaintext highlighter-rouge">offset</code> is the byte distance from the function entry to the transfer. Jump (NOT CALL) there to skip the prologue. This is extremely important if you supply arguments to whatever you’re using your callback for. Odds are, your arguments will get corrupted otherwise. <code class="language-plaintext highlighter-rouge">is_named</code> is true when the containing function is an export with a real symbol name, and false for a <code class="language-plaintext highlighter-rouge">sub_&lt;va&gt;</code> label. A named export is easier to call. An unnamed site still works if you can reach it by RVA but it is probably build-specifc and takes more work to get there.</p>

<p>The tool writes four files. <code class="language-plaintext highlighter-rouge">cbexec.jsonl</code> holds one record per DLL with every hit. <code class="language-plaintext highlighter-rouge">cbexec_hits.tsv</code> is the same hits as a flat table, with the bytes and disassembly in each row so you can grep it. <code class="language-plaintext highlighter-rouge">cbexec_summary.tsv</code> holds per DLL counters. <code class="language-plaintext highlighter-rouge">cbexec_report.md</code> is the grouped report for reading. I use the TSV most of the time, because I am usually filtering for one tier or one argument and I do not want to parse JSON to do that.</p>

<h2 id="using-a-hit">Using a hit</h2>

<p>Start with a direct site and a low argument index. Argument 0 in <code class="language-plaintext highlighter-rouge">rcx</code> is the easiest to supply. A named export in a DLL your process already loads is easier still. You call the export, you pass your function pointer in the right slot, and the export calls it back.</p>

<p>For evasion: the return address on the stack while your callback runs belongs to that export, so a stack walk sees the legitimate DLL. That covers the frames above your callback. It says nothing about what your callback does once it runs, or about the frames your callback creates itself. But this is trivial.</p>

<p>Test each site before you rely on it. A static hit only says the tainted value reaches the transfer. It does not say whether the function is reachable in practice, whether earlier code rejects your argument, or whether the call succeeds with the arguments you pass. The repo is there for that reason. <code class="language-plaintext highlighter-rouge">tests/verify.py</code> scans <code class="language-plaintext highlighter-rouge">tests/fixture.dll</code>, a 64-bit build of <code class="language-plaintext highlighter-rouge">tests/fixture.c</code> with one named direct site and one unnamed derived site, and it compares every hit against <code class="language-plaintext highlighter-rouge">tests/expected.json</code>. If you change the scanner, run that first. It will tell you if you broke the two shapes that matter.</p>

<h2 id="the-same-list-for-defense">The same list, for defense</h2>

<p>Flip it around and the output is a watchlist. These are the functions an implant can hide behind. Hook them or log their arguments, and you see the callback pointer at the moment it is handed over.</p>

<p>The size of the list is a problem here too. 69735 hits across 112 DLLs means callback execution cannot be blocked as a shape. Legitimate code does it constantly. So what do you actually look for? A callback argument that points outside any loaded module is a good start. A stack that only looks legitimate because one frame was borrowed is another. And a function that is rarely called with that argument in normal use is worth watching, even if you cannot block it outright, but that might be hard to quantify, I will give you that. Rank by tier, by name, and by argument index, then check what actually shows up at runtime. The static list tells you where to look. It does not tell you what is malicious.</p>

<h2 id="what-it-does-not-do">What it does not do</h2>

<p>The analysis is intra-procedural. Taint starts at the function entry and stops at the function boundary. If one function stores a callback in a struct and a second function calls it later, <code class="language-plaintext highlighter-rouge">cbexec</code> misses the second half. If a callback passes through a helper call in a volatile register, the taint dies at that call by design. I know that loses real sites. I kept it that way because tracking taint across calls would have meant tracking it across the whole program and that is a different tool.</p>

<p>It proves nothing at runtime. Every hit still needs a live test, with the real arguments, in the real process, under your real EDR.</p>

<h2 id="build-and-run">Build and run</h2>

<p>There is not much to it:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip install -r requirements.txt
python3 find_cb_exec.py &lt;dll-or-dir&gt;... [-r] [-j N] [-o DIR]
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">pefile</code> and <code class="language-plaintext highlighter-rouge">capstone</code> are the only dependencies, <code class="language-plaintext highlighter-rouge">-r</code> recurses into directories, <code class="language-plaintext highlighter-rouge">-j</code> sets parallel workers with 0 for auto up to 8, and <code class="language-plaintext highlighter-rouge">-o</code> sets the output directory. Output is byte identical for any <code class="language-plaintext highlighter-rouge">-j</code>, so a parallel run and a serial run give the same files. <code class="language-plaintext highlighter-rouge">sh tests/build.sh</code> rebuilds the fixture and its expectation, and it needs <code class="language-plaintext highlighter-rouge">x86_64-w64-mingw32-gcc</code>.</p>

<p>Have fun with this!</p>]]></content><author><name>Yasser</name></author><category term="Windows" /><category term="evasion" /><summary type="html"><![CDATA[Automating an arguably uncomplicated process to make our lives easier]]></summary></entry><entry><title type="html">Hello world</title><link href="https://pwn2nice.github.io/2026/09/17/hello-world.html" rel="alternate" type="text/html" title="Hello world" /><published>2026-09-17T12:00:00+00:00</published><updated>2026-09-17T12:00:00+00:00</updated><id>https://pwn2nice.github.io/2026/09/17/hello-world</id><content type="html" xml:base="https://pwn2nice.github.io/2026/09/17/hello-world.html"><![CDATA[<p>hello world</p>]]></content><author><name>Yasser</name></author><category term="Meta" /><summary type="html"><![CDATA[hello world]]></summary></entry></feed>