首页
社区
课程
招聘
[原创](calleng逆向日记)Frida前置知识, ObjC runtime的"反射" KVC实例Demo分析第二部(Demo底部下载)
2023-10-6 02:52 3868

[原创](calleng逆向日记)Frida前置知识, ObjC runtime的"反射" KVC实例Demo分析第二部(Demo底部下载)

2023-10-6 02:52
3868

入门学习逆向的个人笔记,预览(欢迎探讨)

  • C/C++的学习和数据结构

[原创] [calleng的逆向日记] 22y11m-23y3m29d 实现macOS编辑, iOS端编译和调试C/C++
[原创] [calleng的逆向日记] 23/03/30 (C++概念构建篇01) 补充完毕
[分享] [calleng的逆向日记] 22y11m-23y6m7d Win10Arm下的 gdb调试.[数据结构/严蔚敏-之迷宫问题]

  • iOS的学习和逆向

[分享] IOS软件安全工程师技能表(2017.7by非虫) (为自己学习导航) 图片不清楚下面有导图下载
[分享] Frida-Tool的一些 介绍, 和在 iOS下的一些用法 [个人笔记汇总]
[原创] [calleng的逆向日记] 弹窗的修改原理-OC篇 [源码学习和HOOK实践]23/09/24 --待续
[讨论] [calleng的逆向日记] 自学iOS逆向时候,如何自己解决问题.
[分享] [calleng逆向日记] iOS crackMe的破解 与 Frida(Objection) 的入门使用(thanks to roysue)
[分享] (iOS Hook原理,OC底层实现)Frida前置知识的(royuse)的一些知识注解(图片三次压缩失真,详情见附件)
[原创] (calleng逆向日记)Frida前置知识, ObjC runtime的"反射"-KVC-实例代码理解和分析
[分享] [calleng的逆向日记] Frida 前置知识, 类与方法的底层实现, 逻辑批注, (参考AloneMonkey的书)
[原创] (calleng逆向日记)Frida前置知识, ObjC runtime的"反射" KVC实例Demo分析第二部(Demo底部下载)
[分享] [calleng的逆向日记] iOS crackMe and Frida(Objection) Get Started (Oct,16th)
[分享] [calleng的逆向日记] Frida在iOS上内存漫游与黑盒调用 Get Started Section 4


DEMO 运行环境

  • macOS Ventura 13.1 (Intel)
  • Xcode 14.2
  • iOS 14.3
  • Path : ~/Documents/iOS-Lesson-Code-2022-9-5/15.3_UIAlertController's_Usage/15.3_UIAlertController's_Usage.xcodeproj

KVC流程阻断的4种方式

或者可以这样理解, hook函数的4种方式

  • 通过setter方法的属性设置,getter方法的属性调用
  • 通过setter设置getter调用失败后的 _Key,_isKey,Key,isKey方式设置&获取
  • setValue:forUndefinedKey设置值方式
  • valueForUndefinedKey获取值方式

前提基础:

通过 打开 KVC的函数文件NSKeyValueCoding.h Copyright (c) 1994-2019, Apple Inc. All rights reserved. 可以知道

KVC相关API

KVC设值

  • [author1 setValue:@"author1 的新名字" forKey:@"name"];为例

    • 优先通过setter方法,进行属性设置,调用顺序是:
      1 setName
      2 _setName
      3 setIsName
    • 如果以上方法均未找到,并且accessInstanceVariablesDirectly返回YES,则通过成员变量进行设置,顺序是:
      1 _name
      2 _isName
      3 name
      4 isName
  • accessInstanceVariablesDirectly说明

    • 重写+ (BOOL)accessInstanceVariablesDirectly方法让其返回NO,这样的话,如果KVC没有找到set<Key>_set<Key>setIs<Key>相关方法时,会直接用setValue:forUndefinedKey:方法

KVC设值流程图

KVC取值

  • NSString* name = [author1 valueForKey:@"name"];为例
    • getter方法的调用顺序是:
      1 getName
      2 name
      3 isName
      4 _name
    • 如果以上方法没有找到,accessInstanceVariablesDirectly返回YES,则直接返回成员变量,获取顺序依然是:
      1 _name
      2 _isName
      3 name
      4 isName

KVC取值程图

通过代码理解

oc编程不是太熟悉, 所以一边练习, 一边写代码, 本来想在控制台中输入 1,2,3 的选项来学习的, 迷茫3个小时, 以前的code中, 使用 **showActionSheet**的方式, 刚好解决iOS中的, switch, input控制台无法处理的问题.学到知识综合利用才是关键

setKey,getKey方式设置&获取

关键code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@interface Author1 : NSObject
{
@public
    NSString *_isName;
    NSString *name;
    NSString *isName;
    NSString *_name;
}
- (void)setName;
- (void)_setName;
- (void)setIsName;
- (NSString *)getName;
- (NSString *)name;
- (NSString *)isName;
- (NSString *)_name;
 
@end
 
@implementation Author1
+(BOOL)accessInstanceVariablesDirectly{    return NO;}
 
// 设置方法
 - (void)setName:(NSString*)name{    NSLog(@"1,设置值的优先函数 %s - %@",__func__,name); }
 - (void)_setName:(NSString *)name{    NSLog(@"2,设置值的优先函数 %s - %@",__func__,name); }
 - (void)setIsName:(NSString *)name{    NSLog(@"3,设置值的优先函数 %s - %@",__func__,name); }
 
// 取值方法
- (NSString *)getName{    return NSStringFromSelector(_cmd);}
- (NSString *)name{    return NSStringFromSelector(_cmd);}
- (NSString *)isName{    return NSStringFromSelector(_cmd);}
- (NSString *)_name{    return NSStringFromSelector(_cmd);}
@end
 
    UIAlertAction *two = [UIAlertAction actionWithTitle:@"setKey,getKey方式设置&获取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        Author1* author1 = [Author1 new];
        [author1 setValue:@"author1 的新名字" forKey:@"name"];
        NSString* name = [author1 valueForKey:@"name"];
        NSLog(@"取值的优先函数 value for key : %@",name);
        NSLog(@"取值_name:%@",author1->_name);
        NSLog(@"取值_isName:%@",author1->_isName);
        NSLog(@"取值name:%@",author1->name);
        NSLog(@"取值isName:%@",author1->isName);
    }];

代码激活方式

具体效果

_Key,_isKey,Key,isKey方式设置&获取

关键code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@interface Author2 : NSObject
{
@public
    NSString *_isName;
    NSString *name;
    NSString *isName;
    NSString *_name;
}
@end
 
@implementation Author2
+(BOOL)accessInstanceVariablesDirectly{    return YES;}
@end
 
    UIAlertAction *three = [UIAlertAction actionWithTitle:@"_Key,_isKey,Key,isKey方式设置&获取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        Author2* author2 = [Author2 new];
        [author2 setValue:@"author2 的新名字" forKey:@"name"];  // 设定值
        NSString* name = [author2 valueForKey:@"name"];  // 取值
        NSLog(@" value for key : %@",name);
        NSLog(@"取值_name:%@",author2->_name);
        NSLog(@"取值_isName:%@",author2->_isName);
        NSLog(@"取值name:%@",author2->name);
        NSLog(@"取值isName:%@",author2->isName);
    }];

代码激活方式

具体效果

setValue:forUndefinedKey设置值方式

关键code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@interface Author3 : NSObject
{
@public
    NSString *_isName;
    NSString *name;
    NSString *isName;
    NSString *_name;
}
@end
 
@implementation Author3
+(BOOL)accessInstanceVariablesDirectly{    return YES;}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{    NSLog(@"forUndefinedKey出现异常,该key不存在%@",key);}
@end
 
    UIAlertAction *four = [UIAlertAction actionWithTitle:@"setValue:forUndefinedKey设置值方式" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        Author3* author3 = [Author3 new];
        [author3 setValue:@"author3 的新名字" forKey:@"name"];  // 设定值
        [author3 setValue:@"设定一个不存在的属性值" forKey:@"name3"];  // 设定一个不存在的属性值
        NSString* name = [author3 valueForKey:@"name"];  // 取值
        NSLog(@" value for key : %@",name);
        NSLog(@"取值_name:%@",author3->_name);
        NSLog(@"取值_isName:%@",author3->_isName);
        NSLog(@"取值name:%@",author3->name);
        NSLog(@"取值isName:%@",author3->isName);
    }];

代码激活方式

具体效果

valueForUndefinedKey获取值方式

关键code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@interface Author4 : NSObject
{
@public
    NSString *_isName;
    NSString *name;
    NSString *isName;
    NSString *_name;
}
@end
 
@implementation Author4
+(BOOL)accessInstanceVariablesDirectly{    return YES;}
-(id)valueForUndefinedKey:(NSString *)key{    NSLog(@"valueForUndefinedKey出现异常,该key不存在%@",key);   return nil;}
@end
 
    UIAlertAction *five = [UIAlertAction actionWithTitle:@"valueForUndefinedKey获取值方式" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        Author4* author4 = [Author4 new];
        [author4 setValue:@"author4 的新名字" forKey:@"name"];  // 设定值
        NSString* name = [author4 valueForKey:@"name"];  // 取值
        NSString* name4 = [author4 valueForKey:@"name4"];  // 取一个不存在的属性值
        NSLog(@" value for key : %@",name);
        NSLog(@"取值_name:%@",author4->_name);
        NSLog(@"取值_isName:%@",author4->_isName);
        NSLog(@"取值name:%@",author4->name);
        NSLog(@"取值isName:%@",author4->isName);
    }];

代码激活方式

具体效果

感谢以下前辈分享的技术档案和参考


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2023-11-8 00:24 被calleng编辑 ,原因: 修改大纲,填充内容
上传的附件:
收藏
点赞0
打赏
分享
最新回复 (1)
雪    币: 19349
活跃值: (28971)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
秋狝 2023-10-7 09:26
2
1
感谢分享
游客
登录 | 注册 方可回帖
返回