Advertisement
opexxx

query.rb

Jun 5th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.99 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'whois'
  4.  
  5. class StringMangle
  6.  
  7.     def initialize
  8.         @character_string= "a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q,r,s,t,u,v,w,x,y,z"
  9.         @character_list = @character_string.split(",")
  10.     end
  11.  
  12.     def get_random_string(length=5)
  13.         temp_string = ""
  14.         length.times do |t|
  15.             temp_string << @character_list[rand(@character_list.length)]   
  16.         end
  17.     temp_string
  18.     end
  19. end
  20.  
  21. class WordlistManager
  22.    
  23.     def initialize
  24.         @current_directory = File.dirname(__FILE__)
  25.         @wordlist_directory = "#{@current_directory}/wordlists"
  26.     end
  27.    
  28.     def open_wordlist(file)
  29.         File.open(file, :encoding => "ASCII").read.split("\n")
  30.     end
  31.    
  32.     def get_wordlists_by_pattern(pattern)
  33.         list = []
  34.         Dir.glob("#{@wordlist_directory}/*#{pattern}*").each do |file|
  35.             unless File.directory?(file)
  36.  
  37.                 puts "DEBUG: Adding #{file} to the list"
  38.                
  39.                 # Open each file in turn
  40.                 wordlist = File.open(file).read
  41.                
  42.                 # Handle invalid utf-8
  43.                 #ic = ::Iconv.new('UTF-8//IGNORE', 'UTF-8')
  44.                 #valid_wordlist = ic.iconv(wordlist + ' ')[0..-2]
  45.  
  46.                 # Add each word to an array
  47.                 wordlist.split("\n").each do |word|
  48. =begin
  49.                     begin
  50.                         word = word.encode('utf-8', 'ascii')
  51.                     rescue
  52.                         # Probably going to result in garbage
  53.                         word = word.force_encoding('ascii')
  54.                     end
  55. =end               
  56.                     list << word
  57.                 end
  58.             end
  59.         end
  60.     list
  61.     end
  62. end
  63.  
  64. class Logger
  65.  
  66.     def initialize()
  67.         @current_directory = File.dirname(__FILE__)
  68.         @log_directory = "#{@current_directory}/log"
  69.         @log = File.open("#{@log_directory}/finished_words.txt", "w+")
  70.         @print_good_to_screen = true
  71.         @print_bad_to_screen = true
  72.     end
  73.  
  74.     def log_good(string)
  75.         string = "[+] #{string}"
  76.         puts string if @print_good_to_screen
  77.         @log.write(string << "\n")
  78.     end
  79.        
  80.     def log_bad(string)
  81.         string = "[-] #{string}"
  82.         puts string if @print_bad_to_screen
  83.         @log.write(string << "\n")
  84.     end
  85.  
  86. end
  87.  
  88. pattern = ARGV[0]
  89.  
  90. puts "DEBUG: Testing wordlists with pattern #{pattern}"
  91.  
  92. whois_client = Whois::Client.new
  93. logger = Logger.new
  94. manager = WordlistManager.new
  95. wordlist = manager.get_wordlists_by_pattern(pattern)
  96.  
  97. puts "DEBUG: Testing #{wordlist.count} words."
  98.  
  99. wordlist.each do |word|
  100.     ["com"].each do |ending|
  101.        
  102.         # cleanup & construction
  103.         word.gsub!("\r","")
  104.         word.gsub!(" ","")
  105.         domain_string = "#{word}.#{ending}"
  106.        
  107.         begin
  108.             # do the actual query      
  109.             answer = whois_client.query(domain_string)
  110.            
  111.             # do the right thing
  112.             if answer.available?
  113.                 logger.log_good(domain_string)
  114.             else
  115.                 logger.log_bad(domain_string)
  116.             end
  117.            
  118.         rescue Timeout::Error => e
  119.             puts "Error: Timed out while querying the domain (#{domain_string})."
  120.  
  121.             ## Requeue
  122.             wordlist << word
  123.  
  124.         rescue Errno::ECONNREFUSED => e
  125.             puts "Error: Connection refused while querying the domain (#{domain_string})"
  126.  
  127.             ## Requeue
  128.             wordlist << word
  129.  
  130.         rescue Whois::ConnectionError => e
  131.             puts "Error: Connection refused while querying the domain (#{domain_string})"
  132.  
  133.             ## Requeue
  134.             wordlist << word
  135.  
  136.         end
  137.     end
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement