diff --git a/Makefile b/Makefile index 18b5222..0c3856f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ DATA = $(word 1,$(wildcard ./data ../data)) override CFLAGS += -I$(DATA) include $(DATA)/Makefile.common -BINS := $(OUTDIR)/check_sanity $(OUTDIR)/make_kernel_patchfile $(OUTDIR)/apply_patchfile $(OUTDIR)/dump $(OUTDIR)/nm $(OUTDIR)/decrypt_kern sandboxc-armv6.c sandboxc-armv7.c +BINS := $(OUTDIR)/check_sanity $(OUTDIR)/make_kernel_patchfile $(OUTDIR)/apply_patchfile $(OUTDIR)/dump $(OUTDIR)/nm $(OUTDIR)/extract_syms $(OUTDIR)/unpack sandboxc-armv6.c sandboxc-armv7.c ifneq "$(GXX)" "" BINS += $(OUTDIR)/grapher endif @@ -29,9 +29,11 @@ $(OUTDIR)/dump: $(OUTDIR)/dump.o $(DATA)/$(OUTDIR)/libdata.a $(GCC) -o $@ $^ $(OUTDIR)/nm: $(OUTDIR)/nm.o $(DATA)/$(OUTDIR)/libdata.a $(GCC) -o $@ $^ +$(OUTDIR)/extract_syms: $(OUTDIR)/extract_syms.o $(DATA)/$(OUTDIR)/libdata.a + $(GCC) -o $@ $^ $(OUTDIR)/grapher: $(OUTDIR)/grapher.o $(DATA)/$(OUTDIR)/libdata.a $(GXX) -o $@ $^ -O3 -$(OUTDIR)/decrypt_kern: $(OUTDIR)/decrypt_kern.o $(DATA)/$(OUTDIR)/libdata.a +$(OUTDIR)/unpack: $(OUTDIR)/unpack.o $(DATA)/$(OUTDIR)/libdata.a $(GCC) -o $@ $^ -O3 $(OUTDIR)/codesign_allocate: $(OUTDIR)/codesign_allocate.o $(GCC) -o $@ $^ -O3 diff --git a/decrypt_kern.c b/decrypt_kern.c deleted file mode 100644 index 8afd049..0000000 --- a/decrypt_kern.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -int main(int argc, char **argv) { - if(argc != 5) { - fprintf(stderr, "Usage: decrypt_kern \n"); - return 1; - } - uint32_t key_bits; - char *kern_fn; - prange_t data = parse_img3(load_file(kern_fn = argv[1], false, NULL), &key_bits); - prange_t key = parse_hex_string(argv[2]); - prange_t iv = parse_hex_string(argv[3]); - prange_t decompressed = decrypt_and_decompress(key_bits, key, iv, data); - store_file(decompressed, argv[4], 0644); - return 0; -} diff --git a/extract_syms.c b/extract_syms.c new file mode 100644 index 0000000..aa3cb8f --- /dev/null +++ b/extract_syms.c @@ -0,0 +1,70 @@ +/* how trivial... +extract the symbols into a new mach-o +that contains just the symbols */ + +#include +#include +#include +#include +#include +#include + +struct header { + struct mach_header mh; + struct segment_command segment; + struct symtab_command symtab; + struct nlist nl[0]; +} __attribute__((packed)); + +int main(int argc, char **argv) { + assert(argc == 3); + + struct binary binary; + b_init(&binary); + b_load_macho(&binary, argv[1]); + + int out = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0755); + assert(out != -1); + + int size = sizeof(struct header) + binary.mach->ext_nsyms * sizeof(struct nlist); + struct header *hdr = calloc(1, size); + lseek(out, size + 1, SEEK_SET); + + int off = 1; + + for(uint32_t i = 0; i < binary.mach->ext_nsyms; i++) { + hdr->nl[i] = binary.mach->ext_symtab[i]; + const char *name = binary.mach->strtab + hdr->nl[i].n_un.n_strx; + int diff = strlen(name) + 1; + hdr->nl[i].n_un.n_strx = off; + assert(write(out, name, diff) == diff); + off += diff; + + } + size_t end = lseek(out, 0, SEEK_CUR); + + memcpy(&hdr->mh, binary.mach->hdr, sizeof(hdr->mh)); + hdr->mh.ncmds = 2; + hdr->mh.sizeofcmds = sizeof(*hdr) - sizeof(hdr->mh); + + hdr->segment.cmd = LC_SEGMENT; + hdr->segment.cmdsize = sizeof(hdr->segment); + strcpy(hdr->segment.segname, "__LINKEDIT"); + hdr->segment.vmaddr = 0; + hdr->segment.vmsize = (end + 0xfff) & ~0xfff; + hdr->segment.fileoff = 0; + hdr->segment.filesize = end; + hdr->segment.maxprot = PROT_READ | PROT_EXEC; + hdr->segment.initprot = PROT_READ | PROT_EXEC; + hdr->segment.nsects = 0; + hdr->segment.flags = 0; + + hdr->symtab.cmd = LC_SYMTAB; + hdr->symtab.cmdsize = sizeof(hdr->symtab); + hdr->symtab.symoff = sizeof(*hdr); + hdr->symtab.nsyms = binary.mach->ext_nsyms; + hdr->symtab.stroff = size; + hdr->symtab.strsize = end - size; + + assert(pwrite(out, hdr, size, 0) == size); +} diff --git a/unpack.c b/unpack.c new file mode 100644 index 0000000..700162e --- /dev/null +++ b/unpack.c @@ -0,0 +1,12 @@ +#include +int main(int argc, char **argv) { + if(argc < 3 || argc > 5) goto usage; + + prange_t data = unpack(load_file(argv[1], false, NULL), argv[3], argv[4]); + store_file(data, argv[2], 0644); + return 0; + + usage: + fprintf(stderr, "Usage: unpack [ | ]\n"); + return 1; +}