devHọc Dev
EN
Bài học

Tuần 2 - Ngày 11: SAST, DAST, Secret Scanning, Logging và Incident Response

Tuần 2 – Ngày 11

Bài này gộp 2 chủ đề: SAST, DAST và Secret Scanning, Logging, Audit và Incident Response.

Mục tiêu học tập

  • Hiểu SAST (Static), DAST (Dynamic), IAST (Interactive) — khi nào dùng cái nào
  • Sử dụng SonarQube, Semgrep, CodeQL cho SAST với low false-positive rules
  • Triển khai DAST với OWASP ZAP, Burp Suite, Nuclei cho web app
  • Setup secret scanning: gitleaks, truffleHog, GitHub native secret scanning
  • Tích hợp SCA (Software Composition Analysis) với SBOM/dependency scan
  • Đặt scan trong pre-commit hook (husky) và CI/CD pipeline
  • Thiết kế quy trình fail build + exception cho finding critical
  • Xác định các sự kiện security-relevant cần log (auth, privileged action, data access, config change)
  • Tránh leak PII/secret vào log — redact và structured logging với schema
  • Thiết kế centralized log: ELK, Splunk, Datadog, CloudWatch Logs với retention requirement
  • Hiểu SIEM: correlation rules, alerting, threat intel
  • Áp dụng NIST Incident Response lifecycle: Preparation → Detection → Containment → Eradication → Recovery → Lessons Learned
  • Chuẩn bị runbook, on-call rotation, tabletop exercise
  • Forensics 101: preserve evidence, chain of custody, timeline reconstruction
  • GDPR breach notification 72h và các regulation tương tự

Phần 1: SAST, DAST và Secret Scanning

1. SAST vs DAST vs IAST — tổng quan

│Code│RunningApp│Production├───────────────┼────────────────┼────────────────SAST│✓│✗│✗(Static)│scansource││──────────────────┤───────────────┼────────────────┼────────────────DAST│✗│✓│(withcaveat)(Dynamic)││black-box│││fuzzendpoints│──────────────────┤───────────────┼────────────────┼────────────────IAST│✗│✓│✗(Interactive)││instrumented│││duringtest│──────────────────┤───────────────┼────────────────┼────────────────RASP│✗│✓│✓(Runtime│││blockattacksSelf-Protection)│││atruntime
Tiêu chíSASTDASTIAST
InputSource codeURL/endpoint của appApp instrumented + test traffic
Cần app chạy?KhôngCóCó
False positiveCao (cần tune)Thấp (vuln thật)Rất thấp
Tìm đượcSQLi, XSS pattern, hardcoded secret, insecure crypto APISQLi, XSS, IDOR thực tế qua requestCả hai loại trên + data flow runtime
Ngôn ngữPer-language toolLanguage-agnosticCần agent cho language
Khi nàoMỗi PRNightly hoặc pre-prodTrong test environment
Coverage code100% reachableChỉ endpoint test gọiChỉ code chạy trong test

Best practice: dùng cả 3 lớp — SAST (sớm, broad), DAST (xác minh exploit thật), SCA cho deps (Ngày 9). IAST là bonus khi có ngân sách.

2. SAST — Static Application Security Testing

Cách hoạt động

SAST đọc source code (hoặc bytecode) và áp rule pattern matching + dataflow analysis:

Sourcecode──►Parser──►AST/IR──►Ruleengine──►Findings│││├──Patternmatch(regexAST)│├──Tainttracking(userinput→sink)│└──Controlflowanalysis▼Symboltable,callgraph

Ví dụ rule "SQL injection":

Source: HTTP request param (req.query.id, request.GET[...])
Sink:   raw SQL string ("SELECT ... " + var, cursor.execute(f"..."))
Path:   source → sink without sanitizer → ALERT

Tool phổ biến

ToolLoạiĐiểm mạnhĐiểm yếu
SonarQube / SonarCloudMulti-languageQuality + security, dashboard tốt, free tierRule security limited so với chuyên dụng
SemgrepMulti-languageRule viết bằng YAML pattern, nhanh, community rules tốt, free OSSKhông deep dataflow như CodeQL
CodeQL (GitHub)Multi-languageDataflow analysis sâu, free cho public repoPhức tạp viết custom rule (QL language)
Snyk CodeMulti-languageAI-based, ít FPCommercial
Checkmarx, Veracode, FortifyEnterpriseCoverage rộngĐắt, chậm

Semgrep — ví dụ thực tế

# .semgrep.yml — rule custom phát hiện SQL string concat
rules:
  - id: python-sql-string-concat
    pattern-either:
      - pattern: |
          $CURSOR.execute("..." + $X)
      - pattern: |
          $CURSOR.execute(f"...{$X}...")
    message: |
      SQL string concatenation/f-string detected — dùng parameterized query.
    languages: [python]
    severity: ERROR
# Chạy local
semgrep --config auto .

# Chạy với community rules
semgrep --config "p/security-audit" --config "p/owasp-top-ten" .

# Trong CI (GitHub Actions)
semgrep ci --sarif --output=semgrep.sarif

CodeQL trong GitHub Actions

# .github/workflows/codeql.yml
name: CodeQL
on:
  push: { branches: [main] }
  pull_request: { branches: [main] }
  schedule:
    - cron: "0 0 * * 1"     # weekly Monday

jobs:
  analyze:
    runs-on: ubuntu-latest
    permissions:
      security-events: write
      actions: read
      contents: read
    strategy:
      matrix:
        language: [javascript, python]
    steps:
      - uses: actions/checkout@v4
      - uses: github/codeql-action/init@v3
        with:
          languages: ${{ matrix.language }}
          queries: security-extended       # broader, more FP nhưng deeper
      - uses: github/codeql-action/analyze@v3

Finding hiện trong tab Security → Code scanning alerts.

Giảm false positive

Anti-pattern: bật tất cả rule "kitchen sink" → 10000 finding → đội ignore tất cả
                                                              → SAST mất tác dụng

Best practice:
  1. Start với "security-audit" hoặc "OWASP Top 10" rules (high signal)
  2. Triage: dismiss false-positive với lý do (codeql có suppress comment)
  3. Tune custom rule cho domain (vd: pattern crypto công ty đã chuẩn hoá)
  4. Severity-based gating: chỉ fail build ở HIGH/CRITICAL
  5. Diff scan trong PR (chỉ check code thay đổi) thay vì full scan

3. DAST — Dynamic Application Security Testing

Cách hoạt động

DAST chạy như attacker bên ngoài: gửi request có payload độc hại, quan sát response.

DASTScanner──►HTTPrequest──►RunningApp◄──Response││▼├──CrawlendpointDatabase/dependencies├──Injectpayload(SQLi,XSS,SSRF,commandinjection)├──Detectanomaly(errortrace,timedelay,blindboolean)└──Reportfinding

Tool

ToolLoạiUse case
OWASP ZAPFree, open-sourceStandard pick, CI integration tốt
Burp SuiteFree Community + ProManual pentesting, intercept proxy
NucleiTemplate-basedFast scan với cộng đồng template (CVE, misconfig)
Acunetix, NetsparkerCommercialEnterprise, GUI-friendly

OWASP ZAP trong CI

# .github/workflows/zap.yml
name: ZAP Baseline Scan
on:
  schedule:
    - cron: "0 2 * * *"       # nightly 2 AM
  workflow_dispatch:

jobs:
  zap:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: ZAP Baseline Scan
        uses: zaproxy/action-baseline@v0.12.0
        with:
          target: 'https://staging.example.com'
          rules_file_name: '.zap/rules.tsv'
          cmd_options: '-a'                 # active scan (not just passive)

ZAP có 2 mode:

  • Baseline (passive): chỉ analyze response, không gửi payload → an toàn cho prod
  • Active scan: inject payload (SQLi, XSS) → chỉ chạy trên staging/test

Nuclei — template-based fast scan

# Chạy template CVE phổ biến
nuclei -u https://example.com -t cves/ -t vulnerabilities/

# Template ví dụ (.yaml):
# id: nginx-default-page
# requests:
#   - method: GET
#     path: ["{{BaseURL}}"]
#     matchers:
#       - type: word
#         words: ["Welcome to nginx!"]

Cộng đồng có hàng nghìn template (https://github.com/projectdiscovery/nuclei-templates). Chạy trong CI để check infrastructure misconfig.

Authenticated scan

DAST mặc định scan như anonymous → bỏ sót lỗ hổng sau login. Cấu hình ZAP với session token:

# ZAP context với auth
auth:
  loginUrl: https://staging/login
  loggedInIndicatorRegex: "Logout"
  username: dast-user@example.com
  password: ${{ secrets.DAST_PASSWORD }}

Cảnh báo: tạo user riêng cho DAST, không dùng admin (DAST có thể fire DELETE /admin/users test).

4. IAST — Interactive

IAST = SAST insight + DAST reality. Một agent (instrumentation) embed vào app trong test environment, theo dõi:

  • Source: dữ liệu user vào (request param)
  • Sink: SQL execute, exec system command, eval
  • Flow: trace data từ source qua function calls đến sink

Khi test integration chạy, IAST agent log thấy req.body.id chảy đến db.query("SELECT ... " + id) không qua sanitizer → finding precise (line cụ thể + request thực).

Tools: Contrast Security, Seeker (Synopsys), Veracode IAST. Open-source ít. Yêu cầu instrumentation runtime — hỗ trợ Java, .NET, Node, Python nhưng config khác nhau.

Khi nào dùng: team có integration test coverage tốt, muốn finding low FP, có ngân sách commercial tool. Không thay thế SAST — bổ sung.

5. Secret scanning trong CI

Bài toán

Developer commit .env chứa AWS key → push lên GitHub public → 60 giây sau bot quét xong và bắt đầu mining crypto trên account bạn. Đây là sự cố phổ biến nhất, không phải hypothetical.

Tool

ToolLoạiĐặc điểm
gitleaksOSS Go binaryNhanh, custom rule TOML, dễ tích hợp CI
truffleHogOSS PythonEntropy detection + regex, verify secret còn live
GitHub Secret ScanningNativeFree cho public repo, có partner program (AWS, Stripe... auto-revoke)
GitGuardianCommercialDashboard, alerting, full history scan

gitleaks trong pre-commit và CI

# Cài
brew install gitleaks

# Scan working dir + staged
gitleaks detect --source . --verbose

# Scan toàn bộ history
gitleaks detect --source . --log-opts="--all"
# .pre-commit-config.yaml (dùng với pre-commit framework)
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.18.0
    hooks:
      - id: gitleaks
# CI: .github/workflows/secret-scan.yml
name: Secret Scan
on: [pull_request, push]
jobs:
  gitleaks:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }       # full history
      - uses: gitleaks/gitleaks-action@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GitHub native secret scanning

Bật trong Settings → Security → Code security:

  • Secret scanning: GitHub scan commit + PR, alert maintainer
  • Push protection: chặn push nếu có secret pattern match
  • Partner program: GitHub forward leaked key cho provider (AWS, GCP, Stripe...) — provider auto-revoke và notify

Free cho public repo từ 2023. Private repo cần GitHub Advanced Security.

Xử lý secret đã leak

SAI: commit "revert" — secret vẫn trong git history
ĐÚNG:
  1. Revoke/rotate secret NGAY (AWS console, Stripe dashboard...)
  2. Rotate trước, dọn history sau (history rewrite không xoá khỏi forks/clones)
  3. Notify team — ai dùng key đó cần lấy key mới
  4. Audit log provider: kiểm tra xem key đã bị abuse chưa
  5. (Optional) git filter-repo / BFG để xoá khỏi history (nếu repo private)

6. SCA — Software Composition Analysis

(Đã học sâu ở Ngày 9 — Supply Chain, SBOM & Dependency Scanning. Tóm tắt vai trò trong DevSecOps pipeline.)

SCA scan dependencies (npm, pip, Maven, Go modules) vs CVE database:

ToolĐặc điểm
Dependabot (GitHub)Free, auto-PR khi có CVE
SnykMulti-ecosystem, prioritization tốt
TrivyOSS, scan cả container và dep
OWASP Dependency-CheckFree, OWASP project
OSV-Scanner (Google)Dùng OSV database, multi-ecosystem

SCA + SBOM: generate CycloneDX/SPDX SBOM cho mỗi build (syft, cdxgen), attach release. Đáp ứng Executive Order 14028 cho US gov và SLSA framework.

7. Pre-commit hook: client-side scan

Git hooks vs Husky

Git hooks: .git/hooks/pre-commit — bash script, không version control mặc định, mỗi dev phải cài tay.

Husky (Node ecosystem): commit hook lưu trong .husky/ (version control), npm install tự setup.

pre-commit framework (Python, đa ngôn ngữ): config YAML, install hook tự động.

Husky setup

