Skip to content

Commit

Permalink
Add blog post Semgrep rules for generic, kotlin, and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwager committed Jan 12, 2024
1 parent 04b8335 commit 1cee223
Show file tree
Hide file tree
Showing 62 changed files with 1,321 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
27 changes: 27 additions & 0 deletions generic/container-privileged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rules:
- id: container-privileged
message: Found container command with extended privileges
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-250: Execution with Unnecessary Privileges"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
pattern-either:
- pattern: docker ... --privileged
- pattern: docker ... --cap-add=ALL
- pattern: docker ... --net=host
- pattern: docker ... --userns=host
- pattern: docker ... --pid=host
- pattern: docker ... --ipc=host
- pattern: docker ... --security-opt seccomp=unconfined
- pattern: podman ... --privileged
- pattern: podman ... --cap-add=ALL
- pattern: podman ... --net=host
- pattern: podman ... --userns=host
- pattern: podman ... --pid=host
- pattern: podman ... --ipc=host
- pattern: podman ... --security-opt seccomp=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
17 changes: 17 additions & 0 deletions generic/container-user-root.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
rules:
- id: container-user-root
message: Found container command running as root
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-250: Execution with Unnecessary Privileges"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
pattern-either:
- pattern: docker ... -u root
- pattern: docker ... --user root
- pattern: podman ... -u root
- pattern: podman ... --user root
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
19 changes: 19 additions & 0 deletions generic/curl-insecure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rules:
- id: curl-insecure
message: Found `curl` command disabling SSL verification
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
15 changes: 15 additions & 0 deletions generic/curl-unencrypted-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
24 changes: 24 additions & 0 deletions generic/gpg-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
rules:
- id: gpg-insecure-flags
message: Found `gpg` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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 /
13 changes: 13 additions & 0 deletions generic/installer-allow-untrusted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rules:
- id: installer-allow-untrusted
message: Found `installer` command allowing untrusted installations
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-494: Download of Code Without Integrity Check"
confidence: HIGH
likelihood: HIGH
impact: HIGH
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
23 changes: 23 additions & 0 deletions generic/openssl-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
rules:
- id: openssl-insecure-flags
message: Found `openssl` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
13 changes: 13 additions & 0 deletions generic/ssh-disable-host-key-checking.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rules:
- id: ssh-disable-host-key-checking
message: Found `ssh` command disabling host key checking
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
21 changes: 21 additions & 0 deletions generic/tar-insecure-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
rules:
- id: tar-insecure-flags
message: Found `tar` command using insecure flags
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-73: External Control of File Name or Path"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
15 changes: 15 additions & 0 deletions generic/wget-no-check-certificate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rules:
- id: wget-no-check-certificate
message: Found `wget` command disabling SSL verification
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
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
15 changes: 15 additions & 0 deletions generic/wget-unencrypted-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: MEDIUM
likelihood: MEDIUM
impact: HIGH
pattern-either:
- pattern: wget ... http://
- pattern: wget ... ftp://
6 changes: 6 additions & 0 deletions kotlin/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 kotlin/gc-call.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fun main() {
// ruleid: gc-call
System.gc()
}
20 changes: 20 additions & 0 deletions kotlin/gc-call.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
rules:
- id: gc-call
message: |
Calling `gc` suggests to the JVM that the garbage collector should be
run, and memory should be reclaimed. This is only a suggestion, and there
is no guarantee that anything will happen. Relying on this behavior for
correctness or memory management is an anti-pattern.
languages: [java, kotlin]
severity: WARNING
metadata:
category: best-practice
subcategory: [audit]
confidence: HIGH
likelihood: HIGH
impact: LOW
references:
- https://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc
pattern-either:
- pattern: System.gc()
- pattern: Runtime.getRuntime().gc()
23 changes: 23 additions & 0 deletions kotlin/mongo-hostname-verification-disabled.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test

import com.mongodb.MongoClientSettings
import com.mongodb.MongoClients

class HelloWorld {
public static void main(String[] args) {
MongoClientSettings settings = MongoClientSettings.builder()
.applyToSslSettings(builder -> {
builder.enabled(true);
// ruleid: mongo-hostname-verification-disabled
builder.invalidHostNameAllowed(true);
})
.build();

MongoClientSettings settings = MongoClientSettings.builder()
.applyToSslSettings(builder -> {
// ok: mongo-hostname-verification-disabled
builder.enabled(true);
})
.build();
}
}
Loading

0 comments on commit 1cee223

Please sign in to comment.