Powershell logging for Blue Teamers

Shreenkhala Bhattarai
InfoSecStories
Published in
8 min readFeb 26, 2024

--

‍As a blue teamer, having a robust and comprehensive logging system is essential for effective PowerShell monitoring and detection. PowerShell, being highly versatile and commonly used in both legitimate and malicious activities, requires meticulous logging to identify any suspicious behavior in your network. In this article, we will explore the importance of PowerShell logging for blue teamers and delve into best practices to maximize its potential.

Why is PowerShell logging important for blue teamers?

Proactively logging PowerShell activities is crucial for blue teamers to gain valuable insights into potential security threats, spot anomalous behavior, and promptly respond to security incidents. From tracking script execution to monitoring system-wide events, PowerShell logging provides crucial visibility into the actions taking place within your environment.

By enabling PowerShell logging, you can capture detailed information about the commands executed, the parameters used, and the context in which they were executed. This information can be invaluable in identifying malicious activity, such as attempts to bypass security controls, privilege escalation, or data exfiltration. PowerShell logs can also aid in post-incident analysis, allowing you to understand the extent of a compromise and take appropriate remediation measures.

PowerShell logging basics

To enable PowerShell logging on Windows machines, you need to configure the appropriate Group Policy settings or modify the Windows Registry. By default, PowerShell logging is not enabled, so it’s crucial to take the necessary steps to enable and configure it properly.

There are three main types of PowerShell logs that you should be aware of:

  1. Module logging: This type of logging captures information about the PowerShell modules loaded during a session. It records the module name, version, and the path from which it was loaded. Module logging can be useful in identifying potentially malicious or unauthorized modules being used within your environment.
  2. Transcription logging: Transcription logging records all input and output from a PowerShell session, including commands entered, their output, and any errors encountered. It provides a comprehensive record of the activities performed within a session, making it useful for both troubleshooting and forensic analysis.
  3. Event logging: Event logging involves capturing PowerShell-related events generated by the operating system. These events can include script block logging, module logging, and PowerShell script execution events. Event logs can be monitored in real time or analyzed retrospectively to identify suspicious patterns or behaviors.

Enabling PowerShell logging on Windows machines

To enable PowerShell logging on Windows machines, you can use either Group Policy or modify the Windows Registry. Here’s a step-by-step guide on how to enable PowerShell logging using Group Policy:

  1. Open the Group Policy Management Console and navigate to the desired Group Policy Object (GPO) or create a new one.
  2. Expand the tree structure to find the following path: Computer Configuration -> Administrative Templates -> Windows Components -> Windows PowerShell.
  3. Double-click on “Turn on PowerShell Script Block Logging” and set it to “Enabled”.
  4. Double-click on “Turn on PowerShell Module Logging” and set it to “Enabled”.
  5. Double-click on “Turn on PowerShell Transcription” and set it to “Enabled”.
  6. Click “OK” to save the changes.
  7. Apply the GPO to the desired organizational units or computers.
Enable logging with Group Policy

Alternatively, you can modify the Windows Registry to enable PowerShell logging. Here’s how:

  1. Press Win + R to open the Run dialog box.
  2. Type regedit and press Enter to open the Registry Editor.
  3. Navigate to the following path: HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging.
  4. Right-click on the right-hand pane and select “New” -> “DWORD (32-bit) Value”.
  5. Name the new value EnableScriptBlockLogging and set its value to 1.
  6. Repeat steps 4 and 5 to create two more DWORD values: EnableModuleLogging and EnableTranscripting, with values set to 1.
  7. Close the Registry Editor.
Enable logging through the Registry

By following these steps, you can ensure that PowerShell logging is enabled on your Windows machines, allowing you to capture valuable information for analysis and detection.

PowerShell Log on Event Viewer

This screenshot was taken after launching a PowerShell session on a computer. Throughout the process, a multitude of unique script block log entries were produced, offering vital, comprehensive information on each of the multiple script blocks that were being executed.

Configuring advanced PowerShell logging options

While enabling basic PowerShell logging is a good starting point, you can further enhance your logging capabilities by configuring advanced options. These options provide more granular control over what is logged and allow you to tailor the logging settings to your specific requirements.

One advanced logging option is JEA (Just Enough Administration) logging, which allows you to log activities performed using JEA endpoints. JEA enables you to delegate administrative tasks to non-administrative users by providing them with a limited and controlled PowerShell session. By enabling JEA logging, you can capture detailed information about the commands executed by these users, providing an additional layer of visibility and accountability.

Another advanced logging option is Script Block Logging and Transcription, which allows you to log the full content of PowerShell script blocks executed on your system. This level of logging captures the actual code being executed, enabling you to identify and analyze the specific commands used. It can be particularly useful in detecting and analyzing malicious PowerShell scripts.

To configure these advanced logging options, you can modify the Group Policy settings or the Windows Registry as described earlier. However, it’s important to carefully evaluate the impact of enabling these options, as they can generate a significant amount of log data. Ensure that you have sufficient storage capacity and the necessary log management tools in place to effectively handle and analyze the logs.