npm install --save-dev husky lint-staged
npx husky init
// package.json
{
  "scripts": {
    "prepare": "husky"
  },
  "lint-staged": {
    "*.{ts,tsx,js,jsx}": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# 1. Lint + format
npx lint-staged

# 2. Secret scan staged files
gitleaks protect --staged --verbose --redact

# 3. Quick SAST trên file thay đổi
git diff --cached --name-only --diff-filter=ACM | \
  xargs -r semgrep --config p/security-audit --error

Trade-off pre-commit

ProsCons
Catch sớm trước khi commitChậm commit experience
Bảo vệ developer khỏi sai sótDev có thể bypass với --no-verify
Không cần CI quay vòngKhông thay thế CI (CI authoritative)

Quy tắc: pre-commit là defense in depth, không phải defense duy nhất. CI vẫn phải scan độc lập (vì --no-verify bypass được).

8. CI/CD: fail build và exception process

Severity-based gating

# Ví dụ: fail nếu HIGH/CRITICAL, warn nếu MEDIUM
- name: Trivy
  uses: aquasecurity/trivy-action@master
  with:
    image-ref: app:latest
    severity: HIGH,CRITICAL
    exit-code: 1                        # fail build
    ignore-unfixed: true                # bỏ qua CVE chưa có fix

Exception process (allow-list có kiểm soát)

Không phải mọi finding đều fix được ngay (vendor chưa patch, false positive). Cần process để chấp nhận có thời hạn:

# .trivyignore — CVE allow-list với reason + expiry
CVE-2024-12345 exp:2025-01-31    # vendor chưa fix, mitigated by NetworkPolicy
CVE-2024-67890 exp:2025-03-15    # FP — không reachable trong code path
# Gitleaks allow-list trong .gitleaks.toml
[allowlist]
description = "Test fixtures with fake credentials"
paths = [
  '''tests/fixtures/.*''',
]
commits = [
  '''abc123...''',                     # specific commit reviewed
]

Exception governance

1. Finding xuất hiện → CI fail
2. Developer mở "Exception Request" (Jira/Backlog ticket)
3. Security team review:
   - Reason hợp lý? (FP, vendor delay, business critical)
   - Mitigation? (WAF rule, NetworkPolicy, monitoring)
   - Expiry? (30d, 90d — không vĩnh viễn)
4. Approved → thêm vào allow-list với expiry
5. CI có job nightly check expiry, re-open ticket nếu hết hạn

Không có exception process: dev sẽ bypass (--no-verify, comment out scan) → security debt tăng không kiểm soát.

Phần 2: Logging, Audit và Incident Response

1. Log gì cho mục đích security?

Không phải mọi log đều có giá trị security. Tập trung vào sự kiện có thể giúp detect, investigate, prove:

Loại eventVí dụVì sao quan trọng
AuthenticationLogin success/fail, MFA challenge, password resetBrute force, credential stuffing detection
AuthorizationAccess denied, role assumption, privilege escalationPhát hiện horizontal/vertical privilege abuse
Privileged actionsudo, admin user tạo IAM key, modify firewall ruleInsider threat, post-compromise activity
Data accessQuery database, S3 GetObject với PII, export reportGDPR audit, data exfiltration detect
Configuration changeIAM policy modified, SG rule added, deploy productionChange tracking, blame-free root cause
Security tool eventWAF block, IDS alert, EDR detection, AV quarantineActive attack indicator
Application error5xx, exception stack trace với security contextVulnerability exploitation evidence

Minimum log fields cho mỗi event

{
  "timestamp": "2025-06-05T10:23:45.123Z",     // ISO 8601 UTC + millisecond
  "event_id": "auth.login.success",
  "actor": {                                     // ai
    "user_id": "u_12345",
    "session_id": "s_abc...",
    "ip": "203.0.113.42",
    "user_agent": "Mozilla/5.0..."
  },
  "action": "login",                             // làm gì
  "resource": "user/u_12345",                    // trên cái gì
  "result": "success",                           // kết quả
  "request_id": "req_xyz...",                    // để correlate với upstream/downstream
  "trace_id": "trace_abc...",                    // distributed tracing
  "metadata": {
    "mfa_used": true,
    "method": "password"
  }
}

Why structured (JSON)? SIEM/Log platform query field cụ thể (event_id:auth.login.fail AND actor.ip:203.0.113.42). Plain text log → grep chậm + không reliable parse.

2. PII và secret trong log — Redact!

Bài toán

# ❌ BAD
logger.info(f"User signup: {request.json}")
# → log có email, password, credit card đầy đủ
2025-06-05T10:23:45Z INFO User signup: {"email":"alice@example.com","password":"hunter2","ssn":"123-45-6789","cc":"4111-1111-1111-1111"}

Hậu quả: log file leak (CloudWatch Logs IAM misconfig, S3 export public, dev workstation pwned) = leak PII của hàng ngàn user → GDPR fine 4% revenue.

Redact pattern

# Allow-list approach: chỉ log field đã approve
ALLOWED_FIELDS = {"user_id", "email_hash", "role", "country"}

def safe_log(event, payload):
    safe_payload = {k: v for k, v in payload.items() if k in ALLOWED_FIELDS}
    logger.info(event, extra=safe_payload)

# Hoặc deny-list — xoá field nhạy cảm
SENSITIVE = {"password", "ssn", "credit_card", "api_key", "token"}

def redact(payload):
    return {k: ("***REDACTED***" if k in SENSITIVE else v) for k, v in payload.items()}

Pino (Node) redact built-in

import pino from "pino";

const logger = pino({
  redact: {
    paths: [
      "password",
      "*.password",
      "req.headers.authorization",
      "req.headers.cookie",
      "user.ssn",
      "user.creditCard.number",
    ],
    censor: "[REDACTED]",
  },
});

logger.info({ user: { email: "a@x.com", ssn: "123-45-6789" } });
// → user.ssn = "[REDACTED]"

Email/identifier — hash thay vì plain

import hashlib

def email_hash(email):
    return hashlib.sha256(email.lower().strip().encode()).hexdigest()[:16]

logger.info("user.login", extra={"email_hash": email_hash(user.email)})
# → giữ được khả năng correlate request từ cùng user mà không lộ email

Log filter / processor layer

Đặt filter tại pipeline (Fluentd, Vector, Logstash) — xoá field nhạy cảm trước khi vào index. Nếu app log "nhầm", filter vẫn cứu được.

# Vector config — drop field
[transforms.redact]
type = "remap"
inputs = ["app-logs"]
source = '''
  del(.password)
  del(.credit_card)
  if exists(.ssn) { .ssn = "***" }
'''

3. Centralized logging và retention

Kiến trúc

┌──────────┐┌──────────┐┌──────────┐│App1││App2││AppN│└─────┬────┘└─────┬────┘└─────┬────┘│││└────►LogAgent(Fluentd/Vector/CloudWatchAgent)──►Ship│▼┌──────────────────────────┐│CentralizedPlatform││ELK/Splunk/││Datadog/CloudWatch/││Loki/OpenSearch│└─────────┬────────────────┘│┌─────────────┼──────────────┐▼▼▼Search/UIAlertingLong-term(Kibana)(PagerDuty)Archive(S3Glacier)

Lựa chọn platform

PlatformĐặc điểmUse case
ELK / Elastic StackOSS, self-host, mạnhTeam có DevOps capacity
SplunkEnterprise, mạnh SIEM/SOARCompliance, regulated industry
Datadog LogsSaaS, tích hợp APM/metricCloud-native, không muốn ops
AWS CloudWatch LogsAWS-native, Logs Insights queryTích hợp AWS service, ít volume
Grafana LokiOSS, label-based, rẻKhi log volume lớn, query metric-style
OpenSearchAWS fork của ElasticAvoiding Elastic license, AWS native

Retention requirement

StandardRetention
PCI-DSS1 year online, 3 months immediately accessible
HIPAA6 years
SOX7 years
GDPR"Storage limitation" — không quá thời gian cần thiết, thường 6-24 tháng
ISO 27001Định nghĩa trong policy (thường 1-3 years)

Hot/Warm/Cold tier

Hottier(0-30day)─►SSDindex,fastquery,expensiveWarmtier(30-90day)─►Slowerdisk,infrequentqueryColdtier(90day-1year)─►S3/Glacier,archive,slowrestore
# AWS CloudWatch — Logs Group với retention 365 ngày
aws logs put-retention-policy \
  --log-group-name /aws/lambda/api \
  --retention-in-days 365

# Export sang S3 (cheaper long-term)
aws logs create-export-task \
  --log-group-name /aws/lambda/api \
  --from $(date -d "30 days ago" +%s)000 \
  --to $(date +%s)000 \
  --destination security-logs-archive-bucket \
  --destination-prefix lambda-api/

Log integrity

Log có thể bị attacker xoá để cover track. Bảo vệ:

  • Append-only / WORM (Write Once Read Many): S3 Object Lock, immutable index
  • Off-host: ship log đi khỏi server trước khi attacker chiếm máy
  • CloudTrail Lake / log signing: hash chain, không ai chỉnh sửa được
  • Centralized + RBAC: log platform tách hoàn toàn account production

4. SIEM — Security Information & Event Management

SIEM = log aggregation + correlation + alerting + investigation UI cho security.

Core capabilities

Logs──►Normalize──►Enrich──►Correlate──►Alert──►Investigate│││││├──"5failedlogin+1success│││fromsameIPin1min"││├──"IAMroleassumedfrom│││unusualIP"││└──"S3GetObject10000obj/min││byuserwithnohistory"│││├──GeoIP,threatintelfeed,assetDB│└──Usercontext(role,department)│└──Maptoschema:ECS,CIM,OCSF

Tool

SIEMĐặc điểm
Splunk Enterprise SecurityIndustry leader, đắt
Microsoft SentinelCloud-native, Azure-integrated, pay-per-GB
Elastic SecurityFree tier, OSS base
Datadog Cloud SIEMSaaS, gắn với observability stack
AWS Security Lake + AthenaCheap storage + ad-hoc query, không real-time alerting
WazuhOSS HIDS + SIEM-lite

Correlation rule ví dụ

-- Splunk SPL: brute force attempt
index=auth event_id="auth.login.fail"
| stats count by actor.ip, _time span=5m
| where count >= 10
| eval risk=case(count >= 50, "high", count >= 20, "medium", true(), "low")
# Elastic detection rule (KQL)
event.category:authentication AND event.outcome:failure
| histogram time=5m
| where count() >= 10

Alert fatigue

Nguy cơ lớn nhất của SIEM: ngày 100 alert → analyst burnout → bỏ qua → miss real attack.

Anti-pattern: bật rule "kitchen sink", thresholding ngẫu nhiên
            → 100 alert/ngày, 95% là benign

Best practice:
  1. Tune threshold theo baseline (5σ above normal)
  2. Suppress known-benign source (vd: vulnerability scanner IP)
  3. Severity-based routing (critical → PagerDuty, low → ticket queue)
  4. Auto-close alert đã resolved trong 24h không re-trigger
  5. Run-book gắn với mỗi alert: ai investigate, làm gì

5. Incident Response Lifecycle (NIST SP 800-61 Rev. 2)

┌────────────────────────────────────────────────────────────────┐│││1.Preparation││└──Runbook,training,on-call,tools,commchannel││││▼││2.Detection&Analysis││└──Alerttriage,scopeassessment,severityrating││││▼││3.Containment││├──Short-term:isolatehost,revokesession││└──Long-term:patch,rotatecred,fixrootcause││││▼││4.Eradication││└──Removemalware,closelateralmovement,killC2││││▼││5.Recovery││└──Restoreservice,monitorforre-infection││││▼││6.Post-Incident(LessonsLearned)││└──Blamelesspost-mortem,actionitems,updaterunbook││││└────►loopbacktoPreparation│└────────────────────────────────────────────────────────────────┘

Severity matrix (ví dụ)

SeverityĐịnh nghĩaResponse SLA
SEV-1 CriticalProduction down, data breach, ransomware active< 15 phút, all-hands
SEV-2 HighPartial outage, suspicious activity confirmed< 1 giờ
SEV-3 MediumSingle component degraded, possible threat< 4 giờ
SEV-4 LowCosmetic, FYINext business day

Roles trong incident

RoleTrách nhiệm
Incident Commander (IC)Quyết định, không tự fix — coordinate
Tech LeadHands-on debug, propose fix
Communications LeadUpdate status page, notify stakeholder, customer
ScribeGhi timeline, decisions, commands chạy
Subject Matter ExpertsOn-demand cho component liên quan

6. Runbook, on-call, tabletop exercise

Runbook

Tài liệu cho từng loại incident — không phải design doc, không phải full architecture. Chỉ "khi alert X kêu, làm 1, 2, 3".

# Runbook: Suspicious S3 download spike

## Symptom
CloudTrail GetObject count > 5σ above baseline trong 5 phút từ một IAM user/role.

## Severity
SEV-2 default. Bump lên SEV-1 nếu bucket chứa PII (data-classification: confidential).

## Triage (5 phút đầu)
1. Identify actor: `aws cloudtrail lookup-events --lookup-attributes ...`
2. Check actor history: thông thường action gì? Lần đầu access bucket này?
3. Check source IP: GeoIP, có trong VPN range không?

## Containment
- Nếu IAM user: disable access key

aws iam update-access-key --access-key-id AKIA... --status Inactive --user-name

- Nếu IAM role: detach permission policy hoặc revoke session

aws iam put-role-policy --role-name --policy-name DenyAll --policy-document file://deny-all.json

- Block IP via WAF/SG nếu xác định malicious

## Eradication
- Rotate compromised credential
- Audit other resources actor đã access trong 7 ngày qua

## Recovery
- Re-enable access sau khi rotate + verify nguyên nhân
- Monitor 24h cho hành vi tương tự

## Escalation
- Bucket có PII → notify DPO trong 12h (chuẩn bị GDPR 72h notification)
- Bucket có PCI data → notify card brand qua PCI process

On-call rotation

Primary on-call: nhận page đầu tiên, response < 15 phút (SEV-1)
Secondary: backup nếu primary không acknowledge sau N phút
Manager on-call: escalation cho decision business (PR, customer comm)

Tool: PagerDuty, Opsgenie, Splunk On-Call
Pattern: weekly rotation, follow-the-sun cho global team

Tabletop exercise

Diễn tập trên giấy (không thực sự chạy production). 1 lần/quý. Scenario ví dụ:

  1. Facilitator: "Lúc 3AM Sunday, CloudTrail detect IAM user ci-deploy tạo 50 EC2 instance ở region eu-central-1 (region không dùng). Bạn nhận page. Làm gì?"
  2. Team discuss step-by-step: ai làm gì, lệnh gì chạy, escalate ai
  3. Facilitator inject tình huống mới ("EC2 đang chạy crypto miner", "AWS bill đã +$5000")
  4. Hậu tabletop: ghi nhận gap (vd: không biết ai có quyền revoke IAM, không có runbook cho EC2 mass-creation) → tạo action item

7. Forensics 101

Khi có incident nghiêm trọng (data breach, ransomware), bằng chứng phải được preserve để:

  • Phân tích root cause
  • Trình toà nếu có liên quan pháp lý
  • Báo cáo regulator

Nguyên tắc Chain of Custody

Mỗi vật chứng (disk image, log file, memory dump) phải có:
  - Who collected: tên, ID, timestamp
  - From where: hostname, IP, path
  - How: command sử dụng (vd: dd, fmem)
  - Hash integrity: SHA-256 ngay khi collect
  - Storage: ai access từ thời điểm collect đến giờ
  - Each transfer: signed log
# Collect EC2 disk image (snapshot EBS)
aws ec2 create-snapshot --volume-id vol-abc123 \
  --description "IR-2025-06-05 host-compromise"

# Hash snapshot for integrity
aws ec2 describe-snapshots --snapshot-ids snap-xyz | sha256sum > evidence.sha256

# Memory dump (Linux)
sudo ./avml memory.dump      # Microsoft AVML tool
sha256sum memory.dump >> evidence.sha256

Timeline reconstruction

┌─────────────────────────────────────────────────────────────────┐│Timeline(UTC)││││14:23:01CloudTrail:GetCallerIdentityfrom203.0.113.42││14:23:15IAM:AccessKeyAKIA...createdforuserbob││14:24:02S3:GetObjectfromnewkey,1000obj/minspike││14:25:30GuardDutyalert:anomalousdataaccess││14:26:00On-callpaged││14:28:14IAM:Disableaccesskey(containment)││14:30:45Log:50000objdownloaded(10GBtotal)│└─────────────────────────────────────────────────────────────────┘│▼Reconstruct:"Bob'slaptoppwnedat14:20viaphishing,attackerstoleAWScredentials,creatednewkeyat14:23topersist..."

Tool: Plaso/log2timeline (super timeline), Splunk SPL transaction command, Datadog timeline view.

Live vs Dead forensics

  • Live (host running): capture memory, network connection, running process. Mất nếu reboot. Rủi ro: hành động bạn để lại dấu vết.
  • Dead (offline): image disk, analyze offline. Mất evidence in-memory (decrypted secret, network state).

Best practice IR: snapshot trước (preserve state) → isolate (revoke creds, NSG block) → investigate trên snapshot copy, không trên prod.

8. GDPR 72-hour breach notification

GDPR Article 33: nếu personal data breach có "likely risk to rights and freedoms", controller phải notify Data Protection Authority (DPA) trong 72 giờ kể từ khi aware (không phải khi xảy ra).

T+0    : Detection (vd: alert hoặc tipoff)
T+24h  : Initial assessment — scope, data type, count
T+48h  : Containment + impact analysis
T+72h  : Notify DPA + (nếu high risk) notify data subject

         Nếu chưa đủ thông tin → notify "preliminary" với
         thông tin có, hứa supplement sau.

Nội dung notification phải bao gồm:

  • Bản chất breach, loại data, ước lượng số người ảnh hưởng
  • Tên + contact của DPO
  • Hậu quả khả dĩ
  • Biện pháp đã/sẽ thực hiện

Tương đương:

  • CCPA / California: tương tự GDPR nhưng cho residents California
  • HIPAA (US): notify HHS trong 60 ngày
  • PCI-DSS: notify card brand "immediately" — định nghĩa bởi brand
  • Vietnam Nghị định 13/2023: notify Bộ Công An trong 72h, tương tự GDPR

Implication cho dev: log + audit phải sẵn sàng để trả lời "data nào bị access, bởi ai, khi nào, bao nhiêu record" trong < 72h. Đây là lý do log everything security-relevant và keep ≥ 1 năm.


Câu hỏi ôn tập

  1. SAST và DAST tìm cùng loại bug (vd SQLi). Khi nào kết quả khác nhau?

    Xem đáp án
    • SAST FP, DAST không: SAST flag db.query("SELECT " + x) nhưng x thực ra là enum cố định trong code → không exploitable. DAST không thấy vì không có endpoint nào pass user input đến đó.
    • DAST tìm thấy, SAST miss: lỗ hổng nằm trong dependency (vd ORM bug) — code app sạch, SAST không scan deps. Hoặc lỗ hổng cấu hình runtime (vd CORS misconfig) — không có trong source code.
    • Cả hai thấy: bug "kinh điển" — req.body.id đi thẳng vào raw SQL.

    Vì vậy cần cả hai lớp + SCA cho deps để cover được different surface.

  2. Tại sao SAST có FP rate cao và làm sao giảm?

    Xem đáp án

    SAST scan code không chạy → không biết runtime context. Một vài nguyên nhân FP:

    • Variable trông nguy hiểm nhưng đã sanitize ở chỗ khác (rule không track được)
    • Code dead (chưa bao giờ chạy)
    • Pattern match quá rộng (vd: bất kỳ eval đều flag, dù eval chỉ trên constant)

    Giảm FP: (1) bắt đầu với rule set tinh (OWASP Top 10, security-audit) thay vì "all rules", (2) tune custom rule cho codebase, (3) suppress với reason ghi rõ (CodeQL có // codeql[js/sql-injection]), (4) chỉ fail build ở HIGH/CRITICAL, (5) diff scan PR thay vì full repo scan.

  3. Khác nhau giữa ZAP baseline scan và active scan? Nguy hiểm gì khi nhầm môi trường?

    Xem đáp án
    • Baseline (passive): chỉ analyze response từ traffic ZAP tự crawl, không gửi payload độc hại. An toàn để chạy trên staging hoặc thậm chí production (nếu rate-limit).
    • Active scan: gửi SQLi, XSS, command injection payload. Có thể tạo dữ liệu rác, gọi DELETE endpoint, crash service.

    Nguy hiểm: chạy active scan trên production → có thể xoá dữ liệu thật, trigger order/email với payload <script>, gây outage. Luôn verify URL target trước khi chạy active. Tạo user DAST riêng với quyền hạn hẹp.

  4. Tại sao chỉ pre-commit hook không đủ để scan secret?

    Xem đáp án

    Pre-commit chạy trên máy dev. Dev có thể bypass bằng git commit --no-verify (cố ý hoặc vô tình). Pre-commit cũng không có nếu dev không cài Husky/pre-commit framework (lần đầu clone repo, hoặc dùng GitHub web editor).

    Cần defense in depth: (1) pre-commit để bắt sớm, (2) CI scan độc lập (authoritative gate), (3) GitHub native secret scanning + push protection (chặn ngay từ git push). Push protection là lớp cuối cùng — chặn trước khi commit vào shared remote.

  5. Khi gitleaks báo có AWS key trong commit cũ, hành động đầu tiên đúng là gì?

    Xem đáp án

    Revoke/rotate key NGAY trên AWS console (deactivate access key cũ, tạo key mới). Lý do: key đã ở trong git history, có thể đã được public hoá qua fork/clone/mirror trong nhiều giây/phút. Rewrite git history (BFG, filter-repo) không giải quyết — vì:

    • Forks/clones existing vẫn có history cũ
    • GitHub cache, archive (archive.org) có thể đã copy
    • Bot scan public repo thường tìm key trong < 60 giây

    Sau khi rotate: audit CloudTrail xem key đã bị dùng cho action gì, notify team có dùng key đó, sau đó mới history rewrite nếu cần.

  6. Tại sao structured logging (JSON) tốt hơn plain text cho mục đích security?

    Xem đáp án

    SIEM/Log platform query field cụ thể: event.category:auth AND user.id:u_123 AND source.ip:203.0.113.42. Plain text phải dùng regex grep — chậm, không reliable (vd email Alice@Example.com vs ALICE@example.com), không aggregate được (vd "đếm distinct IP"). Structured log còn cho phép correlate cross-service qua trace_id, request_id.

    Schema chuẩn: ECS (Elastic Common Schema), OCSF (Open Cybersecurity Schema Framework), CIM (Splunk).

  7. PII trong log nguy hiểm vì sao và làm sao redact đúng?

    Xem đáp án

    Log thường được index ở nhiều nơi (CloudWatch, SIEM, archive), nhiều người access cho debug. PII trong log = PII trong nhiều system → 1 log platform bị compromise = leak hàng triệu PII. GDPR còn yêu cầu PII chỉ ở nơi cần thiết.

    Redact: (1) allow-list field được phép log (an toàn nhất — bỏ field mới không vô tình lộ), (2) deny-list SENSITIVE fields, (3) hash email/identifier để giữ correlation mà không lộ raw, (4) log filter layer (Vector, Logstash) xoá field trước khi index — backup layer khi app log nhầm.

  8. NIST IR lifecycle có Containment trước Eradication. Khác biệt là gì?

    Xem đáp án
    • Containment: chặn attack lan rộng. Quick action như disable IAM key, isolate host khỏi network, block IP. Chưa root-cause, chỉ giảm bleeding.
    • Eradication: loại bỏ root cause sau khi đã hiểu attack. Remove malware, patch vulnerability, rotate creds liên quan, close lateral movement path.

    Vì sao tách: trong khủng hoảng, mục tiêu đầu tiên là stop the bleeding càng nhanh càng tốt; root cause cần thời gian phân tích. Nếu cố eradicate trước khi contain, attacker có thể exfiltrate thêm trong lúc bạn đang investigate.

  9. Tabletop exercise khác simulation chạy thực thế nào, vì sao vẫn cần?

    Xem đáp án

    Tabletop: discuss trên giấy/whiteboard, không chạy lệnh thật. Nhanh (2-3 giờ), không nguy cơ outage, focus vào decision-making và communication.

    Live drill / chaos engineering: thực sự kill instance, simulate breach. Tốn thời gian hơn, nguy cơ nếu sai môi trường.

    Vẫn cần tabletop vì: (1) test runbook coverage (có thiếu scenario nào?), (2) tìm communication gap (ai gọi ai, escalation path), (3) low cost — chạy được mỗi quý. Live drill bổ sung, không thay thế — thường 1-2 lần/năm cho scenario critical.

  10. Vì sao "snapshot trước khi investigate" quan trọng trong forensics?

Xem đáp án

Hành động investigate (login server, chạy ps, restart process) thay đổi state của vật chứng: timestamp file truy cập đổi, memory rewrite, log thêm dòng. Nếu attacker đặt trap (vd shell script tự xoá khi detect login admin), bạn mất evidence.

Best practice: (1) snapshot disk (EBS snapshot, VM snapshot) — immutable copy, (2) memory dump trước khi reboot/isolate, (3) export logs off-host trước khi attacker có thể edit, (4) investigate trên copy, prod giữ nguyên cho forensic team. Nếu liên quan pháp lý, chain of custody phải document mọi access từ thời điểm snapshot.

Bài tập thực hành

Phần 1: SAST, DAST và Secret Scanning

# 1. Cài và chạy gitleaks
brew install gitleaks
gitleaks detect --source . --verbose

# Tạo "honeypot" commit để test
echo 'AWS_KEY="AKIAIOSFODNN7EXAMPLE"' > .env.test
git add .env.test
gitleaks protect --staged --verbose
# → Finding: AWS access key

# 2. Cài Semgrep và scan
pipx install semgrep        # hoặc brew install semgrep
semgrep --config auto .

# Hoặc rule set chuyên security
semgrep --config "p/security-audit" --config "p/owasp-top-ten" .

# 3. Setup pre-commit framework
pipx install pre-commit
cat > .pre-commit-config.yaml <<'EOF'
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.18.0
    hooks:
      - id: gitleaks
  - repo: https://github.com/returntocorp/semgrep
    rev: v1.50.0
    hooks:
      - id: semgrep
        args: ["--config", "p/security-audit", "--error"]
EOF
pre-commit install
pre-commit run --all-files

# 4. Chạy ZAP baseline (Docker)
docker run --rm -v $(pwd):/zap/wrk -t \
  ghcr.io/zaproxy/zaproxy:stable \
  zap-baseline.py -t https://example.com -r zap-report.html

# 5. Chạy Nuclei
brew install nuclei
nuclei -u https://example.com -t cves/

# 6. Setup GitHub workflow tổng hợp
# Copy snippet ở mục 2 và 5 vào .github/workflows/

Phần 2: Logging, Audit và Incident Response

# 1. Setup structured logging trong Node app
npm install pino pino-pretty

cat > app.js <<'EOF'
const pino = require("pino");
const logger = pino({
  level: "info",
  redact: {
    paths: ["password", "*.password", "req.headers.authorization", "*.ssn"],
    censor: "[REDACTED]",
  },
});

logger.info({ user: { id: "u_1", email: "a@x.com", ssn: "123-45-6789" } }, "user.signup");
EOF
node app.js | jq

# 2. CloudWatch Logs Insights query
aws logs start-query \
  --log-group-name /aws/lambda/api \
  --start-time $(date -d '1 hour ago' +%s) \
  --end-time $(date +%s) \
  --query-string 'fields @timestamp, actor.ip, action | filter event_id="auth.login.fail" | stats count() by actor.ip | sort count() desc | limit 20'

# 3. Set retention 365 ngày cho log group
aws logs put-retention-policy \
  --log-group-name /aws/lambda/api \
  --retention-in-days 365

# 4. Test gitleaks log: tạo runbook mẫu
mkdir -p runbooks
cat > runbooks/aws-key-leak.md <<'EOF'
# Runbook: AWS Key Detected in Public Repo
## Triage (5 min)
1. Identify key: AKIA prefix → console
2. aws iam list-access-keys --user-name <user>
## Contain
1. aws iam update-access-key --status Inactive ...
2. Notify on-call + key owner
## Eradicate
1. Rotate key, delete old
2. Audit CloudTrail with old key for 24h
EOF

# 5. Tabletop scenario (đọc và thảo luận với team)
cat <<'EOF'
SCENARIO: Sáng thứ Hai 9 AM, customer support nhận ticket
"Tôi nhận password reset email không yêu cầu". 30 phút sau,
50 ticket tương tự. CloudTrail thấy IAM user "cs-bot" gọi
sts:AssumeRole vào prod role 200 lần.

Q1: Severity?
Q2: Ai page đầu tiên? Ai làm IC?
Q3: Hành động 5 phút đầu là gì?
Q4: Khi nào notify customer? Legal? DPO?
Q5: GDPR clock bắt đầu chạy lúc nào?
EOF

Tài liệu tham khảo


Bài tiếp theo →