AuditorSEC's picture
Create app.py
77e3130 verified
Raw
History Blame Contribute Delete
2.12 kB
import gradio as gr
import json
import datetime
# OPA/Rego-style policy rules for IoT anomaly detection
RULES = {
"NIGHTSPIKE": lambda v: v > 90,
"SENSOROFFLINE": lambda v: v == 0,
"UNAUTHORIZEDCONFIGCHANGE": lambda v: v < 0,
"HIGHTEMP": lambda v: v > 75,
"CRITICALTHRESHOLD": lambda v: v > 95,
}
def check_policy(sensor_id: str, value: float, sensor_type: str) -> dict:
"""Evaluate IoT sensor reading against policy rules."""
violations = [r for r, fn in RULES.items() if fn(value)]
severity = "CRITICAL" if any(r in violations for r in ["CRITICALTHRESHOLD", "UNAUTHORIZEDCONFIGCHANGE"]) \
else "HIGH" if violations else "OK"
result = {
"sensor_id": sensor_id,
"sensor_type": sensor_type,
"value": value,
"timestamp": datetime.datetime.utcnow().isoformat() + "Z",
"violations": violations,
"severity": severity,
"status": "ALERT" if violations else "OK",
"action": "KILL_SWITCH" if severity == "CRITICAL" else ("ALERT_TELEGRAM" if violations else "PASS")
}
return result
demo = gr.Interface(
fn=check_policy,
inputs=[
gr.Textbox(label="Sensor ID", value="demo-01", placeholder="e.g. sensor-bakhmach-01"),
gr.Number(label="Sensor Value", value=28),
gr.Dropdown(
label="Sensor Type",
choices=["temperature", "humidity", "vibration", "power", "motion"],
value="temperature"
)
],
outputs=gr.JSON(label="Policy Engine Result"),
title="AuditorSEC IoT Policy Simulator",
description="""Real-time OPA/Rego-style policy engine for IoT anomaly detection.
Rules: NIGHTSPIKE >90 | SENSOROFFLINE =0 | UNAUTHORIZEDCONFIGCHANGE <0 | HIGHTEMP >75 | CRITICALTHRESHOLD >95
Powered by AuditorSEC | GitHub: romanchaa997/Audityzer | Telegram: @audityzerbot""",
examples=[
["demo-01", 95, "temperature"],
["sensor-02", 0, "humidity"],
["node-03", 28, "power"],
["edge-04", -5, "vibration"],
["bakhmach-01", 76, "temperature"]
],
flagging_mode="never"
)
demo.launch()