Everything here runs in my own lab subscription, against resources created for this purpose. Fenrir is an authorized assessment tool that I only run in isolated lab tenants, and the nightly tests in this post never run Fenrir at all. They perform harmless look-alike actions and throw the results away.
The problem: a detection rule is a guess until something tests it
- Nobody knows if the rule actually fires on the real attack. It was written from documentation and never tried.
- Nobody knows if it still fires next month. Someone edits it in the portal, a log field changes, a licence expires, and the rule quietly stops working. A broken rule looks exactly like a quiet night.
- There is no history. No review, no diff, no way to tell who changed what.
For every step Fenrir takes that Azure writes to a log, there is a rule in git, and every night a job performs that step for real and fails the build if the alert does not show up.The name comes from Norse mythology, like Fenrir. Fenrir is the wolf. Huginn is one of Odin's two ravens, who fly out every day and report back what they saw.
The whole idea in one picture
Step zero: write down what the attacker really does
- X-08: running a command on a VM (Run Command)
- X-10: listing an App Service's publishing credentials
- X-13: listing a Logic App's callback URL
- X-14: opening a shell in a container (exec)
- P-12: listing a storage account's keys
- P-13: listing a container registry's admin credentials
- Reads are invisible. All of fenrir enumerate produces zero rows.
- Directory reconnaissance needs a paid licence. Microsoft Graph activity logs require Entra ID P1 or P2. My student tenant has neither.
- The log says a command ran, not which command. After a Run Command, the command text is not recorded.
Evidence first: look at the real log rows before writing a query
- For a service principal, the Caller field holds the object ID, not the app ID. A rule that compared it against an app ID would never match.
- The status values in the workspace are Start, Success and Failure. The raw Activity log API says Started, Succeeded and Failed for the same events.
- One Run Command call does not create one row. It creates three (Start, Accept, Success), and they arrive minutes apart. This mattered a lot later.
- Rows arrive 3 to 10 minutes after the action. That number decides how long the test must wait before calling a rule broken.
Detections as code: one folder per rule
Terraform finds the rules by itself
- Stable IDs. Each rule's ID in Sentinel is a GUID computed from the rule's name in git. Renaming the display title never deletes and recreates the rule. This once saved me: I renamed the repository, and because the seed text was frozen, not a single rule was touched.
- Traceability. The description of each deployed rule links back to its folder in git, so an analyst looking at an alert can find the source and its documentation.
Why Terraform and not Bicep
- Huginn needs both Azure resources and Entra ID objects (app registrations with federated credentials). Terraform does both in one tool. Bicep needs extensions or scripts for the second part.
- The folder-discovery loop above is natural in Terraform.
- terraform plan shows exactly what would change before anything changes. That fits my rule of never touching Azure without reading the diff first, and it later became a test of its own.
One shared function, so every rule reads the log the same way
The exclusion trap: never trust a client app
- who (one principal's object ID),
- where (a resource scope prefix),
- what (one operation),
What a rule looks like inside
- A: a managed identity acting from an IP address it has never used. This is the stolen-token case, the heart of what Fenrir does.
- B: one caller listing keys on three or more accounts within an hour.
- C: a new (caller, account) pair, outside deployments and outside approved automation.
The infrastructure: four Terraform stacks
Stack 1, bootstrap: the part only a human may run
- huginn-github-deploy is trusted only for the main branch. It can run terraform plan and read alerts. It is in the watchlist for Terraform's refresh operations.
- huginn-lab-attacker is the one that plays the attacker. It is Contributor on the lab resource group and nothing else. It is trusted only for jobs that run in a GitHub environment called lab-replay, not for a branch. Only a job that explicitly declares that environment can become the attacker, and environments can carry their own protection rules.
Stack 2, sentinel: the detection side
Stack 3, lab: the targets
Stack 4, purple: built to be thrown away
Workflow 1: every push, with no cloud access at all
- the folder is complete and the YAML is valid;
- the operations in fenrir_ops really exist in the inventory;
- every rule marked for CI testing has a replay.sh;
- every entity the rule maps is a column the query actually outputs;
- the time window written in the query matches the schedule in rule.yaml;
- a deployable rule never references a table that needs a licence the tenant does not have.
Workflow 2: every night, act like the attacker and wait for the alert
Job 1, drift: is Azure still running what git says?
Job 2, replay: harmless actions, real log rows
The baseline trap: why every run needs fresh resources
- Night one: new pair, the rule fires, the build is green.
- Night two: the pair is now known. The rule correctly stays silent. The build goes red.
Job 3, cleanup: the safety net
Job 4, validate: wait for the alert
What the lab taught me that documentation could not
Two alerts for one event
One request, three rows
The rule the API enforces and the docs do not mention
- Fast with a short memory: every 5 minutes, remembering pairs for 47 hours. Alerts in 10 to 15 minutes, but a legitimate caller who comes back every three days looks new each time.
- Slow with a long memory: every hour, remembering 14 days. No extra noise, but up to 70 minutes to alert.
A harmless-looking string that broke every night
Smaller surprises
- A new log pipeline drops its first minutes. Right after I created the setting that exports the Activity log, about ten minutes of events never reached the workspace. Nothing was misconfigured. Lesson: check the workspace itself, not just the source, and do not trust the first minutes of a new pipeline.
- "Nightly at 02:30" is a suggestion. GitHub runs scheduled workflows on a best-effort basis. My first nightly run started at 09:32, seven hours late. Fine for this project, but worth knowing before you build anything time-critical on a cron trigger.
Being honest about licences: tiers
- Core: uses only the free Activity log. Deployable anywhere, tested by the nightly replays. This is where the four validated rules live.
- Identity: sign-in and Graph rules. They are written and reviewed but marked deployable: false until a P1/P2 licence exists, and a static test stops them from sneaking into a deploy.
- Beyond Fenrir: rules for the next move an operator would make after Fenrir reports an opportunity, like granting themselves a role. Fenrir never performs these, so they need their own tests.
Purple team: one real Fenrir run with every log switched on
- A Linux VM with a user-assigned identity: X-08 (Run Command) and the host side of token theft.
- A Linux web app with SCM basic auth on: X-10 and X-11 (publishing credentials, Kudu).
- A Logic App with an HTTP trigger: X-12 and X-13 (callback URL).
- A container group running an idle busybox: X-14 (container exec).
- An Automation Account with a runbook, a variable and a credential: X-15 to X-18 (runbook source, variables, credential names).
- A storage account and blob: P-12 and D-01 (listKeys, blob data).
- A container registry with the admin user on: P-13 and D-03 (listCredentials, image pull).
- A Key Vault: E-27 (secret read).
Numbers and cost
- Rules validated: 4, each in two back-to-back replays with exactly one alert per event.
- Time to alert: 5 to 17 minutes. The log takes 3 to 10 minutes to arrive, then comes the wait for the next 5-minute window, then Sentinel's own 5-minute delay.
- Static tests: 74 checks across 14 rule folders, plus 15 query files parsed, on every push.
- Always-on cost: about 0 USD per month. Activity log ingestion is free, and nothing in the lab runs all the time.
- Per nightly run: well under one US cent, almost all of it the VM that lives for about five minutes.
What I would tell someone building the same thing
- Inventory the attacker before writing a rule. The list of 66 operations decided everything else. It showed me that six rules could cover everything the free log can see, and that the rest needed a different answer.
- State your blind spots first. They are the most useful output of the project.
- Look at real rows. Every assumption I took from documentation and did not test was wrong at least once.
- Test the tests. A check that cannot fail protects nothing. Give it something broken and make sure it complains.
- Check for drift instead of auto-deploying. A pipeline that can only read and compare is far harder to abuse than one that can change production, and it still catches everything I care about.
- Exclude identities, not tools. The attacker uses the same tools your admins do.
- Write decisions down while they are fresh. Huginn has eight short decision records. Each one explains a choice, the options I rejected, and what the choice costs. Writing this post was mostly a matter of reading them back.


