Detect Private Keys in Microsoft Sentinel
Adversaries may search for private key and certificate files on compromised systems. Private keys (.key, .pem, .pfx, .p12, .ppk, .pgp, .gpg, .asc) are used for authentication, encryption, and digital signatures. SSH private keys enable key-based lateral movement. TLS/SSL private keys enable HTTPS interception. Code signing certificates enable payload signing for defense evasion. PGP keys decrypt archived data. Adversaries including Machete, Kinsing, Hildegard, Mafalda, and various APT groups actively harvest private keys. Mimikatz's CRYPTO::Extract module extracts keys via Windows CNG API. On network devices, 'crypto pki export' extracts PKI credentials.
MITRE ATT&CK
- Tactic
- Credential Access
- Technique
- T1552 Unsecured Credentials
- Sub-technique
- T1552.004 Private Keys
- Canonical reference
- https://attack.mitre.org/techniques/T1552/004/
KQL Detection Query
// Detect private key file access and search
let KeyExtensions = dynamic([".pem", ".pfx", ".p12", ".key", ".ppk", ".pgp", ".gpg", ".asc", ".crt", ".cer", ".p7b"]);
let KeyDirectories = dynamic([".ssh", "ssl", "certs", "certificates", "keys", "private"]);
DeviceFileEvents
| where Timestamp > ago(24h)
| where ActionType in ("FileRead", "FileAccessed", "FileCopied")
| where (
(FileName endswith ".pem" or FileName endswith ".pfx" or FileName endswith ".p12"
or FileName endswith ".key" or FileName endswith ".ppk" or FileName endswith ".pgp"
or FileName endswith ".gpg" or FileName endswith ".asc")
or FileName in~ ("id_rsa", "id_ecdsa", "id_ed25519", "id_dsa")
)
// Exclude legitimate apps (ssh, scp, openssl) reading their own keys
| where InitiatingProcessFileName !in~ ("ssh", "scp", "sftp", "openssl", "gpg", "putty", "backup.exe")
| project Timestamp, DeviceName, InitiatingProcessAccountName, FolderPath, FileName,
InitiatingProcessFileName, InitiatingProcessCommandLine
| union (
// Detect search commands targeting private key file extensions
DeviceProcessEvents
| where Timestamp > ago(24h)
| where ProcessCommandLine has_any (KeyExtensions)
| where ProcessCommandLine has_any ("find ", "dir /s", "Get-ChildItem", "ls -la",
"findstr", "locate", "mimikatz")
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine, InitiatingProcessFileName
)
| sort by Timestamp desc Detects private key file access and search via two patterns: file read/access events for key files (.pem, .pfx, .p12, .key, .ppk, .pgp, .gpg, SSH private key files) from unexpected processes; and process command lines combining file search tools (find, dir, Get-ChildItem) with private key file extensions. Covers both opportunistic and targeted key theft.
Data Sources
Required Tables
False Positives & Tuning
- SSH and SCP clients legitimately reading their own private key files for authentication (ssh -i, scp -i)
- Web servers and applications reading their own TLS certificate private keys on startup (Apache, Nginx, IIS)
- Certificate management tools (certbot, Let's Encrypt clients) managing certificate lifecycle
- Backup agents reading certificate directories as part of full system backup
- Key management systems and HSM integration software reading and rotating keys
Other platforms for T1552.004
Testing Methodology
Validate this detection against 4 adversary techniques from Atomic Red Team. Each test below lists the behaviour to exercise and the telemetry you should expect to see. Executable commands and cleanup steps are available with Pro.
- Test 1Find SSH Private Keys on Linux
Expected signal: Linux auditd EXECVE records for find with id_rsa and .pem patterns. Multiple OPEN syscalls for each found key file. Process chain visible in auditd.
- Test 2Copy SSH Private Key for Exfiltration
Expected signal: Linux auditd: OPEN for ~/.ssh/id_rsa (read) and /tmp/stolen_key (write). EXECVE for cp and cat commands. File creation event for /tmp/stolen_key.
- Test 3Export Windows Certificate with Private Key via certutil
Expected signal: Sysmon Event ID 1: certutil.exe with -exportPFX, -p (password), and output file path. Sysmon Event ID 11: .pfx file created in C:\Windows\Temp. Windows Security Event 4657 if certificate store auditing enabled.
- Test 4Search for Private Keys with PowerShell
Expected signal: Sysmon Event ID 1: powershell.exe with Get-ChildItem, *.pem, *.pfx, *.ppk patterns. PowerShell ScriptBlock Log Event ID 4104. Multiple file access events for any found key files.
References (6)
- https://attack.mitre.org/techniques/T1552/004/
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1552.004/T1552.004.md
- https://adsecurity.org/?p=2870
- https://www.unit42.paloaltonetworks.com/hildegard-malware-teamtnt/
- https://www.aquasec.com/blog/kinsing-malware-operations/
- https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/certutil
Unlock Pro Content
Get the full detection package for T1552.004 including response playbook, investigation guide, and atomic red team tests.