iOS tweak中添加第三方庫

前幾天在github上看到一個不錯的第三方的調(diào)試庫DBDebugToolkit馋嗜,不過這是在開發(fā)環(huán)境中使用的词疼,做為逆向新手的我想把它通過tweak添加到其它StoreApp中去練練手烦租。

環(huán)境:

  • iPhone6s (已越獄)
  • iOS9.3.1
  • 目標(biāo)app:Instagram
Instagram

1. 打包靜態(tài)庫

如果直接向tweak中添加第三方開源庫的話豺鼻,那需要在Makefile 中把所有的.m文件都要寫進去,太麻煩了挨约,所以我感覺應(yīng)該把開源庫打包成framework或者靜態(tài)庫比較方便些味混。一開始想打包成framwork产雹, 于是就按照大神的文章添加,但是不知什么原因一直加載不進去翁锡。

Reason: image not found

最后沒法解決只能用靜態(tài)庫解決了蔓挖。按照這里的方法生成靜態(tài)庫,加入到tweak可以正常加載馆衔。

注意:工程中默認是不加載靜態(tài)庫中的 category瘟判,這個庫中正好使用了好多 category,需要在Makefile中添加tweakName_LDFLAGS += -all_load才能加載分類角溃。

2. 添加bundle

DBDebugToolkit使用了大量的xib和storyboard來創(chuàng)建view拷获,所以還需要把這些文件添加到tweak中,源程序中使用了bundle我們也打包bundle添加到tweak中:

bundle

bundle位置改變了开镣,也需要更改庫的源碼:

+ (instancetype)debugToolkitBundle {
//    NSBundle *podBundle = [NSBundle bundleForClass:[DBDebugToolkit class]];
//    NSURL *bundleURL = [podBundle URLForResource:@"DBDebugToolkit" withExtension:@"bundle"];
//    return [NSBundle bundleWithURL:bundleURL];

    NSBundle *podBundle = [NSBundle bundleWithPath:@"/InsBundle/DBDebug.bundle"];
    return podBundle;
}

3. Hook并配置DBDebugToolkit

Hook程序AppDelegate方法配置DBDebugToolkit

%hook AppDelegate
- (_Bool)application:(id)arg1 didFinishLaunchingWithOptions:(id)arg2 {
    [DBDebug setup];
    return %orig;
}
%end

另外:不知道什么原因刀诬,DBDebugToolkit中的+ (IMP)replaceMethodWithSelector:(SEL)originalSelector block:(id)block方法調(diào)用一直不成功,所以我又對源碼進行了更改邪财,并且Tweak.xm也需要hook一些其它方法(如有大神知道什么原因歡迎留言交流):

@implementation UIView (DBUserInterfaceToolkit)

#pragma mark - Method swizzling
- (void)hookInitMethod {
    [self db_refreshDebugBorders];
    [self db_registerForNotifications];
}

