|
| 1 | +## ECS mappings generator |
| 2 | + |
| 3 | +This script generates the ECS mappings for the Wazuh indices. |
| 4 | + |
| 5 | +### Requirements |
| 6 | + |
| 7 | +- ECS repository clone. The script is meant to be launched from the root level of that repository. |
| 8 | +- Python 3.6 or higher |
| 9 | +- jq |
| 10 | + |
| 11 | +### Folder structure |
| 12 | + |
| 13 | +There is a folder for each module. Inside each folder, there is a `fields` folder with the required |
| 14 | +files to generate the mappings. These are the inputs for the ECS generator. |
| 15 | + |
| 16 | +### Usage |
| 17 | + |
| 18 | +**Copy the `generate.sh` script to the root level of the ECS repository.** |
| 19 | + |
| 20 | +Use the `generate.sh` script to generate the mappings for a module. The script takes 3 arguments, |
| 21 | +plus 2 optional arguments to upload the mappings to the Wazuh indexer (using **composable** indexes). |
| 22 | + |
| 23 | +```plaintext |
| 24 | +Usage: ./generate.sh <ECS_VERSION> <INDEXER_SRC> <MODULE> [--upload <URL>] |
| 25 | + * ECS_VERSION: ECS version to generate mappings for |
| 26 | + * INDEXER_SRC: Path to the wazuh-indexer repository |
| 27 | + * MODULE: Module to generate mappings for |
| 28 | + * --upload <URL>: Upload generated index template to the OpenSearch cluster. Defaults to https://localhost:9200 |
| 29 | +Example: ./generate.sh v8.10.0 ~/wazuh-indexer vulnerability-detector --upload https://indexer:9200 |
| 30 | +``` |
| 31 | + |
| 32 | +For example, to generate the mappings for the `vulnerability-detector` module using the |
| 33 | +ECS version `v8.10.0` and the Wazuh indexer in path `~/wazuh/wazuh-indexer`: |
| 34 | + |
| 35 | +```bash |
| 36 | +./generate.sh v8.10.0 ~/wazuh/wazuh-indexer vulnerability-detector |
| 37 | +``` |
| 38 | + |
| 39 | +### Output |
| 40 | + |
| 41 | +A new `mappings` folder will be created inside the module folder, containing all the generated files. |
| 42 | +The files are versioned using the ECS version, so different versions of the same module can be generated. |
| 43 | +For our use case, the most important files are under `mappings/<ECS_VERSION>/generated/elasticsearch/legacy/`: |
| 44 | + |
| 45 | +- `template.json`: Elasticsearch compatible index template for the module |
| 46 | +- `opensearch-template.json`: OpenSearch compatible index template for the module |
| 47 | + |
| 48 | +The original output is `template.json`, which is not compatible with OpenSearch by default. In order |
| 49 | +to make this template compatible with OpenSearch, the following changes are made: |
| 50 | + |
| 51 | +- the `order` property is renamed to `priority`. |
| 52 | +- the `mappings` and `settings` properties are nested under the `template` property. |
| 53 | + |
| 54 | +The script takes care of these changes automatically, generating the `opensearch-template.json` file as a result. |
| 55 | + |
| 56 | +### Upload |
| 57 | + |
| 58 | +You can either upload the index template using cURL or the UI (dev tools). |
| 59 | + |
| 60 | +```bash |
| 61 | +curl -u admin:admin -k -X PUT "https://indexer:9200/_index_template/wazuh-vulnerability-detector" -H "Content-Type: application/json" -d @opensearch-template.json |
| 62 | +``` |
| 63 | + |
| 64 | +Notes: |
| 65 | +- PUT and POST are interchangeable. |
| 66 | +- The name of the index template does not matter. Any name can be used. |
| 67 | +- Adjust credentials and URL accordingly. |
| 68 | + |
| 69 | +### Adding new mappings |
| 70 | + |
| 71 | +The easiest way to create mappings for a new module is to take a previous one as a base. |
| 72 | +Copy a folder and rename it to the new module name. Then, edit the `fields` files to |
| 73 | +match the new module fields. |
| 74 | + |
| 75 | +The name of the folder will be the name of the module to be passed to the script. All 3 files |
| 76 | +are required. |
| 77 | + |
| 78 | +- `fields/subset.yml`: This file contains the subset of ECS fields to be used for the module. |
| 79 | +- `fields/template-settings-legacy.json`: This file contains the legacy template settings for the module. |
| 80 | +- `fields/template-settings.json`: This file contains the composable template settings for the module. |
| 81 | + |
| 82 | +### Event generator |
| 83 | + |
| 84 | +For testing purposes, the script `generate_events.py` can be used to generate events for a given module. |
| 85 | +Currently, it is only able to generate events for the `vulnerability-detector` module. To support other |
| 86 | +modules, please extend or refactor the script. |
| 87 | + |
| 88 | +The script prompts for the required parameters, so it can be launched without arguments: |
| 89 | + |
| 90 | +```bash |
| 91 | +./event_generator.py |
| 92 | +``` |
| 93 | + |
| 94 | +The script will generate a JSON file with the events, and will also ask whether to upload them to the |
| 95 | +indexer. If the upload option is selected, the script will ask for the indexer URL and port, credentials, |
| 96 | +and index name. |
| 97 | + |
| 98 | +The script uses a log file. Check it out for debugging or additional information. |
| 99 | + |
| 100 | +#### References |
| 101 | + |
| 102 | +- [ECS repository](https://github.com/elastic/ecs) |
| 103 | +- [ECS usage](https://github.com/elastic/ecs/blob/main/USAGE.md) |
| 104 | +- [ECS field reference](https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html) |
0 commit comments