File size: 2,118 Bytes
77e3130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()