<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Posts on fahersto&#39;s blog</title>
        <link>https://blog.lentic.de/posts/</link>
        <description>Recent content in Posts on fahersto&#39;s blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <copyright>&lt;a href=&#34;https://creativecommons.org/licenses/by-nc/4.0/&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;CC BY-NC 4.0&lt;/a&gt;</copyright>
        <lastBuildDate>Fri, 12 Dec 2025 00:00:00 +0000</lastBuildDate>
        <atom:link href="https://blog.lentic.de/posts/index.xml" rel="self" type="application/rss+xml" />
        
        <item>
            <title>Reflecting on game engine development</title>
            <link>https://blog.lentic.de/posts/gameengine_lessons/</link>
            <pubDate>Fri, 12 Dec 2025 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/gameengine_lessons/</guid>
            <description>Game engine tech stack As some of you may know, I have spent a considerable amount of time developing a game engine in C++. Toward the end of the year, I want to take some time to reflect on where I&amp;rsquo;m at and how I plan to move forward. Let us begin with a list of libraries that are currently used:
Rendering: OpenGL Physics: Jolt Audio: FMOD GUI: ImGui, ImGuizmo, ImNodes Text: msdf-gen Window: GLFW Serialization: yaml-cpp Testing: googletest Scripting: sol3 Navigation: recastnavigation Asset Importing: assimp, ufbx Logging: spdlog Profiling: tracy Graphics Debugging: RenderDoc Feature showreel Building on these libraries we implemented several features.</description>
            <content type="html"><![CDATA[<h2 id="game-engine-tech-stack">Game engine tech stack</h2>
<p>As some of you may know, I have spent a considerable amount of time developing a game engine in C++. Toward the end of the year, I want to take some time to reflect on where I&rsquo;m at and how I plan to move forward. Let us begin with a list of libraries that are currently used:</p>
<ul>
<li><strong>Rendering</strong>: <a href="https://www.opengl.org/">OpenGL</a></li>
<li><strong>Physics</strong>: <a href="https://github.com/jrouwe/JoltPhysics">Jolt</a></li>
<li><strong>Audio</strong>: <a href="https://www.fmod.com/">FMOD</a></li>
<li><strong>GUI</strong>: <a href="https://github.com/ocornut/imgui">ImGui</a>, <a href="https://github.com/CedricGuillemet/ImGuizmo">ImGuizmo</a>, <a href="https://github.com/Nelarius/imnodes">ImNodes</a></li>
<li><strong>Text</strong>: <a href="https://github.com/Chlumsky/msdfgen">msdf-gen</a></li>
<li><strong>Window</strong>: <a href="https://www.glfw.org/">GLFW</a></li>
<li><strong>Serialization</strong>: <a href="https://github.com/jbeder/yaml-cpp">yaml-cpp</a></li>
<li><strong>Testing</strong>: <a href="https://github.com/google/googletest">googletest</a></li>
<li><strong>Scripting</strong>: <a href="https://github.com/ThePhD/sol2">sol3</a></li>
<li><strong>Navigation</strong>: <a href="https://github.com/recastnavigation/recastnavigation">recastnavigation</a></li>
<li><strong>Asset Importing</strong>: <a href="https://github.com/assimp/assimp">assimp</a>, <a href="https://github.com/ufbx/ufbx">ufbx</a></li>
<li><strong>Logging</strong>: <a href="https://github.com/gabime/spdlog">spdlog</a></li>
<li><strong>Profiling</strong>: <a href="https://github.com/wolfpld/tracy">tracy</a></li>
<li><strong>Graphics Debugging</strong>: <a href="https://renderdoc.org/">RenderDoc</a></li>
</ul>
<h2 id="feature-showreel">Feature showreel</h2>
<p>Building on these libraries we implemented several features. So let us show some of these.</p>
<h3 id="pbr-rendering">PBR Rendering</h3>
<p>Physically based rendering has become the standard for rendering realistic graphics. Therefore, it is the default rendering method in our game engine. We even support editing the PBR shader using a node based approach which many developers have to come to expect ever since Unreal Engine introduced its blueprints.
<video width="100%" height="auto" class="video-shortcode" preload="metadata" controls>
    <source src="/gameengine_lessons/shadergraph_pbr.mkv" type="video/mp4" >
    There should have been a video here but your browser does not seem
    to support it.
</video>
</p>
<hr>
<h3 id="particle-systems">Particle Systems</h3>
<p>Another fundamental feature of a game engine is a particle system. Even with a relatively small set of configurable parameters, a wide variety of effects can be achieved. Our system allows configuration of texture, size, lifetime, color, velocity, and gravity all over the lifetime of a particle. Additionally, each paramter can have a variance that is applied to each emitted particle individually. This enables effects such as hearts, which move upward and sideways at different speeds while shrinking over time as can be seen below.</p>
<p><img alt="particle_system_hearts" src="/gameengine_lessons/particle_system_hearts_small.gif"></p>
<p>The same system can also be used to create a fountain-like effect, where each particle is animated over time, affected by gravity, and initialized with a randomized velocity.</p>
<p><img alt="particle_system_animated" src="/gameengine_lessons/particle_system_animated.gif"></p>
<hr>
<h3 id="animations">Animations</h3>
<p>Implementing animation support has been one of the most time-consuming aspects of reaching the current state of the engine. There are many different asset file formats that support animations, and even within a single format there are often multiple ways to represent movement. Combined with additional data such as scaling calculation based on the node hierarchy, there are many cases to handle animation properly.</p>
<p>When testing a new model, our first step is to verify whether other tools or websites can import it correctly. There is an overwhelming number of free models with animations that simply did not properly work in any other tools we tested. Based on our testing, <a href="https://sketchfab.com">Sketchfab</a> provides one of the most robust online model viewers. As a result, our goal was to support any model inside the engine that also works correctly on their platform.</p>
<p>In our opinion, it is always worth creating debug visualizations. The following GIF shows bone weight visualization of a dancing stormtrooper driven by skeletal animation.</p>
<p><img alt="animation_weights" src="/gameengine_lessons/dancing_stormtrooper_weights.gif"></p>
<p>Additionally, the engine provides visualization of the currently selected mesh, as well as options to display the skeleton and render the mesh in wireframe mode.</p>
<video width="100%" height="auto" class="video-shortcode" preload="metadata" controls>
    <source src="/gameengine_lessons/skeleton_pbr.mkv" type="video/mp4" >
    There should have been a video here but your browser does not seem
    to support it.
</video>

<hr>
<h3 id="physics">Physics</h3>
<p>Our Jolt physics integration is still in its early stages. Determinism issues were one of the main reasons we introduced testing. At the moment, the engine supports several collision shapes. In the following video, a mesh collision shape interacts with the floor in multiple scenarios. If the collision response appears incorrect, this is due to poorly configured physics materials and the fact that the simulation body of the mesh does not match the visual representation particularly well.</p>
<video width="100%" height="auto" class="video-shortcode" preload="metadata" controls>
    <source src="/gameengine_lessons/physics_mesh.mp4" type="video/mp4" >
    There should have been a video here but your browser does not seem
    to support it.
</video>

<hr>
<h3 id="text-rendering">Text rendering</h3>
<p>Text rendering turned out to be a far more involved process than we initially expected. For that reason, there exists a separate blog post that discusses this topic in greater detail. For this post, simply enjoy this text component of our game engine. Text can be freely rotated, translated, and scaled in the editor while remaining crisp at all times thanks to MSDF-based text rendering. Also you basically get an outline for free when using this rendering method.</p>
<p><img alt="msdf_textrendering" src="/gameengine_lessons/msdf_textcomponent.gif"></p>
<h2 id="limitations">Limitations</h2>
<p>Since this is a solo project, almost none of the engine’s features are fully finished. However, the following are some of the most significant limitations that currently hinder creating and shipping a game with the engine.</p>
<p><strong>Lights</strong><br>
Only a single directional light and up to four point lights are supported. This is mainly due to the renderer being a simple forward renderer and for each mesh rendered each light in the scene has to be iterated over. This does not scale. We do not regret starting this project with such a simple renderer though since the learning curve to graphcis programming is steep enough even when choosing a simple rendering approach.</p>
<p><strong>Shipping</strong><br>
There is no automated packaging or asset cooking pipeline. Shiping a game currently make it the response of the developer to copy all required assets manually to the right folder. This does not scale and is error prone.</p>
<h2 id="regrets">Regrets</h2>
<p>Our main motivation to write this post is to force ourselves to reflect on issues we encountered during game engine development in the hopes to help others and our future selves.</p>
<h3 id="testing">Testing</h3>
<p>By far our biggest regret is introducing testing far too late in the development process. But why?</p>
<ol>
<li>
<p>We started this project roughly five years ago. At the time, I was still at university and had little understanding of how to write code that can be maintained and extended over many years. In my opinion, testing is an integral part of writing high-quality software a mindset that has since been reinforced by my day job as a Java software engineer.</p>
</li>
<li>
<p>Testing a game engine is not straightforward. Most of my professional experience is with Java Spring applications, which can be tested quickly and easily using tools such as JUnit. Spinning up an entire game engine as part of a test suite is a new task for me. We also introduced many side effects for example by having too much global state. Therefore, the order in which tests run work differently.</p>
</li>
<li>
<p>Testing is not inherently fun, and this is a hobby project. That said, we have since experienced the satisfaction of being able to make large changes without constantly worrying about breaking core functionality, thanks to a solid test suite. The longer a project lives and grows, the more value and enjoyment you get out of the tests that were previously written.</p>
</li>
</ol>
<hr>
<h3 id="moving-too-quick">Moving too quick</h3>
<p>A game engine is a really nice hobby project because there area so many things to implement that you can learn a lot of different things and change up the topics you work on when you do feel like it. There is a downside of working on a big software project like a game engine with this mindset though. You (or atleast I)tend to get things to a &lsquo;working&rsquo; state, instead of a correct state. The shortcuts you took years ago tend to catch you off guard much later on. Additionally, writing good documentation is crucial. We spent so much time debugging issues that in the end turned out to be just a function that did not properly handle a certain case, had a confusing name or was not implemented at all. Switching topics really encourages such issues to pile up. One thing that could help in such cases is writing tickets with acceptence criteria. At that point it just becomes too close to work for my liking. This is supposed to be a hobby you mostly do for fun after all.</p>
<hr>
<h3 id="assimp">Assimp</h3>
<p>Assimp is great because it supports a large number of file formats that can be used to import assets into a game engine. However, assimp is extremely slow in debug mode. Additionally, almost every time we updated to a newer assimp version and re-imported the same <code>.fbx</code> models, something broke. While this is not entirely assimp’s fault (FBX itself is notoriously complex) it was still very frustrating and time-consuming to fix.</p>
<p>Skeleton animations in particular often broke between versions, and debugging issues with assimp is painful due to its terrible performance in debug builds. At this point, we no longer use assimp at all. Instead, we rely on specialized libraries that focus on a single file format and aim to get that one format right, rather than providing a high-level abstraction that has to support many formats at once. So for FBX files we integrated ufbx.</p>
<h2 id="future">Future</h2>
<p><strong>Graphics API</strong></p>
<p>We really want to move to a newer graphics API. The global state of OpenGL and single thread context has been limiting testing aswell as performance. However, we do not regret having started with OpenGL. The learning curve of vulkan is so much steeper and it takes much more upfront work to get the couple first hits of dopamine when you actually see something on screen.</p>
<p><strong>Linux support</strong></p>
<p>We really want to support linux since gaming is the chain that forces us to still run windows. Due to a specific feature of our game engine, linux support has not been feasible with OpenGL. Since we decided to move to Vulkan on our next iteration of the engine we should be able to support linux!</p>
<p><strong>Releasing a game</strong></p>
<p>One of our main goals when creating a game engine is to ship a game that is built with the engine. Sadly, this goal will have to be postponed by a couple of years because of the decision to move to Vulkan alone, since this is a new technology to us.</p>
<p><strong>Game engine 2.0</strong></p>
<p>Over the past couple of years we have learned a lot about game engine development. This allows us to make better design decisions regarding game engine architecture. Therefore, we decided to not only switch from OpenGL to Vulkan but rewrite the entire game engine from scratch. Taking the lessons learned and referencing code of our first version to create a much more robust game engine that can actually be used to release a game.</p>
]]></content>
        </item>
        
        <item>
            <title>Process Doppelgänging - code injection technique</title>
            <link>https://blog.lentic.de/posts/process_doppelganging/</link>
            <pubDate>Sun, 03 Dec 2023 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/process_doppelganging/</guid>
            <description>What is Process Doppelgänging? Process Doppelgänging is a code injection technique which allows to load and execute arbitrary code in the context of a benign process without calling Windows API functions commonly invoked to achieve code injection. The technique was published by Tal Liberman and Eugene Kogan at Black Hat Europe 2017. The concept is to abuse NTFS transactions to create a process from a malicious section that is seemingly backed by a benign file.</description>
            <content type="html"><![CDATA[<h2 id="what-is-process-doppelgänging">What is Process Doppelgänging?</h2>
<p>Process Doppelgänging is a code injection technique which allows to load and execute arbitrary code in the context of a benign process without calling Windows API functions
commonly invoked to achieve code injection. The technique was published by <a href="https://www.blackhat.com/docs/eu-17/materials/eu-17-Liberman-Lost-In-Transaction-Process-Doppelganging.pdf">Tal Liberman
and Eugene Kogan at Black Hat Europe 2017</a>. The concept is to abuse NTFS transactions to create a process from a malicious section that is seemingly backed by a benign file.
From an attacker’s point of view this is desirable as antivirus software may fail to scan the
code of the malicious section and analyze the content of the benign file instead.</p>
<p><img alt="ProcessDoppelgänging" src="/processdoppelganging/process_doppelganging.png"></p>
<p>The image above illustrates the concept of Process Doppelgänging by creating a malicious notepad
process from an overwritten svchost executable. The first step is opening a benign binary file
such as svchost.exe in a transaction. In the next step, the file is overwritten by the malicious
payload which is then mapped into memory. Subsequently, the transaction is rolled back
which restores the changes on the file system. The section mapped into memory is not
affected by this rollback. This results in a payload section in memory that is linked to a
benign file such as svchost.exe. Then, a process based on the payload section is created by
calling the low level Windows API function NtCreateProcessEx. Finally, the attacker creates
the first thread of the process to start execution at the start address of the payload.</p>
<p>This technique is still prevalent on Windows 7 at the time of writing, but has been patched
on Windows 10 where a <a href="https://github.com/hasherezade/process_doppelganging/issues/3">Windows Defender driver blocks process creation</a> from files with
pending transactions. Since this method requires direct calls to low level Windows
API functions, support for WOW64 must be implemented manually. We could not find
an implementation providing WOW64 support. According to malware analyst hasherezade,
implementing WOW64 support is feasible but tedious, since it <a href="https://github.com/hasherezade/process_ghosting/issues/4">requires manually filling</a> in
many parameters that otherwise are handled by the Windows loader.</p>
<p>In 2018 <a href="https://thehackernews.com/2019/07/process-doppelganging-malware.html">security researchers at Kaspersky Lab</a> identified that the SynAck ransomware employed this technique. Later, security researchers from enSilo uncovered that more than
20 malware families had already employed this technique in 2018. They discovered a
loader they named TxHollower to bring Process Doppelgänging to malware families such as
FormBook, LokiBot and SmokeLoader.
We suspect that malware performs Process Doppelgänging when it invokes the NTFS transaction API since transactions are rarely created. We dump the process when NtCreateThreadEx is called to start execution.</p>
<h2 id="prototype-implementation">Prototype implementation</h2>
<p>My Process Doppelgänging implementation is available on <a href="https://github.com/Fahersto/code_injection/blob/master/pe_injection/process_doppelganging.cpp">my GitHub</a>.</p>
]]></content>
        </item>
        
        <item>
            <title>MSDF font rendering in OpenGL</title>
            <link>https://blog.lentic.de/posts/textrendering/</link>
            <pubDate>Wed, 06 Jul 2022 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/textrendering/</guid>
            <description>Text rendering One of the latest additions to my OpenGL game engine is text rendering. TIt took more effort than I expected, and that&amp;rsquo;s why this article exists. The OpenGL standard does not define text rendering. This was surprising to me at first, because while OpenGL is a low-level graphics API, rendering text seemed pretty low-level to me. Boy was I wrong. To ensure the correct placement of individual glyphs, a number of variables must be taken into account, as shown in the following figure:</description>
            <content type="html"><![CDATA[<h2 id="text-rendering">Text rendering</h2>
<p>One of the latest additions to my OpenGL game engine is text rendering. TIt took more effort than I expected, and that&rsquo;s why this article exists. The OpenGL standard does not define text rendering. This was surprising to me at first, because while OpenGL is a low-level graphics API, rendering text seemed pretty low-level to me. Boy was I wrong. To ensure the correct placement of individual glyphs, a number of variables must be taken into account, as shown in the following figure:</p>
<p><img alt="glpyh_metrics" src="/textrendering/glyph_metrics.png">
Source: <a href="https://learnopengl.com/In-Practice/2D-Game/Render-text">https://learnopengl.com/In-Practice/2D-Game/Render-text</a></p>
<p>As if that wasn&rsquo;t annoying enough, there is not only text from left to right, but also from right to left and even vertical text. Also there exist quite a few more letters than the ASCII range. If you aren&rsquo;t convinced yet, get your blood boiling by reading the article <a href="https://gankra.github.io/blah/text-hates-you/">Text Rendering Hates You</a> by Aria Beingessner. It seems like there is no implementation that gets everything perfect. So yes, maybe rendering text is not as easy as I thought.</p>
<p>In addition to the computer-unfriendly way written language works, there are also different methods to achieve font rendering.</p>
<h2 id="legacy-font-rendering">Legacy font rendering</h2>
<p>The basic implementation uses a texture of all characters and cuts out the letters it needs to combine them into a word. This implementation works, but it has the big disadvantage that we can&rsquo;t really scale the text well. Therefore, our font atlas texture would have to have a huge resolution to make large letters look good on for example a 4k screen. This would end up taking precious memory on our graphics card and decrease performance of our font rendering. Moreover, we might face issues when downscaling our characters in case they only occupy little space on a monitor with lower resolution. That&rsquo;s why we need something better.</p>
<h2 id="sdf">SDF</h2>
<p>In a Signed Distance Field (SDF) each point that is part of a defined space is assigned a positive value that corresponds to the smallest distance to any point outside of the space. Each point outside the space is represented by a negative value which corresponds to the smallest distance to any point inside the space. We can write these distances into a channel of a texture (instead of the letter itself) to describe the shape of a character and then use these distances in our shader to construct the letter in an arbitrary size. Since constructing the character in our shader based on the texture can be done very efficiently  this only introduces minimal overhead while allowing for a font atlas texture with much smaller resolution. Here is the result of creating the letter &ldquo;A&rdquo; from a 16x16 SDF texture:</p>
<p><img alt="sdf_A" src="/textrendering/sdf_A.png"></p>
<p>Source: <a href="https://github.com/Chlumsky/msdfgen">https://github.com/Chlumsky/msdfgen</a></p>
<h2 id="msdf">MSDF</h2>
<p>A Multi-Channel Signed Distance Field (MSDF) expands the idea of encoding the shape into a texture to all three color channels. This further improves the quality of the rendered text. This method has been published by Viktor Chlumský in a <a href="https://github.com/Chlumsky/msdfgen">GitHub repository</a>. THis master&rsquo;s thesis on this topic can also be found there and is well worth reading. Here is the result of creating the letter &ldquo;A&rdquo; from a 16x16 MSDF texture:</p>
<p><img alt="msdf_A" src="/textrendering/msdf_A.png"></p>
<p>Source: <a href="https://github.com/Chlumsky/msdfgen">https://github.com/Chlumsky/msdfgen</a></p>
<p>Observe how much sharper the character construction turned out, even though our font atlas texture has the same resolution!</p>
<h3 id="pain">Pain</h3>
<p>After researching various methods of text rendering, I decided to use MSDF. Luckily, Chlumský also provided a project that generates an entire font atlas instead of individual characters (<a href="https://github.com/Chlumsky/msdf-atlas-gen">msdf-atlas-gen</a>). Furthermore, it compresses them into a single texture with all the metadata.</p>
<p>However, in my opinion this project lacks usability. To implement MSDF atlas generation into my game engine I had to dig deep into the code of the CLI tool and other resources. Here are some that helped me:</p>
<p>Viktor Chlumský:</p>
<ul>
<li><a href="https://github.com/Chlumsky/msdf-atlas-gen/discussions/18">https://github.com/Chlumsky/msdf-atlas-gen/discussions/18</a></li>
<li><a href="https://github.com/Chlumsky/msdf-atlas-gen/discussions/30">https://github.com/Chlumsky/msdf-atlas-gen/discussions/30</a></li>
</ul>
<p>Michael Martz:</p>
<ul>
<li><a href="https://github.com/theOtherMichael/Enterprise/blob/master/Engine/src/Systems/Renderer2D/R2D_Text.cpp">https://github.com/theOtherMichael/Enterprise/blob/master/Engine/src/Systems/Renderer2D/R2D_Text.cpp</a></li>
<li><a href="https://www.youtube.com/watch?v=OI1uGNhdnmA">https://www.youtube.com/watch?v=OI1uGNhdnmA</a></li>
<li><a href="https://www.youtube.com/watch?v=oLb7xiRq4eQ">https://www.youtube.com/watch?v=oLb7xiRq4eQ</a></li>
</ul>
<h2 id="pain-relief">Pain relief</h2>
<p>I made this very basic sample implementation <a href="https://github.com/Fahersto/OpenGL_msdf">https://github.com/Fahersto/OpenGL_msdf</a> which I hope may prevent you from suffering as much as I did. As you can see some parts of it are heavily based on code by Chlumský and Martz. Any improvements to this repository are very much welcome. The intent of this project is to make other people not spend solid 2 weeks on implementing MSDF font rendering. Just open a pull request!</p>
<h2 id="demo-inside-my-2d-engine">Demo inside my 2D engine</h2>
<p>Finally, some of that CSI: Miami zoom that MSDF rendering allows:
<img alt="msdf_font_rendering" src="/textrendering/msdf.gif"></p>
]]></content>
        </item>
        
        <item>
            <title>Parametric spline interpolation</title>
            <link>https://blog.lentic.de/posts/spline/</link>
            <pubDate>Sun, 20 Mar 2022 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/spline/</guid>
            <description>Natural cubic spline Piecewise spline interpolation fits cubic polynomials through a set of points. In contrast to utilizing a polynomial of a higher degree, this results in a smooth interpolation that stays much closer to the target points. The interpolation is based on a t value. The t value has to be monotonically increasing. This in turn means that a regular spline can only ever go into one direction. However, parametric splines overcome this limitation.</description>
            <content type="html"><![CDATA[<h2 id="natural-cubic-spline">Natural cubic spline</h2>
<p>Piecewise spline interpolation fits cubic polynomials through a set of points. In contrast to utilizing a polynomial of a higher degree, this results in a smooth interpolation that stays much closer to the target points. The interpolation is based on a <em>t</em> value. The <em>t</em> value has to be monotonically increasing. This in turn means that a regular spline can only ever go into one direction. However, parametric splines overcome this limitation.</p>
<h3 id="the-math">The math</h3>
<p>The construction of a natural cubic spline is described in &ldquo;Numerical Analysis&rdquo; by Richard L. Burden and J. Douglas Faires. In my implementation the <em>t</em> value increases by one for every segment.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">ComputeCubicSpline</span>(<span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> values, <span style="color:#66d9ef">int</span> valueCount)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> x <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[valueCount];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> a <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[valueCount];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">&lt;</span> valueCount; i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">// segment t values: [0,1], [1,2] This is asumed when finding the segment to a t value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        x[i] <span style="color:#f92672">=</span> i; 
</span></span><span style="display:flex;"><span>        a[i] <span style="color:#f92672">=</span> values[i];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (valueCount <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Error: Trying to compute spline without points</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// number of segments is equal to number of points - 1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">int</span> segmentCount <span style="color:#f92672">=</span> valueCount <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Implementation of the books algorithm
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#75715e">// allocate memory
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> b <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> c <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> d <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> h <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">&lt;=</span> segmentCount <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>; i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        h[i] <span style="color:#f92672">=</span> x[i <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> x[i];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 2
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> alpha <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>; i <span style="color:#f92672">&lt;=</span> segmentCount <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>; i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        alpha[i] <span style="color:#f92672">=</span> (<span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> h[i]) <span style="color:#f92672">*</span> (a[i <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> a[i]) <span style="color:#f92672">-</span> (<span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> h[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>]) <span style="color:#f92672">*</span> (a[i] <span style="color:#f92672">-</span> a[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 3
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#75715e">// step 3,4,5 and part of 6 solve tridiagonal system
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> l <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> u <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> z <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">double</span>[segmentCount <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>    l[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    u[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    z[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 4
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>; i <span style="color:#f92672">&lt;=</span> segmentCount <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>; i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        l[i] <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span> <span style="color:#f92672">*</span> (x[i <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> x[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>]) <span style="color:#f92672">-</span> h[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">*</span> u[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>        u[i] <span style="color:#f92672">=</span> h[i] <span style="color:#f92672">/</span> l[i];
</span></span><span style="display:flex;"><span>        z[i] <span style="color:#f92672">=</span> (alpha[i] <span style="color:#f92672">-</span> h[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">*</span> z[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>]) <span style="color:#f92672">/</span> l[i];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] x;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] alpha;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 5
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    l[segmentCount] <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    z[segmentCount] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    c[segmentCount] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] l;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// step 6
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> j <span style="color:#f92672">=</span> segmentCount <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>; j <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">0</span>; j<span style="color:#f92672">--</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        c[j] <span style="color:#f92672">=</span> z[j] <span style="color:#f92672">-</span> u[j] <span style="color:#f92672">*</span> c[j <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>        b[j] <span style="color:#f92672">=</span> (a[j <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> a[j]) <span style="color:#f92672">/</span> h[j] <span style="color:#f92672">-</span> h[j] <span style="color:#f92672">*</span> (c[j <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">+</span> <span style="color:#ae81ff">2</span> <span style="color:#f92672">*</span> c[j]) <span style="color:#f92672">/</span> <span style="color:#ae81ff">3</span>;
</span></span><span style="display:flex;"><span>        d[j] <span style="color:#f92672">=</span> (c[j <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> c[j]) <span style="color:#f92672">/</span> (<span style="color:#ae81ff">3</span> <span style="color:#f92672">*</span> h[j]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] h;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] u;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] z;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">&lt;</span> segmentCount; i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">// store polynomials
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        polynomial_.push_back(Polynomial(d[i], c[i], b[i], a[i]));
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] a;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] b;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] c;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">delete</span>[] d;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>To get a position on the spline we just enter a <em>t</em> value into the corresponding polynomial. Since we know that <em>t</em> is increased by one for every segment, we can directly use <em>t</em> to lookup the corresponding segment/polynomial.</p>
<h2 id="parametric-spline">Parametric spline</h2>
<p>Parametric splines are based on a cubic spline for each dimension. Therefore, a 2D spline consists of two cubic splines and a 3D spline of three.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">ComputeParametricSpline</span>(<span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> x, <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> y, <span style="color:#66d9ef">double</span><span style="color:#f92672">*</span> z, <span style="color:#66d9ef">int</span> valueCount)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    splineX_ <span style="color:#f92672">=</span> CubicSpline();
</span></span><span style="display:flex;"><span>    splineY_ <span style="color:#f92672">=</span> CubicSpline();
</span></span><span style="display:flex;"><span>    splineZ_ <span style="color:#f92672">=</span> CubicSpline();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    splineX_.Compute(x, valueCount);
</span></span><span style="display:flex;"><span>    splineY_.Compute(y, valueCount);
</span></span><span style="display:flex;"><span>    splineZ_.Compute(z, valueCount);
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>To get the position on a three dimensional parametric spline, we just pass our t value to each of our splines:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>Vector3 <span style="color:#a6e22e">eval</span>(<span style="color:#66d9ef">float</span> t)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> Vector3(splineX_.eval(t), splineY_.eval(t), splineZ_.eval(t));
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Since we now have a cubic spline for each dimension, we can move freely into any direction and also intersect ourselves.</p>
<h2 id="demo-inside-my-2d-engine">Demo inside my 2D engine</h2>
<p>This demo constructs a 2D parametric spline from points added by clicking. Furthermore, it animates a point on the spline by passing the current time as <em>t</em> value.
<video width="100%" height="auto" class="video-shortcode" preload="auto" controls>
    <source src="/spline/spline.mp4" type="video/mp4" >
    There should have been a video here but your browser does not seem
    to support it.
</video>
</p>
<h2 id="prototype-implementation">Prototype implementation</h2>
<p>My parametric spline implementation is available on <a href="https://github.com/Fahersto/ParametricSpline">my GitHub</a>.</p>
]]></content>
        </item>
        
        <item>
            <title>GhostWriting - advanced code injection technique</title>
            <link>https://blog.lentic.de/posts/ghostwriting/</link>
            <pubDate>Sat, 19 Mar 2022 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/ghostwriting/</guid>
            <description>I recently had the chance to study several code injection techniques in-depth. Specifically Host-Based Code Injection Attacks (HBCIAs). This term was introduced to distinguish code injection attacks that target the local system from ones that target remote systems such as SQL injection. I have implemented 22 HBCIA techniques over the last couple of months and the GhostWriting technique stood out to me in particular.
What is GhostWriting? GhostWriting is an advanced code injection technique that combines thread hijacking, a write-gadget to write to an arbitrary memory location and an endless loop to stall execution.</description>
            <content type="html"><![CDATA[<p>I recently had the chance to study several code injection techniques in-depth. Specifically Host-Based Code Injection Attacks (HBCIAs). This term was introduced to distinguish code injection attacks that target the local system from ones that target remote systems such as SQL injection. I have implemented 22 HBCIA techniques over the last couple of months and the GhostWriting technique stood out to me in particular.</p>
<h2 id="what-is-ghostwriting">What is GhostWriting?</h2>
<p><a href="http://blog.txipinet.com/2007/04/05/69-a-paradox-writing-to-another-process-without-openning-it-nor-actually-writing-to-it/">GhostWriting</a> is an advanced code injection technique that combines thread hijacking, a write-gadget to write to an arbitrary memory location and an endless loop to stall execution.</p>
<h3 id="implementation-details">Implementation details</h3>
<p>The endless loop and write-gadgets in our implementation are located inside ntdll.dll. This
ensures that they are available in every process:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-asm" data-lang="asm"><span style="display:flex;"><span><span style="color:#75715e">; endless loop		; 32-bit write-gadget		; 64-bit write-gadget
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">jmp</span> <span style="color:#ae81ff">0x0</span>				<span style="color:#66d9ef">mov</span>[<span style="color:#66d9ef">ecx</span>], <span style="color:#66d9ef">edx</span>				<span style="color:#66d9ef">mov</span> [<span style="color:#66d9ef">rbx</span>], <span style="color:#66d9ef">r14</span>
</span></span><span style="display:flex;"><span>						<span style="color:#a6e22e">ret</span>							<span style="color:#66d9ef">mov</span> <span style="color:#66d9ef">rbx</span>, [<span style="color:#66d9ef">rsp</span> <span style="color:#960050;background-color:#1e0010">+</span> <span style="color:#ae81ff">30</span><span style="color:#66d9ef">h</span>]
</span></span><span style="display:flex;"><span>													<span style="color:#a6e22e">add</span> <span style="color:#66d9ef">rsp</span>, <span style="color:#ae81ff">20</span><span style="color:#66d9ef">h</span>
</span></span><span style="display:flex;"><span>													<span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">r14</span>
</span></span><span style="display:flex;"><span>													<span style="color:#a6e22e">retn</span>	
</span></span></code></pre></div><p>The following discussion of the GhostWriting technique is based on the 32-bit write-gadget.
The 64-bit version is implemented analogously, but has to account for side effects caused by
the 64-bit write-gadget. Our 64-bit write-gadget contains side effects since we have to avoid registers that are not set reliably when calling <a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadcontext">SetThreadContext</a> or <a href="http://www.codewarrior.cn/ntdoc/wrk/ps/NtSetContextThread.htm">NtSetContextThread</a>. This happens whenever we suspended our thread during a system call as Sam Russel describes in <a href="https://www.lodsb.com/shellcode-injection-using-threadnameinformation">his brilliant blog post</a>.</p>
<p>The first step when performing GhostWriting is to write the endless loop onto a fabricated
stack. This is achieved with thread hijacking as described in algorithm 1.</p>
<p><img alt="ghostwriting_algorithm" src="/ghostwriting/ghostwriting_algorithm.png"></p>
<p>When the instruction pointer of the thread equals the address of the endless loop, we know
that the thread has written the address of the endless loop to the stack and is now stuck
inside the loop. This is the signal that the data has been written and we can perform the next
operation. Afterwards, the ROP chain is written by repeating algorithm 1 and modifying
the source and destination on every invocation. We can only write 4 bytes in a 32-bit process
and 8 bytes in 64-bit process per invocation, since we are limited by the amount of data a
single register can hold.</p>
<p>Now that we have successfully set up our fake stack, we still need to make the stack executable
to be able to execute our payload. This is achieved by crafting a ROP chain that calls
<a href="https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect">VirtualProtect</a> to add the execute flag to the corresponding memory pages. Since the calling
convention of the function is fastcall, we set up the parameters by writing the corresponding
values to RCX, RDX, R8 and R9.</p>
<p>One challenge when implementing GhostWriting is executable memory. I saw many implementations that took shortcuts such as only injecting into the local process or allocating remote executable memory by calling <a href="https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualallocex">VirtualAllocEx</a>. However, our implementation stays true to the concept and creates executable on the stack by executing a ROP chain that calls <a href="https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect">VirtualProtect</a> to add the execute flag to the stack. A ROP chain allows to control execution flow via the stack, as each gadget performs its instructions and finishes with a RET instruction that transfers the control flow to the next gadget in the chain on top of the stack. Our ROP chain only uses gadgets that can be found in <em>ntdll.dll</em> since every user mode process maps ntdll. Here it is:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-asm" data-lang="asm"><span style="display:flex;"><span><span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">rdi</span><span style="color:#75715e">; ret
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; VirtualProtectAddress
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">rcx</span><span style="color:#75715e">; ret
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; targetAddress,
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">rdx</span><span style="color:#75715e">; pop r11; ret
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; size
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; trash r11 (gadget sideeffect)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">r8</span><span style="color:#75715e">; ret
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; newProtection (PAGE_EXECUTE_READWRITE)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pop</span> <span style="color:#66d9ef">r9</span><span style="color:#75715e">; pop r10; pop r11; ret
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; oldProtection (just some pointer to writeable memory)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; trash r10 (gadget sideeffect)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; trash r11 (gadget sideeffect)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">push</span> <span style="color:#66d9ef">rdi</span><span style="color:#75715e">; ret (this instruction calls VirtualProtect since we put its address into rdi earlier)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">; Address of the written shellcode on the stack. VirtuaProtect will use this address to return to after its call. 
</span></span></span></code></pre></div><p>Instead of the instructions itself we write the virtual addresses of these instructions inside ntdll to our fabricated stack. The commented lines are also filled in during runtime.</p>
<p>Next, the shellcode is written to the stack. The final step is to execute the ROP chain. This
is done by suspending the thread and setting its program counter to the first gadget of the
ROP chain and its stack pointer to the address of the second gadget on the stack. When
the thread is resumed, it first executes the ROP chain which marks the stack as executable
followed by execution of the payload on the stack.</p>
<h2 id="demo">Demo</h2>
<p><img alt="ghostwriting" src="/ghostwriting/ghostwriting.gif">
Notice that the hijacked thread will only write our payload that spawns a MessageBox when we hover the notepad
window.</p>
<h2 id="prototype-implementation">Prototype implementation</h2>
<p>My GhostWriting implementation (tested on Windows 10 Build 19043) is available on <a href="https://github.com/Fahersto/code_injection/blob/master/shellcode_injection/ghostwriting_injection.cpp">my GitHub</a>.</p>
]]></content>
        </item>
        
        <item>
            <title>Register handle operation callbacks from unsigned drivers with this one weird trick</title>
            <link>https://blog.lentic.de/posts/obregistercallbacks/</link>
            <pubDate>Fri, 18 Mar 2022 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/obregistercallbacks/</guid>
            <description>Monitor handle acquisition Acquiring a handle to a target process is a critical step in many code injection techniques. The Windows operating system exposes a mechanism that allows kernel mode drivers to supply handle operation callbacks. These callbacks can be registered by calling ObRegisterCallbacks. When a handle is created or duplicated the pre-operation callback is invoked before the operation is performed and the post-operation callback after the operation occurred. This mechanism is utilized by many antivirus and Anti-Cheat solutions to protect processes from code injection.</description>
            <content type="html"><![CDATA[<h2 id="monitor-handle-acquisition">Monitor handle acquisition</h2>
<p>Acquiring a handle to a target process is a critical step in many code injection techniques. The Windows operating system exposes a mechanism that allows kernel mode drivers to supply handle operation callbacks. These callbacks can be registered by calling <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-obregistercallbacks">ObRegisterCallbacks</a>. When a handle is created or duplicated the pre-operation callback is invoked before the operation is performed and the post-operation callback after the operation occurred. This mechanism is utilized by many antivirus and Anti-Cheat solutions to protect processes from code injection. For more information about how this mechanism can be employed to protect processes and how to overcome these protections take a look at <a href="https://revers.engineering/superseding-driver-altitude-checks-on-windows/">this excellent article</a> by Daax Rynd.</p>
<p>Since I do not own a valid driver certificate our first task is to load our unsigned driver. This can be accomplished by enabling <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/install/the-testsigning-boot-configuration-option">TESTSIGNING</a>. Enabled test signing can be detected from user mode by querying the SystemCodeIntegrityInformation through NtQuerySystemInformation. Therefore, it is reasonable to assume that whatever we want to monitor may check whether test signing is enabled and alter its behavior accordingly. Therefore, we choose the other option which is loading the driver using an exploit.</p>
<h2 id="kdmapper">Kdmapper</h2>
<p><a href="https://github.com/z175/kdmapper">kdmapper</a> exploits a vulnerable Intel driver to manually map unsigned drivers. After we define a custom entry point for our driver, it can be successfully mapped by <a href="https://github.com/z175/kdmapper">kdmapper</a>. The downside of loading our driver this way is that we do not have a <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_driver_object">DRIVER_OBJECT</a>.</p>
<h2 id="obregistercallbacks">ObRegisterCallbacks</h2>
<p>As discussed previously <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-obregistercallbacks">ObRegisterCallbacks</a> requires the callbacks to reside in signed kernel images. This is an issue because our manually mapped kernel mode driver does not have a valid <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_driver_object">DRIVER_OBJECT</a>. The following call chain is invoked when we attempt to register a handle operation callback from our driver:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>ObRegisterCallbacks 
</span></span><span style="display:flex;"><span>	<span style="color:#f92672">|</span> <span style="color:#75715e">// 0x20 = LDRP_VALID_SECTION flag
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#f92672">|--</span>	MmVerifyCallbackFunctionCheckFlags(ourCallbackFunction, <span style="color:#ae81ff">0x20</span>)	
</span></span><span style="display:flex;"><span>		<span style="color:#f92672">|</span> <span style="color:#75715e">// looks up in which module ourCallbackFunction resides
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		<span style="color:#f92672">|--</span>	MiLookupDataTableEntry
</span></span><span style="display:flex;"><span>			<span style="color:#f92672">|</span> <span style="color:#75715e">// checks if our driver has the required flag 										
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>			<span style="color:#f92672">|--</span>	DriverObject<span style="color:#f92672">-&gt;</span>DriverSection<span style="color:#f92672">-&gt;</span>flags <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">0x20</span>   			
</span></span></code></pre></div><p>Since our <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_driver_object">DRIVER_OBJECT</a> is not valid because we mapped it with <a href="https://github.com/z175/kdmapper">kdmapper</a>, we do not have the required flag. However, if you are running Windows 7 you can use <a href="https://github.com/hfiref0x/DSEFix">DSEFix</a> to map your driver which comes with the added benefit of having a valid <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_driver_object">DRIVER_OBJECT</a>. Therefore, on Windows 7 the trick we describe in the next section is not required to register handle operation callbacks.</p>
<h2 id="one-weird-trick">One weird trick</h2>
<p>AdrianVPL suggested a trick to bypass this limitation in a <a href="https://www.unknowncheats.me/forum/2350590-post9.html">forum post</a>. To understand it we take a look at the signature of the <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nc-wdm-pob_pre_operation_callback">pre-operation callback</a>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>OB_PREOP_CALLBACK_STATUS <span style="color:#a6e22e">PobPreOperationCallback</span>(
</span></span><span style="display:flex;"><span>	PVOID RegistrationContext
</span></span><span style="display:flex;"><span>	POB_PRE_OPERATION_INFORMATION OperationInformation
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>{...}
</span></span></code></pre></div><p>The calling convention of PobPreOperationCallback is <a href="https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention">fastcall</a>. This means that the first parameter (<em>RegistrationContext</em>) is passed in the RCX register and the second parameter (OperationInformation) is passed in RDX. The interpretation of the registration context is driver-defined which in turn means that the driver itself is the only code accessing it.</p>
<p>The idea is to set the registration context to the address of our pre-operation callback and set
the pre-operation field which would usually hold the callback to an address containing a <em>JMP
RCX</em> instruction inside a valid driver. This results in <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-obregistercallbacks">ObRegisterCallbacks</a> validating whether the driver containing the <em>JMP RCX</em> instruction is valid instead of checking our unsigned driver. When the callback is executed, the <em>JMP RCX</em> instruction jumps to the value in the registration context, which is the real callback inside our unsigned driver.</p>
<p>In his post, AdrianVPL suggested abusing <em>ntoskrnl.exe</em> which is signed and contains multiple <em>JMP RCX</em> instructions. However, abusing ntoskrnl did not work on several Windows 10 versions we tested. We conducted kernel mode debugging and noticed that ntoskrnl is missing a required flag that is checked when attempting to register a handle operation callback. We solved this issue by <a href="https://github.com/not-wlan/driver-hijack/blob/5af9ce15a80c3629eac548cd27d7bccf5156eec8/memedriver/hijack.cpp#L136">iterating currently running drivers</a> and searching a signed driver with the required flag and a <em>JMP RCX</em> instruction. On our system one such driver is the DirectX Graphics
Kernel which our current implementation abuses.</p>
<p>Our prototype implementation is available here: <a href="https://github.com/Fahersto/kernel_handle_monitoring">https://github.com/Fahersto/kernel_handle_monitoring</a></p>
]]></content>
        </item>
        
        <item>
            <title>Revisiting Age of Empires 2: Definitive Edition</title>
            <link>https://blog.lentic.de/posts/revisiting_age_of_empires/</link>
            <pubDate>Wed, 05 Jan 2022 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/revisiting_age_of_empires/</guid>
            <description>Background The new years has just begun and it&amp;rsquo;s been half a year since I published my findings on Age of Empires 2: Definitive Edition and even longer since these issues have been brought to the games developers and Microsofts attention. Has the issue been fixed?
NO. Brush off a bad day: In Soviet Russia projectile dodges you: As always this works online and in all game modes.
Will it be fixed?</description>
            <content type="html"><![CDATA[<h2 id="background">Background</h2>
<p>The new years has just begun and it&rsquo;s been half a year since I published my <a href="https://blog.lentic.de/posts/age_of_empires/" title="Original article">findings on Age of Empires 2: Definitive Edition</a> and even longer since these issues have been brought to the games developers and Microsofts attention. Has the issue been fixed?</p>
<h1 id="no">NO.</h1>
<p>Brush off a bad day:
<img alt="clickergame" src="/revisit_aoe/brush.gif"></p>
<p>In Soviet Russia projectile dodges you:
<img alt="clickergame" src="/revisit_aoe/projectiles.gif"></p>
<hr>
<p>As always this works online and in all game modes.</p>
<hr>
<h2 id="will-it-be-fixed">Will it be fixed?</h2>
<p>The developers acknowledged the issue when I first reported it severals month ago and have &ldquo;added it to their database&rdquo;. With the release of Age of Empires IV I think it is more unlikely than ever to be fixed. I would love to be surprised though :)</p>
]]></content>
        </item>
        
        <item>
            <title>imogui - draw on overlays using imgui</title>
            <link>https://blog.lentic.de/posts/imogui/</link>
            <pubDate>Mon, 27 Sep 2021 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/imogui/</guid>
            <description>imogui is my GUI library to hook existing overlays and draw on them using imgui. All of imgui&amp;rsquo;s powerfull widgets such as buttons, plots and colorpickers can be used to create an interactive extension to any program using one of the supported overlays. This is very convenient as some of the supported overlays allow to add to any third party application (such as Steam). This project uses hookFTW to hook into the drawing function of the target overlay to achieve its functionality.</description>
            <content type="html"><![CDATA[<p><a href="https://github.com/Fahersto/imogui">imogui</a> is my GUI library to hook existing overlays and draw on them using <a href="https://github.com/ocornut/imgui">imgui</a>. All of imgui&rsquo;s powerfull widgets such as buttons, plots and colorpickers can be used to create an interactive extension to any program using one of the supported overlays. This is very convenient as some of the supported overlays allow to add to any third party application (such as Steam). This project uses <a href="https://github.com/Fahersto/hookFTW">hookFTW</a> to hook into the drawing function of the target overlay to achieve its functionality.</p>
<h2 id="using-imogui">Using imogui</h2>
<p>Additionally to imgui widgets it is also possible to draw several primitives using the passed imogui::Renderer inside the provided OnDraw callback. Here is an example using the Steam overlay in a 64-bit DirectX 11 game:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">OnDraw</span>(imogui<span style="color:#f92672">::</span>Renderer<span style="color:#f92672">*</span> renderer)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">// use the renderer to draw anything
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	[...]
</span></span><span style="display:flex;"><span>	renderer<span style="color:#f92672">-&gt;</span>RenderCircle(bouncyballs[i].pos, bouncyballs[i].radius, bouncyballColor, i <span style="color:#f92672">%</span> <span style="color:#ae81ff">8</span>, <span style="color:#ae81ff">32</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">static</span> <span style="color:#66d9ef">bool</span> openOverlay <span style="color:#f92672">=</span> true;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">// create an imgui window to interact (choose bouncy ball color in this case)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> (ImGui<span style="color:#f92672">::</span>Begin(<span style="color:#e6db74">&#34;IMOGUI - Hook&#34;</span>, <span style="color:#f92672">&amp;</span>openOverlay))
</span></span><span style="display:flex;"><span>	{
</span></span><span style="display:flex;"><span>		ImGui<span style="color:#f92672">::</span>Text(<span style="color:#e6db74">&#34;Intermediate Mode Overlay GUI&#34;</span>);
</span></span><span style="display:flex;"><span>		ImGui<span style="color:#f92672">::</span>ColorPicker3(<span style="color:#e6db74">&#34;Bouncyball Color&#34;</span>, color);
</span></span><span style="display:flex;"><span>		bouncyballColor <span style="color:#f92672">=</span> ImGui<span style="color:#f92672">::</span>ColorConvertFloat4ToU32(ImVec4(color[<span style="color:#ae81ff">2</span>], color[<span style="color:#ae81ff">1</span>], color[<span style="color:#ae81ff">0</span>], <span style="color:#ae81ff">1.f</span>));
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	ImGui<span style="color:#f92672">::</span>End();
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>imogui<span style="color:#f92672">::</span>Steamoverlay steamOverlay;
</span></span><span style="display:flex;"><span>steamOverlay.Hook(imogui<span style="color:#f92672">::</span>renderapi<span style="color:#f92672">::</span>directx11, OnDraw);
</span></span></code></pre></div><p>After compiling it into a DLL and loading it into the target process (here: Rocket League) using a DLL injector, this is the result:
<img alt="hook" src="/imogui/imogui.gif">
The purple discs and two open menus are usually not part of the game.</p>
<p>Unhooking imogui allows to modify your projects code and reinject your modified DLL during initial development (or if your usecase demands it) and is just as easy:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>steamOverlay.Unhook();
</span></span></code></pre></div><p>Note that this library is not limited to be used with games only. It works with any software employing one of the supported rendering APIs.</p>
<h2 id="currently-supported-overlays">Currently supported overlays</h2>
<h3 id="32-bit">32 Bit</h3>
<table>
<thead>
<tr>
<th>Overlay</th>
<th style="text-align:center">OpenGL</th>
<th style="text-align:center">DirectX 9</th>
<th style="text-align:center">DirectX 11</th>
<th style="text-align:center">DirectX 12</th>
</tr>
</thead>
<tbody>
<tr>
<td>Steam</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Discord</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Origin</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>MSI Afterburner</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Overwolf</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>GeForce Experience</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>OBS</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
</tbody>
</table>
<h3 id="64-bit">64 Bit</h3>
<table>
<thead>
<tr>
<th>Overlay</th>
<th style="text-align:center">OpenGL</th>
<th style="text-align:center">DirectX 9</th>
<th style="text-align:center">DirectX 11</th>
<th style="text-align:center">DirectX 12</th>
</tr>
</thead>
<tbody>
<tr>
<td>Steam</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Discord</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Origin</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>MSI Afterburner</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>Overwolf</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>GeForce Experience</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
</tr>
<tr>
<td>OBS</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">❌</td>
<td style="text-align:center">✅</td>
<td style="text-align:center">❌</td>
</tr>
</tbody>
</table>
<h2 id="roadmap">Roadmap</h2>
<p>As you can see in the table above there are numerous overlays and rendering APIs that are yet to be supported by this project. If anyone is interested in contributing to the project I&rsquo;m more than happy to assist. These are the features I&rsquo;m most exited about currently:</p>
<ul>
<li>Add support for OpenGL</li>
<li>Add support for DirectX 12</li>
<li>Add support for Vulkan</li>
<li>Add more overlays</li>
</ul>
]]></content>
        </item>
        
        <item>
            <title>hookFTW - hook for the win(dows)</title>
            <link>https://blog.lentic.de/posts/hookftw/</link>
            <pubDate>Wed, 15 Sep 2021 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/hookftw/</guid>
            <description>This is a post is about my C++ hooking library for Windows (GitHub).
What is a hooking library? A hooking library allows to change a target programs control flow. This can be useful to debug own applications but also to change or extend functionality of other programs. This functionality can be achieved using different methods. I implemented the following methods:
Byte patching .text section Import Address Table (IAT) Virtual Function Table (VFT) Vectored Expcetion Handler (VEH) Why write another Windows hooking library?</description>
            <content type="html"><![CDATA[<p>This is a post is about my C++ hooking library for Windows (<a href="https://github.com/Fahersto/hookFTW">GitHub</a>).</p>
<p><img alt="hook" src="/hookftw/hook_large.png"></p>
<h2 id="what-is-a-hooking-library">What is a hooking library?</h2>
<p>A hooking library allows to change a target programs control flow. This can be useful to debug own applications but also to change or extend functionality of other programs. This functionality can be achieved using different methods. I implemented the following methods:</p>
<ul>
<li>Byte patching .text section</li>
<li>Import Address Table (IAT)</li>
<li>Virtual Function Table (VFT)</li>
<li>Vectored Expcetion Handler (VEH)</li>
</ul>
<h2 id="why-write-another-windows-hooking-library">Why write another Windows hooking library?</h2>
<p>My motivation to write this library was twofold. First of all I wanted to <strong>really</strong> understand how hooking works and get an understanding of some of the low levels details, like relocating ASM instructions. Secondly I wasn&rsquo;t really happy with the publicly available hooking libraries. Nearly all of the big names do not offer midfunction hooking (hooking withing a function instead of its prolog). An exception is <a href="https:/https://frida.re/">Frida</a> but the concept to inject an entire javascript engine into the target process to then communicate using a python binding seems like such a strange idea when dealing with this inherintly low level topic.</p>
<h2 id="relocation">Relocation</h2>
<p>When byte patching the .text section to hook a program, the overwritten bytes have to be saved and executed to preserve the target processes functionality (and not crash it). The issue with this is that many assembler instructions are position dependend - meaning that copying them somewhere else and then execute them will change their semantic. Therefore such instructions have to be detected and modified to preserve their original meaning. To do so a disassembler is required. I chose <a href="https://github.com/zyantific/zydis">Zydis</a> since it offers all the low level details about the disassembled instructions required to relocate instructions, has no third party dependencies, can be built using CMAKE and is blazing fast - all at the same time!</p>
<h2 id="using-the-library">Using the library</h2>
<p>The easiest way to use hookFTW is by cloning it recursivly and building the library using CMAKE. Hooking at a target address then becomes as easy as:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-C++" data-lang="C++"><span style="display:flex;"><span><span style="color:#75715e">// use a midfunction hook. In this example we pass the proxy function as a lambda.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>hookftw<span style="color:#f92672">::</span>MidfunctionHook prologHook;
</span></span><span style="display:flex;"><span>prologHook.Hook(
</span></span><span style="display:flex;"><span>	targetAddress,
</span></span><span style="display:flex;"><span>	[](hookftw<span style="color:#f92672">::</span>context<span style="color:#f92672">*</span> ctx) {
</span></span><span style="display:flex;"><span>		printf(<span style="color:#e6db74">&#34;inside hooked function</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>); 
</span></span><span style="display:flex;"><span>		ctx<span style="color:#f92672">-&gt;</span>PrintRegister();
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>);
</span></span></code></pre></div><h2 id="roadmap">Roadmap</h2>
<p>There are some limitations on locations where hooks can be placed. Hooking a location where the target binary jumps to results in undefined behavior and most likely a crash. While I don&rsquo;t know any feasable method to detect if a binary jumps to the code location, this rarely becomes a problem since hookFTW offers direct register access which usually allows to just hook a couple of bytes before/after and then get/set the desired value from the registers. There are some other handy features that I hope to implement in the near future.</p>
<hr>
<p>This is it for now. In my next blogpost I will demonstrate how I put <a href="https://github.com/Fahersto/hookFTW">hookFTW</a> to good use. Stay tuned!</p>
]]></content>
        </item>
        
        <item>
            <title>Reversing Age of Empires 2: Definitive Edition</title>
            <link>https://blog.lentic.de/posts/age_of_empires/</link>
            <pubDate>Mon, 14 Jun 2021 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/age_of_empires/</guid>
            <description>This is a repost. The original article can be found here.
This blog post describes my lockdown project of (partially) reversing the popular 2019 videogame Age of Empires 2: Definitive Edition. My efforts did not only educate me about lock-step simulation and 90s coding practices, but also lead to various multiplayer hacks.
First I&amp;rsquo;ll give a brief background on the game&amp;rsquo;s multiplayer architecture. Then I&amp;rsquo;ll explain how I interactively explored the game&amp;rsquo;s internals, until I could do things that should not be possible.</description>
            <content type="html"><![CDATA[<p>This is a repost. <a href="https://redrocket.club/posts/age_of_empires/">The original article can be found here.</a></p>
<hr>
<p>This blog post describes my lockdown project of (partially) reversing the popular 2019 videogame <em>Age of Empires 2: Definitive Edition</em>.
My efforts did not only educate me about lock-step simulation and 90s coding practices, but also lead to various multiplayer hacks.</p>
<p>First I&rsquo;ll give a brief background on the game&rsquo;s multiplayer architecture.
Then I&rsquo;ll explain how I interactively explored the game&rsquo;s internals, until I could do things that should not be possible.
A proof of concept that lets you instantly win every online match is provided at the end of the post.</p>
<h2 id="background">Background</h2>
<p>Age of Empires (AoE) is a very popular real-time strategy game series with roots in the late 90s.
Numerous expansion packs and remakes have been published since.
Exactly twenty years after the original Age of Empires 2 was released, the &ldquo;Definitive Edition&rdquo; remake was published in 2019.
Like its predecessors it let&rsquo;s you play online matches against real opponents.</p>
<p>While looking for a way to connect with nature during the corona lockdown (without leaving my basement ofc.), I stumbled upon AoE on steam.
Naturally, I sucked at the game but got interested in how it works internally.
I stumbled upon an <a href="https://www.gamasutra.com/view/feature/131503/1500_archers_on_a_288_network_.php">interesting gamasutra article</a> about how the first Age of Empire games managed to accomplish a (at the time) daunting task. They had to simulate several hundred units for up to 8 (!) players on the internet.</p>
<p>The gamasutra article also involved a curious paragraph:</p>
<pre tabindex="0"><code>Because the game&#39;s outcome depended on all of the users executing exactly the same simulation, it was extremely difficult to hack a client (or client communication stream) and cheat. [...] but these few leaks were relatively easy to secure in subsequent patches and revisions. Security was a huge win.
</code></pre><p>This sounds like a fun challenge!</p>
<p>The AoE network architecture was designed in the LAN party era, commonly referred to as the 90s. It is based on a lock-step simulation.</p>
<p>To make an interesting story short:</p>
<ul>
<li>the game logic runs in &ldquo;turns&rdquo;</li>
<li>each client simulates the game on its own, there is no central server holding state</li>
<li>any command a player sends (such as moving a unit) is sheduled to be executed two &ldquo;turns&rdquo; later</li>
<li>every command sent needs to be verified and acknowledged by all players</li>
<li>if clients disagree about anything (e.g. the validity of a command or the position of a unit) the simulation is in a &ldquo;desynced&rdquo; state and the match gets terminated</li>
</ul>
<p>This architecture implies that clients have to carefully inspect incoming commands and perform sanity checks on them.
Missing or broken sanity checks would make it possible to send invalid commands that alter the game&rsquo;s state in unintended ways.
And that would be devastating, wouldn&rsquo;t it?</p>
<h2 id="exploring-the-game">Exploring the game</h2>
<p>What does a 90s networking architecture have to do with &ldquo;Age of Empires II: Definitive Edition&rdquo; released in November 2019?</p>
<p>Quite a lot actually, since the game is mostly a graphic overhaul.
Much of the underlying code is exactly the same as in the original.</p>
<p>First, I wanted to see if I can perform some actions through code. Using CheatEngine I was able to find a pointer to a unit object, just by manual searching based on changing a units position.
Thanks to Runtime Type Information (RTTI) this also gives insights about its inheritance hierarchie.
<a href="https://github.com/ajkhoury/ReClassEx">ReClassEx</a> reports this RTTI about our unit object:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>AVTRIBE_Combat_Object : AVRGE_Combat_Object : AVRGE_Action_Object : AVRGE_Moving_Object : AVGRGE_Animated_Object : AVRGE_Static_Object
</span></span></code></pre></div><p>So our unit is a AVTRIBE_Combat_Object which inherits from everything else to the right.
Especially interesting is the inherited <code>AVRGE_Moving_Object</code>.
Its existence suggest that movement may be a virtual function that is then overwritten.
To find the move function, I set breakpoints on all functions in our units virtual function table and then remove all breakpoints that trigger for unknown reasons.
By trial and error I could get rid of those &ldquo;random&rdquo; breakpoints until
moving the unit around triggered only one breakpoint.
This way I could identify the <code>movement</code> function inside the virtual function table.</p>
<p>To verify that this was indeed the function triggering movement, I wrote a small bot. This bot automatically dodges (my own) catapult attacks by moving all units from the impact location using their virtual move function.</p>
<p><img alt="dodge" src="/aoe/dodge.gif"></p>
<p>The bot is injected into the Age of Empires process via a dynamic library (.dll), directly calling the <code>movement</code> function.</p>
<p>Unfortunately, there is a tiny issue with this method of moving units through code.
It causes online games to desync and immediately terminate.
This is expected since I&rsquo;m just moving a unit, without sending the appropriate move command to other players.
To synchronize the state, that 90s networking code must be used&hellip;</p>
<h2 id="digging-deeper">Digging deeper</h2>
<p>Games tend to have huge code bases. Finding and identifying something like a command handler can be tedious work&hellip;
This is where RTTI comes in handy once again.
After some digging and working my way up the callstack of the <code>movement</code> function, I was able to uncover several classes of interest.</p>
<p>Here is a ReClassEx screenshot of the structures:</p>
<p><img alt="ReClass" src="/aoe/ReClass.png"></p>
<p>Thanks to RTTI I could find the command handler (called <code>AVTRIBE_Command</code> in the screenshot).
It is now possible to set a breakpoint, which triggers every time the handler is accessed.
Just by joining a multiplayer lobby and performing the actions I&rsquo;m interested in (e.g. move a unit), I can identify the function for sending the corresponding command to the other players.</p>
<p>Using this method it was quite easy to identify the function responsible for sending a <code>move unit</code> command to other players.
Here is an IDA screenshot of that exact function:</p>
<p><img alt="ReClass" src="/aoe/move_unit_ida.png"></p>
<p>In order to keep a synced state between players, I have to call this function.
As I only want to be able to call this function directly, I&rsquo;m not terribly interested in its inner workings.
Hence I did not reverse engineeer it.</p>
<p>To directly call a function, one needs to know the programs calling convention and which parameters to pass.
Since the game is a 64bit windows binary, the <code>fastcall</code> calling convention is pretty much a given and IDA Pro happens to agree.
IDA also determined that the function has 8 parameters&hellip;
Luckily, dynamic analysis combined with RTTI makes the process of reverse engeneering these parameters quite easy.
I could simply set a breakpoint at the start of the function and analyse the parameters when moving units in a multiplayer match.</p>
<p>The reversed parameters with types are:</p>
<ol>
<li>CommandHandler: 	<code>AVTRIBE_Command</code></li>
<li>Unit we want to move:   	<code>AVTRIBE_Combat_Object : AVRGE_Combat_Object : AVRGE_Action_Object : AVRGE_Moving_Object : AVGRGE_Animated_Object : AVRGE_Static_Object</code></li>
<li>number of units we want to move: <code>uint64_t</code></li>
<li>some parameter that always seems to be zero: <code>uint64_t</code></li>
<li>target x position on the map: <code>float</code></li>
<li>target y position on the map: <code>float</code></li>
<li>indicator if movement should be queued as a waypoint: <code>uint8_t</code></li>
<li>some parameter that always seemed to be one: <code>uint8_t</code></li>
</ol>
<p>With this information and the function&rsquo;s offset within the binary, I can define the function&rsquo;s prototype and call it directly from my dynamic library injected into the AoE process:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">typedef</span> <span style="color:#a6e22e">int64_t</span>(__thiscall<span style="color:#f92672">*</span> MoveUnit)(<span style="color:#66d9ef">int64_t</span><span style="color:#f92672">*</span> command, <span style="color:#66d9ef">int64_t</span><span style="color:#f92672">*</span> unit, <span style="color:#66d9ef">int64_t</span> unitCount, <span style="color:#66d9ef">int64_t</span> unknownZero,  <span style="color:#66d9ef">float</span> x, <span style="color:#66d9ef">float</span> y, <span style="color:#66d9ef">char</span> asWaypoint, <span style="color:#66d9ef">char</span> unknownOne);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//int64_t)GetModuleHandle(NULL) is used to get the ASLR base address of the age of empires process
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">static</span> MoveUnit moveUnit <span style="color:#f92672">=</span> (MoveUnit)((<span style="color:#66d9ef">int64_t</span>)GetModuleHandle(NULL) <span style="color:#f92672">+</span> <span style="color:#ae81ff">0xE2CAE0</span>); <span style="color:#75715e">//this offset is valid as of 12 June 2021. 
</span></span></span></code></pre></div><p>By calling this function I can move my units from code.
That&rsquo;s pretty cool, but what would happen if I try to move enemy units?
Surely that&rsquo;s where the afromentioned sanity checks would stop me from doing bad things, right?</p>
<h2 id="losing-sanity">Losing sanity</h2>
<p>Turns out there is no sanity check preventing me from moving my enemies&rsquo; units.
The game&rsquo;s user interface only lets you select your own units.
So it seems that we can only move our own units.</p>
<p>But since I&rsquo;m directly calling functions via my injected library, I can just write the pointer to an enemy unit into my own current unit selection.
This way I can control the enemy units directly via the user interface.
The following GIF shows how I can move my own (blue) and my enemy&rsquo;s (red) units.</p>
<p><img alt="remote_control" src="/aoe/remote_control.gif"></p>
<p>The game does not really visually indicate the movement action of enemy units, as this is not something that should ever happen.</p>
<p>To be honest I did not really expect this to be possible. But since I had so much fun until now, I wanted to see what else is possible.
After all disallowing a player to move units owned by other players would be one of the first sanity checks that comes to my mind when dealing with a real time strategy game.
Turns out the sky is the limit. There seem to be no sanity checks whatsoever.</p>
<!-- raw HTML omitted -->
<p>We can use the same method to work our way back from breakpoints on the command handler to any action in a multiplayer match we are interested in.
One action that seems especially interesting is killing your own units to free up supply (usually done by selecting one of your own units and then pressing the &ldquo;delete&rdquo; key).
It would be quite severe if we could just instantly kill enemy units with the press of a button.
As expected, we can do that, thereby making Age of Empires 2: Definitive Edition my favourite clicker game:</p>
<p><img alt="clickergame" src="/aoe/clickergame.gif"></p>
<p>To state the obvious: Enemy units usually don&rsquo;t come with &ldquo;kill&rdquo; buttons attached to them. This is also a good time to remember that this works in multiplayer and also ranked matches.
Just for the funsies I also implemented a social distancing functionality that kills all enemy units that get to close to one of my units. After all, whats life without a little whimsy?</p>
<h2 id="win-the-game-proof-of-concept">Win the game (proof of concept)</h2>
<p>I guess every good security related blog post should end with a proof of concept and demo video.</p>
<p>So here is a &ldquo;instantly win the game&rdquo; proof of concept for the latest version (2021-12-06 on Steam) of Age of Empires 2: Definitive Edition.
It lets you enter a player&rsquo;s number then loops over all of his units and kills them, instantly defeating the target player.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;windows.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;cstdint&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;cstdio&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//working as of 12/06/2021.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#75715e">/* NOT YET PUBLISHED - MAYBE SOON */</span>
</span></span></code></pre></div><p><em>(We decided to not publish the PoC at this very moment. However, if the bugs won&rsquo;t get fixed, we might publish it via our <a href="https://twitter.com/redrocket_ctf">Twitter</a> at a later time. We just want to make sure that fair play stays possible.)</em></p>
<p>This also works for ranked matches and could come in handy for tournaments (just kidding ;)).
Be aware that the function offsets are hard coded and could change once there is a new patch for the game.
The PoC would still work but would need updated offsets.</p>
<h2 id="showcase">Showcase</h2>
<p>Here is a video showing the PoC in action, killing both enemies&rsquo; instantly.
In the lower right corner you can see the enemy player&rsquo;s screen.</p>
<video width="100%" height="auto" class="video-shortcode" preload="auto" controls>
    <source src="/aoe/aoe_poc.mp4" type="video/mp4" >
    There should have been a video here but your browser does not seem
    to support it.
</video>

<h2 id="conclusion">Conclusion</h2>
<p>Due to hardware and bandwidth limitations of the 90s, Age of Empires uses a distributed state, employing a lock-step approach.
Each client simulates the game by itself. There is no central server that holds the ground truth.
As it usually is, legacy software lives a long live.
In case of &ldquo;Age of Empires 2: Definitve Edition&rdquo; the networking architecture and code seem to still be heavily based on the original game from the 90s.</p>
<p>While this network architecture comes with benefits and has a certain appeal to it, it&rsquo;s security relies purely on sanity checks that each communication partner has to perform.
Unfortunately these sanity checks seem to be missing.</p>
<p>As a rewrite of the game seems unlikely, the way to fix this vulnerability would be to introduce sanity checks for <em>every command</em> received from another player.</p>
<p>While I haven&rsquo;t looked into other &ldquo;Age of Empires&rdquo; games it seems plausable that other versions may have similar issues.</p>
]]></content>
        </item>
        
        <item>
            <title>CodinaColada - game engine playground</title>
            <link>https://blog.lentic.de/posts/codina_colada/</link>
            <pubDate>Sun, 13 Dec 2020 00:00:00 +0000</pubDate>
            
            <guid>https://blog.lentic.de/posts/codina_colada/</guid>
            <description>Implementation of raymarching.
CodinaColada is my 2d game engine. Its main purpose is to be a sandbox in which I can implement any functionality I&amp;rsquo;m interested in.
Technologies OpenGL Box2D Tracy To improve performance of my engine I need to know where the biggest potentials for improvements are. This can be determined using a profiler. Tracy is one such profiler. It is able to measure CPU and GPU performance. This is done by introducing macros inside the functions to be instrumented which then create zones as can be seen in the image showing the performance of a single frame below.</description>
            <content type="html"><![CDATA[<p><img alt="hook" src="/codina_colada/raymarching.gif">
Implementation of raymarching.</p>
<p>CodinaColada is my 2d game engine. Its main purpose is to be a sandbox in which I can implement any functionality I&rsquo;m interested in.</p>
<h2 id="technologies">Technologies</h2>
<h3 id="opengl">OpenGL</h3>
<h3 id="box2d">Box2D</h3>
<h3 id="tracy">Tracy</h3>
<p>To improve performance of my engine I need to know where the biggest potentials for improvements are. This can be determined using a profiler. <a href="https://github.com/wolfpld/tracy">Tracy</a> is one such profiler. It is able to measure CPU and GPU performance. This is done by introducing macros inside the functions to be instrumented which then create zones as can be seen in the image showing the performance of a single frame below.
<img alt="hook" src="/codina_colada/tracy.png"></p>
<p>From the image we can see that the engine is currently CPU bound as the red zones inside the OpenGL context only make up a tiny portion of frame. This is in contrest to the CPU which is busy the entire frame looping over the gameobjects and preparing rendering data (such as calculating positions). We can also see that there are 8 threads updating the gameobjects but the JsonAnalyser objects requires by far more time than the other &gt;1000 gameobjects together. This however is not an issue of the engine but the application I build using the engine. The JsonAnalyser is responsible for rendering the text over 1000 nodes like these:
<img alt="hook" src="/codina_colada/nodes.gif"></p>
<p>Therefore I refactored my code to have each individual node be a gameobject. This way much of the processing can happen concurrently in GameObject::Update and only the drawing itself has to take place in the drawing thread (GameObject::Draw). Here is the result:
<img alt="hook" src="/codina_colada/tracy_optimized.png"></p>
<p>As we can see the giant JsonAnalysis block is gone which in turn reduces the time per frame from ~8ms to ~5ms.</p>
<p>Now the largest block by far is Renderer::Draw, which sets up the data required for the OpenGLShaders (such as position, time&hellip;) for each individual GameObject. My next optimization will probably have to be concurrently creating a command buffer for OpenGL calls and then doing all the OpenGL calls from the OpenGL thread right before drawing.</p>
<h3 id="cmake">CMAKE</h3>
]]></content>
        </item>
        
    </channel>
</rss>
