Skip to content
Back to search

elastic-agent-skills

MarketplaceWorks with Claude CodeGitHubIncomplete

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 11 Nov 2026.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/elastic/agent-skills with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/elastic/agent-skills'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/elastic-agent-skills/badge.svg)](/listings/elastic-agent-skills)

HTML

<a href="/listings/elastic-agent-skills"><img src="/api/listings/elastic-agent-skills/badge.svg" alt="findsafeskills safety rating" /></a>

Install

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

You can install Elastic skills using the Claude Code native plugin system, the `skills` CLI with `npx`, or by cloning this repository and running the bundled installer script. The `npx` method requires `Node.js` with `npx` available in your environment.

> [!TIP] > **Don't install every skill.** Each installed skill adds routing context that your agent evaluates on every request. Install the **cloud** and **elasticsearch** auth skills — most other skills depend on them — then add only the skills relevant to your workflow. Keeping the installed set focused avoids context bloat and helps the agent route to the right skill reliably.

Claude Code plugin (Recommended for Claude Code users)

Claude Code has a native plugin system that manages skills directly. Start by adding this repository as a marketplace source:

claude plugin marketplace add https://github.com/elastic/agent-skills

Once added, install individual plugins by name:

claude plugin install elastic-elasticsearch@elastic-agent-skills
claude plugin install elastic-kibana@elastic-agent-skills
claude plugin install elastic-observability@elastic-agent-skills
claude plugin install elastic-security@elastic-agent-skills
claude plugin install elastic-cloud@elastic-agent-skills

> [!NOTE] > After installing, skills may not appear immediately when running `/reload-plugins`. This is a known Claude Code issue — restart your Claude Code session to pick up newly installed plugins.

Or use the interactive plugin browser inside any Claude Code session:

/plugins

This opens a menu to browse available plugins from all configured marketplaces, select what to install, and manage what is already installed.

GitHub Copilot CLI

GitHub Copilot CLI has a native plugin system. Add this repository as a marketplace source:

copilot plugin marketplace add elastic/agent-skills

Once added, install individual plugins by name:

copilot plugin install elasticsearch@elastic-agent-skills
copilot plugin install kibana@elastic-agent-skills
copilot plugin install observability@elastic-agent-skills
copilot plugin install security@elastic-agent-skills
copilot plugin install cloud@elastic-agent-skills

Or browse available plugins interactively inside a Copilot session:

/plugin list

npx (Recommended)

The fastest way to install skills is with the [`skills`](https://github.com/vercel-labs/skills) CLI. No need to clone this repository — just run:

npx skills add elastic/agent-skills

This launches an interactive prompt to select skills and [target agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents). The CLI copies each skill folder into the correct location for the agent to discover.

Install a specific skill by name:

npx skills add elastic/agent-skills --skill elasticsearch-esql

Or use the `@` shorthand to specify the skill directly as `repo@skill` (equivalent to `--skill`):

npx skills add elastic/agent-skills@elasticsearch-esql

Install to specific agents (see [supported agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents)):

npx skills add elastic/agent-skills -a cursor -a claude-code

List available skills without installing:

npx skills add elastic/agent-skills --list

Install all skills to all agents (non-interactive):

npx skills add elastic/agent-skills --all

| Flag | Description | | ----------------- | ------------------------------------------------- | | `-a, --agent` | Target specific agents (see [Supported agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents)) | | `-s, --skill` | Install specific skills by name | | `-g, --global` | Install to user home instead of project directory | | `-y, --yes` | Skip confirmation prompts |

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

.claude-plugin/marketplace.json · Manifest · 42 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. {
  2. "plugins": [
  3. {
  4. "version": "0.7.0",
  5. "name": "elastic-elasticsearch",
  6. "source": "./plugins/elasticsearch",
  7. "description": "Elasticsearch skills — ES|QL queries, data ingestion, security (authn, authz, audit), and troubleshooting"
  8. },
  9. {
  10. "version": "0.7.0",
  11. "name": "elastic-kibana",
  12. "source": "./plugins/kibana",
  13. "description": "Kibana skills — dashboards, alerting rules, connectors, Vega visualizations, Agent Builder, streams, and audit logging"
  14. },
  15. {
  16. "version": "0.7.0",
  17. "name": "elastic-observability",
  18. "source": "./plugins/observability",
  19. "description": "Elastic Observability skills — OpenTelemetry instrumentation and migration (.NET, Java, Python), LLM observability, log search, SLOs, and service health"
  20. },
  21. {
  22. "version": "0.7.0",
  23. "name": "elastic-security",
  24. "source": "./plugins/security",
  25. "description": "Elastic Security skills — alert triage, case management, detection rule management, and sample data generation"
  26. },
  27. {
  28. "version": "0.7.0",
  29. "name": "elastic-cloud",
  30. "source": "./plugins/cloud",
  31. "description": "Elastic Cloud skills — project setup, access management, network security, and Serverless project lifecycle"
  32. }
  33. ],
  34. "$schema": "https://anthropic.com/claude-code/marketplace.schema.json",
  35. "name": "elastic-agent-skills",
  36. "owner": {
  37. "name": "Elastic"
  38. },
  39. "metadata": {
  40. "description": "Official Elastic agent skills for Elasticsearch, Kibana, Observability, Security, and Cloud"
  41. }
  42. }
README.md · README · 368 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. <p align="center">
  2. <img alt="Elastic logo" src="https://www.elastic.co/static-res/images/elastic-logo-200.png" width="200">
  3. </p>
  4. <p align="center">
  5. <a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-Apache%202.0-blue"></a>
  6. </p>
  7. # Elastic Agent Skills
  8. Elastic Agent Skills — built by the people who built Elastic — deliver native platform expertise directly to your AI coding agent. This is the official Agent Skills library, compatible with agentic IDEs such as Cursor, GitHub Copilot, Windsurf, Gemini CLI, and more. Skills follow the [Agent Skills](https://agentskills.io/) open standard.
  9. > [!NOTE]
  10. > **Technical Preview**
  11. >
  12. > These skills are in early release and under active development. Expect changes as skills are codified with robust
  13. > evaluations and as the model landscape evolves. Check back frequently for updates.
  14. ## About
  15. This repository contains curated skills that are packages of instructions, context, and tooling that teach any AI agent how to correctly work with Elasticsearch, Kibana, Elastic Observability, and Elastic Security. Drop them into the agent runtime you already use, and your assistant stops using outdated patterns and starts getting it right.
  16. ## What are skills?
  17. Skills are self-contained packages that give AI agents the knowledge and tools to complete specific tasks in a repeatable way. Each skill lives in its own folder with a `SKILL.md` file containing metadata and instructions the agent follows.
  18. For more background on the Agent Skills standard, see [agentskills.io](http://agentskills.io/).
  19. ## Scope
  20. Skills in this repository focus on:
  21. - Interacting with Elasticsearch APIs (search, indexing, cluster management)
  22. - Building and managing Kibana content such as alerts, connectors, and more
  23. - Patterns for Elastic Observability, Elastic Security, and Agent Builder
  24. <!-- BEGIN-SKILL-TABLE -->
  25. ## Available Skills
  26. <details>
  27. <summary>Cloud (2)</summary>
  28. | Skill | Description | Version | Author |
  29. | ----- | ----------- | ------- | ------ |
  30. | [cloud-onboarding](skills/cloud/onboarding/SKILL.md) | Onboard an Elastic Cloud organization: configure the `elastic` CLI's Cloud context and API key, establish a default region, then invite users, assign predefined or custom Serverless project roles, and create or revoke Cloud API keys. Use when setting up Cloud authentication or when granting, modifying, or auditing user access to an organization and its projects. | 0.3.0 | elastic |
  31. | [cloud-provisioning](skills/cloud/provisioning/SKILL.md) | Provision and operate Elastic Cloud infrastructure: create, connect to, update, and delete Serverless projects (Elasticsearch, Observability, Security); manage traffic filters (IP and AWS PrivateLink network security); and manage the lifecycle of Elastic Cloud Hosted deployments. Use when creating or performing day-2 operations on serverless projects or hosted deployments, or restricting their network access. | 0.3.0 | elastic |
  32. </details>
  33. <details>
  34. <summary>Elasticsearch (10)</summary>
  35. | Skill | Description | Version | Author |
  36. | ----- | ----------- | ------- | ------ |
  37. | [elasticsearch-anomaly-detection](skills/elasticsearch/elasticsearch-anomaly-detection/SKILL.md) | Create and manage Elastic ML anomaly detection jobs via the API. Use when setting up jobs on an index or data stream, configuring jobs and datafeeds, or opening, starting, or stopping them. | 1.1.0 | elastic |
  38. | [elasticsearch-anomaly-detection-explainer](skills/elasticsearch/elasticsearch-anomaly-detection-explainer/SKILL.md) | Explain Elasticsearch ML anomaly detection scores, model behavior, and result interpretation. Use when the user asks why a score is high or low, how the model learns, what the numbers mean, or how to troubleshoot unexpected anomaly scores. | 0.3.0 | elastic |
  39. | [elasticsearch-cluster-health](skills/elasticsearch/elasticsearch-cluster-health/SKILL.md) | Diagnose a non-green Elasticsearch cluster and surface the single most likely cause with remediation. Use when an operator reports yellow or red status, unassigned shards, allocation failures, or wants read-only triage before deeper investigation. Teaches replica-vs-primary impact, allocation decider classification, and data-loss awareness. | 0.1.0 | elastic |
  40. | [elasticsearch-esql](skills/elasticsearch/elasticsearch-esql/SKILL.md) | Execute ES\|QL (Elasticsearch Query Language) queries, use when the user wants to query Elasticsearch data, analyze logs, aggregate metrics, explore data, or create charts and dashboards from ES\|QL results. | 0.10.0 | elastic |
  41. | [elasticsearch-index-design](skills/elasticsearch/elasticsearch-index-design/SKILL.md) | Design and review Elasticsearch index mappings for stated access patterns: correct field types, text+keyword multi-fields, doc_values tuning, mapping-explosion avoidance, and explicit shard settings. Use when creating a new index, reviewing a mapping for storage or query performance, fixing wrong field types, or when the user asks which type to use for search, filter, sort, or aggregation on a field. | 0.1.0 | elastic |
  42. | [elasticsearch-ingest](skills/elasticsearch/elasticsearch-ingest/SKILL.md) | Load CSV, JSON, and NDJSON files into Elasticsearch indices using the bulk API and explicit mappings when field types matter. Use when batch-importing local files, converting CSV rows or JSON arrays to NDJSON bulk format, confirming cluster connectivity with elastic es info before ingest, or verifying document counts and mappings after ingest — not for Logstash pipelines, Beats, custom scripts, or index-to-index reindex. | 0.1.3 | elastic |
  43. | [elasticsearch-onboarding](skills/elasticsearch/elasticsearch-onboarding/SKILL.md) | Help developers new to Elasticsearch get from zero to a working search experience. Guide them through understanding their intent, mapping their data, and building a search experience with best practices baked in. Use this when the user shows intent to build search-related functionality, asks about Elasticsearch-related concepts for their use case, or expresses the need for help getting started with Elasticsearch. | 0.3.0 | elastic |
  44. | [elasticsearch-query-optimization](skills/elasticsearch/elasticsearch-query-optimization/SKILL.md) | Diagnose slow Elasticsearch Query DSL searches and propose measured fixes. Use when a search is slow, profile output shows an expensive clause, exact-match filters sit in scoring context, or leading wildcards dominate latency. Ground every recommendation in search profiling — move non-scoring clauses to filter context, eliminate leading wildcards, and re-profile to confirm improvement. | 0.1.0 | elastic |
  45. | [elasticsearch-reindex](skills/elasticsearch/elasticsearch-reindex/SKILL.md) | Guide Elasticsearch reindex for performance: local and remote, slicing, throttling, task API. Use when copying or migrating indices, changing mappings, or transforming during reindex. | 0.2.0 | elastic |
  46. | [elasticsearch-search-relevance](skills/elasticsearch/elasticsearch-search-relevance/SKILL.md) | Improve Elasticsearch search relevance for content and catalog indices: pin or promote results with query rules (correct rule type, criteria, and rule-query wiring) and tune organic ranking with multi_match, field boosts, and analysis grounded in the index mapping. Use when search results rank poorly, a specific document must appear first for a query, or the user asks to tune full-text matching — not for ES\|QL analytics, index ingest, or cluster health. | 0.1.0 | elastic |
  47. </details>
  48. <details>
  49. <summary>Kibana (7)</summary>
  50. | Skill | Description | Version | Author |
  51. | ----- | ----------- | ------- | ------ |
  52. | [kibana-agent-builder](skills/kibana/kibana-agent-builder/SKILL.md) | Create and manage Kibana Agent Builder agents and custom tools. Use when asked to create, update, delete, test, or inspect agents or tools in Agent Builder, or when the user wants to understand what agents or tools already exist. | 0.3.0 | elastic |
  53. | [kibana-alerting-rules](skills/kibana/kibana-alerting-rules/SKILL.md) | Create and manage Kibana alerting rules. Use when creating, updating, or managing rule lifecycle (enable, disable, mute, snooze), choosing metric threshold rule types and params, or read-only find/list with tag filters. | 0.3.0 | elastic |
  54. | [kibana-anomaly-detection](skills/kibana/kibana-anomaly-detection/SKILL.md) | Elastic ML anomaly detection — investigation/RCA, score explanation, job lifecycle troubleshooting, and job operations. Use when answering "what broke?"/"which entity?"/RCA, "why is score high/low?"/renormalization, "datafeed stopped"/"memory limit"/hard_limit, or configuring ML anomaly detection jobs. Reads results from `.ml-anomalies-*` and job state from ML REST APIs. | 0.3.0 | elastic |
  55. | [kibana-cases](skills/kibana/kibana-cases/SKILL.md) | Manage investigation and incident cases across Elastic Security, Observability, and Stack Management — create, search, update, and enrich cases with comments, alerts, events, and observables (IOCs). Use when tracking incidents, correlating alerts to a case, adding investigation notes, updating status or severity, or managing triage output. | 0.2.0 | elastic |
  56. | [kibana-context-engine](skills/kibana/kibana-context-engine/SKILL.md) | Query Elastic Context Engine AI Indices, and the Knowledge Indicators (KIs) they store. Use when asked to search, explore, count, or answer questions from an AI Index / KI / Context Engine knowledge index, and when writing the ES\|QL that queries one. | 0.2.1 | elastic |
  57. | [kibana-dashboards](skills/kibana/kibana-dashboards/SKILL.md) | Create and manage Kibana Dashboards and Lens visualizations. Use when you need to define dashboards and visualizations declaratively, version control them, automate their deployment, or improve layout, sections, controls, or chart design. | 0.4.0 | elastic |
  58. | [kibana-workflows](skills/kibana/kibana-workflows/SKILL.md) | Author, validate, test, run, and inspect Elastic Workflow YAML definitions. Use when the user wants to turn natural language into a Kibana workflow, fix workflow YAML, understand triggers or steps, or run a quick test loop against a real Kibana. | 0.5.0 | elastic |
  59. </details>
  60. <details>
  61. <summary>Observability (6)</summary>
  62. | Skill | Description | Version | Author |
  63. | ----- | ----------- | ------- | ------ |
  64. | [observability-csp-investigation](skills/observability/csp-investigation/SKILL.md) | Investigate cloud-provider (CSP) service issues — AWS today (the reasoning applies to any AWS service; schema and failure signatures are deepest for RDS, Lambda, SQS, ALB, EC2), and in future GCP and Azure — using OTel-schema cloud metrics in Elastic. Use when diagnosing cloud-resource problems, whether alert-triggered or ad hoc: database connection/CPU/memory pressure, function error rates and throttling, queue backlog and consumer lag, load-balancer 5xx, instance saturation. Correlates the resource under investigation against its own baseline, classifies the failure mode, and rules out co-occurring but unrelated conditions. | 0.2.0 | elastic |
  65. | [observability-k8s-investigation](skills/observability/k8s-investigation/SKILL.md) | Investigate Kubernetes workload, node, and control-plane issues using OTel telemetry (EDOT). Use when diagnosing pod failures (CrashLoopBackOff, OOMKilled, Error), node pressure, resource exhaustion, image pull failures, admission rejections, autoscaling anomalies, or correlating K8s state with application signals. OTel ingest path only — the legacy ECS Kubernetes integration shape is out of scope. | 0.5.1 | elastic |
  66. | [observability-llm-obs](skills/observability/llm-obs/SKILL.md) | Answer questions about LLM and agentic-application behavior from data already ingested into Elastic: latency and error rate, token and cost utilization, response quality and guardrail events, and agentic call-chain orchestration. Use when the user asks about LLM monitoring, GenAI observability, token spend or AI cost, model latency, prompt or guardrail failures, or how an agent's tool-call chain executed. | 0.3.1 | elastic |
  67. | [observability-onboarding](skills/observability/onboarding/SKILL.md) | Onboard an application into Elastic Observability with the Elastic Distribution of OpenTelemetry (EDOT): route on language and runtime, detect and replace a classic Elastic APM agent, apply the required OTLP configuration, and then verify with ES\|QL that traces, metrics, and logs actually arrive under the expected service name. Use when adding observability to a service, migrating off the classic Elastic APM agent, or debugging why an instrumented service is not showing up in Elastic. | 0.3.0 | elastic |
  68. | [observability-service-reliability](skills/observability/service-reliability/SKILL.md) | Design and operate service reliability targets in Elastic Observability: choose an SLI type and a defensible target, pick a time window and budgeting method, create and maintain SLOs through the Kibana API, attach burn-rate alert rules, and decide when an SLO is the wrong instrument and a threshold rule, anomaly job, or synthetics monitor is right. Use when defining or reviewing SLOs and error budgets, tuning burn-rate alerting, reducing alert noise, or setting up availability monitoring for a user-facing endpoint. | 0.4.1 | elastic |
  69. | [observability-sre-triage](skills/observability/sre-triage/SKILL.md) | Triage a degraded or suspect service end to end: read SLO status and burn rate, check active alerting rules and ML anomalies, measure throughput, latency, and error rate, assess dependency health and infrastructure saturation, and funnel logs down to the failures that explain it. Use when someone asks whether a service is healthy, why it is slow or erroring, what is in its logs, or which attribute distinguishes the requests that are failing. Also use when someone asks for the query behind any of those signals — throughput, latency percentiles, error rate, dependency health, or log volume — over APM/OTel traces, metrics, or logs. | 0.5.2 | elastic |
  70. </details>
  71. <details>
  72. <summary>Security (6)</summary>
  73. | Skill | Description | Version | Author |
  74. | ----- | ----------- | ------- | ------ |
  75. | [security-alert-analysis](skills/security/alert-analysis/SKILL.md) | Investigate a specific Elastic Security detection alert: gather context, assess entity risk, classify the threat, document findings, and optionally acknowledge. Use when triaging a single known alert, performing SOC investigation, or following up after the security-alert-triage skill identifies a group to investigate. | 0.1.0 | elastic |
  76. | [security-alert-triage](skills/security/alert-triage/SKILL.md) | Prioritize and rank the Elastic Security alert queue by weighted risk — base risk score, MITRE tactic boost, entity risk, and asset criticality — then cluster alerts into entity groups. Use when an analyst asks what to focus on, which alerts are most urgent, or wants a ranked starting point across the queue before deeper investigation begins. | 0.2.0 | elastic |
  77. | [security-case-management](skills/security/case-management/SKILL.md) | Create, search, update, and manage SOC cases via the Kibana Cases API. Use when tracking incidents, linking alerts to cases, adding investigation notes, or managing triage output. | 0.1.0 | elastic |
  78. | [security-detection-rule-management](skills/security/detection-rule-management/SKILL.md) | Create, tune, and manage Elastic Security detection rules (SIEM and Endpoint). Use for false positives, exceptions, new coverage, noisy rules, or rule management via Kibana API. | 0.1.0 | elastic |
  79. | [security-generate-security-sample-data](skills/security/generate-security-sample-data/SKILL.md) | Generate sample security events, attack scenarios, and synthetic alerts for Elastic Security. Use when demoing, populating dashboards, testing detection rules, or setting up a POC. | 0.1.0 | elastic |
  80. | [security-ml-integrations](skills/security/ml-integrations/SKILL.md) | Install and manage Elastic security ML integration packages (beaconing, ded, dga, lmd, pad, problemchild). Handles the full lifecycle: pre-flight checks, Fleet package install, component templates, ingest pipelines, transforms, data views, ML anomaly detection jobs, and detection rules. Use when a user asks to install, set up, check the status of, or troubleshoot a security ML integration package. | 0.1.0 | elastic |
  81. </details>
  82. <!-- END-SKILL-TABLE -->
  83. ## Security considerations
  84. AI coding agents operate with real credentials, real shell access, and often the full permissions of the user running them. When those agents are pointed at security workflows, the stakes are higher. That warrants a frank conversation about risk before you get started.
  85. - **Conduct your own threat modeling.** Evaluate what data the agent can access, what actions it can take, and what happens if it behaves unexpectedly. [CISA's joint guidance on deploying AI systems securely](https://www.cisa.gov/news-events/alerts/2024/04/15/joint-guidance-deploying-ai-systems-securely) is a good starting point.
  86. - **Be aware of what data flows through the agent.** Security data can contain PII, credentials embedded in command lines, and other regulated data. When an agent queries alerts or process events, that content enters the model's context and may be sent to a third-party API. Involve your infosec and compliance teams early.
  87. - **These agents process attacker-controlled input.** Alerts, event fields, and file contents regularly contain strings crafted by attackers. Prompt injection is not theoretical here; it is an inherent property of the operating environment. Research like [Brainworm](https://www.originhq.com/blog/brainworm) demonstrates that agent context files alone can serve as a persistence mechanism for promptware.
  88. - **Scope privileges tightly.** Give API keys the minimum privileges required. Broad response privileges are particularly dangerous. Read-only access is a good default until you've validated behavior.
  89. - **Restrict agent tool access and network reach.** Most AI coding agents ship with broad defaults: shell execution, file system writes, internet access. Reducing the available tool surface limits what a compromised or misdirected agent can do.
  90. - **Start in non-production environments.** Use a Serverless trial project, dev cluster, or isolated Kibana space to evaluate skills before connecting them to anything carrying live security data.
  91. These skills are open source precisely so you can audit what they do. We encourage you to read them before you run them.
  92. ## Getting started
  93. You can install Elastic skills using the Claude Code native plugin system, the `skills` CLI with `npx`, or by cloning this repository and running the bundled installer script. The `npx` method requires `Node.js` with `npx` available in your environment.
  94. > [!TIP]
  95. > **Don't install every skill.** Each installed skill adds routing context that your agent evaluates on every request. Install the **cloud** and **elasticsearch** auth skills — most other skills depend on them — then add only the skills relevant to your workflow. Keeping the installed set focused avoids context bloat and helps the agent route to the right skill reliably.
  96. ### Claude Code plugin (Recommended for Claude Code users)
  97. Claude Code has a native plugin system that manages skills directly. Start by adding this repository as a marketplace source:
  98. ```sh
  99. claude plugin marketplace add https://github.com/elastic/agent-skills
  100. ```
  101. Once added, install individual plugins by name:
  102. ```sh
  103. claude plugin install elastic-elasticsearch@elastic-agent-skills
  104. claude plugin install elastic-kibana@elastic-agent-skills
  105. claude plugin install elastic-observability@elastic-agent-skills
  106. claude plugin install elastic-security@elastic-agent-skills
  107. claude plugin install elastic-cloud@elastic-agent-skills
  108. ```
  109. > [!NOTE]
  110. > After installing, skills may not appear immediately when running `/reload-plugins`. This is a known Claude Code issue — restart your Claude Code session to pick up newly installed plugins.
  111. Or use the interactive plugin browser inside any Claude Code session:
  112. ```
  113. /plugins
  114. ```
  115. This opens a menu to browse available plugins from all configured marketplaces, select what to install, and manage what is already installed.
  116. ### GitHub Copilot CLI
  117. GitHub Copilot CLI has a native plugin system. Add this repository as a marketplace source:
  118. ```sh
  119. copilot plugin marketplace add elastic/agent-skills
  120. ```
  121. Once added, install individual plugins by name:
  122. ```sh
  123. copilot plugin install elasticsearch@elastic-agent-skills
  124. copilot plugin install kibana@elastic-agent-skills
  125. copilot plugin install observability@elastic-agent-skills
  126. copilot plugin install security@elastic-agent-skills
  127. copilot plugin install cloud@elastic-agent-skills
  128. ```
  129. Or browse available plugins interactively inside a Copilot session:
  130. ```
  131. /plugin list
  132. ```
  133. ### npx (Recommended)
  134. The fastest way to install skills is with the [`skills`](https://github.com/vercel-labs/skills) CLI. No need to clone this repository — just run:
  135. ```sh
  136. npx skills add elastic/agent-skills
  137. ```
  138. This launches an interactive prompt to select skills and [target agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents). The CLI copies each skill folder into the correct location for the agent to discover.
  139. Install a specific skill by name:
  140. ```sh
  141. npx skills add elastic/agent-skills --skill elasticsearch-esql
  142. ```
  143. Or use the `@` shorthand to specify the skill directly as `repo@skill` (equivalent to `--skill`):
  144. ```sh
  145. npx skills add elastic/agent-skills@elasticsearch-esql
  146. ```
  147. Install to specific agents (see [supported agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents)):
  148. ```sh
  149. npx skills add elastic/agent-skills -a cursor -a claude-code
  150. ```
  151. List available skills without installing:
  152. ```sh
  153. npx skills add elastic/agent-skills --list
  154. ```
  155. Install all skills to all agents (non-interactive):
  156. ```sh
  157. npx skills add elastic/agent-skills --all
  158. ```
  159. | Flag | Description |
  160. | ----------------- | ------------------------------------------------- |
  161. | `-a, --agent` | Target specific agents (see [Supported agents](https://github.com/vercel-labs/skills?tab=readme-ov-file#supported-agents)) |
  162. | `-s, --skill` | Install specific skills by name |
  163. | `-g, --global` | Install to user home instead of project directory |
  164. | `-y, --yes` | Skip confirmation prompts |
  165. | `--all` | Install all skills to all agents without prompts |
  166. | `--list` | List available skills without installing |
  167. ### Local clone
  168. If you prefer to work from a local checkout, or your environment does not have Node.js / npx, clone the repository and use the bundled bash installer:
  169. ```sh
  170. git clone https://github.com/elastic/agent-skills.git
  171. cd agent-skills
  172. ./scripts/install-skills.sh add -a <agent>
  173. ```
  174. The script requires bash 3.2+ and standard Unix utilities (`awk`, `find`, `cp`, `rm`, `mkdir`).
  175. | Flag | Description |
  176. | ----------------- | ------------------------------------- |
  177. | `-a, --agent` | Target agent (repeatable) |
  178. | `-s, --skill` | Install specific skills by name |
  179. | `-f, --force` | Overwrite already-installed skills |
  180. | `-y, --yes` | Skip confirmation prompts |
  181. List all available skills:
  182. ```sh
  183. ./scripts/install-skills.sh list
  184. ```
  185. ### Supported agents
  186. | Agent | Install directory |
  187. | --------------- | ------------------------- |
  188. | claude-code | `.claude/skills` |
  189. | cursor | `.agents/skills` |
  190. | codex | `.agents/skills` |
  191. | opencode | `.agents/skills` |
  192. | pi | `.pi/agent/skills` |
  193. | windsurf | `.windsurf/skills` |
  194. | roo | `.roo/skills` |
  195. | cline | `.agents/skills` |
  196. | github-copilot | `.agents/skills` |
  197. | gemini-cli | `.agents/skills` |
  198. ## Updating skills
  199. The update process depends on how the skills were installed.
  200. ### Claude Code plugin
  201. Update all installed plugins to their latest versions:
  202. ```sh
  203. claude plugin update
  204. ```
  205. Update a specific plugin:
  206. ```sh
  207. claude plugin update elastic-elasticsearch
  208. ```
  209. To keep plugins up to date automatically, enable auto-update using `/plugins` within Claude Code.
  210. When auto-update is on, Claude Code checks for new plugin versions at startup and updates in the background.
  211. ### GitHub Copilot CLI
  212. Update all installed plugins to their latest versions:
  213. ```sh
  214. copilot plugin update
  215. ```
  216. Update a specific plugin:
  217. ```sh
  218. copilot plugin update elasticsearch
  219. ```
  220. ### npx
  221. Check whether any installed skills have changed upstream:
  222. ```sh
  223. npx skills check
  224. ```
  225. Pull the latest versions of all installed skills:
  226. ```sh
  227. npx skills update
  228. ```
  229. The CLI tracks each skill's source repository and a content hash in a lock file. `check` compares your local hashes against GitHub; `update` re-downloads anything that has drifted.
  230. > **Tip:** The default npx installation uses symlinks, so every agent points to a single canonical copy. Updating once refreshes all agents at the same time.
  231. ### Local clone
  232. Re-run the installer with `--force` to overwrite existing skills:
  233. ```sh
  234. git pull
  235. ./scripts/install-skills.sh add -a <agent> --force
  236. ```
  237. Without `--force` the script skips skills that are already installed.
  238. ## Skill format
  239. Every skill folder contains a `SKILL.md` with YAML frontmatter and markdown instructions:
  240. ```yaml
  241. ---
  242. name: elasticsearch-my-skill
  243. description: >
  244. What the skill does AND when an agent should activate it.
  245. metadata:
  246. version: 0.1.0
  247. visibility: public
  248. ---
  249. # My Skill
  250. [Instructions that the agent follows when this skill is active]
  251. ```
  252. The `description` field is the sole trigger mechanism — agent runtimes read it to decide when to load a skill. For the full format specification, see [agentskills.io/specification](https://agentskills.io/specification).
  253. ## Issues
  254. Found a problem or have a suggestion? [Open an issue](https://github.com/elastic/agent-skills/issues/new) and we will review it.
  255. ## Disclaimer
  256. These skills are provided as-is. Always test skills thoroughly in your own environment before relying on them for critical tasks.
plugins/security/skills/generate-security-sample-data/scripts/sample-data.js · Bundled script · 2,587 lines · 3 findings marked
  1. #!/usr/bin/env node
  2. /**
  3. * Generate realistic ECS-compliant security events for installed packages.
  4. * Populates dashboards and triggers detection rules so onboarding isn't hollow.
  5. *
  6. * Uses RFC 5737 (192.0.2.x, 198.51.100.x, 203.0.113.x) and RFC 2606
  7. * (example.com) addresses to avoid triggering real security incidents.
  8. */
  9. import crypto from "node:crypto";
  10. import { createInterface } from "readline";
  11. import { createClient } from "./es-client.js";
  12. import { kibanaDelete, kibanaGet } from "./kibana-client.js";
  13. const FAKE_USERS = ["alice", "bob", "charlie", "dave", "eve", "frank"];
  14. const FAKE_HOSTS = ["web-prod-01", "db-prod-02", "app-staging-03", "jumpbox-01", "ci-runner-04"];
  15. const FAKE_IPS = ["192.0.2.10", "192.0.2.25", "198.51.100.5", "198.51.100.42", "203.0.113.7", "203.0.113.99"];
  16. function promptConfirm(message) {
  17. const rl = createInterface({ input: process.stdin, output: process.stderr });
  18. return new Promise((resolve) => {
  19. rl.question(`${message} [y/N] `, (answer) => {
  20. rl.close();
  21. resolve(answer.trim().toLowerCase() === "y");
  22. });
  23. });
  24. }
  25. function randomFrom(arr) {
  26. return arr[Math.floor(Math.random() * arr.length)];
  27. }
  28. function randomInt(min, max) {
  29. return Math.floor(Math.random() * (max - min + 1)) + min;
  30. }
  31. function recentTimestamp(hoursBack = 24) {
  32. const now = Date.now();
  33. const offset = Math.random() * hoursBack * 60 * 60 * 1000;
  34. return new Date(now - offset).toISOString();
  35. }
  36. const DEFAULT_STACK_VERSION = "8.17.0";
  37. const SAMPLE_DATA_MARKER = "elastic-security-sample-data";
  38. const CLEANUP_SOURCE_SCAN_LOOKBACK = "now-14d";
  39. const CLEANUP_SOURCE_SCAN_PAGE_SIZE = 500;
  40. let _stackVersion = DEFAULT_STACK_VERSION;
  41. let _detected = false;
  42. async function detectClusterVersions(client) {
  43. if (_detected) return;
  44. _detected = true;
  45. try {
  46. const info = await client.info();
  47. _stackVersion = info.version.number;
  48. console.log(` Detected stack version: ${_stackVersion}\n`);
  49. } catch {
  50. console.warn(` Warning: could not detect stack version, using default ${DEFAULT_STACK_VERSION}\n`);
  51. }
  52. }
  53. function stackVersion() {
  54. return ecsVersion();
  55. }
  56. function ecsVersion() {
  57. // ECS expects major.minor.0 (not stack patch versions).
  58. const [major, minor] = String(_stackVersion).split(".");
  59. return `${major || "8"}.${minor || "17"}.0`;
  60. }
  61. function fullStackVersion() {
  62. return _stackVersion;
  63. }
  64. function baseEvent(overrides = {}) {
  65. return {
  66. "@timestamp": recentTimestamp(),
  67. "ecs.version": ecsVersion(),
  68. tags: [SAMPLE_DATA_MARKER],
  69. ...overrides,
  70. };
  71. }
  72. const EVENT_GENERATORS = {
  73. system: () => {
  74. const templates = [
  75. () =>
  76. baseEvent({
  77. "event.category": ["authentication"],
  78. "event.type": ["start"],
  79. "event.kind": "event",
  80. "event.outcome": "success",
  81. "event.module": "system",
  82. "event.dataset": "system.auth",
  83. "event.action": "logged-in",
  84. "user.name": randomFrom(FAKE_USERS),
  85. "source.ip": randomFrom(FAKE_IPS),
  86. "host.name": randomFrom(FAKE_HOSTS),
  87. "host.os.family": "linux",
  88. message: `Accepted publickey for ${randomFrom(FAKE_USERS)} from ${randomFrom(FAKE_IPS)} port ${randomInt(1024, 65535)}`,
  89. "data_stream.type": "logs",
  90. "data_stream.dataset": "system.auth",
  91. "data_stream.namespace": "default",
  92. }),
  93. () =>
  94. baseEvent({
  95. "event.category": ["authentication"],
  96. "event.type": ["start"],
  97. "event.kind": "event",
  98. "event.outcome": "failure",
  99. "event.module": "system",
  100. "event.dataset": "system.auth",
  101. "event.action": "logon-failed",
  102. "user.name": randomFrom(["root", "admin", "test", ...FAKE_USERS]),
  103. "source.ip": randomFrom(FAKE_IPS),
  104. "host.name": randomFrom(FAKE_HOSTS),
  105. "host.os.family": "linux",
  106. message: `Failed password for invalid user ${randomFrom(["root", "admin"])} from ${randomFrom(FAKE_IPS)} port ${randomInt(1024, 65535)}`,
  107. "data_stream.type": "logs",
  108. "data_stream.dataset": "system.auth",
  109. "data_stream.namespace": "default",
  110. }),
  111. () =>
  112. baseEvent({
  113. "event.category": ["process"],
  114. "event.type": ["start"],
  115. "event.kind": "event",
  116. "event.module": "system",
  117. "event.dataset": "system.syslog",
  118. "host.name": randomFrom(FAKE_HOSTS),
  119. "host.os.family": "linux",
  120. "process.name": randomFrom(["sshd", "cron", "systemd", "nginx", "dockerd"]),
  121. "process.pid": randomInt(1000, 50000),
  122. message: `${randomFrom(["Started", "Stopped", "Reloaded"])} ${randomFrom(["nginx.service", "docker.service", "sshd.service"])}`,
  123. "data_stream.type": "logs",
  124. "data_stream.dataset": "system.syslog",
  125. "data_stream.namespace": "default",
  126. }),
  127. ];
  128. return randomFrom(templates)();
  129. },
  130. endpoint: () => {
  131. const templates = [
  132. () =>
  133. baseEvent({
  134. "event.category": ["process"],
  135. "event.type": ["start"],
  136. "event.kind": "event",
  137. "event.module": "endpoint",
  138. "event.dataset": "endpoint.events.process",
  139. "event.action": "exec",
  140. "host.name": randomFrom(FAKE_HOSTS),
  141. "host.os.family": randomFrom(["linux", "windows", "macos"]),
  142. "process.name": randomFrom(["curl", "wget", "python3", "powershell.exe", "cmd.exe", "bash"]),
  143. "process.pid": randomInt(1000, 50000),
  144. "process.executable": randomFrom(["/usr/bin/curl", "/usr/bin/python3", "C:\\Windows\\System32\\cmd.exe"]),
  145. "process.args": [randomFrom(["-L", "-o", "-c", "/k", "-exec"])],
  146. "process.parent.name": randomFrom(["bash", "zsh", "explorer.exe", "sshd"]),
  147. "process.parent.pid": randomInt(500, 5000),
  148. "user.name": randomFrom(FAKE_USERS),
  149. "data_stream.type": "logs",
  150. "data_stream.dataset": "endpoint.events.process",
  151. "data_stream.namespace": "default",
  152. }),
  153. () =>
  154. baseEvent({
  155. "event.category": ["network"],
  156. "event.type": ["connection", "start"],
  157. "event.kind": "event",
  158. "event.module": "endpoint",
  159. "event.dataset": "endpoint.events.network",
  160. "event.action": "connection_attempted",
  161. "host.name": randomFrom(FAKE_HOSTS),
  162. "process.name": randomFrom(["chrome", "firefox", "curl", "python3"]),
  163. "process.pid": randomInt(1000, 50000),
  164. "source.ip": "10.0.0." + randomInt(1, 254),
  165. "destination.ip": randomFrom(FAKE_IPS),
  166. "destination.port": randomFrom([80, 443, 8080, 8443, 22]),
  167. "network.transport": "tcp",
  168. "user.name": randomFrom(FAKE_USERS),
  169. "data_stream.type": "logs",
  170. "data_stream.dataset": "endpoint.events.network",
  171. "data_stream.namespace": "default",
  172. }),
  173. () =>
  174. baseEvent({
  175. "event.category": ["file"],
  176. "event.type": ["creation"],
  177. "event.kind": "event",
  178. "event.module": "endpoint",
  179. "event.dataset": "endpoint.events.file",
  180. "event.action": "creation",
  181. "host.name": randomFrom(FAKE_HOSTS),
  182. "file.name": randomFrom(["payload.exe", "report.pdf", "config.yaml", "update.sh", "notes.txt"]),
  183. "file.path": randomFrom(["/tmp/payload.exe", "/home/user/report.pdf", "C:\\Temp\\update.bat"]),
  184. "file.extension": randomFrom(["exe", "pdf", "yaml", "sh", "txt"]),
  185. "process.name": randomFrom(["bash", "explorer.exe", "python3"]),
  186. "user.name": randomFrom(FAKE_USERS),
  187. "data_stream.type": "logs",
  188. "data_stream.dataset": "endpoint.events.file",
  189. "data_stream.namespace": "default",
  190. }),
  191. ];
  192. return randomFrom(templates)();
  193. },
  194. windows: () => {
  195. const templates = [
  196. () =>
  197. baseEvent({
  198. "event.category": ["authentication"],
  199. "event.type": ["start"],
  200. "event.kind": "event",
  201. "event.code": "4624",
  202. "event.action": "logged-in",
  203. "event.outcome": "success",
  204. "event.module": "windows",
  205. "event.dataset": "windows.security",
  206. "event.provider": "Microsoft-Windows-Security-Auditing",
  207. "winlog.event_id": 4624,
  208. "winlog.channel": "Security",
  209. "winlog.logon.type": randomFrom(["Interactive", "Network", "RemoteInteractive"]),
  210. "host.name": randomFrom(FAKE_HOSTS),
  211. "host.os.family": "windows",
  212. "user.name": randomFrom(FAKE_USERS),
  213. "user.domain": "CORP",
  214. "source.ip": randomFrom(FAKE_IPS),
  215. "data_stream.type": "logs",
  216. "data_stream.dataset": "windows.security",
  217. "data_stream.namespace": "default",
  218. }),
  219. () =>
  220. baseEvent({
  221. "event.category": ["authentication"],
  222. "event.type": ["start"],
  223. "event.kind": "event",
  224. "event.code": "4625",
  225. "event.action": "logon-failed",
  226. "event.outcome": "failure",
  227. "event.module": "windows",
  228. "event.dataset": "windows.security",
  229. "event.provider": "Microsoft-Windows-Security-Auditing",
  230. "winlog.event_id": 4625,
  231. "winlog.channel": "Security",
  232. "host.name": randomFrom(FAKE_HOSTS),
  233. "host.os.family": "windows",
  234. "user.name": randomFrom(["Administrator", "admin", ...FAKE_USERS]),
  235. "user.domain": "CORP",
  236. "source.ip": randomFrom(FAKE_IPS),
  237. "data_stream.type": "logs",
  238. "data_stream.dataset": "windows.security",
  239. "data_stream.namespace": "default",
  240. }),
  241. () =>
  242. baseEvent({
  243. "event.category": ["process"],
  244. "event.type": ["start"],
  245. "event.kind": "event",
  246. "event.code": "4688",
  247. "event.action": "created-process",
  248. "event.module": "windows",
  249. "event.dataset": "windows.security",
  250. "event.provider": "Microsoft-Windows-Security-Auditing",
  251. "winlog.event_id": 4688,
  252. "winlog.channel": "Security",
  253. "host.name": randomFrom(FAKE_HOSTS),
  254. "host.os.family": "windows",
  255. "process.name": randomFrom(["cmd.exe", "powershell.exe", "notepad.exe", "msiexec.exe", "schtasks.exe"]),
  256. "process.executable": randomFrom([
  257. "C:\\Windows\\System32\\cmd.exe",
  258. "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  259. ]),
  260. "process.pid": randomInt(1000, 50000),
  261. "process.parent.name": randomFrom(["explorer.exe", "services.exe", "cmd.exe"]),
  262. "user.name": randomFrom(FAKE_USERS),
  263. "user.domain": "CORP",
  264. "data_stream.type": "logs",
  265. "data_stream.dataset": "windows.security",
  266. "data_stream.namespace": "default",
  267. }),
  268. ];
  269. return randomFrom(templates)();
  270. },
  271. aws: () => {
  272. const awsAcct = "123456789012";
  273. const templates = [
  274. () =>
  275. baseEvent({
  276. "event.category": ["authentication"],
  277. "event.type": ["info"],
  278. "event.kind": "event",
  279. "event.action": "ConsoleLogin",
  280. "event.outcome": randomFrom(["success", "success", "success", "failure"]),
  281. "event.module": "aws",
  282. "event.dataset": "aws.cloudtrail",
  283. "cloud.provider": "aws",
  284. "cloud.account.id": awsAcct,
  285. "cloud.region": randomFrom(["us-east-1", "us-west-2", "eu-west-1"]),
  286. "aws.cloudtrail.event_type": "AwsConsoleSignIn",
  287. "user.name": randomFrom(FAKE_USERS),
  288. "source.ip": randomFrom(FAKE_IPS),
  289. "user_agent.original": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
  290. "data_stream.type": "logs",
  291. "data_stream.dataset": "aws.cloudtrail",
  292. "data_stream.namespace": "default",
  293. }),
  294. () =>
  295. baseEvent({
  296. "event.category": ["iam"],
  297. "event.type": ["info"],
  298. "event.kind": "event",
  299. "event.action": "AssumeRole",
  300. "event.outcome": "success",
  301. "event.module": "aws",
  302. "event.dataset": "aws.cloudtrail",
  303. "cloud.provider": "aws",
  304. "cloud.account.id": awsAcct,
  305. "cloud.region": randomFrom(["us-east-1", "us-west-2"]),
  306. "aws.cloudtrail.event_type": "AwsApiCall",
  307. "user.name": randomFrom(FAKE_USERS),
  308. "source.ip": randomFrom(FAKE_IPS),
  309. "data_stream.type": "logs",
  310. "data_stream.dataset": "aws.cloudtrail",
  311. "data_stream.namespace": "default",
  312. }),
  313. () =>
  314. baseEvent({
  315. "event.category": ["network"],
  316. "event.type": ["info"],
  317. "event.kind": "event",
  318. "event.action": randomFrom([
  319. "AuthorizeSecurityGroupIngress",
  320. "RunInstances",
  321. "StopInstances",
  322. "CreateBucket",
  323. ]),
  324. "event.outcome": "success",
  325. "event.module": "aws",
  326. "event.dataset": "aws.cloudtrail",
  327. "cloud.provider": "aws",
  328. "cloud.account.id": awsAcct,
  329. "cloud.region": randomFrom(["us-east-1", "us-west-2", "eu-west-1"]),
  330. "aws.cloudtrail.event_type": "AwsApiCall",
  331. "user.name": randomFrom(FAKE_USERS),
  332. "source.ip": randomFrom(FAKE_IPS),
  333. "data_stream.type": "logs",
  334. "data_stream.dataset": "aws.cloudtrail",
  335. "data_stream.namespace": "default",
  336. }),
  337. ];
  338. return randomFrom(templates)();
  339. },
  340. okta: () => {
  341. const templates = [
  342. () =>
  343. baseEvent({
  344. "event.category": ["authentication"],
  345. "event.type": ["start"],
  346. "event.kind": "event",
  347. "event.action": "user.session.start",
  348. "event.outcome": "success",
  349. "event.module": "okta",
  350. "event.dataset": "okta.system",
  351. "okta.event_type": "user.session.start",
  352. "okta.display_message": "User login to Okta",
  353. "okta.outcome.result": "SUCCESS",
  354. "user.name": randomFrom(FAKE_USERS),
  355. "user.email": `${randomFrom(FAKE_USERS)}@example.com`,
  356. "source.ip": randomFrom(FAKE_IPS),
  357. "user_agent.original": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
  358. "data_stream.type": "logs",
  359. "data_stream.dataset": "okta.system",
  360. "data_stream.namespace": "default",
  361. }),
  362. () =>
  363. baseEvent({
  364. "event.category": ["authentication"],
  365. "event.type": ["info"],
  366. "event.kind": "event",
  367. "event.action": "user.authentication.auth_via_mfa",
  368. "event.outcome": randomFrom(["success", "success", "failure"]),
  369. "event.module": "okta",
  370. "event.dataset": "okta.system",
  371. "okta.event_type": "user.authentication.auth_via_mfa",
  372. "okta.display_message": "Authentication of user via MFA",
  373. "okta.outcome.result": randomFrom(["SUCCESS", "SUCCESS", "FAILURE"]),
  374. "okta.authentication_context.authentication_step": randomInt(1, 3),
  375. "user.name": randomFrom(FAKE_USERS),
  376. "user.email": `${randomFrom(FAKE_USERS)}@example.com`,
  377. "source.ip": randomFrom(FAKE_IPS),
  378. "data_stream.type": "logs",
  379. "data_stream.dataset": "okta.system",
  380. "data_stream.namespace": "default",
  381. }),
  382. () =>
  383. baseEvent({
  384. "event.category": ["iam"],
  385. "event.type": ["change"],
  386. "event.kind": "event",
  387. "event.action": randomFrom([
  388. "user.account.lock",
  389. "user.account.update_password",
  390. "group.user_membership.add",
  391. ]),
  392. "event.outcome": "success",
  393. "event.module": "okta",
  394. "event.dataset": "okta.system",
  395. "okta.event_type": randomFrom(["user.account.lock", "user.account.update_password"]),
  396. "okta.outcome.result": "SUCCESS",
  397. "user.name": randomFrom(FAKE_USERS),
  398. "user.email": `${randomFrom(FAKE_USERS)}@example.com`,
  399. "user.target.name": randomFrom(FAKE_USERS),
  400. "source.ip": randomFrom(FAKE_IPS),
  401. "data_stream.type": "logs",
  402. "data_stream.dataset": "okta.system",
  403. "data_stream.namespace": "default",
  404. }),
  405. ];
  406. return randomFrom(templates)();
  407. },
  408. };
  409. function generateEvents(packageName, count) {
  410. const generator = EVENT_GENERATORS[packageName];
  411. if (!generator) return [];
  412. const events = [];
  413. for (let i = 0; i < count; i++) {
  414. events.push(generator());
  415. }
  416. return events;
  417. }
  418. function getDataStreamIndex(event) {
  419. const type = event["data_stream.type"] || "logs";
  420. const dataset = event["data_stream.dataset"] || "generic";
  421. const namespace = event["data_stream.namespace"] || "default";
  422. return `${type}-${dataset}-${namespace}`;
  423. }
  424. async function bulkIndex(client, events) {
  425. if (!events.length) return { indexed: 0, errors: 0 };
  426. const body = [];
  427. for (const event of events) {
  428. // Ensure every event can be cleaned up via --cleanup, including attack scenarios.
  429. const tags = Array.isArray(event.tags) ? event.tags : [];
  430. const enriched = tags.includes(SAMPLE_DATA_MARKER) ? event : { ...event, tags: [...tags, SAMPLE_DATA_MARKER] };
  431. body.push({ create: { _index: getDataStreamIndex(enriched) } });
  432. body.push(enriched);
  433. }
  434. const result = await client.bulk({ body, refresh: "wait_for" });
  435. let indexed = 0;
  436. let errors = 0;
  437. if (result.items) {
  438. for (const item of result.items) {
  439. const op = item.create || item.index;
  440. if (op?.error) {
  441. errors++;
  442. } else {
  443. indexed++;
  444. }
  445. }
  446. }
  447. return { indexed, errors, firstError: result.items?.find((i) => (i.create || i.index)?.error) };
  448. }
  449. // ---------------------------------------------------------------------------
  450. // Attack simulation scenarios
  451. // Correlated event sequences designed to trigger prebuilt detection rules.
  452. // Each scenario returns an array of events with coherent timestamps, hosts,
  453. // users, and IPs that tell a recognizable attack story.
  454. // ---------------------------------------------------------------------------
  455. function sequentialTimestamps(count, startHoursBack = 2, intervalMinutes = 1) {
  456. const start = Date.now() - startHoursBack * 60 * 60 * 1000;
  457. return Array.from({ length: count }, (_, i) => new Date(start + i * intervalMinutes * 60 * 1000).toISOString());
  458. }
  459. const ATTACK_SCENARIOS = {
  460. /**
  461. * Windows credential access chain: failed logins → success → suspicious
  462. * process execution (mimikatz-like, encoded powershell, scheduled task).
  463. * Triggers: "Multiple Logon Failure from Same Source",
  464. * "Suspicious PowerShell Encoded Command",
  465. * "Persistence via Scheduled Task", "Credential Dumping".
  466. */
  467. windowsCredentialAccess() {
  468. const attackerIP = "198.51.100.77";
  469. const host = "db-prod-02";
  470. const user = "svc_backup";
  471. const ts = sequentialTimestamps(15, 1.5, 0.5);
  472. const events = [];
  473. for (let i = 0; i < 8; i++) {
  474. events.push({
  475. "@timestamp": ts[i],
  476. "ecs.version": ecsVersion(),
  477. "event.category": ["authentication"],
  478. "event.type": ["start"],
  479. "event.kind": "event",
  480. "event.code": "4625",
  481. "event.action": "logon-failed",
  482. "event.outcome": "failure",
  483. "event.module": "windows",
  484. "event.dataset": "windows.security",
  485. "event.provider": "Microsoft-Windows-Security-Auditing",
  486. "winlog.event_id": 4625,
  487. "winlog.channel": "Security",
  488. "host.name": host,
  489. "host.os.family": "windows",
  490. "host.os.type": "windows",
  491. "user.name": user,
  492. "user.domain": "CORP",
  493. "source.ip": attackerIP,
  494. "data_stream.type": "logs",
  495. "data_stream.dataset": "windows.security",
  496. "data_stream.namespace": "default",
  497. });
  498. }
  499. events.push({
  500. "@timestamp": ts[8],
  501. "ecs.version": ecsVersion(),
  502. "event.category": ["authentication"],
  503. "event.type": ["start"],
  504. "event.kind": "event",
  505. "event.code": "4624",
  506. "event.action": "logged-in",
  507. "event.outcome": "success",
  508. "event.module": "windows",
  509. "event.dataset": "windows.security",
  510. "event.provider": "Microsoft-Windows-Security-Auditing",
  511. "winlog.event_id": 4624,
  512. "winlog.channel": "Security",
  513. "winlog.logon.type": "Network",
  514. "host.name": host,
  515. "host.os.family": "windows",
  516. "host.os.type": "windows",
  517. "user.name": user,
  518. "user.domain": "CORP",
  519. "source.ip": attackerIP,
  520. "data_stream.type": "logs",
  521. "data_stream.dataset": "windows.security",
  522. "data_stream.namespace": "default",
  523. });
  524. const suspiciousProcesses = [
  525. {
  526. name: "powershell.exe",
  527. executable: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  528. args: ["-EncodedCommand", "SQBuAHYAbwBrAGUALQBXAGUAYgBSAGUAcQB1AGUAcwB0AA=="],
  529. parent: "cmd.exe",
  530. code: "4688",
  531. },
  532. {
  533. name: "schtasks.exe",
  534. executable: "C:\\Windows\\System32\\schtasks.exe",
  535. args: ["/create", "/tn", "SystemUpdate", "/tr", "C:\\Temp\\update.exe", "/sc", "onlogon"],
  536. parent: "powershell.exe",
  537. code: "4688",
  538. },
  539. {
  540. name: "procdump.exe",
  541. executable: "C:\\Temp\\procdump.exe",
  542. args: ["-ma", "lsass.exe", "C:\\Temp\\lsass.dmp"],
  543. parent: "cmd.exe",
  544. code: "4688",
  545. },
  546. {
  547. name: "reg.exe",
  548. executable: "C:\\Windows\\System32\\reg.exe",
  549. args: ["save", "HKLM\\SAM", "C:\\Temp\\sam.hiv"],
  550. parent: "cmd.exe",
  551. code: "4688",
  552. },
  553. ];
  554. suspiciousProcesses.forEach((proc, i) => {
  555. events.push({
  556. "@timestamp": ts[9 + i],
  557. "ecs.version": ecsVersion(),
  558. "event.category": ["process"],
  559. "event.type": ["start"],
  560. "event.kind": "event",
  561. "event.code": proc.code,
  562. "event.action": "created-process",
  563. "event.module": "windows",
  564. "event.dataset": "windows.security",
  565. "event.provider": "Microsoft-Windows-Security-Auditing",
  566. "winlog.event_id": parseInt(proc.code, 10),
  567. "winlog.channel": "Security",
  568. "host.name": host,
  569. "host.os.family": "windows",
  570. "host.os.type": "windows",
  571. "process.name": proc.name,
  572. "process.executable": proc.executable,
  573. "process.args": proc.args,
  574. "process.command_line": `${proc.executable} ${proc.args.join(" ")}`,
  575. "process.pid": randomInt(1000, 50000),
  576. "process.parent.name": proc.parent,
  577. "process.parent.pid": randomInt(500, 5000),
  578. "user.name": user,
  579. "user.domain": "CORP",
  580. "data_stream.type": "logs",
  581. "data_stream.dataset": "windows.security",
  582. "data_stream.namespace": "default",
  583. });
  584. });
  585. return events;
  586. },
  587. /**
  588. * AWS IAM attack chain: console login from unusual geo → new IAM user →
  589. * attach admin policy → create access keys → disable CloudTrail.
  590. * Triggers: "AWS Console Login Without MFA", "AWS IAM User Created",
  591. * "AWS CloudTrail Logging Disabled", "Privilege Escalation".
  592. */
  593. awsIAMEscalation() {
  594. const attackerIP = "203.0.113.99";
  595. const awsAcct = "123456789012";
  596. const user = "compromised-dev";
  597. const ts = sequentialTimestamps(8, 1, 2);
  598. const events = [];
  599. events.push({
  600. "@timestamp": ts[0],
  601. "ecs.version": ecsVersion(),
  602. "event.category": ["authentication"],
  603. "event.type": ["info"],
  604. "event.kind": "event",
  605. "event.action": "ConsoleLogin",
  606. "event.outcome": "success",
  607. "event.module": "aws",
  608. "event.dataset": "aws.cloudtrail",
  609. "cloud.provider": "aws",
  610. "cloud.account.id": awsAcct,
  611. "cloud.region": "us-east-1",
  612. "aws.cloudtrail.event_type": "AwsConsoleSignIn",
  613. "aws.cloudtrail.additional_eventdata.MFAUsed": "No",
  614. "aws.cloudtrail.response_elements": "ConsoleLogin",
  615. "user.name": user,
  616. "source.ip": attackerIP,
  617. "source.geo.country_iso_code": "RU",
  618. "user_agent.original": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
  619. "data_stream.type": "logs",
  620. "data_stream.dataset": "aws.cloudtrail",
  621. "data_stream.namespace": "default",
  622. });
  623. events.push({
  624. "@timestamp": ts[1],
  625. "ecs.version": ecsVersion(),
  626. "event.category": ["iam"],
  627. "event.type": ["creation"],
  628. "event.kind": "event",
  629. "event.action": "CreateUser",
  630. "event.outcome": "success",
  631. "event.module": "aws",
  632. "event.dataset": "aws.cloudtrail",
  633. "cloud.provider": "aws",
  634. "cloud.account.id": awsAcct,
  635. "cloud.region": "us-east-1",
  636. "aws.cloudtrail.event_type": "AwsApiCall",
  637. "user.name": user,
  638. "user.target.name": "backdoor-admin",
  639. "source.ip": attackerIP,
  640. "data_stream.type": "logs",
  641. "data_stream.dataset": "aws.cloudtrail",
  642. "data_stream.namespace": "default",
  643. });
  644. events.push({
  645. "@timestamp": ts[2],
  646. "ecs.version": ecsVersion(),
  647. "event.category": ["iam"],
  648. "event.type": ["change"],
  649. "event.kind": "event",
  650. "event.action": "AttachUserPolicy",
  651. "event.outcome": "success",
  652. "event.module": "aws",
  653. "event.dataset": "aws.cloudtrail",
  654. "cloud.provider": "aws",
  655. "cloud.account.id": awsAcct,
  656. "cloud.region": "us-east-1",
  657. "aws.cloudtrail.event_type": "AwsApiCall",
  658. "aws.cloudtrail.request_parameters":
  659. '{"policyArn":"arn:aws:iam::aws:policy/AdministratorAccess","userName":"backdoor-admin"}',
  660. "user.name": user,
  661. "source.ip": attackerIP,
  662. "data_stream.type": "logs",
  663. "data_stream.dataset": "aws.cloudtrail",
  664. "data_stream.namespace": "default",
  665. });
  666. events.push({
  667. "@timestamp": ts[3],
  668. "ecs.version": ecsVersion(),
  669. "event.category": ["iam"],
  670. "event.type": ["creation"],
  671. "event.kind": "event",
  672. "event.action": "CreateAccessKey",
  673. "event.outcome": "success",
  674. "event.module": "aws",
  675. "event.dataset": "aws.cloudtrail",
  676. "cloud.provider": "aws",
  677. "cloud.account.id": awsAcct,
  678. "cloud.region": "us-east-1",
  679. "aws.cloudtrail.event_type": "AwsApiCall",
  680. "user.name": user,
  681. "user.target.name": "backdoor-admin",
  682. "source.ip": attackerIP,
  683. "data_stream.type": "logs",
  684. "data_stream.dataset": "aws.cloudtrail",
  685. "data_stream.namespace": "default",
  686. });
  687. events.push({
  688. "@timestamp": ts[4],
  689. "ecs.version": ecsVersion(),
  690. "event.category": ["configuration"],
  691. "event.type": ["change"],
  692. "event.kind": "event",
  693. "event.action": "StopLogging",
  694. "event.outcome": "success",
  695. "event.module": "aws",
  696. "event.dataset": "aws.cloudtrail",
  697. "cloud.provider": "aws",
  698. "cloud.account.id": awsAcct,
  699. "cloud.region": "us-east-1",
  700. "aws.cloudtrail.event_type": "AwsApiCall",
  701. "user.name": user,
  702. "source.ip": attackerIP,
  703. "data_stream.type": "logs",
  704. "data_stream.dataset": "aws.cloudtrail",
  705. "data_stream.namespace": "default",
  706. });
  707. events.push({
  708. "@timestamp": ts[5],
  709. "ecs.version": ecsVersion(),
  710. "event.category": ["configuration"],
  711. "event.type": ["change"],
  712. "event.kind": "event",
  713. "event.action": "DeleteTrail",
  714. "event.outcome": "success",
  715. "event.module": "aws",
  716. "event.dataset": "aws.cloudtrail",
  717. "cloud.provider": "aws",
  718. "cloud.account.id": awsAcct,
  719. "cloud.region": "us-east-1",
  720. "aws.cloudtrail.event_type": "AwsApiCall",
  721. "user.name": user,
  722. "source.ip": attackerIP,
  723. "data_stream.type": "logs",
  724. "data_stream.dataset": "aws.cloudtrail",
  725. "data_stream.namespace": "default",
  726. });
  727. return events;
  728. },
  729. /**
  730. * Okta account takeover: credential stuffing → MFA fatigue → session
  731. * hijack → admin privilege escalation → policy change.
  732. * Triggers: "Okta Brute Force", "Okta MFA Bombing",
  733. * "Okta Admin Role Assigned", "Okta Policy Rule Modified".
  734. */
  735. oktaAccountTakeover() {
  736. const attackerIP = "198.51.100.88";
  737. const victim = "charlie";
  738. const ts = sequentialTimestamps(20, 1, 0.2);
  739. const events = [];
  740. for (let i = 0; i < 10; i++) {
  741. events.push({
  742. "@timestamp": ts[i],
  743. "ecs.version": ecsVersion(),
  744. "event.category": ["authentication"],
  745. "event.type": ["start"],
  746. "event.kind": "event",
  747. "event.action": "user.session.start",
  748. "event.outcome": "failure",
  749. "event.module": "okta",
  750. "event.dataset": "okta.system",
  751. "okta.event_type": "user.session.start",
  752. "okta.display_message": "User login to Okta",
  753. "okta.outcome.result": "FAILURE",
  754. "okta.outcome.reason": "INVALID_CREDENTIALS",
  755. "user.name": victim,
  756. "user.email": `${victim}@example.com`,
  757. "source.ip": attackerIP,
  758. "user_agent.original": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
  759. "data_stream.type": "logs",
  760. "data_stream.dataset": "okta.system",
  761. "data_stream.namespace": "default",
  762. });
  763. }
  764. for (let i = 0; i < 5; i++) {
  765. events.push({
  766. "@timestamp": ts[10 + i],
  767. "ecs.version": ecsVersion(),
  768. "event.category": ["authentication"],
  769. "event.type": ["info"],
  770. "event.kind": "event",
  771. "event.action": "user.authentication.auth_via_mfa",
  772. "event.outcome": "failure",
  773. "event.module": "okta",
  774. "event.dataset": "okta.system",
  775. "okta.event_type": "user.authentication.auth_via_mfa",
  776. "okta.display_message": "Authentication of user via MFA",
  777. "okta.outcome.result": "FAILURE",
  778. "okta.outcome.reason": "INVALID_CREDENTIALS",
  779. "user.name": victim,
  780. "user.email": `${victim}@example.com`,
  781. "source.ip": attackerIP,
  782. "data_stream.type": "logs",
  783. "data_stream.dataset": "okta.system",
  784. "data_stream.namespace": "default",
  785. });
  786. }
  787. events.push({
  788. "@timestamp": ts[15],
  789. "ecs.version": ecsVersion(),
  790. "event.category": ["authentication"],
  791. "event.type": ["start"],
  792. "event.kind": "event",
  793. "event.action": "user.session.start",
  794. "event.outcome": "success",
  795. "event.module": "okta",
  796. "event.dataset": "okta.system",
  797. "okta.event_type": "user.session.start",
  798. "okta.display_message": "User login to Okta",
  799. "okta.outcome.result": "SUCCESS",
  800. "user.name": victim,
  801. "user.email": `${victim}@example.com`,
  802. "source.ip": attackerIP,
  803. "user_agent.original": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
  804. "data_stream.type": "logs",
  805. "data_stream.dataset": "okta.system",
  806. "data_stream.namespace": "default",
  807. });
  808. events.push({
  809. "@timestamp": ts[16],
  810. "ecs.version": ecsVersion(),
  811. "event.category": ["iam"],
  812. "event.type": ["change"],
  813. "event.kind": "event",
  814. "event.action": "user.account.privilege.grant",
  815. "event.outcome": "success",
  816. "event.module": "okta",
  817. "event.dataset": "okta.system",
  818. "okta.event_type": "user.account.privilege.grant",
  819. "okta.display_message": "Granted admin role to user",
  820. "okta.outcome.result": "SUCCESS",
  821. "user.name": victim,
  822. "user.email": `${victim}@example.com`,
  823. "user.target.name": victim,
  824. "source.ip": attackerIP,
  825. "data_stream.type": "logs",
  826. "data_stream.dataset": "okta.system",
  827. "data_stream.namespace": "default",
  828. });
  829. events.push({
  830. "@timestamp": ts[17],
  831. "ecs.version": ecsVersion(),
  832. "event.category": ["configuration"],
  833. "event.type": ["change"],
  834. "event.kind": "event",
  835. "event.action": "policy.rule.update",
  836. "event.outcome": "success",
  837. "event.module": "okta",
  838. "event.dataset": "okta.system",
  839. "okta.event_type": "policy.rule.update",
  840. "okta.display_message": "Update policy rule",
  841. "okta.outcome.result": "SUCCESS",
  842. "user.name": victim,
  843. "user.email": `${victim}@example.com`,
  844. "source.ip": attackerIP,
  845. "data_stream.type": "logs",
  846. "data_stream.dataset": "okta.system",
  847. "data_stream.namespace": "default",
  848. });
  849. return events;
  850. },
  851. ransomwareChain() {
  852. const host = "app-staging-03";
  853. const hostId = "a1b2c3d4-host-staging-03";
  854. const agentId = "e5f6a7b8-agent-staging-03";
  855. const user = "dave";
  856. const c2IP = "203.0.113.42";
  857. const ts = sequentialTimestamps(16, 1, 0.3);
  858. const events = [];
  859. const eid = RANSOMWARE_ENTITY_IDS;
  860. const hostFields = {
  861. "host.name": host,
  862. "host.id": hostId,
  863. "host.os.type": "windows",
  864. "host.os.family": "windows",
  865. "agent.id": agentId,
  866. "agent.type": "endpoint",
  867. };
  868. const ep = (dataset) => ({
  869. "ecs.version": ecsVersion(),
  870. "event.kind": "event",
  871. "event.module": "endpoint",
  872. "event.dataset": `endpoint.events.${dataset}`,
  873. ...hostFields,
  874. "user.name": user,
  875. "data_stream.type": "logs",
  876. "data_stream.dataset": `endpoint.events.${dataset}`,
  877. "data_stream.namespace": "default",
  878. });
  879. events.push({
  880. "@timestamp": ts[0],
  881. ...ep("process"),
  882. "event.category": ["process"],
  883. "event.type": ["start"],
  884. "event.action": "exec",
  885. "process.name": "WINWORD.EXE",
  886. "process.executable": "C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE",
  887. "process.entity_id": eid.winword,
  888. "process.pid": 4120,
  889. "process.args": ["C:\\Users\\dave\\Downloads\\Invoice_Q4_2025.docm"],
  890. "process.command_line": "WINWORD.EXE C:\\Users\\dave\\Downloads\\Invoice_Q4_2025.docm",
  891. "process.parent.name": "explorer.exe",
  892. "process.parent.entity_id": eid.explorer,
  893. "process.parent.pid": 1024,
  894. });
  895. events.push({
  896. "@timestamp": ts[1],
  897. ...ep("process"),
  898. "event.category": ["process"],
  899. "event.type": ["start"],
  900. "event.action": "exec",
  901. "process.name": "powershell.exe",
  902. "process.executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  903. "process.entity_id": eid.powershell,
  904. "process.pid": 7832,
  905. "process.args": [
  906. "-WindowStyle",
  907. "Hidden",
  908. "-EncodedCommand",
  909. "SQBFAFgAKAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkA",
  910. ],
  911. "process.command_line": "powershell.exe -WindowStyle Hidden -EncodedCommand SQBFAFgA...",
    • Note: Quoted as data in code: Runs code it decodes (base64, hex, character codes, reversed text or a compressed blob), which hides what runs from a reader
    • Line 966 on GitHub
  912. "process.parent.name": "WINWORD.EXE",
  913. "process.parent.entity_id": eid.winword,
  914. "process.parent.pid": 4120,
  915. });
  916. events.push({
  917. "@timestamp": ts[2],
  918. ...ep("network"),
  919. "event.category": ["network"],
  920. "event.type": ["connection", "start"],
  921. "event.action": "connection_attempted",
  922. "process.name": "powershell.exe",
  923. "process.entity_id": eid.powershell,
  924. "process.pid": 7832,
  925. "source.ip": "10.0.0.50",
  926. "destination.ip": c2IP,
  927. "destination.port": 443,
  928. "network.transport": "tcp",
  929. "network.direction": "egress",
  930. });
  931. events.push({
  932. "@timestamp": ts[3],
  933. ...ep("process"),
  934. "event.category": ["process"],
  935. "event.type": ["start"],
  936. "event.action": "exec",
  937. "process.name": "rundll32.exe",
  938. "process.executable": "C:\\Windows\\System32\\rundll32.exe",
  939. "process.entity_id": eid.rundll32,
  940. "process.pid": 9104,
  941. "process.args": ["C:\\Windows\\System32\\comsvcs.dll,MiniDump", "656", "C:\\ProgramData\\lsass.dmp", "full"],
  942. "process.command_line":
  943. "rundll32.exe C:\\Windows\\System32\\comsvcs.dll,MiniDump 656 C:\\ProgramData\\lsass.dmp full",
  944. "process.parent.name": "powershell.exe",
  945. "process.parent.entity_id": eid.powershell,
  946. "process.parent.pid": 7832,
  947. });
  948. events.push({
  949. "@timestamp": ts[4],
  950. ...ep("file"),
  951. "event.category": ["file"],
  952. "event.type": ["creation"],
  953. "event.action": "creation",
  954. "file.name": "beacon.exe",
  955. "file.path": "C:\\ProgramData\\beacon.exe",
  956. "file.extension": "exe",
  957. "process.name": "powershell.exe",
  958. "process.entity_id": eid.powershell,
  959. "process.pid": 7832,
  960. });
  961. events.push({
  962. "@timestamp": ts[5],
  963. ...ep("process"),
  964. "event.category": ["process"],
  965. "event.type": ["start"],
  966. "event.action": "exec",
  967. "process.name": "beacon.exe",
  968. "process.executable": "C:\\ProgramData\\beacon.exe",
  969. "process.entity_id": eid.beacon,
  970. "process.pid": 11240,
  971. "process.parent.name": "powershell.exe",
  972. "process.parent.entity_id": eid.powershell,
  973. "process.parent.pid": 7832,
  974. });
  975. events.push({
  976. "@timestamp": ts[6],
  977. ...ep("process"),
  978. "event.category": ["process"],
  979. "event.type": ["start"],
  980. "event.action": "exec",
  981. "process.name": "vssadmin.exe",
  982. "process.executable": "C:\\Windows\\System32\\vssadmin.exe",
  983. "process.entity_id": eid.vssadmin,
  984. "process.pid": 12004,
  985. "process.args": ["delete", "shadows", "/all", "/quiet"],
  986. "process.command_line": "vssadmin.exe delete shadows /all /quiet",
  987. "process.parent.name": "beacon.exe",
  988. "process.parent.entity_id": eid.beacon,
  989. "process.parent.pid": 11240,
  990. });
  991. events.push({
  992. "@timestamp": ts[7],
  993. ...ep("process"),
  994. "event.category": ["process"],
  995. "event.type": ["start"],
  996. "event.action": "exec",
  997. "process.name": "bcdedit.exe",
  998. "process.executable": "C:\\Windows\\System32\\bcdedit.exe",
  999. "process.entity_id": eid.bcdedit,
  1000. "process.pid": 12200,
  1001. "process.args": ["/set", "{default}", "recoveryenabled", "no"],
  1002. "process.command_line": "bcdedit.exe /set {default} recoveryenabled no",
  1003. "process.parent.name": "beacon.exe",
  1004. "process.parent.entity_id": eid.beacon,
  1005. "process.parent.pid": 11240,
  1006. });
  1007. const encryptedFiles = [
  1008. "document1.docx.encrypted",
  1009. "budget.xlsx.encrypted",
  1010. "presentation.pptx.encrypted",
  1011. "database.mdb.encrypted",
  1012. "report.pdf.encrypted",
  1013. ];
  1014. encryptedFiles.forEach((fname, i) => {
  1015. events.push({
  1016. "@timestamp": ts[8 + i],
  1017. ...ep("file"),
  1018. "event.category": ["file"],
  1019. "event.type": ["change"],
  1020. "event.action": "modification",
  1021. "file.name": fname,
  1022. "file.path": `C:\\Users\\dave\\Documents\\${fname}`,
  1023. "file.extension": "encrypted",
  1024. "process.name": "beacon.exe",
  1025. "process.entity_id": eid.beacon,
  1026. "process.pid": 11240,
  1027. });
  1028. });
  1029. events.push({
  1030. "@timestamp": ts[13],
  1031. ...ep("file"),
  1032. "event.category": ["file"],
  1033. "event.type": ["creation"],
  1034. "event.action": "creation",
  1035. "file.name": "README_RESTORE_FILES.txt",
  1036. "file.path": "C:\\Users\\dave\\Desktop\\README_RESTORE_FILES.txt",
  1037. "file.extension": "txt",
  1038. "process.name": "beacon.exe",
  1039. "process.entity_id": eid.beacon,
  1040. "process.pid": 11240,
  1041. });
  1042. return events;
  1043. },
  1044. };
  1045. export const AVAILABLE_SCENARIOS = Object.keys(ATTACK_SCENARIOS);
  1046. export async function runAttackScenarios(scenarioNames) {
  1047. const client = createClient();
  1048. await detectClusterVersions(client);
  1049. const results = {};
  1050. let totalIndexed = 0;
  1051. let totalErrors = 0;
  1052. const toRun = scenarioNames.length ? scenarioNames : [...AVAILABLE_SCENARIOS];
  1053. for (const name of toRun) {
  1054. const generator = ATTACK_SCENARIOS[name];
  1055. if (!generator) {
  1056. results[name] = { status: "skipped", reason: "unknown scenario" };
  1057. continue;
  1058. }
  1059. const events = generator();
  1060. console.log(` Scenario "${name}": ${events.length} correlated events...`);
  1061. try {
  1062. const { indexed, errors, firstError } = await bulkIndex(client, events);
  1063. totalIndexed += indexed;
  1064. totalErrors += errors;
  1065. results[name] = { status: "ok", indexed, errors, eventCount: events.length };
  1066. if (errors > 0 && firstError) {
  1067. const errDetail = (firstError.create || firstError.index)?.error;
  1068. console.error(
  1069. ` Warning: ${errors} event(s) failed. First error: ${errDetail?.reason || JSON.stringify(errDetail)}`,
  1070. );
  1071. }
  1072. console.log(` ${indexed} indexed, ${errors} failed`);
  1073. } catch (e) {
  1074. totalErrors += events.length;
  1075. results[name] = { status: "error", error: e.message };
  1076. console.error(` Error: ${e.message}`);
  1077. }
  1078. }
  1079. await client.close();
  1080. return { results, totalIndexed, totalErrors };
  1081. }
  1082. // ---------------------------------------------------------------------------
  1083. // Synthetic alert documents
  1084. // Indexes directly into .alerts-security.alerts-default so that the Alerts
  1085. // tab, Attack Discovery, and case workflows light up immediately without
  1086. // waiting for detection rules to fire.
  1087. // ---------------------------------------------------------------------------
  1088. function alertId() {
  1089. return crypto.randomUUID();
  1090. }
  1091. const RANSOMWARE_ENTITY_IDS = {
  1092. explorer: "d0a1b2c3-0001-4aaa-bbbb-000000000001",
  1093. winword: "d0a1b2c3-0002-4aaa-bbbb-000000000002",
  1094. powershell: "d0a1b2c3-0003-4aaa-bbbb-000000000003",
  1095. rundll32: "d0a1b2c3-0004-4aaa-bbbb-000000000004",
  1096. beacon: "d0a1b2c3-0005-4aaa-bbbb-000000000005",
  1097. vssadmin: "d0a1b2c3-0006-4aaa-bbbb-000000000006",
  1098. bcdedit: "d0a1b2c3-0007-4aaa-bbbb-000000000007",
  1099. };
  1100. function buildAlert({
  1101. timestamp,
  1102. ruleName,
  1103. ruleId,
  1104. ruleType = "query",
  1105. severity = "high",
  1106. riskScore = 73,
  1107. tactics = [],
  1108. techniques = [],
  1109. sourceEvent = {},
  1110. description = "",
  1111. }) {
  1112. const now = timestamp || new Date().toISOString();
  1113. const originalTime = sourceEvent["@timestamp"] || now;
  1114. const threat = [];
  1115. if (tactics.length || techniques.length) {
  1116. const tacticEntries = tactics.map((t) => ({
  1117. id: t.id,
  1118. name: t.name,
  1119. reference: `https://attack.mitre.org/tactics/${t.id}/`,
  1120. }));
  1121. const techniqueEntries = techniques.map((t) => ({
  1122. id: t.id,
  1123. name: t.name,
  1124. reference: `https://attack.mitre.org/techniques/${t.id.replace(".", "/")}/`,
  1125. ...(t.subtechnique
  1126. ? {
  1127. subtechnique: [
  1128. {
  1129. id: t.subtechnique.id,
  1130. name: t.subtechnique.name,
  1131. reference: `https://attack.mitre.org/techniques/${t.subtechnique.id.replace(".", "/")}/`,
  1132. },
  1133. ],
  1134. }
  1135. : {}),
  1136. }));
  1137. threat.push({
  1138. framework: "MITRE ATT&CK",
  1139. ...(tacticEntries.length ? { tactic: tacticEntries[0] } : {}),
  1140. ...(techniqueEntries.length ? { technique: techniqueEntries } : {}),
  1141. });
  1142. }
  1143. const hostName = sourceEvent["host.name"] || "";
  1144. const userName = sourceEvent["user.name"] || "";
  1145. const processName = sourceEvent["process.name"] || "";
  1146. const sourceIp = sourceEvent["source.ip"] || "";
  1147. const message = sourceEvent.message || description;
  1148. const reasonParts = [];
  1149. if (processName) reasonParts.push(`process event with process ${processName}`);
  1150. else if (sourceEvent["event.action"]) reasonParts.push(`${sourceEvent["event.action"]} event`);
  1151. else reasonParts.push(ruleName);
  1152. if (userName) reasonParts.push(`by ${userName}`);
  1153. if (hostName) reasonParts.push(`on ${hostName}`);
  1154. if (sourceIp) reasonParts.push(`from ${sourceIp}`);
  1155. const reason = reasonParts.join(", ");
  1156. const ruleUuid = alertId();
  1157. const docId = alertId();
  1158. const executionUuid = alertId();
  1159. const ancestorId = alertId();
  1160. const alert = {
  1161. "@timestamp": now,
  1162. ecs: { version: stackVersion() },
  1163. event: {
  1164. kind: "signal",
  1165. category: sourceEvent["event.category"] || ["threat"],
  1166. type: sourceEvent["event.type"] || ["indicator"],
  1167. action: sourceEvent["event.action"] || "",
  1168. module: sourceEvent["event.module"] || "",
  1169. dataset: sourceEvent["event.dataset"] || "",
  1170. outcome: sourceEvent["event.outcome"] || "",
  1171. },
  1172. message,
  1173. kibana: {
  1174. alert: {
  1175. rule: {
  1176. name: ruleName,
  1177. rule_id: ruleId,
  1178. uuid: ruleUuid,
  1179. rule_type_id: "siem.queryRule",
  1180. type: ruleType,
  1181. category: "Custom Query Rule",
  1182. description,
  1183. producer: "siem",
  1184. consumer: "siem",
  1185. tags: ["Sample Data", "Attack Simulation"],
  1186. revision: 1,
  1187. parameters: {},
  1188. execution: { uuid: executionUuid },
  1189. },
  1190. status: "active",
  1191. workflow_status: "open",
  1192. workflow_tags: [],
  1193. severity,
  1194. risk_score: riskScore,
  1195. depth: 1,
  1196. reason,
  1197. original_time: originalTime,
  1198. uuid: docId,
  1199. url: "",
  1200. start: now,
  1201. time_range: { gte: now },
  1202. ancestors: [
  1203. {
  1204. id: ancestorId,
  1205. type: "event",
  1206. index: `logs-${sourceEvent["event.dataset"] || "generic"}-default`,
  1207. depth: 0,
  1208. },
  1209. ],
  1210. },
  1211. space_ids: ["default"],
  1212. version: fullStackVersion(),
  1213. },
  1214. ...(threat.length ? { threat } : {}),
  1215. };
  1216. if (hostName) {
  1217. alert.host = {
  1218. name: hostName,
  1219. ...(sourceEvent["host.id"] ? { id: sourceEvent["host.id"] } : {}),
  1220. ...(sourceEvent["host.os.family"]
  1221. ? { os: { family: sourceEvent["host.os.family"], type: sourceEvent["host.os.type"] || "" } }
  1222. : {}),
  1223. };
  1224. }
  1225. if (sourceEvent["agent.id"]) {
  1226. alert.agent = {
  1227. id: sourceEvent["agent.id"],
  1228. ...(sourceEvent["agent.type"] ? { type: sourceEvent["agent.type"] } : {}),
  1229. };
  1230. }
  1231. if (userName) {
  1232. alert.user = { name: userName, ...(sourceEvent["user.domain"] ? { domain: sourceEvent["user.domain"] } : {}) };
  1233. }
  1234. if (sourceIp) {
  1235. alert.source = {
  1236. ip: sourceIp,
  1237. ...(sourceEvent["source.geo.country_iso_code"]
  1238. ? { geo: { country_iso_code: sourceEvent["source.geo.country_iso_code"] } }
  1239. : {}),
  1240. };
  1241. }
  1242. if (sourceEvent["destination.ip"]) {
  1243. alert.destination = {
  1244. ip: sourceEvent["destination.ip"],
  1245. ...(sourceEvent["destination.port"] ? { port: sourceEvent["destination.port"] } : {}),
  1246. };
  1247. }
  1248. if (processName || sourceEvent["process.executable"]) {
  1249. const parent = {};
  1250. if (sourceEvent["process.parent.name"]) parent.name = sourceEvent["process.parent.name"];
  1251. if (sourceEvent["process.parent.entity_id"]) parent.entity_id = sourceEvent["process.parent.entity_id"];
  1252. if (sourceEvent["process.parent.pid"]) parent.pid = sourceEvent["process.parent.pid"];
  1253. alert.process = {
  1254. ...(processName ? { name: processName } : {}),
  1255. ...(sourceEvent["process.entity_id"] ? { entity_id: sourceEvent["process.entity_id"] } : {}),
  1256. ...(sourceEvent["process.pid"] ? { pid: sourceEvent["process.pid"] } : {}),
  1257. ...(sourceEvent["process.executable"] ? { executable: sourceEvent["process.executable"] } : {}),
  1258. ...(sourceEvent["process.args"] ? { args: sourceEvent["process.args"] } : {}),
  1259. ...(sourceEvent["process.command_line"] ? { command_line: sourceEvent["process.command_line"] } : {}),
  1260. ...(Object.keys(parent).length ? { parent } : {}),
  1261. };
  1262. }
  1263. if (sourceEvent["file.name"] || sourceEvent["file.path"]) {
  1264. alert.file = {
  1265. ...(sourceEvent["file.name"] ? { name: sourceEvent["file.name"] } : {}),
  1266. ...(sourceEvent["file.path"] ? { path: sourceEvent["file.path"] } : {}),
  1267. };
  1268. }
  1269. if (sourceEvent["cloud.provider"]) {
  1270. alert.cloud = {
  1271. provider: sourceEvent["cloud.provider"],
  1272. ...(sourceEvent["cloud.account.id"] ? { account: { id: sourceEvent["cloud.account.id"] } } : {}),
  1273. ...(sourceEvent["cloud.region"] ? { region: sourceEvent["cloud.region"] } : {}),
  1274. };
  1275. }
  1276. return alert;
  1277. }
  1278. const ALERT_SCENARIOS = {
  1279. credentialAccessAlerts() {
  1280. const host = "db-prod-02";
  1281. const user = "svc_backup";
  1282. const attackerIP = "198.51.100.77";
  1283. const ts = sequentialTimestamps(6, 1.5, 0.5);
  1284. return [
  1285. buildAlert({
  1286. timestamp: ts[0],
  1287. ruleName: "Windows Logon Brute Force",
  1288. ruleId: "sample-windows-brute-force",
  1289. severity: "high",
  1290. riskScore: 73,
  1291. description: "Multiple failed Windows logon attempts from a single source address.",
  1292. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1293. techniques: [{ id: "T1110", name: "Brute Force" }],
  1294. sourceEvent: {
  1295. "@timestamp": ts[0],
  1296. "event.category": ["authentication"],
  1297. "event.action": "logon-failed",
  1298. "event.outcome": "failure",
  1299. "event.module": "windows",
  1300. "event.dataset": "windows.security",
  1301. "host.name": host,
  1302. "host.os.family": "windows",
  1303. "host.os.type": "windows",
  1304. "user.name": user,
  1305. "user.domain": "CORP",
  1306. "source.ip": attackerIP,
  1307. },
  1308. }),
  1309. buildAlert({
  1310. timestamp: ts[1],
  1311. ruleName: "Suspicious PowerShell Encoded Command",
  1312. ruleId: "sample-encoded-powershell",
  1313. severity: "high",
  1314. riskScore: 73,
  1315. description:
  1316. "PowerShell was executed with an encoded command argument, commonly used to obfuscate malicious scripts.",
  1317. tactics: [{ id: "TA0002", name: "Execution" }],
  1318. techniques: [
  1319. {
  1320. id: "T1059",
  1321. name: "Command and Scripting Interpreter",
  1322. subtechnique: { id: "T1059.001", name: "PowerShell" },
  1323. },
  1324. ],
  1325. sourceEvent: {
  1326. "@timestamp": ts[1],
  1327. "event.category": ["process"],
  1328. "event.type": ["start"],
  1329. "event.action": "created-process",
  1330. "event.module": "windows",
  1331. "host.name": host,
  1332. "host.os.type": "windows",
  1333. "user.name": user,
  1334. "user.domain": "CORP",
  1335. "process.name": "powershell.exe",
  1336. "process.executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  1337. "process.args": ["-EncodedCommand", "SQBuAHYAbwBrAGUALQBXAGUAYgBSAGUAcQB1AGUAcwB0AA=="],
  1338. "process.command_line": "powershell.exe -EncodedCommand SQBuAHYAbwBrAGUALQBXAGUAYgBSAGUAcQB1AGUAcwB0AA==",
    • Note: Quoted as data in code: Runs code it decodes (base64, hex, character codes, reversed text or a compressed blob), which hides what runs from a reader
    • Line 1,423 on GitHub
  1339. "process.parent.name": "cmd.exe",
  1340. },
  1341. }),
  1342. buildAlert({
  1343. timestamp: ts[2],
  1344. ruleName: "Persistence via Scheduled Task",
  1345. ruleId: "sample-scheduled-task-persistence",
  1346. severity: "medium",
  1347. riskScore: 47,
  1348. description:
  1349. "A scheduled task was created that may be used for persistence by executing a suspicious binary on logon.",
  1350. tactics: [{ id: "TA0003", name: "Persistence" }],
  1351. techniques: [
  1352. { id: "T1053", name: "Scheduled Task/Job", subtechnique: { id: "T1053.005", name: "Scheduled Task" } },
  1353. ],
  1354. sourceEvent: {
  1355. "@timestamp": ts[2],
  1356. "event.category": ["process"],
  1357. "event.type": ["start"],
  1358. "event.action": "created-process",
  1359. "event.module": "windows",
  1360. "host.name": host,
  1361. "host.os.type": "windows",
  1362. "user.name": user,
  1363. "user.domain": "CORP",
  1364. "process.name": "schtasks.exe",
  1365. "process.executable": "C:\\Windows\\System32\\schtasks.exe",
  1366. "process.args": ["/create", "/tn", "SystemUpdate", "/tr", "C:\\Temp\\update.exe", "/sc", "onlogon"],
  1367. "process.command_line": "schtasks.exe /create /tn SystemUpdate /tr C:\\Temp\\update.exe /sc onlogon",
  1368. "process.parent.name": "powershell.exe",
  1369. },
  1370. }),
  1371. buildAlert({
  1372. timestamp: ts[3],
  1373. ruleName: "Credential Dumping - LSASS Memory Access",
  1374. ruleId: "sample-lsass-dump",
  1375. severity: "critical",
  1376. riskScore: 99,
  1377. description:
  1378. "A process attempted to dump LSASS memory, commonly used to extract credentials from a Windows system.",
  1379. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1380. techniques: [
  1381. { id: "T1003", name: "OS Credential Dumping", subtechnique: { id: "T1003.001", name: "LSASS Memory" } },
  1382. ],
  1383. sourceEvent: {
  1384. "@timestamp": ts[3],
  1385. "event.category": ["process"],
  1386. "event.type": ["start"],
  1387. "event.action": "created-process",
  1388. "event.module": "windows",
  1389. "host.name": host,
  1390. "host.os.type": "windows",
  1391. "user.name": user,
  1392. "user.domain": "CORP",
  1393. "process.name": "procdump.exe",
  1394. "process.executable": "C:\\Temp\\procdump.exe",
  1395. "process.args": ["-ma", "lsass.exe", "C:\\Temp\\lsass.dmp"],
  1396. "process.command_line": "procdump.exe -ma lsass.exe C:\\Temp\\lsass.dmp",
  1397. "process.parent.name": "cmd.exe",
  1398. },
  1399. }),
  1400. buildAlert({
  1401. timestamp: ts[4],
  1402. ruleName: "SAM Registry Hive Dumped",
  1403. ruleId: "sample-sam-dump",
  1404. severity: "critical",
  1405. riskScore: 99,
  1406. description: "The SAM registry hive was exported, which contains password hashes for local accounts.",
  1407. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1408. techniques: [
  1409. {
  1410. id: "T1003",
  1411. name: "OS Credential Dumping",
  1412. subtechnique: { id: "T1003.002", name: "Security Account Manager" },
  1413. },
  1414. ],
  1415. sourceEvent: {
  1416. "@timestamp": ts[4],
  1417. "event.category": ["process"],
  1418. "event.type": ["start"],
  1419. "event.action": "created-process",
  1420. "event.module": "windows",
  1421. "host.name": host,
  1422. "host.os.type": "windows",
  1423. "user.name": user,
  1424. "user.domain": "CORP",
  1425. "process.name": "reg.exe",
  1426. "process.executable": "C:\\Windows\\System32\\reg.exe",
  1427. "process.args": ["save", "HKLM\\SAM", "C:\\Temp\\sam.hiv"],
  1428. "process.command_line": "reg.exe save HKLM\\SAM C:\\Temp\\sam.hiv",
  1429. "process.parent.name": "cmd.exe",
  1430. },
  1431. }),
  1432. ];
  1433. },
  1434. awsEscalationAlerts() {
  1435. const attackerIP = "203.0.113.99";
  1436. const user = "compromised-dev";
  1437. const ts = sequentialTimestamps(5, 1, 2);
  1438. return [
  1439. buildAlert({
  1440. timestamp: ts[0],
  1441. ruleName: "AWS Console Login Without MFA",
  1442. ruleId: "sample-aws-no-mfa",
  1443. severity: "high",
  1444. riskScore: 73,
  1445. description:
  1446. "An AWS console login was performed without multi-factor authentication from an unusual geographic location.",
  1447. tactics: [{ id: "TA0001", name: "Initial Access" }],
  1448. techniques: [
  1449. { id: "T1078", name: "Valid Accounts", subtechnique: { id: "T1078.004", name: "Cloud Accounts" } },
  1450. ],
  1451. sourceEvent: {
  1452. "@timestamp": ts[0],
  1453. "event.category": ["authentication"],
  1454. "event.action": "ConsoleLogin",
  1455. "event.outcome": "success",
  1456. "event.module": "aws",
  1457. "event.dataset": "aws.cloudtrail",
  1458. "user.name": user,
  1459. "source.ip": attackerIP,
  1460. "source.geo.country_iso_code": "RU",
  1461. "cloud.provider": "aws",
  1462. "cloud.account.id": "123456789012",
  1463. "cloud.region": "us-east-1",
  1464. message: `AWS Console login without MFA by ${user} from Russia (${attackerIP})`,
  1465. },
  1466. }),
  1467. buildAlert({
  1468. timestamp: ts[1],
  1469. ruleName: "AWS IAM User Created",
  1470. ruleId: "sample-aws-user-created",
  1471. severity: "medium",
  1472. riskScore: 47,
  1473. description:
  1474. "A new IAM user was created, which may indicate an attacker establishing persistence in the cloud environment.",
  1475. tactics: [{ id: "TA0003", name: "Persistence" }],
  1476. techniques: [{ id: "T1136", name: "Create Account", subtechnique: { id: "T1136.003", name: "Cloud Account" } }],
  1477. sourceEvent: {
  1478. "@timestamp": ts[1],
  1479. "event.category": ["iam"],
  1480. "event.type": ["creation"],
  1481. "event.action": "CreateUser",
  1482. "event.outcome": "success",
  1483. "event.module": "aws",
  1484. "event.dataset": "aws.cloudtrail",
  1485. "user.name": user,
  1486. "source.ip": attackerIP,
  1487. "cloud.provider": "aws",
  1488. "cloud.account.id": "123456789012",
  1489. "cloud.region": "us-east-1",
  1490. message: `IAM user "backdoor-admin" created by ${user}`,
  1491. },
  1492. }),
  1493. buildAlert({
  1494. timestamp: ts[2],
  1495. ruleName: "AWS IAM Privilege Escalation - Admin Policy Attached",
  1496. ruleId: "sample-aws-priv-esc",
  1497. severity: "critical",
  1498. riskScore: 99,
  1499. description:
  1500. "AdministratorAccess policy was attached to a newly created IAM user, granting full access to the AWS account.",
  1501. tactics: [{ id: "TA0004", name: "Privilege Escalation" }],
  1502. techniques: [
  1503. {
  1504. id: "T1098",
  1505. name: "Account Manipulation",
  1506. subtechnique: { id: "T1098.003", name: "Additional Cloud Roles" },
  1507. },
  1508. ],
  1509. sourceEvent: {
  1510. "@timestamp": ts[2],
  1511. "event.category": ["iam"],
  1512. "event.type": ["change"],
  1513. "event.action": "AttachUserPolicy",
  1514. "event.outcome": "success",
  1515. "event.module": "aws",
  1516. "event.dataset": "aws.cloudtrail",
  1517. "user.name": user,
  1518. "source.ip": attackerIP,
  1519. "cloud.provider": "aws",
  1520. "cloud.account.id": "123456789012",
  1521. "cloud.region": "us-east-1",
  1522. message: "AdministratorAccess policy attached to user backdoor-admin",
  1523. },
  1524. }),
  1525. buildAlert({
  1526. timestamp: ts[3],
  1527. ruleName: "AWS CloudTrail Logging Disabled",
  1528. ruleId: "sample-aws-cloudtrail-disabled",
  1529. severity: "critical",
  1530. riskScore: 99,
  1531. description: "CloudTrail logging was stopped, which is a common technique to cover tracks after an intrusion.",
  1532. tactics: [{ id: "TA0005", name: "Defense Evasion" }],
  1533. techniques: [
  1534. {
  1535. id: "T1562",
  1536. name: "Impair Defenses",
  1537. subtechnique: { id: "T1562.008", name: "Disable or Modify Cloud Logs" },
  1538. },
  1539. ],
  1540. sourceEvent: {
  1541. "@timestamp": ts[3],
  1542. "event.category": ["configuration"],
  1543. "event.type": ["change"],
  1544. "event.action": "StopLogging",
  1545. "event.outcome": "success",
  1546. "event.module": "aws",
  1547. "event.dataset": "aws.cloudtrail",
  1548. "user.name": user,
  1549. "source.ip": attackerIP,
  1550. "cloud.provider": "aws",
  1551. "cloud.account.id": "123456789012",
  1552. "cloud.region": "us-east-1",
  1553. message: `CloudTrail logging stopped by ${user} from ${attackerIP}`,
  1554. },
  1555. }),
  1556. ];
  1557. },
  1558. oktaTakeoverAlerts() {
  1559. const attackerIP = "198.51.100.88";
  1560. const victim = "charlie";
  1561. const ts = sequentialTimestamps(4, 1, 0.5);
  1562. return [
  1563. buildAlert({
  1564. timestamp: ts[0],
  1565. ruleName: "Okta Brute Force Login Attempt",
  1566. ruleId: "sample-okta-brute-force",
  1567. severity: "high",
  1568. riskScore: 73,
  1569. description:
  1570. "Multiple failed Okta login attempts detected from a single source, indicating credential stuffing or brute force.",
  1571. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1572. techniques: [
  1573. { id: "T1110", name: "Brute Force", subtechnique: { id: "T1110.004", name: "Credential Stuffing" } },
  1574. ],
  1575. sourceEvent: {
  1576. "@timestamp": ts[0],
  1577. "event.category": ["authentication"],
  1578. "event.action": "user.session.start",
  1579. "event.outcome": "failure",
  1580. "event.module": "okta",
  1581. "event.dataset": "okta.system",
  1582. "user.name": victim,
  1583. "source.ip": attackerIP,
  1584. message: `10 failed Okta login attempts for ${victim} from ${attackerIP}`,
  1585. },
  1586. }),
  1587. buildAlert({
  1588. timestamp: ts[1],
  1589. ruleName: "Okta MFA Bombing / Push Fatigue Attack",
  1590. ruleId: "sample-okta-mfa-bombing",
  1591. severity: "high",
  1592. riskScore: 73,
  1593. description:
  1594. "Multiple MFA push notifications were rejected, suggesting an MFA fatigue attack where the attacker repeatedly triggers push notifications hoping the user accidentally approves.",
  1595. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1596. techniques: [{ id: "T1621", name: "Multi-Factor Authentication Request Generation" }],
  1597. sourceEvent: {
  1598. "@timestamp": ts[1],
  1599. "event.category": ["authentication"],
  1600. "event.action": "user.authentication.auth_via_mfa",
  1601. "event.outcome": "failure",
  1602. "event.module": "okta",
  1603. "event.dataset": "okta.system",
  1604. "user.name": victim,
  1605. "source.ip": attackerIP,
  1606. message: `5 MFA push rejections for ${victim} from ${attackerIP}`,
  1607. },
  1608. }),
  1609. buildAlert({
  1610. timestamp: ts[2],
  1611. ruleName: "Okta Administrator Role Assigned",
  1612. ruleId: "sample-okta-admin-grant",
  1613. severity: "critical",
  1614. riskScore: 99,
  1615. description: "An Okta administrator role was granted to a user account following a suspected account takeover.",
  1616. tactics: [{ id: "TA0004", name: "Privilege Escalation" }],
  1617. techniques: [
  1618. {
  1619. id: "T1098",
  1620. name: "Account Manipulation",
  1621. subtechnique: { id: "T1098.003", name: "Additional Cloud Roles" },
  1622. },
  1623. ],
  1624. sourceEvent: {
  1625. "@timestamp": ts[2],
  1626. "event.category": ["iam"],
  1627. "event.type": ["change"],
  1628. "event.action": "user.account.privilege.grant",
  1629. "event.outcome": "success",
  1630. "event.module": "okta",
  1631. "event.dataset": "okta.system",
  1632. "user.name": victim,
  1633. "source.ip": attackerIP,
  1634. message: `Admin role granted to ${victim} by ${victim} from ${attackerIP}`,
  1635. },
  1636. }),
  1637. buildAlert({
  1638. timestamp: ts[3],
  1639. ruleName: "Okta Security Policy Modified",
  1640. ruleId: "sample-okta-policy-change",
  1641. severity: "high",
  1642. riskScore: 73,
  1643. description: "An Okta security policy was modified, potentially weakening authentication requirements.",
  1644. tactics: [{ id: "TA0005", name: "Defense Evasion" }],
  1645. techniques: [
  1646. {
  1647. id: "T1562",
  1648. name: "Impair Defenses",
  1649. subtechnique: { id: "T1562.007", name: "Disable or Modify Cloud Firewall" },
  1650. },
  1651. ],
  1652. sourceEvent: {
  1653. "@timestamp": ts[3],
  1654. "event.category": ["configuration"],
  1655. "event.type": ["change"],
  1656. "event.action": "policy.rule.update",
  1657. "event.outcome": "success",
  1658. "event.module": "okta",
  1659. "event.dataset": "okta.system",
  1660. "user.name": victim,
  1661. "source.ip": attackerIP,
  1662. message: `Security policy modified by ${victim} from ${attackerIP}`,
  1663. },
  1664. }),
  1665. ];
  1666. },
  1667. ransomwareAlerts() {
  1668. const host = "app-staging-03";
  1669. const hostId = "a1b2c3d4-host-staging-03";
  1670. const agentId = "e5f6a7b8-agent-staging-03";
  1671. const user = "dave";
  1672. const eid = RANSOMWARE_ENTITY_IDS;
  1673. const ts = sequentialTimestamps(6, 1, 0.3);
  1674. const hostFields = {
  1675. "host.name": host,
  1676. "host.id": hostId,
  1677. "host.os.type": "windows",
  1678. "agent.id": agentId,
  1679. "agent.type": "endpoint",
  1680. };
  1681. return [
  1682. buildAlert({
  1683. timestamp: ts[0],
  1684. ruleName: "Suspicious Macro Execution from Office Application",
  1685. ruleId: "sample-macro-execution",
  1686. severity: "high",
  1687. riskScore: 73,
  1688. description:
  1689. "A Microsoft Office application spawned a suspicious child process, indicating possible macro-based malware delivery.",
  1690. tactics: [{ id: "TA0001", name: "Initial Access" }],
  1691. techniques: [
  1692. { id: "T1566", name: "Phishing", subtechnique: { id: "T1566.001", name: "Spearphishing Attachment" } },
  1693. ],
  1694. sourceEvent: {
  1695. "@timestamp": ts[0],
  1696. "event.category": ["process"],
  1697. "event.type": ["start"],
  1698. "event.action": "exec",
  1699. "event.module": "endpoint",
  1700. ...hostFields,
  1701. "user.name": user,
  1702. "process.name": "powershell.exe",
  1703. "process.entity_id": eid.powershell,
  1704. "process.pid": 7832,
  1705. "process.parent.name": "WINWORD.EXE",
  1706. "process.parent.entity_id": eid.winword,
  1707. "process.parent.pid": 4120,
  1708. "process.command_line": "powershell.exe -WindowStyle Hidden -EncodedCommand SQBFAFgA...",
    • Note: Quoted as data in code: Runs code it decodes (base64, hex, character codes, reversed text or a compressed blob), which hides what runs from a reader
    • Line 1,795 on GitHub
  1709. message: "PowerShell spawned by Word with encoded command - likely macro payload",
  1710. },
  1711. }),
  1712. buildAlert({
  1713. timestamp: ts[1],
  1714. ruleName: "C2 Beacon - Outbound Connection to Known Bad IP",
  1715. ruleId: "sample-c2-beacon",
  1716. severity: "critical",
  1717. riskScore: 99,
  1718. description: "A process established a network connection to a known command-and-control server.",
  1719. tactics: [{ id: "TA0011", name: "Command and Control" }],
  1720. techniques: [{ id: "T1071", name: "Application Layer Protocol" }],
  1721. sourceEvent: {
  1722. "@timestamp": ts[1],
  1723. "event.category": ["network"],
  1724. "event.action": "connection_attempted",
  1725. "event.module": "endpoint",
  1726. ...hostFields,
  1727. "user.name": user,
  1728. "process.name": "powershell.exe",
  1729. "process.entity_id": eid.powershell,
  1730. "process.pid": 7832,
  1731. "destination.ip": "203.0.113.42",
  1732. "destination.port": 443,
  1733. message: "Outbound C2 connection from PowerShell to 203.0.113.42:443",
  1734. },
  1735. }),
  1736. buildAlert({
  1737. timestamp: ts[2],
  1738. ruleName: "Credential Dumping via LSASS Memory",
  1739. ruleId: "sample-lsass-dump-ransomware",
  1740. severity: "critical",
  1741. riskScore: 99,
  1742. description:
  1743. "LSASS memory was dumped using rundll32.exe and comsvcs.dll, a common technique to extract credentials for lateral movement during a ransomware attack.",
  1744. tactics: [{ id: "TA0006", name: "Credential Access" }],
  1745. techniques: [
  1746. { id: "T1003", name: "OS Credential Dumping", subtechnique: { id: "T1003.001", name: "LSASS Memory" } },
  1747. ],
  1748. sourceEvent: {
  1749. "@timestamp": ts[2],
  1750. "event.category": ["process"],
  1751. "event.type": ["start"],
  1752. "event.action": "exec",
  1753. "event.module": "endpoint",
  1754. ...hostFields,
  1755. "user.name": user,
  1756. "process.name": "rundll32.exe",
  1757. "process.entity_id": eid.rundll32,
  1758. "process.pid": 9104,
  1759. "process.executable": "C:\\Windows\\System32\\rundll32.exe",
  1760. "process.args": ["C:\\Windows\\System32\\comsvcs.dll,MiniDump", "656", "C:\\ProgramData\\lsass.dmp", "full"],
  1761. "process.command_line":
  1762. "rundll32.exe C:\\Windows\\System32\\comsvcs.dll,MiniDump 656 C:\\ProgramData\\lsass.dmp full",
  1763. "process.parent.name": "powershell.exe",
  1764. "process.parent.entity_id": eid.powershell,
  1765. "process.parent.pid": 7832,
  1766. message: "LSASS memory dumped via rundll32/comsvcs.dll for credential theft",
  1767. },
  1768. }),
  1769. buildAlert({
  1770. timestamp: ts[3],
  1771. ruleName: "Volume Shadow Copy Deletion",
  1772. ruleId: "sample-shadow-copy-deletion",
  1773. severity: "critical",
  1774. riskScore: 99,
  1775. description:
  1776. "Volume shadow copies were deleted, a common precursor to ransomware encryption to prevent recovery.",
  1777. tactics: [{ id: "TA0040", name: "Impact" }],
  1778. techniques: [{ id: "T1490", name: "Inhibit System Recovery" }],
  1779. sourceEvent: {
  1780. "@timestamp": ts[3],
  1781. "event.category": ["process"],
  1782. "event.type": ["start"],
  1783. "event.action": "exec",
  1784. "event.module": "endpoint",
  1785. ...hostFields,
  1786. "user.name": user,
  1787. "process.name": "vssadmin.exe",
  1788. "process.entity_id": eid.vssadmin,
  1789. "process.pid": 12004,
  1790. "process.executable": "C:\\Windows\\System32\\vssadmin.exe",
  1791. "process.args": ["delete", "shadows", "/all", "/quiet"],
  1792. "process.command_line": "vssadmin.exe delete shadows /all /quiet",
  1793. "process.parent.name": "beacon.exe",
  1794. "process.parent.entity_id": eid.beacon,
  1795. "process.parent.pid": 11240,
  1796. message: "Shadow copies deleted via vssadmin - ransomware preparation",
  1797. },
  1798. }),
  1799. buildAlert({
  1800. timestamp: ts[4],
  1801. ruleName: "Ransomware - Mass File Encryption Detected",
  1802. ruleId: "sample-file-encryption",
  1803. severity: "critical",
  1804. riskScore: 99,
  1805. description:
  1806. "A process modified multiple files adding an encryption extension, characteristic of ransomware activity.",
  1807. tactics: [{ id: "TA0040", name: "Impact" }],
  1808. techniques: [{ id: "T1486", name: "Data Encrypted for Impact" }],
  1809. sourceEvent: {
  1810. "@timestamp": ts[4],
  1811. "event.category": ["file"],
  1812. "event.type": ["change"],
  1813. "event.action": "modification",
  1814. "event.module": "endpoint",
  1815. ...hostFields,
  1816. "user.name": user,
  1817. "process.name": "beacon.exe",
  1818. "process.entity_id": eid.beacon,
  1819. "process.pid": 11240,
  1820. "file.name": "document1.docx.encrypted",
  1821. "file.path": "C:\\Users\\dave\\Documents\\document1.docx.encrypted",
  1822. message: "Mass file encryption detected - 5+ files renamed with .encrypted extension by beacon.exe",
  1823. },
  1824. }),
  1825. buildAlert({
  1826. timestamp: ts[5],
  1827. ruleName: "Ransomware Note Created",
  1828. ruleId: "sample-ransom-note",
  1829. severity: "critical",
  1830. riskScore: 99,
  1831. description: "A ransom note file was created on the desktop, confirming ransomware execution.",
  1832. tactics: [{ id: "TA0040", name: "Impact" }],
  1833. techniques: [{ id: "T1486", name: "Data Encrypted for Impact" }],
  1834. sourceEvent: {
  1835. "@timestamp": ts[5],
  1836. "event.category": ["file"],
  1837. "event.type": ["creation"],
  1838. "event.action": "creation",
  1839. "event.module": "endpoint",
  1840. ...hostFields,
  1841. "user.name": user,
  1842. "file.name": "README_RESTORE_FILES.txt",
  1843. "file.path": "C:\\Users\\dave\\Desktop\\README_RESTORE_FILES.txt",
  1844. "process.name": "beacon.exe",
  1845. "process.entity_id": eid.beacon,
  1846. "process.pid": 11240,
  1847. message: "Ransom note README_RESTORE_FILES.txt dropped on desktop",
  1848. },
  1849. }),
  1850. ];
  1851. },
  1852. };
  1853. export const AVAILABLE_ALERT_SCENARIOS = Object.keys(ALERT_SCENARIOS);
  1854. async function bulkIndexAlerts(client, alerts) {
  1855. if (!alerts.length) return { indexed: 0, errors: 0, alertRefs: [] };
  1856. const targetIndex = ".alerts-security.alerts-default";
  1857. console.log(` Target index: ${targetIndex}`);
  1858. const body = [];
  1859. for (const alert of alerts) {
  1860. body.push({ create: { _index: targetIndex } });
  1861. body.push(alert);
  1862. }
  1863. let result;
  1864. try {
  1865. result = await client.bulk({ body, refresh: "wait_for" });
  1866. } catch (e) {
  1867. console.error(` Bulk index error: ${e.message}`);
  1868. if (e.meta?.body) console.error(` Response: ${JSON.stringify(e.meta.body).slice(0, 500)}`);
  1869. return { indexed: 0, errors: alerts.length, firstError: e.message, alertRefs: [] };
  1870. }
  1871. let indexed = 0;
  1872. let errors = 0;
  1873. let firstError = null;
  1874. const alertRefs = [];
  1875. if (result.items) {
  1876. for (const [i, item] of result.items.entries()) {
  1877. const op = item.create || item.index;
  1878. if (op?.error) {
  1879. errors++;
  1880. if (!firstError) firstError = op.error;
  1881. } else {
  1882. indexed++;
  1883. const sourceAlert = alerts[i];
  1884. alertRefs.push({
  1885. id: op?._id,
  1886. index: op?._index,
  1887. rule: {
  1888. id: sourceAlert?.kibana?.alert?.rule?.uuid || sourceAlert?.kibana?.alert?.rule?.rule_id || "unknown-rule",
  1889. name: sourceAlert?.kibana?.alert?.rule?.name || "Unknown rule",
  1890. },
  1891. });
  1892. }
  1893. }
  1894. }
  1895. return { indexed, errors, firstError, alertRefs };
  1896. }
  1897. export async function generateAlerts(scenarioNames) {
  1898. const client = createClient();
  1899. await detectClusterVersions(client);
  1900. const results = {};
  1901. let totalIndexed = 0;
  1902. let totalErrors = 0;
  1903. const alertRefs = [];
  1904. const toRun = scenarioNames.length ? scenarioNames : [...AVAILABLE_ALERT_SCENARIOS];
  1905. for (const name of toRun) {
  1906. const generator = ALERT_SCENARIOS[name];
  1907. if (!generator) {
  1908. results[name] = { status: "skipped", reason: "unknown alert scenario" };
  1909. continue;
  1910. }
  1911. const alerts = generator();
  1912. console.log(` Alert scenario "${name}": ${alerts.length} alerts...`);
  1913. try {
  1914. const { indexed, errors, firstError, alertRefs: scenarioAlertRefs } = await bulkIndexAlerts(client, alerts);
  1915. totalIndexed += indexed;
  1916. totalErrors += errors;
  1917. alertRefs.push(...scenarioAlertRefs);
  1918. results[name] = { status: "ok", indexed, errors, alertCount: alerts.length };
  1919. if (errors > 0 && firstError) {
  1920. console.error(
  1921. ` Warning: ${errors} alert(s) failed. Error: ${firstError?.reason || JSON.stringify(firstError)}`,
  1922. );
  1923. }
  1924. console.log(` ${indexed} indexed, ${errors} failed`);
  1925. } catch (e) {
  1926. totalErrors += alerts.length;
  1927. results[name] = { status: "error", error: e.message };
  1928. console.error(` Error: ${e.message}`);
  1929. }
  1930. }
  1931. await client.close();
  1932. return { results, totalIndexed, totalErrors, alertRefs };
  1933. }
  1934. // ---------------------------------------------------------------------------
  1935. // Case creation — single summary case linking all generated alert groups.
  1936. // ---------------------------------------------------------------------------
  1937. export async function generateCase(kibanaPost, kibanaGet, alertRefs = []) {
  1938. const title = "Sample Data — Combined Attack Investigation";
  1939. if (kibanaGet) {
  1940. try {
  1941. const existing = await kibanaGet("/api/cases/_find", {
  1942. tags: "Sample Data",
  1943. perPage: 1,
  1944. });
  1945. if ((existing?.cases || []).some((c) => c.title === title)) {
  1946. console.log(` Case already exists: "${title}"`);
  1947. return null;
  1948. }
  1949. } catch {}
  1950. }
  1951. try {
  1952. const created = await kibanaPost("/api/cases", {
  1953. title,
  1954. description:
  1955. "Consolidated investigation for all sample-data attack scenarios: " +
  1956. "SSH brute force, credential theft, AWS IAM escalation, ransomware, " +
  1957. "insider exfiltration, and supply-chain compromise. " +
  1958. "Review correlated alerts in Security > Alerts.",
  1959. tags: ["Sample Data"],
  1960. severity: "critical",
  1961. connector: { id: "none", name: "none", type: ".none", fields: null },
  1962. settings: { syncAlerts: true },
  1963. owner: "securitySolution",
  1964. });
  1965. console.log(` Created case: "${title}" (${created.id})`);
  1966. if (created?.id && alertRefs.length) {
  1967. const groupedByRule = new Map();
  1968. for (const ref of alertRefs) {
  1969. if (!ref?.id || !ref?.index || !ref?.rule?.id) continue;
  1970. if (!groupedByRule.has(ref.rule.id)) {
  1971. groupedByRule.set(ref.rule.id, {
  1972. id: ref.rule.id,
  1973. name: ref.rule.name,
  1974. alertIds: [],
  1975. indices: [],
  1976. });
  1977. }
  1978. const group = groupedByRule.get(ref.rule.id);
  1979. group.alertIds.push(ref.id);
  1980. group.indices.push(ref.index);
  1981. }
  1982. let attachedAlerts = 0;
  1983. for (const group of groupedByRule.values()) {
  1984. try {
  1985. await kibanaPost(`/api/cases/${created.id}/comments`, {
  1986. type: "alert",
  1987. owner: "securitySolution",
  1988. alertId: group.alertIds,
  1989. index: group.indices,
  1990. rule: {
  1991. id: group.id,
  1992. name: group.name,
  1993. },
  1994. });
  1995. attachedAlerts += group.alertIds.length;
  1996. } catch (e) {
  1997. console.error(` Alert attach failed for rule "${group.name}": ${e.message}`);
  1998. }
  1999. }
  2000. console.log(` Attached ${attachedAlerts} alert(s) to case`);
  2001. } else {
  2002. console.log(" No alert attachments added (no scenario alerts generated)");
  2003. }
  2004. return created;
  2005. } catch (e) {
  2006. console.error(` Case creation error: ${e.message}`);
  2007. return null;
  2008. }
  2009. }
  2010. const SAMPLE_DATA_INDICES = [
  2011. "logs-system.auth-default",
  2012. "logs-system.syslog-default",
  2013. "logs-endpoint.events.process-default",
  2014. "logs-endpoint.events.network-default",
  2015. "logs-endpoint.events.file-default",
  2016. "logs-windows.security-default",
  2017. "logs-aws.cloudtrail-default",
  2018. "logs-okta.system-default",
  2019. ];
  2020. async function deleteSampleCases() {
  2021. const result = await kibanaGet("/api/cases/_find", {
  2022. tags: "Sample Data",
  2023. perPage: 100,
  2024. });
  2025. const cases = result?.cases || [];
  2026. if (!cases.length) {
  2027. console.log(" No sample cases found");
  2028. return;
  2029. }
  2030. const ids = JSON.stringify(cases.map((c) => c.id));
  2031. await kibanaDelete(`/api/cases?ids=${encodeURIComponent(ids)}`);
  2032. console.log(` ${cases.length} sample case(s) deleted`);
  2033. }
  2034. function hasSampleDataMarkerInSource(source) {
  2035. if (!source || typeof source !== "object") return false;
  2036. const tags = source.tags;
  2037. if (Array.isArray(tags)) return tags.includes(SAMPLE_DATA_MARKER);
  2038. return tags === SAMPLE_DATA_MARKER;
  2039. }
  2040. async function deleteBySourceScan(client, indexPattern) {
  2041. let deleted = 0;
  2042. let matched = 0;
  2043. let scanned = 0;
  2044. let errors = 0;
  2045. let scrollId;
  2046. try {
  2047. let response = await client.search({
  2048. index: indexPattern,
  2049. size: CLEANUP_SOURCE_SCAN_PAGE_SIZE,
  2050. scroll: "2m",
  2051. sort: ["_doc"],
  2052. ignore_unavailable: true,
  2053. allow_no_indices: true,
  2054. expand_wildcards: "open,hidden",
  2055. _source: ["tags"],
  2056. query: {
  2057. range: {
  2058. "@timestamp": {
  2059. gte: CLEANUP_SOURCE_SCAN_LOOKBACK,
  2060. },
  2061. },
  2062. },
  2063. });
  2064. while (true) {
  2065. const hits = response.hits?.hits || [];
  2066. if (!hits.length) break;
  2067. scanned += hits.length;
  2068. const body = [];
  2069. for (const hit of hits) {
  2070. if (hasSampleDataMarkerInSource(hit._source)) {
  2071. matched++;
  2072. body.push({ delete: { _index: hit._index, _id: hit._id } });
  2073. }
  2074. }
  2075. if (body.length) {
  2076. const result = await client.bulk({ body, refresh: false });
  2077. for (const item of result.items || []) {
  2078. const op = item.delete;
  2079. if (op?.error) errors++;
  2080. else deleted++;
  2081. }
  2082. }
  2083. scrollId = response._scroll_id;
  2084. if (!scrollId) break;
  2085. response = await client.scroll({ scroll_id: scrollId, scroll: "2m" });
  2086. }
  2087. } finally {
  2088. if (scrollId) {
  2089. try {
  2090. await client.clearScroll({ scroll_id: scrollId });
  2091. } catch {}
  2092. }
  2093. }
  2094. if (deleted > 0) {
  2095. try {
  2096. await client.indices.refresh({
  2097. index: indexPattern,
  2098. ignore_unavailable: true,
  2099. allow_no_indices: true,
  2100. expand_wildcards: "open,hidden",
  2101. });
  2102. } catch {}
  2103. }
  2104. return { deleted, matched, scanned, errors };
  2105. }
  2106. export async function cleanup() {
  2107. const client = createClient();
  2108. let eventsDeleted = 0;
  2109. let alertsDeleted = 0;
  2110. const hasKibanaAuth =
  2111. !!process.env.KIBANA_API_KEY ||
  2112. !!(process.env.KIBANA_USERNAME && process.env.KIBANA_PASSWORD) ||
  2113. !!(process.env.ELASTICSEARCH_USERNAME && process.env.ELASTICSEARCH_PASSWORD);
  2114. console.log("Cleaning up sample data...\n");
  2115. const sampleDataQuery = {
  2116. bool: {
  2117. should: [
  2118. { term: { tags: SAMPLE_DATA_MARKER } },
  2119. { term: { "tags.keyword": SAMPLE_DATA_MARKER } },
  2120. { match_phrase: { tags: SAMPLE_DATA_MARKER } },
  2121. { term: { "kibana.alert.rule.tags": "Sample Data" } },
  2122. ],
  2123. minimum_should_match: 1,
  2124. },
  2125. };
  2126. for (const index of SAMPLE_DATA_INDICES) {
  2127. try {
  2128. const result = await client.deleteByQuery({
  2129. index,
  2130. body: { query: sampleDataQuery },
  2131. ignore_unavailable: true,
  2132. conflicts: "proceed",
  2133. refresh: true,
  2134. });
  2135. const total = result.total || 0;
  2136. const deleted = result.deleted || 0;
  2137. const conflicts = result.version_conflicts || 0;
  2138. const failures = result.failures?.length || 0;
  2139. const timedOut = !!result.timed_out;
  2140. eventsDeleted += deleted;
  2141. if (deleted > 0) {
  2142. console.log(` ${index}: ${deleted} events deleted`);
  2143. } else {
  2144. console.log(` ${index}: no events deleted`);
  2145. }
  2146. if (total > 0 || conflicts > 0 || failures > 0 || timedOut) {
  2147. console.log(
  2148. ` details: total=${total}, deleted=${deleted}, version_conflicts=${conflicts}, failures=${failures}, timed_out=${timedOut}`,
  2149. );
  2150. }
  2151. if (failures > 0 && result.failures?.[0]) {
  2152. const firstFailure = result.failures[0];
  2153. console.warn(
  2154. ` first failure: ${firstFailure.cause?.type || "unknown"} - ${firstFailure.cause?.reason || JSON.stringify(firstFailure)}`,
  2155. );
  2156. }
  2157. // Fallback for data streams where marker fields are present in _source but not indexed.
  2158. if (deleted === 0 && total === 0) {
  2159. const scan = await deleteBySourceScan(client, index);
  2160. eventsDeleted += scan.deleted;
  2161. if (scan.deleted > 0 || scan.errors > 0) {
  2162. console.log(
  2163. ` source-scan fallback: scanned=${scan.scanned}, matched=${scan.matched}, deleted=${scan.deleted}, errors=${scan.errors}`,
  2164. );
  2165. }
  2166. }
  2167. } catch (e) {
  2168. if (!e.message?.includes("index_not_found")) {
  2169. console.error(` ${index}: ${e.message}`);
  2170. }
  2171. }
  2172. }
  2173. try {
  2174. const result = await client.deleteByQuery({
  2175. index: ".alerts-security.alerts-default",
  2176. body: { query: sampleDataQuery },
  2177. conflicts: "proceed",
  2178. refresh: true,
  2179. });
  2180. alertsDeleted = result.deleted || 0;
  2181. const total = result.total || 0;
  2182. const conflicts = result.version_conflicts || 0;
  2183. const failures = result.failures?.length || 0;
  2184. const timedOut = !!result.timed_out;
  2185. if (alertsDeleted > 0) {
  2186. console.log(` .alerts-security.alerts-default: ${alertsDeleted} alerts deleted`);
  2187. } else {
  2188. console.log(` .alerts-security.alerts-default: no alerts deleted`);
  2189. }
  2190. if (total > 0 || conflicts > 0 || failures > 0 || timedOut) {
  2191. console.log(
  2192. ` details: total=${total}, deleted=${alertsDeleted}, version_conflicts=${conflicts}, failures=${failures}, timed_out=${timedOut}`,
  2193. );
  2194. }
  2195. if (failures > 0 && result.failures?.[0]) {
  2196. const firstFailure = result.failures[0];
  2197. console.warn(
  2198. ` first failure: ${firstFailure.cause?.type || "unknown"} - ${firstFailure.cause?.reason || JSON.stringify(firstFailure)}`,
  2199. );
  2200. }
  2201. } catch (e) {
  2202. if (!e.message?.includes("index_not_found")) {
  2203. console.error(` Alerts cleanup: ${e.message}`);
  2204. }
  2205. }
  2206. await client.close();
  2207. if (process.env.KIBANA_URL && hasKibanaAuth) {
  2208. try {
  2209. await deleteSampleCases();
  2210. } catch (e) {
  2211. console.error(` Cases cleanup: ${e.message}`);
  2212. }
  2213. } else if (process.env.KIBANA_URL) {
  2214. console.log(" Cases cleanup skipped: Kibana auth not configured");
  2215. }
  2216. console.log(`\nCleanup complete: ${eventsDeleted} events + ${alertsDeleted} alerts deleted`);
  2217. return { eventsDeleted, alertsDeleted };
  2218. }
  2219. export const SUPPORTED_PACKAGES = Object.keys(EVENT_GENERATORS);
  2220. export async function generateAndIndex(packageNames, eventsPerPackage = 50) {
  2221. const client = createClient();
  2222. await detectClusterVersions(client);
  2223. const results = {};
  2224. let totalIndexed = 0;
  2225. let totalErrors = 0;
  2226. for (const pkg of packageNames) {
  2227. if (!EVENT_GENERATORS[pkg]) {
  2228. results[pkg] = { status: "skipped", reason: "no sample data templates" };
  2229. continue;
  2230. }
  2231. const events = generateEvents(pkg, eventsPerPackage);
  2232. console.log(` Generating ${events.length} events for ${pkg}...`);
  2233. try {
  2234. const { indexed, errors, firstError } = await bulkIndex(client, events);
  2235. totalIndexed += indexed;
  2236. totalErrors += errors;
  2237. results[pkg] = { status: "ok", indexed, errors };
  2238. if (errors > 0 && firstError) {
  2239. const errDetail = (firstError.create || firstError.index)?.error;
  2240. console.error(
  2241. ` Warning: ${errors} event(s) failed to index. First error: ${errDetail?.reason || JSON.stringify(errDetail)}`,
  2242. );
  2243. }
  2244. console.log(` ${indexed} indexed, ${errors} failed`);
  2245. } catch (e) {
  2246. totalErrors += events.length;
  2247. results[pkg] = { status: "error", error: e.message };
  2248. console.error(` Error: ${e.message}`);
  2249. }
  2250. }
  2251. await client.close();
  2252. return { results, totalIndexed, totalErrors };
  2253. }
  2254. export async function runContinuous(intervalSec = 30, eventsPerBatch = 10) {
  2255. const client = createClient();
  2256. await detectClusterVersions(client);
  2257. let totalIndexed = 0;
  2258. let batchNum = 0;
  2259. console.log(`Continuous mode: generating events every ${intervalSec}s (Ctrl+C to stop)\n`);
  2260. const shutdown = () => {
  2261. console.log(`\nStopping... ${totalIndexed} total events indexed across ${batchNum} batches`);
  2262. client.close().then(() => process.exit(0));
  2263. };
  2264. process.on("SIGINT", shutdown);
  2265. process.on("SIGTERM", shutdown);
  2266. while (true) {
  2267. batchNum++;
  2268. const pkg = randomFrom(SUPPORTED_PACKAGES);
  2269. const events = generateEvents(pkg, eventsPerBatch);
  2270. try {
  2271. const { indexed } = await bulkIndex(client, events);
  2272. totalIndexed += indexed;
  2273. const ts = new Date().toLocaleTimeString();
  2274. process.stdout.write(` [${ts}] Batch #${batchNum}: ${indexed} ${pkg} events (total: ${totalIndexed})\r\n`);
  2275. } catch (e) {
  2276. console.error(` Batch #${batchNum} error: ${e.message}`);
  2277. }
  2278. if (batchNum % 5 === 0) {
  2279. const scenario = randomFrom(AVAILABLE_SCENARIOS);
  2280. const gen = ATTACK_SCENARIOS[scenario];
  2281. if (gen) {
  2282. const scenarioEvents = gen();
  2283. try {
  2284. const { indexed } = await bulkIndex(client, scenarioEvents);
  2285. totalIndexed += indexed;
  2286. process.stdout.write(
  2287. ` [${new Date().toLocaleTimeString()}] + scenario "${scenario}": ${indexed} events\r\n`,
  2288. );
  2289. } catch {}
  2290. }
  2291. }
  2292. if (batchNum % 10 === 0) {
  2293. const alertScenario = randomFrom(AVAILABLE_ALERT_SCENARIOS);
  2294. const gen = ALERT_SCENARIOS[alertScenario];
  2295. if (gen) {
  2296. const alerts = gen();
  2297. try {
  2298. await bulkIndexAlerts(client, alerts);
  2299. process.stdout.write(
  2300. ` [${new Date().toLocaleTimeString()}] + alert scenario "${alertScenario}": ${alerts.length} alerts\r\n`,
  2301. );
  2302. } catch {}
  2303. }
  2304. }
  2305. await new Promise((r) => setTimeout(r, intervalSec * 1000));
  2306. }
  2307. }
  2308. async function main() {
  2309. const argv = process.argv.slice(2);
  2310. let packageNames = [];
  2311. let scenarioNames = [];
  2312. let count = 50;
  2313. let json = false;
  2314. let scenarios = false;
  2315. let alerts = false;
  2316. let doCleanup = false;
  2317. let continuous = false;
  2318. let interval = 30;
  2319. let yes = false;
  2320. for (let i = 0; i < argv.length; i++) {
  2321. const a = argv[i];
  2322. if (a === "--count" || a === "-n") count = parseInt(argv[++i], 10) || 50;
  2323. else if (a === "--json" || a === "-j") json = true;
  2324. else if (a === "--scenarios" || a === "--attack") scenarios = true;
  2325. else if (a === "--scenario") scenarioNames.push(argv[++i]);
  2326. else if (a === "--alerts") alerts = true;
  2327. else if (a === "--cleanup") doCleanup = true;
  2328. else if (a === "--continuous") continuous = true;
  2329. else if (a === "--interval") interval = parseInt(argv[++i], 10) || 30;
  2330. else if (a === "--yes" || a === "-y") yes = true;
  2331. else if (a === "--help" || a === "-h") {
  2332. console.log("Usage: sample-data.js [packages...] [options]");
  2333. console.log(`\nSupported packages: ${SUPPORTED_PACKAGES.join(", ")}`);
  2334. console.log(`Attack scenarios: ${AVAILABLE_SCENARIOS.join(", ")}`);
  2335. console.log(`Alert scenarios: ${AVAILABLE_ALERT_SCENARIOS.join(", ")}`);
  2336. console.log("\nOptions:");
  2337. console.log(" --count, -n Events per package (default: 50)");
  2338. console.log(" --scenarios Run all attack simulation scenarios");
  2339. console.log(" --scenario NAME Run a specific attack scenario");
  2340. console.log(" --alerts Generate synthetic alert documents");
  2341. console.log(" --cleanup Remove all sample data and alerts");
  2342. console.log(" --continuous Keep generating events (Ctrl+C to stop)");
  2343. console.log(" --interval N Seconds between batches in continuous mode (default: 30)");
  2344. console.log(" --json, -j Output results as JSON");
  2345. console.log(" --yes, -y Skip confirmation prompts");
  2346. process.exit(0);
  2347. } else if (!a.startsWith("-")) {
  2348. packageNames.push(a);
  2349. }
  2350. }
  2351. if (doCleanup) {
  2352. if (!yes) {
  2353. const ok = await promptConfirm("Delete all sample data, alerts, and cases? This cannot be undone.");
  2354. if (!ok) {
  2355. console.log("Aborted.");
  2356. return;
  2357. }
  2358. }
  2359. await cleanup();
  2360. return;
  2361. }
  2362. if (continuous) {
  2363. if (!yes) {
  2364. const ok = await promptConfirm(`Start continuous event generation every ${interval}s? (Ctrl+C to stop)`);
  2365. if (!ok) {
  2366. console.log("Aborted.");
  2367. return;
  2368. }
  2369. }
  2370. await runContinuous(interval, count);
  2371. return;
  2372. }
  2373. let allResults = {};
  2374. let grandTotalIndexed = 0;
  2375. let grandTotalErrors = 0;
  2376. if (!scenarios && !scenarioNames.length && !packageNames.length && !alerts) {
  2377. packageNames = [...SUPPORTED_PACKAGES];
  2378. scenarios = true;
  2379. alerts = true;
  2380. console.log("No packages or scenarios specified — running everything.\n");
  2381. }
  2382. if (!yes) {
  2383. const parts = [];
  2384. if (packageNames.length) parts.push(`${count} events for ${packageNames.length} package(s)`);
  2385. if (scenarios || scenarioNames.length) parts.push("attack scenarios");
  2386. if (alerts) parts.push("synthetic alerts");
  2387. const ok = await promptConfirm(`Generate ${parts.join(", ")}?`);
  2388. if (!ok) {
  2389. console.log("Aborted.");
  2390. return;
  2391. }
  2392. }
  2393. if (packageNames.length) {
  2394. console.log(`Generating sample events (${count} per package):\n`);
  2395. const { results, totalIndexed, totalErrors } = await generateAndIndex(packageNames, count);
  2396. Object.assign(allResults, results);
  2397. grandTotalIndexed += totalIndexed;
  2398. grandTotalErrors += totalErrors;
  2399. }
  2400. if (scenarios || scenarioNames.length) {
  2401. console.log("\nRunning attack simulation scenarios:\n");
  2402. const { results, totalIndexed, totalErrors } = await runAttackScenarios(scenarioNames);
  2403. for (const [k, v] of Object.entries(results)) {
  2404. allResults[`scenario:${k}`] = v;
  2405. }
  2406. grandTotalIndexed += totalIndexed;
  2407. grandTotalErrors += totalErrors;
  2408. }
  2409. if (alerts) {
  2410. console.log("\nGenerating synthetic alert documents:\n");
  2411. const { results, totalIndexed, totalErrors } = await generateAlerts([]);
  2412. for (const [k, v] of Object.entries(results)) {
  2413. allResults[`alert:${k}`] = v;
  2414. }
  2415. grandTotalIndexed += totalIndexed;
  2416. grandTotalErrors += totalErrors;
  2417. }
  2418. console.log(`\nDone: ${grandTotalIndexed} events indexed, ${grandTotalErrors} errors`);
  2419. if (json) {
  2420. console.log(JSON.stringify(allResults, null, 2));
  2421. }
  2422. }
  2423. const isDirectRun = process.argv[1]?.endsWith("sample-data.js");
  2424. if (isDirectRun) {
  2425. main().catch((e) => {
  2426. console.error(e.message || e);
  2427. process.exit(1);
  2428. });
  2429. }

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.