Added single byte XOR cipher bruteforce for hex encoded text

py3
panki27 7 years ago
parent c5efdd0c56
commit 55e98fbf72

@ -1,131 +1,166 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, string, types, hashlib, base64, re, urllib import sys, string, types, hashlib, base64, re, urllib, binascii
import operator
from collections import Counter
LOWER_LETTERS = [chr(x) for x in range(97, 123)] LOWER_LETTERS = [chr(x) for x in range(97, 123)]
UPPER_LETTERS = [chr(x) for x in range(65, 91)] UPPER_LETTERS = [chr(x) for x in range(65, 91)]
COMMON_LETTERS = 'ETAOIN SHRDLU'
def xorSingleBrute(encoded='', keybytes=1):
resultList = dict()
if(encoded == ''):
encoded = raw_input('Input your Hex String:')
for xor_key in range(0, 2**(keybytes*8)):
decoded = '';
for i, j in zip(encoded[::2], encoded[1::2]):
decoded += ''.join(chr(int(i+j, 16) ^ xor_key))
#if (all(c in string.printable for c in decoded)):
#if isHumanReadable(decoded):
#print(xor_key, decoded)
resultList[decoded] = commonCounter(decoded)
sortedResults = sorted(resultList.items(), key=operator.itemgetter(1))
for result, key in sortedResults[-20:]:
#if (all(c in string.printable for c in result)):
print(result)
def commonCounter(inputString, limit=7):
countObj = Counter(inputString.upper())
common = countObj & Counter(COMMON_LETTERS)
return sum(common.values())
def fixedxor(string1 = '', string2 = ''):
if(string1 == ''):
string1 = raw_input("Please input your first hex string: ")
string2 = raw_input("Please input your second hex string: ")
hex1 = int(string1, 16)
hex2 = int(string2, 16)
result = hex1 ^ hex2
print(hex(result))
def rot(inputString, amount): def rot(inputString, amount):
outputString = "" outputString = ""
for char in inputString: for char in inputString:
resultChar = "" resultChar = ""
if char.isupper(): if char.isupper():
index = UPPER_LETTERS.index(char) index = UPPER_LETTERS.index(char)
resultChar = UPPER_LETTERS[(index + amount)%len(UPPER_LETTERS)] resultChar = UPPER_LETTERS[(index + amount)%len(UPPER_LETTERS)]
elif char.islower(): elif char.islower():
index = LOWER_LETTERS.index(char) index = LOWER_LETTERS.index(char)
resultChar = LOWER_LETTERS[(index + amount) % len(LOWER_LETTERS)] resultChar = LOWER_LETTERS[(index + amount) % len(LOWER_LETTERS)]
else: else:
resultChar = char resultChar = char
outputString += resultChar outputString += resultChar
return outputString return outputString
def translate(inputString, inputType, outputType): def translate(inputString, inputType, outputType):
result = "" result = ""
if(inputType == outputType): if(inputType == outputType):
result = inputString result = inputString
return result return result
if (inputType == 5): if (inputType == 5):
for char in inputString: for char in inputString:
if(outputType == 1): if(outputType == 1):
result += str(bin(ord(char))) + " " result += str(bin(ord(char))) + " "
elif(outputType == 2): elif(outputType == 2):
result += str(ord(char)) + " " result += str(ord(char)) + " "
elif(outputType == 3): elif(outputType == 3):
result += str(oct(ord(char))) + " " result += str(oct(ord(char))) + " "
elif(outputType == 4): elif(outputType == 4):
result += str(hex(ord(char))) + " " result += str(hex(ord(char))) + " "
else: else:
result = inputString result = inputString
break break
return result return result
elif(inputType == 1): elif(inputType == 1):
inputString = int(inputString, 2) inputString = int(inputString, 2)
elif(inputType == 2): elif(inputType == 2):
inputString = int(inputString) inputString = int(inputString)
elif(inputType == 3): elif(inputType == 3):
inputString = int(inputString, 8) inputString = int(inputString, 8)
elif(inputType == 4): elif(inputType == 4):
inputString = int(inputString, 16) inputString = int(inputString, 16)
if(outputType == 1): if(outputType == 1):
result = bin(inputString) result = bin(inputString)
elif(outputType == 2): elif(outputType == 2):
result = inputString result = inputString
elif(outputType == 3): elif(outputType == 3):
result = oct(inputString) result = oct(inputString)
elif(outputType == 4): elif(outputType == 4):
result = hex(inputString) result = hex(inputString)
elif(outputType == 5): elif(outputType == 5):
result = chr(inputString) result = chr(inputString)
return result return result
def urlEncoder(): def urlEncoder():
input = raw_input("Pleae input your String: ") input = raw_input("Pleae input your String: ")
print(urllib.quote_plus(input)) print(urllib.quote_plus(input))
def reverser(): def reverser():
string = raw_input('Please input your string to reverse:') string = raw_input('Please input your string to reverse:')
print(string[::-1]) print(string[::-1])
def base64prompt(): def base64prompt():
b64regex = '^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$' b64regex = '^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'
inputString = raw_input('Please input your string: ') inputString = raw_input('Please input your string: ')
if (re.match(b64regex, inputString)): if (re.match(b64regex, inputString)):
print(base64.b64decode(inputString)) print(base64.b64decode(inputString))
else: else:
print(base64.b64encode(inputString)) print(base64.b64encode(inputString))
def rotPrompt(): def rotPrompt():
choice = input("What kind of ROT do you want to perform? 1-25, or all: ") choice = input("What kind of ROT do you want to perform? 1-25, or all: ")
userInput = raw_input("Please insert a string: ") userInput = raw_input("Please insert a string: ")
if type(choice) is types.IntType: if type(choice) is types.IntType:
print(rot(userInput, choice)) print(rot(userInput, choice))
else: else:
for i in range(0, 26): for i in range(0, 26):
print(rot(userInput, i)) print(rot(userInput, i))
def translatePrompt(): def translatePrompt():
print("1: Binary") print("1: Binary")
print("2: Decimal") print("2: Decimal")
print("3: Octal") print("3: Octal")
print("4: Hexadecimal") print("4: Hexadecimal")
print("5: ASCII") print("5: ASCII")
inputType = input("Please specify input type: ") inputType = input("Please specify input type: ")
outputType = input("Please specify output type: ") outputType = input("Please specify output type: ")
if (inputType == 5): if (inputType == 5):
inputString = raw_input("Please input your strings, seperated by semicolon: ") inputString = raw_input("Please input your strings, seperated by semicolon: ")
else: else:
inputString = raw_input("Please input your values, seperated by semicolon: ") inputString = raw_input("Please input your values, seperated by semicolon: ")
inputList = inputString.split(";") inputList = inputString.split(";")
for entry in inputList: for entry in inputList:
print(translate(entry, inputType, outputType)) print(translate(entry, inputType, outputType))
def hashPrompt(): def hashPrompt():
typeChoice = raw_input('Would you like to hash a file or a String? f for file, s for string: ') typeChoice = raw_input('Would you like to hash a file or a String? f for file, s for string: ')
algoList = hashlib.algorithms_available algoList = hashlib.algorithms_available
for word in algoList: for word in algoList:
print(word) print(word)
algoChoice = raw_input('Which hashing algorithm? ') algoChoice = raw_input('Which hashing algorithm? ')
if algoChoice in algoList: if algoChoice in algoList:
hasher = hashlib.new(algoChoice) hasher = hashlib.new(algoChoice)
else: else:
print("That's no algorithm!") print("That's no algorithm!")
sys.exit(0) sys.exit(0)
if (typeChoice == 'f'): if (typeChoice == 'f'):
filePath = raw_input('Please input a fully qualified path: ') filePath = raw_input('Please input a fully qualified path: ')
filePath = filePath.strip() filePath = filePath.strip()
with open(filePath, 'rb') as hashFile: with open(filePath, 'rb') as hashFile:
content = hashFile.read() content = hashFile.read()
hasher.update(content) hasher.update(content)
else: else:
inputString = raw_input('Please input a string: ') inputString = raw_input('Please input a string: ')
hasher.update(inputString) hasher.update(inputString)
print(hasher.hexdigest()) print(hasher.hexdigest())
print("Welcome aboard PankiCrypt Airlines!") print("Welcome aboard PankiCrypt Airlines!")
print("How may we serve you today?") print("How may we serve you today?")
@ -135,19 +170,29 @@ print("3: Translation")
print("4: Base64 Encoder/Decoder") print("4: Base64 Encoder/Decoder")
print("5: String Reverser") print("5: String Reverser")
print("6: URL Encoder") print("6: URL Encoder")
choice = input("Please make a selection: ") print("7: Fixed XOR")
if (choice == 1): print("8: XOR Bruteforce Single Byte ")
rotPrompt() choice = raw_input("Please make a selection: ")
elif (choice == 2): if (choice == "1"):
hashPrompt() rotPrompt()
elif (choice == 3): elif (choice == "2"):
translatePrompt() hashPrompt()
elif(choice == 4): elif (choice == "3"):
base64prompt() translatePrompt()
elif(choice == 5): elif(choice == "4"):
reverser() base64prompt()
elif(choice == 6): elif(choice == "5"):
urlEncoder() reverser()
elif(choice == "6"):
urlEncoder()
elif(choice[0] == "7"):
if(len(choice) > 1):
argList = choice.split(" ")
fixedxor(argList[1], argList[2])
else:
fixedxor()
elif(choice == "8"):
xorSingleBrute()
print("Thank you for flying with PankiCrypt Airlines!") print("Thank you for flying with PankiCrypt Airlines!")

Loading…
Cancel
Save