Skip to main content
Module 1 · Lesson 3 of 33/42 in the course~14 min
Module lessons (3/3)

Leaked Secrets & File Permissions

Many high-impact breaches start without zero-day memory exploits: a debug statement writes API credentials to container stdout logs, or an engineer copies .env onto a host with default 644 permissions (-rw-r--r--) readable by any local service account.

1. Anatomy of Credential Exposure

Consider how secrets escape at two layers simultaneously:

Bash
# Annotated Example 1: Insecure deploy script leaking env vars and opening .env to all users
env >> /var/log/deploy.log            # dumps API_TOKEN & DB_URL into plaintext logs!
chmod 644 /home/user/webapp/.env      # readable by EVERY local user & compromised sidecar
JS
// Annotated Example 3: Structured logger middleware scrubbing secrets BEFORE write
const SECRET_PATTERN = /\b(?:ak_demo_[0-9a-f]{8}|sec_demo_[A-Za-z0-9_]+)\b/g;

function formatSafeLogLine(line) {
  return String(line).replace(SECRET_PATTERN, '[REDACTED]');
}
Bash
# Annotated Example 4: Correct Linux least-privilege permissions for secret files
chmod 600 /home/user/webapp/.env      # -rw------- (owner read/write only)
ls -la /home/user/webapp/.env         # audit mode bits immediately after chmod

2. Interactive Labs: POSIX Octal Permissions & SIEM Redaction

Toggle Owner / Group / Other bits down to 600, then inspect how log redaction hides tokens before telemetry ingestion:

3. Progressive Labs: Warm-Up → Attack → Defend → Harden

Warm-Up Stage

Warm-Up: Hunt suspicious entries in the Linux deploy log

Use grep inside /home/user to find the DEBUG_TOKEN line leaked inside /home/user/webapp/logs/deploy.log.

OWASP A05:2021 — Security Misconfiguration
Exercise#security.m1.l3.e1
Attempts: 0Loading…

Inspect /home/user/webapp/logs/deploy.log using grep to print only the line containing DEBUG_TOKEN.

Loading editor…
Show hint

Run grep DEBUG_TOKEN /home/user/webapp/logs/deploy.log.

Solution available after 3 attempts

Attack Stage

Attack: Match leaked debug tokens via regex scanner

Write a bounded regular expression capturing ak_demo_ followed by exactly 8 lowercase hex characters.

OWASP A02:2021 — Cryptographic Failures
Exercise#security.m1.l3.e2
Attempts: 0Loading…

Write a regex matching the leaked debug token format ak_demo_ followed by exactly 8 hex characters (0-9a-f).

Loading editor…
Show hint

Use \\bak_demo_[0-9a-f]{8}\\b so partial prefixes (ak_demo_) or overlong strings are rejected.

Solution available after 3 attempts

Defend Stage

Defend: Restrict .env read permissions to owner only

Fix overly permissive 644 permissions on /home/user/webapp/.env down to 600.

OWASP A05:2021 — Security Misconfiguration
Exercise#security.m1.l3.e3
Attempts: 0Loading…

In /home/user, lock down /home/user/webapp/.env so ONLY the file owner can read or write it (600), leaving zero group or world access.

Loading editor…
Show hint

Run chmod 600 /home/user/webapp/.env to set -rw-------.

Solution available after 3 attempts

Hardening Stage

Harden: Scrub sensitive tokens before writing logs

Build redactSecretLine(line) so leaked ak_demo_... and sec_demo_... keys never appear in plain text.

OWASP A09:2021 — Security Logging & Monitoring Failures
Exercise#security.m1.l3.e4
Attempts: 0Loading…

Implement redactSecretLine(line): mask every token matching /\\b(?:ak_demo_[0-9a-f]{8}|sec_demo_[A-Za-z0-9_]+)\\b/g AND any value after Authorization: Bearer <token> with '[REDACTED]'.

Loading editor…
Show hint

Chain .replace(...) on Authorization: Bearer ... and the synthetic token prefixes (ak_demo_ / sec_demo_) using global g flags.

Solution available after 3 attempts

4. Checkpoints & Code Review

Security Quiz#security.m1.l3.e5
Ready

Your team discovers an AWS key and a debug token inside shared CI build logs. Simply rotating the key in AWS is incomplete unless you also:

Answer options
Security Quiz#security.m1.l3.e6
Ready

Which two mistakes in this provisioning script expose secrets?

Bash
mkdir -p /srv/app/config
cp deploy.env /srv/app/config/.env
chmod 644 /srv/app/config/.env
printenv | tee -a /var/log/app-startup.log
Answer options