Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement commands expansion logic #229

Merged
merged 28 commits into from
Feb 10, 2025

Conversation

QU3B1M
Copy link
Member

@QU3B1M QU3B1M commented Jan 17, 2025

Description

Detect the commands which target type is group, search in the .agents index for the agents of each group and generate the corresponding Orders.

Renamed the Document class to Orders, replacing the previously unused Orders class.

Working validations

  • Command expansion

    Group used for the test: group000

     % curl http://127.0.0.1:9200/.agents/_search | grep "group000"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 15337  100 15337    0     0  1799k      0 --:--:-- --:--:-- --:--:-- 1872k
        "groups": ["group000","group001","group003"],
        "groups": ["group000","group002","group002"],
    • Request

      {
        "commands": [
          {
            "source": "Engine",
            "user": "user53",
            "target": {
              "id": "group000",
              "type": "group"
            },
            "action": {
              "name": "restart",
              "args": {
                "arg1": "/path/to/executable/arg6"
              },
              "version": "v4"
            },
            "timeout": 30
          }
        ]
      }
    • Response

      {
        "_index": ".commands",
        "_orders": [
          {
            "_id": "WS5fjpQBDZQg8-hA8AYd"
          },
          {
            "_id": "Wi5fjpQBDZQg8-hA8AYe"
          }
        ],
        "result": "OK"
      }
    • Command creation log

      [2025-01-22T11:17:19,646][INFO ][c.w.c.i.CommandIndex     ] [integTest-0] Adding command with id [WS5fjpQBDZQg8-hA8AYd] to the bulk request
      [2025-01-22T11:17:19,646][INFO ][c.w.c.i.CommandIndex     ] [integTest-0] Adding command with id [Wi5fjpQBDZQg8-hA8AYe] to the bulk request
    • Command indexed

      % curl http://127.0.0.1:9200/.commands/_search                
      {"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":".commands","_id":"WS5fjpQBDZQg8-hA8AYd","_score":1.0,"_source":{"agent":{"groups":["group000","group001","group003"]},"@timestamp":"2025-01-22T14:17:19Z","delivery_timestamp":"2025-01-22T14:17:49Z","command":{"action":{"args":{"arg1":"/path/to/executable/arg6"},"name":"restart","version":"v4"},"source":"Engine","user":"user53","order_id":"WC5fjpQBDZQg8-hA8AYP","request_id":"Vy5fjpQBDZQg8-hA8AYP","timeout":30,"target":{"id":"group000","type":"group"},"status":"failure"}}},{"_index":".commands","_id":"Wi5fjpQBDZQg8-hA8AYe","_score":1.0,"_source":{"agent":{"groups":["group000","group002","group002"]},"@timestamp":"2025-01-22T14:17:19Z","delivery_timestamp":"2025-01-22T14:17:49Z","command":{"action":{"args":{"arg1":"/path/to/executable/arg6"},"name":"restart","version":"v4"},"source":"Engine","user":"user53","order_id":"WC5fjpQBDZQg8-hA8AYP","request_id":"Vy5fjpQBDZQg8-hA8AYP","timeout":30,"target":{"id":"group000","type":"group"},"status":"failure"}}}]}}
  • Command expansion - Group assigned to the same agent more than once

    Using the group 001

    % curl http://127.0.0.1:9200/.agents/_search | grep "group001"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 15451  100 15451    0     0  2741k      0 --:--:-- --:--:-- --:--:-- 3017k
        "groups": ["group001"],
        "groups": ["group004","group004","group001","group004"],
        "groups": ["group001","group005","group000","group001"],
        "groups": ["group004","group000","group003","group001","group002"],
        "groups": ["group004","group000","group001"],
    • Request
      {
        "commands": [
          {
            "source": "Engine",
            "user": "user53",
            "target": {
              "id": "group001",
              "type": "group"
            },
            "action": {
              "name": "restart",
              "args": {
                "arg1": "/path/to/executable/arg6"
              },
              "version": "v4"
            },
            "timeout": 30
          }
        ]
      }
    • Response
      {
        "_index": ".commands",
        "_orders": [
          {
            "_id": "wP9ojpQBpOpp5jjTDc0s"
          },
          {
            "_id": "wf9ojpQBpOpp5jjTDc0t"
          },
          {
            "_id": "wv9ojpQBpOpp5jjTDc0t"
          },
          {
            "_id": "w_9ojpQBpOpp5jjTDc0t"
          },
          {
            "_id": "xP9ojpQBpOpp5jjTDc0t"
          }
        ],
        "result": "OK"
      }
    • Search on the index
      % curl http://127.0.0.1:9200/.commands/_search       
      {
          "took": 2,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 5,
                  "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                  {
                      "_index": ".commands",
                      "_id": "wP9ojpQBpOpp5jjTDc0s",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group001"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:26:11Z",
                          "delivery_timestamp": "2025-01-22T14:26:41Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "v_9ojpQBpOpp5jjTDc0h",
                              "request_id": "vv9ojpQBpOpp5jjTDc0h",
                              "timeout": 30,
                              "target": {
                                  "id": "group001",
                                  "type": "group"
                              },
                              "status": "failure"
                          }
                      }
                  },
                  {
                      "_index": ".commands",
                      "_id": "wf9ojpQBpOpp5jjTDc0t",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group001",
                                  "group005",
                                  "group000",
                                  "group001"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:26:11Z",
                          "delivery_timestamp": "2025-01-22T14:26:41Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "v_9ojpQBpOpp5jjTDc0h",
                              "request_id": "vv9ojpQBpOpp5jjTDc0h",
                              "timeout": 30,
                              "target": {
                                  "id": "group001",
                                  "type": "group"
                              },
                              "status": "failure"
                          }
                      }
                  },
                  {
                      "_index": ".commands",
                      "_id": "wv9ojpQBpOpp5jjTDc0t",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group004",
                                  "group000",
                                  "group001"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:26:11Z",
                          "delivery_timestamp": "2025-01-22T14:26:41Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "v_9ojpQBpOpp5jjTDc0h",
                              "request_id": "vv9ojpQBpOpp5jjTDc0h",
                              "timeout": 30,
                              "target": {
                                  "id": "group001",
                                  "type": "group"
                              },
                              "status": "failure"
                          }
                      }
                  },
                  {
                      "_index": ".commands",
                      "_id": "w_9ojpQBpOpp5jjTDc0t",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group004",
                                  "group004",
                                  "group001",
                                  "group004"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:26:11Z",
                          "delivery_timestamp": "2025-01-22T14:26:41Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "v_9ojpQBpOpp5jjTDc0h",
                              "request_id": "vv9ojpQBpOpp5jjTDc0h",
                              "timeout": 30,
                              "target": {
                                  "id": "group001",
                                  "type": "group"
                              },
                              "status": "failure"
                          }
                      }
                  },
                  {
                      "_index": ".commands",
                      "_id": "xP9ojpQBpOpp5jjTDc0t",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group004",
                                  "group000",
                                  "group003",
                                  "group001",
                                  "group002"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:26:11Z",
                          "delivery_timestamp": "2025-01-22T14:26:41Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "v_9ojpQBpOpp5jjTDc0h",
                              "request_id": "vv9ojpQBpOpp5jjTDc0h",
                              "timeout": 30,
                              "target": {
                                  "id": "group001",
                                  "type": "group"
                              },
                              "status": "failure"
                          }
                      }
                  }
              ]
          }
      }
  • Basic command creation

    Using agent id: agent94

    • Request
      {
        "commands": [
          {
            "source": "Engine",
            "user": "user53",
            "target": {
              "id": "agent94",
              "type": "agent"
            },
            "action": {
              "name": "restart",
              "args": {
                "arg1": "/path/to/executable/arg6"
              },
              "version": "v4"
            },
            "timeout": 30
          }
        ]
      }
    • Response
      {
        "_index": ".commands",
        "_orders": [
          {
            "_id": "SQJ1jpQBrXbQNgi-ZGvv"
          }
        ],
        "result": "OK"
      }
    • Command indexed
      % curl http://127.0.0.1:9200/.commands/_search                    
      {
          "took": 2,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 1,
                  "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                  {
                      "_index": ".commands",
                      "_id": "SQJ1jpQBrXbQNgi-ZGvv",
                      "_score": 1.0,
                      "_source": {
                          "agent": {
                              "groups": [
                                  "group001",
                                  "group002",
                                  "group002"
                              ]
                          },
                          "@timestamp": "2025-01-22T14:40:45Z",
                          "delivery_timestamp": "2025-01-22T14:41:15Z",
                          "command": {
                              "action": {
                                  "args": {
                                      "arg1": "/path/to/executable/arg6"
                                  },
                                  "name": "restart",
                                  "version": "v4"
                              },
                              "source": "Engine",
                              "user": "user53",
                              "order_id": "SAJ1jpQBrXbQNgi-ZGve",
                              "request_id": "RwJ1jpQBrXbQNgi-ZGve",
                              "timeout": 30,
                              "target": {
                                  "id": "agent94",
                                  "type": "agent"
                              },
                              "status": "failure"
                          }
                      }
                  }
              ]
          }
      }

Issues Resolved

Resolves #88

@QU3B1M QU3B1M self-assigned this Jan 19, 2025
@QU3B1M QU3B1M changed the title Add getters to Target model Implement commands expansion logic Jan 19, 2025
@QU3B1M QU3B1M marked this pull request as ready for review January 22, 2025 14:43
@QU3B1M QU3B1M requested a review from a team as a code owner January 22, 2025 14:43
@f-galland f-galland self-requested a review January 27, 2025 11:37
@f-galland
Copy link
Member

Agents are part of group group000:

$ curl -s http://127.0.0.1:9200/wazuh-agents/_search | grep group000
    "groups": ["group000","group004"],
    "groups": ["group000","group005"],

Issuing a command to group group000:

$ curl -s http://localhost:9200/_plugins/_command_manager/commands -H 'Content-Type: application/json' -d '{
  "commands": [
    {
      "source": "Engine",
      "user": "user53",
      "target": {
        "id": "group000",
        "type": "group"
      },
      "action": {
        "name": "restart",
        "args": {
          "arg1": "/path/to/executable/arg6"
        },
        "version": "v4"
      },
      "timeout": 30
    }
  ]
}' | jq
{
  "_index": "wazuh-commands",
  "_orders": [
    {
      "_id": "6fSGp5QBXpIVW99KMN3m"
    },
    {
      "_id": "6vSGp5QBXpIVW99KMN3m"
    }
  ],
  "result": "OK"
}

Comand creation log:

[2025-01-27T08:29:36,998][INFO ][c.w.c.i.CommandIndex     ] [integTest-0] Adding command with id [6fSGp5QBXpIVW99KMN3m] to the bulk request
[2025-01-27T08:29:36,998][INFO ][c.w.c.i.CommandIndex     ] [integTest-0] Adding command with id [6vSGp5QBXpIVW99KMN3m] to the bulk request

Command indexed:

$ curl -s http://127.0.0.1:9200/wazuh-commands/_search | jq '.hits.hits[0]._source'
{
  "agent": {
    "groups": [
      "group000",
      "group004"
    ]
  },
  "@timestamp": "2025-01-27T11:29:36Z",
  "delivery_timestamp": "2025-01-27T11:30:06Z",
  "command": {
    "action": {
      "args": {
        "arg1": "/path/to/executable/arg6"
      },
      "name": "restart",
      "version": "v4"
    },
    "source": "Engine",
    "user": "user53",
    "order_id": "6PSGp5QBXpIVW99KMN3i",
    "request_id": "5_SGp5QBXpIVW99KMN3i",
    "timeout": 30,
    "target": {
      "id": "group000",
      "type": "group"
    },
    "status": "failure"
  }
}

Basic command creation:

$ curl -s http://localhost:9200/_plugins/_command_manager/commands -H 'Content-Type: application/json' -d '
{
  "commands": [
    {
      "source": "Engine",
      "user": "user53",
      "target": {
        "id": "agent92",
        "type": "agent"
      },
      "action": {
        "name": "restart",
        "args": {
          "arg1": "/path/to/executable/arg6"
        },
        "version": "v4"
      },
      "timeout": 30
    }
  ]
}'
{"_index":"wazuh-commands","_orders":[{"_id":"_KiPp5QBytahsG79nekW"}],"result":"OK"}

Command indexed:

$ curl -s http://localhost:9200/wazuh-commands/_search | jq '.hits.hits[] | select(._id == "_KiPp5QBytahsG79nekW") | ._source'
{
  "agent": {
    "groups": [
      "group002",
      "group003",
      "group005"
    ]
  },
  "@timestamp": "2025-01-27T11:39:54Z",
  "delivery_timestamp": "2025-01-27T11:40:24Z",
  "command": {
    "action": {
      "args": {
        "arg1": "/path/to/executable/arg6"
      },
      "name": "restart",
      "version": "v4"
    },
    "source": "Engine",
    "user": "user53",
    "order_id": "-6iPp5QBytahsG79nekT",
    "request_id": "-qiPp5QBytahsG79nekT",
    "timeout": 30,
    "target": {
      "id": "agent92",
      "type": "agent"
    },
    "status": "failure"
  }
}

Copy link
Member

@f-galland f-galland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and working.

@f-galland
Copy link
Member

Agent assigned to a group more than once:

Group group004 is assigned multiple times to some agents:

$ curl -s http://localhost:9200/wazuh-agents/_search | grep group004
    "groups": ["group001","group004","group000","group004"],
    "groups": ["group004","group001","group000"],
    "groups": ["group004","group004"],

Indexing command:

curl -s http://localhost:9200/_plugins/_command_manager/commands -H 'Content-Type: application/json' -d '{
  "commands": [
    {
      "source": "Engine",
      "user": "user53",
      "target": {
        "id": "group004",
        "type": "group"
      },
      "action": {
        "name": "restart",
        "args": {
          "arg1": "/path/to/executable/arg6"
        },
        "version": "v4"
      },
      "timeout": 30
    }
  ]
}' | jq
{
  "_index": "wazuh-commands",
  "_orders": [
    {
      "_id": "_6iXp5QBytahsG79A-l6"
    },
    {
      "_id": "AKiXp5QBytahsG79A-p6"
    },
    {
      "_id": "AaiXp5QBytahsG79A-p6"
    }
  ],
  "result": "OK"
}

Only 3 commands are created:

$ curl -s http://localhost:9200/wazuh-commands/_search | jq '.hits.hits[] | select(._id == "_6iXp5QBytahsG79A-l6" or ._id == "AKiXp5QBytahsG79A-p6" or ._id == "AaiXp5QBytahsG79A-p6") | ._source'
{
  "agent": {
    "groups": [
      "group004",
      "group004"
    ]
  },
  "@timestamp": "2025-01-27T11:47:59Z",
  "delivery_timestamp": "2025-01-27T11:48:29Z",
  "command": {
    "action": {
      "args": {
        "arg1": "/path/to/executable/arg6"
      },
      "name": "restart",
      "version": "v4"
    },
    "source": "Engine",
    "user": "user53",
    "order_id": "_qiXp5QBytahsG79A-l3",
    "request_id": "_aiXp5QBytahsG79A-l3",
    "timeout": 30,
    "target": {
      "id": "group004",
      "type": "group"
    },
    "status": "failure"
  }
}
{
  "agent": {
    "groups": [
      "group001",
      "group004",
      "group000",
      "group004"
    ]
  },
  "@timestamp": "2025-01-27T11:47:59Z",
  "delivery_timestamp": "2025-01-27T11:48:29Z",
  "command": {
    "action": {
      "args": {
        "arg1": "/path/to/executable/arg6"
      },
      "name": "restart",
      "version": "v4"
    },
    "source": "Engine",
    "user": "user53",
    "order_id": "_qiXp5QBytahsG79A-l3",
    "request_id": "_aiXp5QBytahsG79A-l3",
    "timeout": 30,
    "target": {
      "id": "group004",
      "type": "group"
    },
    "status": "failure"
  }
}
{
  "agent": {
    "groups": [
      "group004",
      "group001",
      "group000"
    ]
  },
  "@timestamp": "2025-01-27T11:47:59Z",
  "delivery_timestamp": "2025-01-27T11:48:29Z",
  "command": {
    "action": {
      "args": {
        "arg1": "/path/to/executable/arg6"
      },
      "name": "restart",
      "version": "v4"
    },
    "source": "Engine",
    "user": "user53",
    "order_id": "_qiXp5QBytahsG79A-l3",
    "request_id": "_aiXp5QBytahsG79A-l3",
    "timeout": 30,
    "target": {
      "id": "group004",
      "type": "group"
    },
    "status": "failure"
  }
}

Copy link
Member

@AlexRuiz7 AlexRuiz7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la expansión de comandos no es correcta.
En una prueba que he hecho, he enviado el comando:

The commands expansion is not working as intended.

I tested using this command:

{
  "commands": [
    {
      "action": {
        "args": {},
        "name": "restart",
        "version": "5.0.0"
      },
      "source": "Users/Services",
      "user": "Management API",
      "timeout": 100,
      "target": {
        "id": "group001",
        "type": "group"
      }
    }
  ]
}

One of the expanded commands is:

      {
        "_index": "wazuh-commands",
        "_id": "J4LK0JQBHI2QBvqR03xq",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "group000",
              "group001"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "group",
              "id": "group001"
            },
            "action": {
              "name": "restart",
              "args": {

              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "JoLK0JQBHI2QBvqR03xU",
            "request_id": "JYLK0JQBHI2QBvqR03xU"
          },
          "@timestamp": "2025-02-04T11:49:00Z",
          "delivery_timestamp": "2025-02-04T11:50:40Z"
        }
      }

The result should be a command targeted to an specific agent, but it is not.

            "target": {
              "type": "agent",
              "id": "<agent-id>"
            }

Move parseCommandList to Command model

Remove unused functions on Search class
@QU3B1M
Copy link
Member Author

QU3B1M commented Feb 6, 2025

Fix for the Agent Target.type applied on commit 0995502

Generating commands using the API

% curl -s http://localhost:9200/_plugins/_command_manager/commands -H 'Content-Type: application/json' -d '{
  "commands": [
    {
      "source": "Engine",
      "user": "user53",
      "target": {
        "id": "group004",
        "type": "group"
      },
      "action": {
        "name": "restart",
        "args": {
          "arg1": "/path/to/executable/arg6"
        },
        "version": "v4"
      },
      "timeout": 30
    }
  ]
}'
{"_index":"wazuh-commands","_orders":[{"_id":"UGGy2JQBcR0nzftiFO9J"},{"_id":"U2Gy2JQBcR0nzftiFO9K"},{"_id":"VmGy2JQBcR0nzftiFO9K"},{"_id":"WWGy2JQBcR0nzftiFO9K"},{"_id":"XGGy2JQBcR0nzftiFO9K"}],"result":"OK"}

The commands are indexed and the target set correctly

% curl http://127.0.0.1:9200/wazuh-commands/_search
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "wazuh-commands",
                "_id": "UGGy2JQBcR0nzftiFO9J",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "group000",
                            "group004"
                        ]
                    },
                    "command": {
                        "source": "Engine",
                        "user": "user53",
                        "target": {
                            "type": "agent",
                            "id": "agent57"
                        },
                        "action": {
                            "name": "restart",
                            "args": {
                                "arg1": "/path/to/executable/arg6"
                            },
                            "version": "v4"
                        },
                        "timeout": 30,
                        "status": "pending",
                        "order_id": "T2Gy2JQBcR0nzftiFO9J",
                        "request_id": "TmGy2JQBcR0nzftiFO9J"
                    },
                    "@timestamp": "2025-02-06T00:38:56Z",
                    "delivery_timestamp": "2025-02-06T00:39:26Z"
                }
            },
            {
                "_index": "wazuh-commands",
                "_id": "U2Gy2JQBcR0nzftiFO9K",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "group000",
                            "group003"
                        ]
                    },
                    "command": {
                        "source": "Engine",
                        "user": "user53",
                        "target": {
                            "type": "agent",
                            "id": "agent18"
                        },
                        "action": {
                            "name": "restart",
                            "args": {
                                "arg1": "/path/to/executable/arg6"
                            },
                            "version": "v4"
                        },
                        "timeout": 30,
                        "status": "pending",
                        "order_id": "UmGy2JQBcR0nzftiFO9K",
                        "request_id": "UWGy2JQBcR0nzftiFO9K"
                    },
                    "@timestamp": "2025-02-06T00:38:56Z",
                    "delivery_timestamp": "2025-02-06T00:39:26Z"
                }
            },
            {
                "_index": "wazuh-commands",
                "_id": "VmGy2JQBcR0nzftiFO9K",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "group000",
                            "group001",
                            "group001"
                        ]
                    },
                    "command": {
                        "source": "Engine",
                        "user": "user53",
                        "target": {
                            "type": "agent",
                            "id": "agent20"
                        },
                        "action": {
                            "name": "restart",
                            "args": {
                                "arg1": "/path/to/executable/arg6"
                            },
                            "version": "v4"
                        },
                        "timeout": 30,
                        "status": "pending",
                        "order_id": "VWGy2JQBcR0nzftiFO9K",
                        "request_id": "VGGy2JQBcR0nzftiFO9K"
                    },
                    "@timestamp": "2025-02-06T00:38:56Z",
                    "delivery_timestamp": "2025-02-06T00:39:26Z"
                }
            },
            {
                "_index": "wazuh-commands",
                "_id": "WWGy2JQBcR0nzftiFO9K",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "group004",
                            "group003",
                            "group000"
                        ]
                    },
                    "command": {
                        "source": "Engine",
                        "user": "user53",
                        "target": {
                            "type": "agent",
                            "id": "agent57"
                        },
                        "action": {
                            "name": "restart",
                            "args": {
                                "arg1": "/path/to/executable/arg6"
                            },
                            "version": "v4"
                        },
                        "timeout": 30,
                        "status": "pending",
                        "order_id": "WGGy2JQBcR0nzftiFO9K",
                        "request_id": "V2Gy2JQBcR0nzftiFO9K"
                    },
                    "@timestamp": "2025-02-06T00:38:56Z",
                    "delivery_timestamp": "2025-02-06T00:39:26Z"
                }
            },
            {
                "_index": "wazuh-commands",
                "_id": "XGGy2JQBcR0nzftiFO9K",
                "_score": 1.0,
                "_source": {
                    "agent": {
                        "groups": [
                            "group003",
                            "group000",
                            "group004",
                            "group003"
                        ]
                    },
                    "command": {
                        "source": "Engine",
                        "user": "user53",
                        "target": {
                            "type": "agent",
                            "id": "agent14"
                        },
                        "action": {
                            "name": "restart",
                            "args": {
                                "arg1": "/path/to/executable/arg6"
                            },
                            "version": "v4"
                        },
                        "timeout": 30,
                        "status": "pending",
                        "order_id": "W2Gy2JQBcR0nzftiFO9K",
                        "request_id": "WmGy2JQBcR0nzftiFO9K"
                    },
                    "@timestamp": "2025-02-06T00:38:56Z",
                    "delivery_timestamp": "2025-02-06T00:39:26Z"
                }
            }
        ]
    }
}

@QU3B1M QU3B1M requested a review from AlexRuiz7 February 6, 2025 00:49
Copy link
Member

@AlexRuiz7 AlexRuiz7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Code Review ✔️
  • Test ✔️

My test consisted on indexing this command:

{
  "commands": [
    {
      "action": {
        "args": {},
        "name": "fetch-config",
        "version": "5.0.0"
      },
      "source": "Users/Services",
      "user": "Management API",
      "timeout": 100,
      "target": {
        "id": "group003",
        "type": "group"
      }
    }
  ]
}

which replied with:

{
  "_index": "wazuh-commands",
  "orders": [
    {
      "_id": "Yfks75QBIaiqx4S6knYU"
    },
    {
      "_id": "ZPks75QBIaiqx4S6knYV"
    },
    {
      "_id": "Z_ks75QBIaiqx4S6knYV"
    },
    {
      "_id": "avks75QBIaiqx4S6knYV"
    }
  ],
  "result": "OK"
}

Then I checked the generated commands (4), and checked these were correct:

  • The generated commands contain the correct Agent ID and Groups.
  • The generated commands are of type agent.
  • The generated commands inherit the parent's command metadata, such as action, args and timeout.

wazuh-agents

{"took":18,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":10,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"wazuh-agents","_id":"U_kr75QBIaiqx4S6BXYs","_score":1.0,"_source":{
  "agent": {
    "id": "agent40",
    "name": "Agent86",
    "type": "macos",
    "version": "v0-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group003","group000","group000"],
    "key": "key569",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot7512"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 27158}, "write": {"bytes": 23765}},
    "domain": "domain385",
    "geo": {
      "city_name": "Tokyo",
      "continent_code": "NA",
      "continent_name": "Europe",
      "country_iso_code": "US",
      "country_name": "United States",
      "location": {"lat": -89.999980, "lon": -180.000000},
      "name": "geo931",
      "postal_code": "23978",
      "region_iso_code": "region909",
      "region_name": "Region 555",
      "timezone": "CET"
    },
    "hostname": "host1969",
    "id": "hostid9052",
    "ip": "163.248.164.197",
    "mac": "f1:0c:23:aa:01:7d",
    "name": "hostname1755",
    "network": {"egress": {"bytes": 17678, "packets": 14061}, "ingress": {"bytes": 2109, "packets": 22041}},
    "os": {"family": "RHEL", "full": "RHEL 74.51", "kernel": "kernel411", "name": "RHEL", "platform": "windows", "type": "RHEL", "version": "74.51"},
    "pid_ns_ino": "1026832",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "low", "static_score": 0, "static_score_norm": 0},
    "uptime": 12418
  }
  }
}},{"_index":"wazuh-agents","_id":"VPkr75QBIaiqx4S6BXau","_score":1.0,"_source":{
  "agent": {
    "id": "agent89",
    "name": "Agent93",
    "type": "linux",
    "version": "v9-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group000"],
    "key": "key788",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot2697"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 8541}, "write": {"bytes": 10605}},
    "domain": "domain214",
    "geo": {
      "city_name": "San Francisco",
      "continent_code": "AS",
      "continent_name": "North America",
      "country_iso_code": "DE",
      "country_name": "United States",
      "location": {"lat": -89.999880, "lon": -180.000000},
      "name": "geo306",
      "postal_code": "29717",
      "region_iso_code": "region890",
      "region_name": "Region 483",
      "timezone": "EST"
    },
    "hostname": "host1051",
    "id": "hostid4550",
    "ip": "42.38.102.141",
    "mac": "48:b0:ba:1e:c5:8b",
    "name": "hostname8986",
    "network": {"egress": {"bytes": 10265, "packets": 28895}, "ingress": {"bytes": 409, "packets": 18429}},
    "os": {"family": "ios", "full": "ios 50.72", "kernel": "kernel596", "name": "ios", "platform": "windows", "type": "ios", "version": "50.72"},
    "pid_ns_ino": "1016433",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "low", "static_score": 0, "static_score_norm": 0},
    "uptime": 26170
  }
  }
}},{"_index":"wazuh-agents","_id":"Vfkr75QBIaiqx4S6BXbW","_score":1.0,"_source":{
  "agent": {
    "id": "agent55",
    "name": "Agent83",
    "type": "windows",
    "version": "v8-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group005","group005"],
    "key": "key492",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot2437"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 20915}, "write": {"bytes": 15460}},
    "domain": "domain357",
    "geo": {
      "city_name": "Tokyo",
      "continent_code": "EU",
      "continent_name": "North America",
      "country_iso_code": "US",
      "country_name": "Germany",
      "location": {"lat": -89.999980, "lon": -179.999880},
      "name": "geo741",
      "postal_code": "17147",
      "region_iso_code": "region519",
      "region_name": "Region 860",
      "timezone": "JST"
    },
    "hostname": "host9290",
    "id": "hostid4319",
    "ip": "86.237.182.144",
    "mac": "4d:e8:34:8a:c0:51",
    "name": "hostname894",
    "network": {"egress": {"bytes": 15442, "packets": 9710}, "ingress": {"bytes": 12847, "packets": 6326}},
    "os": {"family": "android", "full": "android 78.56", "kernel": "kernel141", "name": "android", "platform": "linux", "type": "android", "version": "78.56"},
    "pid_ns_ino": "1027413",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "high", "static_score": 0, "static_score_norm": 0},
    "uptime": 2498
  }
  }
}},{"_index":"wazuh-agents","_id":"Vvkr75QBIaiqx4S6BXb9","_score":1.0,"_source":{
  "agent": {
    "id": "agent36",
    "name": "Agent63",
    "type": "windows",
    "version": "v3-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group001","group002"],
    "key": "key230",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot2415"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 19418}, "write": {"bytes": 14935}},
    "domain": "domain411",
    "geo": {
      "city_name": "Berlin",
      "continent_code": "NA",
      "continent_name": "Europe",
      "country_iso_code": "US",
      "country_name": "United States",
      "location": {"lat": -89.999960, "lon": -179.999680},
      "name": "geo389",
      "postal_code": "24527",
      "region_iso_code": "region724",
      "region_name": "Region 185",
      "timezone": "JST"
    },
    "hostname": "host1643",
    "id": "hostid3199",
    "ip": "228.241.134.236",
    "mac": "c7:28:47:64:36:bd",
    "name": "hostname1544",
    "network": {"egress": {"bytes": 11924, "packets": 6914}, "ingress": {"bytes": 23225, "packets": 29544}},
    "os": {"family": "android", "full": "android 83.91", "kernel": "kernel840", "name": "android", "platform": "linux", "type": "android", "version": "83.91"},
    "pid_ns_ino": "1014370",
    "risk": {"calculated_level": "low", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "low", "static_score": 0, "static_score_norm": 0},
    "uptime": 17012
  }
  }
}},{"_index":"wazuh-agents","_id":"V_kr75QBIaiqx4S6BnZk","_score":1.0,"_source":{
  "agent": {
    "id": "agent45",
    "name": "Agent4",
    "type": "macos",
    "version": "v1-stable",
    "status": "active",
    "last_login": "",
    "groups": ["group004","group000"],
    "key": "key204",
    "host": {
    "architecture": "arm64",
    "boot": {"id": "boot8450"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 15972}, "write": {"bytes": 17845}},
    "domain": "domain636",
    "geo": {
      "city_name": "Berlin",
      "continent_code": "NA",
      "continent_name": "Europe",
      "country_iso_code": "JP",
      "country_name": "Germany",
      "location": {"lat": -89.999860, "lon": -179.999680},
      "name": "geo685",
      "postal_code": "27773",
      "region_iso_code": "region870",
      "region_name": "Region 794",
      "timezone": "PST"
    },
    "hostname": "host4213",
    "id": "hostid733",
    "ip": "62.174.98.126",
    "mac": "38:7f:ce:89:3b:4a",
    "name": "hostname799",
    "network": {"egress": {"bytes": 12011, "packets": 32029}, "ingress": {"bytes": 1442, "packets": 10380}},
    "os": {"family": "macos", "full": "macos 36.20", "kernel": "kernel702", "name": "macos", "platform": "windows", "type": "macos", "version": "36.20"},
    "pid_ns_ino": "1014130",
    "risk": {"calculated_level": "low", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "low", "static_score": 0, "static_score_norm": 0},
    "uptime": 12706
  }
  }
}},{"_index":"wazuh-agents","_id":"WPkr75QBIaiqx4S6BnaX","_score":1.0,"_source":{
  "agent": {
    "id": "agent54",
    "name": "Agent73",
    "type": "windows",
    "version": "v9-stable",
    "status": "active",
    "last_login": "",
    "groups": ["group003","group000","group005"],
    "key": "key605",
    "host": {
    "architecture": "arm64",
    "boot": {"id": "boot7642"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 14956}, "write": {"bytes": 13608}},
    "domain": "domain930",
    "geo": {
      "city_name": "San Francisco",
      "continent_code": "AS",
      "continent_name": "Europe",
      "country_iso_code": "US",
      "country_name": "Japan",
      "location": {"lat": -89.999920, "lon": -179.999800},
      "name": "geo738",
      "postal_code": "15280",
      "region_iso_code": "region614",
      "region_name": "Region 156",
      "timezone": "JST"
    },
    "hostname": "host1813",
    "id": "hostid8847",
    "ip": "97.224.171.248",
    "mac": "fb:54:83:04:d5:58",
    "name": "hostname8459",
    "network": {"egress": {"bytes": 18892, "packets": 31610}, "ingress": {"bytes": 9003, "packets": 12240}},
    "os": {"family": "RHEL", "full": "RHEL 99.3", "kernel": "kernel795", "name": "RHEL", "platform": "windows", "type": "RHEL", "version": "99.3"},
    "pid_ns_ino": "1019739",
    "risk": {"calculated_level": "low", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "medium", "static_score": 0, "static_score_norm": 0},
    "uptime": 24874
  }
  }
}},{"_index":"wazuh-agents","_id":"Wfkr75QBIaiqx4S6BnbH","_score":1.0,"_source":{
  "agent": {
    "id": "agent39",
    "name": "Agent54",
    "type": "macos",
    "version": "v8-stable",
    "status": "active",
    "last_login": "",
    "groups": ["group005"],
    "key": "key65",
    "host": {
    "architecture": "arm64",
    "boot": {"id": "boot485"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 18564}, "write": {"bytes": 1714}},
    "domain": "domain968",
    "geo": {
      "city_name": "New York",
      "continent_code": "EU",
      "continent_name": "Europe",
      "country_iso_code": "DE",
      "country_name": "United States",
      "location": {"lat": -89.999860, "lon": -179.999840},
      "name": "geo951",
      "postal_code": "26324",
      "region_iso_code": "region243",
      "region_name": "Region 969",
      "timezone": "EST"
    },
    "hostname": "host7558",
    "id": "hostid6504",
    "ip": "12.10.40.137",
    "mac": "dc:39:3f:bf:1f:86",
    "name": "hostname2080",
    "network": {"egress": {"bytes": 31910, "packets": 27080}, "ingress": {"bytes": 8875, "packets": 22659}},
    "os": {"family": "android", "full": "android 58.55", "kernel": "kernel284", "name": "android", "platform": "macos", "type": "android", "version": "58.55"},
    "pid_ns_ino": "1020811",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "low", "static_score": 0, "static_score_norm": 0},
    "uptime": 29991
  }
  }
}},{"_index":"wazuh-agents","_id":"Wvkr75QBIaiqx4S6Bnby","_score":1.0,"_source":{
  "agent": {
    "id": "agent70",
    "name": "Agent96",
    "type": "macos",
    "version": "v2-stable",
    "status": "active",
    "last_login": "",
    "groups": ["group000"],
    "key": "key507",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot7506"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 15119}, "write": {"bytes": 20778}},
    "domain": "domain332",
    "geo": {
      "city_name": "Berlin",
      "continent_code": "AS",
      "continent_name": "North America",
      "country_iso_code": "JP",
      "country_name": "United States",
      "location": {"lat": -89.999960, "lon": -179.999920},
      "name": "geo949",
      "postal_code": "29865",
      "region_iso_code": "region724",
      "region_name": "Region 10",
      "timezone": "PST"
    },
    "hostname": "host4556",
    "id": "hostid2512",
    "ip": "114.122.75.30",
    "mac": "c1:2f:0e:ea:78:30",
    "name": "hostname3612",
    "network": {"egress": {"bytes": 31960, "packets": 23026}, "ingress": {"bytes": 26720, "packets": 6590}},
    "os": {"family": "macos", "full": "macos 20.25", "kernel": "kernel826", "name": "macos", "platform": "linux", "type": "macos", "version": "20.25"},
    "pid_ns_ino": "1019876",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "medium", "static_score": 0, "static_score_norm": 0},
    "uptime": 29890
  }
  }
}},{"_index":"wazuh-agents","_id":"W_kr75QBIaiqx4S6B3aA","_score":1.0,"_source":{
  "agent": {
    "id": "agent21",
    "name": "Agent29",
    "type": "macos",
    "version": "v5-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group001","group003"],
    "key": "key348",
    "host": {
    "architecture": "x86_64",
    "boot": {"id": "boot2009"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 5693}, "write": {"bytes": 15255}},
    "domain": "domain213",
    "geo": {
      "city_name": "San Francisco",
      "continent_code": "EU",
      "continent_name": "Europe",
      "country_iso_code": "DE",
      "country_name": "Japan",
      "location": {"lat": -89.999900, "lon": -179.999880},
      "name": "geo829",
      "postal_code": "15242",
      "region_iso_code": "region415",
      "region_name": "Region 463",
      "timezone": "EST"
    },
    "hostname": "host6720",
    "id": "hostid1278",
    "ip": "70.255.157.202",
    "mac": "63:e2:37:0f:e5:95",
    "name": "hostname993",
    "network": {"egress": {"bytes": 10582, "packets": 13672}, "ingress": {"bytes": 11474, "packets": 21777}},
    "os": {"family": "ios", "full": "ios 40.18", "kernel": "kernel836", "name": "ios", "platform": "windows", "type": "ios", "version": "40.18"},
    "pid_ns_ino": "1016987",
    "risk": {"calculated_level": "medium", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "medium", "static_score": 0, "static_score_norm": 0},
    "uptime": 16665
  }
  }
}},{"_index":"wazuh-agents","_id":"XPkr75QBIaiqx4S6B3bp","_score":1.0,"_source":{
  "agent": {
    "id": "agent6",
    "name": "Agent29",
    "type": "macos",
    "version": "v0-stable",
    "status": "inactive",
    "last_login": "",
    "groups": ["group003"],
    "key": "key276",
    "host": {
    "architecture": "arm64",
    "boot": {"id": "boot6181"},
    "cpu": {"usage": 0},
    "disk": {"read": {"bytes": 30392}, "write": {"bytes": 9088}},
    "domain": "domain184",
    "geo": {
      "city_name": "San Francisco",
      "continent_code": "AS",
      "continent_name": "Asia",
      "country_iso_code": "DE",
      "country_name": "Germany",
      "location": {"lat": -89.999840, "lon": -179.999880},
      "name": "geo13",
      "postal_code": "13481",
      "region_iso_code": "region481",
      "region_name": "Region 38",
      "timezone": "PST"
    },
    "hostname": "host2509",
    "id": "hostid440",
    "ip": "188.221.233.178",
    "mac": "bf:1c:8d:98:e0:c4",
    "name": "hostname5436",
    "network": {"egress": {"bytes": 28342, "packets": 5610}, "ingress": {"bytes": 31814, "packets": 28801}},
    "os": {"family": "RHEL", "full": "RHEL 0.81", "kernel": "kernel531", "name": "RHEL", "platform": "macos", "type": "RHEL", "version": "0.81"},
    "pid_ns_ino": "1019191",
    "risk": {"calculated_level": "low", "calculated_score": 0, "calculated_score_norm": 0, "static_level": "medium", "static_score": 0, "static_score_norm": 0},
    "uptime": 1606
  }
  }
}}]}}

wazuh-commands

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "wazuh-commands",
        "_id": "Yfks75QBIaiqx4S6knYU",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "group003"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "agent",
              "id": "agent6"
            },
            "action": {
              "name": "fetch-config",
              "args": {

              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "YPks75QBIaiqx4S6knYU",
            "request_id": "X_ks75QBIaiqx4S6knYU"
          },
          "@timestamp": "2025-02-10T09:24:23Z",
          "delivery_timestamp": "2025-02-10T09:26:03Z"
        }
      },
      {
        "_index": "wazuh-commands",
        "_id": "ZPks75QBIaiqx4S6knYV",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "group001",
              "group003"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "agent",
              "id": "agent21"
            },
            "action": {
              "name": "fetch-config",
              "args": {

              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "Y_ks75QBIaiqx4S6knYV",
            "request_id": "Yvks75QBIaiqx4S6knYV"
          },
          "@timestamp": "2025-02-10T09:24:23Z",
          "delivery_timestamp": "2025-02-10T09:26:03Z"
        }
      },
      {
        "_index": "wazuh-commands",
        "_id": "Z_ks75QBIaiqx4S6knYV",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "group003",
              "group000",
              "group000"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "agent",
              "id": "agent40"
            },
            "action": {
              "name": "fetch-config",
              "args": {

              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "Zvks75QBIaiqx4S6knYV",
            "request_id": "Zfks75QBIaiqx4S6knYV"
          },
          "@timestamp": "2025-02-10T09:24:23Z",
          "delivery_timestamp": "2025-02-10T09:26:03Z"
        }
      },
      {
        "_index": "wazuh-commands",
        "_id": "avks75QBIaiqx4S6knYV",
        "_score": 1,
        "_source": {
          "agent": {
            "groups": [
              "group003",
              "group000",
              "group005"
            ]
          },
          "command": {
            "source": "Users/Services",
            "user": "Management API",
            "target": {
              "type": "agent",
              "id": "agent54"
            },
            "action": {
              "name": "fetch-config",
              "args": {

              },
              "version": "5.0.0"
            },
            "timeout": 100,
            "status": "pending",
            "order_id": "afks75QBIaiqx4S6knYV",
            "request_id": "aPks75QBIaiqx4S6knYV"
          },
          "@timestamp": "2025-02-10T09:24:23Z",
          "delivery_timestamp": "2025-02-10T09:26:03Z"
        }
      }
    ]
  }
}

@AlexRuiz7 AlexRuiz7 merged commit 4da6866 into master Feb 10, 2025
1 check passed
@AlexRuiz7 AlexRuiz7 deleted the enhancement/88-implement-commands-expansion branch February 10, 2025 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement commands' expansion logic
3 participants