iOS逆向----调试之艰辛路

来源:互联网 发布:diffrac.eva软件下载 编辑:程序博客网 时间:2024/04/30 09:55

从5月12日到今天,一直在反反复复的测试gdb、debugserver+lldb、gikdbg等,最终结果没有一个能真正做到调试iOS应用的(悲催啊),感谢《iOS应用逆向工程》的作者--snakeninny,不厌其烦的回答我的问题,大概是我的硬件或者其他什么问题吧,最终还是有没有解决的问题。过程虽痛苦但却积累了不少的经验,让我学会了很多处理问题的方法。下面我就详细把这一失败但却丰富的过程记录如下,先放下(佛说:放下屠刀立地成佛),后面没准就通了。

硬件和软件的环境配置:PC主机系统:win8操作系统安装VMware Workstation9,VMware Workstation9安装Mountain Lion10.8.5系统。

被调试iOS设备:iPhone4s、iPad2、iPadmini

一、GDB安装及调试

   1、在iOS上安装威锋源:http://cydia.radare.org,然后下载GNU Debugger。

         问题:会因为网络问题失败多次。官方作者是saurik的总是报错。我下的是
   2、从网上https://github.com/gdbinit/Gdbinit/archive/master.zip下载gdbinit,然后复           制到iOS设备的/var/root下面,并改名为.gdbinit。
   3、然后用前面说的方法进行ssh连接,进入到usbmuxd下。
        首先,用ps看下有没有前面用过这个监听进程,有就先kill pid
        第一个窗口--打开转发监听:
         wls-Mac:python-client wl$ ./tcprelay.py -t 22:2222
             Forwarding local port 2222 to remote port 22
        第二个窗口--建立ssh连接
        wls-Mac:~ wl$ ssh root@127.0.0.1 -p 2222
           The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
            RSA key fingerprint is b1:23:33:d1:5e:79:a7:27:b3:20:e3:ee:dd:4f:22:73.
            Are you sure you want to continue connecting (yes/no)? yes
            Warning: Permanently added '[127.0.0.1]:2222' (RSA) to the list of known hosts.
            root@127.0.0.1's password: 
            Administratormato-iPad:~ root#
            遇到的问题:如果前面建立过连接,可能出现“Host key verification failed.“的提示,ssh连接不成功。
            这时需要搜索一个.ssh文件夹,这样搜索~/.ssh,然后删除里面的known_hosts文件就ok了。
    4、进入GDB
         Administratormato-iPad:~ root# gdb
             GNU gdb 6.3.50.20050815-cvs (Fri May 20 08:08:42 UTC 2011)
             Copyright 2004 Free Software Foundation, Inc.
             GDB is free software, covered by the GNU General Public License, and you are
             welcome to change it and/or distribute copies of it under certain conditions.
             Type "show copying" to see the conditions.
             There is absolutely no warranty for GDB.  Type "show warranty" for details.
             This GDB was configured as "--host=arm-apple-darwin9 --target=".
     5、Attach Springboard(报错)
         gdb$ attach SpringBoard
            Attaching to process 27.
            0x30d8f004 in ?? ()
            Error while running hook_stop:
            No symbol table is loaded.  Use the "file" command.
            

No symbol table is loaded. Use the "file" command的原因

           (网上说源文件在编译时,没有使用 -g 参数。

加了-g参数后,重新编译,然后再调试就没有问题了。但我这里是attach进程,这个方法肯定不行。

           by the way,为了看到iOS里的进程可以从Sydia源下一个pstree,为了在终端能使用ps命令,可在sydia源下载adv-cmds,使用时用ps -ax可以看全所有的进程。
           作者说:因为Apple已经弃gdb投lldb,所以随着我动态调试的次数越来越频繁,gdb上一个接一个的bug经常会让人很恼火。

二、debugserver+lldb安装与调试
     参考这个链接的帖子一步一步做的http://bbs.iosre.com/forum.php?mod=viewthread&tid=52&extra=&page=1       
         1、获取Debugserver,在iOS的/Developer/usr/bin下面。
              问题:一开始会发现没有,原因是需要用xcode连上该设备随便编译个什么程序就有了。
                          不通iOS版本下的大小不一样,我发现有的有1M多的,有的才100多K。
          2、瘦身
                  lipo -thin armv7s /path/to/debugserver -output /path/to/debugserver
       根据不同设备调整你的cpu架构类型。
       这里会报错:??
   3、签名
       准备一个ent.xml文档
       <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-    1.0.dtd">
 <plist version="1.0">
 <dict>
        <key>com.apple.springboard.debugapplications</key>
        <true/>
        <key>get-task-allow</key>
        <true/>
        <key>task_for_pid-allow</key>
        <true/>
        <key>run-unsigned-code</key>
        <true/>
 </dict>
 </plist>
   输入:/path/to/ldid -S/path/to/ent.xml /path/to/debugserver
    切记-S是连在一起的,不能分开
    这个地方折腾了很久,经常不能成功
    报错:ldid: command not found  原因:ldid要加全路径
    如果要查是否加上了就可以用下面的命令:
    wls-Mac:opt wl$ /opt/ldid -e debugserver
    CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>com.apple.springboard.debugapplications</key>
        <true/>
        <key>get-task-allow</key>
        <true/>
        <key>task_for_pid-allow</key>
        <true/>
        <key>run-unsigned-code</key>
        <true/>
    </dict>
    </plist>
   4、将定制好的debugserver拷贝回iOS,一般放在/usr/bin/debugserver
   5、然后通过ssh连接上iOS,
     Administratormato-iPad:~ root# debugserver *:1234 -a "SpringBoard"       
    Administratormato-iPad:~ root#                                                       //不报错,也没有任何提示

      在osx上用端口转发:
      wls-Mac:python-client wl$ ./tcprelay.py -t 1234:1234
     在osx上进lldb:
     wls-Mac:~ wl$ xcrun lldb                                   //如果这里直接用lldb,会报错:SDK Path:error unable to locate SDK
      (lldb) platform select remote-ios
      Platform: remote-ios
       Connected: no
       SDK Path: "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/7.0"
      SDK Roots: [ 0] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2"
      SDK Roots: [ 1] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3"
      SDK Roots: [ 2] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/5.0"
      SDK Roots: [ 3] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/5.1"
      SDK Roots: [ 4] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/6.0"
      SDK Roots: [ 5] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/6.1"
       SDK Roots: [ 6] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/7.0"
        SDK Roots: [ 7] "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/7.0.3 (11B508)"
       SDK Roots: [ 8] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/5.0.1 (9A406)"
       SDK Roots: [ 9] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/5.1.1 (9B206)"
       SDK Roots: [10] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/6.1 (10B141)"
       SDK Roots: [11] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/6.1.2 (10B146)"
       SDK Roots: [12] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/6.1.3 (10B329)"
       SDK Roots: [13] "/Users/wl/Library/Developer/Xcode/iOS DeviceSupport/7.0.6 (11B651)"
       以上都没有问题,到下面就出问题:
       连接远程监听端口
       (lldb) process connect connect://127.0.0.1:1234
         Process 0 connected
         (lldb) 
         刚才的转发端口的窗口,发出错误消息:
        wls-Mac:python-client wl$ ./tcprelay.py -t 1234:1234
          Forwarding local port 1234 to remote port 1234

           Incoming connection to 1234
           Waiting for devices...
           Connecting to device <MuxDevice: ID 15 ProdID 0x12a0 Serial 'f721a964e879dbd1694d6ba4502c61a5c8914f70' Location 0x3100000>
            ----------------------------------------
            Exception happened during processing of request from ('127.0.0.1', 49415)  //这个地方的端口怎么不是1234?所以端口转发是否有问题
            Traceback (most recent call last):
             File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 582, in process_request_thread
              self.finish_request(request, client_address)
              File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
              self.RequestHandlerClass(request, client_address, self)
              File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 639, in __init__
             self.handle()
              File "./tcprelay.py", line 82, in handle
              dsock = mux.connect(dev, self.server.rport)
              File "/Users/wl/usbmuxd/python-client/usbmux.py", line 235, in connect
              return connector.connect(device, port)
              File "/Users/wl/usbmuxd/python-client/usbmux.py", line 206, in connect
               raise MuxError("Connect failed: error %d"%ret)
              MuxError: Connect failed: error 3
      至此,该路就走不通了,是否是因为虚拟机的端口转发没有成功?还要进一步查找原    因。

   三、gikdbg安装与调试
   1、在Cydia添加源:http://apt.feng.com/geekneo
       下载gikir_server
   2、去http://gikir.com/productphp下载pc端:gikdbg.exe
   3、连接上iOS设备到windows
        打开iOS上下的gikir_iserver服务端。
   3、应用操作比较简单,在PC机(windows)上打开gikdbg.exe,
        选择菜单栏里iDebug-〉Device-〉login(usb)(下方可看见连接成功与否)
        然后就可进菜单进行attach,iDebug-〉file-〉attach,我选择了SpringBoard就可
        看见下方出现错误:failed to execute ‘/Application/gikir_iserver.app/debugserver'
  
    

                
         

    

0 0
原创粉丝点击