Ruby google request fails -


i'm creating google dork tool sends url encoded query google.com , returns results array of links.

#!/usr/bin/env ruby  require 'cgi' require 'socket'  # define full path library cwd = file.expand_path(file.dirname(__file__)) lib = file.join(cwd, "lib")  # require project library files dir.new(lib).each |x|   next unless x[/\.rb$/]   begin     require file.join(lib, x)   rescue     raise loaderror, "failed load #{x}."   end end  # build google dork def query(ext, site, inurl, intitle, intext)   query, values = "", []   dorks = %w(ext site inurl intitle intext)    values.push(ext, site, inurl, intitle, intext)    j = 0   values.each |i|     dork = dorks[j]      if dork.match(/^in/)       value = %q("#{i}")     else       value =     end      query += "#{dork}:#{value} " unless i.nil?     j += 1   end    query end  # sends search query google.com def search(host, query, agent)   sock, links = tcpsocket.new(host, 80), []   query = cgi::escape(query).chop   request = "get /search?q=#{query} http/1.0\r\n\r\n"# http/1.0\r\nuser-agent: #{agent}\r\nconnection: close\r\n\r\n"    sock.puts request   response = sock.read   body = response.split("\r\n\r\n", 2)[1]    body.split("url?q=").each |link|     link = link.to_s.split("&amp;", 0)[0]      links << link if link.match(/^http|^https/) , link !~ /^http:\/\/webcache/   end    links end  agent = randomagent.new host = "google.com"  q = query(argv[0], argv[1], argv[2], argv[3], argv[4]) puts search(host, q, agent.randomize) 

for reason have yet figure out, if send request manually, works. however, if send using ruby script returns 302 error. example:

get /search?q=ext%3apdf+site%3agithub.com+inurl%3a%22email%22 http/1.0

this request generated script. when using script http 302 error. if manually send same request using nc returns results.

nc google.com 80

get /search?q=ext%3apdf+site%3agithub.com+inurl%3a%22email%22 http/1.0

on top of that, if send this:

get /search?q=ext%3apdf+site%3agithub.com http/1.0

it works. third parameter causes have issues reason. can't seem figure out. thanks.

first off, 302 not error, redirecting you.

in case 302 result of google redirecting http://google.com https://google.com.

if want use sockets you'll need deal redirects , ssl handshake. maybe checkout net::http can handle of you?


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -