iOS逆向之一-工具的安装和使用

最近在学习iOS安全方面的技术,有些东西就记录下来了,所有有了这篇文章。顺便也上传了DEMO,可以再这里找到这些DEMO的源码:dhar/iOSReProject

越狱手机安装软件

  • ssh 远程连接到越狱手机需要的软件

    在Cydia中搜索、安装OpenSSH软件。

    ssh 连接到手机:ssh root@192.168.1.112[手机IP]
  • apt-get 包管理软件

    cydia搜索:APT 0.6 Transitional 安装
    使用apt-get 之前要先update,这一步很重要,否则会一直报错找不到你要安装的包
apt-get update
复制代码

apt-get 命令使用

参考链接:bbs.feng.com/read-htm-ti…

apt-get update   【刷新所有的源,相当于获取最新的货品清单,刷新速度比cydia快而稳定,各位自己体验】
apt-get upgrade   【更新所有已安装包,到最新版本】
apt-get install 程序名  【安装该软件,如有依赖包,一并下载,安装前需要你输入y确认】
apt-get remove 程序名  【删除该软件包,不删除依赖包,不删除配置文件,可能比较适合重装软件用】
apt-get purge 程序名     【删除该软件包,不删除依赖包,删除配置文件,属于比较彻底的删除】
apt-get autoremove 【在remove或purge掉某个包之后,将那些不需要的孤魂野鬼(依赖包)彻底赶走,这个要比cydia给力吧】
apt-cache search 字段 【搜索含有该字段的软件包】
apt-cache show 程序名 【详细显示该程序的信息】
apt-get clean  【清除apt-get下载的安装包缓存,可以节省一点储存空间】
复制代码

apt-get安装常用的工具软件

// ping 命令测试网络连通
apt-get install ping
// ps 查看进程:eg. ps -e ps aux
apt-get install ps
// 查找文件 eg. find ./ -name .app
apt-get install find
// tcpdump 抓包
apt-get install tcpdump
apt-get install top
// vim编辑器
apt-get install vim
// 使用ifconfig需要安装这个
apt-get install network-cmds //-arp, ifconfig, netstat, route, traceroute
复制代码

Cycript工具介绍

Cycript是一款脚本语言,可以看作是Objective-JavaScript,它可以帮助我们轻松测试和验证函数效果。

使用apt-get安装

apt-get install cycript
复制代码

找到一个需要注入的APP
使用 ps 命令查找运行的app

iPhone:/User/Library/SMS root# ps -e | grep .app
378 ?? 0:02.72 /Applications/MobileMail.app/MobileMail
610 ?? 1:21.74 /Applications/Cydia.app/Cydia
848 ?? 0:12.78 /Applications/Preferences.app/Preferences
918 ?? 0:15.53 /var/mobile/Containers/Bundle/Application/0E6787B6-B579-41A7-AA54-6E80B6358047/News.app/News
947 ttys000 0:00.01 grep .app
复制代码

选择今日头条APP进行注入,对应的PID是918

// cycript -p [进程名称|进程ID]
iPhone:/User/Library/SMS root# cycript -p 918
// 或者
iPhone:/User/Library/SMS root# cycript -p News
复制代码

进入cycript模式,可以执行OC的代码

// 隐藏系统状态栏
cy# [[UIApplication sharedApplication] setStatusBarHidden:YES]
// 显示系统状态栏
cy# [[UIApplication sharedApplication] setStatusBarHidden:NO]
复制代码

退出cycript使用 ctrl+D

注入SpringBoard的自行截屏的例子

iPhone:/User/Library/SMS root# cycript -p SpringBoard
cy# [[SBScreenShotter sharedInstance] saveScreenshot:YES]
cy#
复制代码

查看RootViewController的例子

cy# [[[[UIApplication sharedApplication] delegate] window] rootViewController]
#"<MMTabBarController: 0x1358d5a60>"
复制代码

查看APP的沙盒路径

iPhone:/var/mobile/Containers/Data/Application/B835F1A8-8A10-487F-9BF3-8D1102C47336/Documents root# cycript -p News
cy# NSHomeDirectory()
@"/var/mobile/Containers/Data/Application/B835F1A8-8A10-487F-9BF3-8D1102C47336"
cy#
复制代码

逆向工具

检测工具
  • Reveal 检测界面布局
  • tcpdump 检测网络,抓取数据包
