Beispiel #1
0
def main():
    s.get('http://hacktactoe.dctf-f1nals-2017.def.camp/action.php?action=init')
    original_state = get_state()
    pt = 'a' * 100
    s.get('http://hacktactoe.dctf-f1nals-2017.def.camp/action.php?name=' + pt)
    state_with_long_name = get_state()
    matching_block = state_with_long_name[112:112 + len(pt)]
    keystream = xor_string(matching_block, pt)
    print(xor_string(original_state[112:], keystream))
    print(keystream.encode("hex"))
    print(xor_string(original_state, keystream[:32] * 10))
Beispiel #2
0
def format_potential_key(ciphertexts, second_xored_ct_index, missing_bytes,
                         start_position, uncovered_content):
    return "?" * start_position + xor_string(
        uncovered_content,
        ciphertexts[second_xored_ct_index][start_position:start_position +
                                           len(uncovered_content)]
    ).encode("hex") + "?" * (missing_bytes - start_position)
Beispiel #3
0
def interactive_hack(xored, ciphertexts):
    from builtins import input, range
    while True:
        potential_plaintext_contents = input(">")
        ciphertext_len = len(ciphertexts[0])
        if len(potential_plaintext_contents) > ciphertext_len:
            err = "Can't break more than a single block at a time! Taking prefix '%s'"
            print(err % potential_plaintext_contents[:ciphertext_len])

        max_missing_bytes = max(map(len,
                                    xored)) - len(potential_plaintext_contents)
        for start_position in set(range(max_missing_bytes)).union({0}):
            for index, xored_ciphertext in enumerate(xored):
                number_of_ciphertexts = len(ciphertexts)
                first_xored_ct_index = index / number_of_ciphertexts
                second_xored_ct_index = index % number_of_ciphertexts
                if len(
                        xored_ciphertext
                ) > start_position and first_xored_ct_index != second_xored_ct_index:
                    uncovered_content = xor_string(
                        potential_plaintext_contents,
                        xored_ciphertext[start_position:start_position +
                                         len(potential_plaintext_contents)])
                    print(
                        'in ' + str(first_xored_ct_index),
                        'ct ' + str(second_xored_ct_index),
                        'offset ' + str(start_position),
                        uncovered_content, 'key=(' + format_potential_key(
                            ciphertexts, second_xored_ct_index,
                            max_missing_bytes, start_position,
                            uncovered_content) + ')')
def format_potential_key(ciphertexts, index, missing_bytes, start_position,
                         uncovered_content):
    partial_input = ciphertexts[index][start_position:start_position +
                                       len(uncovered_content)]
    return "?" * start_position + xor_string(
        uncovered_content,
        partial_input).encode("hex") + "?" * (missing_bytes - start_position)
def repeating_key_xor(ciphertexts):
    """
    Run interactive session of repeating key xor breaking.
    :param ciphertexts: list of ciphertexts xored with the same repeating key
    """
    xored = [xor_string(ciphertexts[0], d) for d in ciphertexts[1:]]
    interactive_hack(xored, ciphertexts)
Beispiel #6
0
def main():
    cts = []
    for i in range(1, 7):
        with codecs.open("encrypted/" + str(i) + "e", "r") as input_file:
            data = input_file.read().strip()
            data = xor_string(chr(ord('&') ^ ord(' ')) * len(data), data)
            print(chr(ord('&') ^ ord(' ')) * len(data))

            cts.append(data)
    print(cts)
Beispiel #7
0
def run(s):
    lines = s.split("\n")
    for line in lines:
        lin = line.strip()
        if "str1" in lin:
            str1 = lin[5:]
        if "str2" in lin:
            str2 = lin[5:]

    return xor_string(str1, str2)
Beispiel #8
0
def repeating_key_xor(ciphertexts):
    """
    Run interactive session of repeating key xor breaking.
    :param ciphertexts: list of ciphertexts xored with the same repeating key
    """
    import itertools
    xored_ciphertexts = [
        xor_string(first, second)
        for (first, second) in itertools.product(ciphertexts, repeat=2)
    ]
    interactive_hack(xored_ciphertexts, ciphertexts)
def interactive_hack(xored, ciphertexts):
    while True:
        print(">")
        potential_contents = raw_input()
        ct_len = len(ciphertexts[0])
        if len(potential_contents) > ct_len:
            print(
                "Can't break more than a single block at a time! Taking prefix '"
                + potential_contents[:ct_len] + "'")
        for index, xored_ciphertext in enumerate(xored):
            missing_bytes = len(xored_ciphertext) - len(potential_contents)
            for start_position in set(range(missing_bytes) + [0]):
                uncovered_content = xor_string(
                    potential_contents,
                    xored_ciphertext[start_position:start_position +
                                     len(potential_contents)])
                print(
                    uncovered_content, 'key=(' +
                    format_potential_key(ciphertexts, index, missing_bytes,
                                         start_position, uncovered_content) +
                    ') or ' + 'key=(' +
                    format_potential_key(ciphertexts, index + 1, missing_bytes,
                                         start_position, uncovered_content))
Beispiel #10
0
    0x6A,
    0x15,
    0x6D,
    0xB,
    0x9D,
    0xF0,
    0xC2,
    0x34,
    0x74,
    0x8A,
    0xD4,
    0x4F,
    0x50,
    0x84,
    0xA0,
    0x7F,
]

possible_rounds = map(convert_array_to_strings, possible_rounds)
ct = convert_array_to_strings(ct)
last = convert_array_to_strings(last)
xorkey = convert_array_to_strings(xorkey)

for i in itertools.permutations(possible_rounds):
    A = AES()
    rounds = i
    try_ct = A.sr_decryptlast(ct, last)
    for r in rounds:
        try_ct = A.sr_decrypt(try_ct, r)
        print(xor_string(try_ct, xorkey))
Beispiel #11
0
from crypto_commons.generic import xor_string
plugin_name = "两个字符串异或操作"
plugin_version = "v1.1"
plugin_author = "1me"
plugin_time = "20200429"
plugin_info = '''
两个字符串异或操作
格式:
str1=abcdefghigk
str2=efg
----------------------
代码:
from crypto_commons.generic import xor_string
xor_string(str1,str2)
'''


def run(s):
    lines = s.split("\n")
    for line in lines:
        lin = line.strip()
        if "str1" in lin:
            str1 = lin[5:]
        if "str2" in lin:
            str2 = lin[5:]

    return xor_string(str1, str2)