Esempio n. 1
0
void test(void)
{
    gadget_t gadgets;
    gadget_init(&gadgets);
    gadget_add(&gadgets, (unsigned char*)TEST_INSTRS, TEST_INSTRS_LEN, 0);
    gadget_print(&gadgets);
    gadget_destroy(&gadgets);
}
Esempio n. 2
0
void gadget_destroy(gadget_t *gadgets)
{
    /* Destroy all child nodes (if any). */
    if (gadgets->previous.head != NULL) {
        gadget_list_item_t *cursor = gadgets->previous.head;
        while (cursor != NULL) {
            gadget_destroy(cursor->gadget);
            free(cursor->gadget);
            cursor = cursor->next;
        }
    }

    /* Destroy the linked-list of children. */
    gadget_list_destroy(&gadgets->previous);
}
Esempio n. 3
0
// allocate new gadget and copy old
struct gadget_t *gadget_new_copy(struct gadget_t *gadget) {
    struct gadget_t *copy;

    //
    if (!gadget) {
        debug_printf (MESSAGE_ERROR, stderr, "error: gadget_new_copy(): gadget was null\n");
        return NULL;
    }

    // allocs
    copy = gadget_new();
    if (!copy) {
        debug_printf (MESSAGE_ERROR, stderr, "error: gadget_new_copy(): copy was not allocated\n");
        return NULL;
    }
    copy->repr = calloc(gadget->sz_repr, sizeof(*copy->repr));
    copy->bytes = calloc(gadget->sz_bytes, sizeof(*copy->bytes));

    // if one of the alloc failed
    // then copied object failed
    if (copy->repr == NULL || copy->bytes == NULL) {
        debug_printf (MESSAGE_ERROR, stderr, "error: gadget_new_copy(): failed bytes or repr allocation\n");
        free(copy->repr);
        free(copy->bytes);

        return NULL;
    }

    // if copy failed
    // then bye
    if (gadget_copy(copy, gadget) == NULL) {
        debug_printf (MESSAGE_ERROR, stderr, "error: gadget_new_copy(): failed copy\n");
        gadget_destroy(&copy);
        return NULL;
    }

    return copy;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
    if (argc != 2) {
        print_usage();
        return 1;
    }

    if (strcmp(argv[1], "demo") == 0) {
        test();
        return 0;
    }


    /* Code below adapted from 'libelf by Example' */
    Elf *e;
    GElf_Phdr phdr;
    int fd;
    size_t n, i;

    if (elf_version(EV_CURRENT) == EV_NONE) {
        printf("Error with elf library [%s]\n", elf_errmsg(-1));
        exit(1);
    }

    if ((fd = open(argv[1], O_RDONLY, 0)) < 0) {
        printf("Error reading file.\n");
        exit(1);
    }

    if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
        printf("elf_begin() failed [%s]\n", elf_errmsg(-1));
        exit(1);
    }

    if (elf_kind(e) != ELF_K_ELF) {
        printf("File is not an ELF object.\n");
        exit(1);
    }

    if (elf_getphdrnum(e, &n) != 0) {
        printf("elf_getphdrnum() failed [%s]\n", elf_errmsg(-1));
        exit(1);
    }

    /* Initialize libdis */
    x86_init(opt_none, NULL, NULL);

    gadget_t gadgets;
    gadget_init(&gadgets);

    /* Loop over each program header (segment) */
    for (i = 0; i < n; i++) {
        if (gelf_getphdr(e, i, &phdr) != &phdr) {
            printf("gelf_getphdr() failed [%s]\n", elf_errmsg(-1));
            exit(1);
        }

        /* If the segment is loaded into memory AND is executable */
        if (phdr.p_type == PT_LOAD && phdr.p_flags & PF_X) {
            /* Load the segment from the ELF file */
            unsigned char *buffer = malloc(phdr.p_filesz);
            lseek(fd, phdr.p_offset, SEEK_SET);
            ssize_t count = read(fd, buffer, phdr.p_filesz);
            if (count != phdr.p_filesz) {
                printf("read sucks.\n"); // FIXME: EINTR etc.
                exit(1);
            }
            /* Add all gadgets in the segment to our gadget tree */
            gadget_add(&gadgets, buffer, phdr.p_filesz, phdr.p_vaddr);
            free(buffer);
        }
    }

    elf_end(e);
    close(fd);

    gadget_print(&gadgets);
    gadget_destroy(&gadgets);

    return 0;
}