{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workspace": {
            "type": "String"
        }
    },
    "resources": [
        {
            "id": "[concat(resourceId('Microsoft.OperationalInsights/workspaces/providers', parameters('workspace'), 'Microsoft.SecurityInsights'),'/alertRules/distributed-password-cracking-attempts')]",
            "name": "[concat(parameters('workspace'),'/Microsoft.SecurityInsights/distributed-password-cracking-attempts')]",
            "type": "Microsoft.OperationalInsights/workspaces/providers/alertRules",
            "kind": "Scheduled",
            "apiVersion": "2023-12-01-preview",
            "properties": {
                "displayName": "Distributed Password Cracking Attempts - Entra ID",
                "description": "Surfaces a single account absorbing credential-failure attempts (50126 wrong password, 50053 locked by bad passwords, 50056 null password) from a distributed set of sources. Distribution is measured by distinct source IPs and countries rather than city-level geolocation. The expired-password code 50055 is deliberately excluded because it signals a correct-but-expired credential, not an attack. IP spread, country spread, account lockout, sign-in risk, target privilege and guest status are weighted into a score, and the account only alerts once that score crosses the threshold. Interactive and non-interactive sign-ins are both evaluated so token-based failures are not missed.\n\nTABLES: SigninLogs, AADNonInteractiveUserSignInLogs, IdentityInfo\nCONNECTORS: Microsoft Entra ID (SignInLogs, NonInteractiveUserSignInLogs); Microsoft Sentinel UEBA (IdentityInfo)\nLICENSE: Microsoft Sentinel; Microsoft Entra ID P1 (sign-in logs), P2 recommended (sign-in risk, UEBA)\n\nTUNING:\n1. DetectionWindow - the failure accumulation window; align the rule run frequency to it.\n2. Keep the rule query period at P14D - the IdentityInfo lookback uses ago(14d) and only resolves if the rule looks back that far.\n3. VolumeFloor / IPFloorGate - candidacy floors for total failures and distinct source IPs before scoring runs.\n4. ModerateIPThreshold / WideIPThreshold - the two IP-spread tiers that drive the spread weights.\n5. CountryThreshold - distinct countries that count as geographic spread.\n6. W_* weights and ScoreThreshold - shift these to fit your environment's noise profile.\n7. SensitiveRoles - the roles that mark a target as privileged and add weight.\n\nKNOWN FALSE POSITIVES:\n1. A multi-device user retrying a stale password - few IPs, no country spread, scores below threshold.\n2. VPN or corporate-proxy users surfacing across several countries - needs a second weighted signal to fire.\n3. Password rotation churn - excluded by dropping the expired-password code 50055.\n\nINVESTIGATION STEPS:\n1. Read RiskIndicators and Score - WideIPSpread, IPSpread, CountrySpread, AccountLockout, HighRiskSignIn, PrivilegedTarget, GuestTarget.\n2. Confirm the target UserPrincipalName and whether it is privileged (AssignedRoles) or a guest (UserType).\n3. Review DistinctFailIPs, DistinctCountries and FailIPs / Countries for the shape of the distribution.\n4. Check FailResultTypes and LockoutCount - lockouts indicate sustained wrong-password volume.\n5. Cross-check for any successful sign-in from the same IPs in the window - a hit means likely compromise, not just attempts.\n6. If compromise is suspected, reset credentials, revoke sessions, and confirm MFA is enforced for the account.\n\nBLIND SPOTS:\nOnly detects failures against accounts already emitting sign-in logs; password spray across many accounts with low per-account volume stays under VolumeFloor. Legacy-auth endpoints that do not log ResultType consistently, and attacks that succeed on the first attempt, are invisible. Country enrichment depends on LocationDetails being populated. Detection is per-account, so a spray campaign is not correlated into one incident.\n\nAuthor: Bartosz Wysocki | https://www.itprofessor.cloud\nVersion: 1.0 | 2026-06-17",
                "severity": "Medium",
                "enabled": true,
                "query": "// =====================================================================\n// Distributed Password Cracking Attempts - Entra ID\n// =====================================================================\n// Tables  : SigninLogs, AADNonInteractiveUserSignInLogs, IdentityInfo\n// License : Microsoft Sentinel; Microsoft Entra ID P1\n// Tuning  : see rule description for full tuning notes\n// Author  : Bartosz Wysocki | itprofessor.cloud\n// Version : 1.0 | 2026-06-17\n// =====================================================================\nlet DetectionWindow = 1d;\nlet IdentityLookback = 14d;       // full directory resync cycle; guarantees complete identity enrichment\nlet CredFailures = dynamic([\"50126\", \"50053\", \"50056\"]);  // wrong password, locked by bad passwords, null password (50055 expired deliberately excluded)\nlet VolumeFloor = 20;             // total credential failures to qualify as a candidate\nlet IPFloorGate = 3;              // minimum distinct source IPs to be considered distributed\nlet ModerateIPThreshold = 10;     // moderately wide IP spread\nlet WideIPThreshold = 30;         // very wide IP spread\nlet CountryThreshold = 3;         // distinct countries that count as geographic spread\nlet SensitiveRoles = dynamic([\n\"Global Administrator\",\n\"Privileged Role Administrator\",\n\"Privileged Authentication Administrator\",\n\"Security Administrator\",\n\"Exchange Administrator\",\n\"SharePoint Administrator\",\n\"User Administrator\",\n\"Intune Administrator\",\n\"Application Administrator\",\n\"Hybrid Identity Administrator\"\n]);\n// Scoring weights - the degree of distribution and the hard outcomes carry the most\nlet W_ModerateSpread = 3;\nlet W_WideSpread     = 2;\nlet W_CountrySpread  = 3;\nlet W_AccountLockout = 2;\nlet W_HighRisk       = 3;\nlet W_Privileged     = 2;\nlet W_Guest          = 1;\nlet ScoreThreshold   = 5;\n// Credential failures (and successes, for context) from a sign-in table\nlet SignInActivity = (tableName:string) {\n    table(tableName)\n    | where TimeGenerated > ago(DetectionWindow)\n    | where ResultType == \"0\" or ResultType in~ (CredFailures)\n    | project TimeGenerated, UserPrincipalName = tolower(UserPrincipalName), IPAddress,\n              ResultType,\n              Country = tostring(LocationDetails.countryOrRegion),\n              AppDisplayName, RiskLevel = tostring(RiskLevelDuringSignIn)\n    | extend IsFailure = ResultType in~ (CredFailures)\n};\nlet SignIns = union isfuzzy=true\n    SignInActivity(\"SigninLogs\"),\n    SignInActivity(\"AADNonInteractiveUserSignInLogs\");\n// Identity context (privilege / guest), keyed on UPN\nlet IdentityContext = IdentityInfo\n    | where TimeGenerated > ago(IdentityLookback)\n    | summarize arg_max(TimeGenerated, AccountDisplayName, Department, JobTitle, AssignedRoles, UserType, IsAccountEnabled) by AccountUPN = tolower(AccountUPN);\nSignIns\n| summarize\n    StartTime = min(TimeGenerated),\n    EndTime = max(TimeGenerated),\n    FailedCount = countif(IsFailure),\n    DistinctFailIPs = dcountif(IPAddress, IsFailure),\n    DistinctCountries = dcountif(Country, IsFailure and isnotempty(Country)),\n    LockoutCount = countif(ResultType == \"50053\"),\n    HighRiskCount = countif(IsFailure and RiskLevel in (\"high\", \"medium\")),\n    FailIPs = make_set_if(IPAddress, IsFailure, 200),\n    Countries = make_set_if(Country, IsFailure, 50),\n    Apps = make_set_if(AppDisplayName, IsFailure, 50),\n    FailResultTypes = make_set_if(ResultType, IsFailure, 5)\n  by UserPrincipalName\n| where FailedCount >= VolumeFloor and DistinctFailIPs >= IPFloorGate\n| join kind=leftouter IdentityContext on $left.UserPrincipalName == $right.AccountUPN\n| extend ModerateSpread = DistinctFailIPs >= ModerateIPThreshold\n| extend WideSpread = DistinctFailIPs >= WideIPThreshold\n| extend CountrySpread = DistinctCountries >= CountryThreshold\n| extend AccountLockout = LockoutCount > 0\n| extend HighRiskSignIn = HighRiskCount > 0\n| extend IsPrivileged = tostring(AssignedRoles) has_any (SensitiveRoles)\n| extend IsGuest = tostring(UserType) =~ \"Guest\"\n| extend Score = toint(ModerateSpread) * W_ModerateSpread\n               + toint(WideSpread)     * W_WideSpread\n               + toint(CountrySpread)  * W_CountrySpread\n               + toint(AccountLockout) * W_AccountLockout\n               + toint(HighRiskSignIn) * W_HighRisk\n               + toint(IsPrivileged)   * W_Privileged\n               + toint(IsGuest)        * W_Guest\n| where Score >= ScoreThreshold\n| extend RiskIndicators = trim(@\"\\s\\|\\s*$\", strcat(\n    iff(WideSpread, \"WideIPSpread | \", iff(ModerateSpread, \"IPSpread | \", \"\")),\n    iff(CountrySpread, \"CountrySpread | \", \"\"),\n    iff(AccountLockout, \"AccountLockout | \", \"\"),\n    iff(HighRiskSignIn, \"HighRiskSignIn | \", \"\"),\n    iff(IsPrivileged, \"PrivilegedTarget | \", \"\"),\n    iff(IsGuest, \"GuestTarget | \", \"\")\n))\n| extend Name = tostring(split(UserPrincipalName, \"@\", 0)[0]), UPNSuffix = tostring(split(UserPrincipalName, \"@\", 1)[0])\n| project\n    StartTime, EndTime, Score, RiskIndicators,\n    UserPrincipalName, Name, UPNSuffix,\n    FailedCount, DistinctFailIPs, DistinctCountries, LockoutCount, HighRiskCount,\n    Countries, FailIPs, Apps, FailResultTypes,\n    AccountDisplayName, Department, JobTitle, AssignedRoles, UserType\n| sort by Score desc",
                "queryFrequency": "P1D",
                "queryPeriod": "P14D",
                "triggerOperator": "GreaterThan",
                "triggerThreshold": 0,
                "suppressionDuration": "PT1H",
                "suppressionEnabled": false,
                "startTimeUtc": null,
                "tactics": [
                    "CredentialAccess"
                ],
                "techniques": [
                    "T1110"
                ],
                "subTechniques": [
                    "T1110.001"
                ],
                "alertRuleTemplateName": null,
                "incidentConfiguration": {
                    "createIncident": true,
                    "groupingConfiguration": {
                        "enabled": true,
                        "reopenClosedIncident": false,
                        "lookbackDuration": "P1D",
                        "matchingMethod": "Selected",
                        "groupByEntities": [
                            "Account"
                        ],
                        "groupByAlertDetails": null,
                        "groupByCustomDetails": null
                    }
                },
                "eventGroupingSettings": {
                    "aggregationKind": "SingleAlert"
                },
                "alertDetailsOverride": {
                    "alertDisplayNameFormat": "Distributed password cracking against {{UserPrincipalName}} ({{RiskIndicators}})",
                    "alertDescriptionFormat": "{{UserPrincipalName}} saw {{FailedCount}} credential failures across {{DistinctFailIPs}} distinct source IPs."
                },
                "customDetails": {
                    "Score": "Score",
                    "RiskIndicators": "RiskIndicators",
                    "FailedCount": "FailedCount",
                    "DistinctFailIPs": "DistinctFailIPs",
                    "DistinctCountries": "DistinctCountries",
                    "LockoutCount": "LockoutCount",
                    "Countries": "Countries"
                },
                "entityMappings": [
                    {
                        "entityType": "Account",
                        "fieldMappings": [
                            {
                                "identifier": "FullName",
                                "columnName": "UserPrincipalName"
                            },
                            {
                                "identifier": "Name",
                                "columnName": "Name"
                            },
                            {
                                "identifier": "UPNSuffix",
                                "columnName": "UPNSuffix"
                            }
                        ]
                    }
                ],
                "sentinelEntitiesMappings": null,
                "templateVersion": null
            }
        }
    ]
}
