Skip to content

Commit

Permalink
Merge pull request #69 from trailofbits/mschwager-35-more-rules
Browse files Browse the repository at this point in the history
Add rules for '35 more Semgrep rules' blog post
  • Loading branch information
mschwager authored Dec 5, 2024
2 parents 828ee71 + 7aac75f commit 71b9ec1
Show file tree
Hide file tree
Showing 70 changed files with 2,263 additions and 0 deletions.
7 changes: 7 additions & 0 deletions generic/amqp-unencrypted-transport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# ruleid: amqp-unencrypted-transport
echo "Hello, World!" | amqp-publish --url=amqp://guest:[email protected]:5672 --routing-key=test_queue

# ok: amqp-unencrypted-transport
echo "Hello, World!" | amqp-publish --url=amqps://guest:[email protected]:5672 --routing-key=test_queue
19 changes: 19 additions & 0 deletions generic/amqp-unencrypted-transport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rules:
- id: amqp-unencrypted-transport
message: |
Found unencrypted AMQP connection, prefer TLS encrypted `amqps://` transport
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [amqp, rabbitmq]
references:
- https://www.rabbitmq.com/docs/uri-spec#the-amqps-uri-scheme
options:
generic_ellipsis_max_span: 0
pattern: amqp://...
53 changes: 53 additions & 0 deletions generic/mongodb-insecure-transport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/"

# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true&tlsAllowInvalidCertificates=true"

# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true&tlsAllowInvalidCertificates=true&something=else"

# we want to be lenient here
# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true&tlsAllowInvalidCertificates=truebutbug"

# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true&tlsAllowInvalidCertificates=truebutbug&something=else"

# we want to be strict here
# ruleid: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=truebutbug"

# ruleid: mongodb-insecure-transport
mongo 'mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=truebutbug&something=else'

# ok: mongodb-insecure-transport
mongo 'mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true'

# ok: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?tls=true&something=else"

# ok: mongodb-insecure-transport
mongo "mongodb://user:[email protected],db1.example.com,db2.example.com/?ssl=true"

echo '
# ok: mongodb-insecure-transport
mongodb://user:[email protected],db1.example.com,db2.example.com/?ssl=true
'

echo '
# ruleid: mongodb-insecure-transport
mongodb://user:[email protected],db1.example.com,db2.example.com/?ssl=truebutbug
'

echo '
# ok: mongodb-insecure-transport
mongodb://user:[email protected],db1.example.com,db2.example.com/?ssl=true&something=else
'

echo '
# ruleid: mongodb-insecure-transport
mongodb://user:[email protected],db1.example.com,db2.example.com/?ssl=truebutbug&something=else
'
25 changes: 25 additions & 0 deletions generic/mongodb-insecure-transport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
rules:
- id: mongodb-insecure-transport
message: |
Found insecure MongoDB connection, prefer TLS encrypted transport by
setting the `tls=true` connection option and ensuring proper verification
languages: [regex]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [mongodb]
references:
- https://www.mongodb.com/docs/manual/reference/connection-string/#connection-options
pattern-either:
- patterns:
- pattern-regex: (?i)mongodb://.+$
- pattern-not-regex: (?i)(?:(.)?)?mongodb://.+[?&]tls=true(?:\g{1}|$|&).*
- pattern-not-regex: (?i)(?:(.)?)?mongodb://.+[?&]ssl=true(?:\g{1}|$|&).*
- pattern-regex: (?i)mongodb://.+[?&]tlsAllowInvalidCertificates=true.*$
- pattern-regex: (?i)mongodb://.+[?&]tlsAllowInvalidHostnames=true.*$
- pattern-regex: (?i)mongodb://.+[?&]tlsInsecure=true.*$
10 changes: 10 additions & 0 deletions generic/mysql-insecure-sslmode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# ruleid: mysql-insecure-sslmode
mysql mysql://myapplicationuser:mypass@myhost:1234/applicationdb?SslMode=Disabled

# ruleid: mysql-insecure-sslmode
mysql mysql://myapplicationuser:mypass@myhost:1234/applicationdb?useSSL=false

# ok: mysql-insecure-sslmode
mysql mysql://myapplicationuser:mypass@myhost:1234/applicationdb?SslMode=Required
21 changes: 21 additions & 0 deletions generic/mysql-insecure-sslmode.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
rules:
- id: mysql-insecure-sslmode
message: |
Found MySQL connection string disabling SSL verification
languages: [regex]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: MEDIUM
likelihood: HIGH
impact: HIGH
technology: [mysql]
references:
- https://dev.mysql.com/doc/connector-net/en/connector-net-8-0-connection-options.html
- https://dev.mysql.com/doc/connector-j/en/connector-j-connp-props-security.html
pattern-either:
- pattern-regex: "(?i)Ssl[ -]?Mode=(Disabled|None|Preferred)"
- pattern-regex: "(?i)sslMode=(DISABLED|PREFERRED)"
- pattern-regex: "(?i)useSSL=false"
12 changes: 12 additions & 0 deletions generic/node-disable-certificate-validation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# ruleid: node-disable-certificate-validation
export NODE_TLS_REJECT_UNAUTHORIZED=0

# ok: node-disable-certificate-validation
export NODE_TLS_REJECT_UNAUTHORIZED=1

# ok: node-disable-certificate-validation
export NODE_TLS_REJECT_UNAUTHORIZED=false

node app.js
22 changes: 22 additions & 0 deletions generic/node-disable-certificate-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
rules:
- id: node-disable-certificate-validation
message: |
Setting this environment variable disables TLS certificate validation.
This makes TLS, and HTTPS by extension, insecure. The use of this
environment variable is strongly discouraged.
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [nodejs]
references:
- https://nodejs.org/api/cli.html#node_tls_reject_unauthorizedvalue
pattern-either:
- pattern: NODE_TLS_REJECT_UNAUTHORIZED=0
- pattern: NODE_TLS_REJECT_UNAUTHORIZED='0'
- pattern: NODE_TLS_REJECT_UNAUTHORIZED="0"
49 changes: 49 additions & 0 deletions generic/postgres-insecure-sslmode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# ruleid: postgres-insecure-sslmode
psql postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=disable

# ruleid: postgres-insecure-sslmode
psql postgres://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=disable

# ruleid: postgres-insecure-sslmode
psql postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=PREfered

# "This option is deprecated in favor of the sslmode setting."
# ruleid: postgres-insecure-sslmode
psql postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?requiressl=0

# ruleid: postgres-insecure-sslmode
psql "postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?ssl=false"

# ok: postgres-insecure-sslmode
psql postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=require

# ok: postgres-insecure-sslmode
psql "postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=verify-full&something=else"

# ok: postgres-insecure-sslmode
psql "postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=require&something=else"

# ok: postgres-insecure-sslmode
psql 'postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=require&something=else'

echo '
# ok: postgres-insecure-sslmode
postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=require&something=else
'

echo '
# ok: postgres-insecure-sslmode
postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?sslmode=require
'

# "for compatibility with JDBC connection URIs, instances of ssl=true are translated into sslmode=require."
# ok: postgres-insecure-sslmode
psql "postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?ssl=true"

# ok: postgres-insecure-sslmode
psql "postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?ssl=true&something=else"

# ok: postgres-insecure-sslmode
psql 'postgresql://myapplicationuser:mypass@myhost:1234/applicationdb?requiressl=1'
23 changes: 23 additions & 0 deletions generic/postgres-insecure-sslmode.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
rules:
- id: postgres-insecure-sslmode
message: |
Found PostgreSQL connection string disabling SSL verification
languages: [regex]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-295: Improper Certificate Validation"
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [postgresql]
references:
- https://www.postgresql.org/docs/current/libpq-connect.html
pattern-either:
- pattern-regex: (?i)postgresql://.+[?&]sslmode=(disable|allow|prefer).*$
- pattern-regex: (?i)postgresql://.+[?&]requiressl=0.*$
- pattern-regex: (?i)postgresql://.+[?&]ssl=false.*$
- pattern-regex: (?i)postgres://.+[?&]sslmode=(disable|allow|prefer).*$
- pattern-regex: (?i)postgres://.+[?&]requiressl=0.*$
- pattern-regex: (?i)postgres://.+[?&]ssl=false.*$
7 changes: 7 additions & 0 deletions generic/redis-unencrypted-transport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# ruleid: redis-unencrypted-transport
redis-cli -u redis://user:password@host:port/dbnum PING

# ok: redis-unencrypted-transport
redis-cli -u rediss://user:password@host:port/dbnum PING
19 changes: 19 additions & 0 deletions generic/redis-unencrypted-transport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rules:
- id: redis-unencrypted-transport
message: |
Found unencrypted Redis connection, prefer TLS encrypted `rediss://` transport
languages: [generic]
severity: WARNING
metadata:
category: security
subcategory: [audit]
cwe: "CWE-319: Cleartext Transmission of Sensitive Information"
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [redis]
references:
- https://redis.io/docs/latest/develop/connect/cli/#host-port-password-and-database
options:
generic_ellipsis_max_span: 0
pattern: redis://...
44 changes: 44 additions & 0 deletions hcl/nomad/docker-hardcoded-password.job
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
task "example" {
driver = "docker"

config {
image = "secret/service"

auth {
username = "dockerhub_user"

# ruleid: docker-hardcoded-password
password = "dockerhub_password"
}
}
}

task "example" {
driver = "docker"

config {
image = "secret/service"

auth {
username = "dockerhub_user"

# ok: docker-hardcoded-password
password = "${PASSWORD}"
}
}
}

task "example" {
driver = "podman"

config {
image = "secret/service"

auth {
username = "dockerhub_user"

# ruleid: docker-hardcoded-password
password = "dockerhub_password"
}
}
}
35 changes: 35 additions & 0 deletions hcl/nomad/docker-hardcoded-password.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
rules:
- id: docker-hardcoded-password
message: |
Found Nomad task using Docker auth with hardcoded password
languages: [hcl]
severity: WARNING
metadata:
category: security
cwe: "CWE-798: Use of Hard-coded Credentials"
subcategory: [audit]
confidence: HIGH
likelihood: HIGH
impact: HIGH
technology: [nomad, docker, podman]
references:
- https://developer.hashicorp.com/nomad/docs/drivers/docker#password
patterns:
- pattern-inside: |
task "..." {
...
driver = "$RUNTIME"
...
config {
...
auth {
...
}
...
}
...
}
- pattern: password = "..."
- metavariable-regex:
metavariable: $RUNTIME
regex: (docker|podman)
Loading

0 comments on commit 71b9ec1

Please sign in to comment.