Powershell commands for Security and System Administration (Part 1)

During day to day system administration I often have to use PowerShell commands, for fault investigation and rectification. This is a skill that I need to develop further so I’ve decided to write a series of blogs with useful commands I have used, or should be using more often day to day. The below list of PowerShell commands (and in the following blogs) cover a range of functionalities—from system monitoring and network analysis to forensic investigation and configuration auditing. While not exhaustive, these commands offer a strong starting point for performing security assessments and incident response tasks. *Top tip – using ‘Get-Help’ before the powershell commandlet will list all the command information and syntax.

System and Process Monitoring

List running processes. The Get-Process command outputs all running processes. The useful columns are process ID and ProcessName. Non-Paged memory (NPM), pageable memory (PM), working set (WS) are listed in Kilobytes and CPU refers to the time the process has been running in seconds.

Get-Process
Screenshot showing the output of the PowerShell command 'Get-Process', listing details of running processes including their handles, memory usage, CPU time, and process IDs.

Find processes by name. This command allows you to name a specific process to investigate, note that the process does not need the file extension.

Get-Process -Name nordvpn-service
PowerShell output showing details of the 'nordvpn-service' process, including metrics like Handles, Non-Paged memory (NPM), pageable memory (PM), working set (WS), CPU time, Process ID, and Process Name.

Check process details (including command line arguments). This command is useful for checking where specific processes are being run from. You can use it to investigate unusual process hierarchy etc.

Get-WmiObject Win32_Process | Select-Object ProcessId, Name, CommandLine
Screenshot of PowerShell command output displaying process details, including ProcessId, Name, and CommandLine.

Find processes with established connections. You could use this command whilst hunting for unauthroised external connection or data exfiltration.

Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' }
Screenshot of PowerShell command output showing established TCP connections with local and remote addresses, ports, and connection states.

Check which processes are listening on network ports. From a system hardening perspective you could use this to reduce unused open ports, or identify rogue services or identify malware binding to ports.

Get-NetTCPConnection | Where-Object { $_.State -eq 'Listen' }
Command line output displaying a list of network connections that are currently in a 'Listen' state, showing local addresses, ports, remote addresses, and additional details.

Network and Firewall Analysis

List all firewall rules. The Get-NetFirewallRule command has a huge output, even for a local host, to make it more manageable select the more important columns. The below screenshot is the part of the unfiltered commandlet.

Get-NetFirewallRule | Select-Object DisplayName, Direction, Action, Enabled
Screenshot displaying the details of a firewall rule for Microsoft Edge, highlighting the settings for inbound mDNS traffic.

Check active network connections. This command shows all TCP connection irrespective of their state. They could be Bound, Listening, Established, CloseWait or TimeWait.

Get-NetTCPConnection
Output of the PowerShell command 'Get-NetTCPConnection', showing active TCP network connections including LocalAddress, LocalPort, RemoteAddress, RemotePort, State, AppliedSetting, and OwningProcess details.

Get details of a specific firewall rule. This command can be used to get all details of a specific firewall rule.

Get-NetFirewallRule -DisplayName "Remote Service Management (RPC)" | Format-List
PowerShell command output displaying details of the 'Remote Service Management (RPC)' firewall rule, including fields like Name, DisplayName, Description, Enabled status, and more.

Enable or disable a firewall rule. This command is specifically useful when working on Command Line windows servers. Be sure to re-enable any rules if you run this command.

Set-NetFirewallRule -DisplayName "Remote Service Management (RPC)" -Enabled False
PowerShell commands for enabling and disabling a firewall rule related to Remote Service Management (RPC).

User and Permission Management

List all local users. From a blue team perspective you could use this commandlet to identify attackers who have created hidden or unauthorised user accounts to maintain access.

Get-LocalUser
PowerShell command output showing a list of local user accounts along with their enabled status and descriptions.

List all local groups. This is important because attackers sometimes create custom groups for privilege escalation or to hide malicious activity.

Get-LocalGroup
Output of the Get-LocalGroup PowerShell command displaying local user groups and their descriptions.

Check group memberships for a user. This command can be useful to ensure proper access control or look for users with unauthroised escalated privileges.

Get-LocalGroupMember -Group "docker-users"
PowerShell command output showing group membership for the 'docker-users' group, displaying ObjectClass, Name, and PrincipalSource.

Find users with administrative privileges. In order to prevent unauthorised access to sensitive resources you can use the below command to see which users are added to privileged AD groups.

Get-LocalGroupMember -Group "Administrators"
PowerShell command output displaying local group members for the Administrators group, showing ObjectClass, Name, and PrincipalSource.

This blog introduced some useful powershell commands looking at AD groups, users, TCP connections and firewall rules. In the following blog we will look at Audit and Log Analysis, Malware and Threat Hunting and System Hardening.

Leave a Reply

Discover more from Planned Link

Subscribe now to keep reading and get access to the full archive.

Continue reading