Class: Rex::Proto::Http::Response
- Defined in:
- lib/rex/proto/http/response.rb
Overview
HTTP response class.
Direct Known Subclasses
Msf::Auxiliary::Web::HTTP::Response, Msf::Exploit::Git::Lfs::Response, Msf::Exploit::Git::SmartHttp::Response, E404, OK
Defined Under Namespace
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#count_100 ⇒ Object
Returns the value of attribute count_100.
-
#message ⇒ Object
Returns the value of attribute message.
-
#peerinfo ⇒ Object
Host address:port associated with this request/response.
-
#proto ⇒ Object
Returns the value of attribute proto.
-
#request ⇒ Object
Used to store a copy of the original request.
Attributes inherited from Packet
#auto_cl, #body, #body_bytes_left, #bufq, #chunk_max_size, #chunk_min_size, #compress, #error, #headers, #incomplete, #inside_chunk, #keepalive, #max_data, #state, #transfer_chunked
Instance Method Summary collapse
-
#check_100 ⇒ Object
Allow 100 Continues to be ignored by the caller.
-
#cmd_string ⇒ Object
Returns the response based command string.
-
#get_cookies ⇒ Object
Gets cookies from the Set-Cookie header in a format to be used in the ‘cookie’ send_request field.
-
#get_cookies_parsed ⇒ Object
Gets cookies from the Set-Cookie header in a parsed format.
-
#get_hidden_inputs ⇒ Array<Hash>
Returns a collection of found hidden inputs.
-
#get_html_document ⇒ Nokogiri::HTML::Document
Returns a parsed HTML document.
-
#get_html_meta_elements ⇒ Array<Nokogiri::XML::Element>
Returns meta tags.
-
#get_html_scripts ⇒ Array<RKelly::Nodes::SourceElementsNode>
Returns parsed JavaScript blocks.
-
#get_json_document ⇒ Hash
Returns a parsed json document.
-
#get_xml_document ⇒ Nokogiri::XML::Document
Returns a parsed XML document.
-
#initialize(code = 200, message = 'OK', proto = DefaultProtocol) ⇒ Response
constructor
Constructage of the HTTP response with the supplied code, message, and protocol.
-
#redirect? ⇒ Boolean
Answers if the response is a redirection one.
-
#redirection ⇒ URI?
Provides the uri of the redirection location.
-
#update_cmd_parts(str) ⇒ Object
Updates the various parts of the HTTP response command string.
Methods inherited from Packet
#[], #[]=, #chunk, #completed?, #from_s, #output_packet, #parse, #parse_body, #parse_header, #reset, #reset_except_queue, #to_s, #to_terminal_output
Constructor Details
#initialize(code = 200, message = 'OK', proto = DefaultProtocol) ⇒ Response
Constructage of the HTTP response with the supplied code, message, and protocol.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rex/proto/http/response.rb', line 47 def initialize(code = 200, = 'OK', proto = DefaultProtocol) super() self.code = code.to_i self. = self.proto = proto # Default responses to auto content length on self.auto_cl = true # default chunk sizes (if chunked is used) self.chunk_min_size = 1 self.chunk_max_size = 10 # 100 continue counter self.count_100 = 0 end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
244 245 246 |
# File 'lib/rex/proto/http/response.rb', line 244 def code @code end |
#count_100 ⇒ Object
Returns the value of attribute count_100.
247 248 249 |
# File 'lib/rex/proto/http/response.rb', line 247 def count_100 @count_100 end |
#message ⇒ Object
Returns the value of attribute message.
245 246 247 |
# File 'lib/rex/proto/http/response.rb', line 245 def @message end |
#peerinfo ⇒ Object
Host address:port associated with this request/response
242 243 244 |
# File 'lib/rex/proto/http/response.rb', line 242 def peerinfo @peerinfo end |
#proto ⇒ Object
Returns the value of attribute proto.
246 247 248 |
# File 'lib/rex/proto/http/response.rb', line 246 def proto @proto end |
#request ⇒ Object
Used to store a copy of the original request
237 238 239 |
# File 'lib/rex/proto/http/response.rb', line 237 def request @request end |
Instance Method Details
#check_100 ⇒ Object
Allow 100 Continues to be ignored by the caller
202 203 204 205 206 207 208 |
# File 'lib/rex/proto/http/response.rb', line 202 def check_100 # If this was a 100 continue with no data, reset if self.code == 100 and (self.body_bytes_left == -1 or self.body_bytes_left == 0) and self.count_100 < 5 self.reset_except_queue self.count_100 += 1 end end |
#cmd_string ⇒ Object
Returns the response based command string.
230 231 232 |
# File 'lib/rex/proto/http/response.rb', line 230 def cmd_string "HTTP\/#{proto} #{code}#{( and .length > 0) ? ' ' + : ''}\r\n" end |
#get_cookies ⇒ Object
Gets cookies from the Set-Cookie header in a format to be used in the ‘cookie’ send_request field
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rex/proto/http/response.rb', line 69 def = "" if (self.headers.include?('Set-Cookie')) = self.headers['Set-Cookie'] key_vals = .scan(/\s?([^, ;]+?)=([^, ;]*?)[;,]/) key_vals.each do |k, v| # Dont downcase actual cookie name as may be case sensitive name = k.downcase next if name == 'path' next if name == 'expires' next if name == 'domain' next if name == 'max-age' << "#{k}=#{v}; " end end return .strip end |
#get_cookies_parsed ⇒ Object
Gets cookies from the Set-Cookie header in a parsed format
91 92 93 94 95 96 97 98 |
# File 'lib/rex/proto/http/response.rb', line 91 def if (self.headers.include?('Set-Cookie')) ret = CGI::Cookie::parse(self.headers['Set-Cookie']) else ret = {} end ret end |
#get_hidden_inputs ⇒ Array<Hash>
Returns a collection of found hidden inputs
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rex/proto/http/response.rb', line 165 def get_hidden_inputs forms = [] noko = get_html_document noko.search("form").each_entry do |form| found_inputs = {} form.search("input").each_entry do |input| input_type = input.attributes['type'] ? input.attributes['type'].value : '' next if input_type !~ /hidden/i input_name = input.attributes['name'] ? input.attributes['name'].value : '' input_value = input.attributes['value'] ? input.attributes['value'].value : '' found_inputs[input_name] = input_value unless input_name.empty? end forms << found_inputs unless found_inputs.empty? end forms end |
#get_html_document ⇒ Nokogiri::HTML::Document
Returns a parsed HTML document. Instead of using regexes to parse the HTML body, you should use this and use the Nokogiri API.
106 107 108 |
# File 'lib/rex/proto/http/response.rb', line 106 def get_html_document Nokogiri::HTML(self.body) end |
#get_html_meta_elements ⇒ Array<Nokogiri::XML::Element>
Returns meta tags. You will probably want to use this the web app’s version info (or other stuff) can be found in the metadata.
140 141 142 143 |
# File 'lib/rex/proto/http/response.rb', line 140 def n = get_html_document n.search('//meta') end |
#get_html_scripts ⇒ Array<RKelly::Nodes::SourceElementsNode>
Returns parsed JavaScript blocks. The parsed version is a RKelly object that allows you to be able do advanced parsing.
150 151 152 153 154 |
# File 'lib/rex/proto/http/response.rb', line 150 def get_html_scripts n = get_html_document rkelly = RKelly::Parser.new n.search('//script').map { |s| rkelly.parse(s.text) } end |
#get_json_document ⇒ Hash
Returns a parsed json document. Instead of using regexes to parse the JSON body, you should use this.
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rex/proto/http/response.rb', line 123 def get_json_document json = {} begin json = JSON.parse(self.body) rescue JSON::ParserError => e elog(e) end json end |
#get_xml_document ⇒ Nokogiri::XML::Document
Returns a parsed XML document. Instead of using regexes to parse the XML body, you should use this and use the Nokogiri API.
115 116 117 |
# File 'lib/rex/proto/http/response.rb', line 115 def get_xml_document Nokogiri::XML(self.body) end |
#redirect? ⇒ Boolean
Answers if the response is a redirection one.
213 214 215 |
# File 'lib/rex/proto/http/response.rb', line 213 def redirect? [301, 302, 303, 307, 308].include?(code) end |
#redirection ⇒ URI?
Provides the uri of the redirection location.
221 222 223 224 225 |
# File 'lib/rex/proto/http/response.rb', line 221 def redirection URI(headers['Location']) rescue ArgumentError, ::URI::InvalidURIError nil end |
#update_cmd_parts(str) ⇒ Object
Updates the various parts of the HTTP response command string.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rex/proto/http/response.rb', line 187 def update_cmd_parts(str) if (md = str.match(/HTTP\/(.+?)\s+(\d+)\s?(.+?)\r?\n?$/)) self. = md[3].gsub(/\r/, '') self.code = md[2].to_i self.proto = md[1] else raise RuntimeError, "Invalid response command string", caller end check_100() end |