<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tpm2 on virtfunc</title><link>https://virtfunc.com/tags/tpm2/</link><description>Recent content in Tpm2 on virtfunc</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 26 Nov 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://virtfunc.com/tags/tpm2/index.xml" rel="self" type="application/rss+xml"/><item><title>TPM2 AntiLog</title><link>https://virtfunc.com/projects/tpm2-antilog/</link><pubDate>Wed, 26 Nov 2025 00:00:00 +0000</pubDate><guid>https://virtfunc.com/projects/tpm2-antilog/</guid><description>&lt;p&gt;TCG logs can be used to verify the boot chain against tampering, and are often used by software to check that the early boot sequence has not been tampered with, and thus that the kernel is (likely) intact.&lt;/p&gt;
&lt;p&gt;However because the root of trust is often the SPI flash itself, such a system is vulnerable to patching of the routines that log and extend the TPM2 Platform Configuration Registers (PCRs). This post will discuss a simple patch that prevents logging of UEFI image hashes and extension of the PCRs related the boot sequence.&lt;/p&gt;</description><content>&lt;p&gt;TCG logs can be used to verify the boot chain against tampering, and are often used by software to check that the early boot sequence has not been tampered with, and thus that the kernel is (likely) intact.&lt;/p&gt;
&lt;p&gt;However because the root of trust is often the SPI flash itself, such a system is vulnerable to patching of the routines that log and extend the TPM2 Platform Configuration Registers (PCRs). This post will discuss a simple patch that prevents logging of UEFI image hashes and extension of the PCRs related the boot sequence.&lt;/p&gt;
&lt;p&gt;Given a firmware binary, the location of the code that performs the measurement must be identified. An easy way I have found to do this is to reference the &lt;a href="https://github.com/tianocore/edk2/"&gt;EDKII&lt;/a&gt; source code and find functions relating to measurement, log creation and PCR extension. &lt;a href="https://github.com/tianocore/edk2/blob/ad5030d73171d9aae30ee74c4b2cd41686b93bcd/SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c#L1310"&gt;&lt;code&gt;Tcg2HashLogExtendEvent&lt;/code&gt;&lt;/a&gt; is a function that resides in &lt;code&gt;Tcg2Dxe&lt;/code&gt;, so analysis should be started by dumping the PE with this name or its associated GUID using a tool that can parse a UEFI firmware image, such as UEFITool.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;EFI_STATUS&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;EFIAPI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;Tcg2HashLogExtendEvent&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;EFI_TCG2_PROTOCOL&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;This&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;UINT64&lt;/span&gt; &lt;span class="n"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;EFI_PHYSICAL_ADDRESS&lt;/span&gt; &lt;span class="n"&gt;DataToHash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;UINT64&lt;/span&gt; &lt;span class="n"&gt;DataToHashLen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;EFI_TCG2_EVENT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Event&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After extracting this binary, the image can be analyzed. I will be using IDA Pro with &lt;a href="https://github.com/binarly-io/efiXplorer/"&gt;efiXplorer&lt;/a&gt; to more easily manage the GUIDs and automatic location of boot services functions. After performing analysis with efiXplorer, a list of multiple protocols interfaces was produced. &lt;code&gt;AMI_PROTOCOL_INTERNAL_HLXE&lt;/code&gt; looked like a good place to start analysis.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/efiXplorer_protocols.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;Some other interfaces of interest were also found in the same call to&lt;br&gt;
&lt;code&gt;gBS-&amp;gt;InstallMultipleProtocolInterfaces&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/interfaces_of_interest.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/struct_ptr.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;Given that this is an interface, it likely has other functions, but the function pointer at offset 0 of the pointer appears to perform hashing of the images because of a call to a function in &lt;code&gt;AMI_DXE_HASH_INTERFACE&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/AMI_DXE_HASH_INTERFACE_caller.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;Given this discovery, I am going to refer to this function as &lt;code&gt;AMI_PROTOCOL_INTERNAL_HLXE&lt;/code&gt;, HLXE being short for Hash Log eXtend Event.&lt;/p&gt;
&lt;p&gt;The function responsible for measuring the images in the DXE phase when they are loaded has been identified, and now creating the aforementioned patch is simple. By synchronizing the hex view with the disassembly, we can copy the function bytes to create a crude signature for this firmware.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/hex_view.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;The resultant bytes are:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;48 8B C4 48 89 58 10 4C 89 40 18 48 89 48 08 55 56 57 41 54 41 55 41 56 41 57 48 83 EC&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This appears to work for many modern AMI binaries, but for some older ones, the function signature is vastly different:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;4C 89 4C 24 20 4C 89 44 24 18 48 89 54 24 10 48 89 4C 24 08 48 81 EC E8 01 00 00 48 C7&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;In either case the function can be patched out by replacing these bytes with the following instructions, padded with extra C3 (ret) to match the length of the search bytes. Obviously we do not care about the code we are overwriting because it will never be executed under our patch.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;48 31 C0 xor rax, rax
C3 ret
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The final patch, after converting to a format UEFIPatch can use, is as follows:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-patch" data-lang="patch"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# Return EFI_SUCCESS early in AMI&amp;#39;s internal HashLogExtendEvent function.
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;39045756-FCA3-49BD-8DAE-C7BAE8389AFF 10 P:488BC4488958104C8940184889480855565741544155415641574883EC:4831C0C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;39045756-FCA3-49BD-8DAE-C7BAE8389AFF 10 P:4C894C24204C89442418488954241048894C24084881ECE801000048C7:4831C0C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This patch functions as expected on my MSI AM5 motherboard, here are the results of the PCRs after the patch. The firmware PCR remains but does not produce any meaningful logs related to the boot chain. It was redacted because I do not want these specific firmware images to be used for fingerprinting. The measurement of the firmware occurs in a different part of the boot chain, thus the PCR is populated.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://virtfunc.com/images/tpm2-antilog/final_pcrs.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;A wasm based UEFIPatch (with this patch available to apply in a few clicks) is available at &lt;a href="https://uefipatch.virtfunc.com/"&gt;https://uefipatch.virtfunc.com/&lt;/a&gt;&lt;/p&gt;</content></item></channel></rss>