KGCS Documentation

07 — Live Query Examples: Neo4j Cypher Queries

Running Queries Against the Live KGCS Graph

All queries in this section can be executed directly against the KGCS Neo4j database.

Query 1: CVE-2021-44228 Lookup (Systems Agent)

Use Case: When you identify a vulnerability, look up all affected platforms

Cypher Query

MATCH (v:Vulnerability {cveId: "CVE-2021-44228"})
OPTIONAL MATCH (v)-[:CAUSED_BY]->(w:Weakness)
OPTIONAL MATCH (v)-[:HAS_SCORE]->(s:Score)
OPTIONAL MATCH (v)-[:AFFECTS]->(p:PlatformConfiguration)
RETURN
    v.cveId AS CVE,
    w.cweId AS CWE,
    s.baseScore AS CVSS_Score,
    COUNT(DISTINCT p) AS Affected_Platforms
ORDER BY s.baseScore DESC

Expected Results

CVE CWE CVSS_Score Affected_Platforms
CVE-2021-44228 CWE-91 10.0 619861

Explanation


Query 2: Attack Path Analysis (Offensive Agent)

Use Case: Given a weakness, find all techniques an attacker can use

Cypher Query

MATCH (w:Weakness {cweId: "CWE-91"})
  -[:DEMONSTRATED_BY]->(ap:AttackPattern)
  -[:IMPLEMENTS]->(t:Technique)
OPTIONAL MATCH (t)-[:PART_OF]->(tac:Tactic)
OPTIONAL MATCH (t)<-[:SUBTECHNIQUE_OF]-(st:SubTechnique)
RETURN
    w.cweId AS Weakness,
    ap.capecId AS Attack_Pattern,
    t.attackId AS Technique,
    tac.phaseName AS Tactic,
    COLLECT(DISTINCT st.attackId) AS SubTechniques
ORDER BY t.attackId

Expected Results

Weakness Attack_Pattern Technique Tactic SubTechniques
CWE-91 CAPEC-88 T1059 Execution [T1059.001, T1059.004, ...]
CWE-91 CAPEC-242 T1059 Execution [T1059.001, T1059.004, ...]

Explanation


Query 3: Defensive Coverage (Defensive Agent)

Use Case: For a technique, find all detections, mitigations, and deceptions

Cypher Query

MATCH (t:Technique {attackId: "T1059"})
OPTIONAL MATCH (t)-[:MITIGATED_BY]->(d:DefensiveTechnique)
OPTIONAL MATCH (t)-[:DETECTED_BY]->(c:DetectionAnalytic)
OPTIONAL MATCH (t)-[:COUNTERED_BY]->(s:DeceptionTechnique)
OPTIONAL MATCH (e:EngagementConcept)-[:DISRUPTS]->(t)
RETURN
    t.attackId AS Technique,
    COUNT(DISTINCT d) AS Mitigations,
    COUNT(DISTINCT c) AS Detections,
    COUNT(DISTINCT s) AS Deceptions,
    COUNT(DISTINCT e) AS Engagements

Expected Results

Technique Mitigations Detections Deceptions Engagements
T1059 7 4 3 2

Explanation


Query 4: Complete Causal Chain

Use Case: Show the entire path from CVE to all defensive controls

Cypher Query

MATCH (v:Vulnerability {cveId: "CVE-2021-44228"})
  -[:CAUSED_BY]->(w:Weakness)
  -[:DEMONSTRATED_BY]->(ap:AttackPattern)
  -[:IMPLEMENTS]->(t:Technique)
OPTIONAL MATCH (t)-[:MITIGATED_BY]->(d:DefensiveTechnique)
OPTIONAL MATCH (t)-[:DETECTED_BY]->(c:DetectionAnalytic)
RETURN
    v.cveId AS CVE,
    w.cweId AS Root_Cause,
    ap.capecId AS Attack_Pattern,
    t.attackId AS Technique,
    COLLECT(DISTINCT d.d3fendId)[0..3] AS Mitigations_Sample,
    COLLECT(DISTINCT c.analyticId)[0..3] AS Detections_Sample

Expected Results

CVE Root_Cause Attack_Pattern Technique Mitigations_Sample Detections_Sample
CVE-2021-44228 CWE-91 CAPEC-88 T1059 [D3-PA, D3-RI, D3-NTF] [CAR-2020-04-001, CAR-2019-14-001]
CVE-2021-44228 CWE-91 CAPEC-242 T1059 [D3-PA, D3-RI, D3-NTF] [CAR-2020-04-001, CAR-2019-14-001]

Explanation

This is the complete KGCS reasoning chain: - Specific CVE → General CWE → Attack Patterns → Techniques → Defenses - Shows how standards connect end-to-end - All relationships are explicitly materialized in the graph


Query 5: Affected Platforms Detail

Use Case: See specific CPE configurations affected by the CVE

Cypher Query

MATCH (v:Vulnerability {cveId: "CVE-2021-44228"})
  -[:AFFECTS]->(pc:PlatformConfiguration)
  -[:MATCHES_PLATFORM]->(p:Platform)
RETURN
    pc.matchCriteriaId AS Config_ID,
    p.vendor AS Vendor,
    p.product AS Product,
    p.version AS Version,
    pc.configStatus AS Status
LIMIT 10

Expected Results (Sample)

Config_ID Vendor Product Version Status
uuid-1 apache log4j 2.0 VULNERABLE
uuid-2 apache log4j 2.1 VULNERABLE
uuid-3 apache log4j 2.14.1 VULNERABLE
... ... ... ... ...
uuid-N (619,861 total configurations)

Explanation


Use Case: Find other CVEs that share the same root weakness

Cypher Query

MATCH (w:Weakness {cweId: "CWE-91"})
  <-[:CAUSED_BY]-(v:Vulnerability)
OPTIONAL MATCH (v)-[:HAS_SCORE]->(s:Score)
RETURN
    v.cveId AS CVE,
    s.baseScore AS CVSS_Score,
    v.published AS Published_Date
ORDER BY s.baseScore DESC
LIMIT 20

Expected Results (Sample)

CVE CVSS_Score Published_Date
CVE-2021-44228 10.0 2021-12-24
CVE-XXXX-XXXXX 8.6 2020-12-15
CVE-YYYY-YYYYY 7.2 2019-08-30

Explanation


Query 7: Agent Coverage Comparison

Use Case: Compare defensive coverage across different techniques

Cypher Query

MATCH (t:Technique)
WITH t
OPTIONAL MATCH (t)-[:MITIGATED_BY]->(d:DefensiveTechnique)
OPTIONAL MATCH (t)-[:DETECTED_BY]->(c:DetectionAnalytic)
OPTIONAL MATCH (t)-[:COUNTERED_BY]->(s:DeceptionTechnique)
RETURN
    t.attackId AS Technique,
    COUNT(DISTINCT d) AS Mitigations,
    COUNT(DISTINCT c) AS Detections,
    COUNT(DISTINCT s) AS Deceptions,
    (COUNT(DISTINCT d) + COUNT(DISTINCT c) + COUNT(DISTINCT s)) AS Total_Controls
ORDER BY Total_Controls DESC
LIMIT 10

Expected Results

Technique Mitigations Detections Deceptions Total_Controls
T1059 7 4 3 14
T1548 6 3 2 11
T1489 5 2 1 8
... ... ... ... ...

Explanation


Running These Queries

In Neo4j Browser

  1. Navigate to http://localhost:7474 (or your Neo4j instance)
  2. Paste any Cypher query above
  3. Click "Play" button
  4. Results display in table/graph format

Via Python (using neo4j-driver)

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

def run_query(query):
    with driver.session() as session:
        result = session.run(query)
        for record in result:
            print(record)

# Example: Query 1 - CVE Lookup
query = """
MATCH (v:Vulnerability {cveId: "CVE-2021-44228"})
  -[:CAUSED_BY]->(w:Weakness)
  -[:HAS_SCORE]->(s:Score)
RETURN v.cveId, w.cweId, s.baseScore
"""

run_query(query)

Via REST API (using cypher-shell or curl)

curl -X POST http://localhost:7474/db/data/transaction/commit \
  -H "Authorization: Basic bmVvNGo6cGFzc3dvcmQ=" \
  -H "Content-Type: application/json" \
  -d '{
    "statements": [
      {
        "statement": "MATCH (v:Vulnerability {cveId: \"CVE-2021-44228\"}) RETURN v"
      }
    ]
  }'

Query Performance Notes

All queries are indexed on CVE ID, CWE ID, and Attack ID for fast lookups.

Next Steps

Try running these queries! Against your KGCS Neo4j instance and see the actual graph data.


All queries are parameterized (use $variables in production to prevent injection)