zerohop/blog

$ cat defender-for-office-365-report.md · 01 Dec 2026 · 5 min read

A Defender for Office 365 report that tells the story

#defender#mdo#kql#python#reporting

Ask a customer "how is your mail security doing?" and you usually get a screenshot of the Defender portal. A few donut charts, a number of blocked messages, and a feeling that it's probably fine. Then the follow-up questions start. Is that more than last month? Which policy actually stopped it? Are there threats sitting in a mailbox right now? Why is half our mail coming from countries we don't do business with?

The data to answer all of that is there, in advanced hunting. Nobody wants to write twenty queries before every monthly meeting. So I wrote them once, and built a report around them.

The cover and executive summary. All screenshots use the invented Contoso demo tenant: no real customer data anywhere.
The cover and executive summary. All screenshots use the invented Contoso demo tenant: no real customer data anywhere.

What it does

One command per customer, run right before the meeting:

bashpython3 -m mdoreport --config Fabrikam/Fabrikam.toml

It runs the KQL queries through the Microsoft Graph hunting API, saves the figures per day, and writes an HTML report and a PDF. One for the whole tenant and one per mail domain, each compared with the previous 30 days.

Start with the answer

The first page is for the person who has five minutes. Three status cards (mail flow, delivered threats, outbound mail), the key findings with a link to the details, and what changed compared with the previous period.

Executive summary: status per theme, key findings, and what changed since the previous 30 days.
Executive summary: status per theme, key findings, and what changed since the previous 30 days.

Everything below that is for the people who want to dig in: 24 sections, each one answering a single question. Every section can be switched off in the parameter file, so a customer without Plan 2 features or without priority accounts doesn't get empty pages.

Mail flow and verdicts

The basics first: how much mail came in per day, and what happened to it.

Inbound mail per day: delivered, junked, blocked. Weekends are easy to spot.
Inbound mail per day: delivered, junked, blocked. Weekends are easy to spot.

One thing I learned the hard way: blocked is not the same as a threat. A big share of blocked mail has no verdict at all. It failed or was dropped before Defender ever judged it. If you add that to the phish count, your numbers look scary and wrong. The report keeps the two apart.

Verdicts per day: malware, phish, spam, bulk and clean mail, with the comparison to the previous period.
Verdicts per day: malware, phish, spam, bulk and clean mail, with the comparison to the previous period.

What actually stopped it

For me this is the most useful section. Which policy acted on the mail, per day? Is it the anti-spam policy doing all the work, or are the impersonation settings finally earning their keep? And which mail was stopped without any policy named?

Inbound messages a policy acted on, per day, with the totals and the change per policy.
Inbound messages a policy acted on, per day, with the totals and the change per policy.

Where does the mail come from?

My favourite section. It looks at the top-level domain of every sender (.be, .com, .ru and so on) and shows how much of the mail from each one was blocked, on a world map and a map of Europe.

Share of the inbound mail that was blocked, per country code top-level domain. Contoso demo data.
Share of the inbound mail that was blocked, per country code top-level domain. Contoso demo data.

The maps are drawn from public domain Natural Earth borders that ship with the tool, so the report needs no internet connection and loads nothing from a third party.

The same figures per continent: delivered, junked, blocked, and how much the organisation's own rules blocked.
The same figures per continent: delivered, junked, blocked, and how much the organisation's own rules blocked.

A caveat I put in the report itself: a top-level domain is not a country. .com says nothing about where a mail comes from, and a country block in a mail flow rule usually looks at the sending server's IP address, not at the domain. The map shows patterns, not geography. It's still very good at starting the conversation: "why do we accept mail from that TLD at all?"

Beating the 30-day limit

Advanced hunting keeps 30 days of data. That's a problem for a monthly report, and a bigger one for trends. The fix is simple: every run saves its figures per day in a history file. The report reads its days from that history, so a day saved by an earlier run is still there after Defender has forgotten it. A small "collect" run halfway between two meetings keeps the history without gaps.

Inbound mail per month, built from the saved days rather than from Defender's 30-day window.
Inbound mail per month, built from the saved days rather than from Defender's 30-day window.

The details that matter

What I'd tell myself at the start

  1. Start from the questions the customer asks in the meeting, not from the tables you have.
  2. Build the demo tenant first. Every screenshot in this post comes from invented data, generated by the same code that builds real reports. That made testing easier, and it makes posts like this one possible without leaking anything.
  3. One page that people read beats fifty pages that are complete.

← all posts