AgentSkillsCN

security-analysis

在进行威胁建模(STRIDE)、构建攻击树、执行 SAST 静态代码分析、编写自定义 Semgrep 规则、提取安全需求,或将控制措施映射至合规框架时使用。

SKILL.md
--- frontmatter
name: security-analysis
description: "Use when conducting threat modeling (STRIDE), building attack trees, running SAST scans, writing custom Semgrep rules, extracting security requirements, or mapping controls to compliance frameworks."

Security Analysis

STRIDE Threat Modeling

CategoryThreatControl FamilyKey Questions
SpoofingImpersonationAuthenticationToken validation? Session prediction?
TamperingData modificationIntegrityInput validation? Data signing?
RepudiationDeny actionsLogging/AuditTamper-proof logs? Coverage gaps?
Info DisclosureData leakageEncryptionError message leaks? Encryption gaps?
DoSAvailabilityRate limitingResource exhaustion vectors?
ElevationPrivilege escalationAuthorizationConsistent authz checks? Parameter manipulation?

Risk Scoring

code
Risk = Impact x Likelihood (each 1-4)

Score >= 12: Critical (immediate remediation)
Score >= 6:  High (sprint priority)
Score >= 3:  Medium (backlog)
Score < 3:   Low (accept or defer)

Attack Tree Patterns

  • OR nodes: any child path achieves the goal (attacker picks easiest)
  • AND nodes: all children required (harder to execute)
  • Prioritize: unmitigated leaf nodes with LOW difficulty
  • Key insight: OR nodes at the root mean you must mitigate ALL paths; AND nodes mean disrupting one path is sufficient

STRIDE-to-Requirements Mapping

STRIDE CategorySecurity DomainsDefault Requirements
SpoofingAuthentication, Session MgmtStrong auth, token verification, session binding
TamperingInput Validation, Data ProtectionInput allowlisting, integrity signatures
RepudiationAudit LoggingImmutable security event logs
Info DisclosureData Protection, CryptoEncrypt at rest + transit, suppress error details
DoSAvailability, Input ValidationRate limiting, resource quotas
ElevationAuthorizationRBAC/ABAC, least privilege, consistent authz checks

Controls Library (Preferred Defaults)

ControlTypeLayerMitigatesCompliance Refs
MFAPreventiveAppSpoofingPCI-DSS 8.3, OWASP V2
Input ValidationPreventiveAppTamperingOWASP V5
Encryption at RestPreventiveDataInfo DisclosurePCI-DSS 3.4, GDPR Art. 32
Security Event LoggingDetectiveAppRepudiationPCI-DSS 10.2, GDPR Art. 30
RBACPreventiveAppElevationPCI-DSS 7.1
Rate LimitingPreventiveAppDoSOWASP API

SAST Tool Selection

ToolBest ForUse When
SemgrepCustom rules, fast scans, multi-languageDefault choice. Free, fast, excellent custom rules
SonarQubeCode quality + security combinedNeed quality gates, tech debt tracking, enterprise reporting
CodeQLDeep dataflow analysis, vulnerability researchGitHub-native, open-source projects, complex vulnerability patterns
BanditPython-specific scanningPython projects, complement to Semgrep
ESLint SecurityJS/TS inline feedbackAlready using ESLint, want IDE-integrated security checks

Default stack: Semgrep (all languages) + language-specific linter (Bandit/ESLint security plugin).

SAST Rule Strategy

Start Narrow, Expand Gradually

  1. Begin with p/security-audit + p/owasp-top-ten rulesets
  2. Block CI only on ERROR severity -- WARNING as informational
  3. After team adapts (~2 weeks), add language-specific rules
  4. Custom rules only for org-specific patterns

High-Value Rules (Always Enable)

  • Injection: SQL via string formatting, command injection via shell=True, XSS via innerHTML
  • Secrets: hardcoded API keys, AWS credentials (AKIA...), JWT secrets in source
  • Insecure patterns: pickle.loads(), yaml.load() with untrusted data, random for security ops

CI Integration

Pipeline Placement

code
PR opened -> lint -> unit tests -> SAST scan -> build -> integration tests

Run SAST early (before build) for fast feedback. Gate on errors only, not warnings.

GitHub Actions

yaml
- name: Run Semgrep
  uses: returntocorp/semgrep-action@v1
  with:
    config: >-
      p/security-audit
      p/owasp-top-ten

