class-dump 和 iOSOpenDev 的使用

class-dump 官网地址:这里

我这里下载的是 class-dump-3.5.dmg 版本号的。

双击.dmg 文件,将  拉倒 /usr / local / bin 文件夹下,这样就能够在终端使用 class-dump 命令了。

这里我演示dump系统自带的计算器,导出它的头文件。

命令例如以下:

class-dump -H /Applications/Calculator.app -o /Users/Rio/Desktop/calculate heads

解释:

/Applications/Calculator.app 是计算器app的路径,注意,这里dump的是应用后缀是.app而不是.ipa,不要傻傻的去dump .ipa后缀了。会提示“

class-dump: Input file (xxx.ipa) is neither a Mach-O file nor a fat archive.”错误的!

/Users/Rio/Desktop/calculate heads 是存放dump出来头文件的文件夹路径

class-dump 3.5 (64 bit)
Usage: class-dump [options] <mach-o-file>

  where options are:
        -a             show instance variable offsets
        -A             show implementation addresses
        --arch <arch>  choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64)
        -C <regex>     only display classes matching regular expression
        -f <str>       find string in method name
        -H             generate header files in current directory, or directory specified with -o
        -I             sort classes, categories, and protocols by inheritance (overrides -s)
        -o <dir>       output directory used for -H
        -r             recursively expand frameworks and fixed VM shared libraries
        -s             sort classes and categories by name
        -S             sort methods by name
        -t             suppress header in output, for testing
        --list-arches  list the arches in the file, then exit
        --sdk-ios      specify iOS SDK version (will look in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS<version>.sdk
        --sdk-mac      specify Mac OS X version (will look in /Developer/SDKs/MacOSX<version>.sdk
        --sdk-root     specify the full SDK root path (or use --sdk-ios/--sdk-mac for a shortcut)

执行命令。能够看到已经dump出头文件了,例如以下所看到的:


   class-dump 尽管非常实用,但有时我们会发现 class-dump 执行失败,无法得到我们想要的 .h 文件,或者 .h 文件的内容是加密的密文。

出现这种现象的原因是:class-dump 额作用对象必须是未经加密的可执行文件,而App Store 下载的 App 都是经过签名加密的。可执行文件被加上了一层“壳”。能够使用 AppCrackr 来自己主动砸壳。


---------- 2016.01.07 更新 ----------------

从App Store 上下载的 App 我这里是通过 Clutch 这个来砸壳的。

先 ssh 到 iPhone 上。可能会遇到这种问题:

MacBook:~ aaron$ ssh root@172.17.24.70

ssh: connect to host 172.17.24.70 port 22: Connection refused

不急,去 Cydia 安装 OpenSSH 就可以,安装完后再次 ssh 到 iPhone 发现已经能够了。

去网上下载 Clutch 这个软件,改名为 clutch (不改也能够。这里改名仅仅是为了以后敲命令的时候老是要第一个字母大写,麻烦!

),然后 copy 到 iPhone 的 /usr/bin/ 文件夹以下。

MacBook:~ aaron$ scp /Users/aaron/Documents/kugou/doc/越狱开发/hack/clutch root@172.17.24.70:/usr/bin/

root@172.17.24.70's password: 

clutch                                        100%  915KB 914.8KB/s   00:00 

呐。已经 ok 了,接下来改动 clutch 为最高权限 777

iPhone:/usr/bin root# chmod 777 ./clutch

查看是否改动成功。

iPhone:/usr/bin root# ls -al | grep clutch

-rwxrwxrwx 1 root   wheel  936752 Jan  7 16:02 clutch

能够看到已经是最高权限了。

接下来是使用 clutch 了,网上说还要安装 Mobile Terminal 啥啥啥的一律不用管它,直接开干。

iPhone:/usr/bin root# clutch -i 

输入上面的命令就能够看到一大堆能够砸壳的应用:

Installed apps:

1) <AlipayWallet bundleID: com.alipay.iphoneclient>


2) <WeChat bundleID: com.tencent.xin>


3) <yidian bundleID: com.yidian.zixun>


4) <YoukuiPhone bundleID: com.youku.YouKu>


5) <嘀嘀打车 bundleID: com.xiaojukeji.didi>


6) <Taobao4iPhone bundleID: com.taobao.taobao4iphone>


7) <NewsBoard bundleID: com.netease.news>


8) <iLady bundleID: cn.com.modernmedia.imodernlady>


9) <netdisk_iPhone bundleID: com.baidu.netdisk>

..........


输入你想要砸壳的应用 bundle identifier 进行砸壳

iPhone:/usr/bin root# clutch -d com.yidian.zixun

成功后你就能够看到已经破解完后的路径:

DONE: /private/var/mobile/Documents/Dumped/com.yidian.zixun-iOS7.0-(Clutch-2.0 RC2).ipa

之后。copy .ipa 文件到你的电脑上,

MacBook:~ aaron$ scp root:172.17.24.70:/private/var/mobile/Documents/Dumped/com.yidian.zixun-iOS7.0-(Clutch-2.0 RC2).ipa ~/Desktop/

