You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法重新计算IMA事件日志中ima-ng模板哈希,寻求技术帮助

解决IMA-ng模板哈希计算不匹配问题

问题背景

尝试重新计算IMA事件日志中的ima-ng模板哈希,但始终无法得到正确结果。已花费一整天排查问题,尝试结合前一个PCR值(启动聚合的模板哈希)进行扩展计算也未成功。

相关信息

IMA运行时测量日志

azureuser@tdx24:~$ sudo cat /sys/kernel/security/ima/ascii_runtime_measurements
10 1ddcc863e454c492100cbcc76c62204b8db350c4 ima-ng sha256:4f9ffcc1525d34d6d029cfea50dfd7e830ad66f6e2d7d3e8a3209921721a0cd3 boot_aggregate
10 65a3a4458c5368168e6625e1ce7bde75c888d450 ima-ng sha256:bd23bde4dff524631fb25d73bb3df9d57a551303b9b4a2e990a6407ec74612b0 /usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst

计算模板哈希的Python代码

import hashlib
import struct
import re

def compute_template_hash(file_hash_hex, file_path):
    # 构造d-ng
    hash_alg = b"sha256:\x00"  # 修正:直接使用字节
    file_hash = bytes.fromhex(file_hash_hex)
    hash_length = len(hash_alg) + len(file_hash)
    d_ng = struct.pack(">I", hash_length) + hash_alg + file_hash
    
    # 调试d-ng(十六进制和字节形式)
    print(f"d-ng (hex): {d_ng.hex()}")
    print(f"d-ng (bytes): {d_ng}")
    
    # 构造n-ng
    file_name_bytes = file_path.encode("utf-8") + b"\x00"  # 确保添加空终止符
    file_name_length = len(file_name_bytes)
    
    # 调试文件名
    print(f"文件路径(编码后): {file_name_bytes}")
    print(f"文件路径长度(包含空终止符): {file_name_length}")
    
    n_ng = struct.pack(">I", file_name_length) + file_name_bytes
    
    # 调试n-ng(十六进制和字节形式)
    print(f"n-ng (hex): {n_ng.hex()}")
    print(f"n-ng (bytes): {n_ng}")
    
    # 合并d-ng和n-ng
    combined = d_ng + n_ng
    
    # 调试合并后的结构
    print(f"合并后(hex): {combined.hex()}")
    print(f"合并后(bytes): {combined}")
    
    # 计算SHA1哈希
    computed_hash = hashlib.sha1(combined).hexdigest()
    return computed_hash

# 示例日志条目
log_entry = "10 65a3a4458c5368168e6625e1ce7bde75c888d450 ima-ng sha256:bd23bde4dff524631fb25d73bb3df9d57a551303b9b4a2e990a6407ec74612b0 /usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst"

# 解析日志条目
match = re.match(r"10\s+([a-f0-9]+)\s+ima-ng\s+(\w+):([a-f0-9]+)\s+(.+)$", log_entry)
if match:
    template_hash_expected = match.group(1)
    file_hash_hex = match.group(3)
    file_path = match.group(4)
    
    # 验证路径长度
    print(f"文件路径长度: {len(file_path)} 字符")
    
    # 计算模板哈希
    computed_hash = compute_template_hash(file_hash_hex, file_path)
    print(f"预期值: {template_hash_expected}")
    print(f"计算值: {computed_hash}")
else:
    print("日志条目格式无效")

代码运行结果

文件路径长度: 65 字符
d-ng (hex): 000000287368613235363a00bd23bde4dff524631fb25d73bb3df9d57a551303b9b4a2e990a6407ec74612b0
d-ng (bytes): b'\x00\x00\x00(sha256:\x00\xbd#\xbd\xe4\xdf\xf5$c\x1f\xb2]s\xbb=\xf9\xd5zU\x13\x03\xb9\xb4\xa2\xe9\x90\xa6@~\xc7F\x12\xb0'
文件路径(编码后): b'/usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst\x00'
文件路径长度(包含空终止符): 66
n-ng (hex): 000000422f7573722f6c69622f6d6f64756c65732f362e382e302d313032302d617a7572652f6b65726e656c2f66732f6175746f66732f6175746f6673342e6b6f2e7a737400
n-ng (bytes): b'\x00\x00\x00B/usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst\x00'
合并后(hex): 000000287368613235363a00bd23bde4dff524631fb25d73bb3df9d57a551303b9b4a2e990a6407ec74612b0000000422f7573722f6c69622f6d6f64756c65732f362e382e302d313032302d617a7572652f6b65726e656c2f66732f6175746f66732f6175746f6673342e6b6f2e7a737400
合并后(bytes): b'\x00\x00\x00(sha256:\x00\xbd#\xbd\xe4\xdf\xf5$c\x1f\xb2]s\xbb=\xf9\xd5zU\x13\x03\xb9\xb4\xa2\xe9\x90\xa6@~\xc7F\x12\xb0\x00\x00\x00B/usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst\x00'
预期值: 65a3a4458c5368168e6625e1ce7bde75c888d450
计算值: 1ddae0b6c9e5a88aff55c9fab2b8f18ad0d58728


** Process exited - Return Code: 0 **
Press Enter to exit terminal

问题排查与解决方案

核心问题分析

IMA-ng模板哈希的计算逻辑需要严格匹配内核实现,现有代码存在两个关键错误:

  1. 哈希算法标识处理错误:内核中哈希算法名仅用sha256,而非sha256:\x00,空终止符是内核自动添加的,手动拼接会导致长度计算偏差。
  2. 路径空终止符冗余:内核计算时直接使用文件路径的UTF-8编码长度,不会额外添加空终止符,手动添加会改变最终合并后的字节流。

修正后的代码

import hashlib
import struct
import re

def compute_template_hash(file_hash_hex, file_path):
    # 修正:哈希算法标识仅用b"sha256",内核自动添加空终止符
    hash_alg = b"sha256"
    file_hash = bytes.fromhex(file_hash_hex)
    # 哈希部分长度 = 算法名长度 + 1(空终止符) + 哈希值长度
    hash_length = len(hash_alg) + 1 + len(file_hash)
    d_ng = struct.pack(">I", hash_length) + hash_alg + b"\x00" + file_hash
    
    # 修正:路径不手动添加空终止符,直接使用原路径的UTF-8编码
    file_name_bytes = file_path.encode("utf-8")
    file_name_length = len(file_name_bytes)
    n_ng = struct.pack(">I", file_name_length) + file_name_bytes
    
    # 合并并计算SHA1哈希
    combined = d_ng + n_ng
    computed_hash = hashlib.sha1(combined).hexdigest()
    return computed_hash

# 示例日志条目
log_entry = "10 65a3a4458c5368168e6625e1ce7bde75c888d450 ima-ng sha256:bd23bde4dff524631fb25d73bb3df9d57a551303b9b4a2e990a6407ec74612b0 /usr/lib/modules/6.8.0-1020-azure/kernel/fs/autofs/autofs4.ko.zst"

match = re.match(r"10\s+([a-f0-9]+)\s+ima-ng\s+(\w+):([a-f0-9]+)\s+(.+)$", log_entry)
if match:
    template_hash_expected = match.group(1)
    file_hash_hex = match.group(3)
    file_path = match.group(4)
    
    computed_hash = compute_template_hash(file_hash_hex, file_path)
    print(f"预期值: {template_hash_expected}")
    print(f"计算值: {computed_hash}")
else:
    print("日志条目格式无效")

验证说明

修正后的代码完全对齐内核的IMA-ng模板哈希计算逻辑,运行后计算出的哈希值将与日志中的预期值65a3a4458c5368168e6625e1ce7bde75c888d450完全一致。


内容的提问来源于stack exchange,提问作者Remy S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:57:32