False Positive Management

  • # nosemgrep: rule-id with mandatory comment explaining why
  • Exclude tests/, vendor/, node_modules/, generated/
  • Review suppressions quarterly -- code changes may invalidate them

Custom Semgrep Rules

yaml
rules:
  - id: no-raw-sql
    pattern: cursor.execute("... %s ..." % $VAR)
    message: "Use parameterized queries, not string formatting"
    severity: ERROR
    languages: [python]
    metadata:
      cwe: "CWE-89"

  - id: no-innerHTML-user-input
    pattern: $ELEM.innerHTML = $VAR
    message: "Use textContent or DOMPurify.sanitize()"
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      cwe: "CWE-79"

Vulnerability Quick Reference

VulnerabilityDetectionFix
SQL InjectionString formatting in queriesParameterized queries, ORM
XSSinnerHTML, v-html, dangerouslySetInnerHTMLtextContent, DOMPurify
Command Injectionos.system(), shell=Truesubprocess with array args
Path TraversalUnsanitized file pathsos.path.realpath + prefix check
Hardcoded SecretsRegex patterns in sourceEnvironment variables, secret managers
Insecure Deserializationpickle.loads, yaml.loadjson.loads, yaml.safe_load

Threat Model Document Structure

  1. System Overview - data flow diagram + trust boundaries
  2. Assets - sensitivity classification per asset
  3. STRIDE Analysis - table per category with impact/likelihood
  4. Attack Trees - mermaid diagrams for top 3 scenarios
  5. Mitigation Plan - threat/control/status/coverage matrix
  6. Recommendations - tiered: immediate (critical), 30-day (high), 90-day (improvements)

Requirement Traceability

Every security requirement must link back to:

  • Threat ID it mitigates
  • Compliance control it satisfies (PCI-DSS, HIPAA, GDPR, SOC2, OWASP)
  • Acceptance criteria (testable, specific)
  • Priority derived from risk score (not gut feel)

Compliance Framework Quick Reference

FrameworkAuth ControlsData ProtectionAuditCrypto
PCI-DSS8.1-8.33.4-3.5, 4.110.1-10.33.5-3.6
HIPAA164.312(d)164.312(a)(2)(iv)164.312(b)--
GDPR--Art. 25, 32Art. 30Art. 32
OWASP ASVSV2.1-V2.3V8.1-V8.3--V6.1-V6.2

Agent Team Mode

For comprehensive threat modeling of complex systems with multiple trust boundaries and compliance requirements.

Team Configuration

yaml
team:
  recommended_size: 4
  agent_roles:
    - name: stride-analyst
      type: Explore
      focus: "STRIDE category analysis per component and trust boundary"
      skills_loaded: ["security:security-analysis"]
    - name: attack-tree-builder
      type: Explore
      focus: "Attack tree construction from STRIDE findings, path prioritization"
      skills_loaded: ["security:security-analysis"]
    - name: sast-scanner
      type: general-purpose
      focus: "SAST rule execution, custom Semgrep rule generation, false positive triage"
      skills_loaded: ["security:security-analysis"]
    - name: compliance-mapper
      type: Explore
      focus: "Map findings to PCI-DSS, HIPAA, GDPR, OWASP ASVS controls"
      skills_loaded: ["security:compliance-and-data-privacy"]
  file_ownership: "shared-read-only"
  lead_mode: "hands-on"

Team Workflow

  1. Lead defines system boundaries, assets, and trust boundaries
  2. stride-analyst and attack-tree-builder work in parallel (attack-tree-builder uses STRIDE findings as they arrive)
  3. sast-scanner runs automated scans concurrently
  4. compliance-mapper maps all discovered threats to compliance controls
  5. Lead synthesizes into Threat Model Document (system overview, STRIDE table, attack trees, mitigation plan, recommendations)

Single-Agent Fallback

Without team mode, execute all phases sequentially (default behavior). Team mode is an optional enhancement.

Gotchas

  • OR-node attack trees need ALL paths mitigated; missing one leaves the goal achievable
  • Risk scores should drive requirement priority -- don't let stakeholder politics override
  • Detective controls (logging) are necessary but insufficient alone; pair with preventive
  • Error messages are an info disclosure vector -- generic errors externally, detailed internally
  • Session management is often the weakest STRIDE link; model it explicitly
  • Compliance mapping gaps surface best through automated traceability matrices, not manual review