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
sed edits lines in a stream or in place, and tr replaces or deletes characters. tee writes output to
a file while you watch it. touch creates files and sets their timestamps.
sed
Edits a stream line by line: substitute, delete, or print.
Replace every match on each line:
sed 's/http:/https:/g' urls.txt # http: to https: on every lineSet your IP in an exploit, in place, and keep a backup:
sed -i.bak 's/10\.10\.10\.10/10.10.14.5/g' exploit.py # swap the IP, keep exploit.py.bakPrint lines 10 to 20:
sed -n '10,20p' /var/log/syslog # lines 10 to 20 onlyDelete comments and blank lines:
sed -E '/^\s*(#|$)/d' /etc/ssh/sshd_config # active settings only| Flag | Meaning |
|---|---|
-n |
Print nothing unless a p command says so |
-i |
Edit the file in place. -i.bak keeps a copy as file.bak |
-E |
Extended regex |
| Command | Meaning |
|---|---|
s/old/new/ |
Replace the first match on each line |
s/old/new/g |
Replace every match on each line |
10,20p |
Print lines 10 to 20 |
/regex/d |
Delete lines that match |
A
.in the pattern matches any character. Escape it as\.in IPs and hostnames.
tr
Replaces, deletes, or squeezes characters.
Lowercase a wordlist:
tr 'A-Z' 'a-z' < users.txt # uppercase to lowercaseDelete Windows line endings (the bad interpreter: /bin/bash^M error):
tr -d '\r' < script.sh > script_fixed.sh # strip carriage returnsSqueeze repeated spaces into one:
ps aux | tr -s ' ' | cut -d' ' -f1,2,11 # user, PID, and commandTurn a comma list into one item per line:
echo "22,80,445" | tr ',' '\n' # one port per line| Flag | Meaning |
|---|---|
-d |
Delete the listed characters |
-s |
Squeeze each run of a repeated character into one |
tee
Writes its input to the screen and to a file at the same time.
Save a scan while you watch it:
nmap -sC -sV $TARGET | tee scan.txt # print and save to scan.txtAppend to a file:
gobuster dir -u http://$TARGET -w common.txt | tee -a web.txt # print and append to web.txtAdd a vhost to /etc/hosts as root:
echo "$TARGET site.htb" | sudo tee -a /etc/hosts # map site.htb to the target| Flag | Meaning |
|---|---|
-a |
Append to the file and keep its content |
A
sudo echoredirect into/etc/hostsfails: your shell opens the file for the redirect as your own user.sudo tee -aopens it as root.
touch
Creates empty files and sets file timestamps.
Create files, or update the time of files that exist:
touch notes.txt users.txt # create both, or update their timesCopy the timestamps of another file:
touch -r /usr/bin/passwd /usr/local/bin/helper # helper gets the times of passwdSet a date:
touch -d "2023-01-15 09:30" shell.php # date as a stringtouch -t 202301150930 shell.php # date as a stampChange only the modification time:
touch -m -d "2023-01-15 09:30" shell.php # modification time only| Flag | Meaning |
|---|---|
-r file |
Use the timestamps of file |
-d "date" |
Use a date string, such as "2023-01-15 09:30" or "yesterday" |
-t stamp |
Use [[CC]YY]MMDDhhmm[.ss] |
-a |
Change only the access time |
-m |
Change only the modification time |
touchcannot set the change time (ctime).statstill shows when you touched the file.