What It Does
This script changes things, for the one account in UserPrincipalName, always in this order because the order is the point. Step one blocks sign-in so the attacker cannot start a new session. Step two revokes every session and refresh token issued before now. It comes after the block because revoking first would let the attacker sign straight back in. Revoked tokens can keep working for a few minutes.
The extras apply only when you turn them on. ResetPassword sets a new random 24 character password that must be changed at next sign-in. It is made here, written to the secure RMM field named in ResultField before it is set, and never printed, logged or returned, and the run refuses to start without a secure field. RemoveMfaMethods deletes the phone, authenticator app, software token, security key, Windows Hello for Business, email and temporary access pass methods, because the attacker may have registered their own; the user must register again once you unblock the account. DisableForwardingRules disables active inbox rules that forward, redirect or forward as an attachment. If rules or mailbox settings cannot be read, the script warns and skips this step. The rules are disabled, not deleted, so you can read them.
It never deletes the account, its mailbox or its data, never unblocks the account (do that when you have cleaned up) and never touches mailbox level forwarding: it only warns when the mailbox has one set. Before the first change it saves the account state, the MFA method IDs and types and the inbox rule definitions as JSON under the Backups folder of the MSPOutpost data folder, with no secrets, and prints the file name. It refuses to make more than MaxChanges changes in one run.
It is a dry run until DryRun=false and Approve=invoke-compromised-account-containment are both set. Running it again revokes the sessions again, because revoking can be repeated, so a second run reports CHANGED rather than already done. The MFA and forwarding steps apply only to what is still there, and ResetPassword sets another new password each time it is on. If DisableForwardingRules is true and the Exchange module is missing, the run stops before any change.
Before You Run It
- Create an app registration with the Graph permissions in the header and grant admin consent. For an administrator account, also give the app's service principal the Privileged Authentication Administrator role.
- For ResetPassword, create a secure RMM custom field and name it in ResultField.
- For DisableForwardingRules, install ExchangeOnlineManagement 3.1 or later and give the app Exchange.ManageAsApp and an Exchange role.
- Read the dry run, then run it for real with DryRun=false and the Approve value.
- Plan the unblock: you have to unblock the account yourself once it is clean.
- Check mailbox level forwarding by hand, because the script only warns about it.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=invoke-compromised-account-containment, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Blocks the account's sign-in; revokes its sessions; optionally sets a new random password, deletes its MFA methods, disables its forwarding inbox rules.
- Restart: It never restarts the machine.
- Typical run: Under 2 minutes.
- Example answer: "[account sign-in] contained: sign-in blocked, sessions revoked, 3 MFA method(s) removed, 1 forwarding rule(s) disabled"
- Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The client tenant: its ID (a GUID) or a domain such as contoso.onmicrosoft.com. Required.
- ClientId: The Application (client) ID of the app registration that holds the Graph permissions. Required.
- CertificateThumbprint: Thumbprint of the app's certificate (private key in LocalMachine\My or CurrentUser\My). Preferred. When empty, the app secret is read from the environment variable ClientSecret (a secure/hidden RMM field; never an argument).
- UserPrincipalName: The compromised user's sign-in name, for example [account sign-in]. Required.
- ResetPassword: true to also set a new random password (needs a secure ResultField). Default false.
- RemoveMfaMethods: true to also delete the account's registered MFA and passwordless methods. Default false.
- DisableForwardingRules: true to also disable the mailbox's forwarding and redirect inbox rules (Exchange Online). Default false.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.