From 94d7f0950ad7027490ca0105f01ee22da68dbfaf Mon Sep 17 00:00:00 2001 From: Leilei Date: Tue, 16 Jun 2020 08:17:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9B=B4=E6=96=B0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hookSublimeText/hookSublimeText/Main.h | 6 + hookSublimeText/hookSublimeText/Main.m | 211 ++++++++++++++++++++++--- 2 files changed, 196 insertions(+), 21 deletions(-) diff --git a/hookSublimeText/hookSublimeText/Main.h b/hookSublimeText/hookSublimeText/Main.h index 879acc6..2a49a44 100644 --- a/hookSublimeText/hookSublimeText/Main.h +++ b/hookSublimeText/hookSublimeText/Main.h @@ -9,6 +9,7 @@ #import #import + @interface PXWindowDelegate : NSObject - (void)windowWillEnterFullScreen:(id)arg1; @@ -36,3 +37,8 @@ - (void)paste:(id)arg1; @end + +@interface HttpConnectionDelegate : NSObject +- (void)connection:(id)arg1 didReceiveData:(id)arg2; +- (void)connection:(id)arg1 didReceiveResponse:(id)arg2; +@end diff --git a/hookSublimeText/hookSublimeText/Main.m b/hookSublimeText/hookSublimeText/Main.m index 4d156b6..e025ce3 100644 --- a/hookSublimeText/hookSublimeText/Main.m +++ b/hookSublimeText/hookSublimeText/Main.m @@ -5,9 +5,20 @@ // Created by lovecode666 on 2020/6/11. // Copyright © 2020 mll. All rights reserved. // + #import +#include +#include +#include +#include "rd_route.h" + #import "Main.h" #import "hookSublimeText.h" +#import "NMHooker.h" + + + +typedef void (^CompletionHandler)(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError); @implementation NSObject (SublimeText) @@ -33,7 +44,7 @@ - (void)m_st_commandLine:(id)arg1 { // - PXWindow - (void)m_st_update { - NSLog(@"SublimeText m_st_update"); + // NSLog(@"SublimeText m_st_update"); [self m_st_update]; [self m_hidden_registerMarkView]; } @@ -63,7 +74,7 @@ - (void)m_hidden_registerMarkView { // Sublime Text 里有一个同名的c函数🐶 - (NSArray *)ns_title_bar_views { NSMutableArray *bars = @[].mutableCopy; - + NSArray *windows = [[NSApplication sharedApplication] windows]; for (NSWindow *win in windows) { @@ -83,37 +94,37 @@ - (id)ns_title_bar_view:(NSWindow *)win { id res = nil; NSView *contenView = [win contentView]; NSArray *views = [[contenView superview] subviews]; - + Class titleBarClass = NSClassFromString(@"NSTitlebarContainerView"); for (id item in views) { - if ([item isKindOfClass:titleBarClass]) { - res = item; - break; - - } + if ([item isKindOfClass:titleBarClass]) { + res = item; + break; + + } } - + if (res) { - NSArray *views = [res subviews]; - for (id item in views) { - if ([item isKindOfClass:NSClassFromString(@"NSTitlebarView")]) { - res = item; - break; - } - } + NSArray *views = [res subviews]; + for (id item in views) { + if ([item isKindOfClass:NSClassFromString(@"NSTitlebarView")]) { + res = item; + break; + } + } } - + return res; } - (void)m_hidden_registerMarkView:(id)titleBarView { - NSLog(@"-- hidden --"); + // NSLog(@"-- hidden --"); if (!titleBarView) { return; } NSArray *views = [titleBarView subviews]; - + for (id item in views) { if ([item isKindOfClass:NSClassFromString(@"NSTextField")]) { NSTextField *txtView = (NSTextField *)item; @@ -154,11 +165,154 @@ - (void)m_st_paste:(id)arg1 { [self m_hidden_registerMarkView]; } +// - HttpConnectionDelegate + +- (void)m_st_connection:(id)arg1 didReceiveData:(id)arg2 { + NSLog(@"SublimeText m_st_connection conn : %@", [arg1 class]); + + NSURLConnection *conn = (NSURLConnection *)arg1; + if (conn.currentRequest) { + NSURLRequest *req = conn.currentRequest; + NSString *urlStr = req.URL.absoluteString; + NSLog(@"req url : %@", urlStr); + NSLog(@"req method : %@", req.HTTPMethod); + NSLog(@"req body : %@", req.HTTPBody); + NSDictionary *headers = req.allHTTPHeaderFields; + NSLog(@"req headers : %@", headers); + } + NSString *body = [[NSString alloc] initWithData:(NSData *)arg2 encoding:NSUTF8StringEncoding]; + NSString *log2 = [NSString stringWithFormat:@"-m_st_connection--->body<---- : %@", body]; + NSLog(log2); + + [self m_st_connection:arg1 didReceiveData:arg2]; +} + +- (void)m_st_connection:(id)arg1 didReceiveResponse:(id)arg2 { + NSLog(@"SublimeText m_st_connection dataResponse : %@", [arg2 class]); + [self m_st_connection:arg1 didReceiveResponse:arg2]; +} + @end -static void __attribute__((constructor)) initialize(void) { +@implementation NSObject (NSURLConnection) +// - NSURLConnection + + +// 同步请求方法 ++ (nullable NSData *)hook_sendSynchronousRequest:(NSURLRequest *)request + returningResponse:(NSURLResponse * _Nullable * _Nullable)response + error:(NSError **)error { + NSString *url = request.URL.absoluteString; + NSString *log = [NSString stringWithFormat:@"----->url<-----:%@", url]; + NSLog(@"----------"); + + //发起请求 + NSData *data = [[self class] hook_sendSynchronousRequest:request returningResponse:response error:error]; + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)(*response); + + *response = httpResponse; + + NSLog(@"-------->over<--------"); + + NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSLog(@"---->body<---- : %@", body); + + return data; +} + +// 异步请求方法 ++ (void)hook_sendAsynchronousRequest:(NSURLRequest*)request queue:(NSOperationQueue*)queue completionHandler:(void (^)(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError)) handler { + NSLog(@"-------->start<--------"); + + CompletionHandler hook_handler = ^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; + + NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSLog(@"---->body<---- : %@", body); + + if (handler) { + NSLog(@"-------->over<--------"); + handler(httpResponse, data, connectionError); + } + }; + NSLog(@"-------->over<--------"); + [[self class] hook_sendAsynchronousRequest:request queue:queue completionHandler:hook_handler]; + return; +} + +@end + +void *(*original2)(int,int) = NULL; + +void * my_strerror_2(char a, int64_t b) +{ + NSLog(@"hooked %lld - %lld ", a, b); + return original2(a, b); +} + +intptr_t g_slide; + +//保存模块偏移基地址的值 +static void _register_func_for_add_image(const struct mach_header *header, intptr_t slide) { + Dl_info image_info; + int result = dladdr(header, &image_info); + if (result == 0) { + NSLog(@"load mach_header failed"); + return; + } + // 获取当前的可执行文件路径 + NSString *execName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; + NSString *execPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingFormat:@"/%@", execName]; + if (strcmp([execPath UTF8String], image_info.dli_fname) == 0) { + g_slide = slide; + } +} + +void *(*ori_settings_get_bool)(void*, void*, void*, void*) = NULL; + +// 处理 settings::get(v142, "update_check", ""); +bool settings_get_bool(void *arg1, void *arg2, void* arg3, bool* is) { + if (strcmp("update_check", arg2) == 0) { + *is = NULL; + return false; + } + return ori_settings_get_bool(arg1, arg2, arg3, is); +} + +uint64_t (*ori_settings_find)(void *, void *, void *) = NULL; +uint64_t settings_find(void *arg1, void *arg2, void *arg3) { + uint64_t res = ori_settings_find(arg1, arg2, arg3); + // printf(" ----> settings_find <----%s| \n", (char *)res); + return res; +} + +void *(*ori_blocking_check_for_update)(void*, void* , void*, void*) = NULL; +void blocking_check_for_update(void *arg1, void* arg2, void* arg3, void* arg4) +{ + printf("---> 请求更新 <-----"); + // ori_blocking_check_for_update(arg1, arg2, arg3, arg4); + + printf("d--->arg:%s<---\n", arg2); + printf("d--->arg:%s<---\n", arg3); + printf("d--->arg:%s<---\n", arg4); +} + +static void __attribute__((constructor)) initialize(void) { + // _dyld_register_func_for_add_image(_register_func_for_add_image); + NSLog(@"++++++++ hookSublimeText loaded ++++++++"); + + // blocking_check_for_update + // c++filt __Z25blocking_check_for_updateRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES7_P5valuePS5_ + //blocking_check_for_update(std::__1::basic_string, std::__1::allocator > const&, std::__1::basic_string, std::__1::allocator > const&, value*, std::__1::basic_string, std::__1::allocator >*) + rd_route_byname("_Z25blocking_check_for_updateRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES7_P5valuePS5_", NULL, blocking_check_for_update, (void **)&ori_blocking_check_for_update); + + // bool settings::get(substring, bool*) const + // NSString *exe = @"/Applications/Sublime Text.app/Contents/MacOS/Sublime Text"; + // rd_route_byname("_ZNK8settings3getIbEEb9substringPT_", NULL, settings_get_bool, (void **)&ori_settings_get_bool); + + // rd_route((void *)(0x100062016+g_slide), hook_testMethod, NULL); // 每次 hook 复位 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"removedMark"]; @@ -166,7 +320,7 @@ static void __attribute__((constructor)) initialize(void) { CBHookInstanceMethod(PXWindowDelegate, @selector(windowWillEnterFullScreen:), @selector(m_st_windowWillEnterFullScreen:)); CBHookInstanceMethod(PXWindowDelegate, @selector(windowWillExitFullScreen:), @selector(m_st_windowWillExitFullScreen:)); - + // CBHookInstanceMethod(PXWindowDelegate, @selector(commandLine:), @selector(m_st_commandLine:)); CBHookInstanceMethod(PXWindow, @selector(update), @selector(m_st_update)); @@ -174,4 +328,19 @@ static void __attribute__((constructor)) initialize(void) { CBHookInstanceMethod(PXApplication, @selector(terminate:), @selector(m_st_terminate:)); CBHookInstanceMethod(PXView, @selector(paste:), @selector(m_st_paste:)); + + // hook HttpConnectionDelegate + CBHookInstanceMethod(HttpConnectionDelegate, @selector(connection:didReceiveData:), @selector(m_st_connection:didReceiveData:)); + CBHookInstanceMethod(CBHookInstanceMethod, @selector(connection:didReceiveResponse:), @selector(m_st_connection:didReceiveResponse:)); + + // hook net request + // 同步请求 + // CBHookInstanceMethod(NSURLConnection, @selector(sendSynchronousRequest:returningResponse:error:), @selector(hook_sendSynchronousRequest:returningResponse:error:)); + //[NMHooker hookClass:@"NSURLConnection" sel:@"sendSynchronousRequest:returningResponse:error:" withClass:@"NSURLConnection" andSel:@"hook_sendSynchronousRequest:returningResponse:error:"]; + //CBHookClassMethod(NSURLConnection, @selector(sendSynchronousRequest:returningResponse:error:), @selector(hook_sendSynchronousRequest:returningResponse:error:)); + + //异步请求 + //[NMHooker hookClass:@"NSURLConnection" sel:@"sendAsynchronousRequest:queue:completionHandler:" withClass:@"NSURLConnection" andSel:@"hook_sendAsynchronousRequest:queue:completionHandler:"]; + // CBHookInstanceMethod(NSURLConnection, @selector(sendAsynchronousRequest:queue:completionHandler:), @selector(hook_sendAsynchronousRequest:queue:completionHandler:)); + //CBHookClassMethod(NSURLConnection, @selector(sendAsynchronousRequest:queue:completionHandler:), @selector(hook_sendAsynchronousRequest:queue:completionHandler:)); }