Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

5.1 KB
Raw Blame

The provided Ruby code is a simulation of a Blind Birthday Attack on a reduced 32-bit HMAC-SHA256. The goal of this attack is to find two different messages that produce the same HMAC (a collision) by exploiting the Birthday Paradox. Here’s a breakdown of the code:

Overview

  • HMAC: Hash-based Message Authentication Code, a cryptographic function that produces a fixed-size output.
  • Oracle: A function that, given two inputs, tells us how many bits of their HMACs match.
  • Tree: Used to organize the messages and facilitate the search for collisions.

Key Components

  1. Constants and HMAC Function:

    HMAC_BITS = 32
    
    def hmac(key, message)
      return OpenSSL::HMAC::digest('SHA256', key, message)[0, HMAC_BITS/8]
    end
    
    • HMAC_BITS is set to 32, meaning the HMAC output is truncated to 32 bits (4 bytes).
    • The hmac function generates a truncated 32-bit HMAC using SHA256 and the OpenSSL library.
  2. Oracle:

    KEY = SecureRandom.random_bytes(32)
    
    def oracle(a, b)
      h1 = hmac(KEY, a)
      h2 = hmac(KEY, b)
      h1_binary = h1.bytes.map { |c| c.to_s(2).rjust(8,'0') }.join('').split('')
      h2_binary = h2.bytes.map { |c| c.to_s(2).rjust(8,'0') }.join('').split('')
      0.upto(HMAC_BITS - 1) do |i|
        if h1_binary[i] != h2_binary[i]
          return i
        end
      end
      return HMAC_BITS
    end
    
    • KEY is a 256-bit random secret key.
    • The oracle function takes two messages, computes their HMACs, and returns the number of leading matching bits.
    • It converts the HMACs to binary strings and compares them bit by bit.
  3. TreeNode Class:

    class TreeNode
      attr_accessor :message, :left, :right
    end
    
    • TreeNode represents a node in the binary search tree.
    • Each node stores a message and has pointers to left and right children.
  4. Random Message Generator:

    def random
      return SecureRandom.random_bytes(32)
    end
    
    • random generates a 32-byte random message.
  5. Attack Implementation:

    def attack
      root = TreeNode.new
      root.message = random()
    
      queries = 0
      tree_size = 0
      closest = 0
    
      loop do
        newnode = TreeNode.new
        newnode.message = random()
        current = root
        matching = 0
    
        loop do
          thismatch = oracle(current.message, newnode.message)
          queries += 1
    
          if thismatch > closest
            closest = thismatch
            puts "Closest collision so far: #{thismatch}"
            puts "Tree size: #{tree_size}"
          end
    
          if thismatch == HMAC_BITS && current.message != newnode.message
            puts "Found a collision amongst #{tree_size} in #{queries} queries!"
            puts "Message 1: #{current.message.unpack("H*")[0]}"
            puts "Message 2: #{newnode.message.unpack("H*")[0]}"
            return
          end
    
          if thismatch > matching
            if current.right.nil?
              current.right = newnode
              tree_size += 1
              break
            end
            current = current.right
          else
            if current.left.nil?
              current.left = newnode
              tree_size += 1
              break
            end
            current = current.left
          end
    
          matching += 1
        end
      end
    end
    
    attack()
    

    Step-by-Step Explanation:

    1. Initialization:

      • A random message is generated and added as the root of the tree.
      • Counters (queries, tree_size, closest) are initialized to track the attack progress.
    2. Main Loop:

      • The attack runs in an infinite loop, continually adding new messages to the tree.
      • For each new message, a new TreeNode is created.
    3. Tree Traversal:

      • Starting from the root, the new message is compared with the current node's message using the oracle.
      • The number of matching bits (thismatch) is determined.
      • If thismatch is greater than any previous match (closest), it's updated and printed.
    4. Collision Detection:

      • If all HMAC_BITS (32 bits) match and the messages are different, a collision is found, and the program stops.
      • The matching bit length determines the direction in the tree:
        • If the (matching+1)st bit matches, move right.
        • Otherwise, move left.
      • If the corresponding child node is nil, the new node is added at that position.
    5. Termination:

      • The attack continues until a collision is found, printing the details of the collision when it occurs.

Key Concepts:

  • Oracle Usage: The oracle helps in efficiently navigating the tree by providing the number of matching HMAC bits, reducing the expected queries.
  • Tree Structure: The binary tree is used to systematically organize messages, ensuring that all possible HMAC prefixes are explored.
  • Collision Search: The goal is to exploit the Birthday Paradox, expecting to find a collision after around (2^{16}) messages (since HMAC_BITS is 32).

This code is a proof of concept and works due to the reduced HMAC size. In real-world scenarios, the HMAC would be much larger (e.g., 256 bits), making such attacks impractical.