Problem overview
Unauthorized administrator logins pose a significant security risk, potentially leading to data breaches, system compromises, or compliance violations. Manually monitoring these logins is inefficient and leaves gaps in security. This script automates the detection of admin logins, filtering out authorized users and triggering alerts only when an unauthorized admin accesses the system, enabling IT teams to take immediate action.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
# Level Library
# https://level.io/library/script-windows-unauthorized-admin-login
#>
# Define allowed admin users
$AllowedAdmins = "{{cf_authorized_admins}}"
$AllowedAdminsArray = $AllowedAdmins -split ", "
# Define known system accounts to ignore
$SystemAccounts = @("DWM-1", "DWM-2", "DWM-3", "UMFD-0", "UMFD-1", "UMFD-2", "UMFD-3", "SYSTEM")
# Get the current time and subtract one hour to filter events
$StartTime = (Get-Date).AddHours(-1)
# Get recent successful logins (Event ID 4624) within the last hour and filter for interactive/RDP logins
$Logins = Get-WinEvent -FilterHashtable @{
LogName = 'Security'
ID = 4624
StartTime = $StartTime
} | Where-Object {
# Extract logon type
$LogonType = $_.Properties[8].Value
# Only check interactive (2) and remote desktop (10) logins
($LogonType -eq 2 -or $LogonType -eq 10)
} | ForEach-Object {
$_.Properties[5].Value # Extract the account name
} | Where-Object { $_ -notin $SystemAccounts } | Select-Object -Unique
# Debug: Show detected logins
Write-Host "Detected Admin Logins in Last Hour: $($Logins -join ', ')"
# Check if any unauthorized admin has logged in
$UnauthorizedAdmins = $Logins | Where-Object { $_ -notin $AllowedAdminsArray }
if ($UnauthorizedAdmins) {
Write-Host "ALERT: Unauthorized admin login detected: $($UnauthorizedAdmins -join ', ')"
exit 1
}
Write-Host "All admin logins in the last hour are authorized."
exit 0
This script scans Windows Event Logs for successful administrator logins (Event ID 4624) within the last hour, specifically identifying interactive (local console) and remote desktop (RDP) logins. It cross-references detected logins against a predefined list of authorized admins stored in a Level custom field (cf_authorized_admins). System accounts are excluded to reduce false positives. If an unauthorized admin is detected, the script generates an alert. Pairing this script with a script-based monitor in Level ensures real-time alerts whenever unauthorized admin activity is detected.
Script
unknown nodeUse cases
- Detect unauthorized administrator logins in real-time.
- Automate security monitoring and prevent unauthorized access.
- Maintain strict oversight of privileged accounts for compliance audits.
- Enhance security by integrating alerts with automated remediation workflows.
Recommendations
- Pair with a script-based monitor in Level to generate alerts when unauthorized admin logins occur.
- Define authorized admins using a Level custom field (Authorized Admins - cf_authorized_admins) to ensure accurate monitoring.
- Test before deploying in a production environment to validate compatibility.
- Regularly review and update the authorized admin list to reflect personnel or policy changes.
- Integrate with security automation tools to respond automatically to unauthorized access attempts.