Threat Hunting with PowerShell - Security even with a small budget - there is no excuse!



Dear Threat Hunter,


Lack of IT security is often excused by little or no available money. In my view, this is a very poor excuse. In this article I will try to give you a jump start on how to investigate threats with PowerShell. Is this a comprehensive and conclusive list of how you can find or detect threats/threats? NO, absolutely not. But it is meant to provide you with the support that you need to move forward on your own.


Let's talk about the "general conditions":

1. If you use the PowerShell scripts I show/explain in this article, this is entirely your responsibility. I use the scripts in different situations, they are not dangerous, but you should already know what you are doing.

2. Written permission! If you are not sure if you are allowed to do an investigation, organize a written permission from your supervisor.

3. In the different scripts I sometimes (for this article deliberately) use standard search words like "malware", "malicious", "hacker" etc. Such search patterns/search words need to be customized, of course. These simply serve as an example.

4. The last part of the article examines some Microsoft cloud services. I am absolutely aware that there are a huge number of tools for hunting in the Microsoft cloud services. It starts with Azure Sentinel and continues with Cloud App Security. Since the focus is on a small budget, I'll leave those tools on the side.



So first, why should you use PowerShell for threat hunting? PowerShell is a useful threat hunting tool because it is a powerful scripting language and a platform for automating tools and accessing data across any Windows environment.

It allows you to quickly gather information from various sources such as event logs, registries, files, and processes. Additionally, it can also be easily integrated with other tools and technologies making it a flexible and efficient tool for threat hunting. Some common use cases for PowerShell in the threat hunting environment include automated collection of log data, identification of unusual behavior anomalies in the system, the discovery of malware or malicious activity by known signatures or patterns or behaviors. These are just a few examples of how PowerShell can be used in a threat hunting capacity. Its versatility and ability to access and manipulate data from across the Windows environment make it a very valuable tool for any security professional.


Threat Hunting in PowerShell - Use Cases:

All right. So now that we understand where PowerShell can benefit an organization from a threat hunting perspective. Let's take a deeper look at some of the actual use cases you might encounter on a day to day basis, first being identify malicious processor files. So specifically, you can conduct raw file analysis to sift through different data shares to look for particular files in question whether that be a signature or even an extension of a certain file being able to quickly search and triage through files is an extreme benefit of using PowerShell for threat hunting.


But how exactly do we start, what can we use as a guide? For example, the MITRE ATT&CK Framework. Here are a few examples:


Indicator Removal: Clear Windows Event Logs


Event Triggered Execution: Installer Packages


Hide Artifacts: NTFS File Attributes


Command and Scripting Interpreter: PowerShell


Command and Scripting Interpreter: Windows Command Shell


Event Triggered Execution: Windows Management Instrumentation Event Subscription


Credentials from Password Stores: Windows Credential Manager


Abuse Elevation Control Mechanism: Bypass User Account Control


The MITRE ATT@CK framework provides a comprehensive and regularly updated overview of tactics, techniques, and procedures (TTPs) used by various threat actors. We can locate these TTPs using PowerShell, here are a few examples:


Indicator Removal: Clear Windows Event Logs


Event Triggered Execution: Installer Packages


Hide Artifacts: NTFS File Attributes


Windows Installer Service is running


Search Alternate Data Streams on NTFS File Systems


Read the Contents of a File


Locating Data Patterns within a File


Search for Encoding with Regex


Search for Command and Scripting Interpreter:


Threat hunting in different environments with PowerShell:


Coming examples are about collecting information in very different environments. Also here a few examples as a kind => as first starting points:


Hunt for Threats in Active Directory:

Some of the scripts are structured in such a way that they must be executed block by block/line by line. So do not execute the whole script at once. Pay attention to the different information that is collected. With some investigations in the Active Directory accounts can be indicated like "guest" or "krbtgt", there must be clear of course how this information is to be estimated. Depending on how and what information is searched.


Hunt for Threats in Exchange Online:


Find mailboxes with the last login.


Hunt for Threats in Azure:


We search Azure for all virtual machines in a subscription.


When was the last password change and when were the accounts created?


Hunt for Threats in SharePoint:


With this script we search for files with the extension .ps1 in a SharePoint Online page.



Is this the best tactic to hunt for threats? No! There are many different tactics/techniques to search for threats. First of all, there are a huge number of different tools that can be used, for example SIEM/SOAR (Security Information and Event Management/Security Orchestration, Automation and Response). These tools are really great, sometimes cost a lot and often it takes a lot of knowledge to use such tools. But what is the use of such tools if the information generated by these tools cannot be understood properly, not very much. For this reason, I have tried in this article with simple tools to generate information that hopefully can be interpreted. Is finished here at this point. NO, the journey continues. The examples in this article are neither exhaustive nor complete, but they should give you a starting point. I hope you can build on this foundation.


I hope that this information is helpful to you and that you have received a good "little" foundation. But I still hope that this information is helpful for you.


Thank you for taking the time to read the article.


Happy Hunting, Tom Wechsler


P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github!

0 Replies