Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, os, zlib, io
- from struct import pack, unpack
- def copyproc(src,dst,size):
- while size:
- block = min(1024,size)
- data = src.read(block)
- dst.write(data)
- size -= block
- def read_ntstr(f):
- bytes = 1
- str = ""
- chr = f.read(1)
- while ord(chr):
- str += chr
- bytes += 1
- chr = f.read(1)
- return (str, bytes)
- if len(sys.argv) < 2:
- print "Wrong"
- exit(1)
- oldfile = sys.argv[1]
- newfile = sys.argv[2]
- f = open(oldfile, "r+b")
- f2 = open(newfile,'r+b')
- print "Opening: %s and new %s" % (oldfile,newfile)
- f.seek(0,0)
- if tuple(unpack("<cccc",f.read(4))) == ('\x7f', 'E', 'L', 'F'):
- f.seek(40,0)
- shoff = (unpack("<Q",f.read(8)))[0]
- f.seek(10,1)
- shsize = (unpack("<H",f.read(2)))[0]
- shnum = (unpack("<H",f.read(2)))[0]
- shidx = (unpack("<H",f.read(2)))[0]
- print "SH Offset: %s, SH Num: %s, SH Size: %s, SH Index: %s" % (shoff, shnum, shsize, shidx)
- f.seek(shoff+(shidx * shsize),0)
- shtextoff = unpack("<IIQQQQIIQQ",f.read(shsize))[4]
- f.seek(shoff,0)
- f2strtaboff = 0
- # Find and copy our string table (it's in front of the section headers, behind the fs block)
- for i in range(shnum):
- sh = unpack("<IIQQQQIIQQ",f.read(shsize))
- curpos = f.tell()
- f.seek(shtextoff+sh[0])
- realname = read_ntstr(f)[0]
- if realname == ".shstrtab":
- print "OLD Name %s, offset: %s, size: %s" % (realname,sh[4], sh[5])
- f.seek(sh[4],0)
- f2.seek(0, io.SEEK_END)
- f2strtaboff = f2.tell()
- copyproc(f,f2,sh[5])
- f.seek(curpos,0)
- # Moving SH headers to new file (strict alignment on 8 seems to be required)
- f2.seek(0, io.SEEK_END)
- f2end = f2.tell()
- if (f2end)%8 != 0 :
- f2end += (8 - (f2end)%8)
- f2.seek(f2end,0)
- print "Shifting to %s" % (f2end)
- f.seek(shoff,0)
- f2shoff = int(f2.tell())
- copyproc(f,f2,shnum*shsize)
- # Done with copy, put the offset @ ELF header
- f2.seek(40,0)
- f2.write(pack('<Q', f2shoff))
- # Fix section headers
- f2.seek(f2shoff+(14*shsize)+24,0)
- print "Writing Partnand offset: %s at: %s" % (f2strtaboff,f2.tell())
- f2.write(pack('<Q', f2strtaboff))
- f2.seek(f2shoff+(15*shsize)+24,0)
- print "Writing SH Text offset: %s at: %s" % (f2strtaboff,f2.tell())
- f2.write(pack('<Q', f2strtaboff))
- print "We installed shoff: %s and strtable: %s in file %s" % (f2shoff, f2strtaboff, newfile)
- else:
- print "Not valid ELF file"
- f.close()
- f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement