Last active
June 11, 2018 14:26
-
-
Save therealprocyon/d90b32b0d6a5a411637aa0d13add6181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Code By CC Oosting (TheRealProcyon) | |
# Includes code written by David Buchanan | |
# This adapted code consists of the byte_frequency variable and the frequency_analysis function | |
from operator import itemgetter | |
from operator import xor | |
from types import * | |
import re | |
import string | |
import os | |
byte_frequency = [0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.022298, 0.000000, 0.000000, 0.022298, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.166921, 0.002692, 0.000806, 0.000006, 0.000012, 0.000006, 0.000000, 0.017219, | |
0.000454, 0.000454, 0.000525, 0.000000, 0.015315, 0.004441, 0.007198, 0.000143, | |
0.000131, 0.000406, 0.000072, 0.000078, 0.000060, 0.000078, 0.000042, 0.000036, | |
0.000060, 0.000066, 0.001528, 0.001158, 0.000000, 0.000000, 0.000000, 0.001206, | |
0.000012, 0.004309, 0.000746, 0.001116, 0.001444, 0.001868, 0.000800, 0.001152, | |
0.001844, 0.004972, 0.000078, 0.000525, 0.000943, 0.001325, 0.001086, 0.001397, | |
0.001027, 0.000507, 0.001265, 0.001725, 0.003408, 0.000663, 0.000310, 0.001528, | |
0.000036, 0.000848, 0.000006, 0.000024, 0.000000, 0.000024, 0.000000, 0.000024, | |
0.000000, 0.054212, 0.009675, 0.016813, 0.031203, 0.090035, 0.013417, 0.016419, | |
0.045247, 0.046572, 0.001325, 0.007174, 0.030159, 0.013399, 0.046978, 0.055173, | |
0.010719, 0.000806, 0.038198, 0.041666, 0.069420, 0.023080, 0.005437, 0.016091, | |
0.001015, 0.014575, 0.000472, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, | |
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000] | |
def main(): | |
xoring() | |
in_progress() | |
def xoring(): | |
with open('4.txt', 'r') as myfile: | |
for lines in myfile: | |
lines = lines.strip() | |
if len(lines) == 60: | |
line_length_half = int(len(lines) / 2) | |
for hexnumber in range(0xff): | |
hexnumber = hex(hexnumber) | |
hexnumber = hexnumber.replace('0x', '') | |
if len(hexnumber) == 1: | |
hex_number_extended = hexnumber * len(lines) | |
else: | |
hex_number_extended = hexnumber * line_length_half | |
lines_bytearray = bytearray.fromhex(lines) | |
hexnumber_bytearray = bytearray.fromhex(hex_number_extended) | |
xored = bytearray(lines_xor ^ hexnumber_xor for lines_xor, hexnumber_xor | |
in zip(lines_bytearray, hexnumber_bytearray)) | |
text = xored.decode(encoding='utf-8', errors='replace') | |
try: | |
with open('file-complete.txt', 'a+') as file1: | |
if all(characters in string.printable for characters in text): | |
file1.write(text + '\n') | |
file1.close() | |
except FileExistsError: | |
os.remove('file-complete.txt') | |
with open('file-complete.txt', 'a+') as file1: | |
if all(characters in string.printable for characters in text): | |
file1.write(text + '\n') | |
file1.close() | |
def frequency_analysis(text): | |
assert (type(text) is bytes) | |
# frequency count | |
freqs = [0] * 256 | |
for char in text: | |
freqs[char] += 1 | |
# normalise | |
total = sum(freqs) | |
freqs = [f / total for f in freqs] | |
# calculate sum of squares of differences | |
score = sum((a - b) ** 2 for a, b in zip(freqs, byte_frequency)) | |
return score | |
# | |
# noinspection PyTypeChecker | |
def in_progress(): | |
with open('file-complete.txt', 'r') as file2: | |
frequency_dict = {} | |
for lines in file2: | |
lines = lines.strip() | |
if not re.match(r'^\s*$', lines): | |
analyzed_lines = frequency_analysis(bytes(lines, encoding="utf-8")) | |
frequency_dict.update({lines: analyzed_lines}) | |
frequency_dict = dict(sorted(frequency_dict.items(), key=itemgetter(1))) | |
print(list(frequency_dict.keys())[0]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment