Guest User

TLS segment alignment patch

a guest
Mar 17th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import struct
  4.  
  5. with open('libsyncthing.so', 'r+b') as f:
  6.   f.seek(0)
  7.   hdr = f.read(16)
  8.   if hdr[0] != 0x7f or hdr[1] != ord('E') or hdr[2] != ord('L') or hdr[3] != ord('F'):
  9.     raise Exception('Not an elf file')
  10.  
  11.   if hdr[4] == 1:
  12.     # 32 bit code
  13.     f.seek(28)
  14.     offset = struct.unpack('<I', f.read(4))[0]
  15.     f.seek(42)
  16.     phsize = struct.unpack('<H', f.read(2))[0]
  17.     phnum = struct.unpack('<H', f.read(2))[0]
  18.     for i in range(0, phnum):
  19.       f.seek(offset + i * phsize)
  20.       t = struct.unpack('<I', f.read(4))[0]
  21.       if t == 7:
  22.         f.seek(28 - 4, 1)
  23.         align = struct.unpack('<I', f.read(4))[0]
  24.         print('Found TLS segment with align = ' + str(align))
  25.         if (align < 32):
  26.           print('TLS segment is underaligned, patching')
  27.           f.seek(-4, 1)
  28.           f.write(struct.pack('<I', 32))
  29.  
  30.   elif hdr[4] == 2:
  31.     # 64 bit code
  32.     f.seek(32)
  33.     offset = struct.unpack('<Q', f.read(8))[0]
  34.     f.seek(54)
  35.     phsize = struct.unpack('<H', f.read(2))[0]
  36.     phnum = struct.unpack('<H', f.read(2))[0]
  37.     for i in range(0, phnum):
  38.       f.seek(offset + i * phsize)
  39.       t = struct.unpack('<I', f.read(4))[0]
  40.       if t == 7:
  41.         f.seek(48 - 4, 1)
  42.         align = struct.unpack('<Q', f.read(8))[0]
  43.         print('Found TLS segment with align = ' + str(align))
  44.         if (align < 64):
  45.           print('TLS segment is underaligned, patching')
  46.           f.seek(-8, 1)
  47.           f.write(struct.pack('<H', 64))
  48.  
  49.   else:
  50.     raise Exception('Unknown file class')
Advertisement
Add Comment
Please, Sign In to add comment