Advertisement
opexxx

xorBruteForcer.py

Sep 5th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #
  4. # XOR Bruteforcer: tries all the possible values for a XOR key looking for a pattern in the output. A single key can be specified too.
  5. #
  6. # http://eternal-todo.com
  7. # Jose Miguel Esparza
  8. #
  9.  
  10. from itertools import cycle, izip
  11. import sys,os,re
  12.  
  13. def process (ss, key):
  14.     key = cycle(key)
  15.     return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(ss, key))
  16.  
  17. found = False
  18. param = ""
  19. xorKey = ""
  20. successfullKeys = []
  21. usage = "Usage: "  + sys.argv[0] + ''' -k xor_key file [search_pattern]
  22.  
  23. Arguments:
  24.  
  25.    file: the source file to be xored.
  26.    search_pattern: pattern that must be found in the xored result.
  27.  
  28. Options:
  29.  
  30.    -k xor_key: key used in the XOR function (00-ff). If not specified, all the possible values will be tested (bruteforcing).
  31. '''
  32.  
  33.  
  34. if len(sys.argv) == 2 or len(sys.argv) == 3:
  35.    file = sys.argv[1]
  36.    if len(sys.argv) == 3:
  37.       param = sys.argv[2]
  38. elif len(sys.argv) == 4 or len(sys.argv) == 5:
  39.    if sys.argv[1] != "-k":
  40.       sys.exit(usage)
  41.    xorKey = sys.argv[2]
  42.    if not re.match("[0-9a-f]{1,2}",xorKey):
  43.       sys.exit(usage)
  44.    xorKey = int(xorKey,16)
  45.    file = sys.argv[3]
  46.    if len(sys.argv) == 5:
  47.       param = sys.argv[4]
  48. else:
  49.     sys.exit(usage)
  50.  
  51. if not os.path.exists(file):
  52.    sys.exit('Error: the file does not exist!!')
  53.  
  54. content = open(file,"r").read()
  55. if xorKey != "":
  56.    decValues = [xorKey]
  57. else:
  58.    decValues = range(256)
  59.  
  60. for i in decValues:
  61.    key = chr(i)
  62.    deciphered = process(content, key)
  63.    if param == "":
  64.       print "["+hex(i).upper()+"]"
  65.       print deciphered
  66.       print "[/"+hex(i).upper()+"]"
  67.    elif re.findall(param,deciphered,re.IGNORECASE) != []:
  68.       found = True
  69.       successfullKeys.append(hex(i).upper())
  70.       print "["+hex(i).upper()+"]"
  71.       print deciphered
  72.       print "[/"+hex(i).upper()+"]"
  73.  
  74. if param != "":
  75.    if not found:
  76.       sys.exit("Warning: Pattern not found!!")
  77.    else:
  78.       sys.exit("Pattern found using the following keys: "+str(successfullKeys))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement