Created
August 5, 2013 22:47
-
-
Save x684867/6160318 to your computer and use it in GitHub Desktop.
Version three of the hash table builder...renamed keyspaceGen.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # | |
| # Universal Hash Table calculator (keyspaceGen.rb) (version 1.3) | |
| # (c) 2013 Sam Caldwell. All Rights Reserved. | |
| # | |
| # ------------------------------------------------------------------------------ | |
| # *** LIMITED EDUCATION-USE LICENSE *** | |
| # Script intended and authorized solely for research and education purposes | |
| # to identify the weaknesses of current cryptographic practices in place on | |
| # many systems so that the system operators can appreciate the use of best | |
| # practices. Use of this script for illegal or harmful purposes is prohibited | |
| # and may result in criminal or civil penalties. The user is forewarned that | |
| # the author *will* cooperate with law enforcement in his jurisdiction in the | |
| # non-political prosecution of persons believed to have used this script or | |
| # derived work to harm any person or organization or to otherwise violate the | |
| # laws of the United States or any state thereof. | |
| # | |
| # Use of this script indicates an acknowledgement of and consent to these terms | |
| # and conditions and an agreement to indemnify and hold harmless the author in | |
| # any and all cases which may arise from your use hereof directly or indirectly. | |
| # ------------------------------------------------------------------------------ | |
| # | |
| # Changes: | |
| # Version 1.0: Proof of Concept. | |
| # Version 1.1: Multi-threaded hash operation | |
| # Version 1.2: Changed to flat-file output. | |
| # Version 1.3: Added re-entrant capabilities (seed array). | |
| # | |
| require 'digest/md5' | |
| require 'digest/sha1' | |
| require 'digest/sha2' | |
| require 'fileutils' | |
| require 'logger' | |
| GC.enable | |
| GC.start | |
| def throw_missing_param() | |
| puts "Missing expected operand. Use -h or --help for USAGE information." | |
| abort | |
| end | |
| def throw_bad_directory() | |
| puts "Bad or missing directory specified. Use -h or --help for USAGE information." | |
| abort | |
| end | |
| def throw_bad_keyspace() | |
| puts "stop_char must be greater than start_char" | |
| puts "This value must also be a valid decimal ASCII code." | |
| puts "Use -h or --help for more information." | |
| abort | |
| end | |
| def throw_bad_width() | |
| puts "width must be greater than zero (0)" | |
| puts "Use -h or --help for more information." | |
| abort | |
| end | |
| def show_usage() | |
| puts "--------------------------------------------------------------------------" | |
| puts " Keyspace Generator" | |
| puts " (c) 2013 Sam Caldwell. All Rights Reserved." | |
| puts "--------------------------------------------------------------------------" | |
| puts "USAGE:" | |
| puts " keyspaceGen.rb base_dir start_char stop_char width " | |
| puts " " | |
| puts " base_dir (string) : directory to which data will be stored" | |
| puts " start_char (int) : starting ASCII character value" | |
| puts " stop_char (int) : ending ASCII character value" | |
| puts " key_width (int) : width of the keyspace to be analyzed" | |
| puts "--------------------------------------------------------------------------" | |
| puts "EXAMPLE:" | |
| puts " tableBuilder.rb 32 127 16" | |
| puts "--------------------------------------------------------------------------" | |
| exit | |
| end | |
| def show_welcome(start_char,stop_char,width,seed,log) | |
| log.info "-- -- -- -- -- -- -- -- -- -- -- -- -- --" | |
| log.info "Starting..." | |
| log.info " start_char: " + start_char.to_s + " (ASCII)" | |
| log.info " stop_char: " + stop_char.to_s + " (ASCII)" | |
| log.info " width: " + width.to_s + " chars" | |
| log.info " seed: [" + seed.to_s + "]" | |
| log.info "-- -- -- -- -- -- -- -- -- -- -- -- -- --" | |
| end | |
| def check_keyspace_end(keyspace,width,stop_char) | |
| end | |
| def increment_keyspace(keyspace,index,start_char,stop_char,width) | |
| if (index == (width-1)) | |
| check_flag=false | |
| keyspace.each do |k| | |
| check_flag=true if(k==stop_char) | |
| end | |
| keyspace[index]=keyspace[index]+1 if(check_flag==false) | |
| return check_flag | |
| else | |
| if(keyspace[index]>=stop_char) | |
| keyspace[index]=0 | |
| return increment_keyspace(keyspace,index+1,start_char,stop_char,width) | |
| else | |
| if((keyspace[index]==0) and (start_char>0)) | |
| keyspace[index]=start_char | |
| else | |
| keyspace[index]=keyspace[index]+1 | |
| end | |
| return false | |
| end | |
| end | |
| end | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| # MAIN ROUTINE | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| # PARSE ARGUMENTS... | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| show_usage() if((ARGV[0]=="-h") || (ARGV[0]=="--help")) | |
| (base_dir=ARGV[0].to_s) or throw_missing_param() | |
| (start_char=ARGV[1].to_i) or throw_missing_param() | |
| (stop_char=ARGV[2].to_i) or throw_missing_param() | |
| (width=ARGV[3].to_i) or throw_missing_param() | |
| seed=Array.new(width) | |
| seed[0] =ARGV[4].to_i | |
| seed[1] =ARGV[5].to_i | |
| seed[2] =ARGV[6].to_i | |
| seed[3] =ARGV[7].to_i | |
| seed[4] =ARGV[8].to_i | |
| seed[5] =ARGV[9].to_i | |
| seed[6] =ARGV[10].to_i | |
| seed[7] =ARGV[11].to_i | |
| seed[8] =ARGV[12].to_i | |
| seed[9] =ARGV[13].to_i | |
| seed[10]=ARGV[14].to_i | |
| seed[11]=ARGV[15].to_i | |
| seed[12]=ARGV[16].to_i | |
| seed[13]=ARGV[17].to_i | |
| seed[14]=ARGV[18].to_i | |
| seed[15]=ARGV[19].to_i | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| # VALIDATE INPUTS... | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| throw_bad_directory() if (not(File.directory?(base_dir))) | |
| throw_bad_keyspace() if(stop_char <= start_char) | |
| throw_bad_width() if(width<=0) | |
| width.times do | i | | |
| seed[i]=0 if((seed[i]<0)or(seed[i]>255)) | |
| end | |
| sliceId=Digest::SHA1.hexdigest(seed.to_s) | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| # CALCULATE HASHES | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| log=Logger.new 'keyspaceGen-'+sliceId | |
| log.info 'keyspaceGen log initialized.' | |
| show_welcome(start_char,stop_char,width,seed,log) | |
| log.info "initialize keyspace" | |
| keyspace=Array.new(width) | |
| width.times do |i| | |
| keyspace[i]=seed[i] | |
| end | |
| log.info "Setup for the hash calculations." | |
| lastkeylen=0 | |
| is_complete=false | |
| hash_file=File.new(base_dir.chomp("/")+"/"+"MD5."+sliceId+".txt","a") | |
| log.info "start calculating hashes...." | |
| hash_count=0 | |
| until is_complete do | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #Perform a "pretty-join" of the keyspace for display/storage. | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| keystring="" | |
| p=1 | |
| keyspace.each do | k | | |
| keystring=keystring+k.to_s(16)+":" unless ((p>=lastkeylen) and ( k==0 )) | |
| p=p+1 | |
| end | |
| keystring=keystring.chomp(":") | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #Perform a join of keyspace as ASCII characters for hashing. | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| ascii_string="" | |
| keyspace.each do | k | | |
| ascii_string=ascii_string+k.chr | |
| end | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #Calculate a hash string (MD5) | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| hashstring=Digest::MD5.hexdigest(ascii_string) | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #Write results to screen and file | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| log.info "sliceId: " + sliceId + " "+hashstring.to_s+" "+keystring + " ASCII: "+ascii_string | |
| puts ">"+Time.now.to_s+" | "+hashstring.to_s+"-"+keystring+" | count:"+hash_count.to_s | |
| hash_file.puts hashstring.to_s+"-"+keystring | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #Increment keyspace | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| is_complete=increment_keyspace(keyspace,0,start_char,stop_char,width) | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| #prepare for next run | |
| #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- | |
| lastkeylen=keystring.length | |
| hash_count=hash_count+1 | |
| end | |
| hash_file.close | |
| log.info "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment