Single byte XOR brute force for multiple lines in a file

py3
panki27 7 years ago
parent 55e98fbf72
commit 8fb2d6d003

@ -7,23 +7,47 @@ 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' COMMON_LETTERS = 'ETAOIN SHRDLU'
def xorSingleBrute(encoded='', keybytes=1): def xorBrutePrompt():
resultList = dict() isfile = (raw_input('f for file; for string:') == 'f')
if(encoded == ''): maxresults = input('How many top hits? ')
if isfile:
path = raw_input('path: ')
path = path.strip()
results = list()
with open(path, 'r') as file:
contents = file.readlines()
contents = [line.strip() for line in contents]
for code in contents:
recursiveResult = xorSingleBrute(code, maxresults, 1)
#nach Punkten aufsteigend sortieren:
sortedList = sorted(recursiveResult.items(), key=operator.itemgetter(1))[-maxresults:]
#Liste in maximalwert dieser als Tupel anhaengen:
results.append((sortedList, max(recursiveResult.iteritems(), key=operator.itemgetter(1))[1]))
#sortieren nach MaxWert:
sortedResults = sorted(results, key=operator.itemgetter(1))
for item in sortedResults:
#da tupel muss so iteriert werden:
for tupel in item[::2]:
for i in tupel:
print(i[0])
else:
encoded = raw_input('Input your Hex String: ') encoded = raw_input('Input your Hex String: ')
result = xorSingleBrute(encoded, maxresults, 1)
sortedResults = sorted(result.items(), key=operator.itemgetter(1))[-maxresults:]
for i, j in sortedResults:
print(i)
def xorSingleBrute(encoded='', maxresults=20, keybytes=1):
resultDict = dict()
if(encoded == ''):
isfile = (raw_input('f for file; for string:') == 'f')
for xor_key in range(0, 2**(keybytes*8)): for xor_key in range(0, 2**(keybytes*8)):
decoded = ''; decoded = '';
for i, j in zip(encoded[::2], encoded[1::2]): for i, j in zip(encoded[::2], encoded[1::2]):
decoded += ''.join(chr(int(i+j, 16) ^ xor_key)) decoded += ''.join(chr(int(i+j, 16) ^ xor_key))
#if (all(c in string.printable for c in decoded)): resultDict[decoded] = commonCounter(decoded)
#if isHumanReadable(decoded): return resultDict
#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): def commonCounter(inputString, limit=7):
countObj = Counter(inputString.upper()) countObj = Counter(inputString.upper())
@ -32,17 +56,17 @@ def commonCounter(inputString, limit=7):
def fixedxor(string1 = '', string2 = ''): def fixedxor(string1 = '', string2 = ''):
if(string1 == ''): if(string1 == ''):
string1 = raw_input("Please input your first hex string: ") string1 = raw_input('Please input your first hex string: ')
string2 = raw_input("Please input your second hex string: ") string2 = raw_input('Please input your second hex string: ')
hex1 = int(string1, 16) hex1 = int(string1, 16)
hex2 = int(string2, 16) hex2 = int(string2, 16)
result = hex1 ^ hex2 result = hex1 ^ hex2
print(hex(result)) 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)]
@ -55,7 +79,7 @@ def rot(inputString, amount):
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
@ -63,13 +87,13 @@ def translate(inputString, inputType, outputType):
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
@ -97,7 +121,7 @@ def translate(inputString, inputType, outputType):
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():
@ -114,8 +138,8 @@ def base64prompt():
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:
@ -123,19 +147,19 @@ def rotPrompt():
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))
@ -148,7 +172,7 @@ def hashPrompt():
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'):
@ -162,37 +186,37 @@ def hashPrompt():
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?')
print("1: ROT/Ceasar Encryption") print('1: ROT/Ceasar Encryption')
print("2: Hashing functions") print('2: Hashing functions')
print("3: Translation") 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')
print("7: Fixed XOR") print('7: Fixed XOR')
print("8: XOR Bruteforce Single Byte ") print('8: XOR Bruteforce Single Byte ')
choice = raw_input("Please make a selection: ") choice = raw_input('Please make a selection: ')
if (choice == "1"): if (choice == '1'):
rotPrompt() rotPrompt()
elif (choice == "2"): elif (choice == '2'):
hashPrompt() hashPrompt()
elif (choice == "3"): elif (choice == '3'):
translatePrompt() translatePrompt()
elif(choice == "4"): elif(choice == '4'):
base64prompt() base64prompt()
elif(choice == "5"): elif(choice == '5'):
reverser() reverser()
elif(choice == "6"): elif(choice == '6'):
urlEncoder() urlEncoder()
elif(choice[0] == "7"): elif(choice[0] == '7'):
if(len(choice) > 1): if(len(choice) > 1):
argList = choice.split(" ") argList = choice.split(' ')
fixedxor(argList[1], argList[2]) fixedxor(argList[1], argList[2])
else: else:
fixedxor() fixedxor()
elif(choice == "8"): elif(choice == '8'):
xorSingleBrute() #xorSingleBrute()
xorBrutePrompt()
print("Thank you for flying with PankiCrypt Airlines!") print('Thank you for flying with PankiCrypt Airlines!')

Binary file not shown.
Loading…
Cancel
Save