- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
247 lines
13 KiB
C
247 lines
13 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));
|
|
#if defined(__arm64__) || defined(__aarch64__)
|
|
void *sys_getpid = dlsym(RTLD_DEFAULT, "getpid");
|
|
void *sys_ftell = dlsym(RTLD_DEFAULT, "ftell");
|
|
assert(sys_getpid != NULL && sys_ftell != NULL);
|
|
const uint32_t local_b[1] = {0x14000000u};
|
|
assert(ig_arm64_trampoline_to_foreign(local_b, local_b, 1));
|
|
assert(ig_verify_function(sys_ftell, "ftell", collect, NULL) == IG_CLEAR);
|
|
struct { uint32_t ins[2]; uint64_t dst; } __attribute__((aligned(8))) ldr_tramp = {
|
|
{0x58000050u, 0xd61f0200u}, (uint64_t)(uintptr_t)collect
|
|
};
|
|
assert(ig_arm64_trampoline_to_foreign(ldr_tramp.ins, ldr_tramp.ins, 2));
|
|
ldr_tramp.dst = (uint64_t)(uintptr_t)sys_getpid;
|
|
assert(!ig_arm64_trampoline_to_foreign(ldr_tramp.ins, ldr_tramp.ins, 2));
|
|
uintptr_t foreign_dst = (uintptr_t)collect;
|
|
uint32_t adrp_foreign[3];
|
|
intptr_t page_delta = ((intptr_t)(foreign_dst & ~(uintptr_t)0xfffu) -
|
|
(intptr_t)((uintptr_t)adrp_foreign & ~(uintptr_t)0xfffu)) >> 12;
|
|
uint32_t imm21 = (uint32_t)page_delta & 0x1fffffu;
|
|
adrp_foreign[0] = 0x90000010u | ((imm21 & 3u) << 29) | ((imm21 >> 2) << 5);
|
|
adrp_foreign[1] = 0x91000210u | ((uint32_t)(foreign_dst & 0xfffu) << 10);
|
|
adrp_foreign[2] = 0xd61f0200u;
|
|
assert(ig_arm64_trampoline_to_foreign(adrp_foreign, adrp_foreign, 3));
|
|
assert(ig_arm64_trampoline_to_foreign(movk_win, movk_win, 8));
|
|
assert(!ig_arm64_trampoline_to_foreign(clean_win, clean_win, 8));
|
|
#endif
|
|
} else return 2;
|
|
return 0;
|
|
}
|