ios swift hook

下面这个文章介绍的很清楚,这里做搬运工,方便需要者查阅。

https://www.securify.nl/blog/SFY20150302/hooking-swift-methods-for-fun-and-profit.html




INTRODUCTION

During the past few years different hooking frameworks have been developed for iOS, which allows users to modify app behavior without applying binary patches. Hooking iOS methods can benefit many things during a (blackbox) security assessment. Hooking allows us to log methods calls, inspect the input and/or output of a method, and even circumvent security measures like SSL/TLS in order to view/manipulate network traffic. In order to hook methods using such a framework, the device needs to be Jailbroken.

Hooking C/C++ and Objective-C methods has become more common over the years. More tools and frameworks are available and are still being developed in order to perform (security) research and create custom app modifications. Apple introduced a new programming language called Swift that is built on top of the Objective-C runtime. Swift methods can be hooked in a similar, but slightly different way. This article will describe how Swift methods can be hooked.

METHOD AND SELECTOR SIGNATURES

In order to hook a method it is important to identify its signature; how many parameters does it require, what are the types, the symbol name, and what type it returns. In order to acquire this information the following tools can be used:

IDA - disassembler;

Hopper - disassembler;

otool - object file info tool;

nm - list symbols from object files;

class-dump - dump class signatures.

Swift symbols are mangled similar to C++ methods. Whenever the symbol table of a Swift app is dumped with a tool like nm, the Swift symbols are listed with a __T prefix whereas C++ symbols start with __Z. Similar to the C++ demangling tool c++filt, demangling Swift methods can be done using xcrun swift-demangle.

The following example shows the difference between a mangled and demangled symbol table:

$ nm<swift binary>[..]0000000100002840 T __TFC7DemoApp11AppDelegates6windowGSqCSo8UIWindow_00000001000019f0 T __TFC7DemoApp14ViewController11viewDidLoadfS0_FT_T_0000000100001af0 T __TFC7DemoApp14ViewController15mySuperFunctionfS0_FT_T_0000000100001d20 T __TFC7DemoApp14ViewController23didReceiveMemoryWarningfS0_FT_T_00000001000024a0 T __TFC7DemoApp14ViewControllerCfMS0_FT5coderCSo7NSCoder_S0_[..]

$ nm<swift binary>|xcrun swift-demangle[..]00000001000027b0 t _@objc DemoApp.AppDelegate.window.setter:ObjectiveC.UIWindow?0000000100001ac0 t _@objc DemoApp.ViewController.viewDidLoad(DemoApp.ViewController)()->()0000000100001cf0 t _@objc DemoApp.ViewController.mySuperFunction(DemoApp.ViewController)()->()0000000100001d80 t _@objc DemoApp.ViewController.didReceiveMemoryWarning(DemoApp.ViewController)()->()00000001000024e0 t _@objc DemoApp.ViewController.init(DemoApp.ViewController.Type)(coder:ObjectiveC.NSCoder)->DemoApp.ViewController[..]

Constructing a method hook requires both representations. The mangled symbol is the actual pointer to the method and is used during the hooking setup. The demangled version helps us to construct the actual hook since it contains parameters and return types. The following example shows a method of the DemoApp class:

funcmySuperFunction(){// do stuff}

The mangled method name looks like this:

0000000100001af0 T __TFC7DemoApp14ViewController15mySuperFunctionfS0_FT_T_

The mangled method name can be broken down in the following components:

__T - indicates a Swift method;

F - the symbol is a function/method;

C - the symbol is a class method;

7DemoApp - app name/module name;

14ViewController - class name;

15mySuperFunction - method name.

This method does not have a return type and does not have any parameters. Or to be more precise, it has one parameter called self. When demangled, the method looks like this:

0000000100001cf0 t _@objc DemoApp.ViewController.mySuperFunction (DemoApp.ViewController)() -> ()

This can be roughly translated into:

Module.classname.functionName (self) -> (no return type)

CONSTRUCTING THE HOOK

Now that we know how to obtain and demangle Swift symbols we can construct hooks for Swift methods. In the following example we will hook the constructor of the NSString class. Our hook will write the constructor value to NSLog. First we need to determine the NSString constructor symbol. Using nm we can see the following mangled symbol:

00000001000025c0 t __TFCSo8NSStringCfMS_FT6stringSS_S_

Demangling the symbol gives us:

00000001000025c0 t _ObjectiveC.NSString.__allocating_init (ObjectiveC.NSString.Type)(string : Swift.String) -> ObjectiveC.NSString

Since NSString is a member of Apple's Foundation framework we could also look the signature up in the developer API

The constructor takes a (Swift) string as parameter and will return an NSString object. With this information in mind we can construct our hook.

First, we need to declare a function pointer that will point to the original method we are going to hook. This will become handy when we want to call the original method from our hook.

staticid(*orig_nsstring_init)(id,id);

Since NSString is a Swift object we can use the id type when identifying parameters and return type. Note that we declared two parameters instead of one. In addition constructor value, we must also process the self parameter. This is different from hooking Objective-C methods where we also process the selector parameter. For example:

idmethod(idself,SELcmd,id param1)// Objective-C method signature

Now we can create the method hook.

idnsstring_init_hook(id str,idself)// hook signature{id orig_value=orig_nsstring_init(str,self);// invoke original constructor, save return valueNSLog(@"# %@ #",orig_value);// print constructor parameterreturnorig_value;// return the value to facilitate the original behaviour}

Important to note here is that in comparison to Objective-C methods, the self parameter is the last parameter instead of the first. As mentioned before, the selector parameter is not supplied. Using Cydia Substrate we can use the following method to tie this all together using MSHookFunction.

MSHookFunction(MSFindSymbol(NULL,"__TFCSo8NSStringCfMS_FT6stringSS_S_"),// find the mangled symbol(void*)nsstring_init_hook,// function pointer to our hook(void**)&orig_nsstring_init);// stores function pointer to original function

CODE LISTING

#import<Foundation/Foundation.h>#import<substrate.h>staticid(*orig_nsstring_init)(id,id)=NULL;idnsstring_init_hook(NSString*_str,id _self){id x=orig_nsstring_init(_str,_self);NSLog(@" ## %@ ##",x);returnx;}__attribute__((constructor))intmain(void){MSHookFunction(MSFindSymbol(NULL,"__TFCSo8NSStringCfMS_FT6stringSS_S_"),(void*)nsstring_init_hook,(void**)&orig_nsstring_init);return0;}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,245评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,749评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,960评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,575评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,668评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,670评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,664评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,422评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,864评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,178评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,340评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,015评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,646评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,265评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,494评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,261评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,206评论 2 352

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,320评论 0 10
  • 文/走在迷茫的人生边上 我看悬! 怎么说呢,你看现在别说结庐在人境了,都已经人堆人了。房子都几十层高,即使你想心远...
    读书少的丝瓜阅读 1,049评论 22 5
  • 有鹤来 跨鹤高飞意壮哉,云霄一羽雪皑皑。此行莫恨天涯远,咫尺理塘归去来。2017一10一26日早
    胜者为王王臣森阅读 281评论 0 0
  • 【一小朵儿】20170923学习力践行D128 (1)精读:小熊宝宝绘本之《过生日》《睡觉》《午饭》(2)泛读:婴...
    佛铃花海阅读 210评论 0 0
  • 某天在银行排号办业务,正等的百无聊懒,戏剧性的一幕出现了: 一五六十岁样貌时尚的女士突然很热情的大声说:“这不他张...
    寒蝉0520阅读 295评论 0 7