New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to modify the dynamic entries for a ELF in python? #118
Comments
Hi $ readelf -d /lib/libm.so.6 |grep INIT
0x000000000000000c (INIT) 0x7628
0x0000000000000019 (INIT_ARRAY) 0x34acd0
0x000000000000001b (INIT_ARRAYSZ) 8 (bytes) As we can see the value of Then with LIEF: >>> lib = lief.parse("/lib/libm.so.6")
>>> lib[lief.ELF.DYNAMIC_TAGS.INIT].value = 0xBADC0DE
>>> lib.write("/tmp/test.so") And now: $ readelf -d /tmp/test.so|grep INIT
0x000000000000000c (INIT) 0xbadc0de
0x0000000000000019 (INIT_ARRAY) 0x34acd0
0x000000000000001b (INIT_ARRAYSZ) 8 (bytes) For the second point you can do: binary = lief.parse(...)
if lief.ELF.DYNAMIC_TAGS.INIT_ARRAY in binary:
array = binary[lief.ELF.DYNAMIC_TAGS.INIT_ARRAY]
print(array) |
Thank you! |
The code you provided works well with the INIT but not with the INIT_ARRAY: >>> lib = lief.parse("/lib/libm.so.6")
>>> lib[lief.ELF.DYNAMIC_TAGS.INIT_ARRAY].value = 0xBADC0DE
>>> lib.write("/tmp/test.so") I get this error:
My idea is to create a new INIT_ARRAY and modify the pointer in the Dynamic Section so I can add an additional start routine to a binary. |
I only get the error when running the code from a script. Running it from a ipython sessions the error doesn't show up. But the output binary doesn't have the INIT_ARRAY value modified :( |
You shouldn't care about the warning Modifying the address of the |
I don't understand :( Do you mean the new address for the INIT_ARRAY? My idea was to create a new segment with the new array and then modify INIT_ARRAY to point there. Could you explain it more or link a resource where I could learn why it's not a good idea? Thank you for your hard work! |
I mean: why not using the existing one and extend it (from the bottom or from the top) ? Usually all Linux shared libraries have such entry If you really need to create a new one you should take account of the relocation process an create new relocations as well |
I'm worry that there is no space to extend the INIT_ARRAY, maybe there is other tables or sections around. Could I extend the INIT_ARRAY using LIEF? PS: I have experience with the PE format, but I feel a bit lost with the ELF. Could you recommend me a good document to understand it? Thank you! |
Yes actually LIEF handle this case: https://github.com/lief-project/LIEF/blob/master/src/ELF/Builder.tcc#L547-L550 ;)
The better documentation is the loader source code: |
Hi,
What is the problem with relocations here? I don't understand since I'm not changing more than just pointers not actual code that may need to be aligned (that's what I thought about when reading relocations). Thanks!! |
If the |
Hello,
I am trying LIEF for the first time, it's an amazing project!
I'm having troubles doing the following.
I want to modify an entry in the dynamic table for an ELF file. I tried this:
But the value is not written into the file.
I saw the DynamicEntryArray class has a method to insert values:
https://lief.quarkslab.com/doc/api/python/elf.html#dynamic-entry-array
But I don't know how to get that DynamicEntryArray from the parsed file :(
How could I do this?
Thank you!
The text was updated successfully, but these errors were encountered: