PowerShell Profile
Adversaries may establish persistence by placing malicious commands into a PowerShell profile. A PowerShell profile script is a script that runs when PowerShell starts and can be used as a logon script to customize user environments. PowerShell supports several profile locations including: $PROFILE (current user, current host), $PROFILE.AllUsersCurrentHost (all users, current host), $PROFILE.CurrentUserAllHosts (current user, all hosts), and $PROFILE.AllUsersAllHosts (all users, all hosts — the most powerful). Malicious profile content executes whenever an interactive PowerShell session is started, providing persistent code execution in the user's context.
let ProfilePaths = dynamic([
"Microsoft.PowerShell_profile.ps1",
"Microsoft.VSCode_profile.ps1",
"Microsoft.PowerShellISE_profile.ps1",
"profile.ps1"
]);
let ProfileFolders = dynamic([
"WindowsPowerShell",
"PowerShell",
"Documents\\PowerShell",
"Documents\\WindowsPowerShell",
"C:\\Windows\\System32\\WindowsPowerShell",
"C:\\Windows\\SysWOW64\\WindowsPowerShell"
]);
DeviceFileEvents
| where Timestamp > ago(24h)
| where FileName in~ (ProfilePaths)
or (FolderPath has_any (ProfileFolders) and FileName endswith ".ps1")
| where ActionType in ("FileCreated", "FileModified")
| extend IsSystemProfile = FolderPath has_any (
"C:\\Windows\\System32\\WindowsPowerShell",
"C:\\Windows\\SysWOW64\\WindowsPowerShell"
)
| extend IsUserProfile = FolderPath has_any ("Documents", "Users")
| extend SuspiciousWriter = InitiatingProcessFileName !in~ (
"powershell.exe", "pwsh.exe", "notepad.exe", "code.exe", "devenv.exe",
"vim.exe", "nano.exe"
)
| project Timestamp, DeviceName, AccountName, ActionType, FileName, FolderPath,
IsSystemProfile, IsUserProfile, SuspiciousWriter,
InitiatingProcessFileName, InitiatingProcessCommandLine
| sort by Timestamp desc Data Sources
Required Tables
False Positives
- PowerShell module installation (Install-Module) that adds initialization code to profiles
- Developer tool setup scripts (Visual Studio Code, PowerShell extension, Posh-git, oh-my-posh) that add profile entries during installation
- IT administrators legitimately configuring PowerShell environment via profile files as part of system baseline
- Security tools that add their own functions or aliases to PowerShell profiles during installation
References (4)
- https://attack.mitre.org/techniques/T1546/013/
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1546.013/T1546.013.md
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
- https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/creating-profiles
Unlock Pro Content
Get the full detection package for T1546.013 including response playbook, investigation guide, and atomic red team tests.