add extract_syms; rename decrypt_kern to unpack and improve it

This commit is contained in:
comex 2011-08-08 23:48:52 -04:00
parent d5e0033ffb
commit 42cc24894d
4 changed files with 86 additions and 17 deletions

View File

@ -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

View File

@ -1,15 +0,0 @@
#include <data/cc.h>
int main(int argc, char **argv) {
if(argc != 5) {
fprintf(stderr, "Usage: decrypt_kern <img3> <key> <iv> <outfile>\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;
}

70
extract_syms.c Normal file
View File

@ -0,0 +1,70 @@
/* how trivial...
extract the symbols into a new mach-o
that contains just the symbols */
#include <data/mach-o/binary.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
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);
}

12
unpack.c Normal file
View File

@ -0,0 +1,12 @@
#include <data/cc.h>
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 <infile> <outfile> [<key> <iv> | <arch>]\n");
return 1;
}