-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable PT_DENY_ATTACH and sysctl debugger checking
- Loading branch information
wutian
committed
Mar 23, 2017
1 parent
54a02d6
commit b2efc00
Showing
7 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// IPAPatchBypassAntiDebugging.h | ||
// IPAPatch | ||
// | ||
// Created by wutian on 2017/3/23. | ||
// Copyright © 2017年 Weibo. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface IPAPatchBypassAntiDebugging : NSObject | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// | ||
// IPAPatchBypassAntiDebugging.m | ||
// IPAPatch | ||
// | ||
// Created by wutian on 2017/3/23. | ||
// Copyright © 2017年 Weibo. All rights reserved. | ||
// | ||
|
||
#import "IPAPatchBypassAntiDebugging.h" | ||
#import "fishhook.h" | ||
#import <dlfcn.h> | ||
#import <sys/sysctl.h> | ||
|
||
#define TESTS_BYPASS 0 | ||
|
||
// Sources: | ||
// https://www.coredump.gr/articles/ios-anti-debugging-protections-part-1/ | ||
// https://www.coredump.gr/articles/ios-anti-debugging-protections-part-2/ | ||
// https://www.theiphonewiki.com/wiki/Bugging_Debuggers | ||
|
||
// Bypassing PT_DENY_ATTACH technique | ||
|
||
static void * (*original_dlsym)(void *, const char *); | ||
|
||
int fake_ptrace(int _request, pid_t _pid, caddr_t _addr, int _data) | ||
{ | ||
return 0; | ||
} | ||
|
||
void * hooked_dlsym(void * __handle, const char * __symbol) | ||
{ | ||
if (strcmp(__symbol, "ptrace") == 0) { | ||
return &fake_ptrace; | ||
} | ||
|
||
return original_dlsym(__handle, __symbol); | ||
} | ||
|
||
static void disable_pt_deny_attach() | ||
{ | ||
original_dlsym = dlsym(RTLD_DEFAULT, "dlsym"); | ||
rebind_symbols((struct rebinding[1]){{"dlsym", hooked_dlsym}}, 1); | ||
} | ||
|
||
// Bypassing sysctl debugger checking technique | ||
|
||
static int (*original_sysctl)(int *, u_int, void *, size_t *, void *, size_t); | ||
|
||
typedef struct kinfo_proc ipa_kinfo_proc; | ||
|
||
int hooked_sysctl(int * arg0, u_int arg1, void * arg2, size_t * arg3, void * arg4, size_t arg5) | ||
{ | ||
bool modify_needed = arg1 == 4 && arg0[0] == CTL_KERN && arg0[1] == KERN_PROC && arg0[2] == KERN_PROC_PID && arg2 && arg3 && (*arg3 >= sizeof(struct kinfo_proc)); | ||
|
||
int ret = original_sysctl(arg0, arg1, arg2, arg3, arg4, arg5); | ||
|
||
if (modify_needed) { | ||
ipa_kinfo_proc * pointer = arg2; | ||
ipa_kinfo_proc info = *pointer; | ||
info.kp_proc.p_flag = 0; | ||
*pointer = info; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
static void disable_sysctl_debugger_checking() | ||
{ | ||
original_sysctl = dlsym(RTLD_DEFAULT, "sysctl"); | ||
rebind_symbols((struct rebinding[1]){{"sysctl", hooked_sysctl}}, 1); | ||
} | ||
|
||
#if TESTS_BYPASS | ||
// Tests | ||
static void test_aniti_debugger(); | ||
#endif | ||
|
||
@implementation IPAPatchBypassAntiDebugging | ||
|
||
+ (void)load | ||
{ | ||
disable_pt_deny_attach(); | ||
disable_sysctl_debugger_checking(); | ||
|
||
#if TESTS_BYPASS | ||
test_aniti_debugger(); | ||
#endif | ||
} | ||
|
||
@end | ||
|
||
#if TESTS_BYPASS | ||
|
||
typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data); | ||
|
||
#if !defined(PT_DENY_ATTACH) | ||
#define PT_DENY_ATTACH 31 | ||
#endif | ||
|
||
static void test_aniti_debugger() | ||
{ | ||
void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW); | ||
ptrace_ptr_t ptrace_ptr = dlsym(handle, "ptrace"); | ||
ptrace_ptr(PT_DENY_ATTACH, 0, 0, 0); | ||
dlclose(handle); | ||
|
||
int name[4]; | ||
struct kinfo_proc info; | ||
size_t info_size = sizeof(info); | ||
|
||
info.kp_proc.p_flag = 0; | ||
|
||
name[0] = CTL_KERN; | ||
name[1] = KERN_PROC; | ||
name[2] = KERN_PROC_PID; | ||
name[3] = getpid(); | ||
|
||
if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) { | ||
perror("sysctl"); | ||
exit(-1); | ||
} | ||
bool debugging = ((info.kp_proc.p_flag & P_TRACED) != 0); | ||
|
||
NSCAssert(!debugging, @"Debug checking should be disabled"); | ||
} | ||
|
||
#endif // TESTS_BYPASS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2013, Facebook, Inc. | ||
// All rights reserved. | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// * Neither the name Facebook nor the names of its contributors may be used to | ||
// endorse or promote products derived from this software without specific | ||
// prior written permission. | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Oops, something went wrong.