예제 #1
0
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