Is There Any Way To Generate ELF BINARY Using Only Python Library?

- 1 answer

Is there any way to generate ELF binary using only the python library?

I want to assemble the file below using python library.

a.s :

line 1 : .global _start
line 2 : _start:
line 3 :  push _STRING1
line 4 :  push _STRING2
line 5 :  call printf
line 6 : _STRING1:
line 7 :  .string "gogo"
line 8 : _STRING2:
line 9 :  .string "%s"

I've tried to use two python libraries keystone, and pwntool.
However, they support only machine code byte emission corresponding to assembly command.
(It didn't generate ELF binary consisting of elf header, section header,.. etc)

Also, It doesn't support handling on external library info. (such as line 5)

Q. I wanna generate a complete ELF BINARY a.out using python library.
Is there any possible way to do that?



I want to assemble the below file

Note that your program is ill-defined:

  • You call printf, but you didn't allow C runtime to be initialized. (If you want to call any libc functions, you should let the _start to be defined in crt0.o, and your program should start at main, not _start).

  • You don't properly terminate your program (don't call exit, _exit, or execute SYS_exit syscall). You can return from main (libc-provided _start will then execute SYS_exit for you), but you must not return from _start, and certainly not fall from the end of it to whatever code or data is immediately after it.

Back to your original question: it's possible to "hand-assemble" an ELF file -- data is data.

However, that's a lot of work (and pretty pointless work), so you are much better off simply invoking external assembler and linker.