ida로 살펴봤을때
strcmp로 비교하기 전에 아마 flag 문자열을 만들어 놓았을 것이다. 그래서 gdb를 통해 해당 주소까지 실행을 시켜본다.
바로 flag값이 보인다.
아니 왠 py 파일이 있다 반갑다.
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
import base64
import hashlib
password = b"k0secretpassword"
def pad(s):
block_size = AES.block_size
padding_size = block_size - len(s) % block_size
if isinstance(s, str):
s = s.encode('utf-8')
return s + bytes([padding_size] * padding_size)
def calculate_md5(input_string):
md5_hash = hashlib.md5(input_string)
return md5_hash.hexdigest()
def encrypt_AES_ECB(data):
cipher = AES.new(password, AES.MODE_ECB)
padded_data = pad(data)
cipher_text = cipher.encrypt(padded_data)
return base64.b64encode(cipher_text)
def calculate_sha1(input_string):
sha1_hash = hashlib.sha1(input_string)
return sha1_hash.hexdigest()
def calculate_sha256(input_string):
sha256_hash = hashlib.sha256(input_string)
return sha256_hash.hexdigest()
function_list = [calculate_md5, encrypt_AES_ECB, calculate_sha1, calculate_sha256]
encrypt_list = ['c394190f83d12181dd10c51102761404',b'PIPDCvQOR/ZbJrN7S/MgQgMcyoMQkXHfZ8h+Ypy8WEMHuPUcNMfXMa3OBLiaSqeQ','dce38cbddcdf234ff55de95edb3dd3bb6ec88f53','aef91ed153ed2a117ecc1ec9b538b67ead4664cb5dbf92a36333f0cbcb6ae1f8']
data = input("Input the string : ").encode()
for func,enc in zip(function_list,encrypt_list) :
if func(data) != enc:
print("Nah...")
exit(1)
print("Correct!")
이건 각각 md5, AES_ECB, sha1, sha256 방식으로 암호화하는 그런 함수다. 이들중 복호화가 가능한건 AES_ECB 뿐 그러니까 해당 암호화 방식에 대해 복호화 함수를 만들어 encrypt_list에 대입시켜본다.
from Crypto.Cipher import AES
import base64
def unpad(s):
padding_size = s[-1]
return s[:-padding_size]
def decrypt_AES_ECB(encrypted_data):
cipher = AES.new(password, AES.MODE_ECB)
decoded_data = base64.b64decode(encrypted_data)
decrypted_data = cipher.decrypt(decoded_data)
unpadded_data = unpad(decrypted_data)
return unpadded_data.decode('utf-8')
decrypt_AES_ECB(encrypt_list[1])
'K0{Reversing_and_Crypto_are_friends}' # 플래그값이 나온다.
조금 생각을 잘못한 부분이 있어서 푸는데 오래걸렸던 문제
다른 함수 부분은 크게 중요한건 없고

요런 함수가 있어서 입력한 문자열의 유효성을 검증하고 있다... 그런데 rand함수로 랜덤한 값으로 XOR 처리를 해버린다.
다행인건 이미지 캡쳐는 하지 않았지만, 앞에서 srand로 시드값을 고정해놓아서 매번 같은 값이 나올것이다.

gdb로 run 시켜보면서 해보면 (일단 Stella라는 name을 앞에서 지정된 리스트 인덱스를 입력해야 넘어가게 해놓았었음)

al 레지스터에 rand 값이 저장되니 표시하도록 display 명령어를 걸어주고

이렇게 한줄 실행하면 0x49 값이 들어가는 것을 알 수 있다.
검증 대상인 byte_4040D8이라는 배열은 값을 알 수 있으니... 한 칸씩 실행해보며 flag값을 찾아보면

K0{I'm_here}