Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fixes, New feature and Functionality and modules #575

Merged
merged 16 commits into from
Sep 17, 2022
Merged
64 changes: 45 additions & 19 deletions core/module_protocols/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from core.utility import process_conditions
from core.utility import get_dependent_results_from_database
from core.utility import replace_dependent_values
from core.utility import replace_dependent_response
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


def response_conditions_matched(sub_step, response):
if not response:
return []
return {}
condition_type = sub_step['response']['condition_type']
conditions = sub_step['response']['conditions']
condition_results = {}
Expand All @@ -32,11 +33,14 @@ def response_conditions_matched(sub_step, response):
condition_results['headers'] = {}
for header in conditions['headers']:
reverse = conditions['headers'][header]['reverse']
regex = re.findall(
re.compile(conditions['headers'][header]['regex']),
response['headers'][header.lower()] if header.lower() in response['headers'] else ""
)
condition_results['headers'][header] = reverse_and_regex_condition(regex, reverse)
try:
regex = re.findall(
re.compile(conditions['headers'][header]['regex']),
response['headers'][header.lower()] if header.lower() in response['headers'] else False
)
condition_results['headers'][header] = reverse_and_regex_condition(regex, reverse)
except TypeError:
condition_results['headers'][header] = []
if condition == 'responsetime':
if len(conditions[condition].split()) == 2 and conditions[condition].split()[0] in [
"==",
Expand Down Expand Up @@ -67,26 +71,32 @@ def response_conditions_matched(sub_step, response):
) or (
'headers' in condition_results and
(
(
list(condition_results.values()).count([]) - 1 !=
len(list(condition_results.values()))
) and
(
list(condition_results['headers'].values()).count([]) !=
len(list(condition_results['headers'].values()))
)

len(list(condition_results.values())) +
len(list(condition_results['headers'].values())) -
list(condition_results.values()).count([]) -
list(condition_results['headers'].values()).count([]) -
1 != 0
)
):
):
if sub_step['response'].get('log',False):
condition_results['log']=sub_step['response']['log']
if 'response_dependent' in condition_results['log']:
condition_results['log'] = replace_dependent_response(condition_results['log'],condition_results)
return condition_results
else:
return []
return {}
if condition_type.lower() == "and":
if [] in condition_results.values() or \
('headers' in condition_results and [] in condition_results['headers'].values()):
return []
return {}
else:
if sub_step['response'].get('log',False):
condition_results['log']=sub_step['response']['log']
if 'response_dependent' in condition_results['log']:
condition_results['log'] = replace_dependent_response(condition_results['log'],condition_results)
return condition_results
return []
return {}


class Engine:
Expand All @@ -104,11 +114,11 @@ def run(
):
backup_method = copy.deepcopy(sub_step['method'])
backup_response = copy.deepcopy(sub_step['response'])
backup_iterative_response_match = copy.deepcopy(sub_step['response']['conditions'].get('iterative_response_match',None))
action = getattr(requests, backup_method, None)
if options['user_agent'] == 'random_user_agent':
sub_step['headers']['User-Agent'] = random.choice(options['user_agents'])
del sub_step['method']
del sub_step['response']
if 'dependent_on_temp_event' in backup_response:
temp_event = get_dependent_results_from_database(
target,
Expand All @@ -120,6 +130,8 @@ def run(
sub_step,
temp_event
)
backup_response = copy.deepcopy(sub_step['response'])
del sub_step['response']
for _ in range(options['retries']):
try:
response = action(**sub_step)
Expand All @@ -135,7 +147,21 @@ def run(
response = []
sub_step['method'] = backup_method
sub_step['response'] = backup_response

if backup_iterative_response_match != None:
backup_iterative_response_match = copy.deepcopy(sub_step['response']['conditions'].get('iterative_response_match'))
del sub_step['response']['conditions']['iterative_response_match']

sub_step['response']['conditions_results'] = response_conditions_matched(sub_step, response)

if backup_iterative_response_match != None and (sub_step['response']['conditions_results'] or sub_step['response']['condition_type']=='or') :
sub_step['response']['conditions']['iterative_response_match'] = backup_iterative_response_match
for key in sub_step['response']['conditions']['iterative_response_match']:
result = response_conditions_matched(
sub_step['response']['conditions']['iterative_response_match'][key],response)
if result:
sub_step['response']['conditions_results'][key]=result

return process_conditions(
sub_step,
module_name,
Expand Down
78 changes: 65 additions & 13 deletions core/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def process_conditions(
continue
del event['response']['conditions']
del event['response']['condition_type']
if 'log' in event['response']:
del event['response']['log']
event_request_keys = copy.deepcopy(event)
del event_request_keys['response']
submit_logs_to_db(
Expand All @@ -83,7 +85,9 @@ def process_conditions(
"json_event": event
}
)
success_event_info(
log_list = merge_logs_to_list(event['response']['conditions_results'])
if log_list:
success_event_info(
messages("send_success_event_from_module").format(
process_number,
module_name,
Expand All @@ -92,25 +96,47 @@ def process_conditions(
total_module_thread_number,
request_number_counter,
total_number_of_requests,
" ".join(
[
color('yellow') + key + color('reset') if ':' in key
else color('green') + key + color('reset')
for key in yaml.dump(event_request_keys).split()
]
),
" ",
filter_large_content(
"conditions: " + " ".join(
"\n".join(
[
color('purple') + key + color('reset') if ':' in key
else color('green') + key + color('reset')
for key in yaml.dump(event['response']['conditions_results']).split()
color('purple') + key + color('reset')
for key in log_list
]
),
filter_rate=150
filter_rate=100000
)
)
)
else:
success_event_info(
messages("send_success_event_from_module").format(
process_number,
module_name,
target,
module_thread_number,
total_module_thread_number,
request_number_counter,
total_number_of_requests,
" ".join(
[
color('yellow') + key + color('reset') if ':' in key
else color('green') + key + color('reset')
for key in yaml.dump(event_request_keys).split()
]
),
filter_large_content(
"conditions: " + " ".join(
[
color('purple') + key + color('reset') if ':' in key
else color('green') + key + color('reset')
for key in yaml.dump(event['response']['conditions_results']).split()
]
),
filter_rate=150
)
)
)
verbose_info(
json.dumps(event)
)
Expand Down Expand Up @@ -212,6 +238,32 @@ def replace_dependent_values(sub_step, dependent_on_temp_event):
return find_and_replace_dependent_values(sub_step, dependent_on_temp_event)


def replace_dependent_response(log,result):
response_dependent = result
if str(log):
key_name = re.findall(
re.compile("response_dependent\\['\\S+\\]"),
log
)
for i in key_name:
try:
key_value = eval(i)
except Exception:
key_value = "response dependent error"
log = log.replace(i," ".join(key_value))
return log


def merge_logs_to_list(result,log_list=[]):
if type(result) == dict:
for i in result:
if 'log'==i:
log_list.append(result['log'])
else:
merge_logs_to_list(result[i],log_list)
return list(set(log_list))


def reverse_and_regex_condition(regex, reverse):
if regex:
if reverse:
Expand Down
4 changes: 2 additions & 2 deletions lib/messages/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ select_language: select a language {0}
select_profile: select profile {0}
select_user_agent: "Select a user agent to send with HTTP requests or enter \"random_user_agent\"
to randomize the User-Agent in the requests."
send_success_event_from_module: "process-{0}|{1}|{2}|module-thread {3}/{4}|request-thread {5}/{6}|{7}|success_condition
(s): {8}"
send_success_event_from_module: "process-{0}|{1}|{2}|module-thread {3}/{4}|request-thread {5}/{6}|{7}|\nsuccess_condition
(s): \n{8}"
send_unsuccess_event_from_module: "process-{0}|{1}|{2}|module-thread {3}/{4}|request-thread
{5}/{6}| all conditions failed"
sending_module_request: "process-{0}|{1}|{2}|module-thread {3}/{4}| sending request
Expand Down
Loading