Is There Any Way To Generate ELF BINARY Using Only Python Library?
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?
Answer
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 incrt0.o
, and your program should start atmain
, not_start
).You don't properly terminate your program (don't call
exit
,_exit
, or executeSYS_exit
syscall). You can return frommain
(libc-provided_start
will then executeSYS_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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module