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

来源:互联网 发布:南京天梯培训中心 知乎 编辑:程序博客网 时间:2024/05/22 05:06

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 命令使用
参考链接:http://bbs.feng.com/read-htm-tid-2328801.html

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 auxapt-get install  ps// 查找文件 eg. find ./ -name .appapt-get install  find// tcpdump 抓包apt-get install tcpdumpapt-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 SpringBoardcy# [[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 Newscy# 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: https://github.com/theos/theos/wiki/Installation

  • 安装到 /opt 目录下
➜  /opt git clone --recursive https://github.com/theos/theos.git 
  • 修改权限sudo chown -R $(id -u):$(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 砸壳工具

  • 下载和编译
    下载最新源码: git clone https://github.com/stefanesser/dumpdecrypted.git
    编译动态库文件(dumpdecrypted.dylib): make
➜  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 .app12082 ??         0:17.83 /var/mobile/Containers/Bundle/Application/17803F58-6C43-4FF6-8ED4-55FAC9587C32/News.app/News12111 ??         6:09.93 /var/mobile/Containers/Bundle/Application/29D93EE3-38D0-462C-AEE5-51079D0A50D4/mmosite.app/mmosite12156 ttys000    0:00.01 grep .appiPhone:~ root# cycript -p Newscy# [[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/Documentsdumpdecrypted.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/Newsmach-o decryption dumperDISCLAIMER: 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 表示文件是未加密的,砸壳成功

原创粉丝点击