Searching 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-ChildItem walks the filesystem and filters by name, attribute, time, and size. Select-String
searches inside files, the grep of PowerShell. On a Windows target the main searches are config files
that hold credentials, files you can write, and interesting file names.
Get-ChildItem
List a tree, files only, hidden ones included, and swallow the access-denied errors:
Get-ChildItem C:\ -Recurse -File -Force -ErrorAction SilentlyContinue # every file, errors hiddengci, ls, and dir are aliases. The filters that matter:
| Flag | Meaning |
|---|---|
-Recurse |
Walk subdirectories |
-Depth N |
Recurse at most N levels |
-File / -Directory |
Only files, or only directories |
-Force |
Include hidden and system items |
-Filter 'p' |
One wildcard pattern, applied by the provider. Fastest |
-Include 'a','b' |
Several patterns. Needs -Recurse or a \* path |
-Exclude 'p' |
Drop matching names |
-ErrorAction SilentlyContinue |
Skip the access-denied errors, like 2>/dev/null |
By name
One fast pattern with -Filter:
Get-ChildItem C:\ -Recurse -Filter *.kdbx -ErrorAction SilentlyContinue # KeePass databasesSeveral patterns with -Include:
Get-ChildItem C:\Users -Recurse -Include *.config,*.xml,*.ini -ErrorAction SilentlyContinue # config files
-Filtertakes one pattern and runs inside the filesystem, so it is much faster on a big tree.-Includetakes a list but filters after listing, and does nothing without-Recurseor a path that ends in\*.
By time and size
Files changed in the last 30 minutes (the -mmin -30 analog):
Get-ChildItem C:\ -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -gt (Get-Date).AddMinutes(-30) }Files over 100 MB, newest first:
Get-ChildItem C:\ -Recurse -File -ErrorAction SilentlyContinue | Where-Object Length -gt 100MB | Sort-Object Length -DescendingWhere-Object is how you combine tests: chain conditions with -and and -or, the way find joins
tests with -a and -o:
Get-ChildItem C:\Users -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Extension -eq '.txt' -and $_.Length -lt 10KB }| Property | Meaning |
|---|---|
LastWriteTime, CreationTime |
When the content last changed, and the creation time |
Length |
Size in bytes. KB, MB, GB literals work |
Extension |
The file extension, with the dot |
FullName |
The full path |
Select-String
Search inside files for a pattern, recursively, with line numbers:
Get-ChildItem C:\inetpub -Recurse -File -ErrorAction SilentlyContinue | Select-String -Pattern 'password' # files that contain "password"Search one glob directly, case-insensitive by default:
Select-String -Pattern 'connectionString' -Path C:\inetpub\wwwroot\*.config # one folder of config files| Flag | Meaning |
|---|---|
-Pattern 'regex' |
The regex to match. -SimpleMatch for a literal string |
-Path 'glob' |
Files to search. One level only, so pipe gci -Recurse for a tree |
-List |
Stop at the first match in each file, for a filename list |
-Context A,B |
Print A lines after and B before each match |
-CaseSensitive |
Match case (the default ignores it) |
-NotMatch |
Print the lines that do not match |
Select-String -Pathdoes not recurse. For a whole tree, pipeGet-ChildItem -Recurseinto it. Pull just the text with(Select-String ...).Line.
findstr
The cmd built-in findstr searches file contents without PowerShell:
findstr /SIM /C:"password" *.config *.xml *.ini *.ps1/S recurses, /I ignores case, /M prints only matching filenames, and /C: is the search string.
It is the tool to reach for from a plain cmd shell. Hunting (Windows) uses
it for credential hunting.
Pentest searches
Config files that mention a password, with the line:
Get-ChildItem C:\ -Recurse -Include *.config,*.xml,*.ini,*.ps1,*.txt -ErrorAction SilentlyContinue | Select-String -Pattern 'password|pwd|secret' -ListDeployment and vault files that keep credentials:
Get-ChildItem C:\ -Recurse -Include unattend.xml,sysprep.xml,web.config,*.kdbx,*.ppk,id_rsa -ErrorAction SilentlyContinueThe registry also stores passwords. Search a hive for a string:
reg query HKLM /f password /t REG_SZ /s # values that contain "password" under HKLMFind a binary on PATH, the which of Windows:
where.exe python.exe nc.exe # paths of these tools, if presentCredential-hunting spots and tools are in Hunting (Windows). Files you can write, which matter for privilege escalation, are in Privesc Primer. Credential stores to dump are in Looting (Windows).