Skip to content

Instantly share code, notes, and snippets.

@gorenje
Created October 29, 2012 10:42
Show Gist options
  • Save gorenje/3972903 to your computer and use it in GitHub Desktop.
Save gorenje/3972903 to your computer and use it in GitHub Desktop.
get-password.rb
#!/usr/bin/ruby
#
# Author: Gerrit Riessen, [email protected]
# Copyright (C) 2009 Gerrit Riessen
# This code is licensed under the GNU Public License.
#
# $Id$
require 'rubygems'
require 'fileutils'
#require 'active_record'
#require 'sqlite3'
#require 'readline'
require 'ostruct'
#require 'ruby-prof'
require 'optparse'
require 'hpricot'
require 'addressable/uri'
require 'cgi'
# getopt like behaviour
@count = 15
@max_length = 32
@symbols = true
class Hash
def to_query(question_mark = '')
return '' if empty?
question_mark + (self.map do |k, v|
[v].flatten.map do |value|
(value.nil?) ? CGI.escape(k.to_s) : "%s=%s" % [CGI.escape(k.to_s),
CGI.escape(value.to_s)]
end
end.flatten.join("&"))
end
end
opts = OptionParser.new do |o|
o.program_name = 'ruby get-password.rb'
o.separator 'Options:'
o.on('--count FILENAME', '-c', 'Number of passwords') { |@count| }
o.on('--max-length FILENAME', '-l', 'Max length of password generated') { |@max_length| }
o.on('--no-symbols', '-s', 'No symbols please') { @symbols = false }
o.on_tail('--help', '-h', 'Show this message') do
puts opts
exit
end
end
opts.parse!(ARGV)
AGENT = ("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us)" +
" AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.28.3")
def do_curl( url, opts = { }, &block )
cookie_file = opts[:cookie_file] || opts[:cookie]
cookie_store = opts[:cookie_store]
header_store = opts[:header_store]
error_file = opts[:error_file]
ignore_errors = opts[:ignore_errors]
ignore_errors = false unless error_file.nil?
agent = opts[:agent] || AGENT
data = opts[:data]
[ cookie_store,
header_store,
error_file ].each { |file| FileUtils.rm_f(file) unless file.nil? }
cmdLine = "curl -A \"#{AGENT}\""
cmdLine << " -s" if ignore_errors or opts[:quiet]
cmdLine << " -k" if opts[:ssl_dont_verify]
cmdLine << " -G" if opts[:do_get]
cmdLine << " -d \"#{data}\"" unless data.nil?
cmdLine << " -c #{cookie_store}" unless cookie_store.nil?
cmdLine << " -b #{cookie_file}" unless cookie_file.nil?
cmdLine << " -D #{header_store}" unless header_store.nil?
cmdLine << " \"#{url}\""
cmdLine << " 2>/dev/null" if ignore_errors
cmdLine << " 2>#{error_file}" unless error_file.nil?
cmdLine << " | #{opts[:pipe_cmd]}" if opts[:pipe_cmd]
content = `#{cmdLine}`
yield(content, opts) if block_given?
content
end
def do_curl_2(url, opts = {}, &block) ; [do_curl(url,opts,&block), opts] ; end
BASE_URL="http://www.gaijin.at/olspwgen.php"
form = Hpricot(do_curl(BASE_URL, {:ignore_errors=>true})).search("form")
if form.size != 1
puts "Form not found #{form.size} != 2"
exit
end
form = form.last
if !(form["method"] =~ /post/i)
puts "form no longer post? => #{form['method']}"
exit
end
form_post_url = Addressable::URI.join(BASE_URL, form["action"])
options = { }
form.search("input").each do |input|
options[input['name']] = input['value']
end
options.merge!({ "ptype" => "OLD",
"poldlen" => @max_length.to_s,
"pcount" => @count.to_s })
options.merge!({"poldfspc" => 0, "poldspc" => 0,"poldlspc" => 0 }) unless @symbols
puts CGI.unescapeHTML(Hpricot(do_curl( form_post_url, { :data => options.to_query, :ignore_errors => true })).search("textarea").first.inner_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment