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:
# 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// 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]');
}# 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 chmod2. 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: 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.
Inspect /home/user/webapp/logs/deploy.log using grep to print only the line containing DEBUG_TOKEN.
Show hint
Run grep DEBUG_TOKEN /home/user/webapp/logs/deploy.log.
Solution available after 3 attempts
Attack: Match leaked debug tokens via regex scanner
Write a bounded regular expression capturing ak_demo_ followed by exactly 8 lowercase hex characters.
Write a regex matching the leaked debug token format ak_demo_ followed by exactly 8 hex characters (0-9a-f).
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: Restrict .env read permissions to owner only
Fix overly permissive 644 permissions on /home/user/webapp/.env down to 600.
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.
Show hint
Run chmod 600 /home/user/webapp/.env to set -rw-------.
Solution available after 3 attempts
Harden: Scrub sensitive tokens before writing logs
Build redactSecretLine(line) so leaked ak_demo_... and sec_demo_... keys never appear in plain text.
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]'.
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
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:
Which two mistakes in this provisioning script expose secrets?
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