from flask import Flask, make_response, request
from Crypto.Util.number import *
from Crypto.Cipher import ChaCha20_Poly1305
import secrets
import requests
import hashlib
CHACHA_KEY = secrets.token_bytes(32)
app = Flask(__name__)
encrypted_text = []
@app.route('/api', methods=['POST'])
def api():
try:
pkt = request.headers.get('pkt')
msg = bytes.fromhex(pkt)
nonce = b"random_text_Lorem_ipsum_sim_ahmet_next_ales"
nonce = hashlib.sha256(msg[:32] + nonce[:32]).digest()[:12]
cipher = ChaCha20_Poly1305.new(key=CHACHA_KEY, nonce=nonce)
ct, tag = cipher.encrypt_and_digest(msg)
ctk = ct + tag + nonce
encrypted_text.append(ctk.hex())
return make_response(ctk.hex())
except:
return make_response("Error")
@app.route('/resource', methods=['POST'])
def send_resource():
try:
pkt = request.headers.get('pkt')
ip = request.headers.get('ip')
if pkt in encrypted_text:
return make_response("You already try that!")
msg = bytes.fromhex(pkt)
ct = msg[:-28]
tag = msg[-28:-12]
nonce = msg[-12:]
cipher = ChaCha20_Poly1305.new(key=CHACHA_KEY, nonce=nonce)
try:
pt = cipher.decrypt_and_verify(ct, tag)
except (ValueError, KeyError):
return make_response("Decryption failed")
if pt:
try:
url = "http://" + ip
resource = open("resource.txt","r").read().strip()
requests.post(url, data=resource, timeout=1)
encrypted_text.add(pkt)
return make_response('Success')
except Exception as e:
return make_response("Error sending resource")
except Exception as e:
return make_response("Error")
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5002)
So this is a server where is a ChaCha20_Poly1305 algorithm,
If i use P1 = [64Bytes + ‘A’] in hexa and P2=[64Bytes + ‘C’] to encrypt datas I get C1 =ciphertext and C2=ciphertext
For the server to give me the resource i need to provide it a ciphertext forged by me without being added on server in encrypted_text list.
I figured out that if I do (C1⊕C2) = I will get zeros with a ‘2’ + tag(which i need to sign datas from ciphertext) + nonce which will be reused cause i know to do it so
How do I find the tag from this problem? it is from poly1305 algorithm that needs CHACHA_KEY
Daniel Burcea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.