Security Power Level: Advanced Chapter 16: PowerShell for Ethical Hackers
Reading Network Listeners, Domain Objects, and Shares
Network and directory checks show what your lab computer offers to others and who exists in a lab domain. You will read listening ports, adapters, domain users and computers, and shares, and learn what a defender looks for.
Lesson 3 of 4 in Chapter 16: PowerShell for Ethical Hackers. Previous step in this path: Reading Your Own System and Services. 3 learning objectives, 5 examples, and a knowledge check.
Start: what you will learn
Network and directory checks show what your lab computer offers to others and who exists in a lab domain. You will read listening ports, adapters, domain users and computers, and shares, and learn what a defender looks for.
Learning objectives
- Read listening connections and adapters on your own lab computer
- Read domain users and computers in a lab domain with filters
- Read shares and spot unexpected listening ports, stale accounts, and open shares
Concept in simple words
What it is: Network and directory enumeration means listing what your computer offers on the network and who exists in a directory. This is for your OWN lab only, with permission, for understanding and defense. A listener is a program waiting for a network connection on a port, which is a numbered door for network traffic. A domain is a managed Windows network where one directory holds users and computers.
Why it works
Why it exists: Every open listener is a chance for someone to connect, so a defender wants a short, expected list. Every account in a directory is a chance to sign in, so stale accounts, which are old accounts no one uses anymore, are a risk. Every share is a folder offered on the network, so an open share can expose files that should stay private. Listing makes each of those chances visible and countable.
What problem it solves: A computer can offer network services without showing a window. A domain can hold hundreds of accounts, and no one can remember them all. A share can be created for one task and left open long after the task ends. Without a list, these facts stay invisible. With a list, a defender can ask, for each item, is this still needed, and who still needs it.
What PowerShell is doing behind the command: Get-NetTCPConnection asks Windows for current TCP connection objects, each with a state, a local address, a local port, and an owning process id. Get-NetAdapter asks for adapter objects, each with a name, a status, and a speed. Get-ADUser and Get-ADComputer ask a directory for user and computer objects, and the -Filter parameter tells the directory which objects to return, before PowerShell ever sees the rest. Get-SmbShare asks for share objects, each with a name, a path, and a description. In every case, PowerShell is reading facts, not changing them.
Listeners in depth: A TCP connection has a State. Listen means a program is waiting for a new connection on that port. Established means a connection is open right now. TimeWait and CloseWait are closing states, and they are normal parts of ending a connection. For a defender review, Listen matters most, because it shows what the machine offers to others, even when no one is connected yet. LocalAddress tells you which address the listener is bound to. A listener on 127.0.0.1 only accepts connections from the same machine. A listener on 0.0.0.0 accepts connections on any local address, which is a wider offer, so note it carefully.
OwningProcess in depth: OwningProcess is the Id of the program behind the listener. That Id alone does not name the program. To name it, you look up that Id in a process list, for example with Get-Process, and match the Id to a Name and a Path. If the process has ended by the time you check, the Id may be gone or reused, so write down the port and the Id at the same time, and check the process at once. A port without a program name is only half a finding.
Adapters in depth: An adapter is a network card, real or virtual, that connects the machine to a network. Get-NetAdapter shows Name, InterfaceDescription, Status, and LinkSpeed. Status Up means the adapter is active. Status Disabled or Disconnected means it is not in use. A lab machine can have several adapters, including virtual ones created by virtualisation software, and those are normal in a lab. The question is not how many adapters exist. The question is whether each active adapter is expected, and whether the machine is connected to a network you intended.
Domains in depth: Active Directory is the directory service for a Windows domain. It holds user objects, computer objects, and group objects in one managed store. Get-ADUser and Get-ADComputer need the Active Directory module, and they only work where a domain exists and where you have permission to read it. They do not work on a stand alone lab computer with only local accounts. That is an important boundary. If the command is not recognised, or if it returns an error about the directory, it may mean you are not in a domain lab, not that the directory is empty.
Filters in depth: The -Filter parameter for Get-ADUser and Get-ADComputer uses a small expression language. A filter has a property, an operator, and a value, such as Enabled -eq $true, which means Enabled equals true. The $true value is PowerShell for true. A filter of * means all objects, and that is the filter to avoid in a large directory, because it returns everything and teaches you nothing at a glance. A good filter asks one clear question, such as only enabled users, or only computers whose names start with a lab prefix you know.
Shares in depth: A share is a folder that Windows offers on the network through SMB, which is the Server Message Block protocol for file sharing. Get-SmbShare lists share objects with Name, Path, and Description. Some shares are built in, such as ADMIN$, C$, and IPC$, and their presence alone is normal on Windows. The risk is a custom share you did not plan, or a share whose path points to a sensitive folder. Get-SmbShareAccess, with -Name and a share name, lists who has access to that share, which is a separate question from whether the share exists. Always answer both: does the share exist, and who can use it.
Think of it like this
Think of it like this: Think of it like checking the doors, the staff list, and the shared rooms in a small office you manage. You want to know which doors are open, who still has a key, and which rooms anyone can walk into. A door you did not open, a key held by someone who left, and a room open to everyone are all worth a note. None of them proves wrongdoing on its own. Together, they show where the office is open wider than planned.
How it works, step by step
How it works, step by step: First, list listening connections with Get-NetTCPConnection and keep only rows where the state is Listen, then note the local port for each one. Second, list adapters with Get-NetAdapter, which shows the network cards on your computer and whether each one is up. Third, in a lab domain only, read users with Get-ADUser and computers with Get-ADComputer, using a filter to keep only the rows you need, such as enabled accounts. Fourth, list shares with Get-SmbShare, and note any share you did not expect.
A careful order for one review: Start with listeners, because they show what the machine offers right now. Then check adapters, because they show which networks those offers sit on. Then check domain objects, because they show who can sign in. Then check shares, because they show what files are offered. End by writing one note per odd item, with the command, the time, and the exact value that looked odd. That order keeps a network review from becoming a pile of unrelated tables.
Let us walk through an example: Suppose you list listening connections in your lab and you see a port you did not expect, and the program behind it is one you do not recognise. You write down the port, the program name, and the time. Then you check whether that program is part of lab software you installed. If you cannot explain it, you flag it for a defender, and you do not connect to it from another machine to test it. Testing it from another machine would turn a read only review into a network probe, and that is out of scope.
A second walkthrough, a stale domain account: Suppose you filter domain users to enabled accounts in your lab domain, and you see an account for a person who left the lab team months ago. You write down the name, the logon name, and the fact that it is still enabled. You do not disable it yourself on a system you do not manage. You flag it, because a stale enabled account is a door that should have been closed when the person left, and the defender who owns the directory can close it properly.
A third walkthrough, a share: Suppose Get-SmbShare shows a share pointing to a folder you thought was private. You write down the share name and the path. Then you read its access list with Get-SmbShareAccess, and you write down who can use it, without opening files inside it. Opening files to explore is not part of a read only share review. The share name, the path, and the access list are enough for a defender to judge the risk.
When to use it
When to use it, and when not to: Use these commands in your own lab, and use the domain commands only in a lab domain you manage or have clear permission to audit. Do not scan a real network, a work domain, or a public range, even with a read only command, and do not use a filter that dumps every account from a directory you do not manage. A read only command on a network you do not own is still out of scope.
Scope and safety, stated plainly: This lesson is for your own lab only, with permission, for defense and understanding. It does not teach how to connect to a listener you do not own, how to move from one machine to another, how to use a stale account, or how to open files in a share you should not access. If a check shows you something interesting, the next step is a note to the defender, not a deeper probe on your own.
Edge cases to know: A listener on a high port can be normal lab software, and a listener on a well known port can be normal Windows work, so port number alone does not prove anything. A domain filter that returns nothing may mean the filter was too narrow, or the property name was wrong, not that the directory is empty. A share list always includes built in shares, so learn those names first, and judge only the custom shares against your plan. An adapter that is Disconnected is not a fault by itself. It may simply be an unused card.
How to compare across time: Save the listener list, the adapter list, the domain filter you used, and the share list, each with a date. Next time, use the same filter and the same commands. A new listener, a newly enabled account, or a new share will then stand out as a change, not as a mystery. If you change the filter each time, you cannot tell whether a difference came from the machine or from your command.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-NetTCPConnection -State Listen | Get-NetAdapter | Get-ADUser -Filter <filter> | Get-ADComputer -Filter <filter> | Get-SmbShareExample 1
Walkthrough: Get-NetTCPConnection lists TCP connections. The -State Listen parameter keeps only programs waiting for a connection, at the source, before PowerShell filters anything. Select-Object shows LocalAddress, which is the address on this computer, LocalPort, which is the numbered door, State, and OwningProcess, which is the id of the program behind the listener. What to look for: a short list of ports you expect, and any port you cannot explain. What changes if you change one part: if you change Listen to Established, you see open connections right now, not offers waiting for a connection. If you remove -State, you see every state, including closing connections, which is noisy for a listener review. If you keep the OwningProcess value, you can match it to a process name in a process list.
Get-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort, State, OwningProcessIllustrative output: LocalAddress LocalPort State OwningProcess ------------ --------- ----- ------------- 127.0.0.1 5985 Listen 1234
Example 2
Walkthrough: Get-ADUser reads users from Active Directory, which is the directory service for a Windows domain. The -Filter value Enabled -eq $true keeps only rows where Enabled equals true, so disabled accounts are left out. The filter is in single quotes so PowerShell passes the whole expression to the directory. Select-Object shows Name, SamAccountName, which is the short logon name, and Enabled. Run this only in a lab domain you have permission to audit. What to look for: current lab users, and any enabled account that should have been disabled. What changes if you change one part: if you change $true to $false, you see disabled accounts instead, which is useful when you check whether a stale account was really disabled. If you change the filter to *, you get every user, which is too broad for a careful review.
Get-ADUser -Filter 'Enabled -eq $true' | Select-Object Name, SamAccountName, EnabledIllustrative output: Name SamAccountName Enabled ---- -------------- ------- Lab User labuser True
Example 3
Walkthrough: Get-NetAdapter lists network adapters as objects. Select-Object shows Name, which is the adapter name, Status, which tells you whether the adapter is Up or not, and LinkSpeed, which is the speed the adapter reports. What to look for: adapters you expect in your lab, with Status Up for the ones in use, and no surprise active adapter. What changes if you change one part: if you add Where-Object { $_.Status -eq 'Up' } before Select-Object, you see only active adapters, which is a good focused check, but you lose the disabled list, so run the full list first for your baseline. If you add InterfaceDescription, you also learn the hardware or virtual type behind each name.
Get-NetAdapter | Select-Object Name, Status, LinkSpeedIllustrative output: Name Status LinkSpeed ---- ------ --------- Ethernet Up 1 Gbps Wi-Fi Disconnected 0 bps
Example 4
Walkthrough: Get-SmbShare lists SMB shares on the computer. Select-Object shows Name, which is the share name seen on the network, Path, which is the local folder behind the share, and Description, which is a short note about the share. What to look for: built in shares you already know, plus any custom share you planned, and no custom share you cannot explain. What changes if you change one part: if you use Get-SmbShare -Name and one share name, you check that one share in detail instead of the whole list. If you add ShareState, you also learn whether each share is Online or not, which helps you separate a present share from an active one.
Get-SmbShare | Select-Object Name, Path, DescriptionIllustrative output: Name Path Description ---- ---- ----------- LabShare C:\LabShare Lab share for testing
Example 5
Walkthrough: Get-ADComputer reads computers from Active Directory. The -Filter value Enabled -eq $true keeps only enabled computer accounts, so disabled or retired machines are left out. Select-Object shows Name, DNSHostName, which is the full network name of the computer, and Enabled. Run this only in a lab domain you have permission to audit. What to look for: lab computers you expect, and any enabled computer you no longer recognise or no longer use. What changes if you change one part: if you change the filter to Name -like and a lab prefix with a star, you narrow the list to computers whose names start with that prefix, which is useful in a larger lab domain. If you use a filter of *, you get every computer, which is too broad for a focused review.
Get-ADComputer -Filter 'Enabled -eq $true' | Select-Object Name, DNSHostName, EnabledIllustrative output: Name DNSHostName Enabled ---- ----------- ------- LABPC01 labpc01.lab.local True
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Running network or domain checks against a real network or domain you do not own, instead of staying inside your own lab domain.
- Dumping every user and computer without a filter, which creates a huge list that is hard to read and hard to defend as a careful audit.
- Seeing an open share and opening files inside it to explore, instead of noting the share name and flagging it for a defender.
- Judging a listener by its port number alone, without matching OwningProcess to a program name and checking whether that program belongs in your lab.
- Assuming a listener on 127.0.0.1 is safe and a listener on 0.0.0.0 is bad, without noting which program owns it and whether that offer was planned.
- Using Get-ADUser or Get-ADComputer on a stand alone computer with no domain, then concluding the directory is empty when the command simply does not apply there.
- Changing your filter between reviews, so a difference in the list may come from your command, not from a change on the machine, and the comparison misleads a defender.
Knowledge check
Knowledge check
What should a defender look for in a list of listening connections?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: A short list of expected listeners, current accounts, and planned shares is a healthy sign. An unexpected listening port, a stale account, or an open share is a note for a defender, not a toy to explore further, and never a reason to probe a network you do not own.
Next step: Reading Logs and Hardening Basics