-bash: syntax error near unexpected token `('

诶,有错误,识别不了`(',那么改一下.ipa 名字后在 scp 吧

iPhone:/private/var/mobile/Documents/Dumped root# mv com.yidian.zixun-iOS7.0-(Clutch-2.0 RC2).ipa yidian.ipa

再 copy 到电脑桌面:

MacBook:~ aaron$ scp root@172.17.24.70:/private/var/mobile/Documents/Dumped/yidian.ipa ~/Desktop/yidian.ipa

root@172.17.24.70's password: 

yidian.ipa                                    100%   25MB   1.5MB/s   00:16

至此,砸壳就已经完毕了,如今能够用 class-dump 把头文件 dump 出来了。cheer!


-------------------------------- 切割线 -----------------------------------

安装 apt-get :

打开cydia –管理设置选择开发人员”—完毕搜索apt。安装APT 0.6 Transitional,它会安装四五个其他依赖包,都不大.

安装后就能用apt-get了。比如apt-get install netstat, apt-get install ps等。




-------------------------------- 切割线 -----------------------------------


在新建project之前另一个非常重要的事情要做。安装打印日志用的 log。

# 看log
apt-get install socat

安装完后,输入以下命令看log

socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock
>watch


安装 socat 的时候出现这个错误提示的话:

E: Could not get lock /var/lib/dpkg/lock - open (35: Resource temporarily unavailable) 

E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

能够尝试用这里的办法解决。

(先试一下这个帖子提到的其他办法,最后不得已再使用例如以下办法)



为了在使用 iosOpenDev 开发时调试更加便利,机子还须要安装这样一些环境:

# 这个是使用iosOpenDev开发时,xcode把deb包安装到手机时。须要手机先具备的环境条件
apt-get install coreutils diskdev-cmds file-cmds system-cmds com.saurik.substrate.safemode mobilesubstrate preferenceloader


若出现“Package diskdev-cmds is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source”这种安装错误的时候。执行例如以下命令后再次安装过,
sudo apt-get update && && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get install packagename
(or)
apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y && apt-get install packagename
參考来源:http://www.blackmoreops.com/2014/12/13/fixing-error-package-packagename-not-available-referred-another-package-may-mean-package-missing-obsoleted-available-another-source-e-pa/


有些机子环境,安装不了netstat、socat等环境,于是就不能通过iosOpenDev来拷贝dylib。也不能用socat来查看log。这时,解决的方法是:
1. 在iosOpenDev编译完后,手动把dylib复制到手机:
scp hookKTV.dylib root@192.168.2.42:/Library/MobileSubstrate/DynamicLibraries/
2. 把原本的NSLog换成写文件。把hook信息写到文件,之后用手机助手之类的工具导出文件。
3. 重新启动springboard:
killall SpringBoard


-------------------------------- 切割线 -----------------------------------

以上全部环境搞定之后就能够新建一个project了:


选择Logos Tweak 。然后一路next 到创建好新project。

新建好的 .xm 文件里会有一个一个提示,要你去 /opt/iOSOpenDev/lib/ 文件夹下拉一个libsubstrate.dylib 动态链接库到project,由于我们这里是要hook SpringBoard,要弹出一个弹出框。所以须要再导入 UIKit.framework.

#error iOSOpenDev post-project creation from template requirements (remove these lines after completed) --

Link to libsubstrate.dylib:

(1) go to TARGETS > Build Phases > Link Binary With Libraries and add /opt/iOSOpenDev/lib/libsubstrate.dylib

(2) remove these lines from *.xm files (not *.mm files as they're automatically generated from *.xm files)


然后在 .xm 里面编写代码例如以下:

// Logos by Dustin Howett
// See http://iphonedevwiki.net/index.php/Logos
#import <UIKit/UIKit.h>

%hook SpringBoard

- (void)applicationDidFinishLaunching:(id)application {
    %orig;
    
    NSLog(@"hook SpringBoard:这是測试log输出。。

。。

"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"welcome" message:@"hellowrold" delegate:nil cancelButtonTitle:@"thanks" otherButtonTitles:nil]; [alert show]; [alert release]; } %end %hook AppDelegate - (void)applicationDidEnterBackground:(UIApplication *)application{ %orig; NSLog(@"hook AppDelegate:这是測试log输出。。。

"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hook" message:@"hellowrold" delegate:nil cancelButtonTitle:@"thanks" otherButtonTitles:nil]; [alert show]; [alert release]; } %end

编译的时候有可能会遇到例如以下的编译错误:


解决的方法是:改动 TARGETS - Build Settings - Code Signing - Provisioning Profile 为 iOS Team Provisioning Profile: * 就好了。


ssl 到手机,执行

socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock

>watch

这个命令。准备查看输出 log。


电脑和越狱设备连接到同一个网络下,然后查看设备的 wifi 地址,记下来,然后填写到 TARGETS - Build Settings - User-Defined - iOSOpenDevDevice 里面去。比方我这里是 192.168.2.42 ,为的是等下 Profiling 的时候把动态链接库 HookTest.dylb copy 到 root@192.168.2.42:/Library/MobileSubstrate/DynamicLibraries/ 文件夹下。

假如你以上环境都能成功搭建的话,那接下来就简单了。点击 XCode -> Product -> Build For -> Profiling ,XCode 就会帮你把动态链接库 HookTest.dylib (我新建的project叫HookTest,所以这里是HookTest.dylib)和 .plist(这里是 HookTest.plist) 文件copy 到 root@192.168.2.42:/Library/MobileSubstrate/DynamicLibraries/ 这个路径下了,然后重新启动springboard:
执行命令 killall SpringBoard,就能够看到手机在启动的时候就会去执行刚刚的 HookTest.dylib 然后弹出一个弹出框了。

copy 进去的 .plist 的作用是在hook的时候系统会依据 .plist 去过滤 hook 那个应用。假设 .plist 里面啥过滤条件都不写的话是全部应用都会去载入刚刚的copy进去的动态库的,默认是hook SpringBoard。
。比方说你如今hook 应用启动时的 

<del>- (void)applicationDidEnterBackground:(UIApplication *)application</del>

这种方法的话。假设.plist里面不指定是哪个应用去载入HookTest.dylib 的话,默认全部应用启动的时候都会去载入 HookTest.dylib 动态库的。


Profiling 完后查看了一下。Library/MobileSubstrate/DynamicLibraries 这个文件夹并没有生成,而是把project打包成 .deb 文件然后 copy 到越狱设备的 iOSOpenDevPackages 文件夹下,比方我生成的是 

com.aaron.hooksb_1.0-1_iphoneos-arm.deb 是这个,Profiling 完之后。iphone 会自己主动重新启动(kill SpringBoard)然后执行hook代码。


用 scp 命令能够将设备里面的文件复制到电脑里查看:

scp root@192.168.2.42:/var/root/iOSOpenDevPackages/com.aaron.hooksb_1.0-1_iphoneos-arm.deb ~/Desktop/



NOTE:在 Profiling 可能遇到的问题: 

1、Permission denied (publickey,password,keyboard-interactive) 

相关的解决方法是:

第一步:拷贝公钥到 iPhone 的 ~/home/userName 文件夹(没有该文件夹新建就可以,事实上我认为应该是iPhone的不论什么文件夹都行的。其目的都是为了第二步)

scp ~/.ssh/id_rsa.pub serverUsername@host.com:/home/serverUsername

第二步:把公钥的key写到 ~/.ssh/authorized_keys (相同没有该文件夹新建)

cat id_rsa.pub >> ~/.ssh/authorized_keys

然后再次 Profiling 就编译通过了。

參考链接:这里


2、Permission denied,Failed to create directory /var/root/iOSOpenDevPackages on device 192.168.2.42 Command /bin/sh failed with exit code 255。

问题看起来应该是跟 1 一样的。网上找到了一个更优雅的解决方案:直接在 MacBook 命令行上执行命令:iosod sshkey -h 192.168.2.42 。然后再次 Profiling 就好了。

參考链接:这里


假如你以上环境安装出错的话,我最好建议你Google一下。当然你也能够选择每次改动之后都手动copy到 root@192.168.2.42:/Library/MobileSubstrate/DynamicLibraries/ 文件夹下,明明能够仅仅是点击一下button就能够搞定的事为什么每次都要手动去copy一下呢,那样不认为非常蛋疼吗?


ok,就到这里了。


-------------------------- 2017.01.16 改动 ----------------------

 今天在用 clutch 砸壳的时候发现出错了:

com.tencent.xin contains watchOS 2 compatible application. It's not possible to dump watchOS 2 apps with Clutch 2.0.3 at this moment.

Zipping WeChat.app

Killed: 9


微信6.5.3的版本号如今用 clutch 是砸不了了,那么就直接改用 dumpdecrypted 来给砸:

dumpdecryptd 的砸壳步骤基本參照这一篇文章来做的: iOS逆向project(简单利用"dumpdecrypted"给ipa砸壳) ,

如今想记录的是砸完壳后得到的 WeChat.decrypted 用这个命令仅仅能 dump 出一个 CDStructures.h 文件

class-dump -H WeChat.decrypted --arch arm64 -o WeChatHead/

 这说明解密是不完整的。能够用 otool 命令查看 cryptid 这个字段验证解密的完整性,1 代表未解密,0 代码已解密。


➜  Desktop otool -l WeChat.decrypted | grep crypt

WeChat.decrypted (architecture armv7):

     cryptoff 16384

    cryptsize 48513024

      cryptid 0

WeChat.decrypted (architecture arm64):

     cryptoff 16384

    cryptsize 52133888

      cryptid 1


能够看到 crypid 为 1 ,确实是还没完整解密的。看来用 arm64 架构解密是不行的。换个架构试试:

class-dump --arch armv7  WeChat.decrypted -H -WeChatHead/


armv7 OK,稍等一会儿。已经成功将头文件 dump 出来了。



拓展链接:

1、手把手教你制作一款iOS越狱App,伪装微信位置

2、一步一步实现iOS微信自己主动抢红包(非越狱)

3、移动App入侵与逆向破解技术 - iOS篇

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/zhchoutai/p/8422993.html