#!/usr/bin/env python3
import sys
import json
import subprocess
from datetime import datetime
LOG_PATH = r"C:\Program Files (x86)\ossec-agent\active-response\ar_debug.log"
def log(msg):
try:
with open(LOG_PATH, "a", encoding="utf-8") as f:
f.write(f"[{datetime.now()}] {msg}\n")
except Exception:
pass
def read_payload():
line = sys.stdin.readline()
if not line:
log("No stdin. Exiting.")
sys.exit(0)
line = line.strip()
log(f"Raw stdin: {line}")
try:
return json.loads(line)
except Exception as e:
log(f"JSON parse error: {e}")
sys.exit(1)
def shell_run_cmdline(cmd_line: str):
"""Run a command via shell and log everything."""
try:
log(f"CMDLINE: {cmd_line}")
result = subprocess.run(
cmd_line,
shell=True,
capture_output=True,
text=True
)
log(f"RETURNCODE: {result.returncode}")
log(f"STDOUT: {result.stdout.strip()}")
log(f"STDERR: {result.stderr.strip()}")
except Exception as e:
log(f"Exception running cmdline: {e}")
def enable_firewall_profiles():
log("Enabling firewall profiles via PowerShell+netsh...")
cmd = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
'"netsh advfirewall set allprofiles state on"'
)
shell_run_cmdline(cmd)
def add_block_rule(dstip: str):
rule_name = f"Wazuh_AR_Block_{dstip}"
log(f"Adding block rule for {dstip}")
cmd_del = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
f'"netsh advfirewall firewall delete rule name=\'{rule_name}\' dir=out"'
)
shell_run_cmdline(cmd_del)
cmd_add = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
f'"netsh advfirewall firewall add rule name=\'{rule_name}\' '
f'dir=out action=block remoteip={dstip} protocol=any"'
)
shell_run_cmdline(cmd_add)
cmd_show = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
f'"netsh advfirewall firewall show rule name=\'{rule_name}\'"'
)
shell_run_cmdline(cmd_show)
def delete_block_rule(dstip: str):
rule_name = f"Wazuh_AR_Block_{dstip}"
log(f"Deleting block rule for {dstip}")
cmd_del = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
f'"netsh advfirewall firewall delete rule name=\'{rule_name}\' dir=out"'
)
shell_run_cmdline(cmd_del)
cmd_show = (
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass '
f'"netsh advfirewall firewall show rule name=\'{rule_name}\'"'
)
shell_run_cmdline(cmd_show)
def main():
payload = read_payload()
params = payload.get("parameters") or {}
alert = params.get("alert") or payload.get("alert") or {}
data = alert.get("data", {}) if isinstance(alert, dict) else {}
dstip = None
if isinstance(data, dict):
dstip = data.get("dstip")
if not dstip and isinstance(alert, dict):
dstip = alert.get("dstip")
log(f"Parsed dstip = {dstip}")
if not dstip:
log("No dstip in payload, exiting.")
sys.exit(0)
action = str(payload.get("command", "")).lower()
if isinstance(data, dict) and data.get("action"):
action = str(data.get("action")).lower()
log(f"Final action = {action}")
if action == "add":
enable_firewall_profiles()
add_block_rule(dstip)
elif action == "delete":
delete_block_rule(dstip)
else:
log(f"Unknown action '{action}', defaulting to add.")
enable_firewall_profiles()
add_block_rule(dstip)
log("Execution completed.\n")
sys.exit(0)
if __name__ == "__main__":
main()