Reading Files
CPTS Notes
- Foundations
- Enumeration with Nmap
- Passwords
- Wordlists
- Cracking
- Protected Files
- Spraying and Defaults
- Hunting (Windows)
- Hunting (Linux)
- Hunting (Shares)
- Hunting (Traffic)
- Looting (Windows)
- Looting (Linux)
- File Services
- FTP
- SMB
- NFS
- Rsync
- Network
- DNS
- SNMP
- SMTP
- Mail Retrieval
- Cloud Mail
- Databases
- MySQL
- MSSQL
- Remote Access
- SSH
- R-Services
- RDP
- WinRM
- WMI
- Linux
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- File Transfer
- Choosing a Method
- Serving Files
- Windows Transfers
- Linux Transfers
- Code One-Liners
- Session Transfers
- Protected Transfers
- Detection & Evasion
- Windows
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- Reference
- Port References
- DNS Resolution
- The Mail Pipeline
- SMB & NetBIOS
Get-Content prints files, with -TotalCount and -Tail for the first or last lines. PowerShell pipes
objects, not text, so Select-Object, Sort-Object, and -split take the place of cut, sort, and
awk. Chain them with | to turn command output and config files into lists.
Get-Content
Print a whole file (cat, gc, and type are aliases):
Get-Content C:\Users\bob\notes.txt # whole file to the screenFirst or last lines:
Get-Content app.log -TotalCount 20 # first 20 linesGet-Content app.log -Tail 20 # last 20 linesFollow a file as it grows (like tail -f):
Get-Content app.log -Tail 10 -Wait # print new lines as they arrivePage through a long file (q quits):
Get-Content app.log | more # scroll through the file| Flag | Meaning |
|---|---|
-TotalCount N |
Read only the first N lines (aliases -Head, -First) |
-Tail N |
Read only the last N lines (alias -Last) |
-Wait |
Keep the file open and print new lines as they arrive |
-Raw |
Read the whole file as one string, not an array of lines |
Get-Contentreturns an array of lines, so(Get-Content file).Countcounts them and(Get-Content file)[0]is the first line.-Rawgives one string instead, which regex and parsers often need.
Picking columns
PowerShell splits a line with the -split operator, which takes a regex:
(Get-Content hosts.txt) | ForEach-Object { ($_ -split '\s+')[1] } # second whitespace field of each lineSplit on a set character, such as a CSV comma:
Get-Content users.csv | ForEach-Object { ($_ -split ',')[0] } # first comma fieldFor real structured data, parse it instead of splitting. ConvertFrom-Csv and Import-Csv give named
columns:
Import-Csv users.csv | Select-Object -ExpandProperty Email # the Email columnSelect-Object picks object properties, the object-world version of choosing columns:
Get-Process | Select-Object Name, Id, Path # three properties of each process| Operator | Meaning |
|---|---|
-split 'regex' |
Split a string into parts on a regex |
($x -split ',')[0] |
The first part, index 0 |
Select-Object -ExpandProperty P |
The raw value of property P, not a wrapped object |
An aligned-column text file splits on
'\s+'(one or more spaces). A delimited file splits on its delimiter, or parses withConvertFrom-Csv -Delimiter.
Sorting and deduping
Sort-Object orders lines, and -Unique drops duplicates:
Get-Content users.txt | Sort-Object -Unique # sorted, no duplicatesSort as numbers, not text:
Get-Content ports.txt | Sort-Object { [int]$_ } # numeric orderCount each value, most frequent first (the sort | uniq -c | sort -rn idiom):
Get-Content access.log | Group-Object | Sort-Object Count -Descending | Select-Object Count, Name| Command | Meaning |
|---|---|
Sort-Object |
Order items |
Sort-Object -Unique |
Order and drop duplicates |
Sort-Object -Descending |
Reverse order |
Group-Object |
Group equal items, with a Count for each |
Select-Object -First N / -Last N |
Keep the first or last N items |
Sort-Object -Uniquesorts and dedupes in one pass.Get-Uniqueonly drops duplicates that are already next to each other, so it needs a sort first, like Linuxuniq.
Joining and pipelines
Join lines into one string (the paste -sd, move, for an nmap -p port list):
(Get-Content ports.txt) -join ',' # all lines as one comma listPair two lists line by line as user:pass:
$u = Get-Content users.txt; $p = Get-Content pass.txt0..($u.Count - 1) | ForEach-Object { "$($u[$_]):$($p[$_])" } # line n of each, joined by a colonBuild a target list from a command. Reachable hosts from a /24, one IP per line:
# hosts that answer a ping, saved to hosts.txt1..254 | ForEach-Object { "10.129.2.$_" } | Where-Object { Test-Connection $_ -Count 1 -Quiet } | Tee-Object hosts.txtTee-Object writes to a file while it prints, like Linux tee (see
Editing Files). Pull user:hash pairs out of a secretsdump file:
Select-String ':::' sam.txt | ForEach-Object { ($_ -split ':')[0,3] -join ':' } # user:nthash
Select-Stringis the grep of PowerShell. It and the rest of the search tools are in Searching Files. Credential stores to read on a host are in Looting (Windows) and Hunting (Windows).