Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/http/response.rb
Views: 11704
# -*- coding: binary -*-1require 'cgi'2require 'uri'34require 'nokogiri'5require 'rkelly'67module Rex8module Proto9module Http1011###12#13# HTTP response class.14#15###16class Response < Packet1718##19#20# Builtin response class wrappers.21#22##2324#25# HTTP 200/OK response class wrapper.26#27class OK < Response28def initialize(message = 'OK', proto = DefaultProtocol)29super(200, message, proto)30end31end3233#34# HTTP 404/File not found response class wrapper.35#36class E404 < Response37def initialize(message = 'File not found', proto = DefaultProtocol)38super(404, message, proto)39end40end4142#43# Constructage of the HTTP response with the supplied code, message, and44# protocol.45#46def initialize(code = 200, message = 'OK', proto = DefaultProtocol)47super()4849self.code = code.to_i50self.message = message51self.proto = proto5253# Default responses to auto content length on54self.auto_cl = true5556# default chunk sizes (if chunked is used)57self.chunk_min_size = 158self.chunk_max_size = 105960# 100 continue counter61self.count_100 = 062end6364#65# Gets cookies from the Set-Cookie header in a format to be used66# in the 'cookie' send_request field67#68def get_cookies69cookies = ""70if (self.headers.include?('Set-Cookie'))71set_cookies = self.headers['Set-Cookie']72key_vals = set_cookies.scan(/\s?([^, ;]+?)=([^, ;]*?)[;,]/)73key_vals.each do |k, v|74# Dont downcase actual cookie name as may be case sensitive75name = k.downcase76next if name == 'path'77next if name == 'expires'78next if name == 'domain'79next if name == 'max-age'80cookies << "#{k}=#{v}; "81end82end8384return cookies.strip85end8687#88# Gets cookies from the Set-Cookie header in a parsed format89#90def get_cookies_parsed91if (self.headers.include?('Set-Cookie'))92ret = CGI::Cookie::parse(self.headers['Set-Cookie'])93else94ret = {}95end96ret97end9899100# Returns a parsed HTML document.101# Instead of using regexes to parse the HTML body, you should use this and use the Nokogiri API.102#103# @see http://www.nokogiri.org/104# @return [Nokogiri::HTML::Document]105def get_html_document106Nokogiri::HTML(self.body)107end108109# Returns a parsed XML document.110# Instead of using regexes to parse the XML body, you should use this and use the Nokogiri API.111#112# @see http://www.nokogiri.org/113# @return [Nokogiri::XML::Document]114def get_xml_document115Nokogiri::XML(self.body)116end117118# Returns a parsed json document.119# Instead of using regexes to parse the JSON body, you should use this.120#121# @return [Hash]122def get_json_document123json = {}124125begin126json = JSON.parse(self.body)127rescue JSON::ParserError => e128elog(e)129end130131json132end133134# Returns meta tags.135# You will probably want to use this the web app's version info (or other stuff) can be found136# in the metadata.137#138# @return [Array<Nokogiri::XML::Element>]139def get_html_meta_elements140n = get_html_document141n.search('//meta')142end143144# Returns parsed JavaScript blocks.145# The parsed version is a RKelly object that allows you to be able do advanced parsing.146#147# @see https://github.com/tenderlove/rkelly148# @return [Array<RKelly::Nodes::SourceElementsNode>]149def get_html_scripts150n = get_html_document151rkelly = RKelly::Parser.new152n.search('//script').map { |s| rkelly.parse(s.text) }153end154155156# Returns a collection of found hidden inputs157#158# @return [Array<Hash>] An array, each element represents a form that contains a hash of found hidden inputs159# * 'name' [String] The hidden input's original name. The value is the hidden input's original value.160# @example161# res = send_request_cgi('uri'=>'/')162# inputs = res.get_hidden_inputs163# session_id = inputs[0]['sessionid'] # The first form's 'sessionid' hidden input164def get_hidden_inputs165forms = []166noko = get_html_document167noko.search("form").each_entry do |form|168found_inputs = {}169form.search("input").each_entry do |input|170input_type = input.attributes['type'] ? input.attributes['type'].value : ''171next if input_type !~ /hidden/i172173input_name = input.attributes['name'] ? input.attributes['name'].value : ''174input_value = input.attributes['value'] ? input.attributes['value'].value : ''175found_inputs[input_name] = input_value unless input_name.empty?176end177forms << found_inputs unless found_inputs.empty?178end179180forms181end182183#184# Updates the various parts of the HTTP response command string.185#186def update_cmd_parts(str)187if (md = str.match(/HTTP\/(.+?)\s+(\d+)\s?(.+?)\r?\n?$/))188self.message = md[3].gsub(/\r/, '')189self.code = md[2].to_i190self.proto = md[1]191else192raise RuntimeError, "Invalid response command string", caller193end194195check_100()196end197198#199# Allow 100 Continues to be ignored by the caller200#201def check_100202# If this was a 100 continue with no data, reset203if self.code == 100 and (self.body_bytes_left == -1 or self.body_bytes_left == 0) and self.count_100 < 5204self.reset_except_queue205self.count_100 += 1206end207end208209# Answers if the response is a redirection one.210#211# @return [Boolean] true if the response is a redirection, false otherwise.212def redirect?213[301, 302, 303, 307, 308].include?(code)214end215216# Provides the uri of the redirection location.217#218# @return [URI] the uri of the redirection location.219# @return [nil] if the response hasn't a Location header or it isn't a valid uri.220def redirection221URI(headers['Location'])222rescue ArgumentError, ::URI::InvalidURIError223nil224end225226#227# Returns the response based command string.228#229def cmd_string230"HTTP\/#{proto} #{code}#{(message and message.length > 0) ? ' ' + message : ''}\r\n"231end232233#234# Used to store a copy of the original request235#236attr_accessor :request237238#239# Host address:port associated with this request/response240#241attr_accessor :peerinfo242243attr_accessor :code244attr_accessor :message245attr_accessor :proto246attr_accessor :count_100247end248249end250end251end252253254