Question: Could you help me with the decryption? # ! / usr / bin / python 3 # Run me like this: # $ python 3
Could you help me with the decryption?
#usrbinpython
# Run me like this:
# $ python paddingoracle.py "WEBSITE" ad
# or select "Padding Oracle" from the VS Code debugger
import json
import sys
import time
from typing import Union, Dict, List
import requests
# Create one session for each oracle request to share. This allows the
# underlying connection to be reused, which speeds up subsequent requests!
s requests.session
def oracleurl: str messages: Listbytes ListDictstr str:
while True:
try:
r sposturl datamessage: mhex for m in messages
rraiseforstatus
return rjson
# Under heavy server load, your request might time out. If this happens,
# the function will automatically retry in seconds for you.
except requests.exceptions.RequestException as e:
sysstderr.writestre
sysstderr.write
Retrying in seconds...
time.sleep
continue
except json.JSONDecodeError as e:
sysstderr.writeIts possible that the oracle server is overloaded right now, or that provided URL is wrong.
sysstderr.writeIf this keeps happening, check the URL. Perhaps your uniqname is not set.
sysstderr.writeRetrying in seconds...
time.sleep
continue
def main:
if lensysargv:
printfusage: sysargv ORACLEURL CIPHERTEXTHEX", filesysstderr
sysexit
oracleurl, message sysargv bytes.fromhexsysargv
if oracleoracleurl, messagestatus "valid":
printMessage invalid", filesysstderr
#
# TODO: Decrypt the message
#
decrypted "TODO"
printdecrypted
if namemain:
main
Here is some Python code representing what we suspect the server does when the verify
endpoint is accessed:
from Crypto.Cipher import AES
from Crypto.Hash import HMAC, SHA
from Crypto.Random import getrandombytes
def padmessage:
n AES.blocksize lenmessage AES.blocksize
if n : n AES.blocksize
return message bytesnn
def unpadmessage:
n message
if n or n AES.blocksize or messagen: bytesnn:
raise Exceptioninvalidpadding'
return message:n
def encryptmessage key:
iv getrandombytesAESblocksize
cipher AES.newkey AES.MODECBC iv
return iv cipher.encryptpadmessage
def decryptciphertext key:
if lenciphertext AES.blocksize: raise Exceptioninvalidlen'
if lenciphertextAESblocksize: raise Exceptioninvalidiv
iv ciphertext:AES.blocksize
cipher AES.newkey AES.MODECBC iv
return unpadcipherdecryptciphertextAESblocksize:
Page
def hmacmessage mackey:
h HMAC.newmackey, digestmodSHA
hupdatemessage
return hdigest
def verifymessage mac, mackey:
if mac hmacmessage mackey:
raise Exceptioninvalidmac'
def macThenEncryptmessage key, mackey:
return encryptmessage hmacmessage mackey key
def decryptThenVerifyciphertext key, mackey:
plaintext decryptciphertext key
message, mac plaintext:SHAdigestsize
plaintextSHAdigestsize:
verifymessage mac, mackey
return message
@app.routeverify methodsPOST
def decoracleroute:
ciphertext bytes.fromhexrequestformmessage
try:
decryptThenVerifyciphertext KEY, MACKEY
excepte:
return status: e
return status: 'valid'
Hint: The real verify endpoint is a bit more powerful. It can accept multiple message
arguments, in which case it returns the results as a JSON array, in the same order as the
arguments. See the oracle function in the starter code for more details.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
