<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Yarden_Shafir - Medium]]></title>
        <description><![CDATA[Security researcher - Medium]]></description>
        <link>https://medium.com/yarden-shafir?source=rss----b7ee08c6baa---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Yarden_Shafir - Medium</title>
            <link>https://medium.com/yarden-shafir?source=rss----b7ee08c6baa---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:42:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/yarden-shafir" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Yes, More Callbacks — The Kernel Extension Mechanism]]></title>
            <link>https://medium.com/yarden-shafir/yes-more-callbacks-the-kernel-extension-mechanism-c7300119a37a?source=rss----b7ee08c6baa---4</link>
            <guid isPermaLink="false">https://medium.com/p/c7300119a37a</guid>
            <category><![CDATA[kernel]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[Yarden Shafir]]></dc:creator>
            <pubDate>Tue, 01 Jan 2019 21:54:27 GMT</pubDate>
            <atom:updated>2019-01-01T21:46:45.458Z</atom:updated>
            <content:encoded><![CDATA[<h3>Yes, More Callbacks — The Kernel Extension Mechanism</h3><p>Recently I had to write a kernel-mode driver. This has made a lot of people very angry and been widely regarded as a bad move. (Douglas Adams, paraphrased)</p><p>Like any other piece of code written by me, this driver had several major bugs which caused some interesting side effects. Specifically, it prevented some other drivers from loading properly and caused the system to crash.</p><p>As it turns out, many drivers assume their initialization routine (DriverEntry) is always successful, and don’t take it well when this assumption breaks. <a href="https://twitter.com/j00ru">j00ru</a> documented some of these cases a few years ago <a href="https://j00ru.vexillium.org/2012/11/defeating-windows-driver-signature-enforcement-part-1-default-drivers/">in his blog</a>, and many of them are still relevant in current Windows versions. However, these buggy drivers are not really the issue here, and j00ru covered it better than I could anyway. Instead I focused on just one of these drivers, which caught my attention and dragged me into researching the so-called “windows kernel host extensions” mechanism.</p><p>The lucky driver is Bam.sys (Background Activity Moderator) — a new driver which was introduced in Windows 10 version 1709 (RS3). When its DriverEntry fails mid-way, the call stack leading to the system crash looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/0*YopiaXY59sXMPZsx" /></figure><p>From this crash dump, we can see that Bam.sys registered a process creation callback and forgot to unregister it before unloading. Then, when a process was created / terminated, the system tried to call this callback, encountered a stale pointer and crashed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*jgWIjZ24O0Dj-pXC" /></figure><p>The interesting thing here is not the crash itself, but rather how Bam.sys registers this callback. Normally, process creation callbacks are registered via nt!<em>PsSetCreateProcessNotifyRoutine(Ex)</em>, which adds the callback to the <em>nt!PspCreateProcessNotifyRoutine</em> array. Then, whenever a process is being created or terminated, <em>nt!PspCallProcessNotifyRoutines</em> iterates over this array and calls all of the registered callbacks. However, if we run for example “!<a href="https://github.com/swwwolf/wdbgark">wdbgark</a>.wa_systemcb /type process“ in WinDbg, we’ll see that the callback used by Bam.sys is not found in this array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cteIdSCztJPyV9WC" /></figure><p>Instead, Bam.sys uses a whole other mechanism to register its callbacks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/0*VtSfCpyzebIjlbUm" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/0*5iIISkcb8os6GEdZ" /></figure><p>If we take a look at <em>nt!PspCallProcessNotifyRoutines</em>, we can see an explicit reference to some variable named <em>nt!PspBamExtensionHost </em>(there is a similar one referring to the Dam.sys driver). It retrieves a so-called “extension table” using this “extension host” and calls the first function in the extension table, which is <em>bam!BampCreateProcessCallback</em>.</p><p>If we open Bam.sys in IDA, we can easily find <em>bam!BampCreateProcessCallback</em> and search for its xrefs. Conveniently, it only has one, in <em>bam!BampRegisterKernelExtension</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/0*LGQ9WbUs1M4ixy18" /></figure><p>As suspected, <em>Bam!BampCreateProcessCallback</em> is not registered via the normal callback registration mechanism. It is actually being stored in a function table named <em>Bam!BampKernelCalloutTable</em>, which is later being passed, together with some other parameters (we’ll talk about them in a minute) to the undocumented <em>nt!ExRegisterExtension</em> function.</p><p>I tried to search for any documentation or hints for what this function was responsible for, or what this “extension” is, and couldn’t find much. The only useful resource I found was the leaked <a href="https://github.com/room/winsdk10/blob/d1acc505c51b11a6ceafb0f93c9dc584b8b4a9d3/Include/10.0.10240.0/um/minwin/ntosifs.h">ntosifs.h</a> header file, which contains the prototype for <em>nt!ExRegisterExtension </em>as well as the layout of the <em>_EX_EXTENSION_REGISTRATION_1</em> structure.</p><p>Prototype for <em>nt!ExRegisterExtension</em> and <em>_EX_EXTENSION_REGISTRATION_1</em>, as supplied in ntosifs.h:</p><pre>NTKERNELAPI NTSTATUS ExRegisterExtension (</pre><pre>    _Outptr_ PEX_EXTENSION *Extension,</pre><pre>    _In_ ULONG RegistrationVersion,</pre><pre>    _In_ PVOID RegistrationInfo</pre><pre>);<br></pre><pre>typedef struct _EX_EXTENSION_REGISTRATION_1 {</pre><pre>    USHORT ExtensionId;</pre><pre>    USHORT ExtensionVersion;</pre><pre>    USHORT FunctionCount;</pre><pre>    VOID *FunctionTable;</pre><pre>    PVOID *HostInterface;</pre><pre>    PVOID DriverObject;</pre><pre>} EX_EXTENSION_REGISTRATION_1, *PEX_EXTENSION_REGISTRATION_1;</pre><p>After a bit of reverse engineering, I figured that the formal input parameter “<em>PVOID RegistrationInfo”</em> is actually of type <em>PEX_EXTENSION_REGISTRATION_1</em>.</p><p>The pseudo-code of <em>nt!ExRegisterExtension</em> is shown in appendix B, but here are the main points:</p><ol><li><em>nt!ExRegisterExtension </em>extracts the <em>ExtensionId </em>and <em>ExtensionVersion </em>members of the <em>RegistrationInfo </em>structure and uses them to locate a matching host in <em>nt!ExpHostList</em> (using the <em>nt!ExpFindHost</em> function, whose pseudo-code appears in appendix B).</li><li>Then, the function verifies that the amount of functions supplied in <em>RegistrationInfo-&gt;FunctionCount </em>matches the expected amount set in the host’s structure. It also makes sure that the host’s <em>FunctionTable </em>field has not already been initialized. Basically, this check means that an extension cannot be registered twice.</li><li>If everything seems OK, the host’s <em>FunctionTable </em>field is set to point to the <em>FunctionTable</em> supplied in <em>RegistrationInfo</em>.</li><li>Additionally, <em>RegistrationInfo-&gt;HostInterface</em> is set to point to some data found in the host structure. This data is interesting, and we’ll discuss it soon.</li><li>Eventually, the fully initialized host is returned to the caller via an output parameter.</li></ol><p>We saw that <em>nt!ExRegisterExtension</em> searches for a host that matches <em>RegistrationInfo</em>. The question now is, where do these hosts come from?</p><ul><li>During its initialization, NTOS performs several calls to <em>nt!ExRegisterHost</em>. In every call it passes a structure identifying a single driver from a list of predetermined drivers (full list in appendix A). For example, here is the call which initializes a host for Bam.sys:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/0*y2DM_uLJOEcmQmtj" /></figure><ul><li><em>nt!ExRegisterHost</em> allocates a structure of type <em>_HOST_LIST_ENTRY</em> (unofficial name, coined by me), initializes it with data supplied by the caller, and adds it to the end of <em>nt!ExpHostList</em>. The <em>_HOST_LIST_ENTRY</em> structure is undocumented, and looks something like this:</li></ul><pre>struct _HOST_LIST_ENTRY</pre><pre>{</pre><pre>    _LIST_ENTRY List;</pre><pre>    DWORD RefCount;</pre><pre>    USHORT ExtensionId;</pre><pre>    USHORT ExtensionVersion;</pre><pre>    USHORT FunctionCount; // number of callbacks that the extension <br>                          // contains</pre><pre>    POOL_TYPE PoolType;   // where this host is allocated</pre><pre>    PVOID HostInterface; // table of unexported nt functions, <br>                         // to be used by the driver to which <br>                         // this extension belongs</pre><pre>    PVOID FunctionAddress; // optional, rarely used. <br>                           // This callback is called before <br>                           // and after an extension for this <br>                           // host is registered / unregistered</pre><pre>    PVOID ArgForFunction; // will be sent to the function saved here</pre><pre>    _EX_RUNDOWN_REF RundownRef;</pre><pre>    _EX_PUSH_LOCK Lock;</pre><pre>    PVOID FunctionTable; // a table of the callbacks that the <br>                         // driver “registers”</pre><pre>    DWORD Flags;         // Only uses one bit. <br>                         // Not sure about its meaning.</pre><pre>} HOST_LIST_ENTRY, *PHOST_LIST_ENTRY;</pre><ul><li>When one of the predetermined drivers loads, it registers an extension using <em>nt!ExRegisterExtension</em> and supplies a <em>RegistrationInfo </em>structure, containing a table of functions (as we saw Bam.sys doing). This table of functions will be placed in the <em>FunctionTable </em>member of the matching host. These functions will be called by NTOS in certain occasions, which makes them some kind of callbacks.</li></ul><p>Earlier we saw that part of <em>nt!ExRegisterExtension</em> functionality is to set <em>RegistrationInfo-&gt;HostInterface</em> (which contains a global variable in the calling driver) to point to some data found in the host structure. Let’s get back to that.</p><p>Every driver which registers an extension has a host initialized for it by NTOS. This host contains, among other things, a <em>HostInterface</em>, pointing to a predetermined table of <strong>unexported </strong>NTOS functions. Different drivers receive different <em>HostInterfaces</em>, and some don’t receive one at all.</p><p>For example, this is the <em>HostInterface </em>that Bam.sys receives:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/0*ApV0rZet2SP5bATn" /></figure><p>So the “kernel extensions” mechanism is actually a bi-directional communication port: The driver supplies a list of “callbacks”, to be called on different occasions, and receives a set of functions for its own internal use.</p><p>To stick with the example of Bam.sys, let’s take a look at the callbacks that it supplies:</p><ul><li>BampCreateProcessCallback</li><li>BampSetThrottleStateCallback</li><li>BampGetThrottleStateCallback</li><li>BampSetUserSettings</li><li>BampGetUserSettingsHandle</li></ul><p>The host initialized for Bam.sys “knows” in advance that it should receive a table of 5 functions. These functions must be laid-out in the exact order presented here, since they are called according to their index. As we can see in this case, where the function found in <em>nt!PspBamExtensionHost-&gt;FunctionTable[4]</em> is called:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/0*MF2iLHpB0zJpSzES" /></figure><p>To conclude, there exists a mechanism to “extend” NTOS by means of registering specific callbacks and retrieving unexported functions to be used by certain predetermined drivers.</p><p>I don’t know if there is any practical use for this knowledge, but I thought it was interesting enough to share. If you find anything useful / interesting to do with this mechanism, I’d love to know :)</p><p><strong>Appendix A</strong> — Extension hosts initialized by NTOS:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*j2PnauBZCMxWhyRf6SxKNA.png" /></figure><p><strong>Appendix B</strong> — functions pseudo-code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d6b5b0f4c9fe9a4786bebc62d5b7201/href">https://medium.com/media/8d6b5b0f4c9fe9a4786bebc62d5b7201/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68436cffaf5901a7a4973d837765107c/href">https://medium.com/media/68436cffaf5901a7a4973d837765107c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c0d513133b9c64ea091c7125050de19/href">https://medium.com/media/6c0d513133b9c64ea091c7125050de19/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fffa10ab27084c7c51fd176958fbe5ed/href">https://medium.com/media/fffa10ab27084c7c51fd176958fbe5ed/href</a></iframe><p><strong>Appendix C </strong>— structures definitions:</p><pre>struct _HOST_INFORMATION</pre><pre>{</pre><pre>    USHORT ExtensionId;</pre><pre>    USHORT ExtensionVersion;</pre><pre>    DWORD FunctionCount;</pre><pre>    POOL_TYPE PoolType;</pre><pre>    PVOID HostInterface;</pre><pre>    PVOID FunctionAddress;</pre><pre>    PVOID ArgForFunction;</pre><pre>    PVOID unk;</pre><pre>} HOST_INFORMATION, *PHOST_INFORMATION;</pre><pre><br>struct _HOST_LIST_ENTRY</pre><pre>{</pre><pre>    _LIST_ENTRY List;</pre><pre>    DWORD RefCount;</pre><pre>    USHORT ExtensionId;</pre><pre>    USHORT ExtensionVersion;</pre><pre>    USHORT FunctionCount; // number of callbacks that the <br>                          // extension contains</pre><pre>    POOL_TYPE PoolType;   // where this host is allocated</pre><pre>    PVOID HostInterface;  // table of unexported nt functions, <br>                          // to be used by the driver to which <br>                          // this extension belongs</pre><pre>    PVOID FunctionAddress; // optional, rarely used. <br>                           // This callback is called before and    <br>                           // after an extension for this host <br>                           // is registered / unregistered</pre><pre>    PVOID ArgForFunction; // will be sent to the function saved here</pre><pre>    _EX_RUNDOWN_REF RundownRef;</pre><pre>    _EX_PUSH_LOCK Lock;</pre><pre>    PVOID FunctionTable;    // a table of the callbacks that <br>                            // the driver “registers”</pre><pre>DWORD Flags;                // Only uses one flag. <br>                            // Not sure about its meaning.</pre><pre>} HOST_LIST_ENTRY, *PHOST_LIST_ENTRY;;</pre><pre><br>struct _EX_EXTENSION_REGISTRATION_1</pre><pre>{</pre><pre>    USHORT ExtensionId;</pre><pre>    USHORT ExtensionVersion;</pre><pre>    USHORT FunctionCount;</pre><pre>    PVOID FunctionTable;</pre><pre>    PVOID *HostTable;</pre><pre>    PVOID DriverObject;</pre><pre>}EX_EXTENSION_REGISTRATION_1, *PEX_EXTENSION_REGISTRATION_1;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7300119a37a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yarden-shafir/yes-more-callbacks-the-kernel-extension-mechanism-c7300119a37a">Yes, More Callbacks — The Kernel Extension Mechanism</a> was originally published in <a href="https://medium.com/yarden-shafir">Yarden_Shafir</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>