-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
unverified_script_exec
rule (#129)
* add unverified_script_exec rule * add safe patterns --------- Co-authored-by: Becojo <[email protected]>
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# METADATA | ||
# title: Unverified Script Execution | ||
# description: |- | ||
# The pipeline executes a script or binary fetched from a remote | ||
# server without verifying its integrity. | ||
# custom: | ||
# level: note | ||
package rules.unverified_script_exec | ||
|
||
import data.poutine | ||
import data.poutine.utils | ||
import rego.v1 | ||
|
||
rule := poutine.rule(rego.metadata.chain()) | ||
|
||
patterns.shell contains sprintf("(%s)", [concat("|", [ | ||
`(bash|source) <\(curl [^\)\n]+?\)`, | ||
`(curl|wget|iwr)[^\n]{0,256}(\|(|.*?[^a-z])((ba)?sh|python|php|node|iex|perl)|chmod ([aug]?\+x|[75]))`, | ||
`iex[^\n]{0,512}\.DownloadString\([^\)]+?\)`, | ||
`deno (run|install) (-A|--allow-all)[^\n]{0,128}https://[^\s]{0,128}`, | ||
])]) | ||
|
||
patterns.safe contains sprintf("(%s)", [concat("|", [ | ||
`https://raw\.githubusercontent\.com/[^/]+/[^/]+/[a-f0-9]{40}/`, | ||
`https://github\.com/[^/]+/[^/]+/raw/[a-f0-9]{40}/`, | ||
])]) | ||
|
||
results contains poutine.finding(rule, pkg_purl, _scripts[pkg_purl][_]) | ||
|
||
_unverified_scripts(script) = [sprintf("Command: %s", [match]) | | ||
match := regex.find_n(patterns.shell[_], script, -1)[_] | ||
not _is_safe(match) | ||
] | ||
|
||
_is_safe(match) = regex.match(patterns.safe[_], match) | ||
|
||
_scripts[pkg.purl] contains { | ||
"path": workflow.path, | ||
"step": step_id, | ||
"job": job.id, | ||
"line": step.lines.run, | ||
"details": details, | ||
} if { | ||
pkg := input.packages[_] | ||
workflow := pkg.github_actions_workflows[_] | ||
job := workflow.jobs[_] | ||
step := job.steps[step_id] | ||
details := _unverified_scripts(step.run)[_] | ||
} | ||
|
||
_scripts[pkg.purl] contains { | ||
"path": action.path, | ||
"step": step_id, | ||
"line": step.lines.run, | ||
"details": details, | ||
} if { | ||
pkg := input.packages[_] | ||
action := pkg.github_actions_metadata[_] | ||
step := action.runs.steps[step_id] | ||
details := _unverified_scripts(step.run)[_] | ||
} | ||
|
||
_scripts[pkg.purl] contains { | ||
"path": config.path, | ||
"line": script.line, | ||
"job": job.name, | ||
"details": details, | ||
} if { | ||
some attr in {"before_script", "after_script", "script"} | ||
pkg := input.packages[_] | ||
config := pkg.gitlabci_configs[_] | ||
job := array.concat(config.jobs, [config["default"]])[_] | ||
script := job[attr][_] | ||
details := _unverified_scripts(script.run)[_] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters