密钥及证书检测 crypto_material

该文章展示了如何利用Yara规则库检测加密材料,如SSH密钥和SSL证书。通过加载crypto_material.yar文件,对libcurl.so.5.3.0文件进行匹配,结果显示检测到了genericPublicKey。HxD工具验证了Yara规则中定义的字符串存在。

用于检测加密材料,如SSH密钥和SSL证书。

文件:libcurl.so.5.3.0

yara文件:https://github.com/leiwuhen92/yara_rule/blob/main/compile_rule/crypto_material.yc

yara命令检测:

yara --print-meta --print-strings crypto_material.yar ibcurl.so.5.3.0

实现逻辑:

import pathlib
import yara
NAME="crypto_material"
CHINESE_NAME="密钥及证书检测"
CHINESE_DESCRIPTION="检测加密材料,如SSH密钥和SSL证书"

# 结果存储位置
class FileAnalysis():
   def__init__(self):
        self.file_path="libcurl.so.5.3.0"
        self.processed_analysis={}
file_object=FileAnalysis()

# 特征文件路径
compile_path=pathlib.Path("crypto_material.yc")

# 加载特征文件
rule=yara.load(str(compile_path))
# 匹配
result=rule.match(file_object.file_path)
print("result:%s"%result)

# 解析结果
file_object.processed_analysis[NAME]={}
summary=set()
foriteminresult:
   print(item)
   print(item.rule)
   print(item.meta)
    summary.add(item.rule)
    file_object.processed_analysis[NAME][item.rule]={"meta":item.meta}
file_object.processed_analysis[NAME]["summary"]=list(summary)

mongo_data={
   "file_path":file_object.file_path,
   "processed_analysis":file_object.processed_analysis
}
print(mongo_data)

运行结果:

result:[genericPublicKey]
genericPublicKey
genericPublicKey
{'author': 'Joerg Stucke', 'description': 'Generic Public Key Block', 'date': '2017-03-16', 'version': '2', 'version_schema_information': 'Version number is increased whenever something changes.'}

{
    "file_path":"libcurl.so.5.3.0",
    "processed_analysis": {
        "crypto_material": {
            "genericPublicKey": {
                "meta": {
                    "author":"Joerg Stucke",
                    "description":"Generic Public Key Block",
                    "date":"2017-03-16",
                    "version":"2",
                    "version_schema_information":"Version number is increased whenever something changes."
                }
            },
            "summary": [
                "genericPublicKey"
            ]
        }
    }
}

从结果可知密钥方式为genericPublicKey。

genericPublicKey的yara规则如下:两条字符串在文本内

HxD工具打开libcurl.so.5.3.0:搜索yara中的两条字符串(-----BEGIN PUBLIC KEY----- 与 -----END PUBLIC KEY-----),都能检索到

参考:

(1条消息) yara规则初识_青霄的博客-优快云博客

import base64 import os import sys import secrets from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from Crypto.Hash import SHA256 from Crypto.Protocol.KDF import HKDF # ---------- 多轮 XOR 加密 ---------- def multi_xor(data: bytes, keys: list) -> bytes: for key in keys: key_bytes = base64.b64decode(key) data = bytes([b ^ key_bytes[i % len(key_bytes)] for i, b in enumerate(data)]) return data # ---------- 生成 header 字符串 ---------- def to_c_header(b64_data, b64_key, b64_iv, b64_tag, xor_keys): xor_lines = '\n'.join([f' "{k}",' for k in xor_keys]) return f"""#pragma once const char* b64_data = R"({b64_data})"; const char* b64_key = "{b64_key}"; const char* b64_iv = "{b64_iv}"; const char* b64_tag = "{b64_tag}"; const char* xor_keys[] = {{ {xor_lines} }}; const int xor_key_count = {len(xor_keys)}; """ # ---------- 主函数 ---------- def main(): if len(sys.argv) != 2: print("Usage: python generate_header.py sc.bin") return input_file = sys.argv[1] if not os.path.exists(input_file): print(f"[ERROR] 文件不存在: {input_file}") return with open(input_file, "rb") as f: raw = f.read() # ----- 随机多轮 XOR key(3轮) ----- xor_keys = [base64.b64encode(secrets.token_bytes(8)).decode() for _ in range(3)] xor_applied = multi_xor(raw, xor_keys[::-1]) # 逆序加密 # ----- 随机 AES-GCM 加密 ----- key_material = get_random_bytes(32) aes_key = HKDF(master=key_material, key_len=32, salt=None, hashmod=SHA256) iv = get_random_bytes(12) cipher = AES.new(aes_key, AES.MODE_GCM, nonce=iv) encrypted, tag = cipher.encrypt_and_digest(xor_applied) # ----- Base64 编结果 ----- b64_data = base64.b64encode(encrypted).decode() b64_key = base64.b64encode(key_material).decode() b64_iv = base64.b64encode(iv).decode() b64_tag = base64.b64encode(tag).decode() # ----- 生成 C++ 头文件 ----- header = to_c_header(b64_data, b64_key, b64_iv, b64_tag, xor_keys) with open("shellcode_b64.h", "w", encoding="utf-8") as f: f.write(header) print("[OK] 生成成功!文件: shellcode_b64.h") if __name__ == "__main__": main() 有没有问题
最新发布
11-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值