Generating NDR Type Serializers for C#

As part of updating NtApiDotNet to v1.1.28 I added support for Kerberos authentication tokens. To support this I needed to write the parsing code for Tickets. The majority of the Kerberos protocol uses ASN.1 encoding, however some Microsoft specific parts such as the Privileged Attribute Certificate (PAC) uses Network Data Representation (NDR). This is due to these parts of the protocol being derived from the older NetLogon protocol which uses MSRPC, which in turn uses NDR.

I needed to implement code to parse the NDR stream and return the structured information. As I already had a class to handle NDR I could manually write the C# parser but that’d take some time and it’d have to be carefully written to handle all use cases. It’d be much easier if I could just use my existing NDR byte code parser to extract the structure information from the KERBEROS DLL. I’d fortunately already written the feature, but it can be non-obvious how to use it. Therefore this blog post gives you an overview of how to extract NDR structure data from existing DLLs and create standalone C# type serializer.

First up, how does KERBEROS parse the NDR structure? It could have manual implementations, but it turns out that one of the lesser known features of the MSRPC runtime on Windows is its ability to generate standalone structure and procedure serializers without needing to use an RPC channel. In the documentation this is referred to as Serialization Services.

To implement a Type Serializer you need to do the following in a C/C++ project. First, add the types to serialize inside an IDL file. For example the following defines a simple type to serialize.

interface TypeEncoders
{
    typedef struct _TEST_TYPE
    {
        [unique, string] wchar_t* Name;
        DWORD Value;
    } TEST_TYPE;

}

You then need to create a separate ACF file with the same name as the IDL file (i.e. if you have TYPES.IDL create a file TYPES.ACF) and add the encode and decode attributes.

interface TypeEncoders
{
    typedef [encode, decode] TEST_TYPE;
}

Compiling the IDL file using MIDL you’ll get the client source code (such as TYPES_c.c), and you should find a few functions, the most important being TEST_TYPE_Encode and TEST_TYPE_Decode which serialize (encode) and deserialize (decode) a type from a byte stream. How you use these functions is not really important. We’re more interested in understanding how the NDR byte code is configured to perform the serialization so that we can parse it and generate our own serializers. 

If you look at the Decode function when compiled for a X64 target it should look like the following:

void
TEST_TYPE_Decode(
    handle_t _MidlEsHandle,
    TEST_TYPE * _pType)
{
    NdrMesTypeDecode3(
         _MidlEsHandle,
         ( PMIDL_TYPE_PICKLING_INFO  )&__MIDL_TypePicklingInfo,
         &TypeEncoders_ProxyInfo,
         TypePicklingOffsetTable,
         0,
         _pType);
}

The NdrMesTypeDecode3 is an API implemented in the RPC runtime DLL. You might be shocked to hear this, but this function and its corresponding NdrMesTypeEncode3 are not documented in MSDN. However, the SDK headers contain enough information to understand how it works.

The API takes 6 parameters:
  1. The serialization handle, used to maintain state such as the current stream position and can be used multiple times to encode or decode more that one structure in a stream.
  2. The MIDL_TYPE_PICKLING_INFO structure. This structure provides some basic information such as the NDR engine flags.
  3. The MIDL_STUBLESS_PROXY_INFO structure. This contains the format strings and transfer types for both DCE and NDR64 syntax encodings.
  4. A list of type offset arrays, these contains the byte offset into the format string (from the Proxy Info structure) for all type serializers.
  5. The index of the type offset in the 4th parameter.
  6. A pointer to the structure to serialize or deserialize.

Only parameters 2 through 5 are needed to parse the NDR byte code correctly. Note that the NdrMesType3 APIs are used for dual DCE and NDR64 serializers. If you compile as 32 bit it will instead use NdrMesType2 APIs which only support DCE. I’ll mention what you need to parse the DCE only APIs later, but for now most things you’ll want to extract are going to have a 64 bit build which will almost always use NdrMesType*3 even though my tooling only parses the DCE NDR byte code.

To parse the type serializers you need to load the DLL you want to extract from into memory using LoadLibrary (to ensure any relocations are processed) then use either the Get-NdrComplexType PS command or the NdrParser::ReadPicklingComplexType method and pass the addresses of the 4 parameters.

