当前位置: 首页 > news >正文

加密算法逆向与HOOK技术实战

1. 密码学基础与逆向特征识别

1.1 算法分类与模式特征

常见算法指纹库

# 算法特征识别字典  
CRYPTO_SIGNATURES = {  
    "AES": {  
        "init": ["AES/ECB", "AES/CBC", "AES/GCM"],  
        "key_len": [128, 256],  
        "iv_required": True  
    },  
    "RSA": {  
        "init": ["RSA/ECB/PKCS1Padding"],  
        "key_spec": ["RSAPublicKey", "RSAPrivateKey"]  
    },  
    "HMAC": {  
        "digest": ["HmacSHA256", "HmacMD5"]  
    }  
}  
1.1.1 对称加密特征
  • AES-CBC模式逆向要点

    • 定位IvParameterSpec初始化

    • 追踪Cipher.getInstance("AES/CBC/PKCS5Padding")调用

    • 识别密钥扩展过程(PBKDF2/Scrypt)

1.1.2 非对称加密特征
  • RSA-OAEP模式识别

    • 查找Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")

    • 分析密钥存储方式(Keystore vs 硬编码)


2. 静态逆向分析技术

2.1 密钥定位策略

2.1.1 硬编码密钥提取
// 典型密钥硬编码模式  
public class Config {  
    private static final String SECRET_KEY = "A3F8D9E1B5C72A";  
    private static final byte[] IV = {0x01, 0x02...};  
}  

自动化扫描脚本

def find_hardcoded_keys(code):  
    patterns = [  
        r'String\s+\w+\s*=\s*"[A-F0-9]{16,}"',  
        r'byte\[\]\s+\w+\s*=\s*\{0x[0-9A-F]{2}(,\s*0x[0-9A-F]{2}){7,}\}'  
    ]  
    return re.findall('|'.join(patterns), code)  
2.1.2 动态密钥推导分析

PBKDF2算法逆向流程

  1. 定位SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")

  2. 提取盐值(Salt)生成逻辑

  3. 分析迭代次数参数

  4. 追踪派生密钥使用路径


3. 动态HOOK技术实战

3.1 Java层HOOK方案

3.1.1 Cipher类监控
Java.perform(() => {  
    const Cipher = Java.use('javax.crypto.Cipher');  
    Cipher.doFinal.overload('[B').implementation = function(input) {  
        console.log(`AES Input: ${hexdump(input)}`);  
        const result = this.doFinal(input);  
        console.log(`AES Output: ${hexdump(result)}`);  
        return result;  
    };  
});  
3.1.2 密钥工厂监控
const SecretKeySpec = Java.use('javax.crypto.spec.SecretKeySpec');  
SecretKeySpec.$init.overload('[B', 'java.lang.String').implementation = function(key, algo) {  
    console.log(`Key Spec Created: ${hexdump(key)} | Algorithm: ${algo}`);  
    return this.$init(key, algo);  
};  

3.2 Native层HOOK方案

3.2.1 OpenSSL函数拦截
const SSL_write = Module.findExportByName('libssl.so', 'SSL_write');  
Interceptor.attach(SSL_write, {  
    onEnter: function(args) {  
        this.ssl = args[0];  
        this.data = args[1];  
        this.len = args[2];  
    },  
    onLeave: function(retval) {  
        console.log(`SSL Write: ${hexdump(this.data, this.len.toInt32())}`);  
    }  
});  
3.2.2 自定义加密库破解
// 目标函数原型  
void custom_encrypt(char* data, int len, char key[16]);  
const encrypt_addr = Module.findExportByName('libenc.so', 'custom_encrypt');  
Interceptor.attach(encrypt_addr, {  
    onEnter: function(args) {  
        console.log(`Key: ${hexdump(args[2], 16)}`);  
        this.encrypted = Memory.dup(args[0], args[1].toInt32());  
    },  
    onLeave: function(retval) {  
        console.log(`Encrypted: ${hexdump(this.encrypted, args[1].toInt32())}`);  
    }  
});  

 


4. 协议逆向工程

4.1 请求响应加解密

典型流程逆向步骤

  1. 抓取原始请求/响应数据包

  2. 定位加密入口(JSON序列化前后)

  3. 追踪加密模式与密钥传递链

  4. 重构加解密原型代码

案例:某IoT设备协议

# 重构后的解密函数  
def iot_decrypt(ciphertext: bytes, key: bytes) -> bytes:  
    iv = ciphertext[:16]  
    cipher = AES.new(key, AES.MODE_CBC, iv)  
    return unpad(cipher.decrypt(ciphertext[16:]))  

