1
1
#!/bin/python3
2
2
3
- # This script generates sample events and injects them into an OpenSearch index.
4
- # The events follow the provided template structure with command-related data fields.
5
- # Additional fields are generated when the --index option is passed.
6
-
7
3
import random
8
4
import json
9
5
import requests
10
6
import warnings
11
7
import logging
12
8
import argparse
9
+ import uuid
13
10
14
11
LOG_FILE = 'generate_data.log'
15
12
GENERATED_DATA_FILE = 'generatedData.json'
22
19
23
20
24
21
def generate_random_command (include_all_fields = False ):
25
- command = {
26
- "source" : random .choice (["Users/Services" , "Engine" , "Content manager" ]),
27
- "user" : f"user{ random .randint (1 , 100 )} " ,
28
- "target" : f"WazuhServerCluster{ random .randint (1 , 10 )} " ,
29
- "type" : random .choice (["agent_group" , "agent" , "wazuh_server" ]),
30
- "action" : {
31
- "type" : random .choice (["Agent groups" , "Agent" , "Server cluster" ]),
32
- "args" : [f"/path/to/executable/arg{ random .randint (1 , 10 )} " ],
33
- "version" : f"v{ random .randint (1 , 10 )} "
34
- },
35
- "timeout" : random .randint (10 , 100 )
22
+ document = {
23
+ "command" : {
24
+ "source" : random .choice (["Users/Services" , "Engine" , "Content manager" ]),
25
+ "user" : f"user{ random .randint (1 , 100 )} " ,
26
+ "target" : {
27
+ "id" : f"target{ random .randint (1 , 10 )} " ,
28
+ "type" : random .choice (["agent" , "group" , "server" ])
29
+ },
30
+ "action" : {
31
+ "name" : random .choice (["restart" , "update" , "change_group" , "apply_policy" ]),
32
+ "args" : [f"/path/to/executable/arg{ random .randint (1 , 10 )} " ],
33
+ "version" : f"v{ random .randint (1 , 5 )} "
34
+ },
35
+ "timeout" : random .randint (10 , 100 )
36
+ }
36
37
}
37
38
38
39
if include_all_fields :
39
- command [ "status" ] = random .choice (
40
- [ "pending" , "sent" , "success" , "failure" ]
41
- )
42
- command ["result" ] = {
40
+ document [ "agent" ][ "groups" ] = [ f"group { random .randint ( 1 , 5 ) } " ],
41
+ document [ "command" ][ "status" ] = random . choice (
42
+ [ "pending" , "sent" , "success" , "failure" ] )
43
+ document [ " command" ] ["result" ] = {
43
44
"code" : random .randint (0 , 255 ),
44
45
"message" : f"Result message { random .randint (1 , 1000 )} " ,
45
46
"data" : f"Result data { random .randint (1 , 100 )} "
46
47
}
47
- command ["request_id" ] = random .randint (1000 , 9999 )
48
- command ["order_id" ] = random .randint (1000 , 9999 )
48
+ # Generate UUIDs for request_id and order_id
49
+ document ["command" ]["request_id" ] = str (uuid .uuid4 ())
50
+ document ["command" ]["order_id" ] = str (uuid .uuid4 ())
49
51
50
- return command
52
+ return document
51
53
52
54
53
55
def generate_random_data (number , include_all_fields = False ):
@@ -58,8 +60,6 @@ def generate_random_data(number, include_all_fields=False):
58
60
59
61
60
62
def inject_events (ip , port , index , username , password , data , use_index = False ):
61
- url = f'https://{ ip } :{ port } /_plugins/_commandmanager'
62
-
63
63
session = requests .Session ()
64
64
session .auth = (username , password )
65
65
session .verify = False
@@ -68,8 +68,12 @@ def inject_events(ip, port, index, username, password, data, use_index=False):
68
68
try :
69
69
for event_data in data :
70
70
if use_index :
71
- id = event_data ["request_id" ] + event_data ["order_id" ]
72
- url = f'https://{ ip } :{ port } /{ index } /_doc/{ id } '
71
+ # Generate UUIDs for the document id
72
+ doc_id = str (uuid .uuid4 ())
73
+ url = f'https://{ ip } :{ port } /{ index } /_doc/{ doc_id } '
74
+ else :
75
+ # Default URL for command manager API without the index
76
+ url = f'https://{ ip } :{ port } /_plugins/_commandmanager'
73
77
74
78
response = session .post (url , json = event_data , headers = headers )
75
79
if response .status_code != 201 :
@@ -83,7 +87,8 @@ def inject_events(ip, port, index, username, password, data, use_index=False):
83
87
84
88
def main ():
85
89
parser = argparse .ArgumentParser (
86
- description = "Generate and optionally inject events into an OpenSearch index or Command Manager." )
90
+ description = "Generate and optionally inject events into an OpenSearch index or Command Manager."
91
+ )
87
92
parser .add_argument (
88
93
"--index" ,
89
94
action = "store_true" ,
@@ -108,7 +113,8 @@ def main():
108
113
logging .info ('Data generation completed.' )
109
114
110
115
inject = input (
111
- "Do you want to inject the generated data into your indexer/command manager? (y/n) " ).strip ().lower ()
116
+ "Do you want to inject the generated data into your indexer/command manager? (y/n) "
117
+ ).strip ().lower ()
112
118
if inject == 'y' :
113
119
ip = input ("Enter the IP of your Indexer: " )
114
120
port = input ("Enter the port of your Indexer: " )
0 commit comments