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
cat, less, head and tail print files. cut and awk pick columns, sort and uniq order and
dedupe lines, and paste joins them. Chain them with | to turn scan output and system files into lists.
cat, less, head, and tail
Print a whole file:
cat /etc/passwd # whole file to the screenPage through a long file (q quits, / searches):
less /var/log/auth.log # scroll through the fileFirst or last lines of a file:
head -n 20 file # first 20 linestail -n 20 file # last 20 linesFollow a file as it grows:
tail -f /var/log/syslog # print new lines as they arrive| Flag | Meaning |
|---|---|
-n |
Number of lines to print |
-f |
Keeps printing lines as the file grows |
cut
Prints selected fields or characters from each line.
Usernames from /etc/passwd:
cut -d: -f1 /etc/passwd # field 1, split on colonsUsernames and home folders:
cut -d: -f1,6 /etc/passwd # fields 1 and 6Port numbers from nmap’s 22/tcp open ssh lines:
grep "open" scan.nmap | cut -d/ -f1 # text before the first slashThe first 200 characters of each line (long minified lines):
grep -rn "api_key" /var/www 2>/dev/null | cut -c1-200 # api_key hits, cut to 200 characters| Flag | Meaning |
|---|---|
-d |
Field delimiter (default is TAB) |
-f |
Fields to print: 1, 1,6, 2-4, 3- |
-c |
Characters to print, same list format as -f |
cuttreats each delimiter as a field break, and two spaces in a row make an empty field. Squeeze the spaces first withtr -s(see Editing Files), or useawk.
awk
Splits each line into columns. These examples use it for columns only.
The first column (split on spaces and tabs):
awk '{print $1}' access.log # column 1Usernames from /etc/passwd:
awk -F: '{print $1}' /etc/passwd # column 1, split on colonsUsers with a login shell, and their home folders:
awk -F: '$NF ~ /sh$/ {print $1, $6}' /etc/passwd # shell ends in sh: user and homeRegular users (UID 1000 and up):
awk -F: '$3 >= 1000 {print $1}' /etc/passwd # users with UID 1000 or higherThe last column of each line:
awk '{print $NF}' file.txt # last column| Flag | Meaning |
|---|---|
-F |
Field separator (default is any run of spaces and tabs) |
| Variable | Meaning |
|---|---|
$1, $2 … |
Column 1, column 2 … |
$0 |
The whole line |
$NF |
The last column |
awksplits on any run of spaces by default. Use it overcuton output with aligned columns, such asps auxorss -tulpn.
sort and uniq
sort orders lines. uniq deletes or counts repeated lines next to each other.
Sort and drop duplicates (dedupe a wordlist):
sort -u users.txt -o users.txt # dedupe the file in placeSort numbers and IP addresses:
sort -n ports.txt # numeric ordersort -V hosts.txt # IP orderCount each value, most frequent first:
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head # top 10 values by countSort /etc/passwd by UID:
sort -t: -k3 -n /etc/passwd # field 3 as a numberLines that appear more than once, or only once:
sort users.txt | uniq -d # repeated linessort users.txt | uniq -u # unique lines| Flag | Meaning |
|---|---|
sort -u |
Drop duplicate lines |
sort -n |
Sort as numbers |
sort -r |
Reverse the order |
sort -V |
Version sort: puts 10.0.0.3 before 10.0.0.20 |
sort -t |
Field separator for -k |
sort -k n |
Sort on field n |
sort -o file |
Write the result to file (it can be the input file) |
uniq -c |
Prefix each line with its count |
uniq -d |
Print only the repeated lines |
uniq -u |
Print only the lines that appear once |
uniqcompares only neighbouring lines. Runsortfirst.
paste
Joins lines side by side or into one line.
Join all lines into one comma list (ports for nmap -p):
paste -sd, ports.txt # all lines as one comma listPair two lists line by line as user:pass:
paste -d: users.txt passwords.txt # line n of each file, joined by a colon| Flag | Meaning |
|---|---|
-s |
Join all lines of one file into a single line |
-d |
Delimiter between joined items (default is TAB) |
Pipelines
Open ports from a grepable nmap scan as one comma list, then scan them in depth:
# open ports as a comma listports=$(grep -oE '[0-9]+/open' scan.gnmap | cut -d/ -f1 | sort -un | paste -sd,)nmap -sC -sV -p$ports $TARGET # deep scan of those ports-sC runs the default scripts and -sV reads service versions.
Live hosts from a ping sweep into a target list:
# up hosts from the sweep, saved to hosts.txtnmap -sn 10.129.2.0/24 -oG - | grep "Status: Up" | awk '{print $2}' | tee hosts.txt-sn pings without a port scan, and -oG - writes grepable output to the pipe.
Users with a login shell as a wordlist:
grep -E 'sh$' /etc/passwd | cut -d: -f1 | tee users.txt # login-shell users to users.txtuser:hash pairs from /etc/shadow for cracking (locked accounts left out):
sudo awk -F: '$2 ~ /^\$/ {print $1":"$2}' /etc/shadow > hashes.txt # hashes that start with $Merge wordlists, lowercase, and dedupe:
cat list1.txt list2.txt | tr 'A-Z' 'a-z' | sort -u > merged.txt # one lowercase list, no duplicatesPassword strings in a web root, one short line per hit:
grep -rniE 'pass(word)?\s*=' /var/www 2>/dev/null | cut -c1-200 | sort -u # pass= and password= assignments
grepis in Searching Files, andtrandteein Editing Files. More leads to search for are in Local Enumeration and Privesc Primer.