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

动态调试实战:Frida脚本编写与内存注入

1. 环境深度配置方案

1.1 多设备适配方案

# 多设备连接管理脚本  
frida-ps -H 192.168.1.100:27042  # 远程设备  
frida-ps -U                      # USB设备  
frida-ps -D emulator-5554        # 指定模拟器  

设备指纹伪装技术

// 修改设备指纹特征  
Java.perform(() => {  
    const Build = Java.use('android.os.Build');  
    Build.MANUFACTURER.value = "Generic";  
    Build.MODEL.value = "VirtualDevice";  
    Build.FINGERPRINT.value = "robolectric/robolectric";  
});  

1.2 内核级调试支持

// 内核模块注入代码示例  
#include <linux/module.h>  
static int __init frida_init(void) {  
    printk(KERN_INFO "FRIDA Kernel Module Loaded\n");  
    return 0;  
}  
module_init(frida_init);  

2. 脚本开发体系

2.1 基础Hook模式

2.1.1 Java层拦截
// 拦截Activity启动流程  
Java.use('android.app.Activity').onCreate.implementation = function(bundle) {  
    console.log(`[+] Activity Created: ${this.getClass().getName()}`);  
    this.onCreate(bundle);  
};  
2.1.2 Native层Hook
// Hook libc的open函数  
Interceptor.attach(Module.findExportByName('libc.so', 'open'), {  
    onEnter: function(args) {  
        this.path = args[0].readCString();  
        console.log(`Opening: ${this.path}`);  
    },  
    onLeave: function(retval) {  
        if (parseInt(retval) === -1) {  
            console.error(`Open failed: ${this.path}`);  
        }  
    }  
});  

2.2 复杂数据类型处理

// 解析复杂Java对象  
function parseBundle(bundle) {  
    const Bundle = Java.use('android.os.Bundle');  
    const keys = bundle.keySet().toArray();  
    return keys.map(key => {  
        return {  
            key: key.toString(),  
            value: Bundle.getString.call(bundle, key)  
        };  
    });  
}  

3. 内存操作技术

3.1 内存扫描与修改

// 全局内存特征搜索  
const pattern = '7F 45 4C 46 ?? ?? ?? ?? 03 00';  
Memory.scan(Module.base, Module.size, pattern, {  
    onMatch: function(address, size) {  
        console.log(`ELF Header found at: ${address}`);  
        Memory.protect(address, size, 'rwx');  
        address.writeByteArray([0x7F, 0x45, 0x4C, 0x46]);  
    }  
});  

3.2 内存Dump与修复

# 内存段导出工具  
import frida  
session = frida.get_usb_device().attach("com.target.app")  
mem = session.read_bytes(0x12345678, 4096)  
with open("dump.bin", "wb") as f:  
    f.write(mem)  

4. 多线程调试方案

4.1 线程状态监控

// 线程创建监控  
const pthread_create = Module.findExportByName('libc.so', 'pthread_create');  
Interceptor.attach(pthread_create, {  
    onEnter: function(args) {  
        this.start_routine = args[2];  
    },  
    onLeave: function(retval) {  
        console.log(`New thread entry: ${this.start_routine}`);  
    }  
});  

4.2 TLS数据拦截

// 线程局部存储操作  
__thread int g_counter = 0;  
void modify_tls() {  
    g_counter = 123;  
}  
// 修改TLS数据  
const tls_base = Process.getCurrentThread().tls;  
Memory.writeS32(tls_base.add(0x10), 456);  


5. 自动化Hook框架

5.1 规则引擎设计

// Hook规则配置文件  
{  
  "hooks": [  
    {  
      "type": "java",  
      "class": "com.example.Crypto",  
      "method": "encrypt",  
      "params": ["java.lang.String"]  
    },  
    {  
      "type": "native",  
      "module": "libnative.so",  
      "symbol": "AES_encrypt"  
    }  
  ]  
}  

5.2 动态规则加载

// 热加载Hook配置  
function reloadConfig(config) {  
    config.hooks.forEach(rule => {  
        if (rule.type === 'java') {  
            hookJavaMethod(rule);  
        } else {  
            hookNativeSymbol(rule);  
        }  
    });  
}  

6. 反检测对抗技术

6.1 Frida特征消除

# 修改frida-server字符串特征  
sed -i 's/frida/fake/g' frida-server  
hexdump -C frida-server | grep '66 61 6b 65'  

6.2 调试端口混淆

// 动态端口切换  
int get_debug_port() {  
    return 31337 + (time(NULL) % 1000);  
}  

7. 企业级应用案例

7.1 通信协议逆向

// SSL_read/SSL_write双向Hook  
const ssl_read = Module.findExportByName('libssl.so', 'SSL_read');  
const ssl_write = Module.findExportByName('libssl.so', 'SSL_write');  

function hook_ssl(func) {  
    Interceptor.attach(func, {  
        onEnter: function(args) {  
            this.buf = args[1];  
            this.len = args[2];  
        },  
        onLeave: function(retval) {  
            console.log(hexdump(this.buf, { length: this.len }));  
        }  
    });  
}  

hook_ssl(ssl_read);  
hook_ssl(ssl_write);  

7.2 游戏引擎逆向

// Unity3D游戏对象分析  
const GameObject = Il2Cpp.Domain.assembly("UnityEngine").image.class("GameObject");  
GameObject.method("GetComponent").implementation = function(type) {  
    const result = this.GetComponent(type);  
    console.log(`GetComponent: ${type.name} => ${result}`);  
    return result;  
};  

8. 性能优化策略

8.1 脚本预编译技术

# 编译脚本为字节码  
frida-compile agent.js -o _agent.js  

8.2 内存缓存机制

// LRU缓存实现  
class MemoryCache {  
    constructor(maxSize = 100) {  
        this.cache = new Map();  
        this.maxSize = maxSize;  
    }  

    get(address) {  
        // ...  
    }  
}  

关于作者:

15年互联网开发、带过10-20人的团队,多次帮助公司从0到1完成项目开发,在TX等大厂都工作过。当下为退役状态,写此篇文章属个人爱好。本人开发期间收集了很多开发课程等资料,需要可联系我

相关文章:

  • 【实战ES】实战 Elasticsearch:快速上手与深度实践-附录-1-常用命令速查表-集群健康检查、索引生命周期管理、故障诊断命令
  • stable Diffusion 中的 VAE是什么
  • P3390 【模板】矩阵快速幂
  • Redis项目_黑马点评
  • 【JavaEE】Spring Web MVC
  • 蓝队基本技能 web入侵指南 日志分析 后门查杀 流量分析
  • Houdini学习笔记
  • 2024 CCPC Liaoning Provincial Contest K
  • 【C++】每日一练(用队列实现栈)
  • VSTO(C#)Excel开发6:与窗体交互
  • Java网络多线程
  • TCP网络协议
  • 评委打分5个评委 去掉一个最高分和一个最低分 取平均分
  • Java高频面试之集合-11
  • 【2025】基于springboot+vue+uniapp的厨师预约上门做菜小程序(源码、万字文档、图文修改、调试答疑)
  • 使用Qt创建悬浮窗口
  • NPU的工作原理:神经网络计算的流水线
  • 【开源+代码解读】Search-R1:基于强化学习的检索增强大语言模型框架3小时即可打造个人AI-search
  • Linux动态监控系统
  • C++ std::list超详细指南:基础实践(手搓list)
  • html网站怎么做的/百度seo搜索引擎优化厂家
  • oss怎么做网站/网络营销的六个特点
  • 模板建站费用/网站怎么做出来的
  • 信誉好的东莞网站建设/怎样优化关键词到首页
  • wordpress 企业网站主题/顾问式营销
  • 做网站 图片格式/百度域名查询官网