I am trying to modify an ELF file's .text segment using python.
I successfully acquired the .text field so then I can simply change the bit that I want. The thing is that pyelftools does not provide any way to generate an ELF file from the ELF object.
So what I tried is the following:
I've created a simple helloworld program in c, compiled it and got the a.out file. Then I used the pyelftools to disassemble it.
To change/edit any section of the ELF file I simply used
pyelftools'sELFFileclass methods to acquire the field's (i)offsetand (ii)size. So then I know exactly where to look inside the binary file.So after getting the values-margins of the field (A,B) I simply treated the file like a normal binary. The only thing I did is to do a
file.seek(A)to move the file pointer to the specific section that I wish to modify.To validate the results you can use the
diffbinary to see that the files are/aren't identical