Best Python code snippet using autotest_python
controller.py
Source:controller.py
1#!/usr/bin/env python2# encoding: UTF-83"""4This file is part of Commix Project (http://commixproject.com).5Copyright (c) 2014-2017 Anastasios Stasinopoulos (@ancst).6This program is free software: you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation, either version 3 of the License, or9(at your option) any later version.10 11For more see the file 'readme/COPYING' for copying permission.12"""13import os14import sys15import urllib216from src.utils import menu17from src.utils import logs18from src.utils import settings19from src.utils import session_handler20from src.thirdparty.colorama import Fore, Back, Style, init21from src.core.requests import headers22from src.core.requests import requests23from src.core.requests import parameters24from src.core.modules import modules_handler25from src.core.requests import authentication26from src.core.injections.controller import checks27from src.core.injections.results_based.techniques.classic import cb_handler28from src.core.injections.results_based.techniques.eval_based import eb_handler29from src.core.injections.blind.techniques.time_based import tb_handler30from src.core.injections.semiblind.techniques.file_based import fb_handler31"""32Command Injection and exploitation controller.33Checks if the testable parameter is exploitable.34"""35"""36Check for previously stored sessions.37"""38def check_for_stored_sessions(url, http_request_method):39 if not menu.options.ignore_session:40 if os.path.isfile(settings.SESSION_FILE) and not settings.REQUIRED_AUTHENTICATION:41 if not menu.options.tech:42 settings.SESSION_APPLIED_TECHNIQUES = session_handler.applied_techniques(url, http_request_method)43 menu.options.tech = settings.SESSION_APPLIED_TECHNIQUES44 if session_handler.check_stored_parameter(url, http_request_method):45 settings.LOAD_SESSION = True46 return True 47 48"""49Check for previously stored injection level.50"""51def check_for_stored_levels(url, http_request_method):52 if not menu.options.ignore_session:53 if menu.options.level == settings.DEFAULT_INJECTION_LEVEL:54 menu.options.level = session_handler.applied_levels(url, http_request_method)55 if type(menu.options.level) is not int :56 menu.options.level = settings.DEFAULT_INJECTION_LEVEL57"""58Proceed to the injection process for the appropriate parameter.59"""60def injection_proccess(url, check_parameter, http_request_method, filename, timesec):61 # User-Agent Injection / Referer Injection / Custom header Injection 62 if check_parameter.startswith(" "):63 header_name = ""64 the_type = " HTTP header"65 else:66 if settings.COOKIE_INJECTION: 67 header_name = " cookie"68 else:69 header_name = ""70 the_type = " parameter"71 check_parameter = " '" + check_parameter + "'"72 # Load modules73 modules_handler.load_modules(url, http_request_method, filename)74 if not settings.LOAD_SESSION:75 info_msg = "Setting the" 76 if not header_name == " cookie" and not the_type == " HTTP header":77 info_msg += " " + http_request_method + ""78 info_msg += the_type + header_name + check_parameter + " for tests."79 print settings.print_info_msg(info_msg)80 # Estimating the response time (in seconds)81 timesec, url_time_response = requests.estimate_response_time(url, timesec)82 skip_code_injections = False83 skip_command_injections = False84 # Procced with file-based semiblind command injection technique,85 # once the user provides the path of web server's root directory.86 if menu.options.web_root and not "f" in menu.options.tech:87 if not menu.options.web_root.endswith("/"):88 menu.options.web_root = menu.options.web_root + "/"89 if checks.procced_with_file_based_technique():90 menu.options.tech = "f"91 # Check if it is vulnerable to classic command injection technique.92 if not menu.options.tech or "c" in menu.options.tech:93 settings.CLASSIC_STATE = None94 if cb_handler.exploitation(url, timesec, filename, http_request_method) != False:95 if not menu.options.tech or "e" in menu.options.tech:96 if not menu.options.batch:97 settings.CLASSIC_STATE = True98 question_msg = "Due to resuts "99 question_msg += "skipping of code injection checks is recommended. "100 question_msg += "Do you agree? [Y/n] > "101 sys.stdout.write(settings.print_question_msg(question_msg))102 procced_option = sys.stdin.readline().replace("\n","").lower()103 else:104 procced_option = ""105 if len(procced_option) == 0:106 procced_option = "y"107 if procced_option in settings.CHOICE_YES:108 skip_code_injections = True109 elif procced_option in settings.CHOICE_NO:110 pass111 elif procced_option in settings.CHOICE_QUIT:112 sys.exit(0)113 else:114 err_msg = "'" + procced_option + "' is not a valid answer." 115 print settings.print_error_msg(err_msg)116 pass117 else:118 settings.CLASSIC_STATE = False119 # Check if it is vulnerable to eval-based code injection technique.120 if not menu.options.tech or "e" in menu.options.tech:121 if not skip_code_injections:122 settings.EVAL_BASED_STATE = None123 if eb_handler.exploitation(url, timesec, filename, http_request_method) != False:124 if not menu.options.batch:125 settings.EVAL_BASED_STATE = True126 question_msg = "Due to resuts, "127 question_msg += "skipping of further command injection checks is recommended. "128 question_msg += "Do you agree? [Y/n] > "129 sys.stdout.write(settings.print_question_msg(question_msg))130 procced_option = sys.stdin.readline().replace("\n","").lower()131 else:132 procced_option = ""133 if len(procced_option) == 0:134 procced_option = "y"135 if procced_option in settings.CHOICE_YES:136 skip_command_injections = True137 elif procced_option in settings.CHOICE_NO:138 pass139 elif procced_option in settings.CHOICE_QUIT:140 sys.exit(0)141 else:142 err_msg = "'" + procced_option + "' is not a valid answer." 143 print settings.print_error_msg(err_msg)144 pass145 else:146 settings.EVAL_BASED_STATE = False147 148 if not skip_command_injections:149 # Check if it is vulnerable to time-based blind command injection technique.150 if not menu.options.tech or "t" in menu.options.tech:151 settings.TIME_BASED_STATE = None152 if tb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response) != False:153 settings.TIME_BASED_STATE = True154 else:155 settings.TIME_BASED_STATE = False156 # Check if it is vulnerable to file-based semiblind command injection technique.157 if not menu.options.tech or "f" in menu.options.tech and not skip_command_injections:158 settings.FILE_BASED_STATE = None159 if fb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response) != False:160 settings.FILE_BASED_STATE = True161 else:162 settings.FILE_BASED_STATE = False163 # All injection techniques seems to be failed!164 if settings.CLASSIC_STATE == settings.EVAL_BASED_STATE == settings.TIME_BASED_STATE == settings.FILE_BASED_STATE == False :165 warn_msg = "The tested"166 if not header_name == " cookie" and not the_type == " HTTP header":167 warn_msg += " " + http_request_method + ""168 warn_msg += the_type + header_name + check_parameter169 warn_msg += " seems to be not injectable."170 print settings.print_warning_msg(warn_msg) + Style.RESET_ALL171"""172Inject HTTP headers (User-agent / Referer) (if level > 2).173"""174def http_headers_injection(url, http_request_method, filename, timesec):175 # User-Agent header injection176 user_agent = menu.options.agent177 menu.options.agent = settings.INJECT_TAG178 settings.USER_AGENT_INJECTION = True179 if settings.USER_AGENT_INJECTION:180 check_parameter = header_name = " User-Agent"181 settings.HTTP_HEADER = header_name[1:].replace("-","").lower()182 check_for_stored_sessions(url, http_request_method)183 injection_proccess(url, check_parameter, http_request_method, filename, timesec)184 settings.USER_AGENT_INJECTION = False185 menu.options.agent = user_agent186 # Referer header injection187 menu.options.referer = settings.INJECT_TAG188 settings.REFERER_INJECTION = True189 if settings.REFERER_INJECTION:190 check_parameter = header_name = " Referer"191 settings.HTTP_HEADER = header_name[1:].lower()192 check_for_stored_sessions(url, http_request_method)193 injection_proccess(url, check_parameter, http_request_method, filename, timesec)194 settings.REFERER_INJECTION = False 195"""196Check for stored injections on User-agent / Referer headers (if level > 2).197"""198def stored_http_header_injection(url, check_parameter, http_request_method, filename, timesec):199 for check_parameter in settings.HTTP_HEADERS:200 settings.HTTP_HEADER = check_parameter201 if check_for_stored_sessions(url, http_request_method):202 if check_parameter == "referer":203 menu.options.referer = settings.INJECT_TAG204 settings.REFERER_INJECTION = True205 else: 206 menu.options.agent = settings.INJECT_TAG207 settings.USER_AGENT_INJECTION = True208 injection_proccess(url, check_parameter, http_request_method, filename, timesec)209 if not settings.LOAD_SESSION:210 http_headers_injection(url, http_request_method, filename, timesec)211"""212Cookie injection 213"""214def cookie_injection(url, http_request_method, filename, timesec):215 settings.COOKIE_INJECTION = True216 # Cookie Injection217 if settings.COOKIE_INJECTION == True:218 cookie_value = menu.options.cookie219 header_name = " cookie"220 settings.HTTP_HEADER = header_name[1:].lower()221 cookie_parameters = parameters.do_cookie_check(menu.options.cookie)222 if type(cookie_parameters) is str:223 cookie_parameters_list = []224 cookie_parameters_list.append(cookie_parameters)225 cookie_parameters = cookie_parameters_list226 # Remove whitespaces 227 cookie_parameters = [x.replace(" ", "") for x in cookie_parameters]228 check_parameters = []229 for i in range(0, len(cookie_parameters)):230 menu.options.cookie = cookie_parameters[i]231 check_parameter = parameters.specify_cookie_parameter(menu.options.cookie)232 check_parameters.append(check_parameter)233 checks.print_non_listed_params(check_parameters, http_request_method, header_name)234 for i in range(0, len(cookie_parameters)):235 parameter = menu.options.cookie = cookie_parameters[i]236 check_parameter = parameters.specify_cookie_parameter(parameter)237 if check_parameter != parameter:238 if len(check_parameter) > 0:239 settings.TESTABLE_PARAMETER = check_parameter240 # Check if testable parameter(s) are provided241 if len(settings.TEST_PARAMETER) > 0:242 if menu.options.test_parameter != None:243 param_counter = 0244 for check_parameter in check_parameters:245 if check_parameter in "".join(settings.TEST_PARAMETER).split(","):246 menu.options.cookie = cookie_parameters[param_counter]247 # Check for session file 248 check_for_stored_sessions(url, http_request_method)249 injection_proccess(url, check_parameter, http_request_method, filename, timesec) 250 param_counter += 1251 break 252 else:253 # Check for session file 254 check_for_stored_sessions(url, http_request_method)255 injection_proccess(url, check_parameter, http_request_method, filename, timesec) 256 257 if settings.COOKIE_INJECTION == True:258 # Restore cookie value259 menu.options.cookie = cookie_value260 # Disable cookie injection 261 settings.COOKIE_INJECTION = False262"""263Check if HTTP Method is GET.264""" 265def get_request(url, http_request_method, filename, timesec):266 #if not settings.COOKIE_INJECTION:267 found_url = parameters.do_GET_check(url)268 if found_url != False:269 check_parameters = []270 for i in range(0, len(found_url)):271 url = found_url[i]272 check_parameter = parameters.vuln_GET_param(url)273 check_parameters.append(check_parameter)274 header_name = ""275 checks.print_non_listed_params(check_parameters, http_request_method, header_name)276 for i in range(0, len(found_url)):277 url = found_url[i]278 check_parameter = parameters.vuln_GET_param(url)279 if check_parameter != url:280 if len(check_parameter) > 0:281 settings.TESTABLE_PARAMETER = check_parameter282 283 # Check if testable parameter(s) are provided284 if len(settings.TESTABLE_PARAMETER) > 0:285 if menu.options.test_parameter != None:286 url_counter = 0287 for check_parameter in check_parameters:288 if check_parameter in "".join(settings.TEST_PARAMETER).split(","):289 url = found_url[url_counter]290 # Check for session file 291 check_for_stored_sessions(url, http_request_method)292 injection_proccess(url, check_parameter, http_request_method, filename, timesec)293 url_counter += 1294 break295 else:296 # Check for session file 297 check_for_stored_sessions(url, http_request_method)298 injection_proccess(url, check_parameter, http_request_method, filename, timesec)299 else:300 # Check for session file 301 check_for_stored_sessions(url, http_request_method)302 injection_proccess(url, check_parameter, http_request_method, filename, timesec)303 # Enable Cookie Injection304 if menu.options.level > settings.DEFAULT_INJECTION_LEVEL and menu.options.cookie:305 settings.COOKIE_INJECTION = True306"""307Check if HTTP Method is POST.308""" 309def post_request(url, http_request_method, filename, timesec):310 # Check if HTTP Method is POST.311 parameter = menu.options.data312 found_parameter = parameters.do_POST_check(parameter)313 # Check if singe entry parameter314 if type(found_parameter) is str:315 found_parameter_list = []316 found_parameter_list.append(found_parameter)317 found_parameter = found_parameter_list318 # Remove whitespaces 319 found_parameter = [x.replace(" ", "") for x in found_parameter]320 # Check if multiple parameters321 check_parameters = []322 for i in range(0, len(found_parameter)):323 parameter = menu.options.data = found_parameter[i]324 check_parameter = parameters.vuln_POST_param(parameter, url)325 check_parameters.append(check_parameter)326 header_name = ""327 checks.print_non_listed_params(check_parameters, http_request_method, header_name)328 for i in range(0, len(found_parameter)):329 parameter = menu.options.data = found_parameter[i]330 check_parameter = parameters.vuln_POST_param(parameter, url)331 if check_parameter != parameter:332 if len(check_parameter) > 0:333 settings.TESTABLE_PARAMETER = check_parameter334 # Check if testable parameter(s) are provided335 if len(settings.TESTABLE_PARAMETER) > 0:336 if menu.options.test_parameter != None:337 param_counter = 0338 for check_parameter in check_parameters:339 if check_parameter in "".join(settings.TEST_PARAMETER).split(","):340 menu.options.data = found_parameter[param_counter] 341 check_for_stored_sessions(url, http_request_method)342 injection_proccess(url, check_parameter, http_request_method, filename, timesec)343 param_counter += 1344 break345 else:346 # Check for session file 347 check_for_stored_sessions(url, http_request_method)348 injection_proccess(url, check_parameter, http_request_method, filename, timesec)349 else:350 # Check for session file 351 check_for_stored_sessions(url, http_request_method)352 injection_proccess(url, check_parameter, http_request_method, filename, timesec)353 # Enable Cookie Injection354 if menu.options.level > settings.DEFAULT_INJECTION_LEVEL and menu.options.cookie:355 settings.COOKIE_INJECTION = True356"""357Perform checks358"""359def perform_checks(url, filename):360 def basic_level_checks():361 settings.PERFORM_BASIC_SCANS = False362 # Check if HTTP Method is GET.363 if not menu.options.data:364 get_request(url, http_request_method, filename, timesec)365 # Check if HTTP Method is POST. 366 else:367 post_request(url, http_request_method, filename, timesec)368 timesec = settings.TIMESEC369 # Check if authentication is needed.370 if menu.options.auth_url and menu.options.auth_data:371 # Do the authentication process.372 authentication.authentication_process()373 # Check if authentication page is the same with the next (injection) URL374 if urllib2.urlopen(url).read() == urllib2.urlopen(menu.options.auth_url).read():375 err_msg = "It seems that the authentication procedure has failed."376 print settings.print_critical_msg(err_msg)377 sys.exit(0)378 elif menu.options.auth_url or menu.options.auth_data: 379 err_msg = "You must specify both login panel URL and login parameters."380 print settings.print_critical_msg(err_msg)381 sys.exit(0)382 else:383 pass384 # Check if HTTP Method is GET.385 if not menu.options.data:386 http_request_method = "GET" 387 else:388 http_request_method = "POST"389 if menu.options.shellshock:390 menu.options.level = settings.HTTP_HEADER_INJECTION_LEVEL391 else:392 check_for_stored_levels(url, http_request_method)393 if settings.PERFORM_BASIC_SCANS:394 basic_level_checks()395 # Check for stored injections on User-agent / Referer headers (if level > 2).396 if menu.options.level >= settings.HTTP_HEADER_INJECTION_LEVEL:397 if settings.INJECTED_HTTP_HEADER == False :398 check_parameter = ""399 stored_http_header_injection(url, check_parameter, http_request_method, filename, timesec)400 else:401 # Enable Cookie Injection402 if menu.options.level > settings.DEFAULT_INJECTION_LEVEL:403 if menu.options.cookie:404 cookie_injection(url, http_request_method, filename, timesec)405 else:406 warn_msg = "The HTTP Cookie header is not provided, "407 warn_msg += "so this test is going to be skipped."408 print settings.print_warning_msg(warn_msg)409 else:410 # Custom header Injection411 if settings.CUSTOM_HEADER_INJECTION == True:412 check_parameter = header_name = " " + settings.CUSTOM_HEADER_NAME413 settings.HTTP_HEADER = header_name[1:].lower()414 check_for_stored_sessions(url, http_request_method)415 injection_proccess(url, check_parameter, http_request_method, filename, timesec)416 settings.CUSTOM_HEADER_INJECTION = None417 if settings.INJECTION_CHECKER == False:418 return False419 else:420 return True 421"""422General check on every injection technique.423"""424def do_check(url, filename):425 if menu.options.wizard:426 if perform_checks(url,filename) == False:427 scan_level = menu.options.level428 while int(scan_level) < int(settings.HTTP_HEADER_INJECTION_LEVEL) and settings.LOAD_SESSION != True:429 if not menu.options.batch:430 question_msg = "Do you want to increase to '--level=" + str(scan_level + 1) 431 question_msg += "' in order to perform more tests? [Y/n] > "432 sys.stdout.write(settings.print_question_msg(question_msg))433 next_level = sys.stdin.readline().replace("\n","").lower()434 else:435 next_level = ""436 if len(next_level) == 0:437 next_level = "y"438 if next_level in settings.CHOICE_YES:439 menu.options.level = int(menu.options.level + scan_level)440 if perform_checks(url,filename) == False and scan_level < settings.HTTP_HEADER_INJECTION_LEVEL :441 scan_level = scan_level + 1442 else:443 break 444 elif next_level in settings.CHOICE_NO:445 break446 elif next_level in settings.CHOICE_QUIT:447 sys.exit(0)448 else:449 err_msg = "'" + next_level + "' is not a valid answer." 450 print settings.print_error_msg(err_msg)451 pass452 else:453 perform_checks(url,filename)454 455 # All injection techniques seems to be failed!456 if settings.CLASSIC_STATE == settings.EVAL_BASED_STATE == settings.TIME_BASED_STATE == settings.FILE_BASED_STATE == False :457 if settings.INJECTION_CHECKER == False:458 err_msg = "All tested parameters "459 if menu.options.level > 2:460 err_msg += "and headers "461 err_msg += "appear to be not injectable."462 if not menu.options.alter_shell :463 err_msg += " Try to use the option '--alter-shell'"464 else:465 err_msg += " Try to remove the option '--alter-shell'"466 if menu.options.level < settings.HTTP_HEADER_INJECTION_LEVEL :467 err_msg += " and/or try to increase '--level' values to perform"468 err_msg += " more tests (i.e 'User-Agent', 'Referer', 'Cookie' etc)"469 else:470 if menu.options.skip_empty:471 err_msg += " and/or try to remove the option '--skip-empty'" 472 err_msg += "."473 print settings.print_critical_msg(err_msg)474 logs.print_logs_notification(filename, url)475 if not menu.options.bulkfile or settings.EOF:476 print "" 477 #sys.exit(0)...
test_utility.py
Source:test_utility.py
...29 pass30 def test_check_parameter_range(self):31 # verify parameter type correction32 with assert_raises(TypeError):33 check_parameter('f', 0, 100)34 with assert_raises(TypeError):35 check_parameter(1, 'f', 100)36 with assert_raises(TypeError):37 check_parameter(1, 0, 'f')38 # with assert_raises(TypeError):39 # check_parameter(argmaxn(value_list=[1, 2, 3], n=1), 0, 100)40 # if low and high are both unset41 with assert_raises(ValueError):42 check_parameter(50)43 # if low <= high44 with assert_raises(ValueError):45 check_parameter(50, 100, 99)46 with assert_raises(ValueError):47 check_parameter(50, 100, 100)48 # check one side49 with assert_raises(ValueError):50 check_parameter(50, low=100)51 with assert_raises(ValueError):52 check_parameter(50, high=0)53 assert_equal(True, check_parameter(50, low=10))54 assert_equal(True, check_parameter(50, high=100))55 # if check fails56 with assert_raises(ValueError):57 check_parameter(-1, 0, 100)58 with assert_raises(ValueError):59 check_parameter(101, 0, 100)60 with assert_raises(ValueError):61 check_parameter(0.5, 0.2, 0.3)62 # if check passes63 assert_equal(True, check_parameter(50, 0, 100))64 assert_equal(True, check_parameter(0.5, 0.1, 0.8))65 # if includes left or right bounds66 with assert_raises(ValueError):67 check_parameter(100, 0, 100, include_left=False,68 include_right=False)69 assert_equal(True, check_parameter(0, 0, 100, include_left=True,70 include_right=False))71 assert_equal(True, check_parameter(0, 0, 100, include_left=True,72 include_right=True))73 assert_equal(True, check_parameter(100, 0, 100, include_left=False,74 include_right=True))75 assert_equal(True, check_parameter(100, 0, 100, include_left=True,76 include_right=True))77 def tearDown(self):...
main.py
Source:main.py
...14 'LSHIFT': lambda x, y: (x << y) % s,15}16def convert(inp):17 return registers.get(inp, 0) if inp[0] in ascii_lowercase else int(inp)18def check_parameter(p):19 if p[0] not in ascii_lowercase:20 registers[p] = int(p)21 if isinstance(registers.get(p), int):22 for register in waiting_for[p]:23 if p in dependencies[register]:24 dependencies[register].remove(p)25 if not dependencies[register]:26 val = registers[register]27 if len(val) == 1:28 registers[register] = convert(val[0])29 elif len(val) == 2:30 registers[register] = val[0](convert(val[1]))31 else:32 registers[register] = val[0](convert(val[1]),33 convert(val[2]))34 check_parameter(register)35 waiting_for[p] = []36for instruction in instructions:37 args = instruction.strip().split(' ')38 if len(args) == 3:39 registers[args[2]] = (args[0],)40 waiting_for[args[0]].append(args[2])41 dependencies[args[2]].append(args[0])42 check_parameter(args[0])43 if len(args) == 4:44 registers[args[3]] = (operators[args[0]], args[1])45 waiting_for[args[1]].append(args[3])46 dependencies[args[3]].append(args[1])47 check_parameter(args[1])48 if len(args) == 5:49 registers[args[4]] = (operators[args[1]], args[0], args[2])50 waiting_for[args[0]].append(args[4])51 waiting_for[args[2]].append(args[4])52 dependencies[args[4]].append(args[0])53 dependencies[args[4]].append(args[2])54 check_parameter(args[0])55 check_parameter(args[2])56print('The value in wire a is {}'.format(registers['a']))57registers = {'b': registers['a']}58for instruction in instructions:59 args = instruction.strip().split(' ')60 if len(args) == 3:61 if args[2] == 'b':62 continue63 registers[args[2]] = (args[0],)64 waiting_for[args[0]].append(args[2])65 dependencies[args[2]].append(args[0])66 check_parameter(args[0])67 if len(args) == 4:68 registers[args[3]] = (operators[args[0]], args[1])69 waiting_for[args[1]].append(args[3])70 dependencies[args[3]].append(args[1])71 check_parameter(args[1])72 if len(args) == 5:73 registers[args[4]] = (operators[args[1]], args[0], args[2])74 waiting_for[args[0]].append(args[4])75 waiting_for[args[2]].append(args[4])76 dependencies[args[4]].append(args[0])77 dependencies[args[4]].append(args[2])78 check_parameter(args[0])79 check_parameter(args[2])...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!