nga
#include <ntddk.h>
#include <bcrypt.h>
// --- Definitions & Constants ---
#define IOCTL_TPM_COMMAND 0x2277388
#define TPM_CC_ReadPublic 0x173 // 371 in decimal
#define POOL_TAG 'mpTf'
typedef NTSTATUS(__fastcall* PDISPATCH_ROUTINE)(PDEVICE_OBJECT, PIRP);
PDISPATCH_ROUTINE OriginalDispatch = NULL;
// Buffer to store the generated spoofed RSA key
UCHAR SpoofedKeyBuffer[1024];
USHORT SpoofedKeySize = 0;
// --- Helper Functions ---
// Swaps bytes for TPM communication (Big Endian <-> Little Endian)
UINT32 SwapEndian(UINT32 val) {
return _byteswap_ulong(val);
}
// Equivalent to the optimized sub_1400014C0; used to copy key data
void CopyMemoryInternal(void* dest, const void* src, size_t size) {
RtlCopyMemory(dest, src, size);
}
// --- Hook Logic ---
// This function intercepts the TPM response and swaps the real key for the fake one
NTSTATUS TpmCompletionRoutine(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context) {
if (Irp->PendingReturned) {
IoMarkIrpPending(Irp);
}
// Check if the response buffer exists and is large enough
if (Irp->AssociatedIrp.SystemBuffer) {
PVOID buffer = Irp->AssociatedIrp.SystemBuffer;
// Ensure the response data is large enough to contain an Endorsement Key
if (SwapEndian(*(ULONG*)((PUCHAR)buffer + 2)) >= 0xEA) {
DbgPrintEx(0, 0, "[tpm-hook] Overwriting EK data with spoofed key\n");
// Overwrite the real TPM key data with our generated RSA key
RtlCopyMemory((PUCHAR)buffer + 106, SpoofedKeyBuffer, 128);
}
}
return Irp->IoStatus.Status;
}
// The Hooked Dispatch routine for tpm.sys
NTSTATUS HookedDispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
// Check if this is a TPM Command (IOCTL)
if (stack->Parameters.DeviceIoControl.IoControlCode == IOCTL_TPM_COMMAND) {
PUCHAR buffer = (PUCHAR)Irp->AssociatedIrp.SystemBuffer;
// Check if the command is specifically TPM_CC_ReadPublic
if (SwapEndian(*(ULONG*)(buffer + 6)) == TPM_CC_ReadPublic) {
DbgPrintEx(0, 0, "[tpm-hook] Intercepted ReadPublic request\n");
// Set up a Completion Routine to modify the data once the hardware responds
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp, TpmCompletionRoutine, NULL, TRUE, TRUE, TRUE);
return IoCallDriver(DeviceObject, Irp);
}
}
// Pass through all other requests to the original TPM driver
return OriginalDispatch(DeviceObject, Irp);
}
// --- Key Generation ---
// Generates a random RSA 2048-bit key to use for spoofing
NTSTATUS GenerateSpoofedKey() {
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_KEY_HANDLE hKey = NULL;
NTSTATUS status;
ULONG resultSize = 0;
status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RSA_ALGORITHM, NULL, 0);
if (!NT_SUCCESS(status)) return status;
// 0x800 = 2048 bit key
status = BCryptGenerateKeyPair(hAlg, &hKey, 2048, 0);
if (NT_SUCCESS(status)) {
status = BCryptFinalizeKeyPair(hKey, 0);
if (NT_SUCCESS(status)) {
// Export the public key to our global buffer
status = BCryptExportKey(hKey, NULL, BCRYPT_RSAPUBLIC_BLOB, NULL, 0, &resultSize, 0);
if (NT_SUCCESS(status)) {
PUCHAR tempPool = ExAllocatePoolWithTag(NonPagedPool, resultSize, POOL_TAG);
if (tempPool) {
status = BCryptExportKey(hKey, NULL, BCRYPT_RSAPUBLIC_BLOB, tempPool, resultSize, &resultSize, 0);
if (NT_SUCCESS(status)) {
SpoofedKeySize = (USHORT)resultSize;
RtlCopyMemory(SpoofedKeyBuffer, tempPool, (resultSize > 1024 ? 1024 : resultSize));
}
ExFreePoolWithTag(tempPool, POOL_TAG);
}
}
}
}
if (hKey) BCryptDestroyKey(hKey);
if (hAlg) BCryptCloseAlgorithmProvider(hAlg, 0);
return status;
}
// --- Entry Point ---
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
UNREFERENCED_PARAMETER(DriverObject);
UNREFERENCED_PARAMETER(RegistryPath);
DbgPrintEx(0, 0, "[tpm-hook] Initializing Spoofer\n");
// 1. Generate the fake identity
if (!NT_SUCCESS(GenerateSpoofedKey())) {
DbgPrintEx(0, 0, "[tpm-hook] Failed to generate fake RSA key\n");
return STATUS_FAILED_DRIVER_ENTRY;
}
// 2. Find the real TPM driver
UNICODE_STRING tpmName;
PDRIVER_OBJECT TpmDriverObject = NULL;
RtlInitUnicodeString(&tpmName, L"\\Driver\\TPM");
// Use ObReferenceObjectByName (prototyped in headers) to get the driver object
NTSTATUS status = ObReferenceObjectByName(&tpmName, OBJ_CASE_INSENSITIVE, NULL, 0,
*IoDriverObjectType, KernelMode, NULL, (PVOID*)&TpmDriverObject);
if (NT_SUCCESS(status)) {
DbgPrintEx(0, 0, "[tpm-hook] Found tpm.sys at %p, applying hooks...\n", TpmDriverObject);
// 3. Save the original dispatch and overwrite the entire dispatch table
OriginalDispatch = TpmDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
for (int i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++) {
TpmDriverObject->MajorFunction = HookedDispatch;
}
ObDereferenceObject(TpmDriverObject);
DbgPrintEx(0, 0, "[tpm-hook] Successfully hooked tpm.sys\n");
return STATUS_SUCCESS;
}
return status;
}