Skip to content

Commit

Permalink
Merge pull request #41 from trailofbits/mschwager-blog-rules
Browse files Browse the repository at this point in the history
Add blog post Semgrep rules for generic, kotlin, and yaml
  • Loading branch information
mschwager committed Jan 16, 2024
2 parents 90561e1 + e73d0fd commit 92e7cb4
Show file tree
Hide file tree
Showing 62 changed files with 1,387 additions and 0 deletions.
13 changes: 13 additions & 0 deletions generic/container-privileged.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# ruleid: container-privileged
docker run --privileged hello-world

# ruleid: container-privileged
podman run --privileged hello-world

# ok: container-privileged
docker run hello-world

# ok: container-privileged
podman run hello-world
36 changes: 36 additions & 0 deletions generic/container-privileged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
rules:
- id: container-privileged
message: Found container command with extended privileges
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-250: Execution with Unnecessary Privileges"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://docs.docker.com/engine/reference/commandline/run/
pattern-either:
- pattern: docker ... --privileged
- pattern: docker ... --cap-add=ALL
- pattern: docker ... --cap-add=SYS_ADMIN
- pattern: docker ... --cap-add=SYS_MODULE
- pattern: docker ... --net=host
- pattern: docker ... --userns=host
- pattern: docker ... --pid=host
- pattern: docker ... --ipc=host
- pattern: docker ... --security-opt seccomp=unconfined
- pattern: docker ... --security-opt apparmor=unconfined
- pattern: podman ... --privileged
- pattern: podman ... --cap-add=ALL
- pattern: podman ... --cap-add=SYS_ADMIN
- pattern: podman ... --cap-add=SYS_MODULE
- pattern: podman ... --net=host
- pattern: podman ... --userns=host
- pattern: podman ... --pid=host
- pattern: podman ... --ipc=host
- pattern: podman ... --security-opt seccomp=unconfined
- pattern: podman ... --security-opt apparmor=unconfined
13 changes: 13 additions & 0 deletions generic/container-user-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# ruleid: container-user-root
docker run -u root hello-world

# ruleid: container-user-root
podman run --user root hello-world

# ok: container-user-root
docker run hello-world

# ok: container-user-root
podman run hello-world
24 changes: 24 additions & 0 deletions generic/container-user-root.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
rules:
- id: container-user-root
message: Found container command running as root
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-250: Execution with Unnecessary Privileges"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://docs.docker.com/engine/reference/commandline/run/
pattern-either:
- pattern: docker ... -u root
- pattern: docker ... -u 0
- pattern: docker ... --user root
- pattern: docker ... --user 0
- pattern: podman ... -u root
- pattern: podman ... --u 0
- pattern: podman ... --user root
- pattern: podman ... --user 0
13 changes: 13 additions & 0 deletions generic/curl-insecure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# ruleid: curl-insecure
curl -k https://google.com > /dev/null

# ruleid: curl-insecure
curl --insecure https://google.com > /dev/null

# ok: curl-insecure
curl --ksomeotherflag https://google.com > /dev/null

# ok: curl-insecure
curl https://google.com > /dev/null
22 changes: 22 additions & 0 deletions generic/curl-insecure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
rules:
- id: curl-insecure
message: Found `curl` command disabling SSL verification
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://curl.se/docs/manpage.html
pattern-either:
# A space character was left at the end of some patterns to help ensure
# that the intended flag was used, and minimize the chance that another,
# longer flag that _starts with_ the intended flag results in a false
# positive
- pattern: "curl ... -k "
- pattern: "curl ... --insecure"
10 changes: 10 additions & 0 deletions generic/curl-unencrypted-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# ruleid: curl-unencrypted-url
curl http://google.com > /dev/null

# ruleid: curl-unencrypted-url
curl ftp://google.com > /dev/null

# ok: curl-unencrypted-url
curl https://google.com > /dev/null
18 changes: 18 additions & 0 deletions generic/curl-unencrypted-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rules:
- id: curl-unencrypted-url
message: Found `curl` command with unencrypted URL (e.g. HTTP, FTP, etc.)
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://curl.se/docs/manpage.html
pattern-either:
- pattern: curl ... http://
- pattern: curl ... ftp://
7 changes: 7 additions & 0 deletions generic/gpg-insecure-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# ruleid: gpg-insecure-flags
gpg --skip-verify --output doc --decrypt doc.gpg

# ok: gpg-insecure-flags
gpg --output doc --decrypt doc.gpg
27 changes: 27 additions & 0 deletions generic/gpg-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rules:
- id: gpg-insecure-flags
message: Found `gpg` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://www.gnupg.org/gph/de/manual/r1023.html
pattern-either:
- pattern: gpg ... --allow-non-selfsigned-uid
- pattern: gpg ... --allow-freeform-uid
- pattern: gpg ... --allow-old-cipher-algos
- pattern: gpg ... --allow-weak-digest-algos
- pattern: gpg ... --allow-weak-key-signatures
- pattern: gpg ... --ignore-time-conflict
- pattern: gpg ... --ignore-valid-from
- pattern: gpg ... --ignore-crc-error
- pattern: gpg ... --ignore-mdc-error
- pattern: gpg ... --skip-verify
- pattern: gpg ... --no-require-cross-certification
7 changes: 7 additions & 0 deletions generic/installer-allow-untrusted.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# ruleid: installer-allow-untrusted
sudo installer -pkg /path/to/package.pkg -target / -allowUntrusted

