auditwright 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- auditwright/__init__.py +7 -0
- auditwright/__main__.py +5 -0
- auditwright/_util.py +55 -0
- auditwright/_version.py +1 -0
- auditwright/attack.py +224 -0
- auditwright/cli.py +521 -0
- auditwright/config.py +104 -0
- auditwright/cvss.py +49 -0
- auditwright/deps.py +532 -0
- auditwright/engine.py +108 -0
- auditwright/inventory.py +92 -0
- auditwright/model.py +181 -0
- auditwright/net.py +91 -0
- auditwright/netscan.py +532 -0
- auditwright/pentest.py +405 -0
- auditwright/py.typed +0 -0
- auditwright/report/__init__.py +1 -0
- auditwright/report/formats.py +220 -0
- auditwright/report/html.py +541 -0
- auditwright/rules/__init__.py +1 -0
- auditwright/rules/base.py +136 -0
- auditwright/rules/crypto.py +101 -0
- auditwright/rules/iac.py +572 -0
- auditwright/rules/patterns.py +266 -0
- auditwright/rules/python_ast.py +843 -0
- auditwright/rules/unicode.py +129 -0
- auditwright/secrets.py +216 -0
- auditwright/testgen.py +483 -0
- auditwright/testrun.py +257 -0
- auditwright/walk.py +255 -0
- auditwright-0.1.0.dist-info/METADATA +280 -0
- auditwright-0.1.0.dist-info/RECORD +36 -0
- auditwright-0.1.0.dist-info/WHEEL +4 -0
- auditwright-0.1.0.dist-info/entry_points.txt +2 -0
- auditwright-0.1.0.dist-info/licenses/LICENSE +202 -0
- auditwright-0.1.0.dist-info/licenses/NOTICE +5 -0
auditwright/rules/iac.py
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
"""CI, container and infrastructure-as-code checks.
|
|
2
|
+
|
|
3
|
+
Workflow and manifest files are analysed line by line with indentation-aware
|
|
4
|
+
context (enough to know which job, step or resource a line belongs to), which
|
|
5
|
+
keeps the analysis dependency-free and robust to YAML styles a strict parser
|
|
6
|
+
would reject.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from collections.abc import Iterator
|
|
13
|
+
|
|
14
|
+
from ..model import Finding, RuleMeta
|
|
15
|
+
from ..walk import SourceFile
|
|
16
|
+
from .base import PatternRule, pattern_rule, register, snippet, suppressed
|
|
17
|
+
|
|
18
|
+
A01 = "A01:2021 Broken Access Control"
|
|
19
|
+
A02 = "A02:2021 Cryptographic Failures"
|
|
20
|
+
A03 = "A03:2021 Injection"
|
|
21
|
+
A05 = "A05:2021 Security Misconfiguration"
|
|
22
|
+
A08 = "A08:2021 Software and Data Integrity Failures"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _r(id_, title, cat, sev, cwe, owasp, fix) -> RuleMeta:
|
|
26
|
+
return register(RuleMeta(id_, title, cat, sev, cwe, owasp, fix))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
GHA_INJECT = _r(
|
|
30
|
+
"AW-GHA-001",
|
|
31
|
+
"Script injection in a workflow",
|
|
32
|
+
"config",
|
|
33
|
+
"critical",
|
|
34
|
+
"CWE-78",
|
|
35
|
+
A03,
|
|
36
|
+
'Pass untrusted values through env: (FOO: ${{ github.event.issue.title }}) and use "$FOO" in the script.',
|
|
37
|
+
)
|
|
38
|
+
GHA_PRT = _r(
|
|
39
|
+
"AW-GHA-002",
|
|
40
|
+
"pull_request_target checks out untrusted code",
|
|
41
|
+
"config",
|
|
42
|
+
"critical",
|
|
43
|
+
"CWE-829",
|
|
44
|
+
A08,
|
|
45
|
+
"Do not check out the PR head in pull_request_target workflows, or split into an unprivileged pull_request job plus a workflow_run job.",
|
|
46
|
+
)
|
|
47
|
+
GHA_PIN = _r(
|
|
48
|
+
"AW-GHA-003",
|
|
49
|
+
"Third-party action not pinned to a commit SHA",
|
|
50
|
+
"supply-chain",
|
|
51
|
+
"medium",
|
|
52
|
+
"CWE-829",
|
|
53
|
+
A08,
|
|
54
|
+
"Pin to a full 40-character commit SHA (keep the version in a comment) and let Dependabot update it.",
|
|
55
|
+
)
|
|
56
|
+
GHA_PERM = _r(
|
|
57
|
+
"AW-GHA-004",
|
|
58
|
+
"Workflow token has write-all permissions",
|
|
59
|
+
"config",
|
|
60
|
+
"high",
|
|
61
|
+
"CWE-250",
|
|
62
|
+
A01,
|
|
63
|
+
"Set permissions: contents: read at the top level and grant extra scopes per job.",
|
|
64
|
+
)
|
|
65
|
+
GHA_NOPERM = _r(
|
|
66
|
+
"AW-GHA-005",
|
|
67
|
+
"Workflow without explicit token permissions",
|
|
68
|
+
"config",
|
|
69
|
+
"low",
|
|
70
|
+
"CWE-250",
|
|
71
|
+
A01,
|
|
72
|
+
"Add a top-level permissions: block (for example contents: read).",
|
|
73
|
+
)
|
|
74
|
+
GHA_SECRETS = _r(
|
|
75
|
+
"AW-GHA-006",
|
|
76
|
+
"Secret printed or passed on a command line",
|
|
77
|
+
"config",
|
|
78
|
+
"high",
|
|
79
|
+
"CWE-532",
|
|
80
|
+
A05,
|
|
81
|
+
"Pass secrets through env: and never echo them; command lines appear in process listings and logs.",
|
|
82
|
+
)
|
|
83
|
+
GHA_SELF = _r(
|
|
84
|
+
"AW-GHA-007",
|
|
85
|
+
"Self-hosted runner on a public trigger",
|
|
86
|
+
"config",
|
|
87
|
+
"high",
|
|
88
|
+
"CWE-250",
|
|
89
|
+
A05,
|
|
90
|
+
"Use GitHub-hosted runners for pull_request workflows, or isolate self-hosted runners per job.",
|
|
91
|
+
)
|
|
92
|
+
GHA_CREDS = _r(
|
|
93
|
+
"AW-GHA-008",
|
|
94
|
+
"Checkout persists credentials",
|
|
95
|
+
"config",
|
|
96
|
+
"low",
|
|
97
|
+
"CWE-522",
|
|
98
|
+
A05,
|
|
99
|
+
"Set persist-credentials: false on actions/checkout unless a later step must push.",
|
|
100
|
+
)
|
|
101
|
+
GHA_ARTIFACT_POISON = _r(
|
|
102
|
+
"AW-GHA-009",
|
|
103
|
+
"workflow_run trusts artifacts from untrusted runs",
|
|
104
|
+
"config",
|
|
105
|
+
"high",
|
|
106
|
+
"CWE-829",
|
|
107
|
+
A08,
|
|
108
|
+
"Treat downloaded artifacts as untrusted data: never execute them or write them into GITHUB_ENV.",
|
|
109
|
+
)
|
|
110
|
+
GHA_ENV = _r(
|
|
111
|
+
"AW-GHA-010",
|
|
112
|
+
"Untrusted data written to GITHUB_ENV or GITHUB_PATH",
|
|
113
|
+
"config",
|
|
114
|
+
"high",
|
|
115
|
+
"CWE-94",
|
|
116
|
+
A03,
|
|
117
|
+
"Validate values before writing them to GITHUB_ENV/GITHUB_PATH; attackers can set LD_PRELOAD or PATH.",
|
|
118
|
+
)
|
|
119
|
+
DOCKER_ROOT = _r(
|
|
120
|
+
"AW-DOCKER-001",
|
|
121
|
+
"Container runs as root",
|
|
122
|
+
"config",
|
|
123
|
+
"medium",
|
|
124
|
+
"CWE-250",
|
|
125
|
+
A05,
|
|
126
|
+
"Add a non-root USER before the final CMD/ENTRYPOINT.",
|
|
127
|
+
)
|
|
128
|
+
DOCKER_LATEST = _r(
|
|
129
|
+
"AW-DOCKER-002",
|
|
130
|
+
"Base image not pinned",
|
|
131
|
+
"supply-chain",
|
|
132
|
+
"medium",
|
|
133
|
+
"CWE-1357",
|
|
134
|
+
A08,
|
|
135
|
+
"Pin the base image to a version and digest: FROM image:1.2.3@sha256:...",
|
|
136
|
+
)
|
|
137
|
+
DOCKER_SECRET = _r(
|
|
138
|
+
"AW-DOCKER-003",
|
|
139
|
+
"Secret baked into an image layer",
|
|
140
|
+
"secrets",
|
|
141
|
+
"high",
|
|
142
|
+
"CWE-538",
|
|
143
|
+
A02,
|
|
144
|
+
"Use build secrets (RUN --mount=type=secret) or runtime environment variables; image layers are readable by anyone with the image.",
|
|
145
|
+
)
|
|
146
|
+
DOCKER_ADD = _r(
|
|
147
|
+
"AW-DOCKER-004",
|
|
148
|
+
"ADD downloads from a URL",
|
|
149
|
+
"supply-chain",
|
|
150
|
+
"medium",
|
|
151
|
+
"CWE-494",
|
|
152
|
+
A08,
|
|
153
|
+
"Use curl with checksum verification, or ADD --checksum=sha256:... (BuildKit).",
|
|
154
|
+
)
|
|
155
|
+
K8S_PRIV = _r(
|
|
156
|
+
"AW-K8S-001",
|
|
157
|
+
"Privileged container or host namespace",
|
|
158
|
+
"config",
|
|
159
|
+
"high",
|
|
160
|
+
"CWE-250",
|
|
161
|
+
A05,
|
|
162
|
+
"Remove privileged/hostNetwork/hostPID/hostIPC and add only the capabilities you need.",
|
|
163
|
+
)
|
|
164
|
+
K8S_SOCK = _r(
|
|
165
|
+
"AW-K8S-002",
|
|
166
|
+
"Container runtime socket mounted",
|
|
167
|
+
"config",
|
|
168
|
+
"critical",
|
|
169
|
+
"CWE-250",
|
|
170
|
+
A05,
|
|
171
|
+
"Never mount docker.sock or containerd.sock into workloads; it grants root on the host.",
|
|
172
|
+
)
|
|
173
|
+
K8S_ROOT = _r(
|
|
174
|
+
"AW-K8S-003",
|
|
175
|
+
"Container may run as root or escalate",
|
|
176
|
+
"config",
|
|
177
|
+
"medium",
|
|
178
|
+
"CWE-250",
|
|
179
|
+
A05,
|
|
180
|
+
"Set runAsNonRoot: true, allowPrivilegeEscalation: false and readOnlyRootFilesystem: true.",
|
|
181
|
+
)
|
|
182
|
+
K8S_HOSTPATH = _r(
|
|
183
|
+
"AW-K8S-004",
|
|
184
|
+
"hostPath volume",
|
|
185
|
+
"config",
|
|
186
|
+
"medium",
|
|
187
|
+
"CWE-668",
|
|
188
|
+
A05,
|
|
189
|
+
"Use persistent volume claims instead of host paths.",
|
|
190
|
+
)
|
|
191
|
+
TF_OPEN = _r(
|
|
192
|
+
"AW-TF-001",
|
|
193
|
+
"Network open to the whole internet",
|
|
194
|
+
"config",
|
|
195
|
+
"high",
|
|
196
|
+
"CWE-284",
|
|
197
|
+
A01,
|
|
198
|
+
"Restrict ingress CIDRs to known ranges; put admin ports behind a VPN or bastion.",
|
|
199
|
+
)
|
|
200
|
+
TF_PUBLIC = _r(
|
|
201
|
+
"AW-TF-002",
|
|
202
|
+
"Publicly readable storage",
|
|
203
|
+
"config",
|
|
204
|
+
"high",
|
|
205
|
+
"CWE-284",
|
|
206
|
+
A01,
|
|
207
|
+
"Make buckets private and serve public content through a CDN with an origin access identity.",
|
|
208
|
+
)
|
|
209
|
+
TF_ENC = _r(
|
|
210
|
+
"AW-TF-003",
|
|
211
|
+
"Encryption at rest disabled",
|
|
212
|
+
"config",
|
|
213
|
+
"medium",
|
|
214
|
+
"CWE-311",
|
|
215
|
+
A02,
|
|
216
|
+
"Enable encryption (storage_encrypted = true, encrypted = true, server-side encryption).",
|
|
217
|
+
)
|
|
218
|
+
TF_IAM = _r(
|
|
219
|
+
"AW-TF-004",
|
|
220
|
+
"IAM policy grants every action or resource",
|
|
221
|
+
"config",
|
|
222
|
+
"high",
|
|
223
|
+
"CWE-269",
|
|
224
|
+
A01,
|
|
225
|
+
"Scope IAM actions and resources to what the workload needs.",
|
|
226
|
+
)
|
|
227
|
+
TF_SECRET = _r(
|
|
228
|
+
"AW-TF-005",
|
|
229
|
+
"Secret value hard-coded in infrastructure code",
|
|
230
|
+
"secrets",
|
|
231
|
+
"high",
|
|
232
|
+
"CWE-798",
|
|
233
|
+
A02,
|
|
234
|
+
"Reference a secret manager or a sensitive variable supplied at apply time.",
|
|
235
|
+
)
|
|
236
|
+
NPMRC = _r(
|
|
237
|
+
"AW-CFG-001",
|
|
238
|
+
"Registry token committed in a config file",
|
|
239
|
+
"secrets",
|
|
240
|
+
"critical",
|
|
241
|
+
"CWE-798",
|
|
242
|
+
A02,
|
|
243
|
+
"Use ${NPM_TOKEN} style environment references and rotate the exposed token.",
|
|
244
|
+
)
|
|
245
|
+
NGINX_AUTOINDEX = _r(
|
|
246
|
+
"AW-CFG-002",
|
|
247
|
+
"Directory listing enabled",
|
|
248
|
+
"config",
|
|
249
|
+
"medium",
|
|
250
|
+
"CWE-548",
|
|
251
|
+
A05,
|
|
252
|
+
"Turn off autoindex / Options Indexes.",
|
|
253
|
+
)
|
|
254
|
+
TLS_OLD = _r("AW-CFG-003", "Obsolete TLS protocol enabled", "crypto", "medium", "CWE-326", A02,
|
|
255
|
+
"Allow TLS 1.2 and 1.3 only.") # fmt: skip
|
|
256
|
+
|
|
257
|
+
_UNTRUSTED_CTX = re.compile(
|
|
258
|
+
r"\$\{\{\s*(?:github\.event\.(?:issue\.(?:title|body)|pull_request\.(?:title|body|head\.ref|head\.label|head\.repo\.default_branch)|"
|
|
259
|
+
r"comment\.body|review\.body|review_comment\.body|pages\.[^}]*\.page_name|commits\.[^}]*\.(?:message|author\.(?:email|name))|"
|
|
260
|
+
r"head_commit\.(?:message|author\.(?:email|name))|workflow_run\.(?:head_branch|head_commit\.message|display_title)|discussion\.(?:title|body)|"
|
|
261
|
+
r"inputs\.[A-Za-z0-9_\-]+)|github\.head_ref|inputs\.[A-Za-z0-9_\-]+|steps\.[^}]*\.outputs\.[^}]*|needs\.[^}]*\.outputs\.[^}]*)\s*\}\}"
|
|
262
|
+
)
|
|
263
|
+
_USES = re.compile(r"^\s*-?\s*uses:\s*['\"]?([^'\"\s#]+)")
|
|
264
|
+
_TRIGGERS_PUBLIC = re.compile(r"\b(?:pull_request_target|pull_request|issue_comment|issues|discussion|workflow_run)\b")
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _f(meta: RuleMeta, f: SourceFile, line: int, msg: str, text: str, **kw) -> Finding | None:
|
|
268
|
+
if suppressed(text, meta.id):
|
|
269
|
+
return None
|
|
270
|
+
return Finding.from_rule(meta, msg, file=f.rel, line=line, snippet=snippet(text), **kw)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _is_workflow(f: SourceFile) -> bool:
|
|
274
|
+
return f.rel.replace("\\", "/").startswith(".github/workflows/") and f.path.suffix in (".yml", ".yaml")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def workflow_findings(f: SourceFile) -> Iterator[Finding]: # noqa: C901 - a single pass over the file
|
|
278
|
+
lines = f.lines()
|
|
279
|
+
text = f.text()
|
|
280
|
+
head = "\n".join(lines[:60])
|
|
281
|
+
on_prt = "pull_request_target" in head
|
|
282
|
+
on_workflow_run = re.search(r"^\s*workflow_run\s*:", head, re.M) is not None
|
|
283
|
+
public = bool(_TRIGGERS_PUBLIC.search(head))
|
|
284
|
+
in_run = False
|
|
285
|
+
run_indent = 0
|
|
286
|
+
top_permissions = re.search(r"^permissions\s*:", text, re.M) is not None
|
|
287
|
+
for i, line in enumerate(lines, 1):
|
|
288
|
+
stripped = line.strip()
|
|
289
|
+
if stripped.startswith("#"):
|
|
290
|
+
continue
|
|
291
|
+
indent = len(line) - len(line.lstrip())
|
|
292
|
+
if re.match(r"^\s*-?\s*run\s*:", line):
|
|
293
|
+
in_run = True
|
|
294
|
+
run_indent = indent
|
|
295
|
+
elif in_run and stripped and indent <= run_indent:
|
|
296
|
+
in_run = False
|
|
297
|
+
if (in_run or re.match(r"^\s*-?\s*script\s*:", line)) and _UNTRUSTED_CTX.search(line):
|
|
298
|
+
fd = _f(
|
|
299
|
+
GHA_INJECT,
|
|
300
|
+
f,
|
|
301
|
+
i,
|
|
302
|
+
"An attacker-controlled expression is expanded inside a shell script.",
|
|
303
|
+
line,
|
|
304
|
+
confidence="high",
|
|
305
|
+
)
|
|
306
|
+
if fd:
|
|
307
|
+
yield fd
|
|
308
|
+
if in_run and re.search(r"\$\{\{\s*secrets\.", line) and re.search(r"\b(?:echo|printf|cat|curl|wget)\b", line):
|
|
309
|
+
fd = _f(GHA_SECRETS, f, i, "A secret is expanded directly into a command line.", line)
|
|
310
|
+
if fd:
|
|
311
|
+
yield fd
|
|
312
|
+
if (
|
|
313
|
+
in_run
|
|
314
|
+
and re.search(r">>\s*\"?\$\{?GITHUB_(?:ENV|PATH)", line)
|
|
315
|
+
and (on_workflow_run or on_prt or _UNTRUSTED_CTX.search(line))
|
|
316
|
+
):
|
|
317
|
+
fd = _f(GHA_ENV, f, i, "Values from an untrusted run are written to GITHUB_ENV/GITHUB_PATH.", line)
|
|
318
|
+
if fd:
|
|
319
|
+
yield fd
|
|
320
|
+
m = _USES.match(line)
|
|
321
|
+
if m:
|
|
322
|
+
ref = m.group(1)
|
|
323
|
+
if not ref.startswith(("./", "docker://")) and "@" in ref:
|
|
324
|
+
owner = ref.split("/", 1)[0]
|
|
325
|
+
version = ref.rsplit("@", 1)[1]
|
|
326
|
+
if not re.fullmatch(r"[0-9a-f]{40}", version) and owner not in ("actions", "github"):
|
|
327
|
+
fd = _f(GHA_PIN, f, i, f"{ref} can change under the same tag.", line)
|
|
328
|
+
if fd:
|
|
329
|
+
yield fd
|
|
330
|
+
elif not re.fullmatch(r"[0-9a-f]{40}", version):
|
|
331
|
+
fd = _f(
|
|
332
|
+
GHA_PIN,
|
|
333
|
+
f,
|
|
334
|
+
i,
|
|
335
|
+
f"{ref} is referenced by a movable tag.",
|
|
336
|
+
line,
|
|
337
|
+
severity="low",
|
|
338
|
+
confidence="medium",
|
|
339
|
+
)
|
|
340
|
+
if fd:
|
|
341
|
+
yield fd
|
|
342
|
+
if ref.startswith("actions/checkout"):
|
|
343
|
+
window = "\n".join(lines[i : i + 8])
|
|
344
|
+
if on_prt and re.search(
|
|
345
|
+
r"ref:\s*\$\{\{\s*(?:github\.event\.pull_request\.head\.(?:sha|ref)|github\.head_ref)", window
|
|
346
|
+
):
|
|
347
|
+
fd = _f(
|
|
348
|
+
GHA_PRT,
|
|
349
|
+
f,
|
|
350
|
+
i,
|
|
351
|
+
"The PR's code is checked out with a privileged token and secrets available.",
|
|
352
|
+
line,
|
|
353
|
+
)
|
|
354
|
+
if fd:
|
|
355
|
+
yield fd
|
|
356
|
+
if "persist-credentials: false" not in window:
|
|
357
|
+
fd = _f(
|
|
358
|
+
GHA_CREDS,
|
|
359
|
+
f,
|
|
360
|
+
i,
|
|
361
|
+
"The workflow token stays in .git/config for every later step.",
|
|
362
|
+
line,
|
|
363
|
+
confidence="medium",
|
|
364
|
+
)
|
|
365
|
+
if fd:
|
|
366
|
+
yield fd
|
|
367
|
+
if on_workflow_run and ref.startswith(("actions/download-artifact", "dawidd6/action-download-artifact")):
|
|
368
|
+
fd = _f(GHA_ARTIFACT_POISON, f, i, "Artifacts from a triggering (possibly fork) run are downloaded.", line,
|
|
369
|
+
confidence="medium", severity="medium") # fmt: skip
|
|
370
|
+
if fd:
|
|
371
|
+
yield fd
|
|
372
|
+
if re.match(r"^\s*permissions\s*:\s*write-all\b", line):
|
|
373
|
+
fd = _f(GHA_PERM, f, i, "Every job gets a token that can write to the repository.", line)
|
|
374
|
+
if fd:
|
|
375
|
+
yield fd
|
|
376
|
+
if (
|
|
377
|
+
public
|
|
378
|
+
and re.match(r"^\s*runs-on\s*:.*self-hosted", line)
|
|
379
|
+
and ("pull_request" in head or "issue_comment" in head)
|
|
380
|
+
):
|
|
381
|
+
fd = _f(GHA_SELF, f, i, "Code from outside contributors can run on a persistent self-hosted runner.", line)
|
|
382
|
+
if fd:
|
|
383
|
+
yield fd
|
|
384
|
+
if not top_permissions and "jobs:" in text:
|
|
385
|
+
fd = _f(
|
|
386
|
+
GHA_NOPERM,
|
|
387
|
+
f,
|
|
388
|
+
1,
|
|
389
|
+
"The default token permissions apply (often read/write).",
|
|
390
|
+
lines[0] if lines else "",
|
|
391
|
+
confidence="medium",
|
|
392
|
+
)
|
|
393
|
+
if fd:
|
|
394
|
+
yield fd
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
_SECRET_ENV = re.compile(
|
|
398
|
+
r"(?i)^\s*(?:ENV|ARG)\s+([A-Z0-9_]*(?:PASSWORD|PASSWD|SECRET|TOKEN|API_?KEY|PRIVATE_KEY|ACCESS_KEY)[A-Z0-9_]*)(?:\s*=\s*|\s+)(\S+)?"
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def dockerfile_findings(f: SourceFile) -> Iterator[Finding]:
|
|
403
|
+
lines = f.lines()
|
|
404
|
+
user_set = False
|
|
405
|
+
last_from = 0
|
|
406
|
+
for i, line in enumerate(lines, 1):
|
|
407
|
+
s = line.strip()
|
|
408
|
+
up = s.upper()
|
|
409
|
+
if up.startswith("FROM "):
|
|
410
|
+
last_from = i
|
|
411
|
+
user_set = False
|
|
412
|
+
image = s.split()[1] if len(s.split()) > 1 else ""
|
|
413
|
+
if image.lower() != "scratch" and "@sha256:" not in image and not image.startswith("$"):
|
|
414
|
+
tag = image.rsplit(":", 1)[1] if ":" in image.rsplit("/", 1)[-1] else "latest"
|
|
415
|
+
sev = "medium" if tag == "latest" else "low"
|
|
416
|
+
fd = _f(DOCKER_LATEST, f, i, f"{image} is {'not tagged (latest)' if tag == 'latest' else 'not pinned by digest'}.", line,
|
|
417
|
+
severity=sev, confidence="high" if tag == "latest" else "medium") # fmt: skip
|
|
418
|
+
if fd:
|
|
419
|
+
yield fd
|
|
420
|
+
elif up.startswith("USER "):
|
|
421
|
+
user_set = s.split()[1] not in ("root", "0", "0:0")
|
|
422
|
+
m = _SECRET_ENV.match(s)
|
|
423
|
+
if m and m.group(2) and not m.group(2).startswith("$"):
|
|
424
|
+
fd = _f(DOCKER_SECRET, f, i, f"{m.group(1)} is set in the image.", line.replace(m.group(2), "****"))
|
|
425
|
+
if fd:
|
|
426
|
+
yield fd
|
|
427
|
+
if up.startswith("ADD ") and re.search(r"\bhttps?://", s) and "--checksum" not in s:
|
|
428
|
+
fd = _f(DOCKER_ADD, f, i, "Remote content is added without a checksum.", line)
|
|
429
|
+
if fd:
|
|
430
|
+
yield fd
|
|
431
|
+
if last_from and not user_set:
|
|
432
|
+
fd = _f(
|
|
433
|
+
DOCKER_ROOT,
|
|
434
|
+
f,
|
|
435
|
+
last_from,
|
|
436
|
+
"The final stage never switches to a non-root USER.",
|
|
437
|
+
lines[last_from - 1],
|
|
438
|
+
confidence="medium",
|
|
439
|
+
)
|
|
440
|
+
if fd:
|
|
441
|
+
yield fd
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def manifest_findings(f: SourceFile) -> Iterator[Finding]:
|
|
445
|
+
text = f.text()
|
|
446
|
+
k8s = bool(re.search(r"^\s*(?:apiVersion|kind)\s*:", text, re.M))
|
|
447
|
+
compose = f.path.name.startswith(("docker-compose", "compose")) or re.search(r"^services\s*:", text, re.M)
|
|
448
|
+
if not (k8s or compose):
|
|
449
|
+
return
|
|
450
|
+
for i, line in enumerate(f.lines(), 1):
|
|
451
|
+
s = line.strip()
|
|
452
|
+
if re.match(r"^(?:privileged|hostNetwork|hostPID|hostIPC)\s*:\s*true\b", s) or re.match(
|
|
453
|
+
r"^(?:network_mode|pid|ipc)\s*:\s*[\"']?host", s
|
|
454
|
+
):
|
|
455
|
+
fd = _f(K8S_PRIV, f, i, f"{s.split(':')[0]} gives the container host-level access.", line)
|
|
456
|
+
if fd:
|
|
457
|
+
yield fd
|
|
458
|
+
if re.search(r"(?:docker|containerd|crio|podman)\.sock", s):
|
|
459
|
+
fd = _f(K8S_SOCK, f, i, "The container runtime socket is exposed to the workload.", line)
|
|
460
|
+
if fd:
|
|
461
|
+
yield fd
|
|
462
|
+
if re.match(r"^(?:allowPrivilegeEscalation\s*:\s*true|runAsUser\s*:\s*0\b|runAsNonRoot\s*:\s*false)", s):
|
|
463
|
+
fd = _f(K8S_ROOT, f, i, "The pod security context allows root or privilege escalation.", line)
|
|
464
|
+
if fd:
|
|
465
|
+
yield fd
|
|
466
|
+
if k8s and re.match(r"^-?\s*hostPath\s*:", s):
|
|
467
|
+
fd = _f(K8S_HOSTPATH, f, i, "A host directory is mounted into the pod.", line, confidence="medium")
|
|
468
|
+
if fd:
|
|
469
|
+
yield fd
|
|
470
|
+
if re.match(r"^-?\s*(?:cap_add|add)\s*:\s*\[?.*\b(?:SYS_ADMIN|ALL|NET_ADMIN|SYS_PTRACE)\b", s):
|
|
471
|
+
fd = _f(K8S_PRIV, f, i, "Powerful Linux capabilities are added.", line, severity="medium")
|
|
472
|
+
if fd:
|
|
473
|
+
yield fd
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def terraform_findings(f: SourceFile) -> Iterator[Finding]:
|
|
477
|
+
for i, line in enumerate(f.lines(), 1):
|
|
478
|
+
s = line.strip()
|
|
479
|
+
if s.startswith(("#", "//")):
|
|
480
|
+
continue
|
|
481
|
+
if re.search(
|
|
482
|
+
r"(?:cidr_blocks|ipv6_cidr_blocks|source_ranges|source_address_prefix|cidr_ip)\s*=\s*\[?\s*\"(?:0\.0\.0\.0/0|::/0|\*)\"",
|
|
483
|
+
s,
|
|
484
|
+
):
|
|
485
|
+
fd = _f(TF_OPEN, f, i, "Ingress is allowed from any address.", line)
|
|
486
|
+
if fd:
|
|
487
|
+
yield fd
|
|
488
|
+
if re.search(
|
|
489
|
+
r"\bacl\s*=\s*\"public-read(?:-write)?\"|block_public_(?:acls|policy)\s*=\s*false|public_network_access_enabled\s*=\s*true|allow_blob_public_access\s*=\s*true",
|
|
490
|
+
s,
|
|
491
|
+
):
|
|
492
|
+
fd = _f(TF_PUBLIC, f, i, "Storage or a service is open to the public.", line)
|
|
493
|
+
if fd:
|
|
494
|
+
yield fd
|
|
495
|
+
if re.search(r"\b(?:storage_encrypted|encrypted|enable_encryption|encryption_enabled)\s*=\s*false", s):
|
|
496
|
+
fd = _f(TF_ENC, f, i, "Data is stored unencrypted.", line)
|
|
497
|
+
if fd:
|
|
498
|
+
yield fd
|
|
499
|
+
if re.search(
|
|
500
|
+
r"\"Action\"\s*:\s*\"\*\"|actions\s*=\s*\[\s*\"\*\"\s*\]|\"Resource\"\s*:\s*\"\*\"[^\n]*\"Effect\"\s*:\s*\"Allow\"",
|
|
501
|
+
s,
|
|
502
|
+
):
|
|
503
|
+
fd = _f(TF_IAM, f, i, "A policy allows every action or resource.", line, confidence="medium")
|
|
504
|
+
if fd:
|
|
505
|
+
yield fd
|
|
506
|
+
m = re.search(r"\b(?:password|secret|access_key|secret_key|token|api_key)\s*=\s*\"([^\"$]{6,})\"", s)
|
|
507
|
+
if m and not re.search(r"(?i)change|example|dummy|placeholder", m.group(1)):
|
|
508
|
+
fd = _f(
|
|
509
|
+
TF_SECRET, f, i, "A secret literal is set in infrastructure code.", line.replace(m.group(1), "****")
|
|
510
|
+
)
|
|
511
|
+
if fd:
|
|
512
|
+
yield fd
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def misc_findings(f: SourceFile) -> Iterator[Finding]:
|
|
516
|
+
name = f.path.name
|
|
517
|
+
if name in (
|
|
518
|
+
".npmrc",
|
|
519
|
+
".yarnrc",
|
|
520
|
+
".yarnrc.yml",
|
|
521
|
+
".pypirc",
|
|
522
|
+
"pip.conf",
|
|
523
|
+
".netrc",
|
|
524
|
+
".git-credentials",
|
|
525
|
+
".docker/config.json",
|
|
526
|
+
):
|
|
527
|
+
for i, line in enumerate(f.lines(), 1):
|
|
528
|
+
m = re.search(r"(?i)(?:_authToken|npmAuthToken|_auth|password|token)\s*[=:]\s*[\"']?([^\s\"'$]{8,})", line)
|
|
529
|
+
if m and not m.group(1).startswith("${"):
|
|
530
|
+
fd = _f(NPMRC, f, i, f"A registry credential is stored in {name}.", line.replace(m.group(1), "****"))
|
|
531
|
+
if fd:
|
|
532
|
+
yield fd
|
|
533
|
+
if name.endswith(".conf") or name in ("nginx.conf", "httpd.conf", ".htaccess", "apache2.conf"):
|
|
534
|
+
for i, line in enumerate(f.lines(), 1):
|
|
535
|
+
s = line.strip()
|
|
536
|
+
if re.match(r"^(?:autoindex\s+on|Options\s+[^#]*\+?Indexes)\b", s) and "-Indexes" not in s:
|
|
537
|
+
fd = _f(NGINX_AUTOINDEX, f, i, "The server lists directory contents.", line)
|
|
538
|
+
if fd:
|
|
539
|
+
yield fd
|
|
540
|
+
if (
|
|
541
|
+
re.match(r"^(?:ssl_protocols|SSLProtocol)\b", s)
|
|
542
|
+
and re.search(r"\b(?:SSLv2|SSLv3|TLSv1(?:\.1)?)(?!\.[23])\b", s)
|
|
543
|
+
and "-TLSv1" not in s
|
|
544
|
+
):
|
|
545
|
+
fd = _f(TLS_OLD, f, i, "SSLv3, TLS 1.0 or TLS 1.1 is enabled.", line)
|
|
546
|
+
if fd:
|
|
547
|
+
yield fd
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
PATTERNS: list[PatternRule] = [
|
|
551
|
+
pattern_rule("AW-CFG-004", "Django/Flask debug or insecure settings", "config", "medium", ("Python",),
|
|
552
|
+
r"^\s*(?:ALLOWED_HOSTS\s*=\s*\[\s*[\"']\*[\"']\s*\]|SESSION_COOKIE_SECURE\s*=\s*False|CSRF_COOKIE_SECURE\s*=\s*False|SECURE_SSL_REDIRECT\s*=\s*False)",
|
|
553
|
+
"A production security setting is disabled.", cwe="CWE-16", owasp=A05,
|
|
554
|
+
fix="Enable secure cookies and SSL redirect, and list allowed hosts explicitly.", files=r"settings"),
|
|
555
|
+
pattern_rule("AW-CFG-005", "CSRF protection disabled", "config", "high", ("Python", "JavaScript", "TypeScript", "Ruby", "PHP", "Java"),
|
|
556
|
+
r"@csrf_exempt|csrf\s*\(\s*\{\s*ignoreMethods|protect_from_forgery\s+with:\s*:null_session|skip_before_action\s+:verify_authenticity_token|\.csrf\(\)\.disable\(\)|WTF_CSRF_ENABLED\s*=\s*False", # auditwright: ignore
|
|
557
|
+
"Cross-site request forgery protection is turned off for at least one route.", cwe="CWE-352", owasp=A01,
|
|
558
|
+
fix="Keep CSRF protection on for cookie-authenticated, state-changing routes."),
|
|
559
|
+
] # fmt: skip
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def analyze(files: list[SourceFile]) -> Iterator[Finding]:
|
|
563
|
+
for f in files:
|
|
564
|
+
if _is_workflow(f):
|
|
565
|
+
yield from workflow_findings(f)
|
|
566
|
+
elif f.language == "Dockerfile":
|
|
567
|
+
yield from dockerfile_findings(f)
|
|
568
|
+
elif f.language == "YAML":
|
|
569
|
+
yield from manifest_findings(f)
|
|
570
|
+
elif f.language == "Terraform" or f.path.suffix == ".tfvars":
|
|
571
|
+
yield from terraform_findings(f)
|
|
572
|
+
yield from misc_findings(f)
|