Let’s look at an example in KERBEROS.DLL. We’ll pick the PAC_DEVICE_INFO structure as it’s pretty complex and would require a lot of work to manually write a parser. If you disassemble the PAC_DecodeDeviceInfo function you’ll see the call to NdrMesTypeDecode3 as follows (from the DLL in Windows 10 2004 SHA1:173767EDD6027F2E1C2BF5CFB97261D2C6A95969).

mov     [rsp+28h], r14  ; pObject
mov     dword ptr [rsp+20h], 5 ; nTypeIndex
lea     r9, off_1800F3138 ; ArrTypeOffset
lea     r8, stru_1800D5EA0 ; pProxyInfo
lea     rdx, stru_1800DEAF0 ; pPicklingInfo
mov     rcx, [rsp+68h]  ; Handle
call    NdrMesTypeDecode3

From this we can extract the following values:

MIDL_TYPE_PICKLING_INFO = 0x1800DEAF0
MIDL_STUBLESS_PROXY_INFO = 0x1800D5EA0
Type Offset Array = 0x1800F3138
Type Offset Index = 5

These addresses are using the default load address of the library which is unlikely to be the same as where the DLL is loaded in memory. Get-NdrComplexType supports specifying relative addresses from a base module, so subtract the base address of 0x180000000 before using them. The following script will extract the type information.

