一、前言
针对安卓APP,部署一套通用的, 便携式,可稳定运行Frida的环境,进而实现移动场景下的抓包,hook,拓展测试和利用场景。
解决如下问题:
真机识别:
-
windows下模拟器为x86架构,编译选项为arm的APP无法安装
-
APP内置模拟器检查逻辑
便携式:
-
Frida hook需要手机连接PC,在PC上使用Frida tools,如何脱离PC或者外部依赖,仅需一部手机即实现tools和server两个角色
二、突破真机识别
针对这个问题,最开始是去尝试了各种模拟器,但发现要去解决编译兼容和APP内置真机校验的对抗成本和技术要求非常高。有舍有得,最终决定投入300块钱的巨资,购置真机搭建测试环境
2.1 需要什么样的手机设备?
-
性能高,各种主流软件的运行不在话下
-
对于各种开源ROM很好的兼容
-
经济实惠, 作为实验探索就算弄坏了也不心疼
2.2 什么样的手机系统?
-
可以Root
-
可以执行Shell, 且软件支持性好,拓展性强,例如运行python
-
android版本高(安装andorid 6及以上版本的APP),适配兼容软件性强
-
良好的社区支持与持续的更新
2.3 选型
-
手机选择为OnePlus3
-
操作系统为Kali NetHunter (实践过Ubuntu Touch,但安装踩入深坑且未能解决). 访问:https://www.kali.org/get-kali/ , 选择"Mobile" 栏目,可以看到Kali NetHunter,在下面有其支持的手机类型,可见是支持Oneplus3的:
2.4 刷机
2.4.1 文件清单
Ubuntu:
-
platform-tools_r30.0.5-linux.zip
-
twrp-3.5.0_9-0-oneplus3.img
OnePlus3:
-
nethunter-2020.4-oneplus3-any-pie-kalifs-full-3.zip
-
MagiskManager-v8.0.7.apk
-
Magisk-v21.4.zip
-
Disable_Dm-Verity_ForceEncrypt_11.02.2020.zip
下载地址:
adb,fastboot:https://developer.android.google.cn/studio/releases/platform-tools.html
Magisk:https://github.com/topjohnwu/Magisk/releases
DisableDm-VeritForceEncrypt:https://zackptg5.com/android.php#disverfe
nethunter: https://www.offensive-security.com/kali-linux-nethunter-download/
TWRP: https://twrp.me/oneplus/oneplusthree.html
氧OS:https://www.oneplus.com/cn/support/softwareupgrade/details?code=PM1574151108358
2.4.2 二次打包nethunter ROM
在安装过程中遇到提示挂载失败等问题,我们需要参考FAQ-4 对nethunter-2020.4-oneplus3-any-pie-kalifs-full-3.zip进行修复后二次打包
2.4.3 部署Ubuntu 18 虚拟机
为什么要部署Ubuntu?参见FAQ-1
将platform-tools_r30.0.5-linux.zip拷贝进去并解压, 拷贝twrp-3.5.0_9-0-oneplus3.img到解压目录
2.4.3 OnePlus3基础设置
Oneplus3下,启动后,做如下动作:
-
设置密码 [重要!!] 后面的TWRP,解密会用到
-
依次: 设置-多次点击版本号-开启开发者-开启USB调试-开启OEM解锁
2.4.4 刷TWRP
cd /home/XXXX/Desktop/platform-tools_r30.0.5-linux/platform-tools
# 这儿我们关闭以下usb调试,然后再开启,触发信任弹框,点击信任
./adb reboot bootloader
./fastboot oem unlock
# 我们通过音量键选择到YES,确定,会自动重启一下
# 我们在新界面,称之为A,要选择到fastboot,否则5秒后会自动启动
./fastboot flashing unlock
./fastboot getvar unlocked
# 因为在虚拟机下,我们拔插一下USB,然后弹框选择到虚拟机
./fastboot devices
./fastboot flash recovery twrp-3.5.0_9-0-oneplus3.img
./fastboot boot twrp-3.5.0_9-0-oneplus3.img
启动之后,这儿我们输入上面设置的密码即可增加可写属性,但是第一次很笨,没有设置密码,若没有设置密码可以参考FAQ-2进行解决
2.4.5 推送文件到Oneplus3
./adb devices
选择Wipe,选择12345项进行清空(先清空,后adb推送,否则推进去被清了)
# ./adb shell , df -h 看到/sdcard/ 目录下空间是够的
# 前往不要拷贝到mnt目录, 否则后面安装挂载步骤会失败
./adb push nethunter-2020.4-oneplus3-any-pie-kalifs-full-3.zip /sdcard/
./adb push Magisk-v21.4.zip /sdcard/
./adb push Disable_Dm-Verity_ForceEncrypt_11.02.2020.zip /sdcard/
./adb push OnePlus3Hydrogen_16_OTA_083_all_2002201900_abc8cfd625b5477b.zip /sdcard/
# 这儿推送文件中途失败的问题参见FAQ-3
2.4.6 安装Magisk和Disable_Dm-Verity_ForceEncrypt
这儿我们需要先刷稍高版本的Oneplus系统
选择Install,
选择OnePlus3Hydrogen_16_OTA_083_all_2002201900_abc8cfd625b5477b.zip安装
选择Magisk-v21.4.zip,Disable_Dm-Verity_ForceEncrypt_11.02.2020.zip安装
Reboot System, 第一次启动比较慢,中间可能重启,直到进入到系统
2.4.7 安装nethunter
再重启进入到recovery下的TWRP:
选择/mnt/nethunter-2020.4-oneplus3-any-pie-kalifs-full-3.zip安装
Reboot System
2.4.8 root
到https://github.com/topjohnwu/Magisk/releases下载apk
adb push .MagiskManager-v8.0.7.apk /sdcard/
然后安装
确认root成功:
adb shell
> su root # 然后在手机界面上授权root即可(同理其他的kali也是在手机界面进行授权即可)
2.5 FAQ
FAQ-1: 为什么选择Ubuntu作为宿主机进行刷机?
原因: 在Win下,使用Fastboot模式,会出现驱动不兼容不识别等问题!
FAQ-2: 未设置手机密码,但进入TWRP要求输入密码?
点击Wipe,右滑Swipe to Factory Reset, 重启进入TWRP
FAQ-3: adb push 推送文件中途失败?
TWRP选择Reboot,选择bootloader进入fastboot
拔掉USB,重启虚拟机,然后重新连接进入TWRP,这种情况主要是虚拟机的USB连接问题
FAQ-4: nethunter安装过程提示挂载失败?
提示如下报错:
TWRP '/system' not in /proc/mounts , '/system' not in /proc/mounts
错误分析:
在TWRP下的Mount把system挂载上去了,但是安装的时候又会自动被卸载,导致报错,
分析了下在挂载状态下,多了:
/dev/block/sde20 /system ext4 rw,seclabel,relatime,data=ordered 0 0
遂用语句:
/sbin/mount -o rw,seclabel,relatime,data=ordered /system
解决方案:修改对应文件,并二次打包nethunter
可以挂载上,那么到底是哪里在做挂载操作呢? 我们把nethunter-2020.4-oneplus3-any-pie-kalifs-full解包,然后在:META-INFcomgoogleandroidupdate-binary 找到了对应的代码:167行:mount /system ,我们改成:
/sbin/mount -o rw,seclabel,relatime,data=ordered /system
然后重新打包成为: nethunter-2020.4-oneplus3-any-pie-kalifs-full-3.zip
2.6 参考资料
OnePlus3安装Nethunter:
https://www.zerozd.xyz/Android/67.html
https://blog.csdn.net/Kevinhanser/article/details/80025260
三、实现便携式Frida
通常,我们Frida-tools是部署在PC上实现对接server,但例如我们可能正在公交上,还未到公司而需要考勤打卡等场景,不可能抱着一台PC然后插USB进行hook打卡,所以我们需要将Frida的tools和服务端都安装到手机上,这样我们可以随时随地的进行hook
3.1 Oneplus上Frida server和tools部署
3.1.1 安装NetHunterStore和termux
NetHunterStore不需要单独安装,名字叫做F-Droid,默认自带
若是不自带可以在该地址下载:https://store.nethunter.com/NetHunterStore.apk
termux下载地址:
https://store.nethunter.com/packages/com.termux/
执行:
adb push com.termux_95.apk /sdcard/
然后在手机上安装该apk
3.1.2 安装基础软件
安装好termux后, 点击图标进入shell
pkg install tsu -y
pkg install busybox -y
pkg install openssh -y
pkg install termux-services -y
3.1.3 进入termux
方案一:ADB 进入 termux
su root
/data/data/com.termux/files/usr/bin/tsu
存在的问题: 进来默认是root权限,pkg实际是apt,导致apt无法使用,而且无法切换账户
方案二: ssh进入termux
手机打开termux APP,在其下创建ssh用户:
sv up sshd
whoami -> u0_a125
passwd u0_a125
>> XXXXXXXXXX
PC主机下:
su root
/data/data/com.termux/files/usr/bin/tsu
ssh u0_a125@0.0.0.0 -p 8022
即可SSH进入到termux
注意: ssh连接termux时,若关闭termux APP,ssh则自动断开
3.1.4 配置pip源
https://mirrors.tuna.tsinghua.edu.cn/help/termux/
sed -i 's@^(deb.*stable main)$@#1ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^(deb.*games stable)$@#1ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^(deb.*science stable)$@#1ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
sed -i 's@^(deb.*science stable)$@#1ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/root.list
apt update && apt upgrade -y
3.1.5 安装vim和关闭SELinux
pkg install vim -y
setenforce 0
pkg install python -y # python --version Python 3.9.1
pkg install nodejs -y
# tools
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install frida-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
# https://github.com/termux/termux-root-packages/issues/179
apt install root-repo -y
3.1.6 安装python和nodejs
方案1:egg法
缺点:版本不匹配和依赖缺失失败
方案2: apt安装
apt-cache madison python
pkg install python -y # python --version Python 3.9.1
pkg install nodejs -y
# tools
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install frida-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
# https://github.com/termux/termux-root-packages/issues/179
apt install root-repo -y
3.1.7 安装frida server和tools
方案1: apt直装
apt install frida-python -y
缺点: 太卡了,下载特别慢
方案2:npm安装
npm --registry http://registry.npm.taobao.org install frida
缺点: 安装失败,未找到匹配版本
方案3: 分离式安装
adb下:
# https://dl.bintray.com/grimler/termux-root-packages-24/aarch64/
# 下载frida-python_12.11.18_aarch64.deb
# 下载frida-server_12.11.18_aarch64.deb
adb push frida-python_12.11.18_aarch64.deb /sdcard/
adb push frida-server_12.11.18_aarch64.deb /sdcard/
adb shell
su root
mv /sdcard/frida-server_12.11.18_aarch64.deb /data/data/com.termux/
mv /sdcard/frida-python_12.11.18_aarch64.deb /data/data/com.termux/
chmod 666 /data/data/com.termux/frida-server_12.11.18_aarch64.deb
chmod 666 /data/data/com.termux/frida-python_12.11.18_aarch64.deb
Termux shell下:
# 注意顺序
dpkg -i frida-server_12.11.18_aarch64.deb
dpkg -i frida-python_12.11.18_aarch64.deb # 会提示缺少pip3.8,不用理会
3.1.8 验证frida可用
在termux外部启动Server:
# which frida-server
# /data/data/com.termux/files/usr/bin/frida-server
adb shell
su root
/data/data/com.termux/files/usr/bin/frida-server
在termux内部启动frida或frida-ps:
frida-ps
此时可以看到列出了运行的APP,即可
方便使用frida-server:
echo '/data/data/com.termux/files/usr/bin/frida-server' > /sdcard/sfs
bash /sdcard/sfs --> 即可启动
3.2 编写Frida hook脚本
这个主要基于:
-
经验脚本的收集整理
-
不断学习
这个可以在后续的AOH系列展开分享
3.3 参考资料
ssh进入termux:
https://android.stackexchange.com/questions/225260/termux-running-termux-via-adb-without-any-direct-interaction-with-the-device
四、任意定位打卡实践
有了前面的基础环境,接下来可以开始携带我们这台改造后的Oneplus在任意地点运行frida脚本。接下来演示,在某办公软件下,实现任意定位和打卡。
由于某些原因,该APP相关信息暂做隐藏。
4.1 逆向与调试APP,编写hook脚本实现任意定位
-
安装软件
-
Oneplus连接PC,PC启动frida-tools, 设备启动frida-server
-
jadx导入APP找到关键函数,通过导入的依赖很明显的可以看到高德定位的sdk,直接上高德通用hook脚本
-
调试脚本,确认可以任意定位
每次脚本运行只随机一次,否则一直跳
0.00001 约为 1.1米, 设定在100米内的偏差,即为 A +/- 0.00100m, 即我们设定随机数0-100,那么当为100时,为0.001 , 随机数x , x/100000
hook_location.js:
if(Java.available){
const good_la = 22.547377;
const good_lo = 113.947154;
const add_delete_1 = Math.floor(Math.random() * 2);
const change_1 = (Math.floor(Math.random() * 101)) /100000;
const add_delete_2 = Math.floor(Math.random() * 2);
const change_2 = (Math.floor(Math.random() * 101)) /100000;
Java.perform(function(){
console.log("i go main");
var hook = Java.use("com.amap.api.location.AMapLocation");
hook.getLatitude.implementation = function () {
console.log("getLatitude: " +this.getLatitude());
var new_la = 0;
if (add_delete_2 ==1){
new_la = good_la+change_2;
}else{
new_la = good_la-change_2;
}
new_la = parseFloat(new_la.toFixed(6));
console.log("new getLatitude: " +new_la);
return new_la;
};
hook.getLongitude.implementation = function () {
console.log("getLongitude: " +this.getLongitude());
var new_lo = 0;
if (add_delete_1==1){
new_lo = good_lo+change_1;
}else{
new_lo = good_lo-change_1;
}
new_lo = parseFloat(new_lo.toFixed(6));
console.log("new getLongitude: " +new_lo);
return new_lo;
};
});
} -
迁移脚本到Oneplus环境
adb push hook_location.js /sdcard/
adb shell
su root
mv /sdcard/hook_location.js /data/data/com.termux/files/home
cd /data/data/com.termux/files/home/
chmod 666 home/hook_location.js
echo 'frida -H 127.0.0.1 com.XXXXX.XX -l hook_location.js --no-pause' > lpXXXX.sh
4.2 演示视频
目标:定位到腾讯总部 - 深圳市南山区高新科技园
-
运行考勤软件
-
Oneplus3, 打开nethunter , 选择android , su 输入bash /sdcard/sfs (启动firda-server)
-
打开termux, 输入 bash lpXXXX.sh
-
观察脚本输出是否hook进去,尝试定位当前位置是否成功定位到腾讯总部
-
打卡(此处略)
Video:
原文始发于微信公众号(Art Of Hunting):[AOH 004]便携式Frida环境-附某考勤任意定位