- (void)hookDellocMethod {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//+ (void)load {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        __block IMP originalInitWithCoderIMP = [self replaceMethodWithSelector:@selector(initWithCoder:)
//                                                                         block:^UIView * (UIView *blockSelf, NSCoder *aDecoder) {
//                                                                            UIView *res = ((UIView * (*)(id, SEL, NSCoder *))originalInitWithCoderIMP)(blockSelf, @selector(initWithCoder:), aDecoder);
//                                                                            [res db_refreshDebugBorders];
//                                                                            [res db_registerForNotifications];
//                                                                            return res;
//                                                                         }];
//        __block IMP originalInitWithFrameIMP = [self replaceMethodWithSelector:@selector(initWithFrame:)
//                                                                         block:^UIView * (UIView *blockSelf, CGRect frame) {
//                                                                             UIView *res = ((UIView * (*)(id, SEL, CGRect))originalInitWithFrameIMP)(blockSelf, @selector(initWithCoder:), frame);
//                                                                             [res db_refreshDebugBorders];
//                                                                             [res db_registerForNotifications];
//                                                                             return res;
//                                                                         }];
//        __block IMP originalDeallocIMP = [self replaceMethodWithSelector:NSSelectorFromString(@"dealloc")
//                                                                   block:^(__unsafe_unretained UIView *blockSelf) {
//                                                                       [[NSNotificationCenter defaultCenter] removeObserver:blockSelf];
//                                                                       ((void (*)(id, SEL))originalDeallocIMP)(blockSelf, NSSelectorFromString(@"dealloc"));
//                                                                   }];
//    });
//}

@implementation UIWindow (DBShakeTrigger)

#pragma mark - Recognizing shake motion
//+ (void)load {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        // Adding informing delegates about shake motion to the original implementation.
//        __block IMP originalIMP = [self replaceMethodWithSelector:@selector(motionEnded:withEvent:)
//                                                            block:^(UIWindow *blockSelf, UIEventSubtype motion, UIEvent *event) {
//                                                                if (motion == UIEventSubtypeMotionShake) {
//                                                                    [blockSelf.shakeDelegates makeObjectsPerformSelector:@selector(windowDidEndShakeMotion:) withObject:self];
//                                                                }
//                                                                ((void (*)(id, SEL, UIEventSubtype, UIEvent *))originalIMP)(blockSelf, @selector(motionEnded:withEvent:), motion, event);
//                                                            }];
//    });
//}

@implementation UIWindow (DBUserInterfaceToolkit)

#pragma mark - Method swizzling
//+ (void)load {
//    NSLog(@"load====DBUserInterfaceToolkit===========");
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        __block IMP originalIMP = [self replaceMethodWithSelector:@selector(sendEvent:)
//                                                            block:^(UIWindow *blockSelf, UIEvent *event) {
//                                                                if (event.type == UIEventTypeTouches) {
//                                                                    [blockSelf db_handleTouches:event.allTouches];
//                                                                }
//                                                                ((void (*)(id, SEL, UIEvent *))originalIMP)(blockSelf, @selector(sendEvent:), event);
//                                                            }];
//    });
//}

Tweak.xm

%hook UIResponder
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    if (motion == UIEventSubtypeMotionShake) {
        [[window shakeDelegates] makeObjectsPerformSelector:@selector(windowDidEndShakeMotion:) withObject:self];
    }
}
%end


%hook UIWindow
- (void)sendEvent:(UIEvent *)event {
    %orig;
    [self db_handleTouches:event.allTouches];
}
%end

%hook UIView
- (id)initWithCoder:(NSCoder *)aDecod {
    [self hookInitMethod];
    return %orig;
}
- (id)initWithFrame:(CGRect)aDecod {
    [self hookInitMethod];
    return %orig;
}

- (void)dealloc {
    [self hookDellocMethod];
    %orig;
}
%end

編譯打包安裝陕壹,成功:

image.png
image.png
image.png
image.png
image.png

其中的網(wǎng)絡(luò)請求監(jiān)控還是挺好的,對逆向app有一定幫助树埠。

如有錯誤歡迎留言指正糠馆。

所有代碼已上傳github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市怎憋,隨后出現(xiàn)的幾起案子又碌,更是在濱河造成了極大的恐慌,老刑警劉巖绊袋,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毕匀,死亡現(xiàn)場離奇詭異,居然都是意外死亡癌别,警方通過查閱死者的電腦和手機皂岔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來展姐,“玉大人躁垛,你說我怎么就攤上這事』浚” “怎么了教馆?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長擂达。 經(jīng)常有香客問我土铺,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任舒憾,我火速辦了婚禮镀钓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘镀迂。我一直安慰自己丁溅,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布探遵。 她就那樣靜靜地躺著窟赏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪箱季。 梳的紋絲不亂的頭發(fā)上涯穷,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音藏雏,去河邊找鬼拷况。 笑死,一個胖子當(dāng)著我的面吹牛掘殴,可吹牛的內(nèi)容都是我干的赚瘦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼奏寨,長吁一口氣:“原來是場噩夢啊……” “哼起意!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起病瞳,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤揽咕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后套菜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亲善,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年逗柴,在試婚紗的時候發(fā)現(xiàn)自己被綠了逗爹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡嚎于,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出挟冠,到底是詐尸還是另有隱情于购,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布知染,位于F島的核電站肋僧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嫌吠,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一止潘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辫诅,春花似錦凭戴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肤视,卻和暖如春档痪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邢滑。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工腐螟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人困后。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓乐纸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親操灿。 傳聞我的和親對象是個殘疾皇子锯仪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容