Ruby as Web Server and Proxy
1. Web Server
You can run Ruby as web server for any folder/file on any unused port
ruby -run -e httpd /var/www/ -p 8000
or
require 'webrick'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => '/var/www/'
# WEBrick::Daemon.start # Stating WEBRick as a daemon
server.start
To make HTTPS server
require 'webrick'
require 'webrick/https'
cert = [
%w[CN localhost],
]
server = WEBrick::HTTPServer.new(:Port => 8000,
:SSLEnable => true,
:SSLCertName => cert,
:DocumentRoot => '/var/www/')
server.start
2. Web Proxy
2.1. Transparent Web Proxy
require 'webrick'
require 'webrick/httpproxy'
handler = proc do |req, res|
puts "[*] Request"
puts req.inspect
request = req.request_line.split
puts "METHOD: " + "#{request[0]}"
puts "Request URL: " + "#{request[1]}"
puts "Request path: "+ "#{req.path}"
puts "HTTP: " + "#{request[2]}"
puts "Referer: " + "#{req['Referer']}"
puts "User-Agent: " + "#{req['User-Agent']}"
puts "Host: " + "#{req['Host']}"
puts "Cookie: " + "#{req['Cookie']}"
puts "Connection: " + "#{req['Connection']}"
puts "Accept: " + "#{req['accept']}"
puts "Full header: " + "#{req.header}"
puts "Body: " + "#{req.body}"
puts "----------[END OF REQUEST]----------"
puts "\n\n"
puts "[*] Response"
puts res.inspect
puts "Full header: " + "#{res.header}"
puts "Body: " + "#{res.body}"
puts "----------[END OF RESPONSE]----------"
puts "\n\n\n"
end
proxy = WEBrick::HTTPProxyServer.new Port: 8000,
ServerName: "RubyFuProxyServer",
ServerSoftware: "RubyFu Proxy",
ProxyContentHandler: handler
trap 'INT' do proxy.shutdown end
proxy.start
2.2. Transparent Web Proxy with Authentication
Well, it was great to know that building a proxy server is that easy. Now we need to Force authentication to connect to the proxy server
To enable authentication for requests in WEBrick you will need a user database and an authenticator. To start, here's a htpasswd database for use with a DigestAuth authenticator:
The :Realm
is used to provide different access to different groups across several resources on a server. Typically you'll need only one realm for a server.
#!/usr/bin/env ruby
require 'webrick'
require 'webrick/httpproxy'
# Start creating the config
config = { :Realm => 'RubyFuSecureProxy' }
# Create an htpasswd database file in the same script path
htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'rubyfuhtpasswd'
# Set authentication type
htpasswd.auth_type = WEBrick::HTTPAuth::DigestAuth
# Add user to the password config
htpasswd.set_passwd config[:Realm], 'rubyfu', 'P@ssw0rd'
# Flush the database (Save changes)
htpasswd.flush
# Add the database to the config
config[:UserDB] = htpasswd
# Create a global DigestAuth based on the config
@digest_auth = WEBrick::HTTPAuth::DigestAuth.new config
# Authenticate requests and responses
handler = proc do |request, response|
@digest_auth.authenticate request, response
end
proxy = WEBrick::HTTPProxyServer.new Port: 8000,
ServerName: "RubyFuSecureProxy",
ServerSoftware: "RubyFu Proxy",
ProxyContentHandler: handler
trap 'INT' do proxy.shutdown end
proxy.start
If you do it right, you'll get an authentication pop-up in your browser just like below.