Last active
March 20, 2019 20:32
-
-
Save lukas2511/ffd6a30a4ba3b465745cdfc9d3d80c72 to your computer and use it in GitHub Desktop.
Revisions
-
lukas2511 revised this gist
Mar 20, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
lukas2511 revised this gist
Mar 20, 2019 . No changes.There are no files selected for viewing
-
lukas2511 revised this gist
Mar 20, 2019 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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_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 = "<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 = "<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 -
lukas2511 revised this gist
Mar 20, 2019 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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_binary) -
lukas2511 revised this gist
Mar 20, 2019 . 1 changed file with 11 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,16 @@ int main(); void exit(int x) { asm("movl %0, %%ebx" : : "r"(x) :); asm("movl $1, %eax"); asm("int $0x80"); while(1); } void _start() { exit(main()); } int main() { return 42; } -
lukas2511 revised this gist
Mar 20, 2019 . 3 changed files with 63 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,9 @@ 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,11 @@ void exit(int x) { while(1) { asm("movl %0, %%ebx" : : "r"(x) :); asm("movl $1, %eax"); asm("int $0x80"); } } void _start() { exit(42); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() 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 = {} program_header = {} 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'] 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) -
lukas2511 created this gist
Mar 20, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ main This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)