~/windows-searching-files

Searching Files

CPTS Notes4 min read

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 hidden

gci, 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 databases

Several patterns with -Include:

Get-ChildItem C:\Users -Recurse -Include *.config,*.xml,*.ini -ErrorAction SilentlyContinue # config files

-Filter takes one pattern and runs inside the filesystem, so it is much faster on a big tree. -Include takes a list but filters after listing, and does nothing without -Recurse or 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 -Descending

Where-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 -Path does not recurse. For a whole tree, pipe Get-ChildItem -Recurse into 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' -List

Deployment and vault files that keep credentials:

Get-ChildItem C:\ -Recurse -Include unattend.xml,sysprep.xml,web.config,*.kdbx,*.ppk,id_rsa -ErrorAction SilentlyContinue

The registry also stores passwords. Search a hive for a string:

reg query HKLM /f password /t REG_SZ /s # values that contain "password" under HKLM

Find a binary on PATH, the which of Windows:

where.exe python.exe nc.exe # paths of these tools, if present

Credential-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).