def request_with_data(post_request, content_type, req_type, retries, time_sleep, timeout_sec, payload, condition, output, sample_event, message, log_in_file, thread_tmp_filename, language, targets, ports, default_ports, socks_proxy): """ this function extracts the data, headers and url for the POST type request which is to be sent to the __http_request_maker function Args: post_request: the returned data from __http_requests_generator function req_type: GET, POST, PUT, DELETE or PATCH content_type: application/json or application/x-www-form-urlencoded payload: the payload corresponding to which the request is made condition: the condition to be evaluated. eg: response.status_code == 200 other args: retries, time_sleep, timeout_sec, output, sample_event, message, log_in_file, thread_tmp_filename, language Returns: the list of outputs in the format [ { "payload": payload, "condition": condition, "result": rule_evaluator(response, condition), "response": response },...... ] """ post_data_format = "" request_line, headers_alone = post_request.split('\r\n', 1) headers = Message(StringIO(headers_alone)).dict clean_headers = {x.strip(): y for x, y in headers.items()} headers = clean_headers if "content-type" in headers: content_type = headers['content-type'] if content_type == 'application/x-www-form-urlencoded': post_data_format = post_data_parser(post_request.split('\r\n')[-1]) elif content_type == 'application/json': post_data_format = json.loads(post_request[post_request.find('{'):post_request.find('}') + 1]) headers.pop("Content-Length", None) url_sample = request_line.strip().split(' ')[1] for target in targets: url = url_sample.replace('__target_locat_here__', str(target)) port = url[url.find(':', 7) + 1:url.find('/', 7)] response = __http_request_maker(req_type, url, headers, retries, time_sleep, timeout_sec, post_data_format, content_type, socks_proxy) if rule_evaluator(response, condition): __log_into_file(thread_tmp_filename, 'w', '0', language) sample_event['PORT'] = port event_parser(message, sample_event, response, payload, log_in_file, language) output.append({ "payload": payload, "condition": condition, "result": rule_evaluator(response, condition), "response": response }) return output
def parse_headers(header_string): """ Parse a header-string into individual headers Implementation based on: http://stackoverflow.com/a/5955949/95122 """ # First line is request line, strip it out if not header_string: return dict() request, headers = header_string.split('\r\n', 1) if not headers: return dict() else: header_msg = Message(StringIO(headers)) return dict(header_msg.items())