وقتی فایل متنی رو میبینیم با توجه به منحنی و فرمتی که داره متوجه میشیم که با Elliptic-Curve طرف هستیم. در واقع این منحنی بیضوی در یک میدان محدود از نقاطی تشکیل شده که باید به معادله به فرمت زیر را برآورده کنه:
y^2 =x^3 + ax + b
و با توجه به عنوان سوال حدس میزنیم که هدف scalar multiplication هست.
importhashlibfromCrypto.CipherimportAESfromCrypto.Util.Paddingimportpad,unpadfromellipticcurveimport*# I'll use my own library for thisfrombase64importb64encodeimportosfromCrypto.Util.numberimportgetPrimedefencrypt_flag(shared_secret:int,plaintext:str):iv=os.urandom(AES.block_size)#get AES key from shared secretsha1=hashlib.sha1()sha1.update(str(shared_secret).encode('ascii'))key=sha1.digest()[:16]#encrypt flagplaintext=pad(plaintext.encode('ascii'),AES.block_size)cipher=AES.new(key,AES.MODE_CBC,iv)ciphertext=cipher.encrypt(plaintext)return{"ciphertext":b64encode(ciphertext),"iv":b64encode(iv)}defmain():the_curve=EllipticCurve(13,245,getPrime(128))start_point=Nonewhilestart_pointisNone:x=getPrime(64)start_point=the_curve.point(x)print("Curve: ",the_curve)print("Point: ",start_point)new_point=start_point*1337flag="byuctf{REDACTED}"print(encrypt_flag(new_point.x,flag))if__name__=="__main__":main()
خب نگاهی به اسکریپت داده شده میندازیم و میبینیم که بعله همونطور که حدس میزدیم هدف محاسبه scalar multiplication با n=1337 و بعدش مختصات x حاصل از خروجی scalar multiplication رو به عنوان shared secret و فلگ رو به عنوان plaintext به تابع encrypt_flag داده می شود.
در واقع تمام کاری که ما باید انجام بدیم اینه که یک تابع scalar multiplication بنویسیم و بعد اون یک تابع برای decrypt کردن فلگ با توجه AES در مد CBC بنویسیم ( همانطور که میبنید ciphertext و iv در فایل times.txt قرار داده شده است ).
fromCrypto.CipherimportAESfrombase64importb64decodefromCrypto.Util.Paddingimportunpaddefdecrypt_flag(shared_secret:int,ciphertext_dict:dict):# Derive AES key from shared secret (same as encryption)sha1=hashlib.sha1()sha1.update(str(shared_secret).encode('ascii'))key=sha1.digest()[:16]# Retrieve ciphertext and IV from dictionaryciphertext=b64decode(ciphertext_dict["ciphertext"])iv=b64decode(ciphertext_dict["iv"])# Decrypt the ciphertextcipher=AES.new(key,AES.MODE_CBC,iv)plaintext=unpad(cipher.decrypt(ciphertext),AES.block_size)# Return the decrypted stringreturnplaintext.decode('ascii')shared_secret=130102914376597655583988556541378621904ciphertext_dict={'ciphertext':b'SllGMo5gxalFG9g8j4KO0cIbXeub0CM2VAWzXo3nbIxMqy1Hl4f+dGwhM9sm793NikYA0EjxvFyRMcU2tKj54Q==','iv':b'MWkMvRmhFy2vAO9Be9Depw=='}decrypt_flag(shared_secret,ciphertext_dict)