Página creada con «== Integración de Wazuh con Telegram mediante un bot == === 1. Crear un bot de Telegram === # En la app de Telegram, buscar '''@BotFather'''. # Iniciar un chat con BotFather y escribir el comando: <code>/newbot</code> # Elegir un nombre para el bot. # Elegir un nombre de usuario para el bot (debe terminar en "bot"). # BotFather proporcionará una clave API. Esta clave se usará en la integración. Ejemplo de URL: <code>https://api.telegram.org/bot:<API_KEY>…» Etiqueta: editor de código 2017 |
mSin resumen de edición Etiqueta: editor de código 2017 |
||
Línea 20: | Línea 20: | ||
Agregar el siguiente contenido: | Agregar el siguiente contenido: | ||
< | <pre> | ||
#!/bin/sh | #!/bin/sh | ||
Línea 29: | Línea 29: | ||
case ${DIR_NAME} in | case ${DIR_NAME} in | ||
*/active-response/bin | */wodles*) | |||
[ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/../..; pwd)" | |||
PYTHON_SCRIPT="${DIR_NAME}/${SCRIPT_NAME}.py" | |||
;; | |||
*/bin) | |||
[ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/..; pwd)" | |||
PYTHON_SCRIPT="${WAZUH_PATH}/framework/scripts/${SCRIPT_NAME}.py" | |||
;; | |||
*/integrations) | |||
[ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/..; pwd)" | |||
PYTHON_SCRIPT="${DIR_NAME}/${SCRIPT_NAME}.py" | |||
;; | |||
esac | esac | ||
${WAZUH_PATH}/${WPYTHON_BIN} ${PYTHON_SCRIPT} "$@" | ${WAZUH_PATH}/${WPYTHON_BIN} ${PYTHON_SCRIPT} "$@" | ||
</ | </pre> | ||
Crear el archivo en Python: | |||
<pre> | <pre> | ||
Línea 54: | Línea 54: | ||
Agregar el siguiente contenido: | Agregar el siguiente contenido: | ||
< | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
Línea 81: | Línea 81: | ||
# Construir mensaje | # Construir mensaje | ||
msg_data = { | msg_data = { | ||
'chat_id': CHAT_ID, | |||
'text': f"Nivel de alerta: {alert_level}\nAgente: {agent}\nDescripción: {description}" | |||
} | } | ||
Línea 91: | Línea 91: | ||
sys.exit(0) | sys.exit(0) | ||
</ | </pre> | ||
=== 3. Asignar permisos a los archivos === | === 3. Asignar permisos a los archivos === | ||
< | <pre> | ||
chown root:ossec /var/ossec/integrations/custom-telegram* | chown root:ossec /var/ossec/integrations/custom-telegram* | ||
chmod 750 /var/ossec/integrations/custom-telegram* | chmod 750 /var/ossec/integrations/custom-telegram* | ||
</ | </pre> | ||
=== 4. Configurar la integración en Wazuh === | === 4. Configurar la integración en Wazuh === | ||
Línea 108: | Línea 108: | ||
</pre> | </pre> | ||
Agregar la siguiente sección (dentro de <ossec_config>): | Agregar la siguiente sección (dentro de <ossec_config> si es necesario): | ||
< | <pre> | ||
<integration> | <integration> | ||
<name>custom-telegram</name> | |||
<level>3</level> | |||
<hook_url>https://api.telegram.org/bot:APIKEY/sendMessage</hook_url> | |||
<alert_format>json</alert_format> | |||
</integration> | </integration> | ||
</ | </pre> | ||
(Reemplazar <code>APIKEY</code> por el token de tu bot). | (Reemplazar <code>APIKEY</code> por el token de tu bot). | ||
Línea 126: | Línea 126: | ||
# Ir a '''Explore > Notifications'''. | # Ir a '''Explore > Notifications'''. | ||
# Agregar un nuevo canal de notificación. | # Agregar un nuevo canal de notificación. | ||
# | # Configurarlo utilizando el token del bot creado previamente. | ||
=== 6. Reiniciar el servicio Wazuh Manager === | === 6. Reiniciar el servicio Wazuh Manager === | ||
< | <pre> | ||
systemctl restart wazuh-manager | systemctl restart wazuh-manager | ||
</ | </pre> |
Revisión del 15:49 23 may 2025
Integración de Wazuh con Telegram mediante un bot
1. Crear un bot de Telegram
- En la app de Telegram, buscar @BotFather.
- Iniciar un chat con BotFather y escribir el comando:
/newbot
- Elegir un nombre para el bot.
- Elegir un nombre de usuario para el bot (debe terminar en "bot").
- BotFather proporcionará una clave API. Esta clave se usará en la integración.
Ejemplo de URL:
https://api.telegram.org/bot:<API_KEY>/sendMessage
2. Crear el script de integración
En el servidor donde está instalado Wazuh, crear el siguiente archivo:
nano /var/ossec/integrations/custom-telegram
Agregar el siguiente contenido:
#!/bin/sh WPYTHON_BIN="framework/python/bin/python3" SCRIPT_PATH_NAME="$0" DIR_NAME="$(cd $(dirname ${SCRIPT_PATH_NAME}); pwd -P)" SCRIPT_NAME="$(basename ${SCRIPT_PATH_NAME})" case ${DIR_NAME} in */active-response/bin | */wodles*) [ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/../..; pwd)" PYTHON_SCRIPT="${DIR_NAME}/${SCRIPT_NAME}.py" ;; */bin) [ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/..; pwd)" PYTHON_SCRIPT="${WAZUH_PATH}/framework/scripts/${SCRIPT_NAME}.py" ;; */integrations) [ -z "${WAZUH_PATH}" ] && WAZUH_PATH="$(cd ${DIR_NAME}/..; pwd)" PYTHON_SCRIPT="${DIR_NAME}/${SCRIPT_NAME}.py" ;; esac ${WAZUH_PATH}/${WPYTHON_BIN} ${PYTHON_SCRIPT} "$@"
Crear el archivo en Python:
nano /var/ossec/integrations/custom-telegram.py
Agregar el siguiente contenido:
#!/usr/bin/env python import sys import json import requests CHAT_ID = "CHAT_ID" # Reemplazar "CHAT_ID" por el ID del chat con el bot. # Obtenerlo accediendo a: # https://api.telegram.org/botTOKEN/getUpdates # Leer parámetros de entrada alert_file = open(sys.argv[1]) hook_url = sys.argv[3] # Leer archivo de alerta alert_json = json.loads(alert_file.read()) alert_file.close() # Extraer información alert_level = alert_json['rule'].get('level', "N/A") description = alert_json['rule'].get('description', "N/A") agent = alert_json['agent'].get('name', "N/A") # Construir mensaje msg_data = { 'chat_id': CHAT_ID, 'text': f"Nivel de alerta: {alert_level}\nAgente: {agent}\nDescripción: {description}" } headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'} # Enviar mensaje requests.post(hook_url, headers=headers, data=json.dumps(msg_data)) sys.exit(0)
3. Asignar permisos a los archivos
chown root:ossec /var/ossec/integrations/custom-telegram* chmod 750 /var/ossec/integrations/custom-telegram*
4. Configurar la integración en Wazuh
Editar el archivo de configuración de Wazuh:
nano /var/ossec/etc/ossec.conf
Agregar la siguiente sección (dentro de <ossec_config> si es necesario):
<integration> <name>custom-telegram</name> <level>3</level> <hook_url>https://api.telegram.org/bot:APIKEY/sendMessage</hook_url> <alert_format>json</alert_format> </integration>
(Reemplazar APIKEY
por el token de tu bot).
5. Configurar desde el Dashboard de Wazuh
- Ingresar al Dashboard de Wazuh.
- Ir a Explore > Notifications.
- Agregar un nuevo canal de notificación.
- Configurarlo utilizando el token del bot creado previamente.
6. Reiniciar el servicio Wazuh Manager
systemctl restart wazuh-manager