Get started
Cheatsheet

DevSecOps

Find and remove secrets, tighten permissions and sudo, harden SSH and Dockerfiles, and check certificates, checksums and signatures.

Practice this path

Secrets in git

  • git grep -nIiE 'password|secret|api_key|token'

    Search the tracked files for likely secrets

  • git log -p -S DB_PASSWORD

    Every commit that added or removed a string, with its diff

  • git grep DB_PASSWORD $(git rev-list --all)

    Search every commit, not just the current one

  • git rm --cached .env  &&  echo .env >> .gitignore

    Stop tracking a file but keep it on disk

  • git check-ignore -v .env

    Which ignore rule matches a file

  • FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch .env' -- --all

    Remove a file from every commit

  • git update-ref -d refs/original/refs/heads/main

    Delete the backup ref a rewrite leaves behind

  • git reflog expire --expire=now --all  &&  git gc --prune=now

    Forget old commits and drop their objects

  • git diff --cached -U0 | grep -iE '^\+.*(password|secret|api_key)'

    Test the staged changes for secrets (the core of a pre-commit hook)

Permissions and accounts

  • find . -perm -o=w

    Files and folders anyone can write to

  • chmod -R o-w app

    Take write access away from others, keep the rest

  • chmod 600 secrets.env  |  chmod 750 config

    Owner-only file; folder closed to others

  • stat -c '%a %U:%G %n' file

    Mode, owner and group of a file

  • sudo useradd --system --shell /usr/sbin/nologin webapp

    A service account nobody can log in as

  • sudo chown -R webapp:webapp app/logs

    Hand a folder and its contents to an account

  • getent passwd webapp  |  id -nG deploy

    Look up an account and its groups

sudo rules

  • deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart webapp

    One command, as root, for one user

  • echo 'RULE' | sudo tee /etc/sudoers.d/deploy

    Write a root-owned rule file

  • sudo chmod 440 /etc/sudoers.d/deploy

    The mode sudo expects for a rule file

  • sudo visudo -cf /etc/sudoers.d/deploy

    Check a rule file's syntax before you log out

  • sudo -l -U deploy

    What a user may run with sudo

SSH server

sshd uses the first value of each keyword; lines after Match apply only to matching connections.

  • PermitRootLogin no

    Nobody logs in as root

  • PasswordAuthentication no

    Keys only (also KbdInteractiveAuthentication no, PermitEmptyPasswords no)

  • AllowUsers deploy ops

    Only these accounts may log in

  • MaxAuthTries 3  |  LoginGraceTime 30

    Fewer guesses, less time to authenticate

  • X11Forwarding no  |  AllowAgentForwarding no

    Turn off forwarding nobody uses

  • LogLevel VERBOSE

    Log the fingerprint of the key used for each login

  • ClientAliveInterval 300  |  ClientAliveCountMax 2

    Close idle sessions

  • sudo sshd -t  |  sudo sshd -T

    Test the config; print the values the server really uses

Certificates

  • openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ca.key

    An EC private key

  • openssl req -x509 -new -key ca.key -subj "/CN=Lab Root CA" -days 365 -out ca.crt

    A self-signed CA certificate

  • openssl req -new -key api.key -subj "/CN=api.internal.test" -addext "subjectAltName=DNS:api.internal.test,IP:10.0.0.5" -out api.csr

    A signing request with DNS and IP names

  • openssl x509 -req -in api.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 90 -copy_extensions copy -out api.crt

    Sign a request with your CA

  • openssl verify -CAfile ca.crt api.crt

    Check a certificate's chain

  • openssl x509 -in api.crt -noout -subject -issuer -dates

    Who it is for, who signed it, and when it expires

  • openssl x509 -in api.crt -noout -ext subjectAltName

    The names a client will accept

  • openssl x509 -checkend 2592000 -noout -in api.crt

    Exit status 1 if it expires within 30 days

Dockerfiles

  • FROM python:3.12-slim@sha256:<digest>

    Pin the base image by digest

  • RUN apt-get update && apt-get install -y --no-install-recommends libpq5 && rm -rf /var/lib/apt/lists/*

    Install only what runs, and clean up in the same layer

  • RUN pip install --no-cache-dir -r requirements.txt

    No package cache left in the image

  • RUN useradd --system --uid 10001 app

    Create an unprivileged user

  • USER app

    Run everything after this line as that user

  • printf '.env\n.git\n' > .dockerignore

    Keep local secrets out of the build context

  • RUN --mount=type=secret,id=token cat /run/secrets/token

    Use a secret during one build step without storing it

Checksums and signatures

  • sha256sum file > SHA256SUMS

    Write a checksum list

  • sha256sum -c SHA256SUMS

    Check every file in a list (OK or FAILED)

  • openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out signing.key

    A signing key

  • openssl pkey -in signing.key -pubout -out signing.pub

    Its public half, to publish

  • openssl dgst -sha256 -sign signing.key -out SHA256SUMS.sig SHA256SUMS

    Sign a file

  • openssl dgst -sha256 -verify signing.pub -signature SHA256SUMS.sig SHA256SUMS

    Verify a signature (Verified OK)

Cheatsheet for the DevSecOps path · Deboistech · All cheatsheets