2020-07-20 15:12:09 -05:00
|
|
|
require 'socket'
|
|
|
|
require './http.rb'
|
|
|
|
require 'time'
|
|
|
|
|
|
|
|
class FakeTunnelConnector
|
|
|
|
attr_accessor :port, :server
|
|
|
|
def initialize(port)
|
|
|
|
@port = port
|
|
|
|
@server = TCPServer.open(port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def listen_and_respond
|
|
|
|
tcp_socket = server.accept
|
2020-07-20 16:31:10 -05:00
|
|
|
response = read_socket(tcp_socket)
|
|
|
|
location = response[:start_line][:location]
|
|
|
|
write_response(tcp_socket, location)
|
2020-07-20 15:12:09 -05:00
|
|
|
tcp_socket.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_socket(tcp_socket)
|
2020-07-20 16:31:10 -05:00
|
|
|
start_line = []
|
|
|
|
headers = []
|
2020-07-20 15:12:09 -05:00
|
|
|
puts "Request Incoming:"
|
|
|
|
puts "-------------------"
|
|
|
|
# read lines from socket
|
|
|
|
while (line = tcp_socket.gets) && (line.chomp.length > 0)
|
|
|
|
# check for a valid http verb sent
|
2020-07-20 16:31:10 -05:00
|
|
|
start_line = parse_http_start_request_line(line) if Http::METHODS.include?(line.split.first)
|
2020-07-20 15:12:09 -05:00
|
|
|
header_line = parse_http_header_request_line(line)
|
|
|
|
headers << header_line unless header_line.nil?
|
|
|
|
end
|
2020-07-20 16:31:10 -05:00
|
|
|
puts start_line
|
2020-07-20 15:12:09 -05:00
|
|
|
puts "Request Headers:"
|
|
|
|
puts "-------------------"
|
|
|
|
puts headers
|
|
|
|
puts "\r\n"
|
2020-07-20 16:31:10 -05:00
|
|
|
{:start_line => start_line, :headers => headers}
|
2020-07-20 15:12:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_http_start_request_line(line)
|
|
|
|
split_request = line.split
|
|
|
|
verb = split_request.first
|
|
|
|
location = split_request.select { |l| l if l.start_with?('/') }.first
|
|
|
|
protocol = split_request.last
|
|
|
|
{
|
|
|
|
verb: verb,
|
|
|
|
location: location,
|
|
|
|
protocol: protocol
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_http_header_request_line(line)
|
|
|
|
if line.split.first.end_with?(':')
|
|
|
|
split_request = line.split(':')
|
|
|
|
key = split_request[0]
|
|
|
|
value = if split_request.count >= 3
|
|
|
|
split_request[1] + ':' + split_request[2]
|
|
|
|
else
|
|
|
|
split_request[1]
|
|
|
|
end
|
|
|
|
Hash[key, value]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-20 16:31:10 -05:00
|
|
|
def write_response(tcp_socket, location)
|
|
|
|
route_response_string = route_request(location)
|
|
|
|
tcp_socket.sendmsg(ok(route_response_string))
|
2020-07-20 15:12:09 -05:00
|
|
|
end
|
|
|
|
|
2020-07-20 16:31:10 -05:00
|
|
|
def ok(body='Success')
|
|
|
|
"HTTP/1.1 200 OK\r\nDate: #{Time.now.utc}\r\n\r\n#{body}\r\n"
|
2020-07-20 15:12:09 -05:00
|
|
|
end
|
2020-07-20 16:31:10 -05:00
|
|
|
|
|
|
|
def route_request(location)
|
|
|
|
if location == '/scans'
|
|
|
|
scans
|
|
|
|
elsif location == '/login'
|
|
|
|
login
|
|
|
|
else
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def scans
|
|
|
|
body = '{"folders":[{"unread_count":0,"custom":0,"default_tag":0,"type":"trash","name":"Trash","id":7},{"unread_count":0,"custom":0,"default_tag":1,"type":"main","name":"My
|
|
|
|
Scans","id":8}],"scans":[{"legacy":false,"permissions":128,"type":"remote","read":true,"last_modification_date":1535393942,"creation_date":1535393789,"status":"completed","uuid":"71e19d73-6308-42b0-adee-b26c9ddabfda","shared":false,"user_permissions":128,"owner":"edbellis","schedule_uuid":"template-7f64b006-dfaf-feee-85bd-ea9dd47280cf3a9573b1fc8089e7","timezone":null,"rrules":null,"starttime":null,"enabled":false,"control":true,"name":"Kenna
|
|
|
|
Security: Nessus API (ON PREM YO)","id":29}],"timestamp":1535395026}'
|
|
|
|
ok(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
|
|
|
body = '{"token":"797118d801342a0c5c5be3ed5420782becbea2e3bceea9275543dff4ee62dfc4"}'
|
|
|
|
ok(body)
|
|
|
|
end
|
|
|
|
|
2020-07-20 15:12:09 -05:00
|
|
|
end
|