import pytest import requests from examples.complex import xss_scanner as xss from mitmproxy.test import tflow, tutils class TestXSSScanner(): def test_get_XSS_info(self): # First type of exploit: # Exploitable: xss_info = xss.get_XSS_data(b"" % xss.FULL_PAYLOAD, "https://example.com", "End of URL") expected_xss_info = xss.XSSData('https://example.com', "End of URL", '" % xss.FULL_PAYLOAD.replace(b"'", b"%27").replace(b'"', b"%22"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", '" % xss.FULL_PAYLOAD.replace(b"'", b"%27").replace(b'"', b"%22").replace(b"/", b"%2F"), "https://example.com", "End of URL") assert xss_info is None # Second type of exploit: # Exploitable: xss_info = xss.get_XSS_data(b"" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").replace(b"\"", b"%22"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "';alert(0);g='", xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"\"", b"%22").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b"\"", b"%22").replace(b"'", b"%22"), "https://example.com", "End of URL") assert xss_info is None # Third type of exploit: # Exploitable: xss_info = xss.get_XSS_data(b"" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").replace(b"'", b"%27"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", '";alert(0);g="', xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"'", b"%27").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b"'", b"%27").replace(b"\"", b"%22"), "https://example.com", "End of URL") assert xss_info is None # Fourth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD, "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "'>", xss.FULL_PAYLOAD.decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"'", b"%27"), "https://example.com", "End of URL") assert xss_info is None # Fifth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"'", b"%27"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "\">", xss.FULL_PAYLOAD.replace(b"'", b"%27").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"'", b"%27").replace(b"\"", b"%22"), "https://example.com", "End of URL") assert xss_info is None # Sixth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD, "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", ">", xss.FULL_PAYLOAD.decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"=", b"%3D"), "https://example.com", "End of URL") assert xss_info is None # Seventh type of exploit: PAYLOAD # Exploitable: xss_info = xss.get_XSS_data(b"%s" % xss.FULL_PAYLOAD, "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "", xss.FULL_PAYLOAD.decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable xss_info = xss.get_XSS_data(b"%s" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").replace(b"/", b"%2F"), "https://example.com", "End of URL") assert xss_info is None # Eighth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "Javascript:alert(0)", xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"=", b"%3D"), "https://example.com", "End of URL") assert xss_info is None # Ninth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", '" onmouseover="alert(0)" t="', xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b'"', b"%22"), "https://example.com", "End of URL") assert xss_info is None # Tenth type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", "' onmouseover='alert(0)' t='", xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"'", b"%22"), "https://example.com", "End of URL") assert xss_info is None # Eleventh type of exploit: Test # Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E"), "https://example.com", "End of URL") expected_xss_info = xss.XSSData("https://example.com", "End of URL", " onmouseover=alert(0) t=", xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E").decode('utf-8')) assert xss_info == expected_xss_info # Non-Exploitable: xss_info = xss.get_XSS_data(b"Test" % xss.FULL_PAYLOAD.replace(b"<", b"%3C").replace(b">", b"%3E") .replace(b"=", b"%3D"), "https://example.com", "End of URL") assert xss_info is None def test_get_SQLi_data(self): sqli_data = xss.get_SQLi_data("SQL syntax MySQL", "", "https://example.com", "End of URL") expected_sqli_data = xss.SQLiData("https://example.com", "End of URL", "SQL syntax.*MySQL", "MySQL") assert sqli_data == expected_sqli_data sqli_data = xss.get_SQLi_data("SQL syntax MySQL", "SQL syntax MySQL", "https://example.com", "End of URL") assert sqli_data is None def test_inside_quote(self): assert not xss.inside_quote("'", b"no", 0, b"no") assert xss.inside_quote("'", b"yes", 0, b"'yes'") assert xss.inside_quote("'", b"yes", 1, b"'yes'otherJunk'yes'more") assert not xss.inside_quote("'", b"longStringNotInIt", 1, b"short") def test_paths_to_text(self): text = xss.paths_to_text("""