调试工具
  • lldb xcode的调试工具
  • cycript 越狱的调试工具
反编译工具
  • IDA、Hopper,Disassembler,classdump

    Classdump: 可以将Mach-O文件中的Objective-C运行时的声明的信息导出,即编写OC代码时的 .h文件。class-dump只能导出未经加密的App的头文件。classdump是对"otool -ov" 信息的翻译,以一种我们熟悉的易读的方式呈现。官网http://stevenygard.com/projects/class-dump/

    使用class-dump反编译出SpringBoard的头文件,保存到/tmp/SpringBoardHeader文件夹下,(/opt/class-dump/class-dump 是我存放class-dump的地方)
➜  $ /opt/class-dump/class-dump -H /tmp/SpringBoard.app/SpringBoard -o /tmp/SpringBoardHeader
// 注意:当砸壳完毕后,使用 class-dump 仍然只导出 CDStructures.h 一个文件,则可能架构选择错误;因为 dumpdecrypted 只会砸你手机处理器对应的那个壳,fat binary 的其它部分仍然是有壳的,而 class-dump 的默认目标又不是被砸壳的那个部分,因此很有可能就会报错;需要指定架构 --arch armv7
➜ /tmp /opt/class-dump/class-dump --arch armv7 -H ./News.crypted -o /tmp/NewsHeader
复制代码

otool(object file displaying tool) :目标文件的展示工具。可以用来发现应用中使用到了哪些系统库,调用了其中哪些方法,使用了库中哪些对象及属性,它是Xcode自带的常用工具。

开发工具

XCode、theos 、ldid、dpkg

theos的安装

wiki: github.com/theos/theos…

  • 安装到 /opt 目录下
➜  /opt git clone --recursive https://github.com/theos/theos.git
复制代码
  • 修改权限
    sudo chown -R (id -g) theos

  • 配置环境变量
    临时修改环境变量

export THEOS=/opt/theos
复制代码

永久修改

    可以写入~/.bash_profile
source ~/.bash_profile
复制代码

查看环境变量值是否设置成功

➜  ~ echo $THEOS
/opt/theos
复制代码

ldid 安装

  • 使用brew安装
brew install ldid fakeroot
复制代码

查看entitlement内容

➜  News.app ldid -e News
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>application-identifier</key>
<string>U9JEY66N6A.com.ss.iphone.article.News</string>
<key>aps-environment</key>
<string>production</string>
<key>beta-reports-active</key>
<true/>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:toutiao.com</string>
<string>applinks:www.toutiao.com</string>
<string>applinks:m.toutiao.com</string>
<string>applinks:open.toutiao.com</string>
<string>applinks:d.toutiao.com</string>
</array>
<key>com.apple.developer.team-identifier</key>
<string>U9JEY66N6A</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.todayExtenstionShareDefaults</string>
</array>
<key>get-task-allow</key>
<false/>
<key>keychain-access-groups</key>
<array>
<string>U9JEY66N6A.com.bytedance.keychainshare</string>
</array>
</dict>
</plist>
复制代码

dpkg 安装使用

dpkg 是Debian package的简写,为”Debian“ 操作系统 专门开发的套件管理系统,用于软件的安装,更新和移除。

  • 使用brew安装dpkg
brew install --from-bottle https://raw.githubusercontent.com/Homebrew/homebrew-core/7a4dabfc1a2acd9f01a1670fde4f0094c4fb6ffa/Formula/dpkg.rb
brew pin dpkg
复制代码
  • dpkg的使用
dpkg -i/-r  deb包安装/卸载
dpkg -s com.iosre.myiosreproject 查看安装包信息
复制代码
砸壳工具的使用

dumpdecrypted 砸壳工具

➜  dumpdecrypted git:(master) sudo make
`xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/Frameworks -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/PrivateFrameworks -arch armv7 -arch armv7s -arch arm64 -c -o dumpdecrypted.o dumpdecrypted.c
`xcrun --sdk iphoneos --find gcc` -Os -Wimplicit -isysroot `xcrun --sdk iphoneos --show-sdk-path` -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/Frameworks -F`xcrun --sdk iphoneos --show-sdk-path`/System/Library/PrivateFrameworks -arch armv7 -arch armv7s -arch arm64 -dynamiclib -o dumpdecrypted.dylib dumpdecrypted.o
复制代码

编译在当前目录下生成了一个 dumpdecrypted.dylib 文件。

  • 获取需要注入的APP的HOME目录
iPhone:~ root# ps -e | grep .app
12082 ?? 0:17.83 /var/mobile/Containers/Bundle/Application/17803F58-6C43-4FF6-8ED4-55FAC9587C32/News.app/News
12111 ?? 6:09.93 /var/mobile/Containers/Bundle/Application/29D93EE3-38D0-462C-AEE5-51079D0A50D4/mmosite.app/mmosite
12156 ttys000 0:00.01 grep .app
iPhone:~ root# cycript -p News
cy# [[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]
#"file:///var/mobile/Containers/Data/Application/B835F1A8-8A10-487F-9BF3-8D1102C47336/Documents/"
cy#
复制代码
  • 拷贝上一步生成的dumpdecrypted.dylib 文件到Document目录
dumpdecrypted git:(master) ✗ scp ./dumpdecrypted.dylib root@192.168.3.41:/var/mobile/Containers/Data/Application/2D36F847-E808-4547-ADC5-0B1BD9F3BC6A/Documents
dumpdecrypted.dylib 100% 193KB 1.0MB/s 00:00
复制代码
  • 执行砸壳
iPhone:/var/mobile/Containers/Data/Application/2D36F847-E808-4547-ADC5-0B1BD9F3BC6A/Documents root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Containers/Bundle/Application/0E6787B6-B579-41A7-AA54-6E80B6358047/News.app/News
mach-o decryption dumper
DISCLAIMER: This tool is only meant for security research purposes, not for application crackers.
[+] detected 32bit ARM binary in memory.
[+] offset to cryptid found: @0x4a4c(from 0x4000) = a4c
[+] Found encrypted data at address 00004000 of length 30556160 bytes - type 1.
[+] Opening /private/var/mobile/Containers/Bundle/Application/0E6787B6-B579-41A7-AA54-6E80B6358047/News.app/News for reading.
[+] Reading header
[+] Detecting header type
[+] Executable is a FAT image - searching for right architecture
[+] Correct arch is at offset 16384 in the file
[+] Opening News.decrypted for writing.
[+] Copying the not encrypted start of the file
[+] Dumping the decrypted data into the file
[+] Copying the not encrypted remainder of the file
[+] Setting the LC_ENCRYPTION_INFO->cryptid to 0 at offset 4a4c
[+] Closing original file
[+] Closing dump file
复制代码

在Document目录下生成了一个新的文件

News.decrypted
复制代码

这个就是砸壳之后的文件。

砸壳之后可以使用otool查看二进制文件是否加密

➜  News.app otool -l News | grep crypt
cryptoff 16384
cryptsize 32096256
cryptid 0
cryptoff 16384
cryptsize 36356096
cryptid 0
复制代码

cryptid 0 表示文件是未加密的,砸壳成功

转载于:https://juejin.im/post/5ca5f4cae51d45379e715099

最新文章

  1. 获取Canvas当前坐标系矩阵
  2. C语言基础(10)-数组
  3. webpack ,gulp/grunt的介绍
  4. Node.js与Sails~Model和ORM的持久化
  5. jQuery的插入
  6. poj2104
  7. 模拟DOMContentLoaded事件
  8. DedeCMS官方手册
  9. linux之SQL语句简明教程---CREATE TABLE
  10. swig include使用方法
  11. 总结两种动态代理jdk代理和cglib代理
  12. #Java学习之路——基础阶段二(第四篇)
  13. Windows编程之模块遍历(C++实现)
  14. .net Forms身份验证不能用在应用的分布式部署中吗?
  15. 深入理解ASP.NET MVC(7)
  16. JQuery的页面操作
  17. 栈的实现与操作(C语言实现)
  18. GGGGCCCC
  19. PHP webservice初探
  20. 再战CS231-快速排序

热门文章

  1. JavaScript基本数据类型及其转换规则
  2. Linux - Ubuntu下执行apt-get update报错:Some index files failed to download. They have been ignored, or old ones used instead.
  3. 中阶 d03.5 (正篇)完整的Dao 操作数据库
  4. tf.train.GradientDescentOptimizer 优化器
  5. nmon 的下一代工具 njmon
  6. 使用snapjs实现svg路径描边动画
  7. python 3 的解释器
  8. 爬取腾讯网的热点新闻文章 并进行词频统计(Python爬虫+词频统计)
  9. windows编译动态链接库,dll+lib的形式
  10. C# WCF的通信模式