<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts on pugachev.io</title><link>https://pugachev.io/posts/</link><description>Recent content in Posts on pugachev.io</description><generator>Hugo</generator><language>en-US</language><copyright>© Maxim Pugachev</copyright><lastBuildDate>Fri, 04 Dec 2026 13:46:18 +0100</lastBuildDate><atom:link href="https://pugachev.io/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>angr_ctf, rebuilt</title><link>https://pugachev.io/2026/11/18/angr_ctf-rebuilt/</link><pubDate>Wed, 18 Nov 2026 18:21:11 +0100</pubDate><guid>https://pugachev.io/2026/11/18/angr_ctf-rebuilt/</guid><description>&lt;p&gt;I first met &lt;a href="https://angr.io" target="_blank" rel="noopener"&gt;angr&lt;/a&gt; while solving &lt;a href="https://pugachev.io/2024/01/08/spooky-license-by-hack-the-box/"&gt;&amp;quot;Spooky License&amp;quot; from Hack The Box&lt;/a&gt;. The crackme was quite simple and didn't force me to dive into angr itself. I found out that &lt;a href="https://docs.angr.io/en/latest/quickstart.html#going-further" target="_blank" rel="noopener"&gt;angr's docs&lt;/a&gt; mention &lt;a href="https://github.com/jakespringer/angr_ctf" target="_blank" rel="noopener"&gt;angr_ctf&lt;/a&gt;, so I thought it would be a good idea to solve it as well and learn more. I played with it a bit back then, but never finished and eventually forgot about it completely.&lt;/p&gt;&#10;&lt;p&gt;angr is a kind of &amp;quot;must have&amp;quot; on my small roadmap for learning RE, so I decided to finally grasp the basics properly and came back to angr_ctf. It's Jake Springer's set of eighteen small challenges, each teaching a single symbolic-execution idea: reach an address, prune the dead paths, inject a symbol into a register or onto the stack, fake the heap, hook a function, merge states, and so on. But it turned out that its binaries are 32-bit only, and it was written for an angr from several versions ago (back when &lt;code&gt;claripy&lt;/code&gt; was still its own package), so it doesn't work with angr 10 out of the box. And my developer's urge was to update it first, solving all the challenges while rewriting them.&lt;/p&gt;&#10;&lt;p&gt;Now that I've solved them all, I don't think it's a great start for beginners. It's nice and useful, but very one-sided and artificial. Unfortunately, angr doesn't have proper, up-to-date docs at all (mostly a &lt;a href="https://docs.angr.io/en/latest/examples.html" target="_blank" rel="noopener"&gt;long list of solved CTFs&lt;/a&gt; as examples), and I don't know anything better. But hey, it's open source, so go and fix it if you don't like it, right? Looks like it's worth adding one more line to my &amp;quot;write a book about...&amp;quot; to-do list.&lt;/p&gt;&#10;&lt;p&gt;The result has the same eighteen levels (plus a new one, more on that below) with the same concepts in the same order and (mostly) the same C generators, brought up to angr 10 and built for both x86-64 and the original i386. The scaffolds (commented skeletons of the solutions with the angr parts left blank for you) and the reference solutions run on angr 10 too. I checked every binary and every solution on both architectures after solving each level, so the right password really gets you &lt;code&gt;Good Job.&lt;/code&gt; in return.&lt;/p&gt;&#10;&lt;p&gt;A few things I had to do differently, and they turned out to be interesting in themselves:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The arbitrary read/write/jump levels (15-17) need a target at an address you can type as printable ASCII. Upstream pinned a whole section (&lt;code&gt;.rodata&lt;/code&gt;, &lt;code&gt;.data&lt;/code&gt; or &lt;code&gt;.text&lt;/code&gt;, depending on the level) to a hand-picked address and hoped the layout held. I found another way: I put the target into its own &lt;code&gt;.secret&lt;/code&gt; section and pinned that, so it always lands at the right address. And it works for a function too, so level 17's jump target is real executable code.&lt;/li&gt;&#10;&lt;li&gt;The Veritesting level stopped working. Veritesting is angr's way of merging the branches of a region back into one state instead of forking on each of them. In angr 10, it seems that it no longer merges a loop body for some reason, so the original 32-iteration loop just forks forever. I split it in two: level 12 keeps the straight-line form that Veritesting still collapses, and the new level 12_1 keeps the loop and merges the states by hand.&lt;/li&gt;&#10;&lt;li&gt;Old angr needed a manual trick to drive a multi-field &lt;code&gt;scanf&lt;/code&gt;, and angr 10 just does it. So seven levels ship a second, shorter solution that relies on that, next to the one that still teaches the technique the old way.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The prebuilt binaries are in the repo, so all you need to start is Python and angr 10. It's all here: &lt;a href="https://github.com/itwaseasy/angr_ctf_rebuilt" target="_blank" rel="noopener"&gt;angr_ctf_rebuilt&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Happy CTFing!&lt;/p&gt;&#10;</description></item><item><title>GOT, PLT, RELRO, and other random characters</title><link>https://pugachev.io/2026/11/08/got-plt-relro-and-other-random-characters/</link><pubDate>Sun, 08 Nov 2026 11:16:29 +0100</pubDate><guid>https://pugachev.io/2026/11/08/got-plt-relro-and-other-random-characters/</guid><description>&lt;p&gt;Every time I open a dynamically linked ELF in a disassembler, I see the same bunch of &lt;code&gt;puts@plt&lt;/code&gt;, &lt;code&gt;_GLOBAL_OFFSET_TABLE_+0x18&lt;/code&gt;, &lt;code&gt;.plt.sec&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt; and &lt;code&gt;.got.plt&lt;/code&gt;. I usually trust IDA (until it breaks), but sometimes this blind faith, and the lack of time to understand the details behind it, makes you feel uncomfortable. So I decided to finally figure out how, say, a &lt;code&gt;puts(3)&lt;/code&gt; call in my awesome 3-liner in C finds the right &lt;code&gt;puts&lt;/code&gt; in glibc, when nobody in the toolchain knew its address at compile time. And what all these random characters around it are for. And all those sections. And ASLR with CET on top. Well, quite a lot of questions to answer.&lt;/p&gt;&#10;&lt;p&gt;Everything here is about x86-64 Linux (System V AMD64 ABI, to be more precise) and glibc. I built and ran every binary on my Debian 13.8 with gcc 14.2, binutils 2.44 and glibc 2.41, so I didn't make up any of the &lt;code&gt;objdump&lt;/code&gt;, &lt;code&gt;readelf&lt;/code&gt; and &lt;code&gt;gdb&lt;/code&gt; output below. The links to the glibc sources point to the &lt;code&gt;glibc-2.41&lt;/code&gt; tag, so the line numbers (should still) match that release. On a different toolchain the mechanics are the same (at least I believe so), only the addresses and counts like &lt;code&gt;RELACOUNT&lt;/code&gt; will be different.&lt;/p&gt;&#10;&lt;h2 id="why-any-of-this-is-needed"&gt;Why any of this is needed&lt;/h2&gt;&#10;&lt;p&gt;But let's start from the beginning. You suddenly call &lt;code&gt;puts(&amp;quot;hi&amp;quot;)&lt;/code&gt;, and this &lt;code&gt;puts&lt;/code&gt; lives inside &lt;code&gt;libc.so.6&lt;/code&gt;, which is a separate file. The loader maps it somewhere into your process, and thanks to the kernel's ASLR this &amp;quot;somewhere&amp;quot; is different every time you run the program. In other words, neither the compiler nor the linker can know the address of &lt;code&gt;puts&lt;/code&gt; in advance, so the code can't just do &lt;code&gt;call 0x7ffff1337101&lt;/code&gt;: this address doesn't exist yet, and it won't be the same next time anyway.&lt;/p&gt;&#10;&lt;p&gt;So there are two ways out of this. Either you patch the real address into every call site once you know it, or you route all calls to &lt;code&gt;puts&lt;/code&gt; through one pointer in writable memory and patch only that pointer once. The second option is what the toolchain actually does, but it's worth understanding why the first one is so bad, and &amp;quot;man, it's slow!&amp;quot; isn't even the main reason.&lt;/p&gt;&#10;&lt;h3 id="why-you-cant-just-patch-the-text"&gt;Why you can't just patch the &lt;code&gt;.text&lt;/code&gt;&lt;/h3&gt;&#10;&lt;p&gt;Say the linker left &lt;code&gt;call 0x0&lt;/code&gt; placeholders in &lt;code&gt;.text&lt;/code&gt;, and the loader fixed them up with the real address of &lt;code&gt;puts&lt;/code&gt; at load time. Four things break at once:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;It dirties code pages. &lt;code&gt;.text&lt;/code&gt; is mapped as &lt;code&gt;r-xp&lt;/code&gt;: read, execute, no write, and the strange trailing &lt;code&gt;p&lt;/code&gt;, which isn't a permission but the sharing mode in &lt;a href="https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html" target="_blank" rel="noopener"&gt;&lt;code&gt;/proc/[pid]/maps&lt;/code&gt;&lt;/a&gt; (why not, right?). This &lt;code&gt;p&lt;/code&gt; means private, or copy-on-write, and &lt;code&gt;s&lt;/code&gt; means shared, just like &lt;code&gt;MAP_PRIVATE&lt;/code&gt; and &lt;code&gt;MAP_SHARED&lt;/code&gt; in &lt;code&gt;mmap(2)&lt;/code&gt;. The &lt;code&gt;r-x&lt;/code&gt; part comes from the smart linker, which puts &lt;code&gt;.text&lt;/code&gt; into a &lt;code&gt;PT_LOAD&lt;/code&gt; segment with &lt;code&gt;R E&lt;/code&gt; flags (&lt;code&gt;p_flags = PF_R | PF_X&lt;/code&gt;, the &lt;code&gt;Flg&lt;/code&gt; column in &lt;code&gt;readelf -l&lt;/code&gt;). And the &lt;code&gt;p&lt;/code&gt; part comes from the kernel: &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1035" target="_blank" rel="noopener"&gt;&lt;code&gt;load_elf_binary()&lt;/code&gt;&lt;/a&gt; walks every &lt;code&gt;PT_LOAD&lt;/code&gt;, turns its flags into page protections with &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L611-L624" target="_blank" rel="noopener"&gt;&lt;code&gt;make_prot()&lt;/code&gt;&lt;/a&gt; and maps it as &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1041" target="_blank" rel="noopener"&gt;&lt;code&gt;MAP_PRIVATE&lt;/code&gt;&lt;/a&gt;. Such pages are then shared between all processes running the same binary, until one of them writes to a page for some reason. In that case, the kernel has to make a private copy of that page for this sneaky process only. Try patching thousands of call sites in a real program, and you end up with most of its code turned into dirty memory unique to this process. The kernel can no longer drop these pages and read them back from the file when needed, and the whole point of sharing one physical copy of libc's code between all processes is gone.&lt;/li&gt;&#10;&lt;li&gt;It needs writable code. To patch &lt;code&gt;.text&lt;/code&gt;, the loader has to make it writable, at least for a while. glibc does just that for binaries with old-school text relocations: it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L281" target="_blank" rel="noopener"&gt;adds &lt;code&gt;PROT_WRITE&lt;/code&gt;&lt;/a&gt; to the segment's &lt;code&gt;r-x&lt;/code&gt;, so for a moment the code is both writable and executable. And this is what &lt;a href="https://en.wikipedia.org/wiki/W%5EX" target="_blank" rel="noopener"&gt;W^X&lt;/a&gt; (write xor execute) exists to prevent, for buggy programs and shellcode kiddies alike. Old-school doesn't mean forgotten, though: such text relocations still happen in the wild. More on that just below.&lt;/li&gt;&#10;&lt;li&gt;The relocations multiply. Every call site of &lt;code&gt;puts&lt;/code&gt; needs its own fixup, so if you call &lt;code&gt;puts&lt;/code&gt; from forty places, that's forty addresses to patch. Route all of them through one pointer, and it's just one.&lt;/li&gt;&#10;&lt;li&gt;It doesn't even fit. A direct &lt;code&gt;call&lt;/code&gt; on x86-64 takes a signed 32-bit offset, so it can reach only 2 GB in either direction. A PIE usually lives somewhere around &lt;code&gt;0x55...&lt;/code&gt; and libc around &lt;code&gt;0x7f...&lt;/code&gt;, which is about 45 TB away, if I subtracted qwords correctly in my head. The loader simply can't encode &lt;code&gt;call puts&lt;/code&gt; there, even if you ask. Every external call would have to become something like &lt;code&gt;movabs rax, &amp;lt;puts&amp;gt;; call rax&lt;/code&gt;, which is 12 bytes instead of 5, so the compiler would have to emit this long form everywhere in advance, just in case. That's what &lt;code&gt;-mcmodel=large&lt;/code&gt; actually does, and you'll see it in action below.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;So, text relocations. They turn up when a shared object is built from non-position-independent (i.e. good old, normal, non-PIC) code: in this case, the compiler puts an absolute address right into an instruction, and the only way to fix it at load time is to write into &lt;code&gt;.text&lt;/code&gt;. Let's make one and see how unhappy the toolchain can be about it. Here &lt;code&gt;-mcmodel=large&lt;/code&gt; makes the compiler load the address of &lt;code&gt;g&lt;/code&gt; with a 64-bit &lt;code&gt;movabs&lt;/code&gt;. Without it, the default small model would, as usual, use a 32-bit absolute address, which can't hold the address of a shared object loaded somewhere high by ASLR, so the linker refuses to build it at all instead of creating a text relocation:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat tr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/* non-PIC code that bakes an absolute address into .text */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;long g = 42;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;long *get_addr(void) { return &amp;amp;g; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -shared -fno-pic -mcmodel&lt;span class="o"&gt;=&lt;/span&gt;large -o libtr.so tr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/usr/bin/ld: /tmp/cc058XuK.o: warning: relocation against `g&amp;#39; in read-only section `.text&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/usr/bin/ld: warning: creating DT_TEXTREL in a shared object&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d libtr.so &lt;span class="p"&gt;|&lt;/span&gt; grep TEXTREL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0000000000000016 (TEXTREL) 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) TEXTREL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel libtr.so&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000000010f9 &amp;lt;get_addr&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10f9:&#9;55 &#9;push rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10fa:&#9;48 89 e5 &#9;mov rbp,rsp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 10fd:&#9;48 b8 00 00 00 00 00 &#9;movabs rax,0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1104:&#9;00 00 00&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1107:&#9;5d &#9;pop rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1108:&#9;c3 &#9;ret&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r libtr.so &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;g\ &amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000010ff 000500000001 R_X86_64_64 0000000000004008 g &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The linker still allows this, but it warns loudly and marks the result: it sets both the legacy &lt;code&gt;DT_TEXTREL&lt;/code&gt; tag and the &lt;code&gt;DF_TEXTREL&lt;/code&gt; bit in &lt;code&gt;DT_FLAGS&lt;/code&gt; (the two lines &lt;code&gt;readelf -d&lt;/code&gt; prints above). The 32-bit version, as I said, it refuses fiercely with &lt;code&gt;relocation R_X86_64_32 against symbol `g' can not be used when making a shared object; recompile with -fPIC&lt;/code&gt;. At runtime, it's the dynamic loader that has to deal with this, not the kernel, which maps the segment as &lt;code&gt;r-x&lt;/code&gt; as before. glibc's &lt;code&gt;_dl_relocate_object&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L257" target="_blank" rel="noopener"&gt;checks &lt;code&gt;DT_TEXTREL&lt;/code&gt;&lt;/a&gt;, and for every &lt;code&gt;PT_LOAD&lt;/code&gt; segment without &lt;code&gt;PF_W&lt;/code&gt; it calls &lt;code&gt;__mprotect&lt;/code&gt; with &lt;code&gt;prot | PROT_WRITE&lt;/code&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L281" target="_blank" rel="noopener"&gt;L281&lt;/a&gt;). The source comment there is quite honest about it: &lt;code&gt;/* Bletch. We must make read-only segments writable long enough to relocate them. */&lt;/code&gt;. Then it applies the relocations to the now-writable code and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L326" target="_blank" rel="noopener"&gt;restores the original protections&lt;/a&gt;. So for a while the page is writable and executable at the same time, which is the exact W^X problem from item 2. After patching, it's a private dirty copy, which is the sharing problem from item 1. Two big problems at once, just because some smart guy shipped non-PIC code. That's why text relocations are a bug, not a feature: the fix is always &lt;code&gt;-fPIC&lt;/code&gt; (and &lt;code&gt;-z text&lt;/code&gt; turns &lt;code&gt;DT_TEXTREL&lt;/code&gt; into a link error). &lt;code&gt;-fPIC&lt;/code&gt; is what makes the code go through the GOT instead, which is what the rest of this post is about.&lt;/p&gt;&#10;&lt;p&gt;So once you see all these constraints, the design is kind of obvious: please keep &lt;code&gt;.text&lt;/code&gt; read-only, position-independent and clean, and put all the &amp;quot;addresses I won't know until runtime&amp;quot; into one separate writable table of pointers. Call this table the Global Offset Table, or &lt;code&gt;.got&lt;/code&gt;, and keep going. The code will refer to external things indirectly, through a &amp;quot;GOT slot&amp;quot;: if you build with &lt;code&gt;-fno-plt&lt;/code&gt;, it's literally &lt;code&gt;call QWORD PTR [rip+puts@GOTPCREL]&lt;/code&gt;. The loader writes the real address into that slot, and voila, &lt;code&gt;.text&lt;/code&gt; never changes.&lt;/p&gt;&#10;&lt;h3 id="pie-or-when-even-your-own-addresses-move"&gt;PIE, or when even your own addresses move&lt;/h3&gt;&#10;&lt;p&gt;And even without shared libraries, there's PIE (position-independent executable), which has been the default &lt;code&gt;gcc&lt;/code&gt; output for years. With PIE, the problem above applies to the program's own code and data too. It's easy to check: a plain compile gives you an &lt;code&gt;ET_DYN&lt;/code&gt;, and &lt;code&gt;gcc&lt;/code&gt; says it was configured this way:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -o t t.c &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; readelf -h t &lt;span class="p"&gt;|&lt;/span&gt; grep Type &lt;span class="c1"&gt;# -no-pie would give EXEC&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type: DYN (Position-Independent Executable file)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -v 2&amp;gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; tr &lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;\n&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; grep enable-default-pie&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;--enable-default-pie&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;A PIE is an &lt;code&gt;ET_DYN&lt;/code&gt; object, so the kernel is free to map it at any address, and it does exactly that (hey, ASLR). You can see the split right in &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1052" target="_blank" rel="noopener"&gt;&lt;code&gt;load_elf_binary()&lt;/code&gt;&lt;/a&gt;. An &lt;code&gt;ET_EXEC&lt;/code&gt; is mapped with &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1058" target="_blank" rel="noopener"&gt;&lt;code&gt;MAP_FIXED_NOREPLACE&lt;/code&gt;&lt;/a&gt; at the &lt;code&gt;p_vaddr&lt;/code&gt; the linker put into the file, without any bias, so its address is fixed at link time. An ordinary PIE, i.e. an &lt;code&gt;ET_DYN&lt;/code&gt; with a &lt;code&gt;PT_INTERP&lt;/code&gt;, gets &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1117-L1119" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias = ELF_ET_DYN_BASE&lt;/code&gt;&lt;/a&gt; plus a new random offset on every exec, &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1120-L1121" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias += arch_mmap_rnd()&lt;/code&gt;&lt;/a&gt;, if the process has the &lt;code&gt;PF_RANDOMIZE&lt;/code&gt; flag. A static-PIE (an &lt;code&gt;ET_DYN&lt;/code&gt; without an interpreter) gets &lt;a href="https://github.com/torvalds/linux/blob/v6.12/fs/binfmt_elf.c#L1126-L1130" target="_blank" rel="noopener"&gt;&lt;code&gt;load_bias = 0&lt;/code&gt;&lt;/a&gt; instead and simply lands at the mmap base, which is already randomized. By the way, &lt;a href="https://github.com/torvalds/linux/blob/v6.12/arch/x86/include/asm/elf.h#L238-L239" target="_blank" rel="noopener"&gt;&lt;code&gt;ELF_ET_DYN_BASE&lt;/code&gt;&lt;/a&gt; on x86-64 is &lt;code&gt;DEFAULT_MAP_WINDOW / 3 * 2&lt;/code&gt;, two-thirds of the way up the address space, and that's where the &lt;code&gt;0x55...&lt;/code&gt; from item 4 comes from. So the program can't even hardcode the absolute address of its own global variables or functions into instructions. Two mechanisms work together here:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;For references inside the module (a function calling another function in the same binary, or taking the address of a global variable defined in it), the compiler uses RIP-relative addressing, like &lt;code&gt;lea rax,[rip+0xec0]&lt;/code&gt;. The distance between two things in the same module doesn't depend on the load address, so the linker can calculate the offset once, and no runtime fixup is needed.&lt;/li&gt;&#10;&lt;li&gt;For anything whose absolute address has to be stored somewhere in memory (a pointer in &lt;code&gt;.data&lt;/code&gt; that points into the module, &lt;code&gt;.init_array&lt;/code&gt; and &lt;code&gt;.fini_array&lt;/code&gt; entries, GOT slots holding the module's own addresses), the module has &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations, which simply do &amp;quot;add the load base to the value at this offset&amp;quot;. The loader walks through them at startup and rebases each one, and in glibc this is literally &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L499" target="_blank" rel="noopener"&gt;&lt;code&gt;*reloc_addr = l_addr + reloc-&amp;gt;r_addend&lt;/code&gt;&lt;/a&gt; in &lt;code&gt;elf_machine_rela_relative&lt;/code&gt;. &lt;code&gt;_dl_relocate_object&lt;/code&gt; calls it from a &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/do-rel.h#L114" target="_blank" rel="noopener"&gt;fast loop&lt;/a&gt; in &lt;code&gt;elf_dynamic_do_Rela&lt;/code&gt; that goes through all the &lt;code&gt;RELATIVE&lt;/code&gt; entries at the start of the table. Their number is what &lt;code&gt;readelf -d&lt;/code&gt; prints as the &lt;code&gt;RELACOUNT&lt;/code&gt; tag. These relocations are cheap, because they don't need any symbol lookup, and for the program's own addresses they're the only ones needed:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#include &amp;lt;stdio.h&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;int main(void) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;first call&amp;#34;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;second call&amp;#34;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return 0;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_partial demo.c -Wl,-z,relro,-z,lazy &lt;span class="c1"&gt;# partial RELRO, -fcf-protection=none keeps the classic PLT layout (more on that below)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep RELACOUNT &lt;span class="c1"&gt;# .init_array, .fini_array and __dso_handle&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffff9 (RELACOUNT) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static-pie -o demo_staticpie demo.c &lt;span class="c1"&gt;# same source, built static-PIE&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep RELACOUNT &lt;span class="c1"&gt;# static-PIE has to rebase all of itself&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffff9 (RELACOUNT) 1099&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Here's &lt;code&gt;main&lt;/code&gt; from this demo. One flag needs a word up front: I build every binary in this post with &lt;code&gt;-fcf-protection=none&lt;/code&gt;. Some toolchains enable &lt;a href="https://en.wikipedia.org/wiki/Control-flow_integrity#Intel_Control-flow_Enforcement_Technology" target="_blank" rel="noopener"&gt;Intel CET&lt;/a&gt; by default (Ubuntu's gcc, for example, but not Debian's), and CET splits the PLT into two sections, which makes every disassembly below more confusing and harder to follow. So turning it off gives the classic layout with a single &lt;code&gt;.plt&lt;/code&gt;, no matter where you build it. How and why CET splits the &lt;code&gt;.plt&lt;/code&gt; is explained in &lt;a href="#pltsec-what-cet-does-to-the-plt"&gt;its own section&lt;/a&gt; further down.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001139 &amp;lt;main&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1139:&#9;55 &#9;push rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 113a:&#9;48 89 e5 &#9;mov rbp,rsp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 113d:&#9;48 8d 05 c0 0e 00 00 &#9;lea rax,[rip&amp;#43;0xec0] # 2004 &amp;lt;_IO_stdin_used&amp;#43;0x4&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1144:&#9;48 89 c7 &#9;mov rdi,rax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1147:&#9;e8 e4 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 114c:&#9;48 8d 05 bc 0e 00 00 &#9;lea rax,[rip&amp;#43;0xebc] # 200f &amp;lt;_IO_stdin_used&amp;#43;0xf&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1153:&#9;48 89 c7 &#9;mov rdi,rax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1156:&#9;e8 d5 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 115b:&#9;b8 00 00 00 00 &#9;mov eax,0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1160:&#9;5d &#9;pop rbp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1161:&#9;c3 &#9;ret&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;I use this one &lt;code&gt;demo.c&lt;/code&gt; for every &lt;code&gt;demo_*&lt;/code&gt; binary in this post, only the compile and link flags are different. I'll show the &lt;code&gt;gcc&lt;/code&gt; command next to each binary when it first comes up, instead of dumping all of them here. And &lt;code&gt;-Wl,-z,lazy&lt;/code&gt; above forces lazy binding: some toolchains (Ubuntu's, for example, but again not Debian's) use &lt;code&gt;-z now&lt;/code&gt; by default, which resolves everything at startup and skips the path I want to show. More on that in &lt;a href="#full-relro-resolve-everything-up-front-then-freeze-it"&gt;the full RELRO section&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;The strings are loaded RIP-relative without any fixups, but &lt;code&gt;puts&lt;/code&gt;, which is an external symbol, gets &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;. Note the &lt;code&gt;@plt&lt;/code&gt; suffix, which stands for Procedure Linkage Table in plain English. &lt;code&gt;puts@plt&lt;/code&gt; is a little stub, and this is where the fun begins.&lt;/p&gt;&#10;&lt;h2 id="the-cast-of-sections"&gt;The cast of sections&lt;/h2&gt;&#10;&lt;p&gt;Before going further, here are the sections involved, with a one-sentence description of each:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Section&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Perms&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;What it holds&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.plt&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r-xp&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Code stubs. Entry 0 (&lt;code&gt;PLT0&lt;/code&gt;) jumps into the resolver, and the rest are lazy stubs, one per function.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.got.plt&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;rw-p&lt;/code&gt;*&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;The writable pointer table for functions. &lt;code&gt;GOT[0..2]&lt;/code&gt; are reserved, and &lt;code&gt;GOT[3..]&lt;/code&gt; are the per-function slots that lazy binding patches.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.got&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r--p&lt;/code&gt; after RELRO&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;The pointer table for everything else: data symbols and functions whose address is taken (resolved at startup via &lt;code&gt;GLOB_DAT&lt;/code&gt; to one canonical pointer, without a lazy stub).&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;.plt.sec&lt;/code&gt; / &lt;code&gt;.plt.got&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;r-xp&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Additional code stubs, for CET and for functions called through &lt;code&gt;.got&lt;/code&gt;. More on them below.&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;* Whether &lt;code&gt;.got.plt&lt;/code&gt; stays writable is what the RELRO (&amp;quot;relocation read-only&amp;quot;) magic is all about, so keep that in mind. Even with partial RELRO, its first three entries (&lt;code&gt;GOT[0..2]&lt;/code&gt;) end up on the read-only page, as you'll see in my pwndbg session below, and with full RELRO it doesn't even exist as a separate section.&lt;/p&gt;&#10;&lt;h2 id="partial-relro-lazy-binding-described-lazily-and-step-by-step"&gt;Partial RELRO: lazy binding, described lazily and step by step&lt;/h2&gt;&#10;&lt;p&gt;The idea of lazy binding is simple: don't resolve &lt;code&gt;puts&lt;/code&gt; until it's actually called for the first time (that's why it's lazy). So if a program imports 200 functions but calls only 5 of them, it pays for 5 lookups, not 200. The price for this is one more level of indirection and a clever resolver that patches the GOT slot on the first call.&lt;/p&gt;&#10;&lt;h3 id="the-classic-three-instruction-stub"&gt;The classic three-instruction stub&lt;/h3&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001020 &amp;lt;puts@plt-0x10&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1020:&#9;ff 35 ca 2f 00 00 &#9;push QWORD PTR [rip&amp;#43;0x2fca] # 3ff0 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x8&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1026:&#9;ff 25 cc 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fcc] # 3ff8 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x10&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 102c:&#9;0f 1f 40 00 &#9;nop DWORD PTR [rax&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001030 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;ff 25 ca 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fca] # 4000 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1036:&#9;68 00 00 00 00 &#9;push 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 103b:&#9;e9 e0 ff ff ff &#9;jmp 1020 &amp;lt;_init&amp;#43;0x20&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;And here's the GOT it points into, right from the file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -s -j .got.plt demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Contents of section .got.plt:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3fe8 e03d0000 00000000 00000000 00000000 .=..............&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3ff8 00000000 00000000 36100000 00000000 ........6.......&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; in this dump is &lt;code&gt;0x3de0&lt;/code&gt; (&lt;code&gt;e03d0000&lt;/code&gt; in little-endian), which is the virtual address of &lt;code&gt;.dynamic&lt;/code&gt;, and also where &lt;code&gt;nm&lt;/code&gt; puts the &lt;code&gt;_DYNAMIC&lt;/code&gt; symbol:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;\.dynamic&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [22] .dynamic DYNAMIC 0000000000003de0 00002de0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; nm ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep _DYNAMIC&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000003de0 d _DYNAMIC&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;GOT[0]&lt;/code&gt; holds the link-time address of &lt;code&gt;_DYNAMIC&lt;/code&gt;, i.e. this object's own &lt;code&gt;.dynamic&lt;/code&gt; array, before any relocation is applied. This is required by the x86-64 psABI (the processor-specific part of the System V ABI I mentioned at the beginning). The reason is a chicken-and-egg problem: &lt;code&gt;.dynamic&lt;/code&gt; is the table that lists the object's relocations (&lt;code&gt;DT_RELA&lt;/code&gt;, &lt;code&gt;DT_JMPREL&lt;/code&gt; and friends live there), so you have to read &lt;code&gt;.dynamic&lt;/code&gt; before you can apply any relocation, which means finding &lt;code&gt;.dynamic&lt;/code&gt; can't depend on a relocation itself. I like this &amp;quot;you can't relocate while you relocate&amp;quot; logic!&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; is supposed to break this circle: the linker puts the link-time address of &lt;code&gt;.dynamic&lt;/code&gt; into that slot as a plain value, which can be read without any relocation. The one who really needs it is the dynamic linker relocating itself (omg): the kernel maps &lt;code&gt;ld.so&lt;/code&gt; and jumps to its entry point with nothing resolved, and there's nobody else to tell this &amp;quot;relocating relocator&amp;quot; where its own &lt;code&gt;.dynamic&lt;/code&gt; is. Unlike &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;, nothing rewrites &lt;code&gt;GOT[0]&lt;/code&gt; at load time, and in my pwndbg session below it's still the raw &lt;code&gt;0x3de0&lt;/code&gt;, not &lt;code&gt;base + 0x3de0&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The idea is nice and simple, but in today's glibc this slot is more of a legacy: the x86-64 loader finds &lt;code&gt;_DYNAMIC&lt;/code&gt; with a RIP-relative reference and &lt;code&gt;__ehdr_start&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L50-L64" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_dynamic&lt;/code&gt;&lt;/a&gt;, without reading &lt;code&gt;GOT[0]&lt;/code&gt; at all. The linker still fills the slot because the ABI says so, forever.&lt;/p&gt;&#10;&lt;p&gt;Unlike the first (or zeroth?) element, &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are zeros in the file and get filled at load time. &lt;code&gt;GOT[3]&lt;/code&gt; is the actual slot for the address of &lt;code&gt;puts&lt;/code&gt;, and here's the first clever part: it's initialized to &lt;code&gt;0x1036&lt;/code&gt;, which is the address of the instruction right after the &lt;code&gt;jmp&lt;/code&gt; in &lt;code&gt;puts&lt;/code&gt;'s own stub.&lt;/p&gt;&#10;&lt;p&gt;Now let's trace the very first &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;jmp QWORD PTR [0x4000]&lt;/code&gt; jumps through the GOT slot of &lt;code&gt;puts&lt;/code&gt;. The slot contains &lt;code&gt;0x1036&lt;/code&gt;, so it jumps to &lt;code&gt;0x1036&lt;/code&gt;... which is the very next instruction. To an uninitiated reader it seems like an obfuscated no-op that lands one line below, but it's not like that. The slot is pre-loaded on purpose to bounce right back into the stub.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;push 0x0&lt;/code&gt; pushes the relocation index for &lt;code&gt;puts&lt;/code&gt;. It's &lt;code&gt;0&lt;/code&gt; because &lt;code&gt;puts&lt;/code&gt; is the first (and only) entry in &lt;code&gt;.rela.plt&lt;/code&gt;:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.plt&amp;#39; at offset 0x610 contains 1 entry:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004000 000300000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The offset of this relocation is &lt;code&gt;0x4000&lt;/code&gt;, which is the GOT slot of &lt;code&gt;puts&lt;/code&gt;, and its index in the table is 0. This index is how the resolver knows what to look for.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;jmp 1020&lt;/code&gt; jumps to &lt;code&gt;PLT0&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;PLT0&lt;/code&gt; does &lt;code&gt;push [GOT+0x08]&lt;/code&gt; and then &lt;code&gt;jmp [GOT+0x10]&lt;/code&gt;. The math is simple, as long as you remember that &lt;code&gt;rip&lt;/code&gt; points to the next instruction: the &lt;code&gt;push QWORD PTR [rip+0x2fca]&lt;/code&gt; at &lt;code&gt;0x1020&lt;/code&gt; is 6 bytes long, so it reads &lt;code&gt;0x1026 + 0x2fca = 0x3ff0&lt;/code&gt;, which is &lt;code&gt;GOT[1]&lt;/code&gt;. The same way, the &lt;code&gt;jmp QWORD PTR [rip+0x2fcc]&lt;/code&gt; at &lt;code&gt;0x1026&lt;/code&gt; reads &lt;code&gt;0x102c + 0x2fcc = 0x3ff8&lt;/code&gt;, which is &lt;code&gt;GOT[2]&lt;/code&gt;. So &lt;code&gt;PLT0&lt;/code&gt; pushes &lt;code&gt;GOT[1]&lt;/code&gt; and jumps through &lt;code&gt;GOT[2]&lt;/code&gt;. And the GOT base, &lt;code&gt;0x3fe8&lt;/code&gt;, is simply the start of &lt;code&gt;.got.plt&lt;/code&gt;, which is what &lt;code&gt;_GLOBAL_OFFSET_TABLE_&lt;/code&gt; points to:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;\.got\.plt&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [24] .got.plt PROGBITS 0000000000003fe8 00002fe8&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;What's inside &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;? The loader sets them once, at load time, in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L70" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_runtime_setup&lt;/code&gt;&lt;/a&gt;, and only if binding is lazy (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L78" target="_blank" rel="noopener"&gt;line 78&lt;/a&gt;):&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;GOT[1]&lt;/code&gt; gets the &lt;code&gt;struct link_map *&lt;/code&gt; of this module, which is the loader's descriptor of the object whose symbol we're resolving (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L94" target="_blank" rel="noopener"&gt;line 94&lt;/a&gt;).&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;GOT[2]&lt;/code&gt; gets &lt;code&gt;GLRO(dl_x86_64_runtime_resolve)&lt;/code&gt;, the resolver trampoline (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L125-L126" target="_blank" rel="noopener"&gt;lines 125-126&lt;/a&gt;). This &lt;code&gt;GLRO&lt;/code&gt; field holds the variant the loader picked for this CPU at startup, &lt;code&gt;_dl_runtime_resolve_fxsave&lt;/code&gt;, &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_xsavec&lt;/code&gt; (all three are built from the same &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; source), so the slot doesn't get a fixed &lt;code&gt;&amp;amp;_dl_runtime_resolve&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;So &lt;code&gt;PLT0&lt;/code&gt; effectively calls &lt;code&gt;_dl_runtime_resolve(link_map *this_module, reloc_index)&lt;/code&gt;, where the relocation index was pushed by the stub of &lt;code&gt;puts&lt;/code&gt;, and the &lt;code&gt;link_map *&lt;/code&gt; by &lt;code&gt;PLT0&lt;/code&gt; itself. To add variety to a nerdy text, here is my pwndbg session (still no pictures) with the same &lt;code&gt;demo_partial&lt;/code&gt; binary as in the disassembly above. Try breaking at &lt;code&gt;main&lt;/code&gt; yourself and following it to the end, and your understanding will improve drastically, trust me. pwndbg's &lt;code&gt;$rebase(off)&lt;/code&gt; trick just adds the PIE base it detected, &lt;code&gt;0x555555554000&lt;/code&gt; here:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# the image base and where the GOT lands: GOT[0..2] on a read-only page, the puts slot on the writable one&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555554000 0x555555555000 r--p 1000 0 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555555000 0x555555556000 r-xp 1000 1000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555556000 0x555555557000 r--p 1000 2000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555557000 0x555555558000 r--p 1000 2000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555558000 0x555555559000 rw-p 1000 3000 demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# all three reserved slots: GOT[0] is the link-time &amp;amp;_DYNAMIC (0x3de0, the raw value from the dump above), GOT[1] is the link_map, GOT[2] is the resolver (telescope follows GOT[1] into the link_map)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x3fe8) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555557fe8 (_GLOBAL_OFFSET_TABLE_) ◂— 0x3de0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;01:0008│ 0x555555557ff0 (_GLOBAL_OFFSET_TABLE_&amp;#43;8) —▸ 0x7ffff7ffe5f0 —▸ 0x555555554000 ◂— 0x10102464c457f&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;02:0010│ 0x555555557ff8 (_GLOBAL_OFFSET_TABLE_&amp;#43;16) —▸ 0x7ffff7fda410 (_dl_runtime_resolve_xsavec) ◂— push rbx&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# the puts slot before the first call, still pointing back into its own lazy stub&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x4000) 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555558000 (puts@got[plt]) —▸ 0x555555555036 (puts@plt&amp;#43;6) ◂— push 0 /* &amp;#39;h&amp;#39; */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# break in the resolver on that first call and read its backtrace&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b _dl_fixup&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 2 at 0x7ffff7fd8050: file ./elf/dl-runtime.c, line 47.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 2, _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;warning: 47&#9;./elf/dl-runtime.c: No such file or directory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; bt 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#0 _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#1 0x00007ffff7fda48a in _dl_runtime_resolve_xsavec () at ../sysdeps/x86_64/dl-trampoline.h:98&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#2 0x000055555555514c in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(More stack frames follow...)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# let the resolver finish, then read the slot again: now it points straight to libc&amp;#39;s puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; finish&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Run till exit from #0 _dl_fixup (l=0x7ffff7ffe5f0, reloc_arg=0x0) at ./elf/dl-runtime.c:47&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;_dl_runtime_resolve_xsavec () at ../sysdeps/x86_64/dl-trampoline.h:99&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;warning: 99&#9;../sysdeps/x86_64/dl-trampoline.h: No such file or directory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Value returned is $1 = 0x7ffff7e46300&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Filtering out read-only entries (display them with -r or --show-readonly)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_partial:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Partial RELRO | Found 1 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555558000] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Everything matches, wow. The first argument of the resolver, &lt;code&gt;l = 0x7ffff7ffe5f0&lt;/code&gt;, is the same value as in &lt;code&gt;GOT[1]&lt;/code&gt;, and &lt;code&gt;reloc_arg = 0&lt;/code&gt; is the index the &lt;code&gt;puts&lt;/code&gt; stub pushed. So &lt;code&gt;PLT0&lt;/code&gt; and the stub really gave &lt;code&gt;_dl_fixup&lt;/code&gt; exactly the two things it needs, and nothing more. Before the call, the &lt;code&gt;puts&lt;/code&gt; slot points to &lt;code&gt;puts@plt+6&lt;/code&gt; (its own &lt;code&gt;push 0; jmp PLT0&lt;/code&gt; lazy stub), and after it, the slot automagically points straight to libc's &lt;code&gt;puts&lt;/code&gt; at &lt;code&gt;0x7ffff7e46300&lt;/code&gt;. So the second &lt;code&gt;puts(&amp;quot;second call&amp;quot;)&lt;/code&gt; skips the whole detour and jumps there directly.&lt;/p&gt;&#10;&lt;p&gt;Now let's map these addresses onto the &lt;code&gt;vmmap&lt;/code&gt; above. &lt;code&gt;GOT[0]&lt;/code&gt;, &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are at &lt;code&gt;...557fe8&lt;/code&gt;, &lt;code&gt;...557ff0&lt;/code&gt; and &lt;code&gt;...557ff8&lt;/code&gt;, at the very end of the &lt;code&gt;r--p&lt;/code&gt; page &lt;code&gt;...557000-...558000&lt;/code&gt;. The &lt;code&gt;puts&lt;/code&gt; slot is the next eight bytes, at &lt;code&gt;...558000&lt;/code&gt;, which is where the &lt;code&gt;rw-p&lt;/code&gt; page &lt;code&gt;...558000-...559000&lt;/code&gt; begins. So &lt;code&gt;.got.plt&lt;/code&gt; crosses the page boundary: its three reserved entries are on the read-only page, and the &lt;code&gt;JUMP_SLOT&lt;/code&gt; array (starting with &lt;code&gt;puts&lt;/code&gt;) is on the writable one. This split is on purpose, and we'll come back to it twice: right after the resolver, and when full RELRO suddenly changes everything.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[1]&lt;/code&gt; points into the data of &lt;code&gt;ld.so&lt;/code&gt; itself, because it's the &lt;code&gt;link_map&lt;/code&gt; of this object, not code. &lt;code&gt;telescope&lt;/code&gt; even follows it one step further, to the &lt;code&gt;l_addr&lt;/code&gt; field of the struct (&lt;code&gt;0x555555554000&lt;/code&gt;, the load base), and to the ELF header sitting there: &lt;code&gt;0x10102464c457f&lt;/code&gt; is just the first eight bytes &lt;code&gt;7f 45 4c 46 02 01 01 00&lt;/code&gt; read as a little-endian number, i.e. &lt;code&gt;\x7fELF&lt;/code&gt;, 64-bit, little-endian, version 1. &lt;code&gt;GOT[2]&lt;/code&gt; is &lt;code&gt;_dl_runtime_resolve_xsavec&lt;/code&gt;, not &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_fxsave&lt;/code&gt;, because this CPU supports &lt;code&gt;xsavec&lt;/code&gt; (check with &lt;code&gt;grep xsavec /proc/cpuinfo&lt;/code&gt;). This choice matters for the next part.&lt;/p&gt;&#10;&lt;h3 id="what-_dl_runtime_resolve-has-to-be-careful-about"&gt;What &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; has to be careful about&lt;/h3&gt;&#10;&lt;p&gt;&lt;code&gt;_dl_runtime_resolve&lt;/code&gt; runs in the middle of your function call: you've already set up the arguments for &lt;code&gt;puts&lt;/code&gt;, but &lt;code&gt;puts&lt;/code&gt; hasn't run yet, and &lt;code&gt;rdi&lt;/code&gt; already holds the string pointer. And the resolver is about to run a big pile of C code (symbol lookup goes through hash tables, string comparisons and so on) that will clobber every caller-saved register. If it then jumps to &lt;code&gt;puts&lt;/code&gt; with a trashed &lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;puts&lt;/code&gt; prints garbage or segfaults.&lt;/p&gt;&#10;&lt;p&gt;So the first job of the trampoline is to save all the registers used for passing arguments, and to restore them after the resolve. The &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-trampoline.h#L64-L71" target="_blank" rel="noopener"&gt;asm code&lt;/a&gt; saves &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt;, &lt;code&gt;rsi&lt;/code&gt;, &lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;r8&lt;/code&gt; and &lt;code&gt;r9&lt;/code&gt; by hand (glibc's source uses the AT&amp;amp;T syntax, but I converted it to Intel to match the rest of the post):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RAX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RCX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RDX&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rdx&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RSI&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_RDI&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;rdi&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_R8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;r8&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;REGISTER_SAVE_R9&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;r9&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;...&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="nf"&gt;fxsave&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;STATE_SAVE_OFFSET&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; or xsave/xsavec, depending on the CPU&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The integer registers are the six SysV argument registers plus &lt;code&gt;rax&lt;/code&gt;, which holds the number of vector registers used by a variadic call. Then &lt;code&gt;fxsave&lt;/code&gt;, &lt;code&gt;xsave&lt;/code&gt; or &lt;code&gt;xsavec&lt;/code&gt; saves the vector state, because the arguments can also be floats or vectors in &lt;code&gt;XMM&lt;/code&gt;, &lt;code&gt;YMM&lt;/code&gt; or &lt;code&gt;ZMM&lt;/code&gt; registers, and the resolver's own code (&lt;code&gt;memcpy&lt;/code&gt; and friends, string functions) will use them too. That's why there are several variants of the resolver: &lt;code&gt;_dl_runtime_resolve_fxsave&lt;/code&gt; for old CPUs without XSAVE (it only knows the SSE registers), and &lt;code&gt;_xsave&lt;/code&gt; or &lt;code&gt;_xsavec&lt;/code&gt; for everything newer. These two save the same state, &lt;code&gt;xsavec&lt;/code&gt; just does it in a compacted form, which is faster, so the loader picks it whenever the CPU supports it. In other words, the whole thing is one big &amp;quot;I'm about to break everything, so let me put it all back exactly as it was&amp;quot; wrapper around the resolve.&lt;/p&gt;&#10;&lt;h3 id="the-resolving-logic-itself"&gt;The resolving logic itself&lt;/h3&gt;&#10;&lt;p&gt;With the registers saved, the trampoline calls &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L41" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_fixup(struct link_map *l, reloc_arg)&lt;/code&gt;&lt;/a&gt;, where &lt;code&gt;l&lt;/code&gt; is the &lt;code&gt;link_map&lt;/code&gt; that &lt;code&gt;PLT0&lt;/code&gt; pushed from &lt;code&gt;GOT[1]&lt;/code&gt;, and &lt;code&gt;reloc_arg&lt;/code&gt; is the index the &lt;code&gt;puts&lt;/code&gt; stub pushed (&lt;code&gt;0&lt;/code&gt;). Its whole job is to turn this number into the real address of &lt;code&gt;puts&lt;/code&gt; and save it in the proper GOT slot, and every step is (mostly) a table lookup starting from &lt;code&gt;l&lt;/code&gt;. I'd rather describe the logic than paste the raw code, because it will be a bit more understandable that way.&lt;/p&gt;&#10;&lt;p&gt;First, &lt;code&gt;_dl_fixup&lt;/code&gt; needs to find the relocation to work with. &lt;code&gt;reloc_arg&lt;/code&gt; isn't a pointer to something useful, it's just an index. &lt;code&gt;_dl_fixup&lt;/code&gt; takes the address of &lt;code&gt;.rela.plt&lt;/code&gt; from the &lt;code&gt;DT_JMPREL&lt;/code&gt; entry in the &lt;code&gt;.dynamic&lt;/code&gt; section (glibc keeps a pointer to every such entry in &lt;code&gt;l-&amp;gt;l_info[]&lt;/code&gt;), and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L53-L55" target="_blank" rel="noopener"&gt;uses the index to pick one &lt;code&gt;Elf64_Rela&lt;/code&gt;&lt;/a&gt; from it. This single entry has everything it needs: its &lt;code&gt;r_offset&lt;/code&gt; says where the &lt;code&gt;puts&lt;/code&gt; slot is (&lt;code&gt;0x4000&lt;/code&gt; here, relative to the load address), and its &lt;code&gt;r_info&lt;/code&gt; packs together the relocation type (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L63" target="_blank" rel="noopener"&gt;&lt;code&gt;R_X86_64_JUMP_SLOT&lt;/code&gt;, checked by an &lt;code&gt;assert&lt;/code&gt;&lt;/a&gt;) and the symbol index.&lt;/p&gt;&#10;&lt;p&gt;Then it needs to link this relocation to a symbol. The symbol index from &lt;code&gt;r_info&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L56" target="_blank" rel="noopener"&gt;picks one &lt;code&gt;Elf64_Sym&lt;/code&gt;&lt;/a&gt; in the &lt;code&gt;.dynsym&lt;/code&gt; of this object. This symbol's &lt;code&gt;st_name&lt;/code&gt; is a byte offset into &lt;code&gt;.dynstr&lt;/code&gt;, and the string there is the name the caller wants: &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt;. So the whole chain is index -&amp;gt; &lt;code&gt;Elf64_Rela&lt;/code&gt; -&amp;gt; symbol index -&amp;gt; &lt;code&gt;Elf64_Sym&lt;/code&gt; -&amp;gt; &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt;, and every step is a simple read from a table the &lt;code&gt;link_map&lt;/code&gt; already points to.&lt;/p&gt;&#10;&lt;p&gt;Next, it needs to find where &lt;code&gt;puts&lt;/code&gt; actually lives. Someone's name still doesn't tell you where exactly to find them: &lt;code&gt;puts&lt;/code&gt; is defined in some other object, and which one depends on the search scope. The scope is an ordered list of objects that the loader builds at startup. &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L156-L157" target="_blank" rel="noopener"&gt;The executable goes first&lt;/a&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L159-L161" target="_blank" rel="noopener"&gt;right after it come the &lt;code&gt;LD_PRELOAD&lt;/code&gt; libraries&lt;/a&gt;, more on them below), then every library reachable through &lt;code&gt;DT_NEEDED&lt;/code&gt;, in breadth-first order (the direct dependencies of an object come before their own dependencies), and each object appears there only once. That's &lt;code&gt;l-&amp;gt;l_searchlist&lt;/code&gt;, and the loader builds it in &lt;code&gt;_dl_map_object_deps&lt;/code&gt;, where &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-deps.c#L177" target="_blank" rel="noopener"&gt;the comment&lt;/a&gt; calls it a &amp;quot;breadth-first search of the dependency tree.&amp;quot;&lt;/p&gt;&#10;&lt;p&gt;Then &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-runtime.c#L95" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt;&lt;/a&gt; passes the name to &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L329" target="_blank" rel="noopener"&gt;&lt;code&gt;do_lookup_x&lt;/code&gt;&lt;/a&gt;, which walks this list from the start and looks for an entry named &lt;code&gt;&amp;quot;puts&amp;quot;&lt;/code&gt; in the symbol table of each object. This entry has to define &lt;code&gt;puts&lt;/code&gt;, not just reference it, which turns out to be an important difference. How exactly it searches inside one object is the &lt;code&gt;.gnu.hash&lt;/code&gt; lookup, which gets &lt;a href="#gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;its own section&lt;/a&gt; further down.&lt;/p&gt;&#10;&lt;p&gt;A &lt;code&gt;.dynsym&lt;/code&gt; can have two kinds of entries for the same name. A definition has a real section index in &lt;code&gt;st_shndx&lt;/code&gt;, which means the symbol actually has an address in this object. An undefined reference (&lt;code&gt;st_shndx == SHN_UNDEF&lt;/code&gt;, value &lt;code&gt;0&lt;/code&gt;) only says that the object uses this name somewhere and needs someone else to provide it. &lt;code&gt;do_lookup_x&lt;/code&gt; wants the first kind and skips the second: its helper &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L73-L77" target="_blank" rel="noopener"&gt;&lt;code&gt;check_match&lt;/code&gt;&lt;/a&gt; drops entries without a value, and for a PLT lookup like this one (there are others), any &lt;code&gt;SHN_UNDEF&lt;/code&gt; entry too. And this isn't just another &lt;code&gt;if&lt;/code&gt; in the code: the demo executable's own &lt;code&gt;.dynsym&lt;/code&gt; has a &lt;code&gt;puts&lt;/code&gt; entry, but only as an undefined reference (it calls &lt;code&gt;puts&lt;/code&gt; but doesn't define it), while the real definition lives in &lt;code&gt;libc.so.6&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf --dyn-syms demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (3)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf --dyn-syms /lib/x86_64-linux-gnu/libc.so.6 &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39; puts@@&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 236: 0000000000080300 514 FUNC WEAK DEFAULT 15 puts@@GLIBC_2.2.5&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;where &lt;code&gt;UND&lt;/code&gt; in the first line is the undefined reference (no address), and &lt;code&gt;15&lt;/code&gt; in the second one is the index of the section where &lt;code&gt;puts&lt;/code&gt; is defined, which is &lt;code&gt;.text&lt;/code&gt; in libc.&lt;/p&gt;&#10;&lt;p&gt;So the scan skips the executable (its &lt;code&gt;puts&lt;/code&gt; is just a reference) and goes on until it finds the real definition in &lt;code&gt;libc.so.6&lt;/code&gt;. The first object in the list that has a definition wins: the scan stops there and returns this &lt;code&gt;Elf64_Sym&lt;/code&gt; together with the &lt;code&gt;link_map&lt;/code&gt; of the object it belongs to.&lt;/p&gt;&#10;&lt;p&gt;By the way, the &lt;code&gt;puts&lt;/code&gt; in libc is &lt;code&gt;WEAK&lt;/code&gt; in the listing above (libc defines it as a &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/libio/ioputs.c#L48" target="_blank" rel="noopener"&gt;weak alias&lt;/a&gt; of &lt;code&gt;_IO_puts&lt;/code&gt;), and it still wins. The static linker cares about weak vs global, but the dynamic one doesn't: in &lt;code&gt;do_lookup_x&lt;/code&gt;, a weak definition &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L472-L483" target="_blank" rel="noopener"&gt;falls through&lt;/a&gt; to the same code as a global one, so the first definition in the list wins anyway. The old &amp;quot;keep looking for a global one&amp;quot; behavior comes back only if you set &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L2662-L2663" target="_blank" rel="noopener"&gt;&lt;code&gt;LD_DYNAMIC_WEAK&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;This &amp;quot;first match wins&amp;quot; rule is what the idea of symbol interposition is based on. The executable is at the beginning of the list, so a function it defines hides the libc function with the same name. And the &lt;code&gt;LD_PRELOAD&lt;/code&gt; libraries come right after the executable, before the usual dependencies, so they can do the same.&lt;/p&gt;&#10;&lt;p&gt;Time for a break from the nerdy text, so let's do a small experiment and see what this order changes. Here's a tiny interposer that defines its own &lt;code&gt;puts&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// preload_puts.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Our own puts(): LD_PRELOAD puts this library ahead of libc, so this one&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; wins. Only write(2) inside, because gcc turns printf(&amp;#34;%s\n&amp;#34;, s) into a&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; puts() call, and we&amp;#39;d end up in a cute school-level recursion bug. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;[preload puts] &amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Build it as a shared library, then run the program with it preloaded. &lt;code&gt;LD_DEBUG=scopes&lt;/code&gt; prints the search scope that the loader built, and you can see that every object uses the same global list, &lt;code&gt;scope 0&lt;/code&gt; (the &lt;code&gt;185:&lt;/code&gt; at the start of each line is just the PID that ld.so adds):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -shared -fPIC -o preload_puts.so preload_puts.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;scopes &lt;span class="nv"&gt;LD_PRELOAD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./preload_puts.so ./demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;Initial object scopes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=./demo_partial [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=linux-vdso.so.1 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 1: linux-vdso.so.1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=./preload_puts.so [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=/lib/x86_64-linux-gnu/libc.so.6 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; scope 0: ./demo_partial ./preload_puts.so /lib/x86_64-linux-gnu/libc.so.6 /lib64/ld-linux-x86-64.so.2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9;object=/lib64/ld-linux-x86-64.so.2 [0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 62:&#9; no scope&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_PRELOAD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./preload_puts.so ./demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[preload puts] first call&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[preload puts] second call&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Every object has the same global &lt;code&gt;scope 0&lt;/code&gt;, and in it &lt;code&gt;./preload_puts.so&lt;/code&gt; is right after &lt;code&gt;./demo_partial&lt;/code&gt; and before &lt;code&gt;libc.so.6&lt;/code&gt;. So a lookup of &lt;code&gt;puts&lt;/code&gt; finds the definition in the preloaded library first and stops there. That's how a preloaded &lt;code&gt;malloc&lt;/code&gt; (tcmalloc/jemalloc and friends) replaces the libc one for the whole process. &lt;code&gt;ld-linux&lt;/code&gt; itself shows &amp;quot;no scope&amp;quot; because its own &lt;code&gt;link_map&lt;/code&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-open.c#L936-L951" target="_blank" rel="noopener"&gt;never gets a scope list&lt;/a&gt; (remember, it relocates itself in its own special way), but it's still the last object in everyone's &lt;code&gt;scope 0&lt;/code&gt;. One more thing comes from the relocation itself: the reference is &lt;code&gt;puts@GLIBC_2.2.5&lt;/code&gt;, so a candidate has to match the symbol version too, not just the name. The loader won't bind to a &lt;code&gt;puts&lt;/code&gt; with a different version tag, but it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L94-L108" target="_blank" rel="noopener"&gt;accepts a definition without any version&lt;/a&gt;, like ours, otherwise this experiment wouldn't work.&lt;/p&gt;&#10;&lt;p&gt;One more weird thing in this output: &lt;code&gt;linux-vdso.so.1&lt;/code&gt; is the only object with a second scope (&lt;code&gt;scope 1: linux-vdso.so.1&lt;/code&gt;). This quirk comes from how &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-object.c#L155-L157" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_new_object&lt;/code&gt;&lt;/a&gt; gives out scopes. Every object loaded after the main program gets the global list as &lt;code&gt;scope 0&lt;/code&gt;, and it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-object.c#L168" target="_blank" rel="noopener"&gt;gets a second, local scope&lt;/a&gt; only if that one is somehow different from the global list. For a &lt;code&gt;DT_NEEDED&lt;/code&gt; dependency or an &lt;code&gt;LD_PRELOAD&lt;/code&gt; library, the chain of loaders goes up to the executable, whose search list is the global list, so both scopes are the same and there's no second one.&lt;/p&gt;&#10;&lt;p&gt;But the vDSO is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/setup-vdso.h#L32" target="_blank" rel="noopener"&gt;created without a loader&lt;/a&gt;, so it becomes its own loader (hi to the &amp;quot;relocating relocator&amp;quot; from above), and its local scope is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/setup-vdso.h#L70-L75" target="_blank" rel="noopener"&gt;a list with just itself in it&lt;/a&gt;, which is different from the global list. Hence the extra &lt;code&gt;scope 1&lt;/code&gt;. The kernel maps the vDSO into the process by itself, so it's all alone: it isn't in anyone's &lt;code&gt;DT_NEEDED&lt;/code&gt;, and it isn't in the global list either. This cozy private scope is how glibc looks up symbols inside the vDSO (&lt;code&gt;__vdso_clock_gettime&lt;/code&gt; and friends), and that's what the comment there says it's for.&lt;/p&gt;&#10;&lt;p&gt;Now it can compute the address, and that's just arithmetic. The &lt;code&gt;st_value&lt;/code&gt; of the found symbol is the offset of &lt;code&gt;puts&lt;/code&gt; inside libc, and the &lt;code&gt;link_map&lt;/code&gt; of libc keeps its load address in &lt;code&gt;l_addr&lt;/code&gt;. &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/generic/ldsodefs.h#L94-L101" target="_blank" rel="noopener"&gt;&lt;code&gt;SYMBOL_ADDRESS&lt;/code&gt; adds them together&lt;/a&gt;, and the result is the real address of &lt;code&gt;puts&lt;/code&gt;: the same &lt;code&gt;0x7ffff7e46300&lt;/code&gt; that the &lt;code&gt;got&lt;/code&gt; command printed above (&lt;code&gt;0x7ffff7dc6000 + 0x80300&lt;/code&gt;, if you're so curious).&lt;/p&gt;&#10;&lt;p&gt;And finally, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L218" target="_blank" rel="noopener"&gt;&lt;code&gt;elf_machine_fixup_plt&lt;/code&gt;&lt;/a&gt; writes this address into the slot from &lt;code&gt;r_offset&lt;/code&gt;. Its whole body is a modest &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L223" target="_blank" rel="noopener"&gt;&lt;code&gt;return *reloc_addr = value;&lt;/code&gt;&lt;/a&gt;, so it also returns the address, and &lt;code&gt;_dl_fixup&lt;/code&gt; passes it back to the trampoline. The trampoline restores the registers it saved (so &lt;code&gt;rdi&lt;/code&gt; points to the string again, as if nothing happened) and jumps to that address. And yay, &lt;code&gt;puts&lt;/code&gt; finally runs.&lt;/p&gt;&#10;&lt;p&gt;So the first call pays for the full symbol lookup and, as a side effect, rewrites its own GOT slot. That's the change we saw in pwndbg above, from the lazy stub to &lt;code&gt;0x7ffff7e46300&lt;/code&gt;. Every next &lt;code&gt;call puts@plt&lt;/code&gt; then just does &lt;code&gt;jmp [slot]&lt;/code&gt; straight into libc: one indirection, no resolver, forever. And that's what lazy binding is about: each imported function is resolved once, on its first call, and after that only the first instruction of its PLT stub ever runs. A simple idea that's quite hard to implement properly, as usual.&lt;/p&gt;&#10;&lt;h3 id="and-the-relro-part-of-partial-relro"&gt;And the RELRO part of partial RELRO&lt;/h3&gt;&#10;&lt;p&gt;Now back to the &lt;code&gt;vmmap&lt;/code&gt; above, where &lt;code&gt;GOT[0..2]&lt;/code&gt; ended up on a read-only page and the &lt;code&gt;puts&lt;/code&gt; slot on a writable one. The loader writes &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; once at startup and never touches them again, so they really can be read-only, while lazy binding keeps writing into the &lt;code&gt;puts&lt;/code&gt; slot, which comes right after them.&lt;/p&gt;&#10;&lt;p&gt;And it's not only about the GOT: partial RELRO (&lt;code&gt;-z relro&lt;/code&gt; without &lt;code&gt;-z now&lt;/code&gt;) makes read-only everything the loader is done with after startup, which is &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt;, &lt;code&gt;.dynamic&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt; and the first three entries of &lt;code&gt;.got.plt&lt;/code&gt;. All of them live in the same writable segment (the &lt;code&gt;RW&lt;/code&gt; &lt;code&gt;LOAD&lt;/code&gt; one at &lt;code&gt;0x3dd0&lt;/code&gt;). To tell the loader which exact part of it to lock, the linker adds an additional program header, &lt;code&gt;PT_GNU_RELRO&lt;/code&gt;, which is basically an address range. Here is how to see it with &lt;code&gt;readelf&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -lW demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; LOAD 0x002dd0 0x0000000000003dd0 0x0000000000003dd0 0x000248 0x000250 RW 0x1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; GNU_RELRO 0x002dd0 0x0000000000003dd0 0x0000000000003dd0 0x000230 0x000230 R 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Section to Segment mapping:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Segment Sections...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 05 .init_array .fini_array .dynamic .got .got.plt .data .bss&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 13 .init_array .fini_array .dynamic .got&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;GNU_RELRO&lt;/code&gt; (number &lt;code&gt;13&lt;/code&gt; in the list) covers the range from &lt;code&gt;0x3dd0&lt;/code&gt; to &lt;code&gt;0x3dd0 + 0x230 = 0x4000&lt;/code&gt;. That's the whole &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt;, &lt;code&gt;.dynamic&lt;/code&gt; and &lt;code&gt;.got&lt;/code&gt;, plus the first 24 bytes of &lt;code&gt;.got.plt&lt;/code&gt; (&lt;code&gt;0x3fe8&lt;/code&gt; to &lt;code&gt;0x4000&lt;/code&gt;, our beloved &lt;code&gt;GOT[0..2]&lt;/code&gt;). &lt;code&gt;readelf&lt;/code&gt; doesn't list &lt;code&gt;.got.plt&lt;/code&gt; in segment &lt;code&gt;13&lt;/code&gt; because it shows only the sections that fit into a segment completely. The rest (the &lt;code&gt;puts&lt;/code&gt; slot at &lt;code&gt;0x4000&lt;/code&gt;, &lt;code&gt;.data&lt;/code&gt; and &lt;code&gt;.bss&lt;/code&gt;) is outside the range, so it stays writable, like the whole &lt;code&gt;LOAD&lt;/code&gt; segment &lt;code&gt;05&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;But how does this range suddenly become read-only, if the kernel maps the whole segment as &lt;code&gt;RW&lt;/code&gt;? The loader does it itself. The segment has to be writable at first, because the loader writes quite a lot into it at startup: the &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations go into &lt;code&gt;.init_array&lt;/code&gt; and &lt;code&gt;.fini_array&lt;/code&gt;, and the &lt;code&gt;GLOB_DAT&lt;/code&gt; ones into &lt;code&gt;.got&lt;/code&gt;. Only when all relocations are done, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L346-L347" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_object&lt;/code&gt; calls &lt;code&gt;_dl_protect_relro&lt;/code&gt;&lt;/a&gt;, which &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L356-L364" target="_blank" rel="noopener"&gt;rounds the range down to page boundaries and &lt;code&gt;mprotect&lt;/code&gt;s it&lt;/a&gt; as &lt;code&gt;PROT_READ&lt;/code&gt;. Here it's one page, from base &lt;code&gt;+0x3000&lt;/code&gt; to base &lt;code&gt;+0x4000&lt;/code&gt;. So &lt;code&gt;RW&lt;/code&gt; in the program header is only the initial permission, and the loader lowers it to &lt;code&gt;r--&lt;/code&gt; as soon as nothing needs to write there anymore. It's the same trick as with &lt;code&gt;DT_TEXTREL&lt;/code&gt; earlier, but in the opposite direction: there the loader adds write permission for a moment to patch the code, and here it removes it for good after the relocations.&lt;/p&gt;&#10;&lt;p&gt;Both ends of the range are rounded down, and that's why the linker ends the RELRO range at &lt;code&gt;0x4000&lt;/code&gt; and puts the &lt;code&gt;puts&lt;/code&gt; (writes the &lt;code&gt;write&lt;/code&gt;?) slot right after it. If the range ended in the middle of a page, this whole page would stay writable, because &lt;code&gt;mprotect&lt;/code&gt; can't protect half a page. So the default linker script does it on purpose: it ends the RELRO region 24 bytes into &lt;code&gt;.got.plt&lt;/code&gt; (three 8-byte slots) and shifts the layout so that this point lands exactly on a page boundary. If you're brave enough, look for &lt;code&gt;DATA_SEGMENT_RELRO_END&lt;/code&gt; in &lt;code&gt;ld --verbose&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;And it's really enforced, not just declared. Here's a small program that tries to write into its own &lt;code&gt;.dynamic&lt;/code&gt;, which it finds through the &lt;code&gt;_DYNAMIC&lt;/code&gt; symbol (the linker adds it to every dynamically linked binary):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// relro_write.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;_DYNAMIC&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// the start of .dynamic, an array of ElfW(Dyn)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;_DYNAMIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;.dynamic (_DYNAMIC) @ %p, first word = 0x%lx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;attempting to overwrite .dynamic[0] ...&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;fflush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x4141414141414141UL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// with RELRO this page is read-only, so SIGSEGV here&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;WRITE SUCCEEDED -&amp;gt; .dynamic is writable (no RELRO)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With partial RELRO the write dies with &lt;code&gt;SIGSEGV&lt;/code&gt;, and with RELRO turned off the same write goes through:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector relro_write.c -Wl,-z,relro,-z,lazy -o relro_partial &lt;span class="c1"&gt;# partial RELRO&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector relro_write.c -Wl,-z,norelro -o relro_norelro &lt;span class="c1"&gt;# RELRO off&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./relro_norelro&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.dynamic (_DYNAMIC) @ 0x556b71fdc1a0, first word = 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;attempting to overwrite .dynamic[0] ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;WRITE SUCCEEDED -&amp;gt; .dynamic is writable (no RELRO)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./relro_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.dynamic (_DYNAMIC) @ 0x557e76949de0, first word = 0x1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;attempting to overwrite .dynamic[0] ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Segmentation fault (core dumped)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;139&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The exit code &lt;code&gt;139&lt;/code&gt; is 128 + 11, where 11 is the number of &lt;code&gt;SIGSEGV&lt;/code&gt;. The program writes to &lt;code&gt;.dynamic&lt;/code&gt; here, but &lt;code&gt;.init_array&lt;/code&gt;, &lt;code&gt;.fini_array&lt;/code&gt; and &lt;code&gt;.got&lt;/code&gt; are in the same &lt;code&gt;PT_GNU_RELRO&lt;/code&gt; range, and the loader makes the whole range read-only with one &lt;code&gt;mprotect&lt;/code&gt;, so a write to any of them would die the same way.&lt;/p&gt;&#10;&lt;h2 id="full-relro-resolve-everything-up-front-then-freeze-it"&gt;Full RELRO: resolve everything up front, then freeze it&lt;/h2&gt;&#10;&lt;p&gt;So partial RELRO protects almost everything except the function slots: the &lt;code&gt;puts&lt;/code&gt; slot in our &lt;code&gt;demo_partial&lt;/code&gt; stays writable for the whole life of the process, because lazy binding has to write there on the first call. In an average binary there are many more slots, but the idea is the same. Full RELRO (&lt;code&gt;-z relro -z now&lt;/code&gt;) fixes it in the most direct way: it gives up lazy binding and resolves every function at startup. After that, nothing needs to write into the GOT anymore, so the function slots can be read-only too. Here is what changes for that, in the binary and in the loader:&lt;/p&gt;&#10;&lt;p&gt;The first change is in &lt;code&gt;.dynamic&lt;/code&gt;: &lt;code&gt;readelf -d&lt;/code&gt; shows the &lt;code&gt;BIND_NOW&lt;/code&gt; bit in &lt;code&gt;FLAGS&lt;/code&gt; and the &lt;code&gt;NOW&lt;/code&gt; bit in &lt;code&gt;FLAGS_1&lt;/code&gt;, and the partial binary has neither. There's no separate &lt;code&gt;DT_BIND_NOW&lt;/code&gt; entry, just these two bits, but glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L158-L159" target="_blank" rel="noopener"&gt;treats&lt;/a&gt; &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L180-L181" target="_blank" rel="noopener"&gt;both of them&lt;/a&gt; as &lt;code&gt;DT_BIND_NOW&lt;/code&gt;, which is the name I'll use below:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_full demo.c -Wl,-z,relro,-z,now &lt;span class="c1"&gt;# full RELRO: add -z now&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_full &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) BIND_NOW&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: NOW PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The second change is in the loader itself. When it sees &lt;code&gt;DT_BIND_NOW&lt;/code&gt;, &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L246-L248" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_object&lt;/code&gt; sets &lt;code&gt;lazy = 0&lt;/code&gt;&lt;/a&gt;, and this one variable changes two things. First, &lt;code&gt;elf_machine_runtime_setup&lt;/code&gt; doesn't fill &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; anymore, because of the same &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L78" target="_blank" rel="noopener"&gt;&lt;code&gt;lazy&lt;/code&gt; check&lt;/a&gt; we saw above: nobody will ever call the resolver in this case, so it doesn't need a &lt;code&gt;link_map&lt;/code&gt; or a trampoline address. Second, the relocation loop skips the &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/do-rel.h#L80" target="_blank" rel="noopener"&gt;cheap lazy path&lt;/a&gt; and processes every &lt;code&gt;JUMP_SLOT&lt;/code&gt; right away with &lt;code&gt;elf_machine_rela&lt;/code&gt;, the same function that handles &lt;code&gt;GLOB_DAT&lt;/code&gt;. It looks up the symbol with the same &lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt; that &lt;code&gt;_dl_fixup&lt;/code&gt; uses, and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-machine.h#L314-L318" target="_blank" rel="noopener"&gt;writes the address straight into the slot&lt;/a&gt;, just like on the &lt;code&gt;GLOB_DAT&lt;/code&gt; path. So by the time &lt;code&gt;main&lt;/code&gt; runs, every function slot already holds its final libc address. One more pwndbg session to confirm all this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# full RELRO: stop at main, before any libc call, and read the GOT&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# plain got hides read-only entries, and here all of them are read-only&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Filtering out read-only entries (display them with -r or --show-readonly)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_full:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Full RELRO | Found 0 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# so show them all with -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_full:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: Full RELRO | Found 6 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fd0] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fd8] __libc_start_main@GLIBC_2.34 -&amp;gt; 0x7ffff7defce0 (__libc_start_main) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fe0] _ITM_deregisterTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557fe8] __gmon_start__ -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557ff0] _ITM_registerTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557ff8] __cxa_finalize@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e07b90 (__cxa_finalize) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The &lt;code&gt;puts&lt;/code&gt; slot already holds &lt;code&gt;0x7ffff7e46300&lt;/code&gt; (its real address in libc) right at the &lt;code&gt;main&lt;/code&gt; breakpoint, before any code there runs. There's no lazy stub and no slow first call, because the loader resolved the whole &lt;code&gt;.rela.plt&lt;/code&gt; in advance. And pwndbg's &lt;code&gt;got&lt;/code&gt; shows zero entries: it hides read-only slots by default, and with full RELRO every slot in the GOT is read-only. That's why you need &lt;code&gt;got -r&lt;/code&gt; here. On top of that, pwndbg also shows the RELRO level in its protection line, in case you somehow missed it: &lt;code&gt;GOT protection: Full RELRO&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The third change is in the layout: &lt;code&gt;.got.plt&lt;/code&gt; disappears. With &lt;code&gt;-z now&lt;/code&gt;, ld uses a different default linker script (compare &lt;code&gt;ld --verbose&lt;/code&gt; with &lt;code&gt;ld -z now --verbose&lt;/code&gt;), where the output &lt;code&gt;.got&lt;/code&gt; takes the &lt;code&gt;.got.plt&lt;/code&gt; input first and then the usual &lt;code&gt;.got&lt;/code&gt; entries. So all the slots end up in one &lt;code&gt;.got&lt;/code&gt;, and the whole section is inside the RELRO range:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -SW demo_full &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [13] .plt.got PROGBITS 0000000000001040 001040 000008 08 AX 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [23] .got PROGBITS 0000000000003fb8 002fb8 000048 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -SW demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [13] .plt.got PROGBITS 0000000000001040 001040 000008 08 AX 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [23] .got PROGBITS 0000000000003fc0 002fc0 000028 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [24] .got.plt PROGBITS 0000000000003fe8 002fe8 000020 08 WA 0 0 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;.plt.got&lt;/code&gt; (&lt;code&gt;[13]&lt;/code&gt;) shows up only because &lt;code&gt;grep&lt;/code&gt; matches it too: it's the same in both binaries, has nothing to do with RELRO, and gets its own section further down. The real difference is &lt;code&gt;.got.plt&lt;/code&gt;: &lt;code&gt;demo_partial&lt;/code&gt; has it as a separate section at &lt;code&gt;0x3fe8&lt;/code&gt;, and &lt;code&gt;demo_full&lt;/code&gt; doesn't have it at all. The sizes add up too: the &lt;code&gt;.got&lt;/code&gt; of &lt;code&gt;demo_full&lt;/code&gt; is &lt;code&gt;0x48&lt;/code&gt; bytes, which is &lt;code&gt;0x28 + 0x20&lt;/code&gt; from &lt;code&gt;demo_partial&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The reserved &lt;code&gt;GOT[0..2]&lt;/code&gt; are still there, at the start of the merged &lt;code&gt;.got&lt;/code&gt; (&lt;code&gt;0x3fb8&lt;/code&gt; to &lt;code&gt;0x3fd0&lt;/code&gt;). The linker still emits &lt;code&gt;PLT0&lt;/code&gt; and the &lt;code&gt;puts&lt;/code&gt; stub that refer to them, but in a normal run it's all dead code: the &lt;code&gt;puts&lt;/code&gt; slot already points into libc, so the stub never gets past its first &lt;code&gt;jmp&lt;/code&gt;. All this looks like someone forgot an &lt;code&gt;if&lt;/code&gt; somewhere, but no: glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L243-L248" target="_blank" rel="noopener"&gt;ignores &lt;code&gt;DT_BIND_NOW&lt;/code&gt;&lt;/a&gt; when you &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L2228" target="_blank" rel="noopener"&gt;profile the program with &lt;code&gt;LD_PROFILE&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc.c#L228-L231" target="_blank" rel="noopener"&gt;use an audit module with PLT hooks&lt;/a&gt;, and then this &amp;quot;dead&amp;quot; lazy path comes back to life. Anyway, &lt;code&gt;readelf -r&lt;/code&gt; lists every fixup the loader will apply, and in &lt;code&gt;.got&lt;/code&gt; they all start at &lt;code&gt;0x3fd0&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_full&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.dyn&amp;#39; at offset 0x550 contains 8 entries:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003db8 000000000008 R_X86_64_RELATIVE 1130&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003dc0 000000000008 R_X86_64_RELATIVE 10f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004008 000000000008 R_X86_64_RELATIVE 4008&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fd8 000100000006 R_X86_64_GLOB_DAT 0000000000000000 __libc_start_main@GLIBC_2.34 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe0 000200000006 R_X86_64_GLOB_DAT 0000000000000000 _ITM_deregisterTM[...] &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe8 000400000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003ff0 000500000006 R_X86_64_GLOB_DAT 0000000000000000 _ITM_registerTMCl[...] &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003ff8 000600000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Relocation section &amp;#39;.rela.plt&amp;#39; at offset 0x610 contains 1 entry:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Offset Info Type Sym. Value Sym. Name &amp;#43; Addend&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fd0 000300000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;If you run the same &lt;code&gt;readelf -r&lt;/code&gt; on &lt;code&gt;demo_partial&lt;/code&gt;, you'll get the same set of relocations: three &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt;, five &lt;code&gt;R_X86_64_GLOB_DAT&lt;/code&gt; and one &lt;code&gt;R_X86_64_JUMP_SLOT&lt;/code&gt; for &lt;code&gt;puts&lt;/code&gt;. So the relocation table doesn't say anything about RELRO. The only difference is where the &lt;code&gt;JUMP_SLOT&lt;/code&gt; points: &lt;code&gt;0x3fd0&lt;/code&gt; in &lt;code&gt;demo_full&lt;/code&gt;, inside &lt;code&gt;.got&lt;/code&gt; and below the page boundary, and &lt;code&gt;0x4000&lt;/code&gt; in &lt;code&gt;demo_partial&lt;/code&gt;, the first byte of the next, writable page.&lt;/p&gt;&#10;&lt;p&gt;The reserved &lt;code&gt;GOT[0..2]&lt;/code&gt; at &lt;code&gt;0x3fb8&lt;/code&gt;, &lt;code&gt;0x3fc0&lt;/code&gt; and &lt;code&gt;0x3fc8&lt;/code&gt; aren't in either list. The lowest &lt;code&gt;.got&lt;/code&gt; offset there is &lt;code&gt;0x3fd0&lt;/code&gt; (the &lt;code&gt;puts&lt;/code&gt; slot), the &lt;code&gt;GLOB_DAT&lt;/code&gt;s go from &lt;code&gt;0x3fd8&lt;/code&gt; up, and the three &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations (&lt;code&gt;0x3db8&lt;/code&gt;, &lt;code&gt;0x3dc0&lt;/code&gt; and &lt;code&gt;0x4008&lt;/code&gt;) aren't in &lt;code&gt;.got&lt;/code&gt; at all. So no relocation touches &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt;, the loader skips them too (as we saw above), and at &lt;code&gt;main&lt;/code&gt; they are still zero:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; x/3gx $rebase(0x3fb8) # GOT[0..2] at the start of .got (0x3fb8 in readelf -S above)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x555555557fb8:&#9;0x0000000000003dc8&#9;0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x555555557fc8:&#9;0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;code&gt;GOT[0]&lt;/code&gt; still has the raw link-time &lt;code&gt;0x3dc8&lt;/code&gt; (the address of &lt;code&gt;_DYNAMIC&lt;/code&gt;, not even rebased), and &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; are zero, while the function slots right after them are already resolved (see the &lt;code&gt;got -r&lt;/code&gt; output above). So in the end, the reserved slots are still there, only &lt;code&gt;PLT0&lt;/code&gt; refers to them, and in a normal run nobody uses them.&lt;/p&gt;&#10;&lt;p&gt;So the call logic doesn't change at all: &lt;code&gt;main&lt;/code&gt; still does &lt;code&gt;call puts@plt&lt;/code&gt;, and the stub still jumps through the GOT slot. What changes is when the slot gets its address, and that it's secured so nobody can write there after startup. That's the trade full RELRO makes: the program pays for resolving everything at startup, and in exchange an attacker loses a writable table of function pointers, which is a very obvious thing to overwrite. More on that in &lt;a href="#the-got-overwrite-primitive-and-what-full-relro-does-to-it"&gt;the last section&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="taking-the-address-eager-functions-and-pltgot"&gt;Taking the address: eager functions and &lt;code&gt;.plt.got&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;So far I experimented with a function (&lt;code&gt;puts&lt;/code&gt;) that was only called, and that's why it could be bound lazily: the call goes through the PLT stub, and the first call fills its &lt;code&gt;.got.plt&lt;/code&gt; slot. But what if the program takes the address of &lt;code&gt;puts&lt;/code&gt;, i.e. stores &lt;code&gt;&amp;amp;puts&lt;/code&gt; in a function pointer, compares it with another one or passes it somewhere as a callback? In this case, this address is just a value read as data, so it has to be the real address of &lt;code&gt;puts&lt;/code&gt; from the very start, without running any lazy machinery. In a PIE, the address of the PLT stub can't play this role: &lt;code&gt;&amp;amp;puts&lt;/code&gt; taken in the executable would then differ from &lt;code&gt;&amp;amp;puts&lt;/code&gt; taken inside a shared library, and comparing function pointers between objects would silently break. So the linker has to bind such a function eagerly: the loader fills a single &lt;code&gt;.got&lt;/code&gt; slot at startup through a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation, and the code reads &lt;code&gt;&amp;amp;puts&lt;/code&gt; from this slot.&lt;/p&gt;&#10;&lt;p&gt;As a side note, a non-PIE executable (compiled with &lt;code&gt;-fno-pie&lt;/code&gt;) does the opposite trick. Its PLT stub has a fixed address, so the linker makes this stub the official (but still fake, because it's not in libc) address of &lt;code&gt;puts&lt;/code&gt; for the whole process. And the loader gives the same address to every shared library that asks for &lt;code&gt;&amp;amp;puts&lt;/code&gt;. But every binary in this post is a PIE, so no demo here: just believe me, or better, check it yourself.&lt;/p&gt;&#10;&lt;p&gt;The funny thing is that once the code takes the address of &lt;code&gt;puts&lt;/code&gt; through the GOT, the call starts to use the same eager slot too. The compiler loads &lt;code&gt;&amp;amp;puts&lt;/code&gt; from a GOT slot (&lt;code&gt;puts@GOTPCREL&lt;/code&gt;), the linker emits a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation for this slot, and then redirects the &lt;code&gt;call&lt;/code&gt; through the same slot with a tiny &lt;code&gt;.plt.got&lt;/code&gt; stub instead of a standard lazy &lt;code&gt;.plt&lt;/code&gt; entry. So such a function requires no &lt;code&gt;JUMP_SLOT&lt;/code&gt; at all. Here's &lt;code&gt;addr.c&lt;/code&gt;, which takes &lt;code&gt;&amp;amp;puts&lt;/code&gt; in two ways and also calls it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cat addr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#include &amp;lt;stdio.h&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;void *p = (void *)&amp;amp;puts; /* address stored in .data -&amp;gt; R_X86_64_64 */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;int main(void){&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; void *volatile q = (void *)&amp;amp;puts; /* address loaded via the GOT -&amp;gt; GLOB_DAT */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; puts(&amp;#34;hi&amp;#34;); /* called too */&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return (p != 0) &amp;amp;&amp;amp; (q != 0);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o addr addr.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r addr &lt;span class="p"&gt;|&lt;/span&gt; grep puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fc8 000600000006 R_X86_64_GLOB_DAT 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000004010 000600000001 R_X86_64_64 0000000000000000 puts@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S addr &lt;span class="p"&gt;|&lt;/span&gt; grep -c rela.plt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So there are two eager relocations for &lt;code&gt;puts&lt;/code&gt;. The &lt;code&gt;GLOB_DAT&lt;/code&gt; at &lt;code&gt;0x3fc8&lt;/code&gt; is the GOT slot with the one real address of &lt;code&gt;puts&lt;/code&gt; (this is what &lt;code&gt;q = &amp;amp;puts&lt;/code&gt; reads), and the &lt;code&gt;R_X86_64_64&lt;/code&gt; at &lt;code&gt;0x4010&lt;/code&gt; writes the same address straight into &lt;code&gt;p&lt;/code&gt; in &lt;code&gt;.data&lt;/code&gt;. The loader applies both of them at startup, and &lt;code&gt;.rela.plt&lt;/code&gt; doesn't even exist (that's the &lt;code&gt;0&lt;/code&gt; above), so &lt;code&gt;puts&lt;/code&gt; has no lazy &lt;code&gt;JUMP_SLOT&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The call is eager too. &lt;code&gt;main&lt;/code&gt; does an ordinary-looking &lt;code&gt;call 1030 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;, but &lt;code&gt;0x1030&lt;/code&gt; isn't in &lt;code&gt;.plt&lt;/code&gt;, it's a &lt;code&gt;.plt.got&lt;/code&gt; stub that jumps straight through the same &lt;code&gt;GLOB_DAT&lt;/code&gt; slot:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel addr&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1131:&#9;48 8b 05 90 2e 00 00 &#9;mov rax,QWORD PTR [rip&amp;#43;0x2e90] # 3fc8 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1146:&#9;e8 e5 fe ff ff &#9;call 1030 &amp;lt;puts@plt&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt.got addr&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001030 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;ff 25 92 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2f92] # 3fc8 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1036:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So both &lt;code&gt;mov&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; end up reading &lt;code&gt;0x3fc8&lt;/code&gt;. Compare it with &lt;code&gt;demo_partial&lt;/code&gt;, where &lt;code&gt;puts&lt;/code&gt; is only called: there it has a lazy &lt;code&gt;JUMP_SLOT&lt;/code&gt; in &lt;code&gt;.rela.plt&lt;/code&gt; and an ordinary &lt;code&gt;.plt&lt;/code&gt; stub, because nobody takes its address.&lt;/p&gt;&#10;&lt;p&gt;But you don't even need to bother writing &lt;code&gt;&amp;amp;&lt;/code&gt; in your own code if you want to get a &lt;code&gt;.plt.got&lt;/code&gt; stub: every ordinary binary has one, for the &lt;code&gt;__cxa_finalize&lt;/code&gt; function. Here it is in &lt;code&gt;demo_partial&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt.got demo_partial&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001040 &amp;lt;__cxa_finalize@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1040:&#9;ff 25 9a 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2f9a] # 3fe0 &amp;lt;__cxa_finalize@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1046:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -r demo_partial &lt;span class="p"&gt;|&lt;/span&gt; grep cxa&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;000000003fe0 000600000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize@GLIBC_2.2.5 &amp;#43; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The reason is the same: the address is taken, just not by you. The startup code that gcc links into every binary declares &lt;code&gt;__cxa_finalize&lt;/code&gt; as a &lt;a href="https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.2.0/libgcc/crtstuff.c#L351-L353" target="_blank" rel="noopener"&gt;weak reference&lt;/a&gt; and &lt;a href="https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.2.0/libgcc/crtstuff.c#L388-L389" target="_blank" rel="noopener"&gt;checks it before the call&lt;/a&gt; with &lt;code&gt;if (__cxa_finalize)&lt;/code&gt;, which reads its address. So the result is the same as in my example: one &lt;code&gt;GLOB_DAT&lt;/code&gt; slot in &lt;code&gt;.got&lt;/code&gt;, filled at startup, and a &lt;code&gt;.plt.got&lt;/code&gt; stub for the call. Here is the rule of thumb if you need one: a &lt;code&gt;.plt.got&lt;/code&gt; entry means a &lt;code&gt;GLOB_DAT&lt;/code&gt; relocation in &lt;code&gt;.rela.dyn&lt;/code&gt;, and a &lt;code&gt;.plt&lt;/code&gt; (or &lt;code&gt;.plt.sec&lt;/code&gt;) entry means a &lt;code&gt;JUMP_SLOT&lt;/code&gt; in &lt;code&gt;.rela.plt&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="pltsec-what-cet-does-to-the-plt"&gt;&lt;code&gt;.plt.sec&lt;/code&gt;: what CET does to the PLT&lt;/h2&gt;&#10;&lt;p&gt;Every binary above was built with &lt;code&gt;-fcf-protection=none&lt;/code&gt;, so they all had the classic layout with a single &lt;code&gt;.plt&lt;/code&gt;. Now it's time to turn CET on and see what changes.&lt;/p&gt;&#10;&lt;p&gt;Intel CET (Control-flow Enforcement Technology) is a hardware protection against control-flow hijacking, like &lt;a href="https://en.wikipedia.org/wiki/Return-oriented_programming" target="_blank" rel="noopener"&gt;ROP&lt;/a&gt; and &lt;a href="https://security.stackexchange.com/questions/201196/concept-of-jump-oriented-programming-jop" target="_blank" rel="noopener"&gt;JOP&lt;/a&gt;. It has two parts: the shadow stack protects return addresses (a second &amp;quot;invisible&amp;quot; copy of them that the CPU checks on every &lt;code&gt;ret&lt;/code&gt;) and has nothing to do with the PLT, and the one that matters here is &lt;a href="https://en.wikipedia.org/wiki/Indirect_branch_tracking" target="_blank" rel="noopener"&gt;IBT&lt;/a&gt; (Indirect Branch Tracking). With IBT, the CPU checks every indirect &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;jmp&lt;/code&gt; and raises a control-protection fault (&lt;code&gt;#CP&lt;/code&gt;) unless the first instruction at the target is &lt;code&gt;endbr64&lt;/code&gt;. This &lt;code&gt;endbr64&lt;/code&gt; (&lt;code&gt;f3 0f 1e fa&lt;/code&gt;) is a 4-byte marker that says &amp;quot;an indirect branch is allowed to land here&amp;quot;. It's encoded as a multi-byte &lt;code&gt;NOP&lt;/code&gt;, so a CPU without CET just skips it, and the same binary runs everywhere. You can turn it on with the &lt;code&gt;-fcf-protection&lt;/code&gt; flag.&lt;/p&gt;&#10;&lt;p&gt;And that's what breaks the classic stub: the first jump through the GOT lands at &lt;code&gt;0x1036&lt;/code&gt;, in the middle of the stub, where there's no &lt;code&gt;endbr64&lt;/code&gt; instruction. So with IBT enabled it would fault and kill the process. To fix this, the linker splits the PLT into two parts. Here's the same &lt;code&gt;demo.c&lt;/code&gt; built with CET (I'll explain the &lt;code&gt;-z ibt&lt;/code&gt; part right after the listing):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;branch -o demo_cet demo.c -Wl,-z,relro,-z,lazy,-z,ibt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel -j .plt -j .plt.sec demo_cet&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Disassembly of section .plt:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001020 &amp;lt;.plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1020:&#9;ff 35 ca 2f 00 00 &#9;push QWORD PTR [rip&amp;#43;0x2fca] # 3ff0 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x8&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1026:&#9;ff 25 cc 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fcc] # 3ff8 &amp;lt;_GLOBAL_OFFSET_TABLE_&amp;#43;0x10&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 102c:&#9;0f 1f 40 00 &#9;nop DWORD PTR [rax&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1030:&#9;f3 0f 1e fa &#9;endbr64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1034:&#9;68 00 00 00 00 &#9;push 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1039:&#9;e9 e2 ff ff ff &#9;jmp 1020 &amp;lt;_init&amp;#43;0x20&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 103e:&#9;66 90 &#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Disassembly of section .plt.sec:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000001050 &amp;lt;puts@plt&amp;gt;:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1050:&#9;f3 0f 1e fa &#9;endbr64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1054:&#9;ff 25 a6 2f 00 00 &#9;jmp QWORD PTR [rip&amp;#43;0x2fa6] # 4000 &amp;lt;puts@GLIBC_2.2.5&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 105a:&#9;66 0f 1f 44 00 00 &#9;nop WORD PTR [rax&amp;#43;rax*1&amp;#43;0x0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -n demo_cet &lt;span class="p"&gt;|&lt;/span&gt; grep -i feature&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Properties: x86 feature: IBT&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Now about &lt;code&gt;-z ibt&lt;/code&gt;. The linker emits &lt;code&gt;.plt.sec&lt;/code&gt; only when the whole output is marked as IBT-compatible, whatever that means. This mark is a logical AND over the &lt;code&gt;.note.gnu.property&lt;/code&gt; of every input file: your object files and the required C runtime startup files that the distro ships and you link with (&lt;code&gt;Scrt1.o&lt;/code&gt;, &lt;code&gt;crti.o&lt;/code&gt; and others). &lt;code&gt;-fcf-protection=branch&lt;/code&gt; marks only your own code. So if the distro's startup files aren't marked, the AND clears the IBT bit, and you get the classic single &lt;code&gt;.plt&lt;/code&gt; without &lt;code&gt;.plt.sec&lt;/code&gt;. That's what happens on my Debian: &lt;code&gt;Scrt1.o&lt;/code&gt;, &lt;code&gt;crti.o&lt;/code&gt; and &lt;code&gt;crtn.o&lt;/code&gt; have no IBT mark (check yours with &lt;code&gt;readelf -n $(gcc -print-file-name=Scrt1.o)&lt;/code&gt;). On Ubuntu and Fedora (at least 24.04 and 41) these files are marked, so a plain &lt;code&gt;-fcf-protection=branch&lt;/code&gt; is enough there. My &lt;code&gt;-z ibt&lt;/code&gt; from the example just forces the IBT bit on the output, and the CET layout of the PLT with it, no matter what the inputs say.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;main&lt;/code&gt; now does &lt;code&gt;call 1050 &amp;lt;puts@plt&amp;gt;&lt;/code&gt;, which lands in &lt;code&gt;.plt.sec&lt;/code&gt;. A &lt;code&gt;.plt.sec&lt;/code&gt; stub is just &lt;code&gt;endbr64&lt;/code&gt; and a &lt;code&gt;jmp&lt;/code&gt; through the already familiar GOT slot, and the old &lt;code&gt;.plt&lt;/code&gt; keeps &lt;code&gt;PLT0&lt;/code&gt; and the lazy stubs. The &lt;code&gt;puts&lt;/code&gt; slot in the GOT initially points to the &lt;code&gt;endbr64&lt;/code&gt; of its lazy stub at &lt;code&gt;0x1030&lt;/code&gt; to make IBT happy. In the classic layout it pointed to &lt;code&gt;0x1036&lt;/code&gt;, the &lt;code&gt;push&lt;/code&gt; in the middle of the stub, but now the jump has to land on an &lt;code&gt;endbr64&lt;/code&gt; instruction. So &lt;code&gt;.plt.sec&lt;/code&gt; is what the code calls, and &lt;code&gt;.plt&lt;/code&gt; is still the way to the resolver. The first call goes &lt;code&gt;.plt.sec&lt;/code&gt; -&amp;gt; GOT -&amp;gt; lazy stub in &lt;code&gt;.plt&lt;/code&gt; -&amp;gt; &lt;code&gt;PLT0&lt;/code&gt; -&amp;gt; resolver, and after that the slot points straight to libc as usual, so &lt;code&gt;.plt.sec&lt;/code&gt; needs just one jump. Same mechanism, one more section, one more stub, one more abstraction, security folks are happy.&lt;/p&gt;&#10;&lt;h2 id="gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;&lt;code&gt;.gnu.hash&lt;/code&gt; vs &lt;code&gt;.hash&lt;/code&gt;: finding &lt;code&gt;puts&lt;/code&gt; in libc fast&lt;/h2&gt;&#10;&lt;p&gt;As promised back in &lt;a href="#the-resolving-logic-itself"&gt;the resolving part&lt;/a&gt; of the post, here's how &lt;code&gt;do_lookup_x&lt;/code&gt; does its lightning-fast searches inside one object. The loader asks every object in the search scope &amp;quot;do you have &lt;code&gt;puts&lt;/code&gt;?&amp;quot;, and almost all of them answer &amp;quot;no&amp;quot;. And they have to answer fast: my libc alone exports about 3100 symbols, and comparing the name against each of them with &lt;code&gt;strcmp&lt;/code&gt;, for every lookup, would be painfully slow even with all the assembly tricks inside it. That's what the hash sections are for.&lt;/p&gt;&#10;&lt;p&gt;There are two of them. The old one is &lt;code&gt;.hash&lt;/code&gt; (&lt;code&gt;DT_HASH&lt;/code&gt;), a plain hash table with chains, which is part of the standard System V ABI. The newer one is &lt;code&gt;.gnu.hash&lt;/code&gt; (&lt;code&gt;DT_GNU_HASH&lt;/code&gt;), a GNU extension from around 2006, and that's what GNU ld produces by default now, at least on Debian (&lt;code&gt;ld --help&lt;/code&gt; says &amp;quot;Default: gnu&amp;quot;, and we just have to believe in GNU). You get the old &lt;code&gt;.hash&lt;/code&gt; only if you specifically ask for it with &lt;code&gt;--hash-style=sysv&lt;/code&gt; or &lt;code&gt;--hash-style=both&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc demo.c -o demo &lt;span class="c1"&gt;# only GNU_HASH by default&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo &lt;span class="p"&gt;|&lt;/span&gt; grep -i hash&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffef5 (GNU_HASH) 0x3b0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc demo.c -o demo -Wl,--hash-style&lt;span class="o"&gt;=&lt;/span&gt;both &lt;span class="c1"&gt;# both of them&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo &lt;span class="p"&gt;|&lt;/span&gt; grep -i hash&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0000000000000004 (HASH) 0x3b0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffef5 (GNU_HASH) 0x3e0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The main point of adding &lt;code&gt;.gnu.hash&lt;/code&gt; (besides the fact that GNU usually likes adding something on top of the standard) is that it's built to make the &amp;quot;no&amp;quot; cheap, because that's the answer the symbol search gives most of the time. A plain hash table can't do that: even for a missing name, the bucket it lands in may hold a chain of other symbols, and the loader has to walk through it using the same &lt;code&gt;strcmp&lt;/code&gt;. So &lt;code&gt;.gnu.hash&lt;/code&gt; smartly puts a &lt;a href="https://en.wikipedia.org/wiki/Bloom_filter" target="_blank" rel="noopener"&gt;Bloom filter&lt;/a&gt; in front of the table. It's a small bit array that can answer either &amp;quot;definitely not here&amp;quot; or &amp;quot;maybe here&amp;quot;, but never a wrong &amp;quot;no&amp;quot;. The loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L398-L407" target="_blank" rel="noopener"&gt;takes one 64-bit word from it and checks only two bits&lt;/a&gt; that depend on the hash of the name. If either of them is 0, there's no such name in this object, and the lookup in it is over. The loader goes to the real table only if both bits are set.&lt;/p&gt;&#10;&lt;p&gt;This real table is the well-known buckets and chains, with one improvement. The resulting bucket for the calculated hash gives the &lt;code&gt;.dynsym&lt;/code&gt; index of the first candidate, and the chain array stores the hash of every symbol, so the loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L409-L424" target="_blank" rel="noopener"&gt;compares the hashes first&lt;/a&gt; and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L89-L91" target="_blank" rel="noopener"&gt;calls &lt;code&gt;strcmp&lt;/code&gt;&lt;/a&gt; only when they match. The lowest bit of each stored hash is reused as a &amp;quot;last in this bucket&amp;quot; flag, which is why the comparison ignores it. All of this lives in one special section, after a 16-byte header with four fields: the total number of buckets (&lt;code&gt;nbuckets&lt;/code&gt;), the &lt;code&gt;.dynsym&lt;/code&gt; index of the first symbol in the table (&lt;code&gt;symndx&lt;/code&gt;, everything before it, usually the undefined imports, is never hashed). Then come the size of the guarding Bloom filter in 64-bit words (&lt;code&gt;maskwords&lt;/code&gt;) and the shift for its second bit (&lt;code&gt;shift&lt;/code&gt;). glibc &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-setup_hash.c#L32-L45" target="_blank" rel="noopener"&gt;reads them one by one&lt;/a&gt; when it loads the object. And the hash function itself is &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/generic/dl-new-hash.h#L26-L34" target="_blank" rel="noopener"&gt;quite simple&lt;/a&gt; (it's called djb2): start with &lt;code&gt;h = 5381&lt;/code&gt;, and for every byte of the name do &lt;code&gt;h = h * 33 + c&lt;/code&gt;. It runs only once per lookup: &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-lookup.c#L760-L779" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_lookup_symbol_x&lt;/code&gt; calculates the hash and passes it to &lt;code&gt;do_lookup_x&lt;/code&gt;&lt;/a&gt; for every object in the scope.&lt;/p&gt;&#10;&lt;p&gt;Demo time! Here's a short Python script (it needs &lt;code&gt;pyelftools&lt;/code&gt;, so &lt;code&gt;apt install python3-pyelftools&lt;/code&gt; on Debian) that repeats the same steps as the loader on the real libc:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;struct&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;elftools.elf.elffile&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ELFFile&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;elf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ELFFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load_from_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elfclass&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;little_endian&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;64-bit little-endian ELF only&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;dynsym&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_section_by_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;.dynsym&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;gnu_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_section_by_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;.gnu.hash&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# the header, then the Bloom filter, the buckets and the chains, back to back&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shift&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;4I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;buckets_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;chains_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buckets_at&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;bloom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;Q&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;buckets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buckets_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;chains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;chains_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;I&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gnu_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chains_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="si"&gt;=}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5381&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xffffffff&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;hash(&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#34;) = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;#010x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bloom&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bloom word &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;maskwords&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;#018x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bit &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, bit &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bit2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bit2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;definitely not here&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bucket &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;nbuckets&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; -&amp;gt; dynsym[&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;]&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;empty bucket, the Bloom filter was wrong&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chains&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;symndx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dynsym&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34; dynsym[&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;] chain &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;stored&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;#010x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sym&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;found&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stored&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# bit 0 marks the last symbol in this bucket&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;not in the chain, the Bloom filter was wrong&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And here's what it says about &lt;code&gt;puts&lt;/code&gt; and about a name that libc definitely doesn't have:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; python3 gnuhash.py /lib/x86_64-linux-gnu/libc.so.6 puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nbuckets=1009 symndx=21 maskwords=512 shift=15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;hash(&amp;#34;puts&amp;#34;) = 0x7c9c7b11&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bloom word 492 = 0xd588080098220002, bit 17 = 1, bit 56 = 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bucket 67 -&amp;gt; dynsym[235]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; dynsym[235] chain 0x886b1bb4 iconv_open&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; dynsym[236] chain 0x7c9c7b10 puts&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;found&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; python3 gnuhash.py /lib/x86_64-linux-gnu/libc.so.6 fork_me_not&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nbuckets=1009 symndx=21 maskwords=512 shift=15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;hash(&amp;#34;fork_me_not&amp;#34;) = 0x0455b758&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bloom word 221 = 0x4140150200800180, bit 24 = 0, bit 43 = 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;definitely not here&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The &lt;code&gt;puts&lt;/code&gt; lookup goes just as expected. Both Bloom bits are set, bucket 67 starts at &lt;code&gt;dynsym[235]&lt;/code&gt;, and the first candidate there is &lt;code&gt;iconv_open&lt;/code&gt;. Its stored hash is completely different, so the loader skips it without any &lt;code&gt;strcmp&lt;/code&gt;. The next one is &lt;code&gt;puts&lt;/code&gt; at index 236, the same index &lt;code&gt;readelf --dyn-syms&lt;/code&gt; gives it. Its stored hash &lt;code&gt;0x7c9c7b10&lt;/code&gt; differs from the real &lt;code&gt;0x7c9c7b11&lt;/code&gt; only in that flag bit (0 here, so the chain goes on after &lt;code&gt;puts&lt;/code&gt;). And the famous &lt;code&gt;fork_me_not&lt;/code&gt; never gets past the Bloom filter: both of its bits are 0, so the table isn't touched at all. By the way, that's also what really happens to the executable's undefined &lt;code&gt;puts&lt;/code&gt; from &lt;a href="#the-resolving-logic-itself"&gt;the resolving part&lt;/a&gt;: in &lt;code&gt;demo_partial&lt;/code&gt;, it's &lt;code&gt;dynsym[3]&lt;/code&gt;, below its &lt;code&gt;symndx&lt;/code&gt; of 6, so the Bloom filter says &amp;quot;no&amp;quot; and &lt;code&gt;check_match&lt;/code&gt; never even sees it. In my &amp;quot;versatile&amp;quot; experiment, I passed 20000 names (&lt;code&gt;nope0&lt;/code&gt; to &lt;code&gt;nope19999&lt;/code&gt;) to the script, and the Bloom filter rejected 19437 of them (about 97%), which is quite good.&lt;/p&gt;&#10;&lt;p&gt;Why this matters for RE: this hash function is easy to recognize in a disassembly. It's &lt;code&gt;mov eax, 0x1505&lt;/code&gt; (that's 5381) followed by a loop with &lt;code&gt;shl&lt;/code&gt; by 5 and two &lt;code&gt;add&lt;/code&gt;s, or with &lt;code&gt;imul eax, eax, 0x21&lt;/code&gt; if the code was optimized for size. Of course, djb2 is used in plenty of ordinary hash tables, but if the same code also parses ELF headers and walks &lt;code&gt;.dynsym&lt;/code&gt;, it's most likely resolving symbols by itself, without the loader. Malware likes doing that too, and it gets &lt;a href="#how-malware-mimics-the-loader-dynamic-import-resolution"&gt;its own section&lt;/a&gt; a bit later.&lt;/p&gt;&#10;&lt;h2 id="no-dynamic-loader-but-still-a-plt-static-binaries-case"&gt;No dynamic loader, but still a PLT: static binaries case&lt;/h2&gt;&#10;&lt;p&gt;A fully static binary (&lt;code&gt;gcc -static&lt;/code&gt;) doesn't need the dynamic loader at all: everything, libc included, is linked into one file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static -o demo_static demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -h demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep Type&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Type: EXEC (Executable file)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;INTERP|DYNAMIC&amp;#39;&lt;/span&gt; &lt;span class="c1"&gt;# nothing&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_static&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;There is no dynamic section in this file.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -d -M intel demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;call.*&amp;lt;_IO_puts&amp;gt;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 401733:&#9;e8 68 31 00 00 &#9;call 4048a0 &amp;lt;_IO_puts&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 401742:&#9;e8 59 31 00 00 &#9;call 4048a0 &amp;lt;_IO_puts&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;No &lt;code&gt;PT_INTERP&lt;/code&gt; present, so the kernel doesn't even load &lt;code&gt;ld.so&lt;/code&gt;, and no &lt;code&gt;PT_DYNAMIC&lt;/code&gt; either, so there's nothing to resolve. Both &lt;code&gt;puts&lt;/code&gt; calls go straight to &lt;code&gt;_IO_puts&lt;/code&gt; (remember that libc's &lt;code&gt;puts&lt;/code&gt; is a weak alias of it) with a plain &lt;code&gt;call&lt;/code&gt;: it's an &lt;code&gt;ET_EXEC&lt;/code&gt; at a fixed address, so the linker knows exactly where everything is.&lt;/p&gt;&#10;&lt;p&gt;But surprisingly (to me), the static binary still has &lt;code&gt;.plt&lt;/code&gt;, &lt;code&gt;.got&lt;/code&gt;, &lt;code&gt;.got.plt&lt;/code&gt; and even &lt;code&gt;.rela.plt&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -S demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;\.plt|\.got&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [ 3] .rela.plt RELA 00000000004002f0 000002f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [ 5] .plt PROGBITS 0000000000401018 00001018&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [18] .got PROGBITS 00000000004a3f48 000a2f48&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [19] .got.plt PROGBITS 00000000004a3fe8 000a2fe8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -oE &lt;span class="s1"&gt;&amp;#39;R_X86_64_[A-Z_]+&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; sort &lt;span class="p"&gt;|&lt;/span&gt; uniq -c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 22 R_X86_64_IRELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;and all 22 relocations in its &lt;code&gt;.rela.plt&lt;/code&gt; are of one type, &lt;code&gt;R_X86_64_IRELATIVE&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;That's how IFUNC (indirect function, another GNU extension) works. Some libc functions, like &lt;code&gt;memcpy&lt;/code&gt;, &lt;code&gt;memcmp&lt;/code&gt;, &lt;code&gt;strcmp&lt;/code&gt; and &lt;code&gt;memset&lt;/code&gt;, come in several versions optimized for different CPUs (SSE2, AVX2, AVX-512 and so on). To properly choose one, each of them has a resolver: a small function that looks at the detected CPU features and returns the address of the best version. For &lt;code&gt;memcmp&lt;/code&gt; on x86-64, the resolver's choice is made in &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/multiarch/ifunc-memcmp.h#L29-L53" target="_blank" rel="noopener"&gt;&lt;code&gt;IFUNC_SELECTOR&lt;/code&gt;&lt;/a&gt;, which returns &lt;code&gt;__memcmp_evex_movbe&lt;/code&gt;, one of the two AVX2 versions, or &lt;code&gt;__memcmp_sse2&lt;/code&gt; as the fallback. The linker can't make this choice by itself, because it doesn't know which CPU the binary will run on, so the &lt;code&gt;IRELATIVE&lt;/code&gt; relocation stores the address of the resolver instead of a symbol. At startup, &lt;code&gt;__libc_start_main&lt;/code&gt; first &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/csu/libc-start.c#L269-L277" target="_blank" rel="noopener"&gt;detects the CPU features once, for all the resolvers, and then applies these relocations&lt;/a&gt;: for each one, it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/sysdeps/x86_64/dl-irel.h#L42-L46" target="_blank" rel="noopener"&gt;calls the resolver and writes the result&lt;/a&gt; into the &lt;code&gt;.got.plt&lt;/code&gt; slot.&lt;/p&gt;&#10;&lt;p&gt;You can see for yourself how one slot gets filled with the right address. &lt;code&gt;nm&lt;/code&gt; shows the address of the &lt;code&gt;memcmp&lt;/code&gt; resolver (the &lt;code&gt;i&lt;/code&gt; type means IFUNC), and the &lt;code&gt;IRELATIVE&lt;/code&gt; relocation with this address as its addend shows which &lt;code&gt;.got.plt&lt;/code&gt; slot gets the result:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; nm demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep -w memcmp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0000000000438ab0 i memcmp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_static &lt;span class="p"&gt;|&lt;/span&gt; grep 438ab0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000004a4058 0000000000000025 R_X86_64_IRELATIVE 438ab0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;And here's the same slot in pwndbg, right after &lt;code&gt;exec&lt;/code&gt; and then at &lt;code&gt;main&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# right after exec, before any startup code: the memcmp slot points into its own .plt stub&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; starti&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Program stopped.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000401600 in _start ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope 0x4a4058 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x4a4058 (_GLOBAL_OFFSET_TABLE_&amp;#43;112) —▸ 0x401076 ◂— nop &#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# and the stub itself is just a jmp through the slot&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; x/2i 0x401070&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x401070:&#9;jmp QWORD PTR [rip&amp;#43;0xa2fe2] # 0x4a4058&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x401076:&#9;xchg ax,ax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# at main, the startup code has already called the resolver&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x401729&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x0000000000401729 in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope 0x4a4058 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x4a4058 (_GLOBAL_OFFSET_TABLE_&amp;#43;112) —▸ 0x439c80 (__memcmp_avx2_movbe) ◂— cmp rdx, 0x20&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Right after &lt;code&gt;exec&lt;/code&gt;, the slot points to &lt;code&gt;0x401076&lt;/code&gt;, the &lt;code&gt;nop&lt;/code&gt; that follows the &lt;code&gt;jmp&lt;/code&gt; in its own &lt;code&gt;.plt&lt;/code&gt; stub. And the stub is just this &lt;code&gt;jmp&lt;/code&gt; through the slot: no heavy stuff like &lt;code&gt;PLT0&lt;/code&gt; or lazy binding, because there's no &lt;code&gt;_dl_runtime_resolve&lt;/code&gt; to call later. By the time &lt;code&gt;main&lt;/code&gt; runs, &lt;code&gt;__libc_start_main&lt;/code&gt; has already called the resolver at &lt;code&gt;0x438ab0&lt;/code&gt; and written its answer into the slot: &lt;code&gt;__memcmp_avx2_movbe&lt;/code&gt;, the AVX2 version my CPU got. So the relocation names the resolver, and the slot ends up holding its answer.&lt;/p&gt;&#10;&lt;p&gt;That's all the PLT does in a static binary: no dynamic linking, only IFUNCs. But this doesn't mean that &lt;code&gt;IRELATIVE&lt;/code&gt; relocations only appear in static binaries: the shared &lt;code&gt;libc.so.6&lt;/code&gt; has 46 of them too, next to the usual &lt;code&gt;JUMP_SLOT&lt;/code&gt; and &lt;code&gt;GLOB_DAT&lt;/code&gt;. And one more thing for RE: these stubs have no names in &lt;code&gt;objdump&lt;/code&gt;. It shows the calls to &lt;code&gt;memcmp&lt;/code&gt; as &lt;code&gt;call 401070 &amp;lt;_init+0x70&amp;gt;&lt;/code&gt; (&lt;code&gt;.plt&lt;/code&gt; just happens to follow &lt;code&gt;.init&lt;/code&gt; in this case), not as &lt;code&gt;memcmp@plt&lt;/code&gt; like in a dynamic binary. To find out what's behind a stub, take the slot from its &lt;code&gt;jmp&lt;/code&gt; and look it up in &lt;code&gt;readelf -r&lt;/code&gt;: the addend of its &lt;code&gt;IRELATIVE&lt;/code&gt; relocation is the actual resolver.&lt;/p&gt;&#10;&lt;h3 id="-static-pie-static-but-rebased-for-aslr"&gt;&lt;code&gt;-static-pie&lt;/code&gt;: static, but rebased for ASLR&lt;/h3&gt;&#10;&lt;p&gt;You probably remember &lt;code&gt;-static-pie&lt;/code&gt; from &lt;a href="#pie-or-when-even-your-own-addresses-move"&gt;the PIE part&lt;/a&gt;: the kernel maps it at a random address, and it has 1099 &lt;code&gt;R_X86_64_RELATIVE&lt;/code&gt; relocations to rebase itself. But who actually applies them, if it's static and there's no &lt;code&gt;ld.so&lt;/code&gt; at all?&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -static-pie -o demo_staticpie demo.c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;&amp;#39;INTERP|DYNAMIC&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; DYNAMIC 0x00000000000abda8 0x00000000000acda8 0x00000000000acda8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep NEEDED &lt;span class="c1"&gt;# nothing, no shared libraries&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -rW demo_staticpie &lt;span class="p"&gt;|&lt;/span&gt; grep -oE &lt;span class="s1"&gt;&amp;#39;R_X86_64_[A-Z_]+&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; sort &lt;span class="p"&gt;|&lt;/span&gt; uniq -c&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 22 R_X86_64_IRELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 1099 R_X86_64_RELATIVE&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# stop at the memcmp resolver and see who calls it&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b memcmp_ifunc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x3fc70&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x00007ffff7f89c70 in memcmp_ifunc ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; bt&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#0 0x00007ffff7f89c70 in memcmp_ifunc ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#1 0x00007ffff7f6b482 in _dl_relocate_static_pie ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#2 0x00007ffff7f53910 in __libc_start_main_impl ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;#3 0x00007ffff7f516e1 in _start ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So &lt;code&gt;-static-pie&lt;/code&gt; has a &lt;code&gt;PT_DYNAMIC&lt;/code&gt; segment (that's how its relocations are found), but no &lt;code&gt;PT_INTERP&lt;/code&gt; and no &lt;code&gt;DT_NEEDED&lt;/code&gt;. Without a loader, glibc has to do it by itself, with a small piece of its code linked right into the binary: &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc-static-pie.c#L36" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_relocate_static_pie&lt;/code&gt;&lt;/a&gt;. It calculates the binary's load address and &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/dl-reloc-static-pie.c#L79" target="_blank" rel="noopener"&gt;runs the same relocation code as &lt;code&gt;ld.so&lt;/code&gt;&lt;/a&gt; over it. The 22 &lt;code&gt;IRELATIVE&lt;/code&gt; relocations are applied in the same pass, which is why &lt;code&gt;memcmp_ifunc&lt;/code&gt; (that's the resolver's own name, which &lt;code&gt;nm&lt;/code&gt; above showed as &lt;code&gt;memcmp&lt;/code&gt; with the &lt;code&gt;i&lt;/code&gt; type) is called from &lt;code&gt;_dl_relocate_static_pie&lt;/code&gt; and not from &lt;code&gt;__libc_start_main&lt;/code&gt;, like in the plain static binary. The order is still right, though: &lt;code&gt;__libc_start_main&lt;/code&gt; calls it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/csu/libc-start.c#L269-L274" target="_blank" rel="noopener"&gt;only after the CPU features are detected&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="got1-to-walk-them-all"&gt;&lt;code&gt;GOT[1]&lt;/code&gt; to walk them all&lt;/h2&gt;&#10;&lt;p&gt;Remember that magic &lt;code&gt;GOT[1]&lt;/code&gt; from the lazy binding part? It holds the &lt;code&gt;link_map&lt;/code&gt; of the executable, and &lt;code&gt;link_map&lt;/code&gt; is more than just a descriptor of one object. It's also a node in a doubly linked list of every loaded object (the executable, every shared library, the vDSO and &lt;code&gt;ld.so&lt;/code&gt; itself), and each node keeps its object's load address (&lt;code&gt;l_addr&lt;/code&gt;) and name (&lt;code&gt;l_name&lt;/code&gt;). So just one RIP-relative load from your own &lt;code&gt;.got.plt&lt;/code&gt;, no syscalls, and you have the key to the whole map of what's loaded and where, without touching &lt;code&gt;/proc/self/maps&lt;/code&gt;. Walking the loader's module list is everyday work for shellcode and malware (on Windows, it even has a special name: PEB walking). On Linux, the more robust and portable way into the same list is the &lt;code&gt;r_debug&lt;/code&gt; structure that &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/link.h#L86-L92" target="_blank" rel="noopener"&gt;&lt;code&gt;DT_DEBUG&lt;/code&gt; points to&lt;/a&gt;, and &lt;code&gt;GOT[1]&lt;/code&gt; is just a cheaper way in.&lt;/p&gt;&#10;&lt;p&gt;Shellcode has no debug info (sad), so it has to walk the list by raw offsets, and on x86-64 they're simple: &lt;code&gt;l_addr&lt;/code&gt; at &lt;code&gt;+0x00&lt;/code&gt;, &lt;code&gt;l_name&lt;/code&gt; at &lt;code&gt;+0x08&lt;/code&gt;, &lt;code&gt;l_ld&lt;/code&gt; at &lt;code&gt;+0x10&lt;/code&gt;, &lt;code&gt;l_next&lt;/code&gt; at &lt;code&gt;+0x18&lt;/code&gt; and &lt;code&gt;l_prev&lt;/code&gt; at &lt;code&gt;+0x20&lt;/code&gt;, 8 bytes each, no padding. They don't (I hope) change between glibc versions, because these five fields are the public head of &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/link.h#L101-L110" target="_blank" rel="noopener"&gt;&lt;code&gt;struct link_map&lt;/code&gt;&lt;/a&gt;, which glibc's header calls &amp;quot;part of the protocol with the debugger&amp;quot; and &amp;quot;the same format used in SVR4&amp;quot;. The internal fields after them (&lt;code&gt;l_info&lt;/code&gt;, &lt;code&gt;l_scope&lt;/code&gt; and the rest) may change between versions, but the walk doesn't need any of them anyway. The executable's node is the first one in the list, so we can start right from &lt;code&gt;GOT[1]&lt;/code&gt; and just follow &lt;code&gt;l_next&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# GOT[1] is the second slot of .got.plt (0x3fe8 &amp;#43; 8), so read it and follow l_next until it&amp;#39;s NULL&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; set $lm = *(struct link_map **)$rebase(0x3ff0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; while $lm&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; printf &amp;#34;Base: 0x%016lx, Name: %s, Next: 0x%016lx\n&amp;#34;, $lm-&amp;gt;l_addr, $lm-&amp;gt;l_name, $lm-&amp;gt;l_next&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; set $lm = $lm-&amp;gt;l_next&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt;end&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x0000555555554000, Name: , Next: 0x00007ffff7fbd000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7fc5000, Name: linux-vdso.so.1, Next: 0x00007ffff7fbd7f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7dc6000, Name: /lib/x86_64-linux-gnu/libc.so.6, Next: 0x00007ffff7ffe0a0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Base: 0x00007ffff7fc7000, Name: /lib64/ld-linux-x86-64.so.2, Next: 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Four nodes, four objects: the executable, the vDSO, libc and &lt;code&gt;ld.so&lt;/code&gt;. The executable has no name: the loader didn't open it (the kernel did), so it &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/rtld.c#L1596" target="_blank" rel="noopener"&gt;creates its &lt;code&gt;link_map&lt;/code&gt; with an empty one&lt;/a&gt;. &lt;code&gt;info proc mappings&lt;/code&gt; confirms the numbers: libc's first mapping starts at &lt;code&gt;0x7ffff7dc6000&lt;/code&gt;, exactly its &lt;code&gt;l_addr&lt;/code&gt;. Strictly speaking, &lt;code&gt;l_addr&lt;/code&gt; isn't the load address, but the difference between the addresses in the file and in memory. For a PIE or a shared library, whose addresses in the file start at 0, it's the same thing, but for a non-PIE executable &lt;code&gt;l_addr&lt;/code&gt; is just 0. And by the way, pwndbg has the &lt;code&gt;linkmap&lt;/code&gt; command that prints the same list, if you don't like scripting gdb.&lt;/p&gt;&#10;&lt;p&gt;There's one catch to remember, though: all this works only with lazy binding. &lt;a href="#full-relro-resolve-everything-up-front-then-freeze-it"&gt;The full RELRO part&lt;/a&gt; showed that with &lt;code&gt;-z now&lt;/code&gt; the loader doesn't fill &lt;code&gt;GOT[1]&lt;/code&gt; and &lt;code&gt;GOT[2]&lt;/code&gt; at all, because nobody will ever call the resolver. The slots are still there, at the start of &lt;code&gt;.got&lt;/code&gt;, but they hold zeros:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# full RELRO: the reserved slots at the start of .got (0x3fb8)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rebase(0x3fb8) 3&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│ 0x555555557fb8 (_GLOBAL_OFFSET_TABLE_) ◂— 0x3dc8&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;01:0008│ 0x555555557fc0 (_GLOBAL_OFFSET_TABLE_&amp;#43;8) ◂— 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;02:0010│ 0x555555557fc8 (_GLOBAL_OFFSET_TABLE_&amp;#43;16) ◂— 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;For real malware, it's not a big deal: the technique from the next section doesn't need anyone's GOT at all, because it finds libc in memory by itself. &lt;code&gt;GOT[1]&lt;/code&gt; is just a civilized shortcut.&lt;/p&gt;&#10;&lt;h2 id="how-malware-mimics-the-loader-dynamic-import-resolution"&gt;How malware mimics the loader: dynamic import resolution&lt;/h2&gt;&#10;&lt;p&gt;A normal import table is a gift to an analyst: &lt;code&gt;nm -D&lt;/code&gt; on a sample with &lt;code&gt;connect&lt;/code&gt;, &lt;code&gt;ptrace&lt;/code&gt; and &lt;code&gt;socket&lt;/code&gt; in its &lt;code&gt;.dynsym&lt;/code&gt; tells more than enough before it even runs. That's why packed and evasive samples often don't have one, and resolve their imports at runtime by themselves, partially doing the loader's job. It usually takes three steps: find libc in memory, find the needed functions in it, and call them through the resolved addresses.&lt;/p&gt;&#10;&lt;p&gt;The first step is the most interesting one. Any pointer into libc will do, and, fortunately, &lt;code&gt;main&lt;/code&gt; always has one: its return address points into &lt;code&gt;__libc_start_call_main&lt;/code&gt;. libc's ELF header sits at the start of its first page, so round the pointer down to a page and go back one page at a time until the memory starts with the magic &lt;code&gt;\x7fELF&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# main&amp;#39;s return address points into libc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; telescope $rbp&amp;#43;8 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00:0000│&amp;#43;008 0x7fffffffea98 —▸ 0x7ffff7defca8 (__libc_start_call_main&amp;#43;120) ◂— mov edi, eax&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# round it down to a page and go back until the ELF magic (0x464c457f is &amp;#34;\x7fELF&amp;#34; as a little-endian int)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; set $p = *(long *)($rbp &amp;#43; 8) &amp;amp; ~0xfff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; while *(int *)$p != 0x464c457f&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt; set $p -= 0x1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;gt;end&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; p/x $p&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$1 = 0x7ffff7dc6000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap $p&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x7ffff7dc6000 0x7ffff7dee000 r--p 28000 0 /usr/lib/x86_64-linux-gnu/libc.so.6 &amp;#43;0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dee000 0x7ffff7f51000 r-xp 163000 28000 /usr/lib/x86_64-linux-gnu/libc.so.6&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The scan stops right at &lt;code&gt;0x7ffff7dc6000&lt;/code&gt;, the same address as libc's &lt;code&gt;l_addr&lt;/code&gt; in the &lt;code&gt;GOT[1]&lt;/code&gt; walk above. libc's mappings should (I believe) go back to back, so the scan never steps into an unmapped page. Once you have the ELF header, find &lt;code&gt;PT_DYNAMIC&lt;/code&gt; in the program headers, and after that, &lt;code&gt;.dynamic&lt;/code&gt; gives you &lt;code&gt;DT_GNU_HASH&lt;/code&gt;, &lt;code&gt;DT_SYMTAB&lt;/code&gt; and &lt;code&gt;DT_STRTAB&lt;/code&gt;, the same tables &lt;code&gt;gnuhash.py&lt;/code&gt; read from the file. One catch here: in memory, these pointers are usually already absolute, because the loader &lt;a href="https://github.com/gnutools/glibc/blob/glibc-2.41/elf/get-dynamic-info.h#L72-L91" target="_blank" rel="noopener"&gt;adds the load address to them in place&lt;/a&gt;. For libc, &lt;code&gt;DT_GNU_HASH&lt;/code&gt; is &lt;code&gt;0x4528&lt;/code&gt; in the file and &lt;code&gt;0x7ffff7dca528&lt;/code&gt; in memory. But if &lt;code&gt;.dynamic&lt;/code&gt; is read-only, like in the vDSO, it stays as it is (&lt;code&gt;0x1e0&lt;/code&gt; there), so teach your hand-made resolver to handle both cases.&lt;/p&gt;&#10;&lt;p&gt;The second step is where the hashes come in. A &lt;code&gt;&amp;quot;ptrace&amp;quot;&lt;/code&gt; string in the sample is just as visible as a name in the import table, so samples usually store only a hash of each name and compare it with the hashes of libc's symbols. If the hash is djb2, the sample can even cheat: use libc's own &lt;code&gt;.gnu.hash&lt;/code&gt; and do what &lt;code&gt;gnuhash.py&lt;/code&gt; does. With any other hash (ROR13 from the Windows shellcoders' world, CRC32, FNV and so on), it just walks all of &lt;code&gt;.dynsym&lt;/code&gt; and hashes every name. The third step is the easiest one: the resolved addresses go into a private table, kind of the sample's own hand-made GOT, and all the calls go through it.&lt;/p&gt;&#10;&lt;p&gt;For RE, the import table is gone, but the work the sample has to do still leaves traces. A binary that magically talks to the network with nothing like &lt;code&gt;socket&lt;/code&gt; in its imports is already suspicious. In the code, try to look for a loop that compares memory with &lt;code&gt;0x464c457f&lt;/code&gt; while stepping back by &lt;code&gt;0x1000&lt;/code&gt;, and for the hash loop from &lt;a href="#gnuhash-vs-hash-finding-puts-in-libc-fast"&gt;the &lt;code&gt;.gnu.hash&lt;/code&gt; part&lt;/a&gt;. The hashes are good for hunting too: pre-calculate them for the interesting names and search the binary for these constants. For djb2, for instance, &lt;code&gt;ptrace&lt;/code&gt; is &lt;code&gt;0x159412a4&lt;/code&gt;, &lt;code&gt;socket&lt;/code&gt; is &lt;code&gt;0x1c31032e&lt;/code&gt; and &lt;code&gt;execve&lt;/code&gt; is &lt;code&gt;0xfc2c9fc5&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;None of this is exotic once you know how the loader works: the malware is just trying to be its own &lt;code&gt;_dl_fixup&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="trust-but-verify-checksec"&gt;Trust, but verify: &lt;code&gt;checksec&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;And last but not the best: &lt;code&gt;checksec&lt;/code&gt;, the usual first look at an unknown binary in tons of writeups. Debian 13 still ships version 2.6.0 from 2022, so I installed &lt;a href="https://github.com/slimm609/checksec/releases/tag/3.2.0" target="_blank" rel="noopener"&gt;the current 3.2.0&lt;/a&gt; from GitHub. Its &lt;code&gt;RELRO&lt;/code&gt; verdict comes from the same two things we've been checking by hand on &lt;code&gt;demo_partial&lt;/code&gt; and &lt;code&gt;demo_full&lt;/code&gt;, the &lt;code&gt;GNU_RELRO&lt;/code&gt; segment and the &lt;code&gt;BIND_NOW&lt;/code&gt; flag. By default, it prints a school-level ASCII art banner and a table more than 300 characters wide, so I use its JSON output and &lt;code&gt;jq&lt;/code&gt; to keep only the &lt;code&gt;RELRO&lt;/code&gt; field I need:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_partial &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Partial RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_full &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Full RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/slimm609/checksec/blob/3.2.0/pkg/checksec/relro.go#L31-L45" target="_blank" rel="noopener"&gt;The check itself&lt;/a&gt; is just a few lines of Go (yes, it was rewritten in Go). Specifically, &lt;code&gt;BIND_NOW&lt;/code&gt; in any of its three forms (the &lt;code&gt;DT_BIND_NOW&lt;/code&gt; tag, or the &lt;code&gt;NOW&lt;/code&gt; flag in &lt;code&gt;DT_FLAGS&lt;/code&gt; or &lt;code&gt;DT_FLAGS_1&lt;/code&gt;) means Full, otherwise &lt;code&gt;GNU_RELRO&lt;/code&gt; means Partial, and with neither of them, it's just No. But look at the order: &lt;code&gt;BIND_NOW&lt;/code&gt; comes first, and checksec doesn't even look for &lt;code&gt;GNU_RELRO&lt;/code&gt; if it's set. So a binary linked with &lt;code&gt;-z norelro -z now&lt;/code&gt; suddenly gets &amp;quot;Full RELRO&amp;quot;, without any RELRO at all:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; gcc -O0 -fno-stack-protector -fcf-protection&lt;span class="o"&gt;=&lt;/span&gt;none -o demo_nowonly demo.c -Wl,-z,norelro,-z,now&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -l demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; grep GNU_RELRO &lt;span class="c1"&gt;# nothing&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; readelf -d demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;&amp;#39;bind|flags&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000000000001e (FLAGS) BIND_NOW&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x000000006ffffffb (FLAGS_1) Flags: NOW PIE&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; checksec file -o json ./demo_nowonly &lt;span class="p"&gt;|&lt;/span&gt; jq -r &lt;span class="s1"&gt;&amp;#39;.[0].checks.relro.value&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Full RELRO&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# at main: resolved like in full RELRO, but is it read-only?&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; b main&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1 at 0x113d&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Breakpoint 1, 0x000055555555513d in main ()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; got -r&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;State of the GOT of /work/demo_nowonly:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;GOT protection: No RELRO | Found 6 GOT entries passing the filter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557330] puts@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e46300 (puts) ◂— push r14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557338] __libc_start_main@GLIBC_2.34 -&amp;gt; 0x7ffff7defce0 (__libc_start_main) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557340] _ITM_deregisterTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557348] __gmon_start__ -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557350] _ITM_registerTMCloneTable -&amp;gt; 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[0x555555557358] __cxa_finalize@GLIBC_2.2.5 -&amp;gt; 0x7ffff7e07b90 (__cxa_finalize) ◂— push r15&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd dbg"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;pwndbg&amp;gt;&lt;/span&gt; vmmap 0x555555557330&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Start End Perm Size Offset File (set vmmap-prefer-relpaths on)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x555555556000 0x555555557000 r--p 1000 2000 demo_nowonly&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;► 0x555555557000 0x555555558000 rw-p 1000 2000 demo_nowonly &amp;#43;0x330&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x7ffff7dc3000 0x7ffff7dc6000 rw-p 3000 0 [anon_7ffff7dc3]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;At &lt;code&gt;main&lt;/code&gt;, everything is resolved up front, just like with full RELRO, but nobody made the GOT read-only afterwards. The &lt;code&gt;puts&lt;/code&gt; slot sits on an &lt;code&gt;rw-p&lt;/code&gt; page, and pwndbg correctly says &amp;quot;No RELRO&amp;quot;. Funny enough, Debian's old 2.6.0 gets it right, because it &lt;a href="https://github.com/slimm609/checksec/blob/2.6.0/checksec#L702-L711" target="_blank" rel="noopener"&gt;checks &lt;code&gt;GNU_RELRO&lt;/code&gt; first&lt;/a&gt;. So don't trust the fancy tools blindly.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item><item><title>IOLI crackmes, rebuilt</title><link>https://pugachev.io/2026/11/05/ioli-crackmes-rebuilt/</link><pubDate>Thu, 05 Nov 2026 21:38:23 +0100</pubDate><guid>https://pugachev.io/2026/11/05/ioli-crackmes-rebuilt/</guid><description>&lt;p&gt;If you've read a beginner writeup on reverse engineering in the last ten years or so, chances are it used one of the IOLI crackmes. These are ten tiny exercises by Pau Oliva Fora (pof) from 2007, and none of them takes more than an hour. I don't remember where I first heard about them, but they're even mentioned in the &lt;a href="https://book.rada.re/crackmes/ioli/intro.html" target="_blank" rel="noopener"&gt;radare2 book&lt;/a&gt;. I found and &lt;a href="https://pugachev.io/2023/12/16/solutions-for-ioli-crackmes/"&gt;solved them back in 2023&lt;/a&gt;: fun at the beginning, a bit boring at the end, but still a classic.&lt;/p&gt;&#10;&lt;p&gt;The binaries, though, haven't aged well. They're 32-bit only: Linux x86, Windows x86 and Windows CE on ARM (does anyone still remember what that was?). There's no x64 version at all. &lt;a href="https://github.com/BinaryResearch/IOLI64" target="_blank" rel="noopener"&gt;IOLI64&lt;/a&gt; did port them to x86-64, but it's Linux-only, built from roughly reconstructed sources, and the repo is archived.&lt;/p&gt;&#10;&lt;p&gt;So, ladies and gentlemen, I'm proud to present my rebuild of them. The goal of this pet project was C sources that compile to code as close to the originals as a modern compiler allows: the same compilation flags, the same functions in the same order, the same conditions, and even the dead code the original compiler (or author?) left in. Then my fellow AI friend and I checked every level against the original Linux and Windows binaries, function by function. The result comes pre-compiled for Linux, Windows and macOS on x86, x64 and arm64, and there's a &lt;code&gt;CMakeLists.txt&lt;/code&gt; if you ever want to try other architectures, operating systems or compilers.&lt;/p&gt;&#10;&lt;p&gt;It's all here: &lt;a href="https://github.com/itwaseasy/IOLI-crackmes-rebuilt" target="_blank" rel="noopener"&gt;IOLI-crackmes-rebuilt&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item><item><title>Multiple ways to find the auxiliary vector (auxv)</title><link>https://pugachev.io/2026/10/25/multiple-ways-to-find-the-auxiliary-vector-auxv/</link><pubDate>Sun, 25 Oct 2026 17:46:59 +0100</pubDate><guid>https://pugachev.io/2026/10/25/multiple-ways-to-find-the-auxiliary-vector-auxv/</guid><description>&lt;p&gt;I've heard the term &amp;quot;auxiliary vector&amp;quot; a few times before, but never had time to dig into it. So I decided to finally make myself a bit smarter and learn what the &lt;code&gt;auxv&lt;/code&gt; is. To make it even more satisfying, I set a goal to find as many ways to get to the &lt;code&gt;auxv&lt;/code&gt; as I could, from a one-liner in the shell down to the &amp;quot;no libc, no envp, scan the stack and pray&amp;quot; method.&lt;/p&gt;&#10;&lt;p&gt;Everything here is x86-64 Linux with the System V AMD64 ABI.&lt;/p&gt;&#10;&lt;h2 id="what-the-auxv-actually-is"&gt;What the &lt;code&gt;auxv&lt;/code&gt; actually is&lt;/h2&gt;&#10;&lt;p&gt;The man page for &lt;code&gt;getauxval(3)&lt;/code&gt; calls it&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;a mechanism that the kernel's ELF binary loader uses to pass certain information to user space when a program is executed.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;That's nice, but it doesn't really say &lt;em&gt;why&lt;/em&gt; it exists. When the kernel's ELF loader (&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;) passes control to a new process, the dynamic linker hasn't run yet: there is no libc, no TLS, no resolved symbols, nothing. But &lt;code&gt;ld.so&lt;/code&gt; still needs to bootstrap itself somehow, and for that it needs things only the kernel could know. Where are the program headers mapped? Where is the vDSO? What's the page size? Is this process privileged? The C runtime can't ask anyone, so the &lt;code&gt;auxv&lt;/code&gt; is how the kernel passes this knowledge. It's a small array of key/value pairs pushed onto the initial stack, right next to &lt;code&gt;argv&lt;/code&gt; and &lt;code&gt;envp&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;Each entry is an &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/elf.h#L1187-L1197" target="_blank" rel="noopener"&gt;&lt;code&gt;Elf64_auxv_t&lt;/code&gt;&lt;/a&gt; struct:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* AT_* tag */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;union&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a_val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* integer value, or a pointer */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;a_un&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are the most interesting ones with their tag numbers, since later we'll need to match the raw numbers:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Tag&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;#&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Meaning&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHDR&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;address of the program's ELF program headers&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHENT&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;size of one program header (always &lt;code&gt;0x38&lt;/code&gt; = &lt;code&gt;sizeof(Elf64_Phdr)&lt;/code&gt;)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PHNUM&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;number of program headers&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_PAGESZ&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;6&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;page size (&lt;code&gt;0x1000&lt;/code&gt; on x86-64)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_BASE&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;7&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;base address of the interpreter (&lt;code&gt;ld.so&lt;/code&gt;)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_ENTRY&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;entry point of the program&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_UID&lt;/code&gt; / &lt;code&gt;AT_EUID&lt;/code&gt; / &lt;code&gt;AT_GID&lt;/code&gt; / &lt;code&gt;AT_EGID&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;11-14&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;the four credential IDs&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_HWCAP&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;16&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;CPU capability bitmask&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_CLKTCK&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;17&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;times()&lt;/code&gt; tick frequency (&lt;code&gt;100&lt;/code&gt; on x86-64)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_SECURE&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;23&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;secure execution mode (setuid and similar)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_RANDOM&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;25&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pointer to kernel-generated random bytes&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_EXECFN&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;31&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pointer to the pathname used to exec the binary&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;33&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;base of the vDSO's ELF header&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;AT_NULL&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;terminating &lt;code&gt;{0, 0}&lt;/code&gt; entry&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;The full list is in the kernel's &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/uapi/linux/auxvec.h" target="_blank" rel="noopener"&gt;&lt;code&gt;include/uapi/linux/auxvec.h&lt;/code&gt;&lt;/a&gt;, and the x86-specific additions are in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/uapi/asm/auxvec.h" target="_blank" rel="noopener"&gt;&lt;code&gt;arch/x86/include/uapi/asm/auxvec.h&lt;/code&gt;&lt;/a&gt; (x86 adds &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; and, for 32-bit, &lt;code&gt;AT_SYSINFO&lt;/code&gt;).&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;AT_RANDOM&lt;/code&gt; is the most thrilling one: glibc takes those 16 bytes to generate the stack canary and the pointer guard, so it shows up in exploitation writeups all the time.&lt;/p&gt;&#10;&lt;p&gt;The kernel builds the &lt;code&gt;auxv&lt;/code&gt; in the &lt;code&gt;create_elf_tables()&lt;/code&gt; function inside &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L164" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;&lt;/a&gt;, if you want to see how it's done for some reason. The order is defined there (and it hasn't changed in years): &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L247" target="_blank" rel="noopener"&gt;&lt;code&gt;ARCH_DLINFO&lt;/code&gt; first&lt;/a&gt; (on x86-64, that's &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; for the vDSO plus &lt;code&gt;AT_MINSIGSTKSZ&lt;/code&gt;), then &lt;code&gt;AT_HWCAP&lt;/code&gt;, &lt;code&gt;AT_PAGESZ&lt;/code&gt;, &lt;code&gt;AT_CLKTCK&lt;/code&gt;, &lt;code&gt;AT_PHDR&lt;/code&gt; and so on.&lt;/p&gt;&#10;&lt;h2 id="the-layout-at-the-entry-point"&gt;The layout at the entry point&lt;/h2&gt;&#10;&lt;p&gt;I heard that examples and pictures make a text easier to understand, so here is one in ASCII. When the kernel passes control to &lt;code&gt;_start&lt;/code&gt;, the &lt;code&gt;rsp&lt;/code&gt; register points to &lt;code&gt;argc&lt;/code&gt;, while &lt;code&gt;argv&lt;/code&gt;, &lt;code&gt;envp&lt;/code&gt; and the &lt;code&gt;auxv&lt;/code&gt; are above it, toward higher addresses:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Higher addresses&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Elf64_auxv_t { AT_NULL, 0 } &amp;lt;- terminating entry&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Elf64_auxv_t { AT_SYSINFO_EHDR, ... } &amp;lt;- auxv[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; NULL &amp;lt;- envp terminator&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; envp[n-1]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; envp[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; NULL &amp;lt;- argv terminator&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argv[argc-1]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argv[0]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; argc &amp;lt;- RSP points here at _start&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Lower addresses&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So the &lt;code&gt;auxv&lt;/code&gt; starts right after the &lt;code&gt;NULL&lt;/code&gt; pointer that terminates &lt;code&gt;envp&lt;/code&gt;, and &lt;code&gt;envp&lt;/code&gt; starts right after the &lt;code&gt;NULL&lt;/code&gt; that terminates &lt;code&gt;argv&lt;/code&gt;. This highlights two things that most of the methods below rely on:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;If you have &lt;code&gt;envp&lt;/code&gt;, finding the &lt;code&gt;auxv&lt;/code&gt; is easy: walk to the &lt;code&gt;NULL&lt;/code&gt; and get the next entry.&lt;/li&gt;&#10;&lt;li&gt;The whole structure above exists only on the initial stack of the main thread. Another thread created with &lt;code&gt;clone(2)&lt;/code&gt; gets a new stack without any of this stuff, and, unfortunately, as we'll see later, that's where the stack-scanning method stops working.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Now, the methods, roughly from normal to 1337.&lt;/p&gt;&#10;&lt;h2 id="method-1-ld_show_auxv1-the-shell-one-liner"&gt;Method 1: &lt;code&gt;LD_SHOW_AUXV=1&lt;/code&gt; (the shell one-liner)&lt;/h2&gt;&#10;&lt;p&gt;The easy one: glibc's dynamic linker will dump the whole vector for you if you just set one environment variable:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;LD_SHOW_AUXV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; /bin/true&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SYSINFO_EHDR: 0x7f67fbff3000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_MINSIGSTKSZ: 1776&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP: f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PAGESZ: 4096&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_CLKTCK: 100&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHDR: 0x555645065040&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHENT: 56&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHNUM: 14&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_BASE: 0x7f67fbff5000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_FLAGS: 0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_ENTRY: 0x5556450677e0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_UID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EUID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_GID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EGID: 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SECURE: 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RANDOM: 0x7ffe1906fab9&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP2: 0x2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EXECFN: /bin/true&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PLATFORM: x86_64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_FEATURE_SIZE: 28&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_ALIGN: 32&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;This is done inside &lt;code&gt;ld.so&lt;/code&gt;: &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L2668-L2669" target="_blank" rel="noopener"&gt;&lt;code&gt;elf/rtld.c&lt;/code&gt;&lt;/a&gt; parses the environment variable and calls &lt;code&gt;_dl_show_auxv()&lt;/code&gt; from &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-sysdep.c#L151" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/unix/sysv/linux/dl-sysdep.c&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;People usually just say it &amp;quot;doesn't work for setuid/setgid and statically linked binaries&amp;quot;. Both are true, but for two completely different reasons:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;static binaries: there is no &lt;code&gt;ld.so&lt;/code&gt; in the process at all, so nobody can parse &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt;, and the binary prints nothing&lt;/li&gt;&#10;&lt;li&gt;&amp;quot;secure-exec&amp;quot; (e.g. setuid) binaries: &lt;code&gt;ld.so&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; there, but it totally ignores &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;But it's not really about the setuid bit. The guilty one is the kernel's &lt;code&gt;AT_SECURE&lt;/code&gt; flag, which is set whenever an &lt;code&gt;execve&lt;/code&gt; &lt;em&gt;gains privileges relative to its caller&lt;/em&gt;: the capability LSM (one of the Linux Security Modules, like SELinux or AppArmor) &lt;a href="https://github.com/torvalds/linux/blob/v6.18/security/commoncap.c#L989-L995" target="_blank" rel="noopener"&gt;sets it&lt;/a&gt; when the setuid/setgid bit actually changes the euid/egid values. Another case is binaries with file capabilities (set with &lt;code&gt;setcap&lt;/code&gt;). If you need more, &lt;a href="https://github.com/torvalds/linux/blob/v6.18/security/selinux/hooks.c#L2415-L2421" target="_blank" rel="noopener"&gt;SELinux sets it too&lt;/a&gt; on domain transitions (unless the policy grants the &lt;code&gt;process:noatsecure&lt;/code&gt; permission), and other LSMs can add their own cases, because why not.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-parse_auxv.h#L48" target="_blank" rel="noopener"&gt;glibc copies&lt;/a&gt; the &lt;code&gt;AT_SECURE&lt;/code&gt; flag value into the &lt;code&gt;__libc_enable_secure&lt;/code&gt; variable, and if it's 1, &lt;code&gt;ld.so&lt;/code&gt; calls &lt;code&gt;process_envvars_secure()&lt;/code&gt; instead of the normal path, which &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L2562-L2575" target="_blank" rel="noopener"&gt;removes a whole list of &amp;quot;very dangerous&amp;quot; variables with &lt;code&gt;unsetenv()&lt;/code&gt;&lt;/a&gt;, including &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; (the list is in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/generic/unsecvars.h#L4" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/generic/unsecvars.h&lt;/code&gt;&lt;/a&gt;). So the variable is gone before it can do anything.&lt;/p&gt;&#10;&lt;p&gt;The important part from above is &amp;quot;relative to its caller&amp;quot;: a setuid binary is not automatically secure (huh?). Run a setuid-root program as a normal user, and euid goes from &lt;code&gt;1000&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;, so &lt;code&gt;AT_SECURE=1&lt;/code&gt; and &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; is removed. Run the same program as root, and nothing changes: euid stays &lt;code&gt;0&lt;/code&gt;, the same as your real uid, so &lt;code&gt;AT_SECURE=0&lt;/code&gt; and the vector is dumped just fine.&lt;/p&gt;&#10;&lt;p&gt;This glibc trick with rewriting &lt;code&gt;environ&lt;/code&gt; for a secure exec will come back later and &lt;a href="#method-6-from-envp-or-environ-in-c"&gt;break one of the methods&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;And just to mention, the method only works with glibc. musl has no &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; (&lt;a href="https://git.musl-libc.org/cgit/musl/tree/ldso/dynlink.c?h=v1.2.6#n1821" target="_blank" rel="noopener"&gt;its dynamic linker is quite conservative and only knows &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; and &lt;code&gt;LD_PRELOAD&lt;/code&gt;&lt;/a&gt;), so there you have to choose one of the methods below.&lt;/p&gt;&#10;&lt;h2 id="method-2-read-procselfauxv-directly"&gt;Method 2: read &lt;code&gt;/proc/self/auxv&lt;/code&gt; directly&lt;/h2&gt;&#10;&lt;p&gt;The kernel also exposes the raw &lt;code&gt;auxv&lt;/code&gt; as a file. It's a flat array of &lt;code&gt;Elf64_auxv_t&lt;/code&gt; structs, i.e. pairs of 8-byte little-endian words, terminated by the &lt;code&gt;{0, 0}&lt;/code&gt; entry. A bit of &lt;code&gt;hexdump&lt;/code&gt; magic does the printing:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; hexdump -v -e &lt;span class="s1"&gt;&amp;#39;1/8 &amp;#34;0x%016x: &amp;#34; 1/8 &amp;#34;0x%016x\n&amp;#34;&amp;#39;&lt;/span&gt; /proc/self/auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000021: 0x00007fa97d6f7000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000033: 0x00000000000006f0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000010: 0x000000000f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000006: 0x0000000000001000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000011: 0x0000000000000064&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0000000000000000: 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The left column is &lt;code&gt;a_type&lt;/code&gt;, and the right one is &lt;code&gt;a_val&lt;/code&gt;, so the tags are in hex here: &lt;code&gt;0x21&lt;/code&gt; is &lt;code&gt;AT_SYSINFO_EHDR&lt;/code&gt; (33), &lt;code&gt;0x33&lt;/code&gt; is &lt;code&gt;AT_MINSIGSTKSZ&lt;/code&gt; (51), &lt;code&gt;0x10&lt;/code&gt; is &lt;code&gt;AT_HWCAP&lt;/code&gt;, &lt;code&gt;0x06&lt;/code&gt; is &lt;code&gt;AT_PAGESZ&lt;/code&gt; and &lt;code&gt;0x11&lt;/code&gt; is &lt;code&gt;AT_CLKTCK&lt;/code&gt;, the same order as in the &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; output above. The last &lt;code&gt;{0, 0}&lt;/code&gt; entry is &lt;code&gt;AT_NULL&lt;/code&gt;. If you don't have &lt;code&gt;hexdump&lt;/code&gt;, &lt;code&gt;od&lt;/code&gt; can do the same:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; od -A n -t x8 -w16 -v /proc/self/auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;A couple of things to know about this file. It's backed by &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/linux/mm_types.h#L1115" target="_blank" rel="noopener"&gt;&lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;&lt;/a&gt;: the kernel keeps its own copy of the &lt;code&gt;auxv&lt;/code&gt; in &lt;code&gt;mm_struct&lt;/code&gt;, and &lt;code&gt;auxv_read&lt;/code&gt; in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L1081-L1094" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/proc/base.c&lt;/code&gt;&lt;/a&gt; just returns that copy. So it's a snapshot the kernel took at &lt;code&gt;execve&lt;/code&gt;, not the &lt;code&gt;auxv&lt;/code&gt; the process is actually using. If something in the process changes the &lt;code&gt;auxv&lt;/code&gt; on the stack for some reason (and &lt;code&gt;ld.so&lt;/code&gt; actually can do that silently, &lt;a href="#when-the-api-can-lie"&gt;more on it below&lt;/a&gt;), &lt;code&gt;/proc/self/auxv&lt;/code&gt; still shows the original kernel values. In other words, the two can be different, and it's neither a feature nor a bug.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;/proc/&amp;lt;pid&amp;gt;/auxv&lt;/code&gt; of &lt;em&gt;another&lt;/em&gt; process &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L3674" target="_blank" rel="noopener"&gt;has mode &lt;code&gt;S_IRUSR&lt;/code&gt;&lt;/a&gt; and is &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/proc/base.c#L1076-L1079" target="_blank" rel="noopener"&gt;protected by &lt;code&gt;PTRACE_MODE_READ_FSCREDS&lt;/code&gt;&lt;/a&gt;. That's the same &amp;quot;could I &lt;code&gt;ptrace&lt;/code&gt; you?&amp;quot; check, but in the &lt;code&gt;FSCREDS&lt;/code&gt; variant, because you access it through a filesystem &lt;code&gt;open()&lt;/code&gt; call. So it compares your fsuid (the identity that file operations use) with the target, not your real uid. In practice, you can read it for your own processes, but for someone else's you need ptrace-level access (same UID, or &lt;code&gt;CAP_SYS_PTRACE&lt;/code&gt;). And &lt;code&gt;/proc/self/auxv&lt;/code&gt; is, of course, always readable by the process itself, which makes this method so reliable: no libc, no special privileges, just open it and read it.&lt;/p&gt;&#10;&lt;h2 id="method-3-getauxval3"&gt;Method 3: &lt;code&gt;getauxval(3)&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;If you're already linked against glibc (&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/misc/Versions#L150" target="_blank" rel="noopener"&gt;2.16+&lt;/a&gt;) or musl (1.1.0+), this is the official API:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/auxv.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pagesz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getauxval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AT_PAGESZ&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* 4096 */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;getauxval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AT_RANDOM&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* the 16 random bytes */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If the tag isn't there, it returns &lt;code&gt;0&lt;/code&gt; and sets &lt;code&gt;errno&lt;/code&gt; to &lt;code&gt;ENOENT&lt;/code&gt;. But &lt;code&gt;0&lt;/code&gt; can also be a real value (e.g. &lt;code&gt;AT_SECURE&lt;/code&gt;, or &lt;code&gt;AT_UID&lt;/code&gt; for root), so clear your &lt;code&gt;errno&lt;/code&gt; before the call and check it after.&lt;/p&gt;&#10;&lt;p&gt;The funny thing I noticed while testing this method is that &lt;code&gt;getauxval&lt;/code&gt; doesn't always return what the kernel put in the &lt;code&gt;auxv&lt;/code&gt;. For &lt;code&gt;AT_HWCAP&lt;/code&gt; and &lt;code&gt;AT_HWCAP2&lt;/code&gt;, it takes a mysterious shortcut: instead of scanning the &lt;code&gt;auxv&lt;/code&gt;, it returns glibc's own internal &lt;code&gt;GLRO(dl_hwcap)&lt;/code&gt; and &lt;code&gt;GLRO(dl_hwcap2)&lt;/code&gt; (see &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/misc/getauxval.c#L30-L39" target="_blank" rel="noopener"&gt;&lt;code&gt;misc/getauxval.c&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;&#10;&lt;p&gt;Initially, these internal fields are just copies of the kernel's values: early in startup, &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/unix/sysv/linux/dl-parse_auxv.h#L50-L53" target="_blank" rel="noopener"&gt;&lt;code&gt;_dl_parse_auxv&lt;/code&gt; fills all &lt;code&gt;dl_hwcap*&lt;/code&gt; fields&lt;/a&gt; directly from the &lt;code&gt;auxv&lt;/code&gt; (&lt;code&gt;GLRO(dl_hwcap) = auxv_values[AT_HWCAP]&lt;/code&gt;, &lt;code&gt;GLRO(dl_hwcap2) = auxv_values[AT_HWCAP2]&lt;/code&gt; and so on). The difference comes later, and only in one of the fields: on x86, &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/x86/cpu-features.c#L1242" target="_blank" rel="noopener"&gt;&lt;code&gt;cpu-features.c&lt;/code&gt; &lt;em&gt;overwrites&lt;/em&gt; &lt;code&gt;dl_hwcap&lt;/code&gt;&lt;/a&gt; with its own custom bitmask (defined in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/sysdeps/x86/dl-hwcap.h#L23-L25" target="_blank" rel="noopener"&gt;&lt;code&gt;sysdeps/x86/dl-hwcap.h&lt;/code&gt;&lt;/a&gt;: bit 0 = SSE2, bit 1 = &amp;quot;x86-64&amp;quot;, bit 2 = AVX512) instead of the kernel's proper CPUID feature bits. &lt;code&gt;dl_hwcap2&lt;/code&gt; is never touched though, so it still has the kernel value. This is what it looks like on my Debian:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# raw kernel value from /proc/self/auxv:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP = 0x000000000f8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# what getauxval hands you on the same machine:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getauxval(AT_HWCAP) = 0x0000000000000002&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So on x86-64, if you need the kernel's real &lt;code&gt;AT_HWCAP&lt;/code&gt; bitmask, &lt;code&gt;getauxval(AT_HWCAP)&lt;/code&gt; is a bad idea: use &lt;code&gt;/proc/self/auxv&lt;/code&gt; or find the &lt;code&gt;auxv&lt;/code&gt; on the stack yourself. For every other tag, the values are correct. And it's only a glibc thing: musl's &lt;code&gt;getauxval&lt;/code&gt; &lt;a href="https://git.musl-libc.org/cgit/musl/tree/src/misc/getauxval.c?h=v1.2.6#n8" target="_blank" rel="noopener"&gt;treats only &lt;code&gt;AT_SECURE&lt;/code&gt; specially&lt;/a&gt; and scans the &lt;code&gt;auxv&lt;/code&gt; for everything else, so there &lt;code&gt;getauxval(AT_HWCAP)&lt;/code&gt; returns the correct kernel bitmask, the same value &lt;code&gt;/proc/self/auxv&lt;/code&gt; shows.&lt;/p&gt;&#10;&lt;h2 id="method-4-prctlpr_get_auxv"&gt;Method 4: &lt;code&gt;prctl(PR_GET_AUXV)&lt;/code&gt;&lt;/h2&gt;&#10;&lt;p&gt;The bleeding edge of the kernel API (well, as of 2023): since Linux 6.4, you can ask the kernel for the &lt;code&gt;auxv&lt;/code&gt; directly, without &lt;code&gt;/proc&lt;/code&gt; (useful in a seccomp sandbox that blocks &lt;code&gt;openat&lt;/code&gt; calls but gently allows &lt;code&gt;prctl&lt;/code&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/prctl.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/prctl.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;elf.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prctl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PR_GET_AUXV&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It copies &lt;code&gt;min(sizeof(mm-&amp;gt;saved_auxv), your_len)&lt;/code&gt; bytes into your buffer and returns &lt;code&gt;sizeof(mm-&amp;gt;saved_auxv)&lt;/code&gt;, the size of the kernel's whole saved array, so you can see if your buffer was too small for this. On my box it returned &lt;code&gt;416&lt;/code&gt;, i.e. &lt;code&gt;26 * sizeof(Elf64_auxv_t)&lt;/code&gt;, because &lt;code&gt;saved_auxv&lt;/code&gt; is an array of &lt;a href="https://github.com/torvalds/linux/blob/v6.18/include/linux/mm_types.h#L30" target="_blank" rel="noopener"&gt;&lt;code&gt;AT_VECTOR_SIZE&lt;/code&gt;&lt;/a&gt; longs (52 on x86-64). That's a fixed size, not the number of entries actually used. And just like &lt;code&gt;/proc/self/auxv&lt;/code&gt;, it reads the kernel's &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;, so it gives you the same initial &lt;code&gt;execve&lt;/code&gt;-time snapshot. The implementation is &lt;code&gt;prctl_get_auxv()&lt;/code&gt; in &lt;a href="https://github.com/torvalds/linux/blob/v6.18/kernel/sys.c#L2463" target="_blank" rel="noopener"&gt;&lt;code&gt;kernel/sys.c&lt;/code&gt;&lt;/a&gt;, and it's quite compact.&lt;/p&gt;&#10;&lt;p&gt;And while we're here: there is also the flip side, if you need it, namely &lt;code&gt;prctl(PR_SET_MM, PR_SET_MM_AUXV, ...)&lt;/code&gt;. It lets a process with the &lt;code&gt;CAP_SYS_RESOURCE&lt;/code&gt; capability &lt;em&gt;overwrite&lt;/em&gt; the kernel's &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;. But only this saved copy, and the &lt;a href="https://github.com/torvalds/linux/blob/v6.18/kernel/sys.c#L2171-L2176" target="_blank" rel="noopener"&gt;kernel comment&lt;/a&gt; says it directly: &amp;quot;doesn't move the auxiliary vector itself since it's pinned to &lt;code&gt;mm_struct&lt;/code&gt;&amp;quot;. So it changes what &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;PR_GET_AUXV&lt;/code&gt; report, but the array on the stack and glibc's cached &lt;code&gt;dl_auxv&lt;/code&gt; pointer stay the same. In other words, it's a nasty way to make &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/auxv&lt;/code&gt; lie, which is a fun detail for anyone doing computer forensics and trusting results blindly.&lt;/p&gt;&#10;&lt;h2 id="method-5-from-the-entry-point-in-asm"&gt;Method 5: from the entry point, in asm&lt;/h2&gt;&#10;&lt;p&gt;Now the 1337 part. Beware, this is the shellcoders' case: you're running at (or near) &lt;code&gt;_start&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt; points to &lt;code&gt;argc&lt;/code&gt;, and you want the &lt;code&gt;auxv&lt;/code&gt; without any imports. Remember my ASCII picture from the beginning of the post: walk past &lt;code&gt;argc&lt;/code&gt;, past &lt;code&gt;argv&lt;/code&gt; and its &lt;code&gt;NULL&lt;/code&gt;, then scan &lt;code&gt;envp&lt;/code&gt; to its &lt;code&gt;NULL&lt;/code&gt;, and the &lt;code&gt;auxv&lt;/code&gt; starts right after it.&lt;/p&gt;&#10;&lt;p&gt;It's tempting to end this code with a &lt;code&gt;ret&lt;/code&gt; (as I did while testing), but then it suddenly segfaults. In proper entry-point shellcode, nobody actually called you, so there is no return address on the stack: &lt;code&gt;[rsp]&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; &lt;code&gt;argc&lt;/code&gt;, &lt;code&gt;ret&lt;/code&gt; pops this small integer into &lt;code&gt;rip&lt;/code&gt;, and you end up debugging your assembly skills. So yes, skip the &lt;code&gt;ret&lt;/code&gt;: you were jumped to from the kernel, not called, so there's no need to act like a function.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;; RSP -&amp;gt; argc. Falls through with RAX = &amp;amp;auxv[0]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; argc&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rsp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;rcx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;; &amp;amp;envp[0]: skip argc, argv[0..argc-1] and its NULL&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nl"&gt;.skip_env:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;lodsq&lt;/span&gt; &lt;span class="c1"&gt;; DF is clear at process entry (SysV ABI)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;test&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;jnz&lt;/span&gt; &lt;span class="nv"&gt;.skip_env&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;rsi&lt;/span&gt; &lt;span class="c1"&gt;; &amp;amp;auxv[0]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;; ... keep going, RAX is live&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The math is simple: &lt;code&gt;rsp&lt;/code&gt; points to &lt;code&gt;argc&lt;/code&gt;, so &lt;code&gt;argv&lt;/code&gt; starts at &lt;code&gt;rsp+8&lt;/code&gt;. Then there are &lt;code&gt;argc&lt;/code&gt; pointers plus a final &lt;code&gt;NULL&lt;/code&gt; (or &lt;code&gt;(argc+1)*8&lt;/code&gt; bytes), so in the end, &lt;code&gt;envp[0]&lt;/code&gt; is at &lt;code&gt;rsp + 8 + (argc+1)*8 = rsp + argc*8 + 16&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="method-6-from-envp-or-environ-in-c"&gt;Method 6: from &lt;code&gt;envp&lt;/code&gt; or &lt;code&gt;environ&lt;/code&gt;, in C&lt;/h2&gt;&#10;&lt;p&gt;The same idea as in Method 5, but in C, when you already have &lt;code&gt;envp&lt;/code&gt; from &lt;code&gt;main&lt;/code&gt; (or &lt;code&gt;__libc_start_main&lt;/code&gt; passed it to you):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;get_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There is also an official shortcut: if you don't have &lt;code&gt;envp&lt;/code&gt;, glibc exports the global &lt;code&gt;environ&lt;/code&gt; (&lt;code&gt;man 7 environ&lt;/code&gt;), so the code is the same. Well, that's &lt;em&gt;mostly&lt;/em&gt; true, but there's one thing.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;environ&lt;/code&gt; points to the kernel's on-stack &lt;code&gt;envp&lt;/code&gt; only until you add the first brand new variable. &lt;code&gt;setenv&lt;/code&gt;/&lt;code&gt;putenv&lt;/code&gt; (&lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c" target="_blank" rel="noopener"&gt;&lt;code&gt;stdlib/setenv.c&lt;/code&gt;&lt;/a&gt;) can't grow the array the kernel left on the stack, so on the first add, glibc &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L193" target="_blank" rel="noopener"&gt;copies it into its own heap array&lt;/a&gt; and &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L277" target="_blank" rel="noopener"&gt;points &lt;code&gt;environ&lt;/code&gt; to it&lt;/a&gt;. Overwriting an existing variable doesn't grow the array, so &lt;code&gt;environ&lt;/code&gt; stays where it was. And once it's on the heap, glibc allocates some extra space just in case and reuses that array, so it doesn't have to move again. But it never goes back to the stack. From that moment, &lt;code&gt;environ&lt;/code&gt; no longer points to the initial stack, the &lt;code&gt;auxv&lt;/code&gt; is nowhere near it, and the innocent-looking &lt;code&gt;get_auxv(environ)&lt;/code&gt; walks into the heap and returns garbage:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;at start: environ=0x7ffc4adb3cf8 (stack) get_auxv(environ) AT_PAGESZ=4096 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;after overwrite: environ=0x7ffc4adb3cf8 (stack) get_auxv(environ) AT_PAGESZ=4096 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;after add-new: environ=0x55666d24c6f0 (HEAP) get_auxv(environ) AT_PAGESZ=0 [truth=4096]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So use the &lt;code&gt;envp&lt;/code&gt; argument from &lt;code&gt;main&lt;/code&gt;/&lt;code&gt;_start&lt;/code&gt;, which still points to the original array on the stack. Fall back to &lt;code&gt;environ&lt;/code&gt; only if you have to, and only before anyone has added a new variable.&lt;/p&gt;&#10;&lt;p&gt;There's an even nastier version of this for setuid binaries, and it's related to &lt;a href="#method-1-ld_show_auxv1-the-shell-one-liner"&gt;Method 1&lt;/a&gt;. When &lt;code&gt;ld.so&lt;/code&gt; removes the &lt;code&gt;LD_*&lt;/code&gt; variables for a secure exec, it uses &lt;code&gt;unsetenv&lt;/code&gt;, which in glibc sadly compacts the array in place: &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/stdlib/setenv.c#L322" target="_blank" rel="noopener"&gt;it shifts the remaining pointers down&lt;/a&gt; and writes an earlier &lt;code&gt;NULL&lt;/code&gt; terminator, leaving stale slots between this new &lt;code&gt;NULL&lt;/code&gt; and the real start of the &lt;code&gt;auxv&lt;/code&gt;. After that, &amp;quot;walk &lt;code&gt;envp&lt;/code&gt; to the &lt;code&gt;NULL&lt;/code&gt; and get the next entry&amp;quot; fails and lands on a stale slot, not on &lt;code&gt;auxv[0]&lt;/code&gt;. Try running a root-owned setuid binary as a normal user with &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt; and &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; in its environment, and &lt;code&gt;get_auxv(envp)&lt;/code&gt; will return an &lt;code&gt;auxv&lt;/code&gt; whose first entry is &lt;code&gt;AT_NULL&lt;/code&gt;, i.e. an empty &lt;code&gt;auxv&lt;/code&gt;. But &lt;code&gt;/proc/self/auxv&lt;/code&gt;, &lt;code&gt;getauxval&lt;/code&gt; and the stack scanner still find the real one 16 bytes higher. So if you're reversing something that runs setuid, don't blindly trust the &lt;code&gt;envp&lt;/code&gt; walk: it can give you an empty &lt;code&gt;auxv&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;But this method still has one big advantage: you can run it before &lt;code&gt;main&lt;/code&gt;. &lt;code&gt;get_auxv&lt;/code&gt; only needs an &lt;code&gt;envp&lt;/code&gt;, and a function in the executable's &lt;code&gt;.init_array&lt;/code&gt; gets one before &lt;code&gt;main&lt;/code&gt; even starts. glibc calls every constructor with &lt;code&gt;(int argc, char **argv, char **envp)&lt;/code&gt; (see &lt;code&gt;call_init&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/csu/libc-start.c#L144" target="_blank" rel="noopener"&gt;&lt;code&gt;csu/libc-start.c&lt;/code&gt;&lt;/a&gt;, where the array has this nice-looking type: &lt;code&gt;void (*[])(int, char **, char **)&lt;/code&gt;). So a constructor like the one below is one of the earliest points in a normal (shellcode-free) program where you can read the &lt;code&gt;auxv&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;__attribute__&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;peek_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;av&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;envp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the walker above&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// inspect av before main(): AT_SECURE, AT_HWCAP, AT_RANDOM, ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;.preinit_array&lt;/code&gt; runs even earlier, before the normal constructors and with the same signature. But only in the main executable, never in a shared library.&lt;/p&gt;&#10;&lt;p&gt;Both run before &lt;code&gt;main&lt;/code&gt;, and both get the original &lt;code&gt;envp&lt;/code&gt; that is still on the stack (glibc gives those constructors &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/csu/libc-start.c#L359" target="_blank" rel="noopener"&gt;the global &lt;code&gt;__environ&lt;/code&gt;&lt;/a&gt;). So the &lt;code&gt;setenv&lt;/code&gt; trap doesn't apply here: no user code has run yet, so nobody could have moved your &lt;code&gt;environ&lt;/code&gt; to the heap. But the setuid case above still applies: &lt;code&gt;ld.so&lt;/code&gt; removes &lt;code&gt;LD_*&lt;/code&gt; during its own startup (&lt;code&gt;dl_main&lt;/code&gt; calls &lt;code&gt;process_envvars_secure&lt;/code&gt;, which calls &lt;code&gt;unsetenv&lt;/code&gt;, all before you even &lt;code&gt;_start&lt;/code&gt;), and as you already know, &lt;code&gt;unsetenv&lt;/code&gt; compacts the array in place instead of reallocating it. So on an &lt;code&gt;AT_SECURE&lt;/code&gt; binary, the &lt;code&gt;envp&lt;/code&gt; walk in a constructor hits the same stale &lt;code&gt;NULL&lt;/code&gt; and fails in the same way as in &lt;code&gt;main&lt;/code&gt;. Don't panic, you still have &lt;code&gt;/proc&lt;/code&gt;, &lt;code&gt;getauxval&lt;/code&gt;, &lt;code&gt;prctl&lt;/code&gt; or the stack scanner for those.&lt;/p&gt;&#10;&lt;p&gt;Passing &lt;code&gt;(argc, argv, envp)&lt;/code&gt; to constructors is actually glibc's own invention, not part of the C standard. So if you care about musl: &lt;a href="https://git.musl-libc.org/cgit/musl/tree/ldso/dynlink.c?h=v1.2.6#n1606" target="_blank" rel="noopener"&gt;it calls &lt;code&gt;.init_array&lt;/code&gt; entries without any arguments&lt;/a&gt; (the same goes for &lt;a href="https://git.musl-libc.org/cgit/musl/tree/src/env/__libc_start_main.c?h=v1.2.6#n64" target="_blank" rel="noopener"&gt;static binaries&lt;/a&gt;), and the three-argument constructor trick gets garbage there. On musl, use &lt;code&gt;environ&lt;/code&gt; inside the constructor instead (it's already valid that early, yes), or just call &lt;code&gt;getauxval&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="method-7-scan-the-stack-for-known-invariants"&gt;Method 7: scan the stack for known invariants&lt;/h2&gt;&#10;&lt;p&gt;The last resort. No &lt;code&gt;argc&lt;/code&gt;, no &lt;code&gt;envp&lt;/code&gt;, no libc, no &lt;code&gt;/proc&lt;/code&gt;, no &lt;code&gt;prctl&lt;/code&gt;: you just have a pointer somewhere on the main thread's stack, and you badly need the &lt;code&gt;auxv&lt;/code&gt;. Easy: it's near the top of that stack, so you only need to scan upward from wherever you are and recognize it by its content.&lt;/p&gt;&#10;&lt;p&gt;How do you recognize it? Two entries have the same value in every process on x86-64: &lt;code&gt;AT_PAGESZ&lt;/code&gt; is always &lt;code&gt;0x1000&lt;/code&gt; (4 KiB pages) and &lt;code&gt;AT_CLKTCK&lt;/code&gt; is always &lt;code&gt;100&lt;/code&gt; (&lt;code&gt;USER_HZ&lt;/code&gt;). These two values plus the edges of the &lt;code&gt;auxv&lt;/code&gt; (the &lt;code&gt;envp&lt;/code&gt; &lt;code&gt;NULL&lt;/code&gt; right before &lt;code&gt;auxv[0]&lt;/code&gt; and a clean &lt;code&gt;AT_NULL&lt;/code&gt; at the end) make a fingerprint that almost never appears by accident.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stddef.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;elf.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define PAGESZ 0x1000 &lt;/span&gt;&lt;span class="cm"&gt;/* x86-64 base pages */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define CLKTCK 100 &lt;/span&gt;&lt;span class="cm"&gt;/* USER_HZ */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define STACK_TOP 0x00007ffffffff000UL &lt;/span&gt;&lt;span class="cm"&gt;/* DEFAULT_MAP_WINDOW = (1&amp;lt;&amp;lt;47) - PAGE_SIZE */&lt;/span&gt;&lt;span class="cp"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Is p the real &amp;amp;auxv[0]? An auxv entry is just two 64-bit words. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;is_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* envp&amp;#39;s NULL sits just below auxv[0] */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* each entry: { a_type, a_val } */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* clean terminator + both anchors */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;AT_MINSIGSTKSZ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* 51 = largest real tag */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_PAGESZ&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;PAGESZ&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;anchors&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AT_CLKTCK&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CLKTCK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;/* Scan the main thread&amp;#39;s stack upward from any address below the auxv. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;find_auxv_by_scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="cm"&gt;/* auxv[0] is 8-byte aligned (not necessarily 16): round down, step one word. */&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)((&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;STACK_TOP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_auxv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Elf64_auxv_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Call it with the address of any local variable (&lt;code&gt;int x; find_auxv_by_scan(&amp;amp;x);&lt;/code&gt;), and it returns &lt;code&gt;&amp;amp;auxv[0]&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;What makes it work:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;scan upward: I suppose you remember that the stack grows down, which means your frame is &lt;em&gt;below&lt;/em&gt; the &lt;code&gt;auxv&lt;/code&gt; the kernel built near the top. So you need to walk toward higher addresses.&lt;/li&gt;&#10;&lt;li&gt;8-byte alignment: &lt;code&gt;auxv[0]&lt;/code&gt; is only guaranteed to be 8-byte aligned, not 16. (&lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L302-L303" target="_blank" rel="noopener"&gt;The 16-byte aligned landmark on the stack is &lt;code&gt;argc&lt;/code&gt;&lt;/a&gt;, because &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L149-L150" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_ROUND&lt;/code&gt;&lt;/a&gt; rounds the stack pointer down to 16 bytes, and the &lt;code&gt;auxv&lt;/code&gt; is a variable number of 8-byte words above it, so it's 16-byte aligned only about half the time.) That's why the code rounds the start down to 8 and steps by 8: with a 16-byte step, it would jump right over &lt;code&gt;auxv[0]&lt;/code&gt; every time it isn't 16-byte aligned.&lt;/li&gt;&#10;&lt;li&gt;false-positive guards: scanning up, you cross C stack frames and the &lt;code&gt;argv&lt;/code&gt;/&lt;code&gt;envp&lt;/code&gt; pointer arrays, so some places will look &lt;code&gt;auxv&lt;/code&gt;-ish. A place counts as the real &lt;code&gt;auxv[0]&lt;/code&gt; only when the word right below it is &lt;code&gt;0&lt;/code&gt; (&lt;code&gt;envp&lt;/code&gt;'s terminating &lt;code&gt;NULL&lt;/code&gt;) &lt;em&gt;and&lt;/em&gt; walking forward from it reaches a clean &lt;code&gt;AT_NULL&lt;/code&gt; with sane tags (&lt;code&gt;&amp;lt;= AT_MINSIGSTKSZ&lt;/code&gt;), including &lt;code&gt;AT_PAGESZ&lt;/code&gt; and &lt;code&gt;AT_CLKTCK&lt;/code&gt; with their known values. Random stack data practically never passes all of that. The first check also helps with a classic issue: a program that copied the &lt;code&gt;auxv&lt;/code&gt; onto its stack (say, with a &lt;code&gt;read&lt;/code&gt; from &lt;code&gt;/proc/self/auxv&lt;/code&gt;) leaves a &lt;em&gt;copy&lt;/em&gt; lower down, and the scan finds it first. A copy usually isn't preceded by a &lt;code&gt;0&lt;/code&gt; word, so it's skipped. Note the &amp;quot;usually&amp;quot; (and the &amp;quot;practically&amp;quot;) above: for instance, a copy in a zero-initialized buffer with a &lt;code&gt;0&lt;/code&gt; right before it is accepted instead of the real &lt;code&gt;auxv&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;There is also a ceiling: the scan stops at &lt;code&gt;STACK_TOP&lt;/code&gt;, which is &lt;code&gt;0x00007ffffffff000&lt;/code&gt; on x86-64. It's not a random constant I chose: it's the kernel's own &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/asm/page_64_types.h#L54-L69" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_TOP&lt;/code&gt;&lt;/a&gt; macro (&lt;code&gt;= DEFAULT_MAP_WINDOW = (1&amp;lt;&amp;lt;47) - PAGE_SIZE&lt;/code&gt;), the value &lt;a href="https://github.com/torvalds/linux/blob/v6.18/fs/binfmt_elf.c#L1028" target="_blank" rel="noopener"&gt;&lt;code&gt;fs/binfmt_elf.c&lt;/code&gt;&lt;/a&gt; passes to &lt;code&gt;randomize_stack_top()&lt;/code&gt; when it lays out the stack. With ASLR on, this function subtracts a random page-aligned offset (up to 16 GiB on x86-64, where &lt;a href="https://github.com/torvalds/linux/blob/v6.18/arch/x86/include/asm/elf.h#L328-L329" target="_blank" rel="noopener"&gt;&lt;code&gt;STACK_RND_MASK&lt;/code&gt;&lt;/a&gt; is 22 bits of pages), and the &lt;code&gt;[stack]&lt;/code&gt; mapping ends at that random top, not at &lt;code&gt;STACK_TOP&lt;/code&gt;. The two are the same only with ASLR off (&lt;code&gt;setarch -R&lt;/code&gt;, or under gdb, which disables it by default), and that's the familiar &lt;code&gt;...-7ffffffff000 [stack]&lt;/code&gt; line in gdb. So &lt;code&gt;STACK_TOP&lt;/code&gt; is an upper bound on where the &lt;code&gt;auxv&lt;/code&gt; can be, not the end of the mapping. And it's not a safety net either: above the real top of &lt;code&gt;[stack]&lt;/code&gt; there is unmapped memory, so a scan that misses the &lt;code&gt;auxv&lt;/code&gt; crashes or walks into unrelated pages long before it reaches &lt;code&gt;STACK_TOP&lt;/code&gt;. On the main thread this never happens, because a correct scan finds the real &lt;code&gt;auxv&lt;/code&gt; inside the mapping first, and the ceiling only keeps the loop bounded on paper. If you need a precise bound, read the real end of &lt;code&gt;[stack]&lt;/code&gt; from &lt;code&gt;/proc/self/maps&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;And one damn hard limitation: this only works on the main thread, unfortunately. A thread created with &lt;code&gt;clone&lt;/code&gt; runs on a new stack without &lt;code&gt;argc&lt;/code&gt;/&lt;code&gt;argv&lt;/code&gt;/&lt;code&gt;envp&lt;/code&gt;/&lt;code&gt;auxv&lt;/code&gt;, far below the &lt;code&gt;0x7fff...&lt;/code&gt; ceiling. So the scan would run past the top of that stack (into whatever is mapped above it, or into unmapped memory) long before it reaches &lt;code&gt;STACK_TOP&lt;/code&gt;. If you really need to do this from another thread, find the main thread's &lt;code&gt;[stack]&lt;/code&gt; in &lt;code&gt;/proc/self/maps&lt;/code&gt; and scan only inside it, starting from its lower end.&lt;/p&gt;&#10;&lt;p&gt;So it's really the last resort. The fingerprint is strong, and the &lt;code&gt;envp&lt;/code&gt; &lt;code&gt;NULL&lt;/code&gt; check helps with the usual copy problem, but it's still a heuristic over raw memory, and an unlucky (or adversarial) layout can fool it. If any of Methods 1-6 is available, use it instead.&lt;/p&gt;&#10;&lt;h2 id="when-the-api-can-lie"&gt;When the API can lie&lt;/h2&gt;&#10;&lt;p&gt;Remember the two copies of the &lt;code&gt;auxv&lt;/code&gt; from &lt;a href="#method-2-read-procselfauxv-directly"&gt;Method 2&lt;/a&gt;? &lt;code&gt;/proc/self/auxv&lt;/code&gt; and &lt;code&gt;prctl(PR_GET_AUXV)&lt;/code&gt; read the kernel's snapshot in &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt;, while &lt;code&gt;getauxval&lt;/code&gt; reads the copy on the stack, which &lt;code&gt;ld.so&lt;/code&gt; can change. Usually they are the same, but there is one easy way to make them different: run the dynamic loader explicitly.&lt;/p&gt;&#10;&lt;p&gt;Normally you run &lt;code&gt;./prog&lt;/code&gt;: the kernel sees the &lt;code&gt;PT_INTERP&lt;/code&gt; entry, maps &lt;code&gt;ld.so&lt;/code&gt; for you, and fills the &lt;code&gt;auxv&lt;/code&gt; to describe &lt;code&gt;./prog&lt;/code&gt; (&lt;code&gt;AT_PHDR&lt;/code&gt;/&lt;code&gt;AT_PHNUM&lt;/code&gt;/&lt;code&gt;AT_ENTRY&lt;/code&gt; point to the program, and &lt;code&gt;AT_EXECFN&lt;/code&gt; is its path). But you can also run &lt;code&gt;ld.so ./prog&lt;/code&gt;, i.e. run the loader itself as the program, with &lt;code&gt;./prog&lt;/code&gt; as its argument. Now the executed file &lt;em&gt;is&lt;/em&gt; &lt;code&gt;ld.so&lt;/code&gt;, so the kernel fills the &lt;code&gt;auxv&lt;/code&gt; to describe &lt;code&gt;ld.so&lt;/code&gt; itself: &lt;code&gt;AT_PHDR&lt;/code&gt;/&lt;code&gt;AT_PHNUM&lt;/code&gt;/&lt;code&gt;AT_ENTRY&lt;/code&gt; point to the loader, and &lt;code&gt;AT_EXECFN&lt;/code&gt; is &lt;code&gt;/lib64/ld-linux-x86-64.so.2&lt;/code&gt;. When &lt;code&gt;ld.so&lt;/code&gt; finally loads &lt;code&gt;./prog&lt;/code&gt;, it makes the process look like &lt;code&gt;./prog&lt;/code&gt; was executed directly. To make it work, it walks the &lt;code&gt;auxv&lt;/code&gt; on the stack and rewrites (yes) those four tags (&lt;code&gt;AT_PHDR&lt;/code&gt;, &lt;code&gt;AT_PHNUM&lt;/code&gt;, &lt;code&gt;AT_ENTRY&lt;/code&gt;, &lt;code&gt;AT_EXECFN&lt;/code&gt;) to point to the real program. This loop is in &lt;code&gt;dl_main&lt;/code&gt; in &lt;a href="https://github.com/gnutools/glibc/blob/release/2.44/master/elf/rtld.c#L1610" target="_blank" rel="noopener"&gt;&lt;code&gt;elf/rtld.c&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;But it patches only the copy on the stack. &lt;code&gt;mm-&amp;gt;saved_auxv&lt;/code&gt; is never touched, so &lt;code&gt;/proc/self/auxv&lt;/code&gt; and &lt;code&gt;PR_GET_AUXV&lt;/code&gt; still describe &lt;code&gt;ld.so&lt;/code&gt;, while &lt;code&gt;getauxval&lt;/code&gt;, which reads the patched copy on the stack, describes &lt;code&gt;./prog&lt;/code&gt;. Weird, I know. Same process, same tags, two different answers:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; /lib64/ld-linux-x86-64.so.2 ./ldso_auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;argv[0] = ./ldso_auxv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;tag /proc/self/auxv (kernel) on-stack (ld.so&amp;#39;s copy)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SYSINFO_EHDR 33 0x7f7310c2f000 0x7f7310c2f000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_MINSIGSTKSZ 51 1776 1776&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP 16 0xf8bfbff 0xf8bfbff&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PAGESZ 6 4096 4096&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_CLKTCK 17 100 100&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHDR 3 0x7f7310c31040 0x7f7310c26040 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHENT 4 56 56&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PHNUM 5 9 14 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_BASE 7 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_FLAGS 8 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_ENTRY 9 0x7f7310c4e2c0 0x7f7310c27100 &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_UID 11 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EUID 12 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_GID 13 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EGID 14 1000 1000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_SECURE 23 0 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RANDOM 25 0x7fffc881c749 0x7fffc881c749&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_HWCAP2 26 2 2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_EXECFN 31 /lib64/ld-linux-x86-64.so.2 ./ldso_auxv &amp;lt;- differs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_PLATFORM 15 0x7fffc881c759 0x7fffc881c759&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_FEATURE_SIZE 27 28 28&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;AT_RSEQ_ALIGN 28 32 32&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;4 of 22 tags differ&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Run the same binary the normal way (&lt;code&gt;./prog&lt;/code&gt;), and the two copies match to the byte. In other words, the &lt;code&gt;auxv&lt;/code&gt; is actually two things: the kernel's snapshot and a copy on the stack that the loader is allowed to edit. And the method you choose decides which one you see.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item><item><title>"Kernel Monarch" by Hack The Box</title><link>https://pugachev.io/2026/06/05/kernel-monarch-by-hack-the-box/</link><pubDate>Fri, 05 Jun 2026 13:36:46 +0100</pubDate><guid>https://pugachev.io/2026/06/05/kernel-monarch-by-hack-the-box/</guid><description>&lt;p&gt;This is a nice, compact, and straightforward way to brush up on basic Linux rootkits. The module includes full debugging information, so you won't have to spend much time working in IDA.&lt;/p&gt;&#10;&lt;p&gt;You get a loadable kernel module (LKM) with a modified flag: the file you have is not the one running on the host. The module hides itself from &lt;code&gt;lsmod&lt;/code&gt; in a simple way and hooks the &lt;code&gt;read&lt;/code&gt;, &lt;code&gt;write&lt;/code&gt;, and &lt;code&gt;syslog&lt;/code&gt; system calls, so neither &lt;code&gt;cat&lt;/code&gt; nor &lt;code&gt;dmesg&lt;/code&gt; shows the data you need.&lt;/p&gt;&#10;&lt;p&gt;The module hides its traces in a few files (&lt;code&gt;/proc/kallsyms&lt;/code&gt; and ftrace's function lists, for example), but the only interesting one is &lt;code&gt;/dev/kmsg&lt;/code&gt;, the kernel log buffer. So you need a way to read it unfiltered.&lt;/p&gt;&#10;&lt;p&gt;Luckily, the module only hooks the 64-bit &lt;code&gt;read&lt;/code&gt;, so other ways to read a file still work: &lt;code&gt;readv&lt;/code&gt;, &lt;code&gt;pread64&lt;/code&gt;, or even the 32-bit &lt;code&gt;read&lt;/code&gt;. GCC is installed on the host, so the task boils down to writing a small C program, compiling it, and using it to read the messages.&lt;/p&gt;&#10;&lt;p&gt;Have fun!&lt;/p&gt;&#10;&lt;p&gt;You can find the challenge &lt;a href="https://app.hackthebox.com/challenges/Kernel%20Monarch" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>No more pwn.college writeups</title><link>https://pugachev.io/2026/01/09/no-more-pwn-college-writeups/</link><pubDate>Fri, 09 Jan 2026 06:48:48 +0100</pubDate><guid>https://pugachev.io/2026/01/09/no-more-pwn-college-writeups/</guid><description>&lt;p&gt;Sorry to those unknown people who suddenly made my sweet little blog popular somewhere, but I've deleted all my pwn.college writeups. It turns out I violated the platform's rules (who even reads the rules, right?), and I've been politely asked not to do it again.&lt;/p&gt;&#10;&lt;p&gt;I really appreciate the work of the pwn.college team, so no more writeups.&lt;/p&gt;&#10;&lt;p&gt;Happy hacking!&lt;/p&gt;&#10;</description></item><item><title>"Hexecution" by Hack The Box</title><link>https://pugachev.io/2025/11/03/hexecution-by-hack-the-box/</link><pubDate>Mon, 03 Nov 2025 15:54:17 +0100</pubDate><guid>https://pugachev.io/2025/11/03/hexecution-by-hack-the-box/</guid><description>&lt;p&gt;Another day, another toy virtual machine challenge from HTB. It doesn't seem complicated, so I'll keep my story short.&lt;/p&gt;&#10;&lt;p&gt;This challenge even provides the source code as a text file, so there's no need to search for it in the binary. It looks strange at first glance, but it's the same assembly as always: &lt;code&gt;mov&lt;/code&gt;, &lt;code&gt;xor&lt;/code&gt;, &lt;code&gt;cmp&lt;/code&gt;, and so on, just &amp;quot;encrypted&amp;quot; with funny words like &lt;code&gt;BOIL&lt;/code&gt; or &lt;code&gt;QUICKMAFFS&lt;/code&gt;. It reminded me of my school days, when I found a cracked version of MS Visual Basic translated into Russian, including the programming language itself. So I had to literally write Visual Basic code using Russian words. Strange experience, but it was fun.&lt;/p&gt;&#10;&lt;p&gt;The virtual machine itself has 12 instructions (one of which is not used in the provided source code), 6 registers, and an unusual &amp;quot;stack&amp;quot; implementation, which I'll leave for you to figure out. To get you started with the internal structure, I'll say that the stack grows up, and the SP register is &amp;quot;hardcoded&amp;quot; as the last element of the array used for emulation. This is funny in itself because you could try to write an exploit that breaks either the virtual stack or the underlying array, since neither is bounds-checked.&lt;/p&gt;&#10;&lt;p&gt;As usual, I wrote a small disassembler, but now I call it a &amp;quot;translator.&amp;quot; Because I didn't disassemble anything, right? I'm translating a strange language into my strange assembly code. And, as usual, I recommend trying to solve the challenge first, and only using my code when you get stuck.&lt;/p&gt;&#10;&lt;p&gt;The resulting translated program doesn't look very complicated: it contains a hardcoded flag encrypted using 2 simple permutations that can be reversed in two minutes.&lt;/p&gt;&#10;&lt;p&gt;Have fun!&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Hexecution" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and my awesome translator with the source code &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/hexecution" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Rega's Town" by Hack The Box</title><link>https://pugachev.io/2025/07/06/regas-town-by-hack-the-box/</link><pubDate>Sun, 06 Jul 2025 10:42:07 +0100</pubDate><guid>https://pugachev.io/2025/07/06/regas-town-by-hack-the-box/</guid><description>&lt;p&gt;Rust is a special language in many ways. As someone who has been a software developer for quite some time, I would say that the idea is good, but the implementation is, to put it mildly, unique. And I don't like it.&lt;/p&gt;&#10;&lt;p&gt;Trying to solve various challenges written in Rust just confirms my opinion: the structure of the internals, the generated code, and other low-level things are, well, special. The difficulty of such challenges is usually in Rust itself, not in the logic that needs to be understood and reversed. So, no, I definitely don't like it.&lt;/p&gt;&#10;&lt;p&gt;This challenge from HTB is exactly what I expected. It primarily tests your knowledge of regular expressions and requires you to write a small script to brute-force the desired outcome. But to understand what it does, you need to get through all the weird code that the Rust compiler generates. Luckily, the latest versions of IDA are very helpful.&lt;/p&gt;&#10;&lt;p&gt;In short, the challenge consists of 2 stages: first, it &amp;quot;filters&amp;quot; the input based on 9 regular expressions, which are executed one after the other:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;1: ^.{33}$&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;2: (?:^[\x48][\x54][\x42]).*&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;3: ^.{3}(\x7b).*(\x7d)$&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;4: ^[[:upper:]]{3}.[[:upper:]].{3}[[:upper:]].{3}[[:upper:]].{3}[[:upper:]].{4}[[:upper:]].{2}[[:upper:]].{3}[[:upper:]].{4}$&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;5: (?:.*\x5f.*)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;6: (?:.[^0-9]*\d.*){5}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;7: .{24}\x54.\x65.\x54.*&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;8: ^.{4}[X-Z]\d._[A]\D\d.................[[:upper:]][n-x]{2}[n|c].$&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;9: .{11}_T[h|7]\d_[[:upper:]]\dn[a-h]_[O]\d_[[:alpha:]]{3}_.{5}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, they already tell you a lot about what the flag should look like, and even give away some of its characters.&lt;/p&gt;&#10;&lt;p&gt;The second stage is to &amp;quot;check&amp;quot; the input. Once you make it through the generated code with all its iterators, ranges, and iterator products, the basic idea is pretty simple: the unknown flag part (which is inside &lt;code&gt;HTB{}&lt;/code&gt;) is split into 7 parts by the &lt;code&gt;_&lt;/code&gt; character. To check that each part is correct, the characters in each part are multiplied, and the results are compared to a hardcoded constant. In other words, if one of the parts is &lt;code&gt;abc&lt;/code&gt;, the check will do something like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;some_hardcoded_const&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Given that the 9 regular expressions only give away part of the answer, you will have to brute-force each part, which is not that hard, since the regular expressions significantly narrow down the possible answers.&lt;/p&gt;&#10;&lt;p&gt;Have fun!&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Rega%27s%20Town" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"vvm" by Hack The Box</title><link>https://pugachev.io/2025/04/11/vvm-by-hack-the-box/</link><pubDate>Fri, 11 Apr 2025 17:15:44 +0100</pubDate><guid>https://pugachev.io/2025/04/11/vvm-by-hack-the-box/</guid><description>&lt;p&gt;This is not the first HTB challenge I've solved that uses a virtual machine, and I have to say that &lt;a href="https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/"&gt;vmcrack&lt;/a&gt; was much more interesting, challenging, and consistent in implementation than this one. In any case, it was quite fun to solve because of the various stack and bitwise exercises you had to deal with.&lt;/p&gt;&#10;&lt;p&gt;But let's start from the beginning. It's a custom virtual machine implementation with 29 different opcodes, and everything is stack-based. There are no registers as such, and the stack is used to store the results of calculations.&lt;/p&gt;&#10;&lt;p&gt;Each opcode is 4 bytes long and can have 0 to 2 operands, which are also 4 bytes long. So the encoding is pretty simple: no need to deal with bits, different sizes, conditions and so on. Just read the first 4 bytes and then decide, based on that number, how many operands to read. But the flip side of this simplicity is that each opcode can do anything from a simple jump or a push/pop to crazy things like allocating a block of memory and reading a line from stdin, or (my favorite) deleting a newline from a buffer.&lt;/p&gt;&#10;&lt;p&gt;To make things easier, I wrote a small disassembler that decodes the VM code into what I call &amp;quot;pseudo-assembly&amp;quot;, so don't try to compile it back. I used the &lt;code&gt;r1&lt;/code&gt;, &lt;code&gt;r2&lt;/code&gt;, and &lt;code&gt;sp&lt;/code&gt; registers to make it more readable, but again, the original code doesn't have registers.&lt;/p&gt;&#10;&lt;p&gt;As for the inconsistencies, please note that the &lt;code&gt;jmp&lt;/code&gt; instruction works a little weirdly: it counts the bytes to the destination from its operand, not from the next instruction. So if you decode it like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="nf"&gt;b&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;jmp&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mh"&gt;0x10&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;the 4 bytes &lt;code&gt;0x00000010&lt;/code&gt; will be included in the offset.&lt;/p&gt;&#10;&lt;p&gt;Another funny thing is that, according to the HTB changelog, the author updated the challenge. I had the previous version, and it is a bit more complex at the last step of checking the flag. Well, it happens.&lt;/p&gt;&#10;&lt;p&gt;The opcode logic is encrypted in the binary, but the xor (of course) code is the same everywhere, so you can decrypt it with the following IDC script:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x2a4d21210b2a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;function_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;function_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can read the disassembler output I left on GitHub to get the full picture (I even commented it a bit), but basically, it looks like this:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Read the flag from stdin.&lt;/li&gt;&#10;&lt;li&gt;Check the size of the input data.&lt;/li&gt;&#10;&lt;li&gt;Compute 8 integers (4 bytes each) using not-so-complicated bitwise logic. Each of these integers contains 4 characters from the original input, encoded in some way.&lt;/li&gt;&#10;&lt;li&gt;Validate that the resulting integers match the numbers hardcoded into the virtual machine code (or calculated dynamically in a previous version).&lt;/li&gt;&#10;&lt;li&gt;Print the validation result to stdout.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The fun begins with checking the size of the input data. The virtual machine tries to validate it using the following complex mathematical equation:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(((((((x * 3) - 6) * 2 + 4) / 2) * 8) + 24) / 10) = 76&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;but in fact, you can skip it simply because before that, it allocates a buffer exactly the same size as the input should be.&lt;/p&gt;&#10;&lt;p&gt;It then encodes groups of 4 characters using the following logic:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Take 4 characters from the input string. For example, the first integer will contain characters from positions &lt;code&gt;0x18&lt;/code&gt;, &lt;code&gt;0x0E&lt;/code&gt;, &lt;code&gt;0x1B&lt;/code&gt; and &lt;code&gt;0x0F&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;Group them into a single integer using standard bitwise logic: &lt;code&gt;(c0 | c1 &amp;lt;&amp;lt; 0x08) | (c2 &amp;lt;&amp;lt; 0x10) | (c3 &amp;lt;&amp;lt; 0x18)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;For each group, rotate the result left by &lt;code&gt;Z&lt;/code&gt; bits: &lt;code&gt;(previous_step_value &amp;lt;&amp;lt; Z) | (previous_step_value &amp;gt;&amp;gt; (0x20 - Z))&lt;/code&gt;, where &lt;code&gt;Z&lt;/code&gt; is chosen for each group separately.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;So, for example, for the group &lt;code&gt;ABCD&lt;/code&gt; the following will be done:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step1: group1 = &amp;#39;ABCD&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step2: round1 = (&amp;#39;A&amp;#39; | &amp;#39;B&amp;#39; &amp;lt;&amp;lt; 0x08) | (&amp;#39;C&amp;#39; &amp;lt;&amp;lt; 0x10) | (&amp;#39;D&amp;#39; &amp;lt;&amp;lt; 0x18)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step3: Z = 0x05&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step4: round2 = (round1 &amp;lt;&amp;lt; 0x05) | (round1 &amp;gt;&amp;gt; 0x1B)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then it compares &lt;code&gt;round2&lt;/code&gt; with the hardcoded value to check if the input is correct.&lt;/p&gt;&#10;&lt;p&gt;To reverse the algorithm, as usual, you need to go in the opposite direction:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Get the hardcoded value (which is our &lt;code&gt;round2&lt;/code&gt;) and &lt;code&gt;Z&lt;/code&gt; from the VM code.&lt;/li&gt;&#10;&lt;li&gt;Restore &lt;code&gt;round1&lt;/code&gt; by rotating the value right by &lt;code&gt;Z&lt;/code&gt; bits.&lt;/li&gt;&#10;&lt;li&gt;Put the 4 characters from &lt;code&gt;round1&lt;/code&gt; back into their original positions.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Well, and that's all.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/vvm" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and my awesome IDA-killer and the VM code can be found &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vvm" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Wayback" by Hack The Box</title><link>https://pugachev.io/2025/04/06/wayback-by-hack-the-box/</link><pubDate>Sun, 06 Apr 2025 17:39:35 +0100</pubDate><guid>https://pugachev.io/2025/04/06/wayback-by-hack-the-box/</guid><description>&lt;p&gt;This is a basic exercise based on the &lt;code&gt;srand&lt;/code&gt; + &lt;code&gt;rand&lt;/code&gt; pair and the textbook fact that what &lt;code&gt;rand&lt;/code&gt; returns depends on the seed passed to &lt;code&gt;srand&lt;/code&gt;. The only small problem here is that there is no logic to reveal the original flag, so you need to brute-force the solution.&lt;/p&gt;&#10;&lt;p&gt;There are 2 files in the challenge: a binary that generates a password based on 3 rules, and a Python script that decrypts a hardcoded binary blob containing the flag. Your task is to find the correct password that will decrypt this blob.&lt;/p&gt;&#10;&lt;p&gt;The logic behind the password generation is simple. First, it prepares the set of characters for the password based on the 3 questions it asks: the length of the password and whether to include numbers or &amp;quot;symbols&amp;quot;. It then generates the password using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;libc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;srand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;srand_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# calculated from the local time, more on this below&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password_length&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;character_set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;libc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;character_set&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The main question is: when exactly was the password generated? Fortunately, the author of the challenge is very kind and tells us the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&amp;quot;password is 20 characters long, and consisted of only alphanumeric characters and symbols&amp;quot;&lt;/li&gt;&#10;&lt;li&gt;&amp;quot;between the 10th and the 11th of December 2013&amp;quot;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Basically, that's all we need to brute-force it using code like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strptime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;10.12.2013 00:00:00 UTC&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s2"&gt;.%m.%Y %H:%M:%S %Z&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calendar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timegm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;libc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;srand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;srand_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# calculated from t, see below&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password_length&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;character_set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;libc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;character_set&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;decrypted_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;decrypted_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;Decrypted message: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;decrypted_message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;break&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gmtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The logic for generating &lt;code&gt;srand_value&lt;/code&gt; can be found by disassembling the binary, and it is not very complicated: it glues the date and time into one decimal number, &lt;code&gt;YYYYMMDDhhmmss&lt;/code&gt;, which is then truncated to int32.&lt;/p&gt;&#10;&lt;p&gt;There are a few points worth noting:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The binary is compiled with a recent GCC (14.2), so it didn't work under my Debian 12: its libstdc++ is too old (the binary needs &lt;code&gt;GLIBCXX_3.4.32&lt;/code&gt;).&lt;/li&gt;&#10;&lt;li&gt;The logic that generates &lt;code&gt;srand_value&lt;/code&gt; uses int32, so pay attention to this when porting the algorithm to Python or another language.&lt;/li&gt;&#10;&lt;li&gt;BSD &lt;code&gt;rand&lt;/code&gt; is not the same as glibc &lt;code&gt;rand&lt;/code&gt;, and both will return different values given the same &lt;code&gt;srand&lt;/code&gt; input value. Be aware of this if you try to brute-force it on, say, macOS.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Wayback" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"BinCrypt Breaker" by Hack The Box</title><link>https://pugachev.io/2025/04/03/bincrypt-breaker-by-hack-the-box/</link><pubDate>Thu, 03 Apr 2025 16:48:02 +0100</pubDate><guid>https://pugachev.io/2025/04/03/bincrypt-breaker-by-hack-the-box/</guid><description>&lt;p&gt;Wow, it's been a year since my last post. Long time no see, so to speak.&lt;/p&gt;&#10;&lt;p&gt;Anyway, I've finally managed to (hopefully) get back into reversing, and I want to start with something not too difficult to refresh my skills. Hack The Box has always been a great place for a good challenge, and it seems to still be that way.&lt;/p&gt;&#10;&lt;p&gt;The &amp;quot;crypt&amp;quot; part of the challenge's name is a bit of an exaggeration, but if you believe that &lt;code&gt;xor&lt;/code&gt; and &lt;code&gt;swap&lt;/code&gt; must be in any good crypto algorithm, then so be it. It all starts, of course, with good old &lt;code&gt;xor&lt;/code&gt;: the &lt;code&gt;checker&lt;/code&gt; binary &amp;quot;decrypts&amp;quot; &lt;code&gt;file.bin&lt;/code&gt; using the complex &lt;code&gt;^0xAB&lt;/code&gt; method and executes the resulting binary.&lt;/p&gt;&#10;&lt;p&gt;The decrypted thing is also not very complicated, but it still does a little more than &lt;code&gt;xor&lt;/code&gt;. The whole algorithm transforms the input string as follows:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Swap 8 characters at specific positions.&lt;/li&gt;&#10;&lt;li&gt;Split the resulting string into 2 halves.&lt;/li&gt;&#10;&lt;li&gt;Shuffle each half and xor 6 of its characters with a key (&lt;code&gt;2&lt;/code&gt; for the left half and &lt;code&gt;3&lt;/code&gt; for the right one).&lt;/li&gt;&#10;&lt;li&gt;Combine the 2 halves and compare the result with &lt;code&gt;RV{r15]_vcP3o]L_tazmfSTaa3s0&lt;/code&gt;. If they are equal, you have found the correct flag.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Given that this is a reversing exercise, you need to, well, reverse everything, including the steps you need to take to get the original flag out of that weird string. Specifically, you need to do something like this:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Split the encrypted flag into 2 halves.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;xor&lt;/code&gt; the 6 characters in each half using the same keys, and shuffle the result using the reversed algorithm. &amp;quot;Reversed&amp;quot; in the sense that you need to do everything in reverse order, not that you need to fire up IDA or another disassembler.&lt;/li&gt;&#10;&lt;li&gt;Combine the 2 halves and swap the 8 characters back.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The &lt;code&gt;shuffle&lt;/code&gt; algorithm uses a hardcoded array that says where each character of the result comes from: &lt;code&gt;[9, 12, 2, 10, 4, 1, 6, 3, 8, 5, 7, 11, 0, 13]&lt;/code&gt;. The algorithm is quite simple:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shuffle_array&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;shuffle_array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To reverse it, you need the inverse of this array: &lt;code&gt;0&lt;/code&gt; goes to index 9, &lt;code&gt;1&lt;/code&gt; goes to index 12, and so on.&lt;/p&gt;&#10;&lt;p&gt;Simple but cute for recharging forgotten skills.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/BinCrypt%20Breaker" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"ARMs Race" by Hack The Box</title><link>https://pugachev.io/2024/04/16/arms-race-by-hack-the-box/</link><pubDate>Tue, 16 Apr 2024 12:12:18 +0100</pubDate><guid>https://pugachev.io/2024/04/16/arms-race-by-hack-the-box/</guid><description>&lt;p&gt;I usually skip &amp;quot;reversing&amp;quot; challenges when the task doesn't require reverse engineering at all. But this one caught my attention because of what it can teach you.&lt;/p&gt;&#10;&lt;p&gt;Roughly speaking, this is a programming exercise: the remote server sends you ARM code as a hexadecimal string, and your job is to tell what is in the &lt;code&gt;r0&lt;/code&gt; register after that code is executed. The problem is that the code itself is long and contains non-trivial bit manipulation and some math. Unfortunately, you have about 5 seconds to answer each question, and there are 50 questions that change every time you connect to the server.&lt;/p&gt;&#10;&lt;p&gt;In other words, if you can't mentally xor dwords in milliseconds, some kind of automation is required here. And that's the part that interested me.&lt;/p&gt;&#10;&lt;p&gt;Long story short, today I learned about the &lt;a href="https://www.unicorn-engine.org/" target="_blank" rel="noopener"&gt;Unicorn&lt;/a&gt; framework. It's a project by the great guys who created &lt;a href="https://www.capstone-engine.org/" target="_blank" rel="noopener"&gt;Capstone&lt;/a&gt; (or at least the main developers worked on both of them), and it allows you to emulate different CPUs in software. It's crazy: it's based on QEMU, can emulate 10 architectures, has bindings for 17 languages, and is free.&lt;/p&gt;&#10;&lt;p&gt;For this task, you only need 5 lines of code to answer any question:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;emu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Uc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UC_ARCH_ARM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;UC_MODE_ARM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;emu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mem_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;EMU_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;emu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mem_write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;EMU_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytecode&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;emu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emu_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;EMU_ADDRESS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;EMU_ADDRESS&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytecode&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;emu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reg_read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UC_ARM_REG_R0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, you also need something to communicate with the server and send data back and forth, but the interesting part is just these 5 lines. Amazing!&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/ARMs%20Race" target="_blank" rel="noopener"&gt;here&lt;/a&gt;, and the solution &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/arms_race" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"vmcrack" by Hack The Box</title><link>https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/</link><pubDate>Sun, 14 Apr 2024 15:18:36 +0100</pubDate><guid>https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/</guid><description>&lt;p&gt;It seems that custom virtual machines are the trend in CTF challenges now, and this task has one too. The only thing is that the crackmes I've seen so far did this, let's say, at an amateur level. But this example is really cool: it's a full implementation that supports and interprets a subset of the x86 architecture using 65 opcodes, 32/16/8-bit registers, various addressing modes including full Base/Index/Scale/Displacement mode, and more. It can even use the &lt;code&gt;fs&lt;/code&gt; segment register!&lt;/p&gt;&#10;&lt;p&gt;To make life a bit more interesting, the challenge has a few anti-debugging techniques, so be prepared if you're going to debug it. I didn't, because I usually prefer to do things statically, but it's interesting to see all these ideas implemented in one binary anyway. I counted 9 different ways to prevent you from debugging it, but there are probably more:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/tls-callback/" target="_blank" rel="noopener"&gt;TLS Callbacks&lt;/a&gt;. There are 3 of them, but they have the same idea: the code finds the address of the desired function using the &amp;quot;PEB technique&amp;quot; and calls it. The function names are hidden using xor with a hardcoded constant that is different for each callback.&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/checkremotedebuggerpresent/" target="_blank" rel="noopener"&gt;CheckRemoteDebuggerPresent&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ntqueryinformationprocess/" target="_blank" rel="noopener"&gt;NtQueryInformationProcess&lt;/a&gt;, but with the &lt;code&gt;ProcessDebugObjectHandle&lt;/code&gt; class&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ntsetinformationthread/" target="_blank" rel="noopener"&gt;NtSetInformationThread&lt;/a&gt; with the &lt;code&gt;ThreadHideFromDebugger&lt;/code&gt; class&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ice-0xf1/" target="_blank" rel="noopener"&gt;icebp instruction&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;PEB's &lt;a href="https://unprotect.it/technique/isdebugged-flag/" target="_blank" rel="noopener"&gt;BeingDebugged&lt;/a&gt; flag&lt;/li&gt;&#10;&lt;li&gt;PEB's &lt;a href="https://unprotect.it/technique/ntglobalflag/" target="_blank" rel="noopener"&gt;NtGlobalFlag&lt;/a&gt; field&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/heap-flag/" target="_blank" rel="noopener"&gt;ProcessHeap&lt;/a&gt; flags&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/trap-flag/" target="_blank" rel="noopener"&gt;Trap Flag&lt;/a&gt; in the &lt;code&gt;EFLAGS&lt;/code&gt; register&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://anti-debug.checkpoint.com/techniques/assembly.html#int3" target="_blank" rel="noopener"&gt;int3&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Speaking of TLS callbacks, there is another one that is called first and uses the &lt;code&gt;IsWow64Process2&lt;/code&gt; function to check that the process is running under &lt;a href="https://learn.microsoft.com/en-us/windows/win32/winprog64/running-32-bit-applications" target="_blank" rel="noopener"&gt;WOW64&lt;/a&gt;. If it isn't, or the machine isn't &lt;code&gt;IMAGE_FILE_MACHINE_I386&lt;/code&gt;/&lt;code&gt;AMD64&lt;/code&gt;, it will not start. By the way, &lt;code&gt;vmcrack&lt;/code&gt; is a 32-bit binary that emulates x86, so this check kind of makes sense.&lt;/p&gt;&#10;&lt;p&gt;Now let's dive into the actual implementation of the virtual machine. The whole code uses a single function to run it, and it receives only 2 parameters: the bytecode to be executed and a pointer to the memory block that the machine will use. The memory block size is always the same: &lt;code&gt;0x10000&lt;/code&gt; for the memory itself and &lt;code&gt;0x2C&lt;/code&gt; for the registers.&lt;/p&gt;&#10;&lt;p&gt;The internal implementation is confusing, and I'm guessing intentionally: there are no separate functions that emulate instructions. It's more like a &lt;a href="https://en.wikipedia.org/wiki/Stack_machine" target="_blank" rel="noopener"&gt;stack machine&lt;/a&gt; that performs individual steps during emulation, such as resolving the opcode, parsing the instruction parameters, handling the addressing modes, and so on. Each step gets data from the stack and returns the result to the stack so that the next one can do its job too. Eventually, when parameter checking and resolution are complete (which are more or less the same across instructions), the logic of the instruction is executed. Each callback gets its parameters in registers (yes, no more stack), typically like this: &lt;code&gt;eax=dst ecx=src edx=size&lt;/code&gt;, where &lt;code&gt;dst/src&lt;/code&gt; is either a register or memory, and &lt;code&gt;size&lt;/code&gt; specifies the size of the operands. I say &amp;quot;typically&amp;quot; because some instructions have special meaning, but most of the time the logic is the same.&lt;/p&gt;&#10;&lt;p&gt;The virtual machine has &lt;a href="https://homepage.cs.uiowa.edu/~dwjones/arch/notes/30varlen.html" target="_blank" rel="noopener"&gt;variable-length instructions&lt;/a&gt;: the smallest instruction is encoded using 2 bytes, and the largest takes up to 15 bytes. Some instructions can perform non-trivial logic, so the VM implements a kind of &lt;a href="https://en.wikipedia.org/wiki/Complex_instruction_set_computer" target="_blank" rel="noopener"&gt;CISC&lt;/a&gt; architecture. The general encoding for &lt;em&gt;standard&lt;/em&gt; instructions (there are also non-standard ones) is as follows:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;General encoding:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- opcode: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- first argument&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand_type: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) operand_size: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand: 1 to 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) index_register: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) scale: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) displacement: 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- (optional) second argument&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand_type: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) operand_size: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand: 1 to 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) index_register: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) scale: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) displacement: 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Operand types:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x01: register&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x02: memory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x03: immediate&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Operand sizes:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x01: byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x02: word&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x03: dword&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Registers:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x04: eax/ax/al&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x08: ecx/cx/cl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0C: edx/dx/dl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x10: ebx/bx/bl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x14: esp/sp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x18: ebp/bp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x1C: esi/si&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x20: edi/di&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Argument rules:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `register`, argument format is &amp;#39;operand_type operand_size register&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `memory`, argument format is &amp;#39;operand_type operand_size base_register index_register scale displacement/4 bytes&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `immediate`, argument format is &amp;#39;operand_type immediate_value/4 bytes&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are some examples:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1D 01 03 18&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1D=push&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;18=ebp&lt;/code&gt; =&amp;gt; &lt;code&gt;push ebp&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;1A 01 03 18 01 03 14&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1A=mov&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;18=ebp&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;14=esp&lt;/code&gt; =&amp;gt; &lt;code&gt;mov ebp, esp&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;09 02 01 20 0C 04 BE BA FE CA 03 7C 00 00 00&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;09=add&lt;/code&gt; &lt;code&gt;02=memory&lt;/code&gt; &lt;code&gt;01=byte&lt;/code&gt; &lt;code&gt;20=edi&lt;/code&gt; &lt;code&gt;0C=edx&lt;/code&gt; &lt;code&gt;04=scale&lt;/code&gt; &lt;code&gt;BE BA FE CA=displacement&lt;/code&gt; &lt;code&gt;03=immediate&lt;/code&gt; &lt;code&gt;7C 00 00 00=immediate_value&lt;/code&gt; =&amp;gt; &lt;code&gt;add byte ptr [edi+edx*4+0xCAFEBABE], 0x7C&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;To make my life a little easier, I wrote a &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vmcrack/disassembler" target="_blank" rel="noopener"&gt;small disassembler&lt;/a&gt;. All the rules are there, so it may be easier to understand the logic of the virtual machine by reading the code instead of this text.&lt;/p&gt;&#10;&lt;p&gt;These are exceptions to the rules described earlier:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;opcodes &lt;code&gt;0x33&lt;/code&gt;-&lt;code&gt;0x35&lt;/code&gt; have the format &lt;code&gt;opcode immediate/2 bytes&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;opcodes &lt;code&gt;0x36&lt;/code&gt;-&lt;code&gt;0x40&lt;/code&gt; have the format &lt;code&gt;opcode operand_type&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Please refer to the disassembler sources to see which opcode maps to which x86 instruction. I won't describe them here, except for the one I called &lt;code&gt;mov_wtf&lt;/code&gt;. The name says it all: I was struggling to understand what it does until I saw it used in the bytecode and found this &lt;a href="https://web.archive.org/web/20261114205946/http://blog.rewolf.pl/blog/?p=102" target="_blank" rel="noopener"&gt;blog post&lt;/a&gt;. Honestly, I didn't even know that this was possible, but it does exactly what the article describes: it switches from 32-bit mode to 64-bit mode and back! It's really cool.&lt;/p&gt;&#10;&lt;p&gt;Okay, I hope that my chaotic description of the VM architecture was more or less clear (or that you at least read the disassembler source code), so let's move on to the task itself.&lt;/p&gt;&#10;&lt;p&gt;First of all, every time you do something wrong, it shows some weird text and exits. For example, if you run it under a debugger and hit one of the anti-debugging checks, that's what happens. If your input is incorrect, it shows you something else, and so on. In fact, every wrong move has its own text, so this is also a kind of way to debug it :)&lt;/p&gt;&#10;&lt;p&gt;These messages are stored in the binary, but they are encrypted. To decrypt and print them, the crackme runs the virtual machine with special bytecode. If you disassemble the challenge, you will see the following pattern:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_is_wrong&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;encrypted_text_offset&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;binary_blob_with_encrypted_text_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;encrypted_text_key_offset&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xF00DCAFE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or any other key&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;run_vm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;run_vm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytecode_exit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, the same bytecode is used all the time, but to print different messages, the necessary pointers and keys are patched into the bytecode, so when the virtual machine starts, it decrypts and prints the right message. On top of that, the addresses of the &lt;code&gt;puts&lt;/code&gt; and &lt;code&gt;exit&lt;/code&gt; functions are patched in the same way, but the code does so in the TLS callback responsible for the &lt;code&gt;WOW64&lt;/code&gt; check.&lt;/p&gt;&#10;&lt;p&gt;The string encryption algorithm is quite simple, so I used the following IDC script to view the text in IDA:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xF00DCAFE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x7C&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you get past half of the anti-debugging tricks, the virtual machine executes the bytecode that reads and validates the input. The &lt;code&gt;run_vm&lt;/code&gt; call with this bytecode is protected by another trick, and some of the tricks are in the bytecode itself, so have fun if you're still trying to debug it. For this bytecode to work, the pointers to &lt;code&gt;fputs&lt;/code&gt;/&lt;code&gt;fgets&lt;/code&gt; and &lt;code&gt;stdin&lt;/code&gt;/&lt;code&gt;stdout&lt;/code&gt; are also patched into it, directly in the &lt;code&gt;main&lt;/code&gt; function.&lt;/p&gt;&#10;&lt;p&gt;Once the input is read from standard input, the bytecode encrypts it with a set of instructions that shuffle the bits back and forth, and then &lt;code&gt;memcmp&lt;/code&gt; checks that the input is correct. If not, you get another strange message from the AI, and it exits. Fortunately, the algorithm is easily reversible, and the correct input can be decoded using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;xor_byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kr"&gt;__asm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mov&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;xor&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;xor_byte&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;inc&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;rol&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x1d&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sub&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x3e&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;neg&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;xor&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xc3&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0e&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ror&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x9f&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;dec&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mov&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;where &lt;code&gt;c&lt;/code&gt; is the current encrypted character and &lt;code&gt;xor_byte&lt;/code&gt; is the previous one.&lt;/p&gt;&#10;&lt;p&gt;This is probably all you need if your goal is to get the flag, because the challenge shows it when the input is correct. But I was more curious and decided to figure out where the flag was and how it was encrypted.&lt;/p&gt;&#10;&lt;p&gt;Unsurprisingly, this uses yet another piece of bytecode, and it's actually quite fun. If you've been analyzing the binary from the beginning, you've probably noticed that the &lt;code&gt;main&lt;/code&gt; function also initializes some global variables with pointers to various functions. Some of them are obvious: they directly use the Windows API to calculate SHA-256 and MD5 hashes. Another one isn't as obvious, but if you know how CRC32 works, you'll recognize it right away. The last one took me a while to figure out, until I found a table that also reminded me of something. It turned out that this is an &lt;a href="https://en.wikipedia.org/wiki/Rijndael_S-box" target="_blank" rel="noopener"&gt;AES S-box&lt;/a&gt; table, and the function performs AES decryption. All of these are used in the final bytecode that decrypts the flag, embedded in gorgeous ASCII art. To make it a little funnier, the text is also hidden using the same method used for other string data in the binary. So, to know that the user's input was correct, the challenge tries to decrypt the flag and checks that the CRC32 of the decrypted blob is equal to &lt;code&gt;0x4da2c9f6&lt;/code&gt;, which is the CRC32 of the correct data.&lt;/p&gt;&#10;&lt;p&gt;To summarize, the flag can be decrypted using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;sha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SHA256&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;sha&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;correct_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;md5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MD5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;md5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;correct_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AES&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sha&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="no"&gt;AES&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MODE_CBC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;md5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# decrypt the text&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0xc1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x7c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/vmcrack" target="_blank" rel="noopener"&gt;here&lt;/a&gt;, and my disassembler and the disassembled bytecode can be found &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vmcrack" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"ReRop" by Hack The Box</title><link>https://pugachev.io/2024/04/07/rerop-by-hack-the-box/</link><pubDate>Sun, 07 Apr 2024 15:38:14 +0100</pubDate><guid>https://pugachev.io/2024/04/07/rerop-by-hack-the-box/</guid><description>&lt;p&gt;Sometimes everything looks scarier than it really is, like in this task. ROP (return-oriented programming) usually looks like something complicated. But if you put aside how to apply it against all the modern protections, the basic approach is quite simple: find short sequences of instructions (&amp;quot;gadgets&amp;quot;, as they call them) in the executable and chain them to get the result you want. This challenge is a simple demonstration of this idea.&lt;/p&gt;&#10;&lt;p&gt;Apart from a ton of extra code in the binary (it's statically linked), the check logic looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Enter the flag: &amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;fgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64LL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;magic_here&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_buf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that's all. If you think that there should be something in the &lt;code&gt;check&lt;/code&gt; function, there isn't:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017B5&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt; &lt;span class="kt"&gt;proc&lt;/span&gt; &lt;span class="kt"&gt;near&lt;/span&gt; &lt;span class="c1"&gt;; CODE XREF: main+6D↓p&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017B5&lt;/span&gt; &lt;span class="c1"&gt;; __unwind {&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017B5&lt;/span&gt; &lt;span class="nf"&gt;endbr64&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017B9&lt;/span&gt; &lt;span class="nf"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rdi&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017BC&lt;/span&gt; &lt;span class="nf"&gt;retn&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.text:00000000004017BC&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt; &lt;span class="kt"&gt;endp&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;So the only thing it does is point the stack pointer to this &lt;code&gt;magic_here&lt;/code&gt; blob in the binary, and the following &lt;code&gt;ret&lt;/code&gt; takes the first address from it. What kind of blob is this? It's a chain of addresses of these &amp;quot;carefully chosen machine instruction sequences&amp;quot; (plus the values they use), which executes logic that is not directly represented in the file as functions or other common forms.&lt;/p&gt;&#10;&lt;p&gt;I don't know if this blob can be easily disassembled, so I decided to run it under a debugger and follow the code. Luckily, the logic is fairly trivial. It checks whether the code is running under a debugger, and if not, it executes the following (simplified) logic:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mh"&gt;0x1D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="n"&gt;input_buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is not exactly the code that is executed, because there is no real &lt;code&gt;encrypted_flag&lt;/code&gt; array anywhere. It's more like running the same sequence of gadgets with different values (and not even in order) that represent the current &lt;code&gt;encrypted_flag[i]&lt;/code&gt;. To be clear: the debugger check, the flag check, printing the result in different colors, all of this is here, in the blob. It may look complicated at first, but during a debugging session this repeating pattern is quite obvious, so in the end, you'll know where to look for the next encrypted character.&lt;/p&gt;&#10;&lt;p&gt;To reverse the algorithm and get the flag, you can use the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mh"&gt;0x1D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;%c&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0x05&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can find the challenge &lt;a href="https://app.hackthebox.com/challenges/ReRop" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Iterative Virus" by Hack The Box</title><link>https://pugachev.io/2024/04/07/iterative-virus-by-hack-the-box/</link><pubDate>Sun, 07 Apr 2024 13:35:11 +0100</pubDate><guid>https://pugachev.io/2024/04/07/iterative-virus-by-hack-the-box/</guid><description>&lt;p&gt;A good pet project for a developer who wants to learn something new is writing a game. I've done this before, but it wasn't fun. The best project was writing a virus: tons of new knowledge, a broken OS when your creature &amp;quot;infected&amp;quot; something it shouldn't have (plus the infection algorithm didn't work correctly), and a fight with an antivirus that easily moved your file to quarantine. Oh, the good old days.&lt;/p&gt;&#10;&lt;p&gt;This challenge reminded me of those days: this is literally an old-school virus that infects files correctly! I don't mean this fancy &amp;quot;dropper with a downloader with a rootkit with...&amp;quot;, but rather something that finds a file, adds a new section, patches the entry point, and does all the things that viruses used to do. It even has something resembling polymorphic behavior! Pretty cool for a CTF challenge.&lt;/p&gt;&#10;&lt;p&gt;Let's start exploring it. First, this is a regular PE64 file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; diec ./HELLO_WORLD_INFECTED_&lt;span class="se"&gt;\!\!\!&lt;/span&gt;.exe&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;PE64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Linker: Microsoft Linker(14.28.30038)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Compiler: Microsoft Visual C/C&amp;#43;&amp;#43;(19.28.30038)[C]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Tool: Visual Studio(2019 version 16.9-16.10)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;But, like a good old-fashioned infected file, it has a strange section that the entry point points to:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -wh ./HELLO_WORLD_INFECTED_&lt;span class="se"&gt;\!\!\!&lt;/span&gt;.exe &lt;span class="p"&gt;|&lt;/span&gt; grep ivir&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 6 .ivir 00000d80 000000014001c000 000000014001c000 00018400 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE, DATA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -f ./HELLO_WORLD_INFECTED_&lt;span class="se"&gt;\!\!\!&lt;/span&gt;.exe &lt;span class="p"&gt;|&lt;/span&gt; grep start&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;start address 0x000000014001c000&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The code at the entry point also looks familiar: it first finds the address of the &lt;code&gt;GetProcAddress&lt;/code&gt; function using the &amp;quot;PEB technique&amp;quot;, and then resolves the addresses of additional functions that will be needed later. Here are the functions it tries to find, with their IDs:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;5: ExitProcess&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;6: CreateFileA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;7: GetFileSize&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;8: CreateFileMappingA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;9: MapViewOfFile&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0C: FindFirstFileA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0D: FindNextFileA&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0E: FindClose&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0x0F: CloseHandle&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then the fun begins. The crackme checks whether the byte at offset 5 from the entry point (the one that follows the first &lt;code&gt;jmp&lt;/code&gt; instruction) is &lt;code&gt;5&lt;/code&gt;. If so, it calls a function that currently appears to be encrypted. Otherwise, it selects a 64-bit number based on this byte and stores it for decryption later. In the current binary, this byte is &lt;code&gt;1&lt;/code&gt;, so it looks like we need to do something to decrypt the extra code. So let's go with the flow and see what the crackme does next.&lt;/p&gt;&#10;&lt;p&gt;The next step is the actual infection phase. In short, it does the following:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Search for files matching the &lt;code&gt;*.exe&lt;/code&gt; wildcard, starting in the current directory.&lt;/li&gt;&#10;&lt;li&gt;Map the found file into memory using the &lt;code&gt;CreateFileMappingA&lt;/code&gt;/&lt;code&gt;MapViewOfFile&lt;/code&gt; functions.&lt;/li&gt;&#10;&lt;li&gt;If this file is not an EXE file (the first two bytes must be &lt;code&gt;MZ&lt;/code&gt;) or &lt;code&gt;_IMAGE_FILE_HEADER.Machine&lt;/code&gt; is not &lt;code&gt;0x8664&lt;/code&gt; (AMD64), close it and try to find another victim.&lt;/li&gt;&#10;&lt;li&gt;To avoid accidentally infecting your entire system, it only infects files with &lt;code&gt;0x53494854&lt;/code&gt; (&lt;code&gt;SIHT&lt;/code&gt;, or &lt;code&gt;THIS&lt;/code&gt; if you read the bytes in the file) in &lt;code&gt;_IMAGE_FILE_HEADER.TimeDateStamp&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;If there is enough space in the binary for a new &lt;code&gt;_IMAGE_SECTION_HEADER&lt;/code&gt;, it is added right after the last one. The new section has the following characteristics: &lt;code&gt;CNT_CODE | CNT_INITIALIZED_DATA | MEM_EXECUTE | MEM_READ&lt;/code&gt; and is named &lt;code&gt;.ivir&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;The actual virus code is then copied into the mapped memory, right after the last section (aligned to &lt;code&gt;FileAlignment&lt;/code&gt;, of course). By the way, when the crackme calls &lt;code&gt;MapViewOfFile&lt;/code&gt; in the second step, it already requests extra memory for the virus body.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;If everything goes well, the crackme moves to the encrypted part: it &amp;quot;decrypts&amp;quot; the copy in the new file one more time, using the key selected by the magic byte I described earlier. Given that the binary we have has a &lt;code&gt;1&lt;/code&gt; there, it uses &lt;code&gt;keys[1]&lt;/code&gt; and writes &lt;code&gt;2&lt;/code&gt; into the magic byte of the new file. In other words, every time it infects a file, it applies one more key and increments the byte. This means that if you run a newly infected file, it will use &lt;code&gt;keys[2]&lt;/code&gt;, and so on. When the magic byte is &lt;code&gt;5&lt;/code&gt;, the code is fully decrypted, and the virus calls it instead of infecting more files.&lt;/p&gt;&#10;&lt;p&gt;You could definitely play around with this and create specially crafted victim files to get the decrypted code back, but I cut corners and used the following IDC script:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x28C8AA0746A75909&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x6E2368B9C685770B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0EB7FD64E061C1A3D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0CB8FF2D53D7505A1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0F1EF554206DCE4D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x198&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_qword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_qword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, the decryption algorithm is just a multiplication. So, if you apply it 4 more times with the correct constants (the author has already done it once), you get the code back. Nice!&lt;/p&gt;&#10;&lt;p&gt;The decrypted code is simple: it shows you a message box with the flag if the name of the user running the binary matches the flag :) So you'll have to disassemble it anyway. Also, since this code runs inside an infected file, it also dynamically resolves the functions it needs, using the same idea as the main code.&lt;/p&gt;&#10;&lt;p&gt;One final note: the virus kindly stores the original entry point of the infected file in the qword following the magic byte. The only thing is that this is not the RVA itself, but the difference between the new and the original entry points. Feel free to write an antivirus.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Iterative%20Virus" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"PTRACE_NOPEEKING" by Hack The Box</title><link>https://pugachev.io/2024/04/02/ptrace_nopeeking-by-hack-the-box/</link><pubDate>Tue, 02 Apr 2024 17:26:12 +0100</pubDate><guid>https://pugachev.io/2024/04/02/ptrace_nopeeking-by-hack-the-box/</guid><description>&lt;p&gt;Debugging a debugger is probably as much fun as disassembling a disassembler or decompiling a decompiler. But disassembling two debuggers debugging each other at the same time and preventing you from decompiling them is priceless.&lt;/p&gt;&#10;&lt;p&gt;To start simple, there is one binary that forks and checks 2 parts of the flag at the same time. To complicate things a little, these 2 processes behave as both a debugger and a debuggee, controlling each other's execution.&lt;/p&gt;&#10;&lt;p&gt;Each process manages its own state: the flag-checking algorithm is not executed sequentially but is divided into several stages. For example, the first step (state &lt;code&gt;0&lt;/code&gt;) of the parent process is to initialize the global variables used by the check and read the input from standard input. It then moves to the next state and passes control to the child. When the child starts, its first state is also initialization. On its next turn, the parent runs the check logic. Then the child. And it all goes back and forth, so both processes take turns.&lt;/p&gt;&#10;&lt;p&gt;To make things even harder and prevent you from easily reading the C code decompiled for you by IDA, the crackme uses a trick with &lt;code&gt;ud2&lt;/code&gt; &lt;a href="https://www.felixcloutier.com/x86/ud" target="_blank" rel="noopener"&gt;instructions&lt;/a&gt;, and this is where the &amp;quot;debug-each-other&amp;quot; logic comes into play. When the debuggee executes this instruction, the OS wakes up the debugger because the debuggee was stopped by the delivery of a signal. Then, if this signal is &lt;code&gt;SIGILL&lt;/code&gt;, the magic happens: the current debugger checks the 2 bytes following the &lt;code&gt;ud2&lt;/code&gt; instruction and runs additional logic based on them using &lt;code&gt;ptrace&lt;/code&gt;. For example, if &lt;code&gt;SIGILL&lt;/code&gt; was delivered and the next 2 bytes are &lt;code&gt;0x5253&lt;/code&gt;, the debugger runs logic that reads user input. If the bytes are &lt;code&gt;0x4543&lt;/code&gt;, the debugger runs logic that decrypts the current character, and so on. There are 5 such cases that either simply transfer data from a register to memory, or run more complex logic, such as reading from standard input.&lt;/p&gt;&#10;&lt;p&gt;To split the check between the 2 processes, a global array is used:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.data:0000000000202020&lt;/span&gt; &lt;span class="nf"&gt;whose_turn_to_check&lt;/span&gt; &lt;span class="nv"&gt;dd&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.data:0000000000202068&lt;/span&gt; &lt;span class="kd"&gt;dd&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;There are 27 integers (one per flag character), each of which indicates which process should check the current character. If it is 0, the parent should do it, otherwise the child is responsible for it. The interesting thing is that if the currently active process does not need to check the character, it still switches its current state to &amp;quot;I'm-going-to-check-this-character&amp;quot;. But instead of checking the actual character, it gets a value from &lt;code&gt;rand()&lt;/code&gt; and marks it, so in the following states, it can understand that the character is &amp;quot;fake&amp;quot; and skip it correctly.&lt;/p&gt;&#10;&lt;p&gt;In any case, once you figure out what's going on, the logic behind the check won't seem very complicated. Each process does the same thing, which can be summarized in the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;encrypted_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_char&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flag_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;whose_turn_to_check&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part_0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part0_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;part_1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part1_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_char&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;flag_char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In other words, the process performs some math on the current character entered by the user, and if the result is not equal to the hardcoded value, the input is invalid. &lt;code&gt;part_0&lt;/code&gt; and &lt;code&gt;part_1&lt;/code&gt; are arrays of expected values that the parent and the child check in turn.&lt;/p&gt;&#10;&lt;p&gt;To recover the flag, you can use the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;part0_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;part1_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mh"&gt;0x1A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;whose_turn_to_check&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part_0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part0_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;part_1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part1_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mh"&gt;0x2C&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mh"&gt;0x1A&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;%c&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/PTRACE_NOPEEKING" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Coffee Invocation" by Hack The Box</title><link>https://pugachev.io/2024/04/02/coffee-invocation-by-hack-the-box/</link><pubDate>Tue, 02 Apr 2024 12:56:46 +0100</pubDate><guid>https://pugachev.io/2024/04/02/coffee-invocation-by-hack-the-box/</guid><description>&lt;p&gt;The great thing about the challenges I've seen on Hack The Box is that almost all of them have some crazy stuff that forces you to do a lot of research and experimentation. It's amazing because that's how people like me learn new things. Thank you for that!&lt;/p&gt;&#10;&lt;p&gt;This task is a C program running Java code inside of it, which is something that doesn't happen very often in real life. The common use of &lt;a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html" target="_blank" rel="noopener"&gt;JNI&lt;/a&gt; that I have seen is to implement native code called from Java. But this example is a little different: the program itself starts a Java virtual machine and uses JNI to configure and control the flow of the program, rather than the other way around.&lt;/p&gt;&#10;&lt;p&gt;The author probably loves coffee too much, because it's all about the coffee. It &amp;quot;emulates&amp;quot; a coffee machine and even prints nice ASCII art of different coffee flavors :) To run the real challenge, you need to select item &lt;code&gt;3&lt;/code&gt;, called &lt;code&gt;[REDACTED]&lt;/code&gt;, from the menu. But before that, you need to specify the correct flag as the first argument of the program, like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./coffee_invocation &amp;lt;FLAG_HERE&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Digging deeper into the code, I discovered that the crackme calls 2 functions, passing &lt;code&gt;argv[1]&lt;/code&gt; as a parameter (in addition to &lt;code&gt;JNIEnv*&lt;/code&gt;), and if they both return 0, the flag is printed. The sad thing is that the &amp;quot;flag&amp;quot; in this case is just your command-line argument without any changes, so there's no fancy &amp;quot;XOR encryption&amp;quot; here.&lt;/p&gt;&#10;&lt;p&gt;I called these functions &lt;code&gt;verify1&lt;/code&gt; and &lt;code&gt;verify2&lt;/code&gt;, so let's start with the first one. If you summarize the JNI calls, it does the following:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Add a shutdown hook so that calling &lt;code&gt;System.exit()&lt;/code&gt; from Java will call a predefined C function.&lt;/li&gt;&#10;&lt;li&gt;Override what the &lt;code&gt;Byte.valueOf()&lt;/code&gt; and &lt;code&gt;Short.valueOf()&lt;/code&gt; methods return. More on this a bit later.&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#DefineClass" target="_blank" rel="noopener"&gt;Define a class&lt;/a&gt; from a byte buffer hardcoded in the binary (which is just a compiled Java class).&lt;/li&gt;&#10;&lt;li&gt;Create an array of 2 strings: the first is a substring of length &lt;code&gt;0x1A&lt;/code&gt; from &lt;code&gt;argv[1]&lt;/code&gt;, which is the first part of the flag. The second is a hardcoded string: &lt;code&gt;~PL{A;PL{?;:=|PIC{HzP:A;~x&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;Call the &lt;code&gt;main&lt;/code&gt; method of the defined class and pass this array as an argument.&lt;/li&gt;&#10;&lt;li&gt;Undo the override from step 2.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The shutdown hook is trivial: it saves the &lt;code&gt;exit&lt;/code&gt; status in a global variable, which &lt;code&gt;verify1&lt;/code&gt; returns later as its result.&lt;/p&gt;&#10;&lt;p&gt;The override logic is the fun part: the JNI code changes the behavior of the Java code, which is an interesting trick in itself. First of all, the code calls a function that returns a table by ID: &lt;code&gt;1&lt;/code&gt; for the &lt;code&gt;Byte&lt;/code&gt; table, &lt;code&gt;2&lt;/code&gt; for the &lt;code&gt;Short&lt;/code&gt; table, and &lt;code&gt;0&lt;/code&gt; for the original one. To change the return value of the &lt;code&gt;valueOf&lt;/code&gt; method, the JNI code does the following:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;256&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Byte&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;overriding_table&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The same idea works for the &lt;code&gt;Short&lt;/code&gt; class. Why does this work at all? Simply because both classes have an internal cache of static objects, so every time you call &lt;code&gt;Byte.valueOf(i)&lt;/code&gt; or &lt;code&gt;Short.valueOf(i)&lt;/code&gt; where &lt;code&gt;i&lt;/code&gt; is between &lt;code&gt;-128&lt;/code&gt; and &lt;code&gt;127&lt;/code&gt;, the cached object is returned (at least that's how it works in OpenJDK). The JNI code modifies the private &lt;code&gt;value&lt;/code&gt; field of these static objects that the JDK created in advance. Nice and easy!&lt;/p&gt;&#10;&lt;p&gt;Now, the remaining steps are defining the class and calling its static method. The binary blob that the &lt;code&gt;DefineClass&lt;/code&gt; function uses is easily identified by the famous &lt;code&gt;CAFEBABE&lt;/code&gt; magic bytes in the header. You can use any of the existing decompilers to get the source code back, or just read it &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/coffee_invocation" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;The only thing the &lt;code&gt;main&lt;/code&gt; method of the &lt;code&gt;Verify1&lt;/code&gt; class does is compare the 2 strings passed as parameters. But the code looks a bit strange:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;private&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;compareByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Short&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// init code skipped&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;compareByte&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;short&lt;/span&gt;&lt;span class="p"&gt;)((&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;=&amp;gt; User might be an alien!!!&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So it calls the &lt;code&gt;compareByte&lt;/code&gt; method, and boxing its arguments triggers calls to the &lt;code&gt;Byte.valueOf&lt;/code&gt; and &lt;code&gt;Short.valueOf&lt;/code&gt; methods. This way, the code indirectly uses the modified tables that the JNI code has initialized in advance. In other words, it doesn't actually compare the 2 original strings, but rather the modified versions of them. So I'm guessing that the string &lt;code&gt;~PL{A;PL{?;:=|PIC{HzP:A;~x&lt;/code&gt; is a modified version of the flag, and the question is how to reverse it.&lt;/p&gt;&#10;&lt;p&gt;After analyzing both mapping tables, I discovered the following pattern:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;each value in the &lt;code&gt;Byte&lt;/code&gt; table is shifted by &lt;code&gt;0x51&lt;/code&gt;: &lt;code&gt;(i + 0x51) &amp;amp; 0xFF&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;the values in the &lt;code&gt;Short&lt;/code&gt; table are arranged in descending order: &lt;code&gt;(~i + 1) &amp;amp; 0xFF&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;So, to get the first part of the flag back, we need to do the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;map each character back through the &lt;code&gt;Short&lt;/code&gt; table&lt;/li&gt;&#10;&lt;li&gt;map the result back through the &lt;code&gt;Byte&lt;/code&gt; table&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The second step is necessary because the input characters also go through the &lt;code&gt;Byte&lt;/code&gt; table, so we need to sort of &amp;quot;reverse&amp;quot; this operation too. The following code gets the first part of the flag:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;encodedChar&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;flag_firstPart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;shortRemap&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;encodedChar&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;byteRemap&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shortRemap&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0x51&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;byteRemap&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This wasn't difficult, so let's move on to the second part of the flag and the &lt;code&gt;verify2&lt;/code&gt; function.&lt;/p&gt;&#10;&lt;p&gt;The steps there are almost the same:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Add a shutdown hook so that calling &lt;code&gt;System.exit()&lt;/code&gt; from Java will call a predefined C function.&lt;/li&gt;&#10;&lt;li&gt;Override what the &lt;code&gt;Character.valueOf()&lt;/code&gt; method returns. More on this a bit later.&lt;/li&gt;&#10;&lt;li&gt;Change the value of the &lt;code&gt;Boolean.TRUE&lt;/code&gt; and &lt;code&gt;Boolean.FALSE&lt;/code&gt; fields so that the first value becomes &lt;code&gt;false&lt;/code&gt; and the second value becomes &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#DefineClass" target="_blank" rel="noopener"&gt;Define a class&lt;/a&gt; from a byte buffer hardcoded in the binary that will become the &lt;code&gt;Verify2&lt;/code&gt; class.&lt;/li&gt;&#10;&lt;li&gt;Call the &lt;code&gt;main&lt;/code&gt; method of the defined class and pass the second part of the flag to it.&lt;/li&gt;&#10;&lt;li&gt;Undo the &lt;code&gt;Boolean&lt;/code&gt; override.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Now the shutdown hook looks a bit different. In addition to saving the exit status in a global variable (which will be the result of this function), it has the following logic: if the status is greater than &lt;code&gt;2&lt;/code&gt;, the hook overrides &lt;code&gt;Character.valueOf&lt;/code&gt; again, using the table with ID &lt;code&gt;status + 1&lt;/code&gt;. In other words, every time Java calls &lt;code&gt;System.exit(N)&lt;/code&gt; with &lt;code&gt;N &amp;gt; 2&lt;/code&gt;, a new table is applied.&lt;/p&gt;&#10;&lt;p&gt;If you check how the table is selected, you will see that there are 13 tables in total.&lt;/p&gt;&#10;&lt;p&gt;This is what the main logic of the &lt;code&gt;Verify2&lt;/code&gt; class looks like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;complexSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;complexSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Cr1KD5mk0_uUzQYifaGVqlN2B3wvpgPtSx6Odo{8hjJLHy9IXb4RnWZ}TAFEsMce7&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// This will trigger remapping&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;First of all, it looks like the tables switch after every 2 characters. Also, since the JNI code swapped &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;, the Java code doesn't sort our input, it sorts the constant string instead. Remember that the &lt;code&gt;Character.valueOf&lt;/code&gt; method is also modified by the JNI code, so this constant string looks different every time the tables are switched.&lt;/p&gt;&#10;&lt;p&gt;Luckily for us, sorting a list of ASCII characters (no matter what order they are in after overriding) will always produce the same result: &lt;code&gt;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz{}&lt;/code&gt;. Given that the second part of the flag has length &lt;code&gt;0x1A&lt;/code&gt;, the only thing we need to do is match every 2 characters of the beginning of this string, &lt;code&gt;0123456789ABCDEFGHIJKLMNOP&lt;/code&gt;, using a different table for each pair.&lt;/p&gt;&#10;&lt;p&gt;Unfortunately, I don't see any pattern in how the characters are laid out in the tables, so I had to copy them all into my code. Here's how to decode the second part of the flag:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;0123456789ABCDEFGHIJKLMNOP&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;characterTables&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Coffee%20Invocation" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and the decompiled code &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/coffee_invocation" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Virtually Mad" by Hack The Box</title><link>https://pugachev.io/2024/03/26/virtually-mad-by-hack-the-box/</link><pubDate>Tue, 26 Mar 2024 16:22:19 +0100</pubDate><guid>https://pugachev.io/2024/03/26/virtually-mad-by-hack-the-box/</guid><description>&lt;p&gt;This challenge implements a simple virtual machine with only 5 instructions. Your task is to write a program for this VM that satisfies some requirements, and the flag is the bytecode of this program. Pretty interesting!&lt;/p&gt;&#10;&lt;p&gt;Each instruction is 4 bytes long and has the following format:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;nibbles 7 and 6 encode the opcode&lt;/li&gt;&#10;&lt;li&gt;nibble 5 should always be 1&lt;/li&gt;&#10;&lt;li&gt;nibble 4 encodes the destination register&lt;/li&gt;&#10;&lt;li&gt;nibble 3 is a flag: if set to 1, nibble 2 encodes the source register, otherwise, nibbles 0-2 encode the immediate value&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;I'll write the instructions I used like this: &lt;code&gt;opcode=01 always=1 dst_reg=0 irflag=0 imm_or_src_reg=100&lt;/code&gt;, where &lt;code&gt;irflag&lt;/code&gt; is the &amp;quot;immediate or register&amp;quot; flag. The other fields are self-explanatory.&lt;/p&gt;&#10;&lt;p&gt;The VM has only 4 registers, encoded using their index in the VM state structure: &lt;code&gt;0=reg_a, 1=reg_b, 2=reg_c, 3=reg_d&lt;/code&gt;. There is also a &lt;code&gt;flags&lt;/code&gt; register, which can only have one of 2 values: &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;0x10000000&lt;/code&gt;. It is used to store the result of the &lt;code&gt;cmp&lt;/code&gt; instruction: if the values are equal, the flags register will have the value &lt;code&gt;0x10000000&lt;/code&gt;, otherwise, it will be &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;These are the opcodes implemented by the virtual machine (where the number is the value of the opcode): &lt;code&gt;1=mov, 2=add, 3=sub, 4=cmp, 5=exit&lt;/code&gt;. Every opcode except &lt;code&gt;exit&lt;/code&gt; checks &lt;code&gt;irflag&lt;/code&gt; to see where the source value comes from. For example, &lt;code&gt;add&lt;/code&gt; in pseudocode looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irflag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dst_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The same logic applies to both the &lt;code&gt;sub&lt;/code&gt; and &lt;code&gt;mov&lt;/code&gt; opcodes. The only exception is &lt;code&gt;cmp&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irflag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dst_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mh"&gt;0x10000000&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;=&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mh"&gt;0x10000000&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that's all this virtual machine can do. Now the task is to write a program that ends with the following state:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;reg_a&lt;/code&gt; should be equal to &lt;code&gt;0x200&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_b&lt;/code&gt; should be equal to &lt;code&gt;-1&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_c&lt;/code&gt; should be equal to &lt;code&gt;-1&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_d&lt;/code&gt; should be equal to &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;flags&lt;/code&gt; should be set to &lt;code&gt;0x10000000&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The total number of instructions in the program must be equal to &lt;code&gt;5&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;There are many ways to achieve these goals, so to narrow down the options (and subsequently check the flag), the crackme places the following restrictions on your program. Each instruction passes if at least one of its two conditions is true:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The first instruction should be &lt;code&gt;add&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_a&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The second instruction should be &lt;code&gt;add&lt;/code&gt;, or its immediate value should be &lt;code&gt;0x100&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The third instruction should be &lt;code&gt;sub&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_b&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The fourth instruction should be &lt;code&gt;mov&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_c&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The fifth instruction should be &lt;code&gt;cmp&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_d&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The intended program is the following:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0: opcode=02 always=1 dst_reg=0(a) irflag=0 imm_or_src_reg=100 add a, 256&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;1: opcode=02 always=1 dst_reg=0(a) irflag=0 imm_or_src_reg=100 add a, 256&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;2: opcode=03 always=1 dst_reg=1(b) irflag=0 imm_or_src_reg=001 sub b, 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;3: opcode=01 always=1 dst_reg=2(c) irflag=1 imm_or_src_reg=100 mov c, b&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;4: opcode=04 always=1 dst_reg=3(d) irflag=0 imm_or_src_reg=000 cmp d, 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So, if you concatenate the encoded instructions into one line, you get the correct answer.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Virtually%20Mad" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"FFModule" by Hack The Box</title><link>https://pugachev.io/2024/03/25/ffmodule-by-hack-the-box/</link><pubDate>Mon, 25 Mar 2024 16:43:24 +0100</pubDate><guid>https://pugachev.io/2024/03/25/ffmodule-by-hack-the-box/</guid><description>&lt;p&gt;It's always fun to solve a challenge without even running the binary. Sometimes this is necessary because either you don't have the required hardware, OS, or library, or because the challenge has bugs and doesn't work. This crackme is an example of the latter: with my configuration at the time (Windows 11 + Firefox 124.0.1), it just crashed (that's after disabling Windows Defender, otherwise it gets killed even earlier). But in this case, a disassembler is enough.&lt;/p&gt;&#10;&lt;p&gt;This crackme is a classic example of how to inject malicious code into a process, and in particular, of how Firefox can be used as a target to steal data. Again, the problem is that this is a &amp;quot;classic&amp;quot; and an &amp;quot;example&amp;quot;, so even a mediocre antivirus will stop it.&lt;/p&gt;&#10;&lt;p&gt;But let's start from the very beginning: the code it injects is &amp;quot;hidden&amp;quot;, so at the first stage, the crackme &amp;quot;decrypts&amp;quot; it and then runs the injector. To see this code, just execute the following IDC script in IDA:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x5A4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0x72&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The injector doesn't look too fancy: it iterates through the list of running processes, and if a process's executable is &lt;code&gt;firefox.exe&lt;/code&gt;, it allocates memory in that process, copies the next stage into it, makes it executable, and starts a new thread there to run it.&lt;/p&gt;&#10;&lt;p&gt;The goal of the second stage is to install a trampoline in the &lt;code&gt;PR_Write&lt;/code&gt; function from &lt;a href="https://firefox-source-docs.mozilla.org/nspr/reference/pr_write.html" target="_blank" rel="noopener"&gt;Firefox's NSPR&lt;/a&gt;, which is used to write data to a file descriptor. To do this correctly, the following steps are needed:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;First, the shellcode (or what should I call it now?) finds the address of the &lt;code&gt;GetProcAddress&lt;/code&gt; function using the &amp;quot;PEB technique&amp;quot; described everywhere and by everyone, so I won't describe it here again.&lt;/li&gt;&#10;&lt;li&gt;It then finds the addresses of the &lt;code&gt;VirtualAlloc&lt;/code&gt; and &lt;code&gt;VirtualProtect&lt;/code&gt; functions and allocates executable memory to store the part of the original &lt;code&gt;PR_Write&lt;/code&gt; function that it will overwrite later.&lt;/li&gt;&#10;&lt;li&gt;The next step is to find the loaded Firefox library that contains the &lt;code&gt;PR_Write&lt;/code&gt; function: the shellcode compares the CRC32C hashes of the loaded library names with 3 values instead of using raw strings. At first, I could only identify &lt;code&gt;nss3.dll&lt;/code&gt; and guessed that another one should be &lt;code&gt;nspr4.dll&lt;/code&gt;, but the hash was different. It turns out that all 3 hashes are &lt;code&gt;nss3.dll&lt;/code&gt;, just spelled in different case: &lt;code&gt;nss3.dll&lt;/code&gt;, &lt;code&gt;NSS3.dll&lt;/code&gt; and &lt;code&gt;NSS3.DLL&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;It gets the address of the &lt;code&gt;PR_Write&lt;/code&gt; function and copies part of the function into the memory allocated in the second step. What's interesting is that the shellcode copies bytes until it finds the following byte sequence: &lt;code&gt;0x003300730073006E&lt;/code&gt;, which is the UTF-16 string &amp;quot;nss3&amp;quot;. Honestly, I don't know why it wouldn't use the exact size here, but it is what it is.&lt;/li&gt;&#10;&lt;li&gt;The last step is to set the memory protection bits and install the following trampoline:&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;push&lt;/span&gt; &lt;span class="nb"&gt;r13&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;push&lt;/span&gt; &lt;span class="nb"&gt;r12&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;movabs&lt;/span&gt; &lt;span class="nb"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;PR_Write_hook&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;movabs&lt;/span&gt; &lt;span class="nb"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;unmodified_PR_Write&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;jmp&lt;/span&gt; &lt;span class="nb"&gt;r12&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;int3&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;The third stage is the &lt;code&gt;PR_Write_hook&lt;/code&gt; itself, which fires every time the original function is called. First of all, it checks that the size of the data being sent is at least 5 bytes, and the first 4 bytes in the buffer are &lt;code&gt;POST&lt;/code&gt;. If so, then the following magic happens:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;It finds the address of the &lt;code&gt;GetProcAddress&lt;/code&gt; function using the same code that was used in the second stage.&lt;/li&gt;&#10;&lt;li&gt;Then, again using the same technique, the shellcode finds the necessary libraries and addresses of the following functions: &lt;code&gt;WSAStartup&lt;/code&gt;, &lt;code&gt;socket&lt;/code&gt;, &lt;code&gt;sendto&lt;/code&gt;, &lt;code&gt;closesocket&lt;/code&gt; and &lt;code&gt;WSACleanup&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;After the initial preparation (roughly speaking, because this preparation and the logic itself are mixed), the following code is executed:&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;sockaddr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mh"&gt;0x02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x39&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x7f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xa0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x20&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_flag&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;WSAStartup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x202&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;wsaData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;SOCKET&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SOCK_DGRAM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IPPROTO_UDP&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;sendto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SOCKADDR&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sockaddr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sockaddr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;closesocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;WSACleanup&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To avoid the hassle with &amp;quot;host&amp;quot; and &amp;quot;network&amp;quot; byte order, I used the following code to see the destination:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;sockaddr_in&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;sockaddr_in&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;INET_ADDRSTRLEN&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;inet_ntop&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sin&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sin_addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ntohs&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sin&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sin_port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;%s:%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;which gave me &lt;code&gt;127.0.0.1:1337&lt;/code&gt;. So, the last question: what is this flag? I would probably cheat at this point and use Wireshark to see it, but as I said at the beginning, the crackme crashed my Firefox, so I had to decrypt it manually. Luckily, there was no rocket science, and the following IDC script will do the trick:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rotate_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mh"&gt;0x13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Sometimes a good disassembler is the only tool you need.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/FFModule" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Maze" by Hack The Box</title><link>https://pugachev.io/2024/03/22/maze-by-hack-the-box/</link><pubDate>Fri, 22 Mar 2024 12:30:59 +0100</pubDate><guid>https://pugachev.io/2024/03/22/maze-by-hack-the-box/</guid><description>&lt;p&gt;This crackme doesn't require a lot of reversing skills but provides plenty of opportunities to learn new tools and techniques. At least I learned a few new things.&lt;/p&gt;&#10;&lt;p&gt;First of all, the archive contains 3 files: a Windows executable, a password-protected zip archive (the usual &amp;quot;hackthebox&amp;quot; password won't work), and a beautiful picture. Let's check what's inside this Windows executable:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; diec Maze.exe&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;PE64&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Linker: Microsoft Linker(14.36.32825)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Compiler: Microsoft Visual C/C&amp;#43;&amp;#43;(19.36.32825)[C]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Tool: Visual Studio(2022 version 17.6)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Packer: PyInstaller&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;This was the first thing I learned: &lt;a href="https://pyinstaller.org/" target="_blank" rel="noopener"&gt;there is a way&lt;/a&gt; to pack a Python program into an executable file for different platforms, with a Python interpreter and all the necessary dependencies inside! Luckily for me, there's no need to dig into its innards and unpack it (although that would be an interesting exercise), since &lt;a href="https://github.com/extremecoders-re/pyinstxtractor" target="_blank" rel="noopener"&gt;an unpacker&lt;/a&gt; already exists, even with a web version.&lt;/p&gt;&#10;&lt;p&gt;Running &lt;code&gt;pyinstxtractor&lt;/code&gt; produces a lot of files, but only 2 of them look interesting: &lt;code&gt;maze.pyc&lt;/code&gt; and &lt;code&gt;obf_path.pyc&lt;/code&gt;, which &lt;code&gt;maze.pyc&lt;/code&gt; imports. The other files seem to be dependencies, dependencies of dependencies, and some kind of glue for PyInstaller to work.&lt;/p&gt;&#10;&lt;p&gt;Writing a decompiler for &lt;code&gt;pyc&lt;/code&gt; files also seems like an interesting challenge, but I cut corners and used &lt;a href="https://github.com/rocky/python-uncompyle6/" target="_blank" rel="noopener"&gt;one that already exists&lt;/a&gt;. Now, the fun begins.&lt;/p&gt;&#10;&lt;p&gt;The first thing &lt;code&gt;maze.py&lt;/code&gt; does is check that the input string is &lt;code&gt;Y0u_St1ll_1N_4_M4z3&lt;/code&gt;, and then it calls the &lt;code&gt;obf_path.obfuscate_route()&lt;/code&gt; function. Decompiling &lt;code&gt;obf_path.pyc&lt;/code&gt; doesn't help much: it &lt;a href="https://docs.python.org/3/library/marshal.html#marshal.loads" target="_blank" rel="noopener"&gt;loads&lt;/a&gt; a binary blob and executes it. The question is how to convert this blob back to Python code.&lt;/p&gt;&#10;&lt;p&gt;Google didn't give me a quick answer on how to generate &lt;code&gt;pyc&lt;/code&gt; files from &lt;code&gt;code&lt;/code&gt; objects, so I had to dig deeper. And that was the second thing I learned. There is a module named &lt;a href="https://docs.python.org/3/library/py_compile.html" target="_blank" rel="noopener"&gt;py_compile&lt;/a&gt; that generates bytecode from a source file, so it should definitely have something related to my question. It turned out that all it does is call a function from &lt;code&gt;importlib&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;invalidation_mode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;PycInvalidationMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;source_stats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;path_stats&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;importlib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_bootstrap_external&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_code_to_timestamp_pyc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_stats&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;mtime&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;source_stats&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;size&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;source_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;importlib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;source_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;importlib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_bootstrap_external&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_code_to_hash_pyc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;source_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invalidation_mode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;PycInvalidationMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CHECKED_HASH&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then it writes this blob to the &lt;code&gt;pyc&lt;/code&gt; file. I thought that the &lt;code&gt;_code_to_timestamp_pyc&lt;/code&gt; function would do some complex things, but no, the &lt;code&gt;pyc&lt;/code&gt; file format is quite simple:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_code_to_timestamp_pyc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mtime&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;Produce the data for a timestamp-based pyc.&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MAGIC_NUMBER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_pack_uint32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_pack_uint32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mtime&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_pack_uint32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source_size&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marshal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So, for our task, we just need to call this function directly with the code object from the &lt;code&gt;obf_path.py&lt;/code&gt; file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;...&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;pyc_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;importlib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_bootstrap_external&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_code_to_timestamp_pyc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;obf_path_unpacked.pyc&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;wb&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pyc_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Decompiling the result with &lt;code&gt;uncompyle6&lt;/code&gt; brings another challenge: the next layer uses &lt;code&gt;lzma&lt;/code&gt; and &lt;code&gt;zlib&lt;/code&gt; to decompress a binary blob and then executes it. If you unpack it and print it, you get Python code that looks a bit weird: between the long lines that assign a string with a lot of repeated &lt;code&gt;__regboss__&lt;/code&gt; to a variable with the same name, there is another &lt;code&gt;exec(loads(...))&lt;/code&gt; call. I googled this string just out of curiosity, and it turns out that this code was generated by a &amp;quot;protector&amp;quot; named &lt;a href="https://github.com/Fadi002/regboss" target="_blank" rel="noopener"&gt;Regboss&lt;/a&gt;. Protector! That compresses the code and puts a variable with a long name around it! The world is going crazy.&lt;/p&gt;&#10;&lt;p&gt;In any case, decompiling the blob using the same technique with &lt;code&gt;importlib&lt;/code&gt; will eventually give you the source code. With the logic simplified a bit, it looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;index_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;maze.png&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;rb&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4817&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2624&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2640&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2720&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;G00d!! you could escape the obfuscated path&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;take this it may help you: &amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;seed(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;for i in range(300):&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt; randint(32,125)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;Be Careful!!!! the route from here is not safe.&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So, there's no flag here, but it looks like a hint for the next step:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;493&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;125&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Back to where we started: after calling the &lt;code&gt;obf_path.obfuscate_route()&lt;/code&gt; function I just described, &lt;code&gt;maze.py&lt;/code&gt; does the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;unpack the &lt;code&gt;enc_maze.zip&lt;/code&gt; archive with the hardcoded password&lt;/li&gt;&#10;&lt;li&gt;read the contents of the &lt;code&gt;maze&lt;/code&gt; file from the archive&lt;/li&gt;&#10;&lt;li&gt;&amp;quot;decrypt&amp;quot; the content using the following logic and write the result to the &lt;code&gt;dec_maze&lt;/code&gt; file:&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;dec_maze&lt;/code&gt; file looks like an ELF binary, but none of the analysis tools can recognize it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; xxd -l &lt;span class="m"&gt;64&lt;/span&gt; dec_maze&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000: 3f45 4c46 0201 0100 0000 5f00 0000 0000 ?ELF......_.....&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000010: 0300 3e00 6000 0000 8010 0000 0000 3d00 ..&amp;gt;.`.........=.&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000020: 4000 0000 0000 0000 4b31 0000 0000 0000 @.......K1......&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000030: 0000 3d00 4000 3800 0d00 4000 6d00 1c00 ..=.@.8...@.m...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; file dec_maze&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dec_maze: data&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; objdump -f ./dec_maze&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;objdump: ./dec_maze: file format not recognized&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;If you look closely, the file is corrupt: the first byte of the ELF magic should be &lt;code&gt;7f&lt;/code&gt;, plus some other fields in the header have strange values too. My idea was that it was not fully &amp;quot;decrypted&amp;quot;, or the decryption algorithm had to be modified somehow to get the correct result.&lt;/p&gt;&#10;&lt;p&gt;And indeed, when I carefully read &lt;code&gt;maze.py&lt;/code&gt; again, I found the problem:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In other words, the second round of the &amp;quot;decryption&amp;quot; algorithm actually does nothing: it xors the data with 0, which leaves it unchanged. My gut feeling told me that I needed to use the &amp;quot;hint&amp;quot; code from the &lt;code&gt;obf_path.obfuscate_route()&lt;/code&gt; function:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;493&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;125&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and voila:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; file dec_maze&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dec_maze: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fda317f523cc4b926eea4e2565e7b9e6390f5aff, for GNU/Linux 3.2.0, stripped&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;Now we finally have a binary to analyze! Disassembling it reveals the following logic (in pseudo-C):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;fgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;H&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;T&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;B&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;flag_index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;flag_index&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In other words, the algorithm compares the sum of every 3 adjacent characters in the input with an integer from the &lt;code&gt;encrypted_flag&lt;/code&gt; array. This means that &lt;code&gt;'H' + 'T' + 'B'&lt;/code&gt; should be equal to &lt;code&gt;0xDE&lt;/code&gt;, then &lt;code&gt;'T' + 'B' + '{'&lt;/code&gt; should be equal to &lt;code&gt;0x111&lt;/code&gt;, and so on.&lt;/p&gt;&#10;&lt;p&gt;To reverse this algorithm, we need to do the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;get the current integer from &lt;code&gt;encrypted_flag&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;subtract the sum of the 2 previous characters from it&lt;/li&gt;&#10;&lt;li&gt;the result is the next character of the flag&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In code it might look like this (in pseudo-Java now, because why not):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;H&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;T&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;B&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;encrypted_flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that's all we need.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Maze" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item></channel></rss>