Editing 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
The -replace operator rewrites text with a regex, and Set-Content writes the result back. Tee-Object
saves output while you watch it. New-Item creates files, and a file’s timestamp properties set its
times. These are the PowerShell forms of sed, tee, and touch.
Replace text
-replace substitutes every regex match in a string:
(Get-Content urls.txt) -replace 'http:','https:' # http: to https: on every lineEdit a file in place. Read it fully first, inside the parentheses, then write it back:
(Get-Content exploit.ps1) -replace '10\.10\.10\.10','10.10.14.5' | Set-Content exploit.ps1 # swap the IPKeep a backup before an in-place edit:
Copy-Item exploit.ps1 exploit.ps1.bak # backup first(Get-Content exploit.ps1) -replace '10\.10\.10\.10','10.10.14.5' | Set-Content exploit.ps1| Operator | Meaning |
|---|---|
-replace 'p','r' |
Replace every regex match of p with r. Case-insensitive |
-creplace 'p','r' |
Same, case-sensitive |
$1, $2 in r |
Insert a captured group from the pattern |
-replacereads the pattern as a regex, so escape.as\.in IPs and hostnames. A literal$in the replacement must be written$$. Without the parentheses,Get-Content ... \| Set-Contentto the same file truncates it before the read finishes.
Transform characters
Lowercase a wordlist (the tr A-Z a-z move):
(Get-Content users.txt).ToLower() # uppercase to lowercaseStrip carriage returns so a script transferred from Windows runs on Linux:
(Get-Content script.sh -Raw) -replace "`r`n","`n" | Set-Content script.sh -NoNewline # CRLF to LFTurn a comma list into one item per line:
'22,80,445' -split ',' | Set-Content ports.txt # one port per lineIn a double-quoted string, PowerShell writes special characters with a backtick:
`ris a carriage return,`na line feed,`ta tab.-NoNewlinestopsSet-Contentfrom adding a trailing newline.
Write output
Tee-Object writes its input to a file and to the screen at once, like Linux tee:
nxc smb $TARGET -u users.txt -p pass.txt | Tee-Object spray.txt # print and saveAppend instead of overwrite:
Get-ChildItem C:\ | Tee-Object -Append listing.txt # print and appendThe redirection operators and the *-Content cmdlets also write files:
systeminfo > sysinfo.txt # overwrite, like Set-Contentwhoami /priv >> sysinfo.txt # append, like Add-ContentAdd-Content notes.txt 'found creds in web.config' # append one line| Command | Meaning |
|---|---|
Tee-Object file |
Write the pipeline to file and pass it on |
Tee-Object -Append |
Add to the file and keep its content |
Set-Content / > |
Write, replacing the file |
Add-Content / >> |
Append to the file |
Out-File -Encoding utf8 |
Write with a chosen text encoding |
Set-Contentand>pick a default encoding that changed between PowerShell 5 and 7. For a file another tool must read back, set it withOut-File -Encoding utf8orSet-Content -Encoding utf8.
Create files and timestamps
Create an empty file (the touch of a new file):
New-Item -ItemType File -Path notes.txt # create an empty fileSet a file’s timestamps, the anti-forensics side of touch:
$f = Get-Item shell.aspx$f.LastWriteTime = '2023-01-15 09:30' # modification time$f.CreationTime = '2023-01-15 09:30' # creation time$f.LastAccessTime = '2023-01-15 09:30' # access timeCopy the timestamps of another file, so a dropped file blends in:
$ref = Get-Item C:\Windows\System32\cmd.exe$f = Get-Item shell.aspx$f.LastWriteTime = $ref.LastWriteTime # match a system file's time| Property | Meaning |
|---|---|
LastWriteTime |
When the content last changed |
CreationTime |
The file’s creation time |
LastAccessTime |
When the file was last read |
New-Item -Forceon a path that exists overwrites it with an empty file, so it is not a safetouch. To only update times, set the properties on the existingGet-Itemobject. Windows keeps a separate MFT change time that these properties do not reach.