iOS逆向1023-反调试&&反反调试

概括:Xcode、LLDB

001--debugserver
002--debugserver下
003--命令行工具
004--反调试Ptrace
005--反Ptrace
006--通过framework防护调试
007--总结


001--debugserver上

查看 debugserver

iOS逆向1023-反调试&&反反调试_第1张图片
image.png
iOS逆向1023-反调试&&反反调试_第2张图片
image.png

手机IP

image.png

越狱环境下:先连接手机,再通过debugserver调试

# ./debugserver             //查看debugserver
# ps -A                         //查看进程
# ./debugserver *:12346 -a WeChat   //debugserver 服务12346  映射到手机端口 22,附加到微信饿进程中

$ lldb
(lldb) process connect connect://192.168.31.245:12346  // 连接debugserver
(lldb) c            //继续运行
(lldb) ret          //返回
(lldb) b onNext     //添加一个断点,会断点很多处(一般通过地址下断点)
(lldb) b list       //断点个数

002--debugserver下
# Clutch -i       //查看砸壳信息
# Clutch -d xxNum //去砸壳
iOS逆向1023-反调试&&反反调试_第3张图片
image.png

砸壳后

iOS逆向1023-反调试&&反反调试_第4张图片
image.png

Hooper 去分析

iOS逆向1023-反调试&&反反调试_第5张图片
image.png

(lldb) b +地址 //下个断点

image.png
$ file debugserver
$ldid -e 可执行文件 > 文件名称.entitlement
$ldid -e debugserver > debugserver1.entitlement
image.png
iOS逆向1023-反调试&&反反调试_第6张图片
image.png
iOS逆向1023-反调试&&反反调试_第7张图片
image.png

重签
$ldid -Sdebugserver1.entitlement debugserver

iOS逆向1023-反调试&&反反调试_第8张图片
image.png
iOS逆向1023-反调试&&反反调试_第9张图片
image.png

003--命令行工具

创建调试

iOS逆向1023-反调试&&反反调试_第10张图片
image.png
#import 

int main(int argc, char * argv[]) {
    
    if (argv[0]) {
        printf("%s\n",argv[0]);
    }
    if (argv[1]) {
        printf("%s\n",argv[1]);
    }
    if (argv[2]) {
        printf("%s\n",argv[2]);
    }
    if (argv[3]) {
        printf("%s\n",argv[3]);
    }
    
    printf("这是一个命令工具!!欢迎使用!\n");
    return 0;
}

004--Ptrace反调试(防护代码)

/* 反调试
ptrace (process trace 进程跟踪)
此函数提供了一个进程监听控制另外一个进程.并且可以检测被控制进程的内存和寄存器里面的数据!
它可以用来实现断点调试和系统调用跟踪.debugserver就是用的它
iOS 中没有提供相关的头.书籍:<程序员的自我修养>
*/

Ptrace 相关函数

image.png

#import 
#import "AppDelegate.h"
#import 

int main(int argc, char * argv[]) {
    @autoreleasepool {
    //ptrace(int _request, pid_t _pid, caddr_t _addr, int _data)
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
//---start #import "MyPtraceHeader.h"---------------------------------------------------
#ifndef    _SYS_PTRACE_H_
#define    _SYS_PTRACE_H_

#include 
#include 

enum {
    ePtAttachDeprecated __deprecated_enum_msg("PT_ATTACH is deprecated. See PT_ATTACHEXC") = 10
};

#define    PT_TRACE_ME    0    /* child declares it's being traced */
#define    PT_READ_I    1    /* read word in child's I space */
#define    PT_READ_D    2    /* read word in child's D space */
#define    PT_READ_U    3    /* read word in child's user structure */
#define    PT_WRITE_I    4    /* write word in child's I space */
#define    PT_WRITE_D    5    /* write word in child's D space */
#define    PT_WRITE_U    6    /* write word in child's user structure */
#define    PT_CONTINUE    7    /* continue the child */
#define    PT_KILL        8    /* kill the child process */
#define    PT_STEP        9    /* single step the child */
#define    PT_ATTACH    ePtAttachDeprecated    /* trace some running process */
#define    PT_DETACH    11    /* stop tracing a process */
#define    PT_SIGEXC    12    /* signals as exceptions for current_proc */
#define PT_THUPDATE    13    /* signal for thread# */
#define PT_ATTACHEXC    14    /* attach to running process with signal exception */

#define    PT_FORCEQUOTA    30    /* Enforce quota for root */
#define    PT_DENY_ATTACH    31

#define    PT_FIRSTMACH    32    /* for machine-specific requests */

__BEGIN_DECLS

int    ptrace(int _request, pid_t _pid, caddr_t _addr, int _data);

__END_DECLS

#endif    /* !_SYS_PTRACE_H_ */
//---end #import "MyPtraceHeader.h"---------------------------------------------------


//---start #import "ViewController.h"-------------------------------------------------
#import "ViewController.h"
#import "MyPtraceHeader.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    /* 附加进程
     arg1:ptrace要做的事情
     arg2:要操作进程的ID
     arg3(地址)\arg4(数据): 取决于arg1
     */
    ptrace(PT_DENY_ATTACH, 0, 0, 0);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"点击了!");
    // Dispose of any resources that can be recreated.
}
@end
//---end #import "ViewController.h"-------------------------------------------------


005--反Ptrace(攻击代码)

5.1 创建动态库

iOS逆向1023-反调试&&反反调试_第11张图片
image.png
image.png

5.2 导入fishhook:交换Ptrace方法

iOS逆向1023-反调试&&反反调试_第12张图片
image.png

编译运行,可以调试!


006--通过framework防护调试

6.1、反Hook:思路是-最快调用
6.2、通过 ptrace 反调试:思路是-最快调用
调用的顺序的先后
注入的动态库 还要在后面

6.3 创建antiDebug 防护动态库(下一节具体介绍)

iOS逆向1023-反调试&&反反调试_第13张图片
image.png


总结:

debugserver 连接APP

$ debugserver *:端口号 -a 进程
  • *:端口号
    • 使用手机的某个端口提供服务
  • -a 进程
    • 连接的APP (进程ID,进程名称--MachO文件的名称)

LLDB 启动

  • 开启LLDb
$lldb
  • 连接debugserver
(lldb) process connect connect://手机IP地址:debugserver服务的端口

连接成功,程序就被断住的.

命令行工具的权限文件

导出权限文件

$ldid -e 可执行文件 > 文件名称.entitlement

两个关键字段:
get-task-allow
task_for_pid-allow //pid 附加到别人的app上

签名权限

$ldid -S权限文件 可执行文件

你可能感兴趣的:(iOS逆向1023-反调试&&反反调试)