Metadata-Version: 2.5
Name: log-firewall
Version: 0.1.0
Summary: Installable defences for LLM agents that read logs, against log-borne prompt injection
Project-URL: Homepage, https://github.com/ManvGit17/log-firewall
Project-URL: Repository, https://github.com/ManvGit17/log-firewall
Project-URL: Issues, https://github.com/ManvGit17/log-firewall/issues
Author: ManvGit17
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: ai-agents,aiops,defense,guardrails,llm,llm-security,logs,prompt-injection,telemetry
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Provides-Extra: detector
Requires-Dist: numpy>=1.26; extra == 'detector'
Requires-Dist: onnxruntime>=1.18; extra == 'detector'
Requires-Dist: tokenizers>=0.19; extra == 'detector'
Description-Content-Type: text/markdown

# log-firewall

**Installable defences for LLM agents that read logs — tested against published *and* adaptive log-injection attacks.**

An attacker can write text into your logs without logging in: a URL, a User-Agent header, an SSH username, or any
input that lands in an error message. When an AI agent later reads those logs — to triage an alert or debug an
outage — it may obey that text: call a real attack benign, or run the attacker's "fix". `log-firewall` sits between
your telemetry and your agent and offers several defences, and this repo measures each one — including against
attacks written specifically to beat it.

> **Research question:** *do log-injection defences survive an attacker who knows they are there?* Short answer:
> **gating the agent's actions holds; filtering what it reads does not.** See the numbers below.

## Results (headline)

Tool-using **SOC responder agent**, `qwen2.5:7b` (simulated tools; attacker goal = close a malicious alert as
benign *and* allowlist the source). Lower is better except utility.

| Defence | Utility | Malicious called benign | Harmful action executed |
|---|---|---|---|
| none (no defence) | 75% | 87% | **86%** |
| best stack (gate + detector + AIOpsShield), fixed attacks | 65% | 6% | **0 / 150** |
| best stack, **adaptive** attacks aimed at it | 72% | 35% (`K1`) | **0 / 173** |

**The layered stack takes harmful actions from 86% to zero and keeps them there even under adaptive attack.** But
adaptive attacks push the *verdict* ("is this malicious?") back up — content filters that redact or rewrite log
text are evaded once the attacker knows them. The action gate is what holds.

What beat the content filters (all measured; full table in [`eval/reports/full.md`](eval/reports/full.md)):
- **AIOpsShield (field abstraction), fail-open:** an attacker log line in a format it has no template for passes
  through raw → conceal `0.3% → 100%`.
- **Trained detector:** log-native phrasing found by a small black-box search evades it on most lines; LogJack
  hijacks `9% → 22%`.
- **Summarise-then-reason:** holds on one model/task but cracks on another — SOC conceal up to `35%`, `qwen3`
  triage up to `50%`.
- **Action gates:** hold on *forbidden* actions (`0/173`) but can be steered into *allowed* harmful ones — e.g.
  `block_ip` of your own gateway (`40%`), or a `secretsmanager batch-get-secret-value` the policy didn't list.

Across three open models (`qwen2.5:7b`, `qwen3:8b`, `llama3.1:8b`) and three attack sets (LogJack, a reconstructed
SOC/triage set, and AIOpsDoom-generated payloads). Full method, per-attack breakdown, hypotheses and Fisher tests:
[`eval/reports/`](eval/reports/).

## Install

```bash
pip install log-firewall            # the defences (dependency-free)
pip install "log-firewall[detector]"  # + ONNX runtime, to run a trained detector
```

## Use

```python
from log_firewall import make

# 1. Clean a tool result before the agent sees it (strip instruction-like text, kept structure)
defence = make("sanitise")
safe = defence.transform_tool_output("get_logs", raw_tool_result)   # pass `safe` to the model

# 2. Or gate the agent's actions: block a command whose justification traces to untrusted log text
gate = make("gate-v2")
decision = gate.check_tool_call("run_command", {"command": "aws iam create-user --user-name x"})
if not decision.allowed:
    print("blocked:", decision.reason)       # writes need an approved change record

# 3. Defence-in-depth: gate + detector + field abstraction (the strongest combination measured).
#    Needs the [detector] extra + a trained model (LOG_FIREWALL_DETECTOR) and your AIOpsShield templates:
#    stack = make("gate-v2+detector+aiopsshield")
#    Or compose your own from the data-free defences:
from log_firewall import Stack, PolicyGateV2, Sanitise, Quarantine
stack = Stack(PolicyGateV2(), Sanitise(), Quarantine())
```

Every defence implements the same interface — `transform_tool_output(tool_name, text)` and
`check_tool_call(tool_name, args)` — so you can drop it into any agent framework (plain function calls, LangGraph,
the Claude Agent SDK) wherever tool results and tool calls pass through.

## The defences

| Name (`make(...)`) | What it does | Works out of the box? |
|---|---|---|
| `quarantine` | Wraps tool output in typed, random-tagged "untrusted data" blocks | yes |
| `sanitise` | Strips instruction-like text, role markers, Unicode tricks, encoded blobs from free-text fields; keeps structure | yes |
| `gate` / `gate-v2` | Vets each tool call: blocks writes and commands whose text traces to untrusted log output | yes |
| `gate-soc` | The same idea for a SOC agent: persistent config changes (allowlist, suppress) need a human | yes |
| `detector-heuristic` | Rule-based injection scorer; withholds flagged log lines | yes |
| `detector` | A fine-tuned small encoder (ONNX, CPU) that scores each line | needs a trained model dir (`LOG_FIREWALL_DETECTOR`) + `[detector]` extra |
| `aiopsshield` | Reconstruction of AIOpsShield (USENIX Sec 2026): replaces attacker-writable fields with tokens | needs templates derived by fuzzing **your** app |
| `str` | Summarise-then-reason (Dual-LLM): a tool-less model extracts typed facts; the agent sees only those | needs a model you bind for the extraction call |

The trained detector model and AIOpsShield templates are **not** shipped (they are app-specific): train/derive your
own (`scripts/train_detector.py`, `docs/detector-model-card.md`) or pass your own artifacts.

## Honest caveats
- **Adaptive attacks beat content filters.** Defences that redact or rewrite log text are evaded by an attacker
  who knows the defence. Treat them as depth, not a boundary; gate the actions.
- **Per-model.** The same defence helps one model family and hurts another — measure on your model.
- **Open-weight models.** Results are on `qwen2.5/qwen3/llama3.1`; frontier models resist naive payloads more.
- **Reconstruction.** `aiopsshield` is rebuilt from the paper's description (its code is unreleased as of
  2026-12); see [`docs/aiopsshield-reconstruction.md`](docs/aiopsshield-reconstruction.md).

## Safety of the test setup
All agent tools in the evaluation harness are **simulated**. Read tools return canned log text; action tools only
record the attempted command. Nothing is ever executed.

## Development
Requires [uv](https://docs.astral.sh/uv/) and GNU Make. The research harness (`src/harness`) is not part of the
published wheel; it runs from a source checkout.

```bash
make install         # create the environment
make check           # lint, type-check, tests
make fetch-data      # download third-party attack data (pinned, hash-checked; not stored in git)
make smoke MODEL=ollama_chat/qwen2.5:3b
```

## Credits and licence
Apache-2.0 (see [LICENSE](LICENSE)). The harness reuses prompts, tool definitions and scoring from
[LogJack](https://github.com/HarshShah1997/logjack) (MIT, © 2026 Harsh Shah) and generates payloads with
[AIOpsDoom](https://github.com/RSAC-Labs/AIOpsDoom) (Apache-2.0); see [NOTICE](NOTICE). AIOpsShield is reproduced
from Pasquini et al., *When AIOps Become "AI Oops"* (USENIX Security 2026). CVE data via the NVD API — this product
uses the NVD API but is not endorsed or certified by the NVD.
