Logos是CydiaSubstruct框架中提供的一组宏定义。利于开发者使用宏进行Hook操作,其语法简单,功能是非常强大且稳定。

详细内容logos语法为http://iphonedevwiki.net/index.php/Logos

 

Logos语法分为三大类:

  • Block level:这类型的指令会开辟一个代码块,以%end结束。%group、%hook、%subclass、%end
  • Top level:TopLevel指令不放在BlockLevel中。%config、%hookf、%ctor、%dtor
  • Function level:这块指令放在方法中。%init、%class、%c、%orig、%log

指定需要hook住的class,必须要以%end结尾

  1. %hook SpringBoard
  2. - (void)_menuButtonDown:(id)down {
  3. NSLog(@"你好");
  4. %orig; // call the original __menuButtonDown
  5. }
  6. %end

 意思是勾住(Hook)SpringBoard类中的_menuButtonDown,先打印下,再执行函数原本的操作。

指令再%hook内部去使用,将函数的类名和参数等信息写入syslog。

  1. %hoot SpringBoard
  2. - (void)_menubuttonDown:(id)down
  3. {
  4. %log((NSString *)@"iOSRE",(NSString *)@"Debug");
  5. %orig;//call the original _menuButtonDown;
  6. }
  7. %end

指令在%hook内部中使用,执行被hook住的函数原始代码

  1. %hook SpringBoard
  2. - (void)_menuButtonDown:(id)down
  3. {
  4. NSLog(@"你好");
  5. %orig; //
  6. }
  7. %end

如果去掉了%orig,原始函数就不会被执行

  1. hook SpringBoard
  2. - (void)_menuButtonDown:(id)down
  3. {
  4. NSLog(@"你好");
  5. }
  6. %end

还可以利用%orig更改原始行数的参数。

  1. %hook SBLockScreenDateViewController
  2. - (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2
  3. {
  4. %orig(@"Red",arg2);
  5. }
  6. %end

指令用于%hook分组,便于代码管理以及按条件初始化分组,也是必须要以%end结尾;%group中可以包含很多个%hook,所有不属于某一个自定义group中的%hook也将会被归类到%group_ungroupes中。

  1. %group iOS11Hook
  2. %hook iOS12Class
  3. - (id)iOS11Method {
  4. id result = %orig;
  5. NSLog(@"This class & method only exist in iOS 11.");
  6. return result;
  7. } %end
  8. %end // iOS11Hook
  9.  
  10. %group iOS12Hook
  11. %hook iOS12Class
  12. - (id)iOS8Method {
  13. id result = %orig;
  14. NSLog(@"This class & method only exist in iOS 12."); return result;
  15. }
  16. %end
  17. %end // iOS12Hook

指令用于初始化某个%group,必须在%hook或者%ctor内调用;如果需要带参数,则初始化指定的group,如果不带参数时,就会初始化_ungrouped。只有调用了%init,其对应的%group才能够起到作用。

  1. #ifndef kCFCoreFoundationVersionNumber_iOS_11_0
  2. #define kCFCoreFoundationVersionNumber_iOS_11_0 1140.10 #endif
  3. %hook SpringBoard
  4. - (void)applicationDidFinishLaunching:(id)application {
  5. %orig;
  6. %init; // Equals to %init(_ungrouped)
  7. if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_12_0 && kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_11_0)
  8. %init(iOS12Hook);
  9. if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_11_0)
  10. %init(iOS11Hook);
  11. }
  12. %end

该指令完成初始化工作,如果不显示其定义,theos自动会生成%ctor,也会调用%init

  1. %hook SpringBoard
  2. - (void)reboot {
  3. NSLog(@"If rebooting doesn't work then I'm screwed.");
  4. %orig;
  5. }
  6. %end

成功生效,Theos隐式调用了内容如下

  1. %ctor
  2. {
  3. %init(_ungrouped);
  4. }

  1. %hook SpringBoard
  2. - (void)reboot{
  3. NSLog(@"If rebooting doesn't work then I'm screwed.");
  4. %orig;
  5. }
  6. %end
  7. %ctor
  8. {
  9. // Need to call %init explicitly!
  10. }

