{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "workspace": {
      "type": "String"
    }
  },
  "resources": [
    {
      "apiVersion": "2024-01-01-preview",
      "id": "[concat(resourceId('Microsoft.OperationalInsights/workspaces/providers', parameters('workspace'), 'Microsoft.SecurityInsights'),'/alertRules/2d920e74-687d-4639-952a-8021297a5e66')]",
      "kind": "Scheduled",
      "name": "[concat(parameters('workspace'),'/Microsoft.SecurityInsights/2d920e74-687d-4639-952a-8021297a5e66')]",
      "properties": {
        "alertDetailsOverride": {
          "alertDescriptionFormat": "{{Computer}} loaded a package into LSA or SAM that is not on the list of packages shipped with Windows: {{LoadedPackage}}. Check which software installed the DLL, when it was registered and whether it is expected on this host.",
          "alertDisplayNameFormat": "Uncommon {{PackageType}} loaded on {{Computer}}"
        },
        "alertRuleTemplateName": "2d920e74-687d-4639-952a-8021297a5e66",
        "customDetails": {
          "EventID": "EventID",
          "LoadedPackage": "LoadedPackage",
          "PackageType": "PackageType"
        },
        "description": "Identifies a package that is not shipped with Windows being loaded by the Local Security Authority (LSA) or the Security Account Manager (SAM): an authentication package (event 4610), a security package (event 4622) or a notification package such as a password filter (event 4614).\nThese DLLs run inside lsass.exe with access to credentials, and password filters receive every new password in clear text, so adversaries register malicious ones, such as mimilib, for persistence and credential theft.\nThe events are logged whenever a package is loaded, no matter how it was registered, and require auditing of the Security System Extension subcategory.\nAdd legitimate third-party packages, such as security products, Cygwin (cyglsa), Lithnet Password Protection or the Microsoft Identity Manager password change notification service, to the lists at the top of the query.\nA DLL on the lists is trusted only when it is loaded directly from \\Windows\\System32 on any drive. If Windows is installed in a different folder on some hosts, add that folder to SystemFolders. If the package value can't be read from an event, the event is not filtered out and the alert shows the package as unknown.\nReference: https://learn.microsoft.com/previous-versions/windows/it-pro/windows-10/security/threat-protection/auditing/audit-security-system-extension\n",
        "displayName": "Uncommon package loaded by LSA or SAM",
        "enabled": true,
        "entityMappings": [
          {
            "entityType": "Host",
            "fieldMappings": [
              {
                "columnName": "Computer",
                "identifier": "FullName"
              },
              {
                "columnName": "HostName",
                "identifier": "HostName"
              },
              {
                "columnName": "HostNameDomain",
                "identifier": "DnsDomain"
              }
            ]
          },
          {
            "entityType": "File",
            "fieldMappings": [
              {
                "columnName": "FileName",
                "identifier": "Name"
              },
              {
                "columnName": "FileDirectory",
                "identifier": "Directory"
              }
            ]
          }
        ],
        "eventGroupingSettings": {
          "aggregationKind": "AlertPerResult"
        },
        "OriginalUri": "https://github.com/Azure/Azure-Sentinel/blob/master/Detections/SecurityEvent/UncommonLSAOrSAMPackageLoaded.yaml",
        "query": "// Packages shipped with Windows. Add packages that are expected in your environment.\n// 4610 and 4622: a DLL is allowed only if its file name is on the list and it is loaded directly from one of the SystemFolders.\n// Any drive letter is accepted. Windows Vista and later always install to a folder named Windows,\n// so add a folder here only if some hosts have Windows in a different one. Write it without the drive letter.\nlet SystemFolders = dynamic([@\"\\Windows\\System32\"]);\nlet AllowedAuthenticationPackages = dynamic([\"msv1_0.dll\"]);\n// clusauthmgr.dll is loaded on failover cluster nodes\nlet AllowedSecurityPackages = dynamic([\n    \"cloudap.dll\", \"kerberos.dll\", \"lsasrv.dll\", \"msv1_0.dll\", \"negoexts.dll\", \"pku2u.dll\",\n    \"schannel.dll\", \"sfapm.dll\", \"tspkg.dll\", \"wdigest.dll\", \"clusauthmgr.dll\"\n]);\n// 4614: this event has no path, only the name the package was registered with.\n// clusauthmgr is loaded on failover cluster nodes, azureadpasswordprotectionpfd.dll is the Microsoft Entra Password Protection DC agent.\nlet AllowedNotificationPackages = dynamic([\n    \"kdcpw\", \"kdcsvc\", \"rassfm\", \"scecli\", \"wdigest\", \"clusauthmgr\", \"azureadpasswordprotectionpfd.dll\"\n]);\nSecurityEvent\n| where EventID in (4610, 4614, 4622)\n// 4610 can use the AuthenticationPackageName column, the values of 4614 and 4622 are only in EventData\n| extend LoadedPackage = case(\n    EventID == 4610, coalesce(AuthenticationPackageName, extract(@'<Data Name=\"AuthenticationPackageName\">([^<]*)</Data>', 1, EventData)),\n    EventID == 4614, extract(@'<Data Name=\"NotificationPackageName\">([^<]*)</Data>', 1, EventData),\n    extract(@'<Data Name=\"SecurityPackageName\">([^<]*)</Data>', 1, EventData))\n// For 4610 and 4622 the value is \"<DLL path> : <package name>\". The package name is chosen by the package itself, so only the DLL path is checked.\n| extend DllPath = iff(EventID == 4614, \"\", extract(@'^(.*?)\\s*:\\s', 1, LoadedPackage))\n// Folder of the DLL without the drive letter, and the DLL file name if that folder is one of the SystemFolders\n| extend DllFolder = extract(@'^[A-Za-z]:(\\\\.*)\\\\[^\\\\]+$', 1, DllPath)\n| extend SystemDllName = iff(DllFolder in~ (SystemFolders), tolower(extract(@'([^\\\\]+)$', 1, DllPath)), \"\")\n// Only a match with the lists removes an event. If the package value can't be read from the event, it stays empty,\n// matches nothing and the event still alerts, so a parsing problem is shown instead of being silently dropped.\n| where not(\n    (EventID == 4610 and SystemDllName in (AllowedAuthenticationPackages))\n    or (EventID == 4622 and SystemDllName in (AllowedSecurityPackages))\n    or (EventID == 4614 and tolower(LoadedPackage) in (AllowedNotificationPackages)))\n| extend PackageType = case(\n    EventID == 4610, \"authentication package\",\n    EventID == 4614, \"password filter\",\n    \"security package\")\n| extend FileName = iff(EventID == 4614, LoadedPackage, extract(@'([^\\\\]+)$', 1, DllPath)),\n         FileDirectory = iff(EventID == 4614, \"\", extract(@'^(.*)\\\\[^\\\\]+$', 1, DllPath))\n| extend LoadedPackage = iff(isempty(LoadedPackage), \"unknown (package value not found in the event)\", LoadedPackage)\n| summarize StartTime = min(TimeGenerated), EndTime = max(TimeGenerated), EventCount = count()\n    by Computer, EventID, PackageType, LoadedPackage, FileName, FileDirectory\n| extend HostName = tostring(split(Computer, \".\")[0]), DomainIndex = toint(indexof(Computer, '.'))\n| extend HostNameDomain = iff(DomainIndex != -1, substring(Computer, DomainIndex + 1), \"\")\n| project-away DomainIndex\n",
        "queryFrequency": "PT1H",
        "queryPeriod": "PT1H",
        "severity": "Medium",
        "status": "InPreview",
        "subTechniques": [
          "T1547.002",
          "T1547.005",
          "T1556.002"
        ],
        "suppressionDuration": "PT1H",
        "suppressionEnabled": false,
        "tactics": [
          "CredentialAccess",
          "DefenseEvasion",
          "Persistence",
          "PrivilegeEscalation"
        ],
        "techniques": [
          "T1547",
          "T1556"
        ],
        "templateVersion": "1.0.0",
        "triggerOperator": "GreaterThan",
        "triggerThreshold": 0
      },
      "type": "Microsoft.OperationalInsights/workspaces/providers/alertRules"
    }
  ]
}
