- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
222 lines
11 KiB
C
222 lines
11 KiB
C
#include "dispatch/IGDispatch.h"
|
|
#include "resolve/IGResolve.h"
|
|
#include "anti_hook/IGAntiHook.h"
|
|
#include <assert.h>
|
|
#include <dlfcn.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
|
|
static int early_ok;
|
|
__attribute__((constructor)) static void early(void) {
|
|
#ifdef IG_EXPECT_INIT_FAILURE
|
|
early_ok=1;
|
|
#else
|
|
int fd=open("/dev/null",O_WRONLY);
|
|
early_ok=fd>=0 && IG_dispatch_write(fd,"early",5)==5 && IG_dispatch_close(fd)==0;
|
|
#endif
|
|
}
|
|
void *ig_fixture_missing_resolve(void *handle, const char *name) { (void)handle; (void)name; return NULL; }
|
|
static int reasons[256], unavailable;
|
|
static int collect(const IGFinding *f,void *ctx) {
|
|
(void)ctx;
|
|
if (f->reason>=0 && f->reason<256) reasons[f->reason]++;
|
|
if (f->status==IG_UNAVAILABLE) unavailable++;
|
|
return 0;
|
|
}
|
|
static int fake_close(int fd) { (void)fd; abort(); }
|
|
static void *writer(void *ctx) {
|
|
(void)ctx;
|
|
int fd=open("/dev/null",O_WRONLY);
|
|
assert(fd>=0);
|
|
for (int i=0;i<100;i++) assert(IG_dispatch_write(fd,"abc",3)==3);
|
|
assert(IG_dispatch_close(fd)==0);
|
|
return NULL;
|
|
}
|
|
static void signal_handler(int sig) { (void)sig; }
|
|
static pthread_t main_thread;
|
|
static void *interruptor(void *ctx) { (void)ctx; usleep(30000); pthread_kill(main_thread,SIGUSR1); return NULL; }
|
|
|
|
int main(int argc,char **argv) {
|
|
assert(argc==2 && early_ok);
|
|
if (!strcmp(argv[1],"observation")) {
|
|
#ifdef IG_OBSERVATION
|
|
IGDispatchObservation before=ig_dispatch_observation();
|
|
errno=0; assert(IG_dispatch_write(-1,"x",1)==-1 && errno==EBADF);
|
|
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EBADF);
|
|
IGDispatchObservation after=ig_dispatch_observation();
|
|
assert(after.write_attempted==before.write_attempted+1);
|
|
assert(after.write_dispatched==before.write_dispatched+1);
|
|
assert(after.close_attempted==before.close_attempted+1);
|
|
assert(after.close_dispatched==before.close_dispatched+1);
|
|
ig_dispatch_test_override(1,(void *)fake_close);
|
|
assert(IG_dispatch_close(-1)==-1 && errno==EACCES);
|
|
IGDispatchObservation rejected=ig_dispatch_observation();
|
|
assert(rejected.close_attempted==after.close_attempted+1);
|
|
assert(rejected.close_dispatched==after.close_dispatched);
|
|
#else
|
|
return 2;
|
|
#endif
|
|
} else if (!strcmp(argv[1],"init-failure")) {
|
|
assert(IG_dispatch_write(-1,"x",1)==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_close(-1)==-1 && errno==ENOSYS);
|
|
char b;
|
|
assert(IG_dispatch_read(-1,&b,1)==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_open("/dev/null",O_RDONLY)==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_unlink("/tmp/ig-must-not-remove")==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_getpid()==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_fork()==-1 && errno==ENOSYS);
|
|
assert(IG_dispatch_dlopen(NULL,RTLD_LAZY)==NULL && errno==ENOSYS);
|
|
assert(IG_dispatch_dlsym(RTLD_DEFAULT,"getpid")==NULL && errno==ENOSYS);
|
|
assert(ig_dispatch_check(collect,NULL)==0 && unavailable==11);
|
|
assert(reasons[IG_REASON_DISPATCH_UNAVAILABLE]==11);
|
|
} else if (!strcmp(argv[1],"extended")) {
|
|
char path[]="/tmp/ig-dispatch-XXXXXX";
|
|
int fd=mkstemp(path); assert(fd>=0); close(fd); unlink(path);
|
|
mode_t mask=umask(0);
|
|
errno=EDOM; fd=IG_dispatch_open(path,O_CREAT|O_EXCL|O_RDWR,0600);
|
|
umask(mask);
|
|
assert(fd>=0 && errno==EDOM);
|
|
struct stat st; assert(fstat(fd,&st)==0 && (st.st_mode & 0777)==0600);
|
|
assert(IG_dispatch_write(fd,"abc",3)==3); assert(lseek(fd,0,SEEK_SET)==0);
|
|
char bytes[16]={0}; errno=ERANGE;
|
|
assert(IG_dispatch_read(fd,bytes,sizeof(bytes))==3 && !memcmp(bytes,"abc",3) && errno==ERANGE);
|
|
assert(IG_dispatch_read(fd,bytes,sizeof(bytes))==0);
|
|
assert(IG_dispatch_close(fd)==0);
|
|
fd=IG_dispatch_open(path,O_RDONLY); assert(fd>=0); assert(IG_dispatch_close(fd)==0);
|
|
assert(IG_dispatch_open(path,O_CREAT|O_EXCL|O_RDWR,0600)==-1 && errno==EEXIST);
|
|
FILE *stream=IG_dispatch_fopen(path,"r");
|
|
assert(stream && fgetc(stream)=='a' && fclose(stream)==0);
|
|
assert(IG_dispatch_unlink(path)==0);
|
|
assert(IG_dispatch_fopen(path,"r")==NULL && errno==ENOENT);
|
|
assert(IG_dispatch_open(path,O_RDONLY)==-1 && errno==ENOENT);
|
|
assert(IG_dispatch_unlink(path)==-1 && errno==ENOENT);
|
|
assert(IG_dispatch_read(-1,bytes,1)==-1 && errno==EBADF);
|
|
errno=EDOM; assert(IG_dispatch_getpid()==getpid() && errno==EDOM);
|
|
void *self_handle=IG_dispatch_dlopen(NULL,RTLD_LAZY);
|
|
assert(self_handle!=NULL);
|
|
assert(IG_dispatch_dlsym(RTLD_DEFAULT,"getpid")==dlsym(RTLD_DEFAULT,"getpid"));
|
|
pid_t child=IG_dispatch_fork();
|
|
assert(child>=0);
|
|
if (child==0) IG_dispatch_exit(42);
|
|
int wstatus=0;
|
|
assert(waitpid(child,&wstatus,0)==child && WIFEXITED(wstatus) && WEXITSTATUS(wstatus)==42);
|
|
} else if (!strcmp(argv[1],"extended-tamper")) {
|
|
pid_t child=fork();
|
|
assert(child>=0);
|
|
if (child==0) {
|
|
ig_dispatch_test_override(7,(void *)fake_close);
|
|
IG_dispatch_exit(37);
|
|
}
|
|
int wstatus=0;
|
|
assert(waitpid(child,&wstatus,0)==child && WIFEXITED(wstatus) && WEXITSTATUS(wstatus)==37);
|
|
for (unsigned i=2;i<11;i++) ig_dispatch_test_override(i,(void *)fake_close);
|
|
char bytes[1];
|
|
assert(IG_dispatch_read(-1,bytes,1)==-1 && errno==EACCES);
|
|
assert(IG_dispatch_open("/dev/null",O_RDONLY)==-1 && errno==EACCES);
|
|
assert(IG_dispatch_unlink("/tmp/ig-must-not-remove")==-1 && errno==EACCES);
|
|
assert(IG_dispatch_getpid()==-1 && errno==EACCES);
|
|
assert(IG_dispatch_fopen("/dev/null","r")==NULL && errno==EACCES);
|
|
assert(IG_dispatch_fork()==-1 && errno==EACCES);
|
|
assert(IG_dispatch_dlopen(NULL,RTLD_LAZY)==NULL && errno==EACCES);
|
|
assert(IG_dispatch_dlsym(RTLD_DEFAULT,"getpid")==NULL && errno==EACCES);
|
|
assert(ig_dispatch_check(collect,NULL)==9 && reasons[IG_REASON_DISPATCH_TAMPER]==9);
|
|
} else if (!strcmp(argv[1],"read-eintr")) {
|
|
int p[2]; assert(pipe(p)==0);
|
|
struct sigaction action={0};action.sa_handler=signal_handler;sigemptyset(&action.sa_mask);
|
|
assert(sigaction(SIGUSR1,&action,NULL)==0);
|
|
main_thread=pthread_self();pthread_t thread;
|
|
assert(pthread_create(&thread,NULL,interruptor,NULL)==0);
|
|
char b; assert(IG_dispatch_read(p[0],&b,1)==-1 && errno==EINTR);
|
|
pthread_join(thread,NULL);IG_dispatch_close(p[1]);IG_dispatch_close(p[0]);
|
|
} else if (!strcmp(argv[1],"semantics")) {
|
|
assert(ig_dispatch_check(collect,NULL)==0 && unavailable==0);
|
|
errno=0; assert(IG_dispatch_write(-1,"x",1)==-1 && errno==EBADF);
|
|
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EBADF);
|
|
int p[2]; assert(pipe(p)==0);
|
|
errno=EDOM; assert(IG_dispatch_write(p[1],"abc",3)==3 && errno==EDOM);
|
|
char b[4]={0}; assert(read(p[0],b,3)==3 && !memcmp(b,"abc",3));
|
|
assert(IG_dispatch_close(p[1])==0 && IG_dispatch_close(p[0])==0);
|
|
} else if (!strcmp(argv[1],"short-write")) {
|
|
int p[2]; assert(pipe(p)==0);
|
|
assert(fcntl(p[1],F_SETFL,O_NONBLOCK)==0);
|
|
const size_t count=1024*1024;
|
|
char *bytes=calloc(count,1);assert(bytes);
|
|
ssize_t n=IG_dispatch_write(p[1],bytes,count);
|
|
assert(n>0 && n<(ssize_t)count);
|
|
while (IG_dispatch_write(p[1],bytes,count)>0) {}
|
|
assert(errno==EAGAIN);
|
|
IG_dispatch_close(p[1]); IG_dispatch_close(p[0]);
|
|
free(bytes);
|
|
} else if (!strcmp(argv[1],"eintr")) {
|
|
int p[2]; assert(pipe(p)==0);
|
|
assert(fcntl(p[1],F_SETFL,O_NONBLOCK)==0);
|
|
char bytes[4096]={0}; while (write(p[1],bytes,sizeof(bytes))>0) {}
|
|
assert(errno==EAGAIN && fcntl(p[1],F_SETFL,0)==0);
|
|
struct sigaction action={0};action.sa_handler=signal_handler;sigemptyset(&action.sa_mask);
|
|
assert(sigaction(SIGUSR1,&action,NULL)==0);
|
|
main_thread=pthread_self();pthread_t thread;
|
|
assert(pthread_create(&thread,NULL,interruptor,NULL)==0);
|
|
assert(IG_dispatch_write(p[1],bytes,sizeof(bytes))==-1 && errno==EINTR);
|
|
pthread_join(thread,NULL);IG_dispatch_close(p[1]);IG_dispatch_close(p[0]);
|
|
} else if (!strcmp(argv[1],"threads")) {
|
|
pthread_t threads[8];
|
|
for (unsigned i=0;i<8;i++) assert(pthread_create(&threads[i],NULL,writer,NULL)==0);
|
|
for (unsigned i=0;i<8;i++) pthread_join(threads[i],NULL);
|
|
} else if (!strcmp(argv[1],"tamper")) {
|
|
ig_dispatch_test_override(1,(void *)fake_close);
|
|
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EACCES);
|
|
assert(ig_dispatch_check(collect,NULL)==1 && reasons[IG_REASON_DISPATCH_TAMPER]==1);
|
|
} else if (!strcmp(argv[1],"reentry")) {
|
|
ig_dispatch_test_reentry(1);
|
|
assert(IG_dispatch_close(-1)==-1 && errno==EDEADLK);
|
|
ig_dispatch_test_reentry(0);
|
|
} else if (!strcmp(argv[1],"resolver")) {
|
|
unsigned char encoded[300];memset(encoded,'x',sizeof(encoded));char out[4];
|
|
assert(!ig_decrypt(encoded,1,out,0));assert(!ig_decrypt(NULL,1,out,sizeof(out)));
|
|
assert(!ig_decrypt(encoded,4,out,sizeof(out)) && out[0]==0);
|
|
encoded[0]=IG_ENC_KEY;assert(!ig_decrypt(encoded,1,out,sizeof(out)));
|
|
assert(ig_resolve_verified(NULL,encoded,300,NULL,NULL).status==IG_UNAVAILABLE);
|
|
const char *name="IG_missing_symbol_774444";
|
|
for (unsigned i=0;i<strlen(name);i++) encoded[i]=name[i]^IG_ENC_KEY;
|
|
assert(ig_resolve_verified(NULL,encoded,strlen(name),collect,NULL).status==IG_UNAVAILABLE);
|
|
assert(reasons[IG_REASON_SYMBOL_UNRESOLVED]==1);
|
|
assert(ig_verify_function((void *)1,"invalid",collect,NULL)==IG_UNAVAILABLE);
|
|
assert(reasons[IG_REASON_VERIFICATION_UNAVAILABLE]==1);
|
|
} else if (!strcmp(argv[1],"prologue")) {
|
|
assert(ig_arm64_suspicious_prologue(0x14000001,0));
|
|
assert(ig_arm64_suspicious_prologue(0x58000050,0xd61f0200));
|
|
assert(ig_arm64_suspicious_prologue(0x58000049,0xd61f0120)); /* ldr x9; br x9 */
|
|
assert(!ig_arm64_suspicious_prologue(0x58000050,0xd61f0220));
|
|
assert(!ig_arm64_suspicious_prologue(0x18000050,0xd61f0200));
|
|
assert(!ig_arm64_suspicious_prologue(0xd503245f,0xd65f03c0));
|
|
const uint32_t clean_win[8] = {
|
|
0xd503237fu, 0xa9bf7bfdu, 0x910003fdu, 0xaa0003f3u,
|
|
0xf100001fu, 0x54000080u, 0xd503201fu, 0xd65f03c0u
|
|
};
|
|
assert(!ig_arm64_suspicious_window(clean_win, 8));
|
|
const uint32_t adrp_win[8] = {
|
|
0xd503237fu, 0x9000000au, 0x9100414au, 0xd61f0140u,
|
|
0xd503201fu, 0xd503201fu, 0xd503201fu, 0xd503201fu
|
|
};
|
|
assert(ig_arm64_suspicious_window(adrp_win, 8));
|
|
const uint32_t movk_win[8] = {
|
|
0xd280000bu, 0xf2c2468bu, 0xd61f0160u, 0xd503201fu,
|
|
0xd503201fu, 0xd503201fu, 0xd503201fu, 0xd503201fu
|
|
};
|
|
assert(ig_arm64_suspicious_window(movk_win, 8));
|
|
const uint32_t mid_ldr_win[8] = {
|
|
0xd503237fu, 0xa9bf7bfdu, 0x910003fdu, 0x58000050u,
|
|
0xd61f0200u, 0xd503201fu, 0xd503201fu, 0xd503201fu
|
|
};
|
|
assert(ig_arm64_suspicious_window(mid_ldr_win, 8));
|
|
} else return 2;
|
|
return 0;
|
|
}
|