# ok: installer-allow-untrusted
sudo installer -pkg /path/to/package.pkg -target /
16 changes: 16 additions & 0 deletions generic/installer-allow-untrusted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rules:
- id: installer-allow-untrusted
message: Found `installer` command allowing untrusted installations
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-494: Download of Code Without Integrity Check"
confidence: HIGH
likelihood: HIGH
impact: HIGH
references:
- https://ss64.com/mac/installer.html
pattern: installer ... -allowUntrusted
10 changes: 10 additions & 0 deletions generic/openssl-insecure-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# ruleid: openssl-insecure-flags
openssl genpkey -algorithm RSA -out private_key.pem -aes-256-cbc -pass pass:mysecretpass

# ok: openssl-insecure-flags
openssl genpkey -algorithm RSA -out private_key.pem --noencsomeotherflag

# ok: openssl-insecure-flags
openssl genpkey -algorithm RSA -out private_key.pem -aes-256-cbc -pass env:PASSVAR
26 changes: 26 additions & 0 deletions generic/openssl-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
- id: openssl-insecure-flags
message: Found `openssl` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://www.openssl.org/docs/manmaster/man1/
pattern-either:
# A space character was left at the end of some patterns to help ensure
# that the intended flag was used, and minimize the chance that another,
# longer flag that _starts with_ the intended flag results in a false
# positive
- pattern: "openssl ... -pass pass:"
- pattern: "openssl ... -passin pass:"
- pattern: "openssl ... -passout pass:"
- pattern: "openssl ... -nodes "
- pattern: "openssl ... -noenc "
- pattern: "openssl ... -sha1 "
7 changes: 7 additions & 0 deletions generic/ssh-disable-host-key-checking.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# ruleid: ssh-disable-host-key-checking
ssh -o StrictHostKeyChecking=no user@hostname

# ok: ssh-disable-host-key-checking
ssh user@hostname
16 changes: 16 additions & 0 deletions generic/ssh-disable-host-key-checking.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rules:
- id: ssh-disable-host-key-checking
message: Found `ssh` command disabling host key checking
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://man7.org/linux/man-pages/man1/ssh.1.html
pattern: ssh ... StrictHostKeyChecking=no
13 changes: 13 additions & 0 deletions generic/tar-insecure-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# ruleid: tar-insecure-flags
tar -xvf --absolute-paths archive.tar

# ruleid: tar-insecure-flags
tar -xvf -P archive.tar

# ok: tar-insecure-flags
tar -xvf --Psomeotherflag archive.tar

# ok: tar-insecure-flags
tar -xvf archive.tar
24 changes: 24 additions & 0 deletions generic/tar-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
rules:
- id: tar-insecure-flags
message: Found `tar` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-73: External Control of File Name or Path"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://man7.org/linux/man-pages/man1/tar.1.html
pattern-either:
# A space character was left at the end of some patterns to help ensure
# that the intended flag was used, and minimize the chance that another,
# longer flag that _starts with_ the intended flag results in a false
# positive
- pattern: "tar ... -P "
- pattern: "tar ... --absolute-paths"
- pattern: "tar ... --absolute-names"
- pattern: "tar ... --passphrase "
10 changes: 10 additions & 0 deletions generic/wget-no-check-certificate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# ruleid: wget-no-check-certificate
wget --no-check-certificate https://google.com

# ruleid: wget-no-check-certificate
wget --no-hsts https://google.com

# ok: wget-no-check-certificate
wget https://google.com
18 changes: 18 additions & 0 deletions generic/wget-no-check-certificate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rules:
- id: wget-no-check-certificate
message: Found `wget` command disabling SSL verification
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://linux.die.net/man/1/wget
pattern-either:
- pattern: wget ... --no-check-certificate
- pattern: wget ... --no-hsts
10 changes: 10 additions & 0 deletions generic/wget-unencrypted-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# ruleid: wget-unencrypted-url
wget http://google.com

# ruleid: wget-unencrypted-url
wget ftp://google.com

# ok: wget-unencrypted-url
wget https://google.com
18 changes: 18 additions & 0 deletions generic/wget-unencrypted-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rules:
- id: wget-unencrypted-url
message: Found `wget` command with unencrypted URL (e.g. HTTP, FTP, etc.)
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
technology: [shell]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
references:
- https://linux.die.net/man/1/wget
pattern-either:
- pattern: wget ... http://
- pattern: wget ... ftp://
6 changes: 6 additions & 0 deletions jvm/gc-call.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Test {
public static void main(String[] args) {
// ruleid: gc-call
System.gc();
}
}
4 changes: 4 additions & 0 deletions jvm/gc-call.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fun main() {
// ruleid: gc-call
System.gc()
}
Loading

0 comments on commit 92e7cb4

Please sign in to comment.