Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. def is_dynelf(path):
  2.     if os.path.getsize(path) <= 0:
  3.         return False
  4.     with open(path, 'rb') as f:
  5.         if f.read(4) != '\x7fELF':
  6.             return False
  7.         #ELFCLASS32 = 1, ELFCLASS64 = 2
  8.         wordsize = fun('B',f.read(1))*4
  9.         wordfmt = 'Q' if wordsize == 8 else 'L'
  10.         end = fun('B',f.read(1))
  11.         #ELFDATA2LSB = 1, ELFDATA2MSB = 2
  12.         end = '<' if end == 1 else '>'
  13.         f.seek(16)
  14.         elftype = fun(end+'H',f.read(2))
  15.         #ET_REL = 1, ET_EXEC = 2, ET_DYN = 3
  16.         if elftype not in range(1,4):
  17.             return False
  18.         # read section hdr offset
  19.         f.seek(24+wordsize*2)
  20.         shoff = fun(end+wordfmt,f.read(wordsize))
  21.         # read section hdr entry size + nr. of entries
  22.         f.seek(10, 1)
  23.         shentsize = fun(end+'H',f.read(2))
  24.         shnum = fun(end+'H',f.read(2))
  25.         if shentsize < 1:
  26.             return False
  27.         # seek to section hdr and read entry by entry
  28.         for i in xrange(0,shnum):
  29.             f.seek(shoff+(i*shentsize)+4)  # 4 = sh_type off
  30.             shenttype = fun(end+'L',f.read(4))
  31.             # SHT_DYNAMIC = 6
  32.             if shenttype == 6:
  33.                 return True
  34.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement