Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys, os, zlib, io
  4. from struct import pack, unpack
  5.  
  6. def copyproc(src,dst,size):
  7.     while size:
  8.         block = min(1024,size)
  9.         data = src.read(block)
  10.         dst.write(data)
  11.         size -= block
  12.  
  13. def read_ntstr(f):
  14.         bytes = 1
  15.         str = ""
  16.         chr = f.read(1)
  17.         while ord(chr):
  18.                 str += chr
  19.                 bytes += 1
  20.                 chr = f.read(1)
  21.         return (str, bytes)
  22.  
  23. if len(sys.argv) < 2:
  24.     print "Wrong"
  25.     exit(1)
  26.  
  27. oldfile = sys.argv[1]
  28. newfile = sys.argv[2]
  29.  
  30. f = open(oldfile, "r+b")
  31. f2 = open(newfile,'r+b')
  32. print "Opening: %s and new %s" % (oldfile,newfile)
  33. f.seek(0,0)
  34.  
  35. if tuple(unpack("<cccc",f.read(4))) == ('\x7f', 'E', 'L', 'F'):
  36.     f.seek(40,0)
  37.     shoff = (unpack("<Q",f.read(8)))[0]
  38.     f.seek(10,1)
  39.         shsize = (unpack("<H",f.read(2)))[0]
  40.         shnum = (unpack("<H",f.read(2)))[0]
  41.         shidx = (unpack("<H",f.read(2)))[0]
  42.     print "SH Offset: %s, SH Num: %s, SH Size: %s, SH Index: %s" % (shoff, shnum, shsize, shidx)
  43.  
  44.         f.seek(shoff+(shidx * shsize),0)
  45.         shtextoff = unpack("<IIQQQQIIQQ",f.read(shsize))[4]
  46.         f.seek(shoff,0)
  47.  
  48.     f2strtaboff = 0
  49.  
  50.     # Find and copy our string table (it's in front of the section headers, behind the fs block)
  51.         for i in range(shnum):
  52.                 sh = unpack("<IIQQQQIIQQ",f.read(shsize))
  53.                 curpos = f.tell()
  54.                 f.seek(shtextoff+sh[0])
  55.                 realname = read_ntstr(f)[0]
  56.         if realname == ".shstrtab":
  57.             print "OLD Name %s, offset: %s, size: %s" % (realname,sh[4], sh[5])
  58.             f.seek(sh[4],0)
  59.             f2.seek(0, io.SEEK_END)
  60.             f2strtaboff = f2.tell()
  61.             copyproc(f,f2,sh[5])
  62.                 f.seek(curpos,0)
  63.  
  64.     # Moving SH headers to new file (strict alignment on 8 seems to be required)
  65.     f2.seek(0, io.SEEK_END)
  66.     f2end = f2.tell()
  67.     if (f2end)%8 != 0 :
  68.             f2end += (8 - (f2end)%8)
  69.             f2.seek(f2end,0)
  70.         print "Shifting to %s" % (f2end)
  71.  
  72.     f.seek(shoff,0)
  73.     f2shoff = int(f2.tell())
  74.     copyproc(f,f2,shnum*shsize)
  75.  
  76.     # Done with copy, put the offset @ ELF header
  77.     f2.seek(40,0)
  78.     f2.write(pack('<Q', f2shoff))
  79.  
  80.     # Fix section headers
  81.     f2.seek(f2shoff+(14*shsize)+24,0)
  82.     print "Writing Partnand offset: %s at: %s" % (f2strtaboff,f2.tell())
  83.     f2.write(pack('<Q', f2strtaboff))
  84.  
  85.     f2.seek(f2shoff+(15*shsize)+24,0)
  86.     print "Writing SH Text offset: %s at: %s" % (f2strtaboff,f2.tell())
  87.     f2.write(pack('<Q', f2strtaboff))
  88.  
  89.     print "We installed shoff: %s and strtable: %s in file %s" % (f2shoff, f2strtaboff, newfile)
  90.  
  91. else:
  92.     print "Not valid ELF file"
  93.  
  94. f.close()
  95. f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement