Advertisement
opexxx

iheartxor.py

Sep 5th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # iheartxor is a tool for bruteforcing encoded strings
  4. # within a boundary defined by a regular expression. It
  5. # will bruteforce the key value range of 0x1 through 0x255
  6. #
  7. # Version 0.01 - still need to test passing regular expressions
  8. #
  9. # Created by Alexander.Hanel@gmail.com
  10. #
  11. # Usage: brutexor.py [options] <file>
  12. #
  13. # Options:
  14. #   -h, --help            show this help message and exit
  15. #   -k KEY, --key=KEY     Static XOR key to use
  16. #   -f, --full            XOR full file
  17. #   -r PATTERN, --re=PATTERN
  18. #                       Regular Expression Pattern to search for
  19.  
  20. import string
  21. import re
  22. import sys
  23. from optparse import OptionParser
  24.  
  25. def valid_ascii(char):
  26.         if char in string.printable[:-3]:
  27.                 return True
  28.         else:
  29.                 return None
  30.  
  31. def xor(data, key):
  32.         decode = ''
  33.         if isinstance(key, basestring):
  34.                 key = int(key,16)
  35.                
  36.         for d in data:
  37.                 decode = decode + chr(ord(d) ^ key)
  38.         return decode                
  39.        
  40. def main(argv):
  41.         parser = OptionParser()
  42.         usage = 'usage: iheartxor.py [options] <file>'
  43.         parser  =  OptionParser(usage=usage)
  44.         parser.add_option('-k', '--key', action='store', dest='key', type='string', help='Static XOR key to use')
  45.         parser.add_option('-f', '--full', action='store_true', dest='full', help='XOR full file')
  46.         parser.add_option('-r', '--re', action='store', dest='pattern', type='string', help='Regular Expression Pattern to search for')
  47.         (options, args) = parser.parse_args()
  48.         # Test for Args
  49.         if len(sys.argv) < 2:
  50.                 parser.print_help()
  51.                 return
  52.         try:
  53.                 f = open(sys.argv[len(sys.argv)-1],'rb+')
  54.         except Exception:
  55.                 print '[ERROR] FILE CAN NOT BE OPENED OR READ!'
  56.                 return
  57.         # Test that the full option contains a XOR Key
  58.         if options.full != None and options.key == None:
  59.                 print '[ERROR] --FULL OPTION MUST INCLUDE XOR KEY'
  60.                 return
  61.         # XOR the full file with key
  62.         if options.full != None and options.key != None:
  63.                 sys.stdout.write(xor(f.read(), options.key))
  64.                 return
  65.         # Parse file for regular expressions
  66.         if options.pattern == None:
  67.                 regex = re.compile(r'\x00(?!\x00).+?\x00')
  68.         else:
  69.                 try:
  70.                         regex = re.compile(pattern)
  71.                 except Exception:
  72.                         print "ERROR: INVALID REGULAR EXPRESSION PATTERN"
  73.                         sys.exit(1)
  74.         buff = ''
  75.         # for each regex pattern found
  76.         for match in regex.finditer(f.read()):
  77.                 if len(match.group()) < 8:
  78.                         continue
  79.                 if options.key == None:
  80.                         # for XOR key in range of 0x0 to 0xff
  81.                         for key in range(1,0x100):
  82.                                 # for each byte in match of regex pattern
  83.                                 for byte in match.group():
  84.                                         if byte == '\x00':
  85.                                                 buff = buff + '\x00'
  86.                                                 continue
  87.                                         else:
  88.                                                 tmp = xor(byte,key)
  89.                                                 if valid_ascii(tmp) == None:
  90.                                                         buff = ''
  91.                                                         break
  92.                                                 else:
  93.                                                         buff = buff + tmp
  94.                                 if buff != '':
  95.                                         sys.stdout.write(hex(match.start()) + ' ' + 'key ' + hex(key) + ' ' +  buff + '\n')
  96.                                         buff = ''
  97.                        
  98.                 else:
  99.                         # same as above but for static key
  100.                         key = int(options.key,16)
  101.                         for byte in match.group():
  102.                                 if byte == '\x00':
  103.                                         buff = buff + '\x00'
  104.                                         continue
  105.                                 else:
  106.                                         tmp = xor(byte,key)
  107.                                         if valid_ascii(tmp) == None:
  108.                                                 buff = ''
  109.                                                 break
  110.                                         else:
  111.                                                 buff = buff + tmp
  112.                         if buff != '':
  113.                                 sys.stdout.write(hex(match.start()) + ' ' + 'key ' + hex(key) + ' ' + buff + '\n')
  114.                        
  115.                        
  116.         return
  117.  
  118. if __name__== '__main__':
  119.         main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement