forked from noperator/panos-scanner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
panos-scanner.py
331 lines (275 loc) · 11.1 KB
/
panos-scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
"""
Developed with <3 by the Bishop Fox Continuous Attack Surface Testing (CAST) team.
https://www.bishopfox.com/continuous-attack-surface-testing/how-cast-works/
Author: @noperator
Purpose: Determine the software version of a remote PAN-OS target.
Notes: - Requires version-table.txt in the same directory.
- Usage of this tool for attacking targets without prior mutual
consent is illegal. It is the end user's responsibility to obey
all applicable local, state, and federal laws. Developers assume
no liability and are not responsible for any misuse or damage
caused by this program.
Usage: python3 panos-scanner.py [-h] [-v] [-s] -t TARGET
"""
import argparse
import datetime
import json
import logging
import requests
import requests.exceptions
import time
import urllib3
import urllib3.exceptions
import re
from urllib.parse import urlparse
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
verbose = False
# timeout value in seconds
default_timeout = 2
# proxies = {
# 'https': 'http://127.0.0.1:8080',
# }
# Set up logging.
logging.basicConfig(
format="%(asctime)s %(levelname)-8s [%(funcName)s] %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ",
)
logger = logging.getLogger(__name__)
if verbose:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.ERROR)
logging.Formatter.converter = time.gmtime
def etag_to_datetime(etag: str) -> datetime.date:
if etag.find('-'):
epoch_hex = etag.split('-', 1)[0]
else:
epoch_hex = etag[-8:]
try:
answer = datetime.datetime.fromtimestamp(int(epoch_hex, 16)).date()
toto = int(epoch_hex, 16)
except :
answer=""
return answer
def last_modified_to_datetime(last_modified: str) -> datetime.date:
return datetime.datetime.strptime(last_modified[:-4], "%a, %d %b %Y %X").date()
def get_resource(target: str, resource: str, date_headers: dict, errors: tuple) -> dict:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Connection": "close",
"Accept-Language": "en-US,en;q=0.5",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Upgrade-Insecure-Requests": "1",
}
logger.debug(resource)
try:
resp = requests.get(
"%s/%s" % (target, resource), headers=headers, timeout=default_timeout, verify=False
)
resp.raise_for_status()
return {
h: resp.headers[h].strip('"') for h in date_headers if h in resp.headers
}
except (requests.exceptions.HTTPError, requests.exceptions.ReadTimeout) as e:
logger.warning(type(e).__name__)
return None
except errors as e:
raise e
def load_version_table(version_table: str) -> dict:
with open(version_table, "r") as f:
entries = [line.strip().split() for line in f.readlines()]
return {
e[0]: datetime.datetime.strptime(" ".join(e[1:]), "%b %d %Y").date()
for e in entries
}
def check_date(version_table: dict, date: datetime.date) -> list:
matches = []
for n in [0, 1, -1, 2, -2]:
nearby_date = date + datetime.timedelta(n)
versions = [
version for version, date in version_table.items() if date == nearby_date
]
if not len(versions):
continue
precision = "exact" if n == 0 else "approximate"
append = True
for match in matches:
if match["precision"] == precision:
append = False
if append:
matches.append(
{
"date": nearby_date,
"versions": versions,
"precision": precision
}
)
if precision == 'approximate':
logger.debug(f"Appromixate version found for : {date.strftime('%d %b %Y')}")
return matches
def get_matches(date_headers: dict, resp_headers: dict, version_table: dict) -> list:
unmatched_date=""
matches = []
for header in date_headers.keys():
if header in resp_headers:
date = globals()[date_headers[header]](resp_headers[header])
if date != "":
matches.extend(check_date(version_table, date))
if len(matches) == 0 and 'date' in locals(): # if no matching but data return add as debug log
logger.debug(f"no matching for : {date.strftime('%b %d %Y')}")
unmatched_date = str(date.strftime('%b %d %Y'))
return matches,unmatched_date
def strip_url(fullurl: str) -> str:
"""
Extracts the host and port from a full URL and returns it.
Args:
fullurl (str): The full URL string.
Returns:
str: The host and port extracted from the URL.
"""
parsed_url = urlparse(fullurl)
# Combining the hostname and port if port is specified
if parsed_url.port:
return f"{parsed_url.hostname}:{parsed_url.port}"
else:
return parsed_url.hostname
def get_targets_from_file(inputfile: str):
"""
Read lines from the input file and return valid targets in the format 'https://1.2.3.4/' or 'https://1.2.3.4:8889/'.
Args:
inputfile (str): Path to the input file.
Returns:
list: List of valid targets in the format 'https://1.2.3.4/' or 'https://1.2.3.4:8889/'.
Raises:
ValueError: If any line in the file does not match the specified format.
IOError: If there's an error reading the input file.
"""
targets = []
try:
with open(inputfile, 'r') as file:
for line in file:
line = line.strip()
if re.match(r'^https://(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?::\d+)?/$', line):
targets.append(line)
else:
raise ValueError(f"Invalid format in line: {line}")
except IOError as e:
raise IOError(f"Error reading file: {e}")
return targets
def get_cve_link(results):
outputlink = "https://security.paloaltonetworks.com/?product=PAN-OS&sort=-cvss"
for match in results:
if match["precision"] == "exact":
outputlink += "&version=PAN-OS+" + match["versions"][0]
break
return outputlink
def main():
# Parse arguments.
parser = argparse.ArgumentParser(
description="""
Determine the software version of a remote PAN-OS target. Requires
version-table.txt in the same directory. See
https://security.paloaltonetworks.com/?product=PAN-OS for security
advisories for specific PAN-OS versions.
"""
)
parser.add_argument("-v", dest="verbose", action="store_true", help="verbose output")
parser.add_argument("-s", dest="stop", action="store_true", help="stop after one exact match")
parser.add_argument("-cve", dest="cve", action="store_true", help="Add link to official PAN security advisory page")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-t", dest="target", help="https://example.com")
group.add_argument("-f", dest="file", help="inputfile. One target per line. See target format")
args = parser.parse_args()
static_resources = [
"login/images/favicon.ico",
"global-protect/portal/images/bg.png",
"global-protect/portal/css/login.css",
"js/Pan.js",
"global-protect/portal/images/favicon.ico",
]
version_table = load_version_table("version-table.txt")
# The keys in "date_headers" represent HTTP response headers that we're
# looking for. Each of those headers maps to a function in this namespace
# that knows how to decode that header value into a datetime.
date_headers = {
"ETag": "etag_to_datetime",
"Last-Modified": "last_modified_to_datetime",
}
# These errors are indicative of target-level issues. Don't continue
# requesting other resources when encountering these; instead, bail.
target_errors = (
requests.exceptions.ConnectTimeout,
requests.exceptions.SSLError,
requests.exceptions.ConnectionError,
)
if args.file is not None:
targets_to_scan = get_targets_from_file(args.file)
else:
targets_to_scan = [args.target]
if args.verbose:
logger.setLevel(logging.DEBUG)
logger.debug(f"scanning : {len(targets_to_scan)} target(s)")
logger.debug(f"scanning target: {targets_to_scan}")
# Let's scan each target
for target_to_scan in targets_to_scan:
# A match is a dictionary containing a date/version pair per target.
total_matches = []
# Total of responses per target
total_responses = 0
# returned date Etag if unmatched
unknown_version = ""
# Check for the presence of each static resource.
for resource in static_resources:
try:
resp_headers = get_resource(
target_to_scan,
resource,
date_headers.keys(),
target_errors
)
except target_errors as e:
logger.error(f"could not connect to target: {type(e).__name__}")
continue
if resp_headers == None:
continue
if len(resp_headers) > 0 :
total_responses += len(resp_headers)
# Convert date-related HTTP headers to a standardized format, and
# store any matching version strings.
resource_matches,unknown_version = get_matches(date_headers, resp_headers, version_table)
for match in resource_matches:
match["resource"] = resource
total_matches.extend(resource_matches)
# Stop if we've got an exact match.
stop = False
if args.stop:
for match in resource_matches:
if match["precision"] == "exact":
stop = True
if stop:
continue
# Print results.
target_to_print = strip_url(target_to_scan)
try:
cve_link = get_cve_link(resource_matches)
except:
cve_link = ""
if args.cve and cve_link != "":
results = {"target": target_to_print, "match": {}, "all": total_matches, "cvelink": cve_link}
else:
results = {"target": target_to_print, "match": {}, "all": total_matches}
if total_responses == 0: # not a single answer
logger.error("Web service is up but no URL returned an answer. Are you sure it has GlobalProtect active ? ")
if not args.verbose:
logger.error("Try adding -v option for more verbosity")
continue
if not len(total_matches):
logger.error("no matching versions found for : " + target_to_scan)
results = {"target": target_to_print, "unmatch": {}, "etag": unknown_version}
else:
closest = sorted(total_matches, key=lambda x: x["precision"], reverse=True)[0]
results["match"] = closest
print(json.dumps(results, default=str))
if __name__ == "__main__":
main()