AppleM1上的ARM64代码注入导致EXC_BAD_ACCESS崩溃。
创始人
2024-09-10 19:02:06
0

这个问题的一个解决方法是使用沙盒环境来注入代码。下面是一个示例代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PATH_MAX 1024

static char *resolve_binary_path(pid_t pid, char *buff, int len)
{
    int mib[4];
    size_t size;
    int ret;

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROCARGS2;
    mib[2] = pid;
    mib[3] = ARG_MAX;

    size = len;

    ret = sysctl(mib, 4, buff, &size, NULL, 0);
    if (ret < 0) {
        perror("sysctl KERN_PROCARGS2");
        return NULL;
    }

    return buff + sizeof(int);
}

int inject_with_sandbox(char *proc_path, char *shared_obj_path,
                        char *class_hash, int hash_len)
{
    pid_t pid = getpid();
    char buff[PATH_MAX];
    char *binary_path = resolve_binary_path(pid, buff, PATH_MAX);
    if (binary_path == NULL) {
        printf("Error: could not resolve binary path\n");
        return -1;
    }

    printf("Binary path: %s\n", binary_path);

    mach_port_t task;
    mach_error_t error = task_for_pid(current_task(), pid, &task);
    if (error != KERN_SUCCESS) {
        printf("Error: could not get task for pid %d (error %s)\n", pid,
                mach_error_string(error));
        return -1;
    }

    vm_offset_t target_addr;
    error = vm_map_find(target_task, VM_OBJECT_NULL, 0, &target_addr, PAGE_SIZE,
            TRUE, VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE,
            VM_INHERIT_DEFAULT);
    if (error != KERN_SUCCESS) {
        printf("Error: could not allocate target memory (error %s)\n",
                mach_error_string(error));
        return -1;
    }

    vm_size_t size;
    int fd = open(shared_obj_path, O_RDONLY);
    if (fd < 0) {
        printf("Error: could not open shared object file %s\n", shared_obj_path);
        return -1;
    }
    struct stat stbuf;
    if (fstat(fd, &stbuf) != 0) {
        printf("Error: could not determine size of shared object file %s\n", shared_obj_path);
        return -1;
    }
    size = stbuf.st_size;

    void *mapping = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (mapping == MAP_FAILED) {
        printf("Error: could not map shared object file %s\n", shared_obj_path);
        return -1;
    }

    memcpy((void *)target_addr, mapping, size);
    munmap(mapping, size);
    close(fd);

    error = mach_vm_write(task, (vm_address_t)binary_path, (vm_offset_t)class_hash,
            (mach_msg_size_t)hash_len);
    if (error != KERN_SUCCESS) {
        printf("Error: could not write to binary path %s (error %s)\n",
                binary_path, mach_error_string(error));
        return -1;
    }

    printf("Class hash injected\n");
    return 0

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...