Countable Clouds
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# LIT CTF ## The Oracle's Prophecy In this challenge, we're given an implementation of AES, which is remarkably similar to CBC mode. We're allowed to encrypt a string with some flavor text that contains the flag and then the user input. A MAC is appended at the end of the message before encryption, a SHA1 hash of a substring of the message from 223-239. The message's padding is key to solving the problem. Rather than the traditional method of padding following a message, in this implementation, our padding precedes the message. Secondly, there is a decryption oracle which allows you to decrypt a string, and see if the decrypted ciphertext has a valid MAC and valid padding. ```python= def decrypt(self,ct): if(len(ct) % (2 * self.BLOCK_SIZE) != 0): print("Something went wrong while fulfilling the prophecy D:"); return; iv = ct[:32]; c = ct[32:]; msg = self.decryptHex(iv,c); msg = self.check_padding(msg); if(msg): if self.check_MAC(msg): print("The prophecy is fulfilled!"); else: print("Something went wrong while fulfilling the prophecy D:"); else: print("Something went wrong while fulfilling the prophecy D:"); return; ``` The check padding function checks the first byte of the message. It uses PKCS#7 padding, but doesn't actually check every byte. So, it simply removes the first n bytes of the message, if n is the first byte. So, let's get into the specifics of what type of AES this is, if not CBC. Here's a diagram (THE diagram) that shows the encryption and decryption process of AES CBC: ![](https://i.imgur.com/OuEB6Re.png) ![](https://i.imgur.com/q73RxaM.png) The only difference between CBC and the implementation version is that ```prevXOR```, i.e. the IV that's used for a given block in the chain, is the ciphertext of the previous block in the chain for AES CBC, but ```ct ^ pt``` for our implementation. So how do we do this decryption shtick. Well normally, in AES CBC, you alter the inputted IV, and as you can see in the diagram, that only affects the plaintext for the first block. That all changes in this implementation version, however. You'll notice that the final plaintext of every block is fed into the IV for the next one. So, if we take our original IV, and xor it with a new value, our entire outputted decrypted plaintext would be xorred by the new value as well. That doesn't actually help us much, for two reasons. Firstly, the only way to leak the flag would be for it to be the very first block, so you can do some kind of padding oracle (which only operates on the first block). However, the flag isn't the first block yet, and we can't really put it there yet. Not to mention, xorring the whole message would ruin the outputted plaintext, since the SHA1 output would be ruined. Somehow, we need to change our ciphertext to get the flag at the beginning, while also preserving the message from 223-239, and preserving the end of the message, the SHA1 hash. We achieve this with one key observation. Any permutation of the first n blocks, in this scheme, doesn't affect the decryption of any of the blocks after the first n. To be exact, the final effective IV after the n blocks is the xor of $D(c_i), 0\leq i \leq n$, where c_i is the ith block of the ciphertext xored further with the initial IV. You can verify this yourself, and since xor is commutative and all that shit, it works out. So yay we have a plan! We can move the flag block to the very beginning, and that doesn't affect the decryption of 223-239 or the SHA1 hash. We notice two things that can solve this whole problem! Firstly, we know the effective IV of the flag, namely the xor of the plaintext block that preceded it and its encryption. We have both since we encrypted the plaintext and got the ciphertext. Of course, we also have the initial IV, which was chosen randomly at encryption but returned to us along with the ciphertext. So, if we move the flag block to the beginning, even though the later blocks stay the same, the flag block would now change. To be precise, looking at our diagram, its initial decryption would be xored with the effective IV and the initial IV. So, if the xor of two knowns and the flag block gives the right first byte to pass the padding, we can deduce what the initial decryption of the first byte of the flag block was. Which is what we want! So what final bytes pass the padding? Well, if there used to be 3 bytes of padding, then the only first byte that works is 3. You might think, at first glance, that any number from 1 to 16 would work. However, the padding bytes are removed before the MAC is verified. And the MAC is verified with an indexed substring! The index changes if the padding byte is different. Anyway, good news is, if we get 256 oracle decryptions on average, we can get one byte of the flag! Moreover, we can choose a specific flag byte, by varying the block of the plaintext that we bump to the beginning, as well as the number of padding bytes that are added to the beginning of the message (which we control with our arbitrary input). Tada! That's all! ```python= def leak_byte(input_length, block_index): input_str ='a'*input_length spell = 'f'*25 ## The length of the flag temp_message = 'Double, double toil and trouble;\nFire burn and caldron bubble.\nWhen midnight strikes and the god dislikes,\nI swear on my soul that\n{0}\n{1}\nIn thunder, lightning, and in rain.\nFather of omens! Give me blood beyond sight!\n'.format(spell,input_str) h = hashlib.sha1() h.update(temp_message[2*223:2*239].encode("utf-8")); temp_message = pad(temp_message.encode("utf-8").hex() + h.digest().hex()) r = remote('oracleprophecy.litctf.live', 1337) r.recv() input_str = bytes(input_str, encoding='utf-8') while True: out = encrypt(r, input_str) ## Encrypts our message of choice iv, ct = out[:32], out[32:] in_b = block(temp_message, 32) xor_byte = int(temp_message[:2], 16) out_b = block(ct, 32) if not counter: print(len(ct)) new_out_b = [out_b[block_index+1]] + out_b[:block_index+1] + out_b[block_index+2:] old_iv = xorHex(in_b[block_index],out_b[block_index]) if decrypt(r, iv+''.join(new_out_b)): ## Decryption is true if padding and MAC verification succeed return chr(int(xorHex(old_iv, iv)[:2], 16) ^ xor_byte) break ``` Yeah so this does the thing. About actually getting the flag in order and all that, I didn't automate it. But I'm sure you can :). ```flag{4j4c3nt_5w4p_4tt4ck}``` This flag references the fact that this implementation is actually called AES-PCBC. Ajacent. :)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully