'2019/11/08'에 해당되는 글 2건

주어진 소스는 다음과 같다.


source.py


import hashlib

import sys


def repeat(str1,length):

    return (str1*(length//len(str1)+1))[:length]


def operation(str1,str2):

    s=""

    for ch1,ch2 in zip(str1,str2):

        AopB = ~(ord(ch1)&ord(ch2))

        s+=chr(~((~(ord(ch1)&AopB))&(~(ord(ch2)&AopB))))

    return s


key = ""

plainText = "HAN_Was_sad_because_SKT1_LoST_THe_LOL_World_Cup"


plainText += key

plainText += hashlib.md5(plainText).hexdigest()

cipherText = operation(plainText,repeat(key,len(plainText)))

print(cipherText.encode('hex')) 

# result is 03021a192c323b6f243e096c3d110b042a3e56316012754c140f3b152f0c1c783200217c132b3f0a2d215731702c51360817123d281b786708325e6c2b1c0d3a127e5d5d6a785c1c7b2064764c6b2b0167675d053912590766280508506b101e727b6c7f483278

    

주어진 평문과 암호 값을 통해 키 값을 알아내면 된다. operation 함수에서 평문과 키를 통해 비트 연산들을 수행하는데, 암호화 값을 생성할 때 평문과 키 값을 1바이트씩 1:1 매칭해서 암호화 값을 만들어내기 때문에 1자리씩 브포 돌려서 키 값을 구해주면 된다.


solve.py


import hashlib

import sys


def repeat(str1,length):

    return (str1*(length//len(str1)+1))[:length]


def operation(str1,str2):

    s=""

    for ch1,ch2 in zip(str1,str2):

        AopB = ~(ord(ch1)&ord(ch2))

        s+=chr(~((~(ord(ch1)&AopB))&(~(ord(ch2)&AopB))))

    return s


enc_data = "03021a192c323b6f243e096c3d110b042a3e56316012754c140f3b152f0c1c783200217c132b3f0a2d215731702c51360817123d281b786708325e6c2b1c0d3a127e5d5d6a785c1c7b2064764c6b2b0167675d053912590766280508506b101e727b6c7f483278"

plainText = "HAN_Was_sad_because_SKT1_LoST_THe_LOL_World_Cup"

flag = ""


for j in range(0,len(plainText)*2,2):

    for i in range(0,127):

        key = chr(i)

        tmp = plainText[j/2]

        tmp += key

        tmp += hashlib.md5(tmp).hexdigest()

        cipherText = operation(tmp,repeat(key,len(plainText)))

        if cipherText.encode('hex')[0:2]==enc_data[j:j+2]:

            flag += key


print flag

블로그 이미지

JeonYoungSin

메모 기록용 공간

,

주어진 소스는 다음과 같다. 옛날 문제라 문제 서버가 닫혀있어서 그냥 소스에 플래그 임의로 구성해서 풀었다.


source.py

from Crypto.Util.number import *
import uuid

BLOCKSIZE = 32

key_1 = uuid.uuid4().hex
key_2 = uuid.uuid4().hex

flag = "HZVII{Test_Flag_Easy_Xor_Challenge!!!!!!!!!!!!!}"


def menu():
    print()
    print('[1] Encrypt')
    print('[2] Get Flag')
    print('[3] Exit')
    return input()


def chunky(string):
    return (string[0 + i:BLOCKSIZE + i] for i in range(0, len(string), BLOCKSIZE))


def encrypt(m):
    m = m
    cipher = ""
    chunks = list(chunky(m))
    if len(chunks) > 10:
        return "Calm down i'm not your slave .. "
    key = key_1
    for n in chunks:
        cipher += hex(int(n, 16) ^ int(key, 16) ^ int(key_2, 16))[2:]
        key = hex(int(key, 16) // 2)[2:]
    return cipher

while True:

    choice = menu()

    if choice == '1':
        m = bytes_to_long(input('\nYour_Plain >> ').strip().encode())
        print('\nCipher: ' + str(encrypt(hex(m)[2:])))

    elif choice == '2':
        print('\nFlag: ' + encrypt(hex(bytes_to_long(flag.encode()))[2:]))

    elif choice == '3':
        print('See ya H4cK3r')
        break


코드를 보면 키 값을 알 수 없는 상황이지만 암호화가 단순 xor 연산이라 그냥 플래그랑 동일한 크기의 인풋 으로 암호화 값을 구하고 이거 두 개를 xor해서 key 값을 구해주면 된다. 그 다음 플래그 암호화 값을 구해서 키 값이랑 위 코드 암호화 로직에 맞춰  xor 돌려주면 플래그를 구할 수 있다.



decrypt.py


from Crypto.Util.number import *

  

def get_key(input,enc_input):

    hex_input = hex(bytes_to_long(input.encode()))[2:]

    key_list = []


    for i in range(0,len(hex_input),32):

        key_list.append(int(hex_input[i:i+32],16)^int(enc_input[i:i+32],16))


    return key_list


def decrypt(enc_flag,key_list):

    dec_flag = ""

    for i in range(0,len(enc_flag),32):

        dec_flag += bytes.fromhex(hex(int(enc_flag[i:i+32],16)^key_list[int(i/32)])[2:]).decode("utf-8")

    return dec_flag


enc_flag = "8719d71f952e5ac948ae838f2d443c63db783fd5faed08878d56e93b16d74449d1d10b70b864727807d30eb2c6553d57"

input = "1"*48

enc_input = "fe72b067ed643f9d0aebedf870146a0daf287d9d948456c4e324b06b4b8a101687851b60a874626817c31ea2d6452d1b"


key_list = get_key(input,enc_input)

print(decrypt(enc_flag,key_list))


'Crypto & Network & Etc > Crypto Practice' 카테고리의 다른 글

Plaid CTF 2015 Strength  (0) 2019.11.11
TokyoWesterns CTF 2019 baby_rsa  (0) 2019.11.11
KCTF Operation 1  (0) 2019.11.08
TG:Hack 2019 CTF Josefssons Final Exam  (0) 2019.11.07
RITSEC CTF 2018 Nobody uses the eggplant emoji  (0) 2019.11.07
블로그 이미지

JeonYoungSin

메모 기록용 공간

,