hook框架frida的安装以及简单实用案例

1.下载地址

https://github.co/frida/frida/releases

2.另外两种安装方法

1.Install from prebuilt binaries

This is the recommended way to get started. All you need to do is:

pip install frida-tools # CLI tools
pip install frida       # Python bindings
npm install frida       # Node.js bindings

You may also download pre-built binaries for various operating systems from Frida‘s releases page on GitHub.

2.Build your own binaries

Dependencies

For running the Frida CLI tools, i.e. frida, frida-ls-devices, frida-ps, frida-kill, frida-trace, and frida-discover, you need Python plus a few packages:

pip3 install colorama prompt-toolkit pygments

Linux

make

macOS and iOS

First make a trusted code-signing certificate. You can use the guide at https://sourceware.org/gdb/wiki/PermissionsDarwin in the sections "Create a certificate in the System Keychain" and "Trust the certificate for code signing". You can use the name frida-cert instead of gdb-cert if you‘d like.

Next export the name of the created certificate to the environment variables MAC_CERTID and IOS_CERTID, and run make:

export MAC_CERTID=frida-cert
export IOS_CERTID=frida-cert
make

To ensure that macOS accepts the newly created certificate, restart the taskgated daemon:

sudo killall taskgated

Windows

frida.sln

(Requires Visual Studio 2017.)

3.简单实用

得到android手机当前最前端Activity所在的进程

get_front_app.py

其中get_front_app.py的内容如下:

import frida
rdev = frida.get_remote_device()
front_app = rdev.get_frontmost_application()
print front_app
12341234

枚举android手机所有的进程

enum_process.py

enum_process.py内容如下:

import frida
rdev = frida.get_remote_device()
processes = rdev.enumerate_processes()
for process in processes:
    print process
1234512345

枚举某个进程加载的所有模块以及模块中的导出函数

import frida
rdev = frida.get_remote_device()
session = rdev.attach("com.tencent.mm")  #如果存在两个一样的进程名可以采用rdev.attach(pid)的方式
modules = session.enumerate_modules()
for module in modules:
    print module
    export_funcs = module.enumerate_exports()
    print "\tfunc_name\tRVA"
    for export_func in export_funcs:
        print "\t%s\t%s"%(export_func.name,hex(export_func.relative_address))
1234567891012345678910

hook android的native函数

import frida
import sys
rdev = frida.get_remote_device()
session = rdev.attach("com.tencent.mm")
scr = """
Interceptor.attach(Module.findExportByName("libc.so" , "open"), {
    onEnter: function(args) {
        send("open("+Memory.readCString(args[0])+","+args[1]+")");
    },
    onLeave:function(retval){

    }
});
"""
script = session.create_script(scr)
def on_message(message ,data):
    print message
script.on("message" , on_message)
script.load()
sys.stdin.read()
12345678910111213141516171819201234567891011121314151617181920

hook android的java层函数

如下代码为hook微信(测试版本为6.3.13,不同版本由于混淆名字的随机生成的原因或者代码改动导致类名不一样)
com.tencent.mm.sdk.platformtools.ay类的随机数生成函数,让微信猜拳随机(type=2),而摇色子总是为6点(type=5)

import frida
import sys
rdev = frida.get_remote_device()
session = rdev.attach("com.tencent.mm")

scr = """
Java.perform(function () {
var ay = Java.use("com.tencent.mm.sdk.platformtools.ay");
ay.pu.implementation = function(){
    var type = arguments[0];
    send("type="+type);
    if (type == 2)
    {
    return this.pu(type);
    }
    else
    {
    return 5;
    }
};

});
"""

script = session.create_script(scr)
def on_message(message ,data):
    print message
script.on("message" , on_message)
script.load()
sys.stdin.read()
123456789101112131415161718192021222324252627282930123456789101112131415161718192021222324252627282930

通过frida向android进程注入dex

import frida, sys, optparse, re
def on_message(message, data):
    if message['type'] == 'send':
        print("[*] {0}".format(message['payload']))
    else:
        print(message)

jscode = """
Java.perform(function () {
    var currentApplication = Java.use("android.app.ActivityThread").currentApplication();
    var context = currentApplication.getApplicationContext();
    var pkgName = context.getPackageName();
    var dexPath = "%s";
    var entryClass = "%s";
    Java.openClassFile(dexPath).load();
    console.log("inject " + dexPath +" to " + pkgName + " successfully!")
    Java.use(entryClass).%s("%s");
    console.log("call entry successfully!")
});
"""

def checkRequiredArguments(opts, parser):
    missing_options = []
    for option in parser.option_list:
        if re.match(r'^\[REQUIRED\]', option.help) and eval('opts.' + option.dest) == None:
            missing_options.extend(option._long_opts)
    if len(missing_options) > 0:
        parser.error('Missing REQUIRED parameters: ' + str(missing_options))

if __name__ == "__main__":
    usage = "usage: python %prog [options] arg\n\n"             "example: python %prog -p com.android.launcher "             "-f /data/local/tmp/test.apk "             "-e com.parker.test.DexMain/main "             "\"hello fridex!\""
    parser = optparse.OptionParser(usage)
    parser.add_option("-p", "--package", dest="pkg", type="string",
                      help="[REQUIRED]package name of the app to be injected.")
    parser.add_option("-f", "--file", dest="dexPath", type="string",
                      help="[REQUIRED]path of the dex")
    parser.add_option("-e", "--entry", dest="entry", type="string",
                      help="[REQUIRED]the entry function Name.")

    (options, args) = parser.parse_args()
    checkRequiredArguments(options, parser)
    if len(args) == 0:
        arg = ""
    else:
        arg = args[0]

    pkgName = options.pkg
    dexPath = options.dexPath
    entry = options.entry.split("/")
    if len(entry) > 1:
        entryClass = entry[0]
        entryFunction = entry[1]
    else:
        entryClass = entry[0]
        entryFunction = "main"

    process = frida.get_usb_device(1).attach(pkgName)
    jscode = jscode%(dexPath, entryClass, entryFunction, arg)
    script = process.create_script(jscode)
    script.on('message', on_message)
    print('[*] Running fridex')
    script.load()
    sys.stdin.read()
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666712345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667

通过注入抛出异常代码实现跟踪程序调用栈

在<>这本书中第八章有介绍通过重打包写入异常代码进行栈跟踪,但是这样比较麻烦,使用frida注入更方便。

原文地址:https://www.cnblogs.com/pythonywy/p/12254670.html

时间: 2024-08-13 00:04:53

hook框架frida的安装以及简单实用案例的相关文章

jquery,tree无限级树形菜单+简单实用案例

jquery,tree无限级树形菜单+简单实用案例 我在项目中用到产品类别的树形.各种地方都要用. 我就封装起来,方便以后调用. 记录下来,希望给新手们提供帮助.要记得导入jquery.js  tree.js 哦 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri=&quo

Pig的安装和简单实用

1.Pig是基于hadoop的一个数据处理的框架. MapReduce是使用java进行开发的,Pig有一套自己的数据处理语言,Pig的数据处理过程要转化为MR来运行.2.Pig的数据处理语言是数据流方式的,类似于初中做的数学题.3.Pig基本数据类型:int.long.float.double.chararray.bytearray 复合数据类型:Map.Tuple.Bag Bag的类型如{('age',31),('name','张三')} 4.如何安装Pig4.1 把pig-0.11.1.t

redis安装与简单实用

1.在Linux上redis的安装时十分简单的: 第一步:wget http://download.redis.io/releases/redis-2.8.12.tar.gz 解压: tar zxvf redis-2.8.12.tar.gz 进入目录:  cd redis-2.8.12 编译:make 启动并运行redis:src/redis-server 新开一个终端:cd redis-2.8.12 运行redis客户端: src/redis-cli [[email protected] re

Sublime Text3 安装和简单实用

在前端开发的过程中,编辑器是必须要用的,前辈们留给我们很多优秀的编辑器.notepad++.editPlus.Emacs不太适合前端开发,vim学习成本太高,brackets我并不觉得好用,还有微软最近出的Visual Studio Code.就前端范围来讲webstrom绝对是个利器,但毕竟软件太大,很多电脑跑起来有点费力,另一个就是大名鼎鼎Sublime Text,现在已经升级到第三版了.我就记下来吧! Mooc有一个很好的Sublime编辑器使用教程,有时间可以看看!地址:http://w

Linux系统ELK的安装和简单实用(一)

本博客安装的ELK版本为目前最新版本6.3.0,由于elasticsearch是基于java开发的,所以对JDK的版本有要求,在5.0以后的版本中,要求JDK版本不低于1.8的才可正常实用. 同时,elasticsearch,logstash,kibana三个版本最好一致,否则会产生因版本冲突发生的错误. 下面开始安装步骤: 1.elasticsearch的安装 下载elasticsearch的官方版本:https://www.elastic.co/cn/downloads/elasticsea

Linux下Libevent安装和简单实用

前言 Libevent 是一个用C语言编写的.轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易读:跨平台,支持 Windows. Linux. *BSD 和 Mac Os:支持多种 I/O 多路复用技术, epoll. poll. dev/poll. select 和 kqueue 等:支持 I/O,定时器和信号等事件:注册事件优先级.Libevent 已经被广泛的应用,作为底层的

Java Web开发SpringMVC和MyBatis框架开发环境搭建和简单实用

1.下载SpringMVC框架架包,下载地址: 点击下载 点击打开地址如图所示,点击下载即可 然后把相关的jar复制到lib下导入 2.MyBatis(3.4.2)下载 点击下载 MyBatis中文文档地址 点击查看 下载解压之后把jar复制到lib下导入,大概是这样子的 3.jdbc连接库还没有下载...这个是5.1.41版本的... 点击下载 解压之后这样子... 4.fastjson 阿里巴巴的json解析库 点击下载 版本是1.2.24 这个是托管到了github上面的,地址是:点击进入

QT5安装及简单实用-01

下载地址:http://qt-project.org/downloads 版本选择: 刚开始我安装的是第一版本,运行一个程序 报错!说编译器没配置,可能需要安装msvc2012 才好使吧,我没试过. 最后,我安装的是mingw版本,并且 有安装了mingw(首先安装),这样就OK 我的系统 win7 64

安装并简单实用salt-ssh

1.安装epel源.salt-ssh yum -y install epel-release yum -y install salt-ssh 2.编辑添加主机的登录凭证 vim /etc/salt/roster salt2: host: 10.0.0.106 user: root passwd: centos salt3: host: 10.0.0.107 user: root passwd: centos 采用salt-ssh协议执行命令(没有minion) salt-ssh '*' -r '