Skip to content

Commit 935c8a9

Browse files
authored
Improved linting (#50)
1 parent 245eb29 commit 935c8a9

File tree

11 files changed

+695
-227
lines changed

11 files changed

+695
-227
lines changed

.github/workflows/auto_prefix_issues.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Autoprefix & Label Issues
1+
name: Auto-prefix & Label Issues
22

33
on:
44
issues:

Makefile

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,70 @@
1-
.PHONY : all clean build upload
1+
.PHONY : all clean build upload test test-verbose test-coverage lint lint-fix fix
2+
3+
PROJECTNAME := apachetomcatscanner
24

35
all: install clean
46

57
clean:
68
@rm -rf `find ./ -type d -name "*__pycache__"`
7-
@rm -rf ./build/ ./dist/ ./apachetomcatscanner.egg-info/
9+
@rm -rf ./build/ ./dist/ ./$(PROJECTNAME).egg-info/
10+
11+
generate-docs:
12+
@python3 -m pip install pdoc --break-system-packages
13+
@echo "[$(shell date)] Generating docs ..."
14+
@PDOC_ALLOW_EXEC=1 python3 -m pdoc -d markdown -o ./documentation/ ./$(PROJECTNAME)/
15+
@echo "[$(shell date)] Done!"
16+
17+
uninstall:
18+
pip uninstall $(PROJECTNAME) --yes --break-system-packages
819

920
install: build
10-
pip install . --break-system-packages
21+
python3 -m pip install . --break-system-packages
1122

1223
build:
13-
python3 -m pip uninstall apachetomcatscanner --yes --break-system-packages
14-
pip install .[build] --break-system-packages
15-
python3 -m build --wheel
24+
python3 -m pip uninstall $(PROJECTNAME) --yes --break-system-packages
25+
python3 -m pip install build --break-system-packages
26+
python3 -m build
27+
28+
upload: uninstall clean build
29+
python3 -m pip install twine setuptools packaging --upgrade --break-system-packages
30+
python3 -m twine upload dist/*.whl dist/*.tar.gz
31+
32+
test:
33+
@echo "[$(shell date)] Running tests ..."
34+
@cd $(PROJECTNAME)/tests && python3 run_tests.py
35+
@echo "[$(shell date)] Tests completed!"
36+
37+
test-verbose:
38+
@echo "[$(shell date)] Running tests with verbose output ..."
39+
@cd $(PROJECTNAME)/tests && python3 -m unittest discover -v
40+
@echo "[$(shell date)] Tests completed!"
41+
42+
test-coverage:
43+
@echo "[$(shell date)] Installing coverage and running tests with coverage ..."
44+
@python3 -m pip install coverage --break-system-packages
45+
@coverage run --source=$(PROJECTNAME) $(PROJECTNAME)/tests/run_tests.py
46+
@coverage report
47+
@coverage html
48+
@echo "[$(shell date)] Coverage report generated in htmlcov/"
49+
50+
lint:
51+
@echo "[$(shell date)] Installing linting tools ..."
52+
@python3 -m pip install flake8 black isort --break-system-packages
53+
@echo "[$(shell date)] Running flake8 linting ..."
54+
@python3 -m flake8 $(PROJECTNAME)/ --max-line-length=88 --extend-ignore=E501
55+
@echo "[$(shell date)] Running black code formatting check ..."
56+
@python3 -m black --check --diff $(PROJECTNAME)/
57+
@echo "[$(shell date)] Running isort import sorting check ..."
58+
@python3 -m isort --check-only --diff $(PROJECTNAME)/
59+
@echo "[$(shell date)] Linting completed!"
1660

17-
upload: build
18-
pip install .[twine] --break-system-packages
19-
twine upload dist/*
61+
lint-fix:
62+
@echo "[$(shell date)] Installing linting tools ..."
63+
@python3 -m pip install flake8 black isort --break-system-packages
64+
@echo "[$(shell date)] Running black to fix formatting issues ..."
65+
@python3 -m black $(PROJECTNAME)/
66+
@echo "[$(shell date)] Running isort to fix import sorting ..."
67+
@python3 -m isort $(PROJECTNAME)/
68+
@echo "[$(shell date)] Running flake8 to check remaining issues ..."
69+
@python3 -m flake8 $(PROJECTNAME)/ --max-line-length=88 --extend-ignore=E501
70+
@echo "[$(shell date)] Code formatting fixes completed!"

apachetomcatscanner/Config.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ def debug(self, msg):
3939

4040
def __load_default_credentials(self):
4141
self.credentials = {}
42-
path_to_creds = os.path.dirname(__file__) + os.path.sep + 'data' + os.path.sep + 'credentials.json'
43-
f = open(path_to_creds, 'r')
42+
path_to_creds = (
43+
os.path.dirname(__file__)
44+
+ os.path.sep
45+
+ "data"
46+
+ os.path.sep
47+
+ "credentials.json"
48+
)
49+
f = open(path_to_creds, "r")
4450
self.credentials = json.loads(f.read())["credentials"]
4551
f.close()
4652
return None
4753

48-
def load_credentials_from_options(self, username, password, usernames_file, passwords_file):
54+
def load_credentials_from_options(
55+
self, username, password, usernames_file, passwords_file
56+
):
4957
usernames = []
5058
passwords = []
5159

@@ -69,11 +77,9 @@ def load_credentials_from_options(self, username, password, usernames_file, pass
6977
self.credentials = []
7078
for username in usernames:
7179
for password in passwords:
72-
self.credentials.append({
73-
"username": username,
74-
"password": password,
75-
"description": ""
76-
})
80+
self.credentials.append(
81+
{"username": username, "password": password, "description": ""}
82+
)
7783
return len(self.credentials)
7884

7985
# Get / Set functions
@@ -122,7 +128,7 @@ def get_debug_mode(self):
122128
return self.debug_mode
123129

124130
def set_debug_mode(self, value):
125-
if value == True:
131+
if value:
126132
self.verbose_mode = True
127133
self.debug_mode = value
128134

@@ -140,18 +146,22 @@ def set_request_proxies(self, proxy_ip, proxy_port, protocol=None):
140146
if protocol is None:
141147
self.request_proxies = {
142148
"http": "%s:%d" % (proxy_ip, proxy_port),
143-
"https": "%s:%d" % (proxy_ip, proxy_port)
149+
"https": "%s:%d" % (proxy_ip, proxy_port),
144150
}
145151
else:
146-
self.request_proxies[protocol] = "%s://%s:%d/" % (protocol, proxy_ip, proxy_port)
152+
self.request_proxies[protocol] = "%s://%s:%d/" % (
153+
protocol,
154+
proxy_ip,
155+
proxy_port,
156+
)
147157
return self.request_proxies
148158

149159
def clear_request_proxies(self):
150160
self.request_proxies = {}
151161

152162
def get_no_colors(self):
153163
return self.no_colors
154-
164+
155165
def set_no_colors(self, value):
156166
self.no_colors = value
157167

apachetomcatscanner/Reporter.py

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os.path
99
import sqlite3
1010
import traceback
11+
1112
import xlsxwriter
1213

1314

@@ -32,7 +33,11 @@ def report_result(self, computer_ip, computer_port, result, credentials_found):
3233
finding["computer_port"] = computer_port
3334
finding["credentials_found"] = credentials_found
3435

35-
finding["cves"] = self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(finding["version"], colors=False, reverse=True)
36+
finding["cves"] = (
37+
self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(
38+
finding["version"], colors=False, reverse=True
39+
)
40+
)
3641

3742
if computer_ip not in self.data.keys():
3843
self.data[computer_ip] = {}
@@ -49,7 +54,15 @@ def print_new_results(self):
4954
prompt = "[>] [Apache Tomcat/%s] on %s:%s (manager: accessible) on %s "
5055
else:
5156
prompt = "[>] [Apache Tomcat/\x1b[1;95m%s\x1b[0m] on \x1b[1;93m%s\x1b[0m:\x1b[1;93m%s\x1b[0m (manager: \x1b[1;92maccessible\x1b[0m) on \x1b[4;94m%s\x1b[0m "
52-
print(prompt % (finding["version"], finding["computer_ip"], finding["computer_port"], finding["manager_url"]))
57+
print(
58+
prompt
59+
% (
60+
finding["version"],
61+
finding["computer_ip"],
62+
finding["computer_port"],
63+
finding["manager_url"],
64+
)
65+
)
5366

5467
if len(finding["credentials_found"]) != 0:
5568
for statuscode, creds in finding["credentials_found"]:
@@ -58,7 +71,14 @@ def print_new_results(self):
5871
prompt = " | Valid user: %s | password: %s | %s"
5972
else:
6073
prompt = " | Valid user: \x1b[1;92m%s\x1b[0m | password: \x1b[1;92m%s\x1b[0m | \x1b[94m%s\x1b[0m"
61-
print(prompt % (creds["username"], creds["password"], creds["description"]))
74+
print(
75+
prompt
76+
% (
77+
creds["username"],
78+
creds["password"],
79+
creds["description"],
80+
)
81+
)
6282
else:
6383
if self.config.no_colors:
6484
prompt = " | Valid user: %s | password: %s"
@@ -68,19 +88,35 @@ def print_new_results(self):
6888

6989
else:
7090
if self.config.no_colors:
71-
prompt = "[>] [Apache Tomcat/%s] on %s:%s (manager: not accessible)"
91+
prompt = (
92+
"[>] [Apache Tomcat/%s] on %s:%s (manager: not accessible)"
93+
)
7294
else:
7395
prompt = "[>] [Apache Tomcat/\x1b[1;95m%s\x1b[0m] on \x1b[1;93m%s\x1b[0m:\x1b[1;93m%s\x1b[0m (manager: \x1b[1;91mnot accessible\x1b[0m)\x1b[0m "
74-
print(prompt % (finding["version"], finding["computer_ip"], finding["computer_port"]))
96+
print(
97+
prompt
98+
% (
99+
finding["version"],
100+
finding["computer_ip"],
101+
finding["computer_port"],
102+
)
103+
)
75104

76105
# List of cves
77-
if self.config.list_cves_mode == True and self.config.show_cves_descriptions_mode == False:
78-
cve_list = self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(finding["version"], colors=True, reverse=True)
106+
if (
107+
self.config.list_cves_mode
108+
and not self.config.show_cves_descriptions_mode
109+
):
110+
cve_list = self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(
111+
finding["version"], colors=True, reverse=True
112+
)
79113
cve_list = [cve_colored for cve_colored, cve_content in cve_list]
80114
if len(cve_list) != 0:
81-
print(" | CVEs: %s" % ', '.join(cve_list))
82-
elif self.config.show_cves_descriptions_mode == True:
83-
cve_list = self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(finding["version"], colors=True, reverse=True)
115+
print(" | CVEs: %s" % ", ".join(cve_list))
116+
elif self.config.show_cves_descriptions_mode:
117+
cve_list = self.vulns_db.get_vulnerabilities_of_version_sorted_by_criticity(
118+
finding["version"], colors=True, reverse=True
119+
)
84120
for cve_colored, cve_content in cve_list:
85121
print(" | %s: %s" % (cve_colored, cve_content["description"]))
86122

@@ -104,8 +140,15 @@ def export_xlsx(self, path_to_file):
104140
workbook = xlsxwriter.Workbook(path_to_file)
105141
worksheet = workbook.add_worksheet()
106142

107-
header_format = workbook.add_format({'bold': 1})
108-
header_fields = ["Computer IP", "Port", "Apache tomcat version", "Manager accessible", "Default credentials found", "CVEs on this version"]
143+
header_format = workbook.add_format({"bold": 1})
144+
header_fields = [
145+
"Computer IP",
146+
"Port",
147+
"Apache tomcat version",
148+
"Manager accessible",
149+
"Default credentials found",
150+
"CVEs on this version",
151+
]
109152
for k in range(len(header_fields)):
110153
worksheet.set_column(k, k + 1, len(header_fields[k]) + 3)
111154
worksheet.set_row(0, 20, header_format)
@@ -115,16 +158,18 @@ def export_xlsx(self, path_to_file):
115158
for computername in self.data.keys():
116159
computer = self.data[computername]
117160
for _, finding in computer.items():
118-
cve_str = ', '.join([cve["cve"]["id"] for cve in finding["cves"]])
119-
credentials_str = ', '.join([f"{cred[1]} ({cred[0]})" for cred in finding["credentials_found"]])
161+
cve_str = ", ".join([cve["cve"]["id"] for cve in finding["cves"]])
162+
credentials_str = ", ".join(
163+
[f"{cred[1]} ({cred[0]})" for cred in finding["credentials_found"]]
164+
)
120165

121166
data = [
122167
finding["computer_ip"],
123168
finding["computer_port"],
124169
finding["version"],
125170
str(finding["manager_accessible"]).upper(),
126171
credentials_str,
127-
cve_str
172+
cve_str,
128173
]
129174
worksheet.write_row(row_id, 0, data)
130175
row_id += 1
@@ -141,7 +186,7 @@ def export_json(self, path_to_file):
141186
path_to_file = basepath + os.path.sep + filename
142187
else:
143188
path_to_file = filename
144-
f = open(path_to_file, 'w')
189+
f = open(path_to_file, "w")
145190
f.write(json.dumps(self.data, indent=4))
146191
f.close()
147192

@@ -158,21 +203,27 @@ def export_sqlite(self, path_to_file):
158203

159204
conn = sqlite3.connect(path_to_file)
160205
cursor = conn.cursor()
161-
cursor.execute("CREATE TABLE IF NOT EXISTS results(computer_ip VARCHAR(255), computer_port INTEGER, version VARCHAR(255), manager_accessible VARCHAR(255), credentials_found VARCHAR(255), cves INTEGER);")
206+
cursor.execute(
207+
"CREATE TABLE IF NOT EXISTS results(computer_ip VARCHAR(255), computer_port INTEGER, version VARCHAR(255), manager_accessible VARCHAR(255), credentials_found VARCHAR(255), cves INTEGER);"
208+
)
162209
for computername in self.data.keys():
163210
computer = self.data[computername]
164211
for _, finding in computer.items():
165-
cve_str = ', '.join([cve["cve"]["id"] for cve in finding["cves"]])
166-
credentials_str = ', '.join([f"{cred[1]} ({cred[0]})" for cred in finding["credentials_found"]])
212+
cve_str = ", ".join([cve["cve"]["id"] for cve in finding["cves"]])
213+
credentials_str = ", ".join(
214+
[f"{cred[1]} ({cred[0]})" for cred in finding["credentials_found"]]
215+
)
167216

168-
cursor.execute("INSERT INTO results VALUES (?, ?, ?, ?, ?, ?)", (
217+
cursor.execute(
218+
"INSERT INTO results VALUES (?, ?, ?, ?, ?, ?)",
219+
(
169220
finding["computer_ip"],
170221
finding["computer_port"],
171222
finding["version"],
172223
str(finding["manager_accessible"]).upper(),
173224
credentials_str,
174-
cve_str
175-
)
225+
cve_str,
226+
),
176227
)
177228
conn.commit()
178229
conn.close()

0 commit comments

Comments
 (0)