AES-CTR Cryptography: Reused Key Weakness - HackTheBox Cyber Apocalypse CTF

Поділитися
Вставка
  • Опубліковано 16 жов 2024
  • If you would like to support the channel and I, check out Kite! Kite is a coding assistant that helps you code faster, on any IDE offer smart completions and documentation. www.kite.com/g... (disclaimer, affiliate link) Moving your first steps into hacking? Start from HTB Academy: bit.ly/3vuWp08
    Hungry for more hacking training? Join Hack The Box now: bit.ly/331nQCl
    For more content, subscribe on Twitch! / johnhammond010
    If you would like to support me, please like, comment & subscribe, and check me out on Patreon: / johnhammond010
    PayPal: paypal.me/john...
    E-mail: johnhammond010@gmail.com
    Discord: johnhammond.or...
    Twitter: / _johnhammond
    GitHub: github.com/Joh...

КОМЕНТАРІ • 62

  • @Paju_
    @Paju_ 3 роки тому +137

    I would like to point out that unlike you make it out to be in this video, reusing keys with CTR mode isn't insecure by design. The actual problem lies in reusing the same initialization vector value (IV) with multiple encryptions with the same key. The IV values should be nonces (or 'number used only once') to protect against this attack. Usually these nonce values are achieved by using a running counter value added to the original IV value (IV || CTR[i]), hence the name counter mode. Let me demonstrate the attack and how to prevent it:
    Ciphertext1 = Plaintext1 ⊕ AES(key, IV)
    Ciphertext2 = Plaintext2 ⊕ AES(key, IV)
    Which leads to the following ciphertext pair:
    Ciphertext1 ⊕ Ciphertext2 = Plaintext1 ⊕ AES(key, IV) ⊕ Plaintext2 ⊕ AES(key, IV)
    Now, because the (key, IV) pair is reused, the AES(key, IV) will yield the same result for both ciphertexts.
    This means that an attacker can now compute Ciphertext pairs easily by cancelling the AES encryption out of the equation (XORing anything by itself will always yield to 0):
    Ciphertext1 ⊕ Ciphertext2 = Plaintext1 ⊕ Plaintext2
    Therefore an attacker can easily get the Plaintext2 value by computing the following operation:
    Plaintext2 = Plaintext1 ⊕ Ciphertext1 ⊕ Ciphertext2
    As was demonstrated in this video.
    When using the counter mode properly, we get the ciphertexts in the following way:
    Ciphertext1 = Plaintext1 ⊕ AES(key, (IV || CTR[0]))
    Ciphertext2 = Plaintext2 ⊕ AES(key, (IV || CTR[1]))
    Which leads to the following ciphertext pair:
    Ciphertext1 ⊕ Ciphertext2 = Plaintext1 ⊕ AES(key, (IV || CTR[0]))
    ⊕ Plaintext2 ⊕ AES(key, (IV || CTR[1]))
    Now, because the AES encryption operations yield different results, an attacker can no longer just cancel the AES encryptions out and would actually need to compute the values themselves. Even if the attacker knows the original IV value, they have no way of actually computing these without obtaining the key! Therefore, the attack is rendered useless whenever unique (key, IV) pairs are used.
    The code in question should be fixed by making the following change to the counter:
    iv = os.urandom(16)
    ctr = Counter.new(128, int.from_bytes(iv, byteorder='big'))
    cipher = AES.new(KEY, AES.MODE_CTR, counter=ctr)

    • @gareth4168
      @gareth4168 3 роки тому +7

      This is exactly right - the real issue here is not re-using a key but re-using the IV / nonce for a given key. That is a school boy fail!

    • @ghawk1347
      @ghawk1347 3 роки тому +3

      I find it interesting that you use the syntax "Ciphertext1 = Plaintext1 ⊕ AES(key, IV)" and have a few questions:
      1. Is the plaintext itself not actually fed into the AES algorithm?
      2. Is the plaintext really just XORed with the AES output using some IV as input?
      3. Would feeding the plaintext itself into the AES algorithm provide any marginal security benefit vs XORing it with the AES output of some IV?
      4. My understanding is that AES outputs the same number of bytes in the input. For the XOR operation to work, does the IV need to be the same length as the plaintext? Put differently, how is the AES output padded (if at all) to allow for an XOR with the plaintext?

    • @gareth4168
      @gareth4168 3 роки тому +2

      @@ghawk1347 1. Counter mode operation works by encrypting a counter or other number only used once (nonce) with a key to produce a unpredictable output. This output is usually called "keystream" and must never be reused, as Arttu explained. This keystream is xor'd with the plaintext to produce ciphertext. CTR mode does not put the plaintext into the AES algorithm. Look up a diagram of counter mode operation on wiki etc.
      2. No - only the counter is put into the AES cipher. This works so long as you're careful about how you choose / maintain those inputs.
      3. Done properly AES-CTR is secure. It's used in AES-GCM (Galois counter mode) which is still pretty much state of the art for example. The main difference between these two is that AES-CTR does nothing whatsoever to protect the integrity of the encrypted message - only its confidentiality.
      3. The AES block cipher (for any key length - 128/192/256) has a block size of 128 bits. That means the input to the cipher is 128 bits, as is the output. CTR mode effectively converts a block cipher into a stream cipher meaning you can encrypt arbitrary sized plaintexts without padding. This is achieved by discarding the unused bits of keystream produced from the final encrypt operation; no padding is necessary.

    • @ghawk1347
      @ghawk1347 3 роки тому

      @@gareth4168 Thanks so much for the answer! That makes a lot of sense. I'll look into CTR and the other modes a bit more.

    • @sohailsaha7427
      @sohailsaha7427 3 роки тому +2

      You missed something critical with the source code in the CTF: each encryption run was initialiazing a new counter with no added variables, and so, each plaintext actually ended up using up the same initial value of the counter (because if both times the counter was initialized in the same way, which it was, then the initial counter value would also be the same).
      When John says 'keys', I think he probably means the end key which was used to encrypt the plaintext, and not the key which was provided from urandom. The end key remains the same, because this key is a 'mix' of the urandom key and the counter, both of which remains the same in both encryption runs, thus resulting in key reusage.
      Thanks for the comment though, it made me wanna look deeper into the problem.

  • @_JohnHammond
    @_JohnHammond  3 роки тому +9

    I did not have the right understanding for this challenge and did not give the right explanation in the video, and I'm sorry for that. You can find a solid explanation in Arttu Paju's comment pinned below and the other comments that explain where I went wrong in this one. Sorry!

    • @coolmanberr1738
      @coolmanberr1738 3 роки тому +1

      I really love how humble John is. You're the best man

  • @GaViNa352
    @GaViNa352 3 роки тому +48

    you + sleep deprivation = hilarious

  • @NateRoberts
    @NateRoberts 3 роки тому +5

    Hope you know your sleep deprivation hasn’t gone unappreciated, I seriously like camp out everyday after work looking forward to these. Love and appreciate you John!

  • @andreigrigoras1453
    @andreigrigoras1453 3 роки тому +2

    In this specific scenario, the actual vulnerability is the non-unique (nonce, key) pair between 2 distinct encryptions. As during the creation of the AES object no value for nonce(=IV) is specified, a default one is used and thus, 2 ciphertext will share the same default IV and key which makes it vulnerable

  • @Antkneee
    @Antkneee 3 роки тому +2

    "Your life should be in Dark Mode...." John Hammond
    That should be a famous quote!

  • @shivaminc.1467
    @shivaminc.1467 3 роки тому +2

    I really learn a lot through your videos, best part I also enjoy watching them again and again ❤️

  • @tqsprince
    @tqsprince 3 роки тому +14

    Dark mode John isn't bad at all

  • @reverendtoady7098
    @reverendtoady7098 3 роки тому +3

    your videos are so fun to watch and so educating

  • @claymoody
    @claymoody 3 роки тому

    nice video, I enjoyed the end credit bonus scene of crazy john with the lights. Keep it up, buddy.

  • @unknownanonymous4735
    @unknownanonymous4735 3 роки тому

    bro , the dark mode in the end was super duper cool ! test it one in a while :)

  • @HaouasLeDocteur
    @HaouasLeDocteur 3 роки тому

    This is my new favorite channel.

  • @ayush_panwar1
    @ayush_panwar1 3 роки тому

    Its 2 : 11 and im watching your video , i should also have to go to bed now good night John, btw awesome content as always ❗

  • @jorgevilla6523
    @jorgevilla6523 3 роки тому

    Thanks for the video John!

  • @christophertharp7763
    @christophertharp7763 3 роки тому

    learned something new again. Thanks John

  • @jb_lofi
    @jb_lofi 3 роки тому

    Real talk? The room looks great at the end there!

  • @alialavizadeh2775
    @alialavizadeh2775 3 роки тому

    amazing John

  • @matthewlandry1352
    @matthewlandry1352 3 роки тому +1

    DarkMODE for the Win.

  • @xB-yg2iw
    @xB-yg2iw 3 роки тому

    Awesome!

  • @TheH2OWeb
    @TheH2OWeb 3 роки тому +1

    I like dark mode ! Keep it :-)

  • @dani3l3_
    @dani3l3_ 3 роки тому +1

    Nice

  • @technicalgamer2565
    @technicalgamer2565 3 роки тому

    Addicted to you sir

  • @aryan2628
    @aryan2628 3 роки тому +13

    Just reusing a key and it breaks one of the most popular encryption algorithms

    • @onlyastron4ut
      @onlyastron4ut 3 роки тому +1

      That’s why randomization is such an important factor in crypto

    • @EverettWilson
      @EverettWilson 3 роки тому

      There's no crypto algorithm on the world that's immune to being used wrong.

    • @_Omni
      @_Omni 3 роки тому

      IV should not be the same 🤦‍♂️

  • @Explor1ngth3w0rld
    @Explor1ngth3w0rld 3 роки тому

    john sir king🤴🤴🤴🤴

  • @ajaykrishna7814
    @ajaykrishna7814 3 роки тому

    how many hours do you actually sleep in a day? appreciate your videos and knowledge sharing

  • @viv_2489
    @viv_2489 3 роки тому +1

    Cool video in dark mode ...

  • @JimmyGeschwind
    @JimmyGeschwind 3 роки тому

    Oh, so all you had to do was Xor? I did not know that worked for AES! I thought you had to brute force the urandom-value against the know string to find the key and then decrypt the flag. :p

    • @cybersecurity4466
      @cybersecurity4466 3 роки тому

      if you know enrypted text and plaintext...then you acquire the KEY (and IV in this example). but same key was used again, so you know Key (with same IV) and encrypted-Flag...then you acquire plaintext of Flag.

  • @rubiskelter
    @rubiskelter 3 роки тому

    I wonder if they called it PhaseStream3, or PS3, on purpose.. The first PS3 hack involved a bad PRNG .

  • @malfoytech4601
    @malfoytech4601 3 роки тому +7

    why don't u make python series where u gonna teach pentesting python to us. If this would happen gonna appreciate it vro🙏

    • @agowa338
      @agowa338 3 роки тому +1

      "pentesting python" is just advanced python...

    • @malfoytech4601
      @malfoytech4601 3 роки тому +1

      @@agowa338agreed. that's why we want little series where he gonna teach us all the modules. of python3.

    • @agowa338
      @agowa338 3 роки тому

      @@malfoytech4601 Why? Because you never learned how to read the documentation???

  • @SuryaTejaKarra
    @SuryaTejaKarra 3 роки тому

    how would you attempt this if the source string wasn't supplied?

    • @thowbikdustan6515
      @thowbikdustan6515 3 роки тому

      Hahaha well It's just a CTF challenge my boy, It's like think and solve it that's all.

  • @technicalgamer2565
    @technicalgamer2565 3 роки тому

    Love from india

  • @serdarcatal503
    @serdarcatal503 3 роки тому

    1 dislike is the ip john hammond hacked

  • @tituslawson8311
    @tituslawson8311 3 роки тому +1

    I see that you have Linux but... it’s not kali bro you need to try kali Linux it will change your life. Ps I love your videos keep up the good work 🙂🙂

    • @NicolastheThird-h6m
      @NicolastheThird-h6m 3 роки тому +2

      Bro he Completed oscp and i think he is going to tak OSCE this year ,and there you are saying him to use Kali.💀

  • @luks1337
    @luks1337 3 роки тому

    yeah I just start the video ... (i wr0t3 c0mm3n7 b3f0r3 st4r7ing l0l)

  • @_tartofraise
    @_tartofraise 3 роки тому +2

    You explained absolutly nothing in this video..Reusing the key is not the only problem here.

  • @pitzel
    @pitzel 3 роки тому

    ok

  • @swaevye9071
    @swaevye9071 3 роки тому

    What can you hack is the sky the limit or are their specifics

  • @0xhhhhff
    @0xhhhhff 3 роки тому

    Heartt

  • @Insomnia_2311
    @Insomnia_2311 3 роки тому

    HTB{ {H)igh (E)ducation (A)ttentional (R)ight (NOW) (T)raffic! } --->Thx!

  • @_AN203
    @_AN203 3 роки тому

    John You really need to sleep