Analyzing PowerShell logs for security incidents

Once you have PowerShell logging enabled and configured, the next step is to analyze the logs for potential security incidents. PowerShell logs can provide valuable insights into the activities taking place within your environment and help you identify suspicious or malicious behavior.

To effectively analyze PowerShell logs, you can leverage various techniques and tools. Here are some key considerations:

  1. Log aggregation: Collect and centralize all PowerShell logs from your systems into a centralized log management solution or SIEM (Security Information and Event Management) platform. This allows you to correlate events across your network and gain a holistic view of PowerShell activities.
  2. Automated log analysis: Utilize log analysis tools that can automatically parse and analyze PowerShell logs for known indicators of compromise (IOCs) and suspicious patterns. These tools can help you identify potential security incidents more efficiently and reduce the time required for manual analysis.
  3. Threat intelligence integration: Integrate threat intelligence feeds into your log analysis process to enhance the detection of known malicious PowerShell commands or techniques. By cross-referencing your logs with up-to-date threat intelligence, you can identify potential threats more accurately.
  4. Anomaly detection: Implement anomaly detection algorithms or machine learning models to identify unusual or abnormal PowerShell behavior. These techniques can help you detect unknown threats or insider attacks that may not match known IOCs.

Remember that PowerShell logs are just one piece of the puzzle. To perform effective log analysis, it’s essential to combine PowerShell logs with logs from other sources, such as network devices, endpoint protection solutions, and authentication systems. This holistic approach allows you to correlate events and detect sophisticated attack techniques that span multiple layers of your infrastructure.

Tools and techniques for PowerShell log analysis

To aid in the analysis of PowerShell logs, there are several tools and techniques available. Here are some commonly used ones:

  1. PowerShell Logging Frameworks: Frameworks such as the PowerShell Logging Module provide an easy and standardized way to enable and manage PowerShell logging. These frameworks offer additional features like log rotation, customizable log formats, and integration with log management tools.
  2. Log Analysis Tools: Log analysis tools like ELK Stack (Elasticsearch, Logstash, Kibana), LogRhythm, Logpoint, and FortiSIEM can help you parse, search, and visualize PowerShell logs. These tools offer powerful search capabilities, dashboards, and alerting mechanisms to facilitate log analysis.
  3. YARA Rules: YARA is a powerful pattern-matching tool that can be used to create rules for identifying specific patterns or behaviors in PowerShell logs. By creating YARA rules tailored to your organization’s specific requirements, you can automate the detection of known malicious activity.
  4. PowerShell Logging Cheat Sheets: Various cheat sheets and resources are available online that guide configuring and analyzing PowerShell logs. These cheat sheets can help you navigate the complexities of PowerShell logging and ensure you are leveraging the full potential of your logs.

By leveraging these tools and techniques, you can streamline the analysis of PowerShell logs and enhance your organization’s ability to detect and respond to security incidents effectively.

Best practices for PowerShell logging on enterprise networks

To establish an effective PowerShell logging strategy on enterprise networks, consider the following best practices:

  1. Define a logging policy: Develop a comprehensive logging policy that outlines the specific PowerShell logging requirements for your organization. This policy should cover what should be logged, how long logs should be retained, and who should have access to the logs.
  2. Regularly review and update logging configurations: PowerShell logging configurations should be periodically reviewed and updated to align with your organization’s evolving security requirements. Stay up to date with the latest logging best practices and adjust your configurations accordingly.
  3. Monitor and analyze logs in real-time: Establish real-time log monitoring and alerting mechanisms to detect potential security incidents promptly. Implementing a Security Operations Center (SOC) or leveraging managed security services can help ensure continuous monitoring and timely response to any threats.
  4. Secure log storage: Protect the integrity and confidentiality of your PowerShell logs by implementing appropriate access controls and encryption mechanisms. Apply the principle of least privilege to limit access to log files and ensure logs are securely transmitted and stored.
  5. Regularly train and educate personnel: Provide training and educational resources to your security teams and system administrators to ensure they understand the importance of PowerShell logging and know how to effectively analyze the logs. Regularly update their knowledge to keep up with emerging threats and techniques.
  6. Perform log analysis and incident response drills: Regularly conduct log analysis exercises and incident response drills to validate the effectiveness of your PowerShell logging strategy. These drills can help identify any gaps or deficiencies in your logging configurations and response procedures.

By following these best practices, you can establish a robust PowerShell logging strategy that enhances your organization’s security posture and strengthens your defense against cyber threats.

PowerShell logging is a crucial component of a blue teamer’s arsenal for effective monitoring and detection. By enabling and configuring PowerShell logging, you can gain valuable insights into potential security threats, spot anomalous behavior, and respond promptly to security incidents.

References

Powershell Logging Recommendations for detecting Malicious PowerShell | ashizZz (ashishbogati.com.np)

--

--