其中的%hook无法生效,因显示定义了%ctor,却没有定义%ctor,不需要以%end结尾。一般用于初始化%group。

在%hook内部中使用,给现有class增新函数,功能与class_addMethod意义相同。

  1. %hook SpringBoard
  2. %new
  3. - (void)namespaceNewMethod {
  4. NSLog(@"We've added a new method to SpringBoard.");
  5. }
  6. %end

%c

指令和objc_getClass或者NSClassFromString,动态获取一个类的定义。用于%hook和%ctor中使用。

 

 

 

  1. $class-dump -H 001-LogosDemo -o /Users/yaoqi/Desktop/LogosHeaders

 

 

 

此时MonkeyDev工程将libsubstrate.dylib和RevealServer.framework注入了工程,里面有libsubstrate.dylib就可以利用logos语法了。

 

 

 

 

 _02_loginHookDemoDylib.xm

  1. // See http://iphonedevwiki.net/index.php/Logos
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5. @interface ViewController: UIViewController
  6. - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
  7. + (void)CL_classMethod;
  8. @end
  9.  
  10. %hook ViewController
  11. - (void)loginBtnClicked:(id)arg1 {
  12. %log;
  13. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Hook成功了!!!" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
  14. [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:nil]];
  15. [self presentViewController:alertVC animated:YES completion:nil];
  16. }
  17. %new
  18. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  19. [self.view endEditing:YES];
  20. [self.class CL_classMethod];
  21. }
  22. %new
  23. + (void)CL_classMethod {
  24. NSLog(@"这是一个类方法!!!");
  25. }
  26. %end

 

 

 

在MonkeyDev根目录添加Podfile文件,Target为Monkey动态库的Target

  1. platform :ios, '9.0'
  2. target '002-loginHookDemoDylib' do
  3. use_frameworks!
  4. pod 'FLEX'
  5. end

FlEX可以查看App的文件、数据库、界面层级以及沙盒

 

 

要求:微信首页加个“+”按钮,左边按钮和右边的效果一样。

 

  1. Target <NewMainFrameRightTopMenuBtn: 0x104dd99d0>
  2. Action showRightTopMenuBtn

 

 

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface NewMainFrameViewController :UIViewController
  4. @end
  5.  
  6. @interface NewMainFrameRightTopMenuBtn: UIView
  7. - (void)showRightTopMenuBtn;
  8. @end
  9.  
  10. @interface MMBarButtonItem: UIBarButtonItem
  11. @property(nonatomic,weak)NewMainFrameRightTopMenuBtn *view;
  12. @end
  13.  
  14. %hook NewMainFrameViewController
  15. -(UINavigationItem *)navigationItem{
  16. // NSLog(@"\n\n\n-------------navigationItem-----");
  17. //方法交换! 调用自己!
  18. return %orig;
  19. }
  20. - (void)viewDidAppear:(_Bool)arg1{
  21. %orig;
  22. UIButton * leftBtn = [UIButton buttonWithType:(UIButtonTypeContactAdd)];
  23. [leftBtn addTarget:self action:@selector(CL_leftClick) forControlEvents:(UIControlEventTouchUpInside)];
  24. [self.navigationItem setLeftBarButtonItem: [[UIBarButtonItem alloc] initWithCustomView:leftBtn]];
  25. }
  26. - (void)viewDidLoad{
  27. %orig;
  28. // NSLog(@"\n\n\n-----viewDidLoad-----------");
  29. }
  30. %new
  31. -(void)CL_leftClick
  32. {
  33. /**
  34. 从内存中能查到调用该方法:[self.navigationItem.rightBarButtonItem.view showRightTopMenuBtn]
  35. self:代表NewMainFrameViewController控制器
  36. */
  37. MMBarButtonItem *btn = self.navigationItem.rightBarButtonItem;
  38. [btn.view showRightTopMenuBtn];
  39. }
  40. %end

 

 

上面就是Logos语法及讲解,如果对大家有所帮助,希望大家关注,也可以点个喜欢,下一篇我们将讲解越狱的相关知识,请大家准备好越狱手机和PP助手!!!

版权声明:本文为guohai-stronger原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/guohai-stronger/p/11985402.html