-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_nextcloud_security.py
More file actions
executable file
·210 lines (168 loc) · 6.63 KB
/
check_nextcloud_security.py
File metadata and controls
executable file
·210 lines (168 loc) · 6.63 KB
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
#!/usr/bin/env python3
"""
Check a Nextcloud instance for known vulnerabilities using scan.nextcloud.com API.
Authors: Massoud Ahmed, Georg Schlagholz (IT-Native GmbH)
"""
import argparse
import logging
import re
import sys
from typing import Any, Dict, Optional
from dataclasses import dataclass, field
import requests
LOGGER = logging.getLogger("check_nextcloud")
SCAN_QUEUE_URL = "https://scan.nextcloud.com/api/queue"
SCAN_RESULT_URL = "https://scan.nextcloud.com/api/result"
SCAN_REQUEUE_URL = "https://scan.nextcloud.com/api/requeue"
@dataclass(frozen=True)
class ScanContext:
host: str
proxy: Optional[str] = None
debug: bool = False
rescan: bool = False
@dataclass(frozen=True)
class ScanRequestInfo:
headers: Dict[str, str] = field(default_factory=lambda: {
"Content-type": "application/x-www-form-urlencoded",
"X-CSRF": "true",
})
data: Dict[str, str] = field(default_factory=dict)
proxies: Optional[Dict[str, str]] = None
uuid: Optional[str] = None
@dataclass
class ScanResult:
response: Dict[str, Any]
uuid: str
# --- Utility Functions ---
def check_if_ip_or_host(host: str) -> None:
"""Exit if host is an IP address (not supported by the API)."""
if re.match(r"^\d{1,3}(\.\d{1,3}){3}$", host):
print("IP addresses are not supported by the Scan API.")
sys.exit(3)
def send_scan_request(context: ScanContext) -> ScanResult:
"""Send initial security check request to the Nextcloud Scan Server."""
request_info = ScanRequestInfo(
data={"url": context.host},
proxies={"http": context.proxy, "https": context.proxy} if context.proxy else None
)
LOGGER.debug("Initiating scan for host: %s", context.host)
if context.proxy:
LOGGER.debug("Using proxy: %s", context.proxy)
try:
response = requests.post(
SCAN_QUEUE_URL,
headers=request_info.headers,
data=request_info.data,
proxies=request_info.proxies,
timeout=10,
)
response.raise_for_status()
answer = response.json()
except Exception as e:
print(
f"UNKNOWN: {context.host} Scan failed! Either no Nextcloud/ownCloud found "
f"or too many scans queued: {e}"
)
sys.exit(3)
LOGGER.debug("Response from scan.nextcloud.com: %s", answer)
if isinstance(answer, str) and "Too many instances" in answer:
print(f"UNKNOWN: {context.host} Scan failed! Reason: {answer}")
sys.exit(3)
uuid: Optional[str] = answer.get("uuid")
if not uuid:
print(f"UNKNOWN: Failed to retrieve scan UUID for {context.host}.")
sys.exit(3)
try:
response_scan = requests.get(
f"{SCAN_RESULT_URL}/{uuid}", proxies=request_info.proxies, timeout=10
).json()
except Exception as e:
print(f"UNKNOWN: Could not retrieve scan results for {context.host}: {e}")
sys.exit(3)
return ScanResult(response=response_scan, uuid=uuid)
def check_vulnerabilities(context: ScanContext, scan_result: ScanResult) -> None:
"""Check the Nextcloud instance for known vulnerabilities and print the result."""
request_info = ScanRequestInfo(
data={"url": context.host},
proxies={"http": context.proxy, "https": context.proxy} if context.proxy else None
)
uuid_url = f"{SCAN_RESULT_URL}/{scan_result.uuid}"
response_scan = scan_result.response
if context.rescan:
LOGGER.debug("Triggering rescan for %s", scan_result.uuid)
try:
requests.post(
SCAN_REQUEUE_URL,
headers=request_info.headers,
data=request_info.data,
proxies=request_info.proxies,
timeout=10
)
response_scan = requests.get(uuid_url, proxies=request_info.proxies, timeout=10).json()
except Exception as e:
print(f"UNKNOWN: Failed to rescan {scan_result.uuid}: {e}")
sys.exit(3)
rating: int = response_scan.get("rating", -1)
product: str = response_scan.get("product", "Unknown")
version: str = response_scan.get("version", "Unknown")
domain: str = response_scan.get("domain", "Unknown")
scan_date: str = response_scan.get("scannedAt", {}).get("date", "Unknown")
rate_map: Dict[int, str] = {5: "A+", 4: "A", 3: "C", 2: "D", 1: "E", 0: "F"}
rate: str = rate_map.get(rating, "Unknown")
vulnerabilities: list[Dict[str, Any]] = response_scan.get("vulnerabilities", [])
num_vulns: int = len(vulnerabilities)
msg: str = "UNKNOWN: Scan result unclear. Please verify manually."
exit_code: int = 3
if rating in {5, 4} and num_vulns == 0:
msg = (
"OK: Server is up to date. No known vulnerabilities."
if rating == 5
else "OK: Update available, but no known vulnerabilities."
)
exit_code = 0
elif num_vulns > 0:
severity_map = {1: "high", 2: "medium", 3: "low"}
severity = severity_map.get(rating, "unknown")
if rating <= 1:
msg = f"CRITICAL: Found {num_vulns} vulnerabilities (at least one {severity})."
exit_code = 2
elif rating <= 3:
msg = f"WARNING: Found {num_vulns} vulnerabilities (at least one {severity})."
exit_code = 1
elif rating == 0:
msg = "CRITICAL: This server version is end-of-life and has no security fixes."
exit_code = 2
print(f"{msg}\n{product} {version} on {domain}, rating: {rate}, last scanned: {scan_date}")
sys.exit(exit_code)
# --- Main ---
def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug mode.")
parser.add_argument("-H", "--host", required=True, help="Nextcloud server address.")
parser.add_argument("-P", "--proxy", default=None, help="Proxy server address.")
parser.add_argument(
"-r",
"--rescan",
action="store_true",
default=False,
help="Trigger rescan on every check. Default: False.",
)
args = parser.parse_args()
context = ScanContext(
host=args.host,
proxy=args.proxy,
debug=args.debug,
rescan=args.rescan
)
logging.basicConfig(
level=logging.DEBUG if context.debug else logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
LOGGER.debug("Starting scan for host: %s", context.host)
check_if_ip_or_host(context.host)
scan_result = send_scan_request(context)
check_vulnerabilities(context, scan_result)
if __name__ == "__main__":
main()