ganadist

undexopt

Feb 7th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # undexopt.py
  3.  
  4. import sys, os
  5. import struct
  6. import zipfile
  7.  
  8. OPT_MAGIC = 'dey\n'
  9. OPT_VER   = '036\0'
  10.  
  11. optheader = '8s8i'
  12. header_size = struct.calcsize(optheader)
  13.  
  14. def extractDex(odexfile):
  15.     f = open(odexfile)
  16.     header = struct.unpack(optheader, f.read(header_size))
  17.     magic, offset, length, depsoffset, depslength, optoffset, optlength, flag, checksum = header
  18.     f.seek(offset, os.SEEK_SET)
  19.     return f.read(length)
  20.  
  21. def addJar(jarfile, dexbuf):
  22.     out = zipfile.ZipFile(jarfile, 'a')
  23.     out.writestr('classes.dex', dexbuf)
  24.  
  25. if __name__ == '__main__':
  26.     if len(sys.argv) == 3:
  27.         infile, outfile = sys.argv[1:3]
  28.     elif len(sys.argv) == 4:
  29.         infile, apk, outfile = sys.argv[1:4]
  30.         import shutil
  31.         shutil.copy(apk, outfile)
  32.     else:
  33.         print 'usage: %s odexfile apkfile [outfile]'%sys.argv[0]
  34.         sys.exit(-1)
  35.  
  36.     dex = extractDex(infile)
  37.     addJar(outfile, dex)
Advertisement
Add Comment
Please, Sign In to add comment