Skip to content

Commit

Permalink
reformat some code + warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
devl00p committed Sep 22, 2024
1 parent c4d1e08 commit f6747b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion wapitiCore/attack/mod_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from httpx import RequestError
import humanize
from cryptography.hazmat._oid import NameOID
from cryptography.x509.oid import NameOID
from cryptography.hazmat.backends import default_backend
from cryptography import x509

Expand Down
3 changes: 3 additions & 0 deletions wapitiCore/net/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ async def async_get(
@param headers: Dictionary of additional headers to send with the request.
@type headers: dict
@type stream: bool
@type timeout: float
@rtype: Response
"""
timeout = self.timeout if timeout is None else httpx.Timeout(timeout)
Expand Down Expand Up @@ -262,6 +263,7 @@ async def async_post(
@type follow_redirects: bool
@type headers: dict
@type stream: bool
@type timeout: float
@rtype: Response
"""
form_headers = {}
Expand Down Expand Up @@ -332,6 +334,7 @@ async def async_request(
@type follow_redirects: bool
@type headers: dict
@type stream: bool
@type timeout: float
@rtype: Response
"""
form_headers = {}
Expand Down
41 changes: 15 additions & 26 deletions wapitiCore/parsers/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ class Swagger:
swagger_dict = None
routes = None


def __init__(self, swagger_url: str = None, base_url: str = None) -> None:
if swagger_url:
try:
self.swagger_dict = ResolvingParser(swagger_url, backend='openapi-spec-validator',
strict=False, recursion_limit=5).specification
strict=False, recursion_limit=5).specification
except ParseError as e:
logging.error("[-] Error: Swagger file format invalid : " + str(e.args[0]))
except ValidationError as e:
logging.error("[-] Error: Swagger file is not valid : " + str(e.args[0]) +
". See https://swagger.io/specification/ for more information.")
". See https://swagger.io/specification/ for more information.")
except AssertionError:
logging.error("[-] Error: File not found")
except ResolutionError:
Expand All @@ -64,7 +63,6 @@ def __init__(self, swagger_url: str = None, base_url: str = None) -> None:
if self.swagger_dict:
self.routes = self._get_routes(self.swagger_dict, base_url)


@staticmethod
def _get_base_url(swagger_dict: dict, url: str) -> str:
try:
Expand Down Expand Up @@ -115,10 +113,9 @@ def _check_properties(model_name: dict) -> dict:
return model_name['additionalProperties']
return model_name


# Parse object in swagger file.
# Replace all object by their type and all array by their type
# acording to their properties and definitions.
# according to their properties and definitions.
# It will be easier to create request with default value.
def _parse_object(self, model_name):
model = {}
Expand Down Expand Up @@ -151,7 +148,6 @@ def _parse_object(self, model_name):
"\nSee https://swagger.io/specification/ for more information")
return model


def _check_params(self, params: dict) -> list:
raws = []
for param in params:
Expand All @@ -178,9 +174,9 @@ def _check_params(self, params: dict) -> list:
if 'type' in param:
if param['type'] == "array":
if 'enum' in param['items']:
raw['type'] = {"enum" : param['items']['enum']}
raw['type'] = {"enum": param['items']['enum']}
else:
raw['type'] = {"array" : param['items']['type']}
raw['type'] = {"array": param['items']['type']}
else:
raw['type'] = param['type']
if 'name' in param:
Expand All @@ -191,7 +187,6 @@ def _check_params(self, params: dict) -> list:
raws.append(raw)
return raws


@staticmethod
def is_valid_url(url) -> bool:
try:
Expand All @@ -200,7 +195,6 @@ def is_valid_url(url) -> bool:
except ValueError:
return False


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
Expand All @@ -216,10 +210,15 @@ def _get_routes(self, swagger_dict: dict, base_url: str) -> dict:
request[route] = []
try:
if params:
request_route = {"method": method.upper(), "route": route.replace(method.upper() + ' ', '')}
request_route['params'] = []
request_route = {
"method": method.upper(),
"route": route.replace(method.upper() + ' ', ''),
'params': []
}

if 'requestBody' in params:
request_route['params'] += self._check_params(params['requestBody']['content'])

if 'parameters' in params:
request_route['params'] += self._check_params(params['parameters'])
request_route['params'] += self._check_params(params)
Expand All @@ -231,7 +230,6 @@ def _get_routes(self, swagger_dict: dict, base_url: str) -> dict:
logging.error("[-] Error: " + str(e))
return request


def _parse_parameters(self, params: list, route: str) -> str:
for param in params:
if not "type" in param:
Expand All @@ -243,7 +241,6 @@ def _parse_parameters(self, params: list, route: str) -> str:
route += "&" + param['name'] + "=" + self.AUTOFILL_VALUES[param['type']]
return route


def _get_parameters(self, swagger_dict: dict, route: str, url: str) -> list:
try:
base_path = self._get_base_url(swagger_dict, url)
Expand All @@ -261,7 +258,6 @@ def _get_parameters(self, swagger_dict: dict, route: str, url: str) -> list:
logging.warning("[-] Skipping " + route + " : " + str(e))
return None


# transform dict {array: something} and if something is a dict and contains {array: something} transform it
def _transform_array(self, array: dict) -> list:
if 'array' in array:
Expand All @@ -279,7 +275,6 @@ def _transform_array(self, array: dict) -> list:
array[key] = self.AUTOFILL_VALUES[array[key]]
return array


def _transform_query(self, route: str, param: dict, option: str):
if '?' in self.routes[route][0]['route'] or '?' in option:
option += "&" + param['name'] + "="
Expand Down Expand Up @@ -320,7 +315,6 @@ def _transform_query(self, route: str, param: dict, option: str):

return option


def _transform_url(self, param: dict, url: str, route: str) -> str:
name = param['name']
if "{" in url:
Expand All @@ -332,14 +326,13 @@ def _transform_url(self, param: dict, url: str, route: str) -> str:
# we will use the target url from -u option
pass
method = route.split(' ')[0].lower()
if not 'type' in param:
if 'type' not in param:
param = self.swagger_dict['paths'][route_parsed][method]['parameters'][0]['schema']
if not 'type' in param:
if 'type' not in param:
# if still missing, we add a default type string
param['type'] = "string"
return url.replace("{" + name + "}", self.AUTOFILL_VALUES[param['type']])


def _transform_body(self, param: dict) -> str:
json_dict = {}
if param['model']:
Expand All @@ -352,7 +345,6 @@ def _transform_body(self, param: dict) -> str:
json_dict[key] = self.AUTOFILL_VALUES[param['model'][key]]
return json.dumps(json_dict)


def _transform_formData(self, param: dict, files: list) -> str:
data = ""
if 'type' in param:
Expand All @@ -367,7 +359,6 @@ def _transform_formData(self, param: dict, files: list) -> str:
data = self._add_data(data, param['name'], self.AUTOFILL_VALUES[param['type']])
return data


# create request with default value from swagger file
def _create_request(self, routes: dict) -> list[Request]:
requests_list = []
Expand Down Expand Up @@ -396,13 +387,12 @@ def _create_request(self, routes: dict) -> list[Request]:
if not 'type' in param:
param["type"] = "string"
header[param['name']] = self.AUTOFILL_VALUES[param['type']]
request = Request(path=url+option, method=urls[0]['method'], post_params=data, file_params=files,
request = Request(path=url + option, method=urls[0]['method'], post_params=data, file_params=files,
enctype="application/json")
request.set_headers(header)
requests_list.append(request)
return requests_list


@staticmethod
def _add_data(data, name: str, value: str) -> str:
if data != "":
Expand All @@ -411,7 +401,6 @@ def _add_data(data, name: str, value: str) -> str:
data += name + "=" + value
return data


def _replace_param(self, json_dict: dict) -> dict:
if 'array' in json_dict:
if isinstance(json_dict['array'], dict):
Expand Down

0 comments on commit f6747b4

Please sign in to comment.