Wazuh使用企业微信机器人预警
vim /var/ossec/etc/ossec.conf
<integration>
<name>custom-wechat</name>
<hook_url>WEBHOOK</hook_url>
<level>12</level>
<api_key>APIKEY</api_key> <!-- Replace with your external service API key -->
<alert_format>json</alert_format>
</integration>
只有name属性是必填的,该参数值是自定义脚本文件的文件名
自定义脚本必须要以"custom-"作为文件名的前缀,可以是shell或python脚本文件
自定义脚本必须读取和解析处理命令行参数,其中参数1对应着包含报警消息内容的文件、参数2对应着api_key的值、参数3对应着hook_url的值。
在Wazuh服务器:cat /var/ossec/integrations/custom-wechat
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import wechat_work_webhook
# 替换为你的企业微信webhook URL
webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
wechat = wechat_work_webhook.connect(webhook_url)
# 读取配置参数
alert_file = open(sys.argv[1])
# 读取告警文件
alert_json = json.loads(alert_file.read())
alert_file.close()
alert_level = alert_json['rule']['level']
ruleid = alert_json['rule']['id']
description = alert_json['rule']['description']
agentid = alert_json['agent']['id']
agentname = alert_json['agent']['name']
#wechat.markdown(u'<font color="warning">告警</font> n'+"HostName: "+agentname+"n"+"AlertLevel: "+str(alert_level)+"n"+"Description: "+description)
wechat.markdown(u'<font color="warning">告警</font> n'+u'主机名: '+agentname+"n"+u"告警级别: "+str(alert_level)+"n"+u"告警内容: "+description)
#wechat.text('test',['@all'])
利用python脚本发送告警信息到飞书机器人
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import sys
import re
import requests
webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxx"
# 使用一个变量记录当前的日志段落
current_log = ""
for line in sys.stdin:
# 将每一行添加到当前的日志段落
current_log += line
# 检查是否到达一个新的日志段落的末尾
if line.strip() == "":
# 解析日志段落
match = re.search(r'Rule: d+ (level (12|13|14|15))', current_log)
if match:
alert_level = match.group(1)
# 发送到飞书机器人
payload = {
"msg_type": "text",
"content": {
"text": f"New Alert (Level {alert_level}):n{current_log}"
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(webhook_url, json=payload, headers=headers)
# 打印HTTP响应码和响应内容(用于调试)
print(f"HTTP Response Code: {response.status_code}")
print(f"HTTP Response Content: {response.text}")
if response.status_code == 200:
print("Notification sent successfully.")
else:
print(f"Failed to send notification. Status code: {response.status_code}")
# 重置当前的日志段落
current_log = ""
将编写好的脚本执行以下命令,放到后台执行。
nohup tail -n 0 -F /var/ossec/logs/alerts/alerts.log | python3 process_log.py > output.log 2>&1 & disown
原文始发于微信公众号(三沐数安):开源主机安全平台企业微信、飞书机器人预警
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论