Skip to content

Instantly share code, notes, and snippets.

@lukas2511
Last active March 20, 2019 20:32
Show Gist options
  • Select an option

  • Save lukas2511/ffd6a30a4ba3b465745cdfc9d3d80c72 to your computer and use it in GitHub Desktop.

Select an option

Save lukas2511/ffd6a30a4ba3b465745cdfc9d3d80c72 to your computer and use it in GitHub Desktop.

Revisions

  1. lukas2511 revised this gist Mar 20, 2019. 1 changed file with 0 additions and 0 deletions.
    Empty file.
  2. lukas2511 revised this gist Mar 20, 2019. No changes.
  3. lukas2511 revised this gist Mar 20, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions strip.py
    Original file line number Diff line number Diff line change
    @@ -7,17 +7,17 @@

    is_64bit = binary[0x04] == 0x02

    elf_header_names = ['e_ident[EI_MAG]', 'e_ident[EI_CLASS]', 'e_ident[EI_DATA]', 'e_ident[EI_VERSION]', 'e_ident[EI_OSABI]', 'e_ident[EI_ABIVERSION]', 'e_type', 'e_machine', 'e_version', 'e_entry', 'e_phoff', 'e_shoff', 'e_flags', 'e_ehsize', 'e_phentsize', 'e_phnum', 'e_shentsize', 'e_shnum', 'e_shstrndx']
    elf_header_names = ['e_ident[EI_MAG]', 'e_ident[EI_CLASS]', 'e_ident[EI_DATA]', 'e_ident[EI_VERSION]', 'e_ident[EI_OSABI]', 'e_ident[EI_ABIVERSION]', 'e_ident[EI_RESERVED]', 'e_type', 'e_machine', 'e_version', 'e_entry', 'e_phoff', 'e_shoff', 'e_flags', 'e_ehsize', 'e_phentsize', 'e_phnum', 'e_shentsize', 'e_shnum', 'e_shstrndx']

    elf_header = {}
    program_header = {}

    if is_64bit:
    elf_header_format = "<IBBBBBxxxxxxxHHIQQQIHHHHHH"
    elf_header_format = "<IBBBBB7sHHIQQQIHHHHHH"
    program_header_format = "<IIQQQQQQ"
    program_header_names = ['p_type', 'p_flags', 'p_offset', 'p_vaddr', 'p_paddr', 'p_filesz', 'p_memsz', 'p_align']
    else:
    elf_header_format = "<IBBBBBxxxxxxxHHIIIIIHHHHHH"
    elf_header_format = "<IBBBBB7sHHIIIIIHHHHHH"
    program_header_format = "<IIIIIIII"
    program_header_names = ['p_type', 'p_offset', 'p_vaddr', 'p_paddr', 'p_filesz', 'p_memsz', 'p_flags', 'p_align']

    @@ -38,6 +38,7 @@

    elf_header['e_entry'] -= program_header_length
    elf_header['e_phnum'] = 1
    elf_header['e_ident[EI_RESERVED]'] = b"METTIGL"

    program_header['p_offset'] -= program_header_length
    program_header['p_vaddr'] -= program_header_length
  4. lukas2511 revised this gist Mar 20, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion strip.py
    Original file line number Diff line number Diff line change
    @@ -51,9 +51,11 @@

    new_elf_header = struct.pack(elf_header_format, *elf_header_values)
    new_program_header = struct.pack(program_header_format, *program_header_values)
    new_binary = new_elf_header + new_program_header + program

    print("ELF header size: %d" % len(new_elf_header))
    print("Program header size: %d" % len(new_program_header))
    print("Program size: %d" % len(program))
    print("Total size: %d" % len(new_binary))

    open(sys.argv[1], "wb").write(new_elf_header + new_program_header + program)
    open(sys.argv[1], "wb").write(new_binary)
  5. lukas2511 revised this gist Mar 20, 2019. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,16 @@
    int main();

    void exit(int x) {
    while(1) {
    asm("movl %0, %%ebx" : : "r"(x) :);
    asm("movl $1, %eax");
    asm("int $0x80");
    }
    asm("movl %0, %%ebx" : : "r"(x) :);
    asm("movl $1, %eax");
    asm("int $0x80");
    while(1);
    }

    void _start() {
    exit(42);
    exit(main());
    }

    int main() {
    return 42;
    }
  6. lukas2511 revised this gist Mar 20, 2019. 3 changed files with 63 additions and 19 deletions.
    8 changes: 6 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    main: main.c Makefile strip.py
    gcc -o main main.c -nodefaultlibs -nostdlib -m32 -nostartfiles -Os -s -static -Wl,--build-id=none -Wl,--nmagic
    BITS ?= 32

    .PHONY: main clean test

    main:
    gcc -o main main.c -nodefaultlibs -nostdlib -m$(BITS) -nostartfiles -Os -s -static -Wl,--build-id=none -Wl,--nmagic
    strip --remove-section=.comment --remove-section=.eh_frame --strip-all main
    sstrip -z main
    python strip.py main
    10 changes: 8 additions & 2 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    #define EXIT(x) { __asm__("movl $1, %eax; movl $" x ", %ebx; int $0x80"); }
    void exit(int x) {
    while(1) {
    asm("movl %0, %%ebx" : : "r"(x) :);
    asm("movl $1, %eax");
    asm("int $0x80");
    }
    }

    void _start() {
    EXIT("42");
    exit(42);
    }
    64 changes: 49 additions & 15 deletions strip.py
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,59 @@
    #!/usr/bin/env python3

    import sys
    import struct

    binary = open(sys.argv[1], "rb").read()

    elf_header = bytearray(binary[0x00:0x00+0x34])
    program_header = bytearray(binary[0x34:0x34+0x20])
    program = bytearray(binary[0x74:])
    is_64bit = binary[0x04] == 0x02

    elf_header[0x2c] = 1 # only 1 program header
    elf_header[0x18] = 0x54 # fix entrypoint
    program_header[0x04] = 0x54 # fix program offset in file
    program_header[0x08] = 0x54 # fix program offset in memory
    program_header[0x0C] = 0x54 # fix program offset in "physical" memory
    elf_header_names = ['e_ident[EI_MAG]', 'e_ident[EI_CLASS]', 'e_ident[EI_DATA]', 'e_ident[EI_VERSION]', 'e_ident[EI_OSABI]', 'e_ident[EI_ABIVERSION]', 'e_type', 'e_machine', 'e_version', 'e_entry', 'e_phoff', 'e_shoff', 'e_flags', 'e_ehsize', 'e_phentsize', 'e_phnum', 'e_shentsize', 'e_shnum', 'e_shstrndx']

    # remove remaining section headers
    program = program[:-program[::-1].index(0x2e)-1]
    program = program[:-program[::-1].index(0x2e)-1]
    elf_header = {}
    program_header = {}

    # strip nullbytes
    while program[-1] == 0x00:
    program = program[:-1]
    if is_64bit:
    elf_header_format = "<IBBBBBxxxxxxxHHIQQQIHHHHHH"
    program_header_format = "<IIQQQQQQ"
    program_header_names = ['p_type', 'p_flags', 'p_offset', 'p_vaddr', 'p_paddr', 'p_filesz', 'p_memsz', 'p_align']
    else:
    elf_header_format = "<IBBBBBxxxxxxxHHIIIIIHHHHHH"
    program_header_format = "<IIIIIIII"
    program_header_names = ['p_type', 'p_offset', 'p_vaddr', 'p_paddr', 'p_filesz', 'p_memsz', 'p_flags', 'p_align']

    open(sys.argv[1], "wb").write(elf_header + program_header + program)
    elf_header_length = struct.calcsize(elf_header_format)
    program_header_length = struct.calcsize(program_header_format)

    elf_header_values = list(struct.unpack(elf_header_format, binary[0x00:0x00+elf_header_length]))
    program_header_values = list(struct.unpack(program_header_format, binary[elf_header_length:elf_header_length+program_header_length]))

    for i in range(len(elf_header_names)):
    elf_header[elf_header_names[i]] = elf_header_values[i]

    for i in range(len(program_header_names)):
    program_header[program_header_names[i]] = program_header_values[i]

    program_offset = elf_header_length + elf_header['e_phnum'] * program_header_length
    program = binary[program_offset:program_offset+program_header['p_filesz']]

    elf_header['e_entry'] -= program_header_length
    elf_header['e_phnum'] = 1

    program_header['p_offset'] -= program_header_length
    program_header['p_vaddr'] -= program_header_length
    program_header['p_paddr'] -= program_header_length

    for i, e_name in enumerate(elf_header_names):
    elf_header_values[i] = elf_header[e_name]

    for i, p_name in enumerate(program_header_names):
    program_header_values[i] = program_header[p_name]

    new_elf_header = struct.pack(elf_header_format, *elf_header_values)
    new_program_header = struct.pack(program_header_format, *program_header_values)

    print("ELF header size: %d" % len(new_elf_header))
    print("Program header size: %d" % len(new_program_header))
    print("Program size: %d" % len(program))

    open(sys.argv[1], "wb").write(new_elf_header + new_program_header + program)
  7. lukas2511 created this gist Mar 20, 2019.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    main
    12 changes: 12 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    main: main.c Makefile strip.py
    gcc -o main main.c -nodefaultlibs -nostdlib -m32 -nostartfiles -Os -s -static -Wl,--build-id=none -Wl,--nmagic
    strip --remove-section=.comment --remove-section=.eh_frame --strip-all main
    sstrip -z main
    python strip.py main

    clean:
    rm main

    test: main
    @stat --printf="Size: %s Byte\n" main
    @./main; if [ $${?} = 42 ]; then echo "Seems good :)"; else echo "Seems broken :-/"; fi
    5 changes: 5 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #define EXIT(x) { __asm__("movl $1, %eax; movl $" x ", %ebx; int $0x80"); }

    void _start() {
    EXIT("42");
    }
    25 changes: 25 additions & 0 deletions strip.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/usr/bin/env python3

    import sys

    binary = open(sys.argv[1], "rb").read()

    elf_header = bytearray(binary[0x00:0x00+0x34])
    program_header = bytearray(binary[0x34:0x34+0x20])
    program = bytearray(binary[0x74:])

    elf_header[0x2c] = 1 # only 1 program header
    elf_header[0x18] = 0x54 # fix entrypoint
    program_header[0x04] = 0x54 # fix program offset in file
    program_header[0x08] = 0x54 # fix program offset in memory
    program_header[0x0C] = 0x54 # fix program offset in "physical" memory

    # remove remaining section headers
    program = program[:-program[::-1].index(0x2e)-1]
    program = program[:-program[::-1].index(0x2e)-1]

    # strip nullbytes
    while program[-1] == 0x00:
    program = program[:-1]

    open(sys.argv[1], "wb").write(elf_header + program_header + program)