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

Fix the out of scope issue with swagger option #601

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tests/cli/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ async def test_use_web_creds(mock_async_try_form_login, _, __):
# Test swagger option with a valid url
@pytest.mark.asyncio
@mock.patch("wapitiCore.main.wapiti.Wapiti.browse")
async def test_swagger_valid_url(mock_browse):
@mock.patch("wapitiCore.main.wapiti.Wapiti.attack")
async def test_swagger_valid_url(mock_browse, _):
testargs = [
"wapiti",
"-u", "https://petstore.swagger.io",
Expand All @@ -285,7 +286,8 @@ async def test_swagger_valid_url(mock_browse):
# Test swagger option with an invalid url or when option break
@pytest.mark.asyncio
@mock.patch("wapitiCore.main.wapiti.Wapiti.browse")
async def test_swagger_invalid_url(mock_browse):
@mock.patch("wapitiCore.main.wapiti.Wapiti.attack")
async def test_swagger_invalid_url(mock_browse, _):
testargs = [
"wapiti",
"-u", "http://testphp.vulnweb.com",
Expand All @@ -299,6 +301,24 @@ async def test_swagger_invalid_url(mock_browse):
mock_browse.assert_called_once()


@pytest.mark.asyncio
@mock.patch("wapitiCore.main.wapiti.Wapiti.browse")
@mock.patch("wapitiCore.main.wapiti.Wapiti.attack")
@mock.patch("wapitiCore.controller.wapiti.Wapiti.add_start_url")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add mock for attack and browse ?

async def test_out_of_scope_swagger(mock_add_start_url, _, __):
"""Test with out of scope swagger"""
testsagrs = [
"wapiti",
"--url", "http://testphp.vulnweb.com/",
"--swagger", "./tests/data/openapi3.yaml",
"-m", ""
]

with mock.patch.object(sys, "argv", testsagrs):
await wapiti_main()
mock_add_start_url.assert_not_called()


@pytest.mark.asyncio
@mock.patch("wapitiCore.main.wapiti.Wapiti.browse")
@mock.patch("wapitiCore.main.wapiti.Wapiti.attack")
Expand Down
8 changes: 7 additions & 1 deletion wapitiCore/main/wapiti.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ async def wapiti_main():

if args.swagger_uri:
swagger = Swagger(swagger_url=args.swagger_uri, base_url=url)
nb_out = 0
for request in swagger.get_requests():
wap.add_start_url(request)
if wap.target_scope.check(request):
wap.add_start_url(request)
else:
nb_out += 1
if nb_out > 0:
logging.warning(f"[!] {nb_out} out of scope requests from the Swagger file are not added.")

try:
for start_url in args.starting_urls:
Expand Down
10 changes: 4 additions & 6 deletions wapitiCore/parsers/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, swagger_url: str = None, base_url: str = None) -> None:
logging.error("[-] Error: No URL or file")

if self.swagger_dict:
self.routes = self._get_routes(self.swagger_dict, swagger_url, base_url)
self.routes = self._get_routes(self.swagger_dict, base_url)


@staticmethod
Expand Down Expand Up @@ -196,11 +196,9 @@ def is_valid_url(url) -> bool:
return False


def _get_routes(self, swagger_dict: dict, swagger_url: str, base_url: str) -> dict:
if Swagger.is_valid_url(swagger_url):
url = swagger_url
else:
url = base_url
def _get_routes(self, swagger_dict: dict, base_url: str) -> dict:
# We use the url from the -u unless the swagger file has one
url = base_url
request = {}
base_path = self._get_base_url(swagger_dict, url)
for path in swagger_dict['paths']:
Expand Down