wazhu之agent manage
代理生命周期
注册代理
一旦代理程序安装在要监控的计算机上,就必须向Wazuh管理器注册才能建立通信。这可以通过命令行,Authd或RESTful API完成。
注册代理将保留在管理器中,直到用户将其删除。在任何给定时间内,代理可能有四种不同的状态,如下图所示:
代理状态
- 从未连接:代理已注册但尚未连接到管理器。
- 待定 身份验证过程正在等待:管理服务器已收到来自代理的连接请求,但尚未收到任何其他内容。这可能表示防火墙问题。代理将在其连接生命周期中处于此状态一次。
- 活动:代理已成功连接,现在可以与管理器通信。
- 已断开连接:如果代理在半小时内未收到来自代理的任何消息,则管理员将认为代理已断开连接。
删除代理
从agent主机的管理器中删除代理程序后,连接生命周期即将结束。这可以通过RESTful API,命令行或Authd完成(如果启用了force选项)。
强制插入
如果您尝试添加具有已注册到其他代理的IP地址的代理,该
manage_agents
命令将返回错误。您仍然可以使用-F选项强制添加。列如:
安装了名为Server1的IP 10.0.0.10 的代理,并且ID为005.如果我们假设必须重新安装服务器,则必须重新安装新的代理并将其连接到管理器。在这种情况下,我们可以使用参数-F 0,这意味着将删除先前的代理(005)(使用备份),并且将使用IP重新创建新代理。新代理将具有新ID:
/var/ossec/bin/manage_agents -n Server1 -a 10.10.10.10 -F 0列出代理
运行 /var/ossec/bin/agent_control 可列出代理的连接状态:
[root@wazhu-manage ~]# /var/ossec/bin/agent_control -lWazuh agent_control. List of available agents:ID: 000, Name: wazhu-manage (server), IP: 127.0.0.1, Active/LocalID: 001, Name: agent01, IP: 45.77.105.194, ActiveID: 002, Name: agent02, IP: 155.138.165.154, ActiveID: 003, Name: agent03, IP: 45.77.93.54, ActiveID: 004, Name: agent04, IP: 45.77.4.139, ActiveList of agentless devices:删除代理
运行 /var/ossec/bin/manage_agents 删除代理
如果要在删除代理之前进行确认,请使用以下命令:
[root@wazhu-manage ~]# /var/ossec/bin/manage_agents***************************************** Wazuh v3.8.0 Agent manager. ** The following options are available: *****************************************(A)dd an agent (A).(E)xtract key for an agent (E).(L)ist already added agents (L).(R)emove an agent (R).(Q)uit.Choose your action: A,E,L,R or Q: RAvailable agents:ID: 001, Name: agent01, IP: 45.77.105.194ID: 002, Name: agent02, IP: 155.138.165.154ID: 003, Name: agent03, IP: 45.77.93.54ID: 004, Name: agent04, IP: 45.77.4.139Provide the ID of the agent to be removed (or '\q' to quit): 004Confirm deleting it?(y/n): YAgent '004' removed.***************************************** Wazuh v3.8.0 Agent manager. ** The following options are available: *****************************************(A)dd an agent (A).(E)xtract key for an agent (E).(L)ist already added agents (L).(R)emove an agent (R).(Q)uit.Choose your action: A,E,L,R or Q: Q如果您想在没有确认的情况下删除代理,请使用以下选项:
# /var/ossec/bin/manage_agents -r 001 **************************************** * Wazuh v3.8.0 Agent manager. * * The following options are available: * **************************************** (A)dd an agent (A). (E)xtract key for an agent (E). (L)ist already added agents (L). (R)emove an agent (R). (Q)uit. Choose your action: A,E,L,R or Q: Available agents: ID: 001, Name: new, IP: any Provide the ID of the agent to be removed (or '\q' to quit): 001 Confirm deleting it?(y/n): y Agent '001' removed. ** You must restart OSSEC for your changes to take effect. manage_agents: Exiting.列出代理连接状态
请求GET /agents 返回可用代理列表。
注意:GET是curl的默认操作,不需要特别引用。
[root@wazhu-manage ~]# curl -u wazuh-api:wazu123 "http://149.248.9.0:55000/agents?pretty"{"error": 0,"data": {"totalItems": 5,"items": [{"status": "Active","name": "wazhu-manage","ip": "127.0.0.1","manager": "wazhu-manage","node_name": "node01","dateAdd": "2019-01-21 09:58:43","version": "Wazuh v3.8.0","lastKeepAlive": "9999-12-31 23:59:59","os": {"major": "7","name": "CentOS Linux","uname": "Linux |wazhu-manage |3.10.0-957.1.3.el7.x86_64 |#1 SMP Thu Nov 29 14:49:43 UTC 2018 |x86_64","platform": "centos","version": "7","codename": "Core","arch": "x86_64"},"id": "000"},{"status": "Active","configSum": "ab73af41699f13fdd81903b5f23d8d00","group": ["default"],"name": "agent01","mergedSum": "f8d49771911ed9d5c45b03a40babd065","ip": "45.77.105.194","manager": "wazhu-manage","node_name": "node01","dateAdd": "2019-01-22 07:14:26","version": "Wazuh v3.8.0","lastKeepAlive": "2019-01-22 18:11:46","os": {"major": "16","name": "Ubuntu","uname": "Linux |agent01 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64","platform": "ubuntu","version": "16.04.5 LTS","codename": "Xenial Xerus","arch": "x86_64","minor": "04"},"id": "001"},{"status": "Active","configSum": "ab73af41699f13fdd81903b5f23d8d00","group": ["default"],"name": "agent02","mergedSum": "f8d49771911ed9d5c45b03a40babd065","ip": "155.138.165.154","manager": "wazhu-manage","node_name": "node01","dateAdd": "2019-01-22 09:29:21","version": "Wazuh v3.8.0","lastKeepAlive": "2019-01-22 18:11:39","os": {"major": "16","name": "Ubuntu","uname": "Linux |agent02 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64","platform": "ubuntu","version": "16.04.5 LTS","codename": "Xenial Xerus","arch": "x86_64","minor": "04"},"id": "002"},{"status": "Active","configSum": "ab73af41699f13fdd81903b5f23d8d00","group": ["default"],"name": "agent03","mergedSum": "f8d49771911ed9d5c45b03a40babd065","ip": "45.77.93.54","manager": "wazhu-manage","node_name": "node01","dateAdd": "2019-01-22 10:15:26","version": "Wazuh v3.8.0","lastKeepAlive": "2019-01-22 18:11:42","os": {"major": "16","name": "Ubuntu","uname": "Linux |agent03 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64","platform": "ubuntu","version": "16.04.5 LTS","codename": "Xenial Xerus","arch": "x86_64","minor": "04"},"id": "003"},{"status": "Active","configSum": "ab73af41699f13fdd81903b5f23d8d00","group": ["default"],"name": "agent04","mergedSum": "f8d49771911ed9d5c45b03a40babd065","ip": "45.77.4.139","manager": "wazhu-manage","node_name": "node01","dateAdd": "2019-01-22 10:34:01","version": "Wazuh v3.8.0","lastKeepAlive": "2019-01-22 18:11:43","os": {"major": "16","name": "Ubuntu","uname": "Linux |agent04 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64","platform": "ubuntu","version": "16.04.5 LTS","codename": "Xenial Xerus","arch": "x86_64","minor": "04"},"id": "004"}]}}删除代理
请求DELETE /agents/:agent_id删除指定的代理。
[root@wazhu-manage ~]# curl -u wazuh-api:wazuh -X DELETE "http://149.248.9.0:55000/agents/003"{"error":0,"data":{"msg":"All selected agents were removed","affected_agents":["003"]}}
检查与Manager的连接
在检查代理与管理器的连接之前,请首先确保代理指向管理器的IP地址。这是ossec.conf
使用<client>
XML标记设置的。有关详细信息,请参阅客户端参考。
<ossec_config><client><server><address>149.248.9.0/address><protocol>udp</protocol></server></client></ossec_config>
这将设置149.248.9.0作为Wazuh Manager服务器。完成此操作后,您需要重新启动代理:
a.For Systemd:
# systemctl restart wazuh-agent
b.For SysV Init:
# service wazuh-agent restart
注册代理并且已成功连接后,您可以看到连接到管理器的代理列表:
[root@wazhu-manage ~]# /var/ossec/bin/agent_control -lc #代理管理器
Wazuh agent_control. List of available agents:
ID: 000, Name: wazhu-manage (server), IP: 127.0.0.1, Active/Local
ID: 001, Name: agent01, IP: 45.77.105.194, Active
ID: 002, Name: agent02, IP: 155.138.165.154, Active
您还可以通过验证是否已建立与管理器的UDP连接来检查代理是否正确连接:
root@agent02:~# netstat -vatunp|grep ossec-agentd #代理客服端上
udp 0 0 155.138.165.154:58599 149.248.9.0:1514 ESTABLISHED 5088/ossec-agentd
结果应与代理和管理器IP地址匹配。
在agent_control部分中,您可以找到有关向管理器注册的代理的状态的信息。
分组代理
3.0.0版中的新功能。
配置注册代理有两种方法。可以使用ossec.conf文件在本地配置它们,也可以使用集中配置远程配置它们。如果使用集中式配置,则可以将代理分配给组,每个组具有唯一的配置。这极大地简化了整个配置过程。
除非另行指定,否则所有新代理都自动属于“默认”组。在安装过程中创建此组,并将配置文件放在/var/ossec/etc/shared/default/
文件夹中。这些文件将从管理器推送到属于该组的所有代理。
1.以下是将代理分配给具有特定配置的组的步骤:
将代理添加到管理器后,使用agent_groups工具或 API将其分配给组。以下方法是将具有ID 002的代理分配给组“test01”示列:
使用agent_groups:
[root@wazhu-manage default]# cd /var/ossec/etc/shared
[root@wazhu-manage shared]# mkdir test01
[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -a -i 002 -g test01
Do you want to add the group 'test01' to the agent '002'? [y/N]: y
Group 'test01' added to agent '002'.
使用API:
# curl -u wazuh-api:wazuh -X PUT "http://localhost:55000/agents/002/group/test01?pretty"
注意:必须在分配代理之前创建和配置该组。
2.可以使用以下命令之一检查代理程序的组分配:
使用agent_groups:
[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -l -g test01
1 agent(s) in group 'test01':
ID: 002 Name: agent02.
使用API:
[root@wazhu-manage test01]# curl -u wazuh-api:wazuh -X GET "http://localhost:55000/agents/groups/test01?pretty"
{
"error": 0,
"data": {
"totalItems": 1,
"items": [
{
"status": "Active",
"configSum": "1a0e4e4aa11b2b284200925e3026090f",
"group": [
"default",
"test01"
],
"name": "agent02",
"mergedSum": "2bdddd3efec5ffb7ec1486e314c28eb7",
"ip": "155.138.165.154",
"manager": "wazhu-manage",
"node_name": "node01",
"dateAdd": "2019-01-22 09:29:21",
"version": "Wazuh v3.8.0",
"lastKeepAlive": "2019-01-23 02:22:58",
"os": {
"major": "16",
"name": "Ubuntu",
"uname": "Linux |agent02 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64",
"platform": "ubuntu",
"version": "16.04.5 LTS",
"codename": "Xenial Xerus",
"arch": "x86_64",
"minor": "04"
},
"id": "002"
}
]
}
}
创建组后,agent.conf可以编辑其文件以包含要分配给该组的特定配置。对于此示例,要编辑的文件位于/var/ossec/etc/shared/test01/agent.conf中,并且属于该组的每个代理都将接收此文件。在连接到管理器的20分钟内,分配给组的每个代理将从管理器接收“test01”文件夹中包含的agent.conf文件,包括在上一步中修改的文件。管理器将这些文件推送到代理程序所需的时间长度取决于文件的大小,组中的代理程序数以及使用的连接协议。例如,根据网络带宽和性能,使用UDP在100个代理上接收10 MB文件夹(不包括merged.mg文件)可能需要8分钟,但是如果使用TCP,则可能会更快地复制。一旦特定代理属于某个组,即使它是以其他名称或ID注册,它也会自动重新分配给该组。发生这种情况是因为当代理重新注册时,代理merged.mg发送的校验和将与向管理员注册的其他代理的校验和进行比较。然而,这不是默认的设置,而且,如果需要的话,必须明确地在local_internal_options.conf文件中通过添加选项remoted.guess_agent_group=1进行激活(参见remoted在内部选件)。
多个组
3.7.0版中的新功能。
自Wazuh v3.7.0起,代理可以属于多个组。代理将从每个组接收所有配置文件。从上次分配的组接收的配置优先于其他组。
管理多个组
此功能专注于以更高的细分级别自定义代理的配置。通过API和agent_groups 列出代理状态,同时也允许分配/变更/取消分配组,代理管理组。让我们看一下在现有代理上管理多个组的三个用例。
将多个组分配给代理
将多个组设置为代理很简单,有三种不同的方法可以将代理分配给一个或多个组:注册,CLI和API。
在此示例中,代理001已添加到Web服务器和apache组。首先,使用API:
[root@wazhu-manage default]# cd /var/ossec/etc/shared
[root@wazhu-manage shared]# mkdir webserver
[root@wazhu-manage shared]# mkdir apache
[root@wazhu-manage shared]# curl -u wazuh-api:wazuh -X PUT "http://localhost:55000/agents/001/group/webserver?pretty"
{
"error": 0,
"data": "Group 'webserver' added to agent '001'."
}
}
[root@wazhu-manage shared]# curl -u wazuh-api:wazuh -X PUT "http://localhost:55000/agents/001/group/apache?pretty"{"error": 0,"data": "Group 'apache' added to agent '001'."}
之后,我们可以向API询问代理所属的组:
[root@wazhu-manage shared]# curl -u wazuh-api:wazuh -X GET "http://localhost:55000/agents/001?pretty"{ "error": 0, "data": { "status": "Active", "configSum": "1a0e4e4aa11b2b284200925e3026090f", "group": [ "default", "webserver", "apache" ], "name": "agent01", "mergedSum": "32990d31d5f43a96cf6ca1aa4f1d839b", "ip": "45.77.105.194", "manager": "wazhu-manage", "node_name": "node01", "dateAdd": "2019-01-22 07:14:26", "version": "Wazuh v3.8.0", "lastKeepAlive": "2019-01-23 02:32:46", "os": { "major": "16", "name": "Ubuntu", "uname": "Linux |agent01 |4.4.0-137-generic |#163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018 |x86_64", "platform": "ubuntu", "version": "16.04.5 LTS", "codename": "Xenial Xerus", "arch": "x86_64", "minor": "04" }, "id": "001" }}
在这种情况下,当任何配置参数存在冲突时,组apache的远程配置是三个组中最优先的。
使用agent_groups CLI,代理可以以相同的方式注册到组:
[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -a -i 001 -g webserverDo you want to add the group 'webserver' to the agent '001'? [y/N]: yAgent '001' already belongs to group 'webserver[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -a -i 001 -g apacheDo you want to add the group 'apache' to the agent '001'? [y/N]: yAgent '001' already belongs to group 'apache'.
要在注册过程中将代理分配给一个或多个组,请使用-G选项注册代理设置代理将包含在的组:
root@agent01:~# /var/ossec/bin/agent-auth -m 149.248.9.0 -G webserver,apache2019/01/23 02:36:01 agent-auth: INFO: Started (pid: 18442).INFO: No authentication password provided.INFO: Connected to 149.248.9.0:1515INFO: Using agent name as: agent01INFO: Send request to manager. Waiting for reply.INFO: Received response with agent keyINFO: Valid key created. Finished.INFO: Connection closed.
列出组和配置
可以实时了解属于组的代理,以及根据其所属的组应用于每个代理的配置和共享文件。
例如,要列出现在可用的组,我们可以对agent_groups运行以下查询:
[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -l -g webserver1 agent(s) in group 'webserver': ID: 005 Name: agent01.
同样容易查询分配给代理001的组:
[root@wazhu-manage shared]# /var/ossec/bin/agent_groups -s -i 005The agent 'agent01' with ID '005' belongs to groups: webserver, apache.
组的优先级从左到右增加,最后一个优先级最高。
对多个组进行更改
以同样的方式可以将多个组分配给代理,可以还原分配并在可用组之间切换。下面显示了如何取消代理001 的组apache:
# /var/ossec/bin/agent_groups -r -i 001 -g apache -q Group 'apache' unset for agent '001'. # /var/ossec/bin/agent_groups -s -i 001 The agent 'ag-windows-12' with ID '001' has the group: '[u'webserver']'.
也可以在组覆盖现有分配之间切换:
# /var/ossec/bin/agent_groups -s -i 001 The agent 'ag-windows-12' with ID '001' has the group: '[u'default', u'webserver']'. # /var/ossec/bin/agent_groups -a -f -i 001 -g apache Group 'apache' set to agent '001'. # /var/ossec/bin/agent_groups -s -i 001 The agent 'ag-windows-12' with ID '001' has the group: '[u'apache']'.
该-f
参数重置分配给代理的组,并强制它仅属于新组。
最后,要检查单个代理的组配置的同步状态,可以使用以下两种方法:
# /var/ossec/bin/agent_groups -S -i 001 The agent '008' sync status is: Agent configuration is synced. # curl -u foo:bar -X GET "http://localhost:55000/agents/001/group/is_sync?pretty" { "error": 0, "data": { "synced": "Agent configuration is synced." } }
agent_groups的其余功能可以在其参考部分找到。对于提供具有类似行为的调用的API也是如此。
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">