Skip to content

Instantly share code, notes, and snippets.

@fbettag
Created May 24, 2011 00:46
Show Gist options
  • Save fbettag/987941 to your computer and use it in GitHub Desktop.
Save fbettag/987941 to your computer and use it in GitHub Desktop.
Ruby Google Dork
#!/usr/bin/ruby
# encoding: utf-8
# Copyright (c) 2011, Franz Bettag <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by Franz Bettag.
#
# THIS SOFTWARE IS PROVIDED BY FRANZ BETTAG ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL FRANZ BETTAG BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'optparse'
require 'net/http'
require 'uri'
require 'pp'
@options = {
:limit => 100,
:pages => 1,
:wait => 2,
:fh => STDOUT,
:show_title => false,
:show_status => false,
}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on("-d", "--dork [mandatory]", "Dork Search String", :REQUIRED) {|d| @options[:dork] = URI.escape(d) }
opts.on("-p", "--pages [optional]", "Pages to scan (default 1)", :REQUIRED) {|d| @options[:pages] = d.to_i }
opts.on("-w", "--wait [optional]", "Wait seconds after each request (default 2)", :REQUIRED) {|d| @options[:wait] = d.to_i }
opts.on("-o", "--outfile [optional]", "Output file (txt)", :REQUIRED) {|o| @options[:fh] = File.open(o, 'w+') }
opts.on("-t", "--showtitle [optional]", "Shows the Title of the found Document") {|o| @options[:show_title] = true }
@options[:theopts] = opts
end.parse!
unless @options.has_key? :dork
puts @options[:theopts]
puts ''
puts 'Dorks:'
puts ' -trac -trunk -"127.0.0.1" -localhost -"Google Code" -source -repository'
puts ' -intext:"nothing found" -intext:404'
puts ''
exit 1
end
def scan(page)
opts = []
opts << "q=#{@options[:dork]}"
opts << "num=#{@options[:limit]}"
opts << "start=#{page * @options[:limit]}" if page * @options[:limit] > 0
req = Net::HTTP.new('www.google.com').get("/search?" + opts.join('&'))
matches = req.body.scan(/h3 class="?[^"]+"?><a href="(http[s]?:\/\/[^"]+?)"/)
if matches.length == 0
puts "Nothing found"
else
matches.each do |m|
unless @options[:show_title] or @options[:show_status]
@options[:fh].puts m
else
begin
suri = URI.parse m[0]
sreq = Net::HTTP.new(suri.host).get(suri.path)
smatches = sreq.body.scan(/(?:<title>)([^<]+)<\/title>/)
title = smatches[0][0].gsub(/[\r\n]/, '')
@options[:fh].puts "#{m[0]} - \"#{title}\""
rescue
end
end
end
end
end
@options[:pages].times do |i|
puts "## Scanning page #{i}" if @options[:fh] != STDOUT
scan(i)
puts "## Sleeping for #{@options[:wait]} seconds..." if @options[:fh] != STDOUT
sleep @options[:wait] unless i == @options[:pages]-1
end
@options[:fh].close if @options[:fh] != STDOUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment