Detections & IOCS's suspicious powershell activity

Here's a list of detections and IOC's that can be used to identify the activity from the PSLoader lab with SharpKatz.

1. Powershell Logs (EventID 4104)

By having a look in the prowershell event logs, we can see the powershell commands that were executed. If you have enabled logging of module loads, you can also see the loaded assemblies (like SharpKatz).

1.1 SUSPICIOUS POWERSHELL PARAMETERS TO LOOK FOR

ParameterAlternative ParametersFull NameDescriptionWhy Malicious Actors Use ThisDetection Notes
-nop-NoProfile, -NoPNo ProfileSkips loading PowerShell user profilesBypasses PowerShell profile scripts that might contain security configurations, logging, or detection mechanismsLook for PowerShell executions without expected profile loading
-nonI-NonInteractive, -NonINon InteractiveRuns PowerShell in non-interactive modePrevents interactive prompts that could halt automated malicious scripts or reveal malicious activity to usersMonitor for automated PowerShell execution patterns
-w hidden-WindowStyle Hidden, -w h, -wi hWindowStyle HiddenHides the PowerShell windowMaintains stealth by preventing users from seeing PowerShell execution windowsDetect hidden window PowerShell processes via process monitoring
-enc-EncodedCommand, -e, -ecEncodedCommandExecutes Base64 encoded PowerShell commandsObfuscates malicious PowerShell commands to evade signature-based detection and analysisHigh priority IOC - Base64 encoded commands in PowerShell logs
iexInvoke-Expression, IEX (alias)Invoke-ExpressionExecutes string as PowerShell commandEnables dynamic code execution from downloaded or constructed stringsMonitor for IEX usage with network downloads or obfuscated strings
DownloadStringDownloadString(), .DownloadStringWebClient.DownloadStringDownloads content from URL as stringDownloads malicious scripts/payloads from remote servers for executionMonitor for DownloadString usage in PowerShell logs
http://http://, HTTP traffic from PowerShellHTTP ProtocolDownloads content over unencrypted HTTPUses unencrypted HTTP to download payloads, avoiding HTTPS inspectionMonitor for HTTP URLs in PowerShell commands and network connections
webclientWebClient, New-Object Net.WebClient, System.Net.WebClientSystem.Net.WebClient.NET class for web operationsDownloads files and data from web servers programmaticallyMonitor for WebClient instantiation in PowerShell logs

1.2 Decoded Base64 commands in powershell event logs, (requires GPO to be enabled):

  • On your Windows machine, open ELEX (Event Log Explorer) and open the workspace called "WORKSPACE_PSLoader.ELX"

image

  • Navigate to the folder "c:\THEV\Labs\PSLoader\" and open the workspace called "WORKSPACE_PSLoader.ELX"

image

This workspace has been pre-configured (Custom filter, color coding and columns) for you with the relevant event logs and filters. We have some color-coding going on here:

image

image

The color coding already gives away some hints, but if you have tons of powershell events, you can use the filter to only show events with -enc or iex in them.

  • Click on the filter icon in the toolbar to open the filter window (it's already pre-poluated with event ID 4104).

image

  • You can load custom filters by clicking on Load at the bottom, navigate to "c:\THEV\Labs\PSLoader\" and select the file called FILTER_pwsh_IEX.elc

image

We have also configured some custom columns for you, so you can see the commandline used.

  • Simply right-click on the column header and select Choose columns... to add or remove columns.

image

You should see the following, all powershell events that have IEX in them:

image

1.3 Encoded Base64 commands in powershell event logs.

Attackers often use Base64 encoding to obfuscate their commands, making it harder for defenders to quickly identify malicious activity. By filtering for -enc in the powershell event logs, we can find these encoded commands.

  • Go back to your filter and change the Text in description to -enc:

image

image

1.4 Complete content of the script block:

When attackers use IEX to download and execute scripts, the complete content of the script block is often logged in the powershell event logs. This can include the actual script being executed, as well as any loaded assemblies (like SharpKatz).

  • Go back to your filter and change the Text in description to SharpKatz:

image

You will see the complete content of the script block, which includes the loaded assembly (SharpKatz).

image

2 . Powershell Hunter

PowerShell-Hunter is a growing collection of PowerShell-based threat hunting tools designed to help defenders investigate and detect malicious activity in Windows environments. This project aims to provide security analysts with powerful, flexible tools that leverage PowerShell's native capabilities for threat hunting.

https://github.com/MHaggis/PowerShell-Hunter

image

Open a PowerShell terminal as administrator and navigate to the folder "c:\THEV\Labs\PSLoader\" and run the following command, it will analyze the powershell event logs and filter them based on the patterns in the Patterns.csv file.:

cd c:\THEV\Labs\PSLoader\
 .\Analyze-PowerShellEvents.ps1 -PatternFile "Patterns.csv" -OutputFile "FilteredEvents.csv"

image

You can choose the output format, either CSV or JSON. In this case, we are using CSV. If you open this with Excel or Visual Studio Code, you will see the filtered events based on the patterns in the Patterns.csv file.

NOTE: Install CSV plugin in Visual Studio Code to view CSV files.

  • In visual studio code go to extensions and search for "CSV" and install the one by "ReprEng"

image

When you open the CSV file in Visual Studio Code, it will look like this:

image

3. SYSMON EVENTID 3 - NETWORK CONNECTIONS

Powershell.exe making network connections to the internet is suspicious in itself, but especially when it downloads and executes scripts. We can detect this with with sysmon (or your EDR of choice), by monitoring network connections from powershell.exe.

image

NOTE: This detection is already added to your sysmon config file.

  • On your Windows machine, open ELEX (Event Log Explorer) and open the workspace called "WORKSPACE_Sysmon3.ELX" located in the folder "c:\THEV\Labs\PSLoader\"

image

You can see the filter is already pre-configured for you to look for powershell.exe and event ID 3 (network connections). We also configured some custom columns for you, so you can see the User, Source host name, Destination host name, and Destination port.

image

And the output will look like this:

image

NOTE: Since we are tagging our sysmon rules with names, you can also filter on the RuleName column for Powershell Network Connections to find the relevant events.

4. SYSMON EVENTID 1 - PROCESS CREATION

When attackers use powershell to download and execute scripts, they often use specific parameters to obfuscate their activity. We can detect this with sysmon (or your EDR of choice), by monitoring process creation events for powershell.exe with specific command line parameters.

image

NOTE: This detection is already added to your sysmon config file.

  • On your Windows machine, open ELEX (Event Log Explorer) and open the workspace called "WORKSPACE_Sysmon1.ELX" located in the folder "c:\THEV\Labs\PSLoader\"

image

You can see the filter is already pre-configured for you to look for powershell.exe with the parameters powershell.exe" -enc in the command line. Always check how the events are formatted - you can see sysmon logs the full path to powershell.exe between "" and then the parameters.

image

And the output will look like this:

image

INCIDENT REPONSE

When incident response teams investigate a potential compromise involving PowerShell-based attacks, they often look for specific Indicators of Compromise (IOCs) and artifacts that can help identify malicious activity. More often than not incident responders will not have physical access to the compromised machine, so they will have to rely on logs and artifacts that are collected remotely.

Let's use Velociraptor to hunt for the above IOC's and artifacts (-enc,-nop,-nonI,-w hidden, iex, http, WebClient) in powershell logs.

  • On your Windows machine, open Velociraptor in your chrome browser by opening the URL: https://kali:8889/app/index.html#/dashboard/:
  • Navigate to the Hunt Manager in the left side menu (icon with the crosshair) Click on + in the top left corner

image

  • Click on Select Artifacts on the bottom of the screen, typoe powershell in the search box at the top and select the artifact called Windows.EventLogs.PowershellScriptBlock.

image

  • Click on Next: Configure Parameters on the bottom right of the screen.
  • Set the SearchStrings to "http|webclient|enc|iex"
  • set the LogLevel to All - Don't forget this!
  • Click on Launch on the bottom right of the screen.

image

You will see a new hunt in paused state.

  • click on the play button to start the hunt.

image

Wait until the hunt is finished, then click on the notebook tab to see the results. This will hunt through the powershell event logs for the above IOC's, instead of downloading the entire event log, which would take more time and bandwidth.

image

  • Click on the Download button to download the results in CSV format.

image

The query results will be saved to your Downloads folder.

image

  • Open Visual Studio Code and open the downloaded CSV "Query1.csv" file, located in your Downloads folder. don't just double-click the file, for some reason the csv extension doesn't load that way.

image

image

When you open the CSV file in Visual Studio Code, it will look like this:

image

This was a simple example of how to use Velociraptor to hunt for IOC's and artifacts in powershell logs. You can use the same approach to hunt for other IOC's and artifacts in other logs as well.