PS> $lib = Import-Win32Module KERBEROS.DLL
PS> $types = Get-NdrComplexType -PicklingInfo 0xDEAF0 -StublessProxy 0xD5EA0 <br />&nbsp; &nbsp; &nbsp;-OffsetTable 0xF3138 -TypeIndex 5 -Module $lib<br /><br />As long as there was no error from this command the <i>$types</i> variable will now contain the parsed complex types, in this case there'll be more than one. Now you can format them to a C# source code file to use in your application using <i>Format-RpcComplexType</i>.<br /><br />PS&gt; Format-RpcComplexType $types -Pointer<br /><br />This will generate a C# file which looks like <a href="https://gist.github.com/tyranid/e82fbc61aac9b91394df07cc897603bc" rel="noreferrer" target="_blank">this</a>. The code contains <a href="https://gist.github.com/tyranid/e82fbc61aac9b91394df07cc897603bc#file-pac_device_info-cs-L380" rel="noreferrer" target="_blank">Encoder</a><i> </i>and <a href="https://gist.github.com/tyranid/e82fbc61aac9b91394df07cc897603bc#file-pac_device_info-cs-L422" rel="noreferrer" target="_blank">Decoder</a><i> </i>classes with static methods for each structure. We also passed the <i>Pointer</i> parameter to <i>Format-RpcComplexType</i>. This is so that the structured are wrapped inside a Unique Pointers. This is the default when using the real RPC runtime, although except for <a href="https://docs.microsoft.com/en-us/windows/win32/rpc/structures-tfs#:~:text=A%20conformant%20structure%20contains%20only,is%20embedded%20in%20this%20structure." rel="noreferrer" target="_blank">Conformant Structures</a> isn't strictly necessary. If you don't do this then the decode will typically fail, certainly in this case.<br /><br />You might notice a serious issue with the generated code, there are no proper structure names. This is unavoidable, the MIDL compiler doesn't keep any name information with the NDR byte code, only the structure information. However, the basic Visual Studio refactoring tool can make short work of renaming things if you know what the names are supposed to be. You could also manually rename everything in the parsed structure information before using <i>Format-RpcComplexType</i>.<br /><br />In this case there is an alternative to all that. We can use the fact that the official MS documentation contains a <a href="https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-pac/1d4912dd-5115-4124-94b6-fa414add575f" rel="noreferrer" target="_blank">full IDL</a> for <i>PAC_DEVICE_INFO</i> and its related structures and build our own executable with the NDR byte code to extract. How does this help? If you reference the <i>PAC_DEVICE_INFO</i> structure as part of an RPC interface no only can you avoid having to work out the offsets as Get-RpcServer will automatically find the location you can also use an additional feature to extract the type information from your private symbols to fixup the type information.<br /><br />Create a C++ project and in an IDL file copy the <i>PAC_DEVICE_INFO </i>structures from the protocol documentation. Then add the following RPC server.<br /><br />[<br />&nbsp; &nbsp; uuid(4870536E-23FA-4CD5-9637-3F1A1699D3DC),<br />&nbsp; &nbsp; version(1.0),<br />]<br />interface RpcServer<br />{<br />&nbsp; &nbsp; int Test([in] handle_t hBinding,&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[unique] PPAC_DEVICE_INFO device_info);<br />}<br /><br />Add the generated server C code to the project and add the following code somewhere to provide a basic implementation:<br /><br />#pragma comment(lib, "rpcrt4.lib")<br /><br />extern "C" void* __RPC_USER MIDL_user_allocate(size_t size) {<br />&nbsp; &nbsp; return new char[size];<br />}<br /><br />extern "C" void __RPC_USER MIDL_user_free(void* p) {<br />&nbsp; &nbsp; delete[] p;<br />}<br /><br />int Test(<br />&nbsp; &nbsp; handle_t hBinding,<br />&nbsp; &nbsp; PPAC_DEVICE_INFO device_info) {<br />&nbsp; &nbsp; printf("Test %p\n", device_info);<br />&nbsp; &nbsp; return 0;<br />}<br /><br />Now compile the executable as a 64-bit release build if you're using 64-bit PS. The release build ensures there's no weird debug stub in front of your function which could confuse the type information. The implementation of <i>Test </i>needs to be unique, otherwise the linker will fold a duplicate function and the type information will be lost, we just <i>printf </i>a unique string.<br /><br />Now parse the RPC server using <i>Get-RpcServer </i>and format the complex types.<br /><br />PS&gt; $rpc = Get-RpcServer RpcServer.exe -ResolveStructureNames<br />PS&gt; Format-RpcComplexType $rpc.ComplexTypes -Pointer<br /><br />If everything has worked you'll now find the <a href="https://gist.github.com/tyranid/db971d5484b676f6f394bf051450760b" rel="noreferrer" target="_blank">output</a> to be much more useful. Admittedly I also did a bit of further cleanup in my version in <a href="https://github.com/googleprojectzero/sandbox-attacksurface-analysis-tools/blob/master/NtApiDotNet/Win32/Security/Authentication/Kerberos/Ndr/PacDeviceInfoParser.cs" rel="noreferrer" target="_blank">NtApiDotNet</a> as I didn't need the encoders and I added some helper functions.<br /><br />Before leaving this topic I should point out how to handle called to <i>NdrMesType*2</i> in case you need to extract data from a library which uses that API. The parameters are slightly different to <i>NdrMesType*3</i>.<br /><br />void<br />TEST_TYPE_Decode(<br />&nbsp; &nbsp; handle_t _MidlEsHandle,<br />&nbsp; &nbsp; TEST_TYPE * _pType)<br />{<br />&nbsp; &nbsp; NdrMesTypeDecode2(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_MidlEsHandle,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( PMIDL_TYPE_PICKLING_INFO&nbsp; )&amp;__MIDL_TypePicklingInfo,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;TypeEncoders_StubDesc,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( PFORMAT_STRING&nbsp; )&amp;types__MIDL_TypeFormatString.Format[2],<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_pType);<br />}<br /><ol><li>The serialization handle.</li><li>The&nbsp;<i>MIDL_TYPE_PICKLING_INFO</i>&nbsp;structure.</li><li>The <i>MIDL_STUB_DESC</i>&nbsp;structure. This only contains DCE NDR byte code.</li><li>A pointer into the format string for the start of the type.</li><li>A pointer to the structure to serialize or deserialize.</li></ol>Again we can discard the first and last parameters. You can then get the addresses of the middle three and pass them to <i>Get-NdrComplexType</i>.<br /><br />PS&gt; Get-NdrComplexType -PicklingInfo 0x1234
    -StubDesc 0x2345 -TypeFormat 0x3456 -Module $lib

You’ll notice that there’s a offset in the format string (2 in this case) which you can pass instead of the address in memory. It depends what information your disassembler shows:

PS> Get-NdrComplexType -PicklingInfo 0x1234 `
    -StubDesc 0x2345 -TypeOffset 2 -Module $lib

Hopefully this is useful for implementing these NDR serializers in C#. As they don’t rely on any native code (or the RPC runtime) you should be able to use them on other platforms in .NET Core even if you can’t use the ALPC RPC code.

Article Link: https://www.tiraniddo.dev/2020/07/generating-ndr-type-serializers-for-c.html