Skip to content

Instantly share code, notes, and snippets.

@le4ker
Last active September 22, 2024 20:54
Show Gist options
  • Select an option

  • Save le4ker/2eceadbd3f64bf62d252f720bbb226d3 to your computer and use it in GitHub Desktop.

Select an option

Save le4ker/2eceadbd3f64bf62d252f720bbb226d3 to your computer and use it in GitHub Desktop.

Revisions

  1. le4ker revised this gist Oct 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cbc-bit-flipping.rb
    Original file line number Diff line number Diff line change
    @@ -44,4 +44,4 @@ def decrypt(ciphertext)
    new_plaintext = unauthenticatedEncryption.decrypt(intercepted_ciphertext)

    puts new_plaintext
    # admin=0
    # admin=1
  2. le4ker revised this gist Oct 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cbc-bit-flipping.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Demonstration of CBC Bit Flipping Attack #
    # Author: Panos Sakkos <[email protected]> #
    # Date: May 2017 #
    # Date: October 2017 #
    # License: MIT #
    # # # # # # # # # # # # # # # # # # # # # # # # # #

  3. le4ker created this gist Oct 22, 2017.
    47 changes: 47 additions & 0 deletions cbc-bit-flipping.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Demonstration of CBC Bit Flipping Attack #
    # Author: Panos Sakkos <[email protected]> #
    # Date: May 2017 #
    # License: MIT #
    # # # # # # # # # # # # # # # # # # # # # # # # # #

    require 'openssl'

    class UnauthenticatedEncryption

    def encrypt(plaintext)

    cipher = OpenSSL::Cipher::AES.new(256, :CBC)
    cipher.encrypt
    @key = cipher.random_key
    iv = cipher.random_iv
    ciphertext = cipher.update(plaintext) + cipher.final

    return iv + ciphertext

    end

    def decrypt(ciphertext)

    decipher = OpenSSL::Cipher::AES.new(256, :CBC)
    decipher.decrypt
    decipher.key = @key
    decipher.iv = ciphertext[0..15]
    plaintext = decipher.update(ciphertext[16..(ciphertext.length - 1)]) + decipher.final

    return plaintext

    end

    end

    plaintext = 'admin=0'

    unauthenticatedEncryption = UnauthenticatedEncryption.new()
    intercepted_ciphertext = unauthenticatedEncryption.encrypt(plaintext)
    intercepted_ciphertext[6] = (intercepted_ciphertext.bytes[6] ^ 0x01).chr

    new_plaintext = unauthenticatedEncryption.decrypt(intercepted_ciphertext)

    puts new_plaintext
    # admin=0