5. 白盒密码分析

5.1 密钥隐藏技术破解

字符串混淆对抗方案

// 原密钥生成  
String key = decodeStr(new byte[]{0x12, 0x34...});  

// 动态Hook获取  
Java.use("com.example.Crypto").decodeStr.implementation = function(arr) {  
    const result = this.decodeStr(arr);  
    console.log(`Decoded Key: ${result}`);  
    return result;  
};  

5.2 代码虚拟化对抗

VMP保护逆向策略

  1. 定位虚拟机入口函数

  2. 分析字节码调度逻辑

  3. Hook解释器核心函数

const opcode_handler = Module.findExportByName('libvmp.so', 'handle_opcode');  
Interceptor.attach(opcode_handler, {  
    onEnter: function(args) {  
        const opcode = args[0].toInt32();  
        console.log(`VMP Opcode: 0x${opcode.toString(16)}`);  
    }  
});  

6. 自动化逆向框架

6.1 密钥追踪系统设计

class KeyTracer:  
    def __init__(self, apk):  
        self.apk = apk  
        self.keys = []  

    def trace(self):  
        # 静态分析定位密钥相关代码  
        for cls in self.apk.classes:  
            if "Crypto" in cls.name:  
                self._analyze_crypto_class(cls)  

    def _analyze_crypto_class(self, cls):  
        # 具体分析逻辑...  

6.2 智能模式匹配引擎

rules:  
  - name: AES_KEY_DERIVATION  
    pattern: |  
      SecretKeyFactory\.getInstance\("PBKDF2WithHmacSHA256"\)  
      .*generateSecret\(.*\)  
    action: LOG_KEY  

7. 反HOOK对抗技术

7.1 环境检测防御

检测Frida特征

__attribute__((constructor)) void detect_frida() {  
    if (access("/data/local/tmp/frida-server", F_OK) == 0) {  
        exit(0);  
    }  
}  

7.2 动态代码混淆

指令级混淆方案

; 原始指令  
LDR R0, [R1]  
ADD R0, R0, #1  
STR R0, [R1]  

; 混淆后  
MOV R3, #1  
LDR R0, [R1]  
ADD R0, R0, R3  
STR R0, [R1]  
NOP  
BX LR  

8. 企业级实战案例

8.1 金融APP加密协议逆向

破解流程

  1. 使用jadx定位com.xxx.security

  2. 分析SecureSession初始化过程

  3. Hook SSLContext.init获取密钥材料

  4. 提取并验证RSA公钥证书

  5. 重写Python请求模拟器

关键代码

from cryptography.hazmat.primitives import serialization  
from requests import Session  

class FinancialAPI(Session):  
    def __init__(self, pub_key):  
        self.pub_key = pub_key  
        # 证书加载...  

    def _sign_request(self, data):  
        # 使用逆向得到的签名逻辑...  
        return signed_data  

相关文章:

  • 吴恩达机器学习笔记复盘(四)线性回归模型概述
  • Unity编辑器界面扩展——4、Inspector栏UI扩展
  • SpringBoot实现一个Redis限流注解
  • 如何从受 Cloudflare 保护的网站提取数据:技术与挑战
  • 每日一题---数组中两个字符串的最小距离
  • 混淆矩阵概念
  • 使用kubeadm方式以及使用第三方工具sealos搭建K8S集群
  • 使用Node的http模块创建web服务,给客户端返回html页面时,css失效的根本原因(有助于理解http)
  • 走路碎步营养补充贴士
  • 使用libwebsocket写一个server
  • 【AI】利用Azure AI的元数据过滤器提升 RAG 性能并增强向量搜索案例
  • 【备考记录】三种校验码
  • pop是什么的缩写?为什么Python用它表示删除元素?
  • 【统计学相关笔记】2. 多元正态的Cochran定理
  • iptables练习笔记20250315
  • 盖革管死区时间导致脉冲丢失分析
  • 3.9/Q2,Charls最新文章解读!
  • 苹果电脑杀毒软件CleanMyMac
  • Android 手机启动过程
  • [C++Qt] 槽函数收不到信号问题(信号的注册)
  • 调查:“网约摩的”上线起步价五六元,合规性及安全性引质疑
  • 世卫大会中国代表团:中国深入参与全球卫生治理,为构建人类卫生健康共同体贡献中国力量
  • 从《缶翁的世界》开始,看吴昌硕等湖州籍书画家对海派的影响
  • 北斗系统全面进入11个国际组织的标准体系
  • 陈刚:推动良好政治生态和美好自然生态共生共优相得益彰
  • 专访|《内沙》导演杨弋枢:挽留终将失去的美好