Skip to content

Commit a447ec1

Browse files
committed
added pyint conformity
1 parent 7515724 commit a447ec1

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

fapi.py

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
# pylint: disable=line-too-long,C0114
2+
13
import argparse
4+
import sys
25
import requests
36

4-
class ValidateMethodsAction(argparse.Action):
7+
def validatemethod(value):
8+
r"""Validates the methods passed into the script via the -m parameter
9+
"""
10+
11+
methods = list(dict.fromkeys(value.split(',')))
12+
if methods[0] == 'all':
13+
return 'get', 'post', 'delete', 'put'
14+
for method in methods:
15+
if method.lower() not in ['get','post','put','delete']:
16+
raise argparse.ArgumentTypeError(f"argument -m/--method: invalid choice(s): '{methods}' (choose from 'get', 'post', 'put', 'delete' or choose 'all')")
17+
return methods
18+
19+
def formatter(prog):
20+
r"""Formats the help display of the script to have a certain width
21+
"""
522

6-
def __call__(self, parser, namespace, values, option_string=None):
7-
methods = list(dict.fromkeys(values[0].split(',')))
8-
if methods[0] != 'all':
9-
for method in methods:
10-
if method.lower() not in ['get','post','put','delete']:
11-
parser.error(f"argument -m/--method: invalid choice(s): '{methods}' (choose from 'get', 'post', 'put', 'delete' or choose 'all')")
12-
else:
13-
methods = ['get', 'post', 'delete', 'put']
14-
setattr(namespace, self.dest, methods)
23+
return argparse.HelpFormatter(prog, max_help_position=64)
1524

1625
def error(message):
26+
r"""Is called whenever an error is handled by the script
27+
28+
:param message: Error message to be printed.
29+
"""
1730

1831
print(message)
19-
exit()
32+
sys.exit()
2033

2134
def process_response(request, match_string, default_testing_length, verbose):
35+
r"""Processes the response of the selected method and print it out
36+
37+
:param request: The request's:class:`Response <Response>` object.
38+
:param default_testing_length: The default_testing_length that is tested against.
39+
:param verbose: Enable/disable verbose output.
40+
:param match_string: The string to match within the response text.
41+
"""
2242

2343
response = request.text
2444
if match_string is not None:
@@ -31,51 +51,66 @@ def process_response(request, match_string, default_testing_length, verbose):
3151
if verbose:
3252
print(f"[VERBOSE] {request.request.method} {str(len(response))} {request.request.url}")
3353

34-
def prepare_request(methods, url, default_testing_length, verbose, ignore_ssl_verification, match_string):
54+
def prepare_request(methods : list, url : str, match_string : str, timeout : float, default_testing_length : int, verbose : bool, ignore_ssl_verification : bool):
55+
r"""Prepare the requests to be sent
56+
57+
:param methods: The methods set for the current URL.
58+
:param url: The current URL to be reqquested.
59+
:param default_testing_length: The default_testing_length that is tested against.
60+
:param verbose: Enable/disable verbose output.
61+
:param timeout: The amount of seconds after a request times out.
62+
:param ignore_ssl_verification: Ignore SSL verification.
63+
:param match_string: The string to match within the response text.
64+
"""
3565

3666
for method in methods:
3767
try:
38-
request = requests.request(method, url, verify=ignore_ssl_verification)
68+
request = requests.request(method, url, timeout=timeout, verify=ignore_ssl_verification)
3969
process_response(request, match_string, default_testing_length, verbose)
4070
except requests.ConnectTimeout:
41-
if verbose:
42-
print(f"[Verbose] {method} - Connection timed out - {url}")
71+
print(f"[Verbose] {method} - Connection timed out - {url}")
4372

44-
def banner():
73+
def banner() -> None:
74+
r"""Prints the banner at the start of the script
75+
"""
4576

4677
print("""_____________________________________________________________________
4778
FAPI - API endpoint fuzzer v0.1
4879
Developed by: arcan3, TheM8thy
49-
Last updated: 26/04/2023
80+
Last updated: 27/04/2023
5081
_____________________________________________________________________
5182
""")
5283

5384
def fapi():
85+
r"""Main function
86+
"""
5487

5588
banner()
5689

57-
formatter = lambda prog: argparse.HelpFormatter(prog,max_help_position=64)
5890
parser = argparse.ArgumentParser(formatter_class=formatter)
5991
parser.add_argument('-u', '--url', metavar='<example url>', type=str, help='Specify the target url after the -u flag.', nargs='+', required=True)
6092
parser.add_argument('-w', '--wordlist', metavar='<wordlist>', type=argparse.FileType('r'), help='Specify your chosen wordlist after the -w flag.', nargs=1, required=True)
61-
parser.add_argument('-m', '--method', action=ValidateMethodsAction, metavar='<method>', type=str, help='Specify the desired request methods, accepted methods are get,post,put,delete.', nargs=1, required=True)
93+
parser.add_argument('-m', '--method', metavar='<method>', type=validatemethod, help='Specify the desired request methods, accepted methods are get,post,put,delete.', nargs=1, required=True)
6294
parser.add_argument('-dl', '--default_testing_length', metavar='', type=int, help='Specify the testing length thats tested against.', default=0, nargs=1)
6395
parser.add_argument('-ms', '--match_string', metavar='<string>', type=str, help='Match a specific string within the response text.', nargs='+')
96+
parser.add_argument('-t', '--timeout', metavar='<seconds>', type=float, help='Specify the amount of seconds before a request times out, defaults to 5', default=5)
6497
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose mode')
6598
parser.add_argument('-k', '--ignore_certificates', action='store_false', help='Ignore SSL certificate verification.')
6699
#group = parser.add_mutually_exclusive_group(required=True)
67-
#group.add_argument('-t', '--threading', metavar='<threads>', type=int, help='Specify number of threads to use.', nargs=1)
100+
#group.add_argument('-th', '--threading', metavar='<threads>', type=int, help='Specify number of threads to use.', nargs=1)
68101
#group.add_argument('-mp', '--multiprocessing', metavar='<processes>', type=int, help='Specify number of processes to use.', nargs=1)
69102
parser.add_argument('--version', action='version', help='Show %(prog)s version number', version='%(prog)s 0.1')
70-
parser._actions[0].help='Display the help options.'
103+
parser._actions[0].help='Display the help options.' # pylint: disable=W0212
71104
args = parser.parse_args()
105+
72106
urls : list = args.url
73107
wordlist = args.wordlist[0]
74-
methods : list = args.method
108+
methods : list = args.method[0]
75109
default_testing_length : int = args.default_testing_length[0] if isinstance(args.default_testing_length,list) else args.default_testing_length
76-
ignore_ssl_verification = args.ignore_certificates
110+
ignore_ssl_verification : bool = args.ignore_certificates
77111
match_string : list = args.match_string
78112
verbose : bool = args.verbose
113+
timeout : float = args.timeout
79114

80115
print(f"URL: {','.join(urls)}")
81116
print(f"Methods: {','.join([x.upper() for x in methods])}")
@@ -94,6 +129,6 @@ def fapi():
94129
for url in urls:
95130
for endpoint in [x for x in endpoints if x[0].strip() not in ['#','','/']]:
96131
request_url = f"{url}/{endpoint.strip()}"
97-
prepare_request(methods, request_url, default_testing_length, verbose, ignore_ssl_verification, match_string)
132+
prepare_request(methods, request_url, match_string, timeout, default_testing_length, verbose, ignore_ssl_verification)
98133

99134
fapi()

0 commit comments

Comments
 (0)