find — Search the Filesystem

What is find?

find walks directory trees and returns files/directories matching your criteria — by name, type, permissions, owner, size, or modification time.

OSCP use: Find SUID binaries, writable scripts, config files, .git folders, SSH keys, and pass results to grep or xargs.


Syntax

find [PATH...] [EXPRESSION]
find / -name "*.conf" 2>/dev/null

📌 Common Tests (Expressions)

ExpressionDescription
-name PATTERNFilename (case-sensitive glob)
-iname PATTERNFilename (case-insensitive)
-type fRegular files only
-type dDirectories only
-perm -4000SUID files
-perm -2000SGID files
-perm -002World-writable
-writableWritable by current user
-user NAMEOwned by user
-group NAMEOwned by group
-size +NLarger than N (c=bytes, k=KB, M=MB)
-mtime -NModified within last N days
-maxdepth NLimit search depth
-mindepth NMinimum depth
-emptyEmpty files/dirs
-exec CMD {} \;Run command on each result
-exec CMD {} +Run command with multiple files (faster)

Combine with -and / -or / -not:

find / -name "*.php" -and -writable 2>/dev/null
find / \( -name "*.conf" -o -name "*.config" \) 2>/dev/null

📌 Basic Usage

# By name
find / -name "flag.txt" 2>/dev/null
find / -name "*.conf" 2>/dev/null
find / -iname "*password*" 2>/dev/null
 
# By type
find /var/www -type f -name "*.php"
find / -type d -name ".git" 2>/dev/null
 
# Limit depth (faster)
find / -maxdepth 3 -name "*.txt" 2>/dev/null

📌 PrivEsc — High-Value Finds

# SUID binaries
find / -perm -4000 -type f 2>/dev/null
 
# SGID
find / -perm -2000 -type f 2>/dev/null
 
# World-writable files
find / -writable -type f 2>/dev/null | grep -v proc
 
# Writable directories
find / -writable -type d 2>/dev/null | grep -v proc
 
# Writable by root-owned paths
find / -writable -user root -type f 2>/dev/null
 
# SSH keys
find / -name "id_rsa" -o -name "id_ed25519" 2>/dev/null
 
# Config / cred files
find / -name "*.conf" -o -name "*.config" -o -name "*.xml" 2>/dev/null
find /home -name ".bash_history" 2>/dev/null

📌 Chaining with xargs / grep

# Grep every .conf file for passwords
find / -name "*.conf" 2>/dev/null | xargs grep -l "password" 2>/dev/null
 
# Run file command on all SUID binaries
find / -perm -4000 -type f 2>/dev/null | xargs ls -la
 
# Execute command on each match
find /tmp -name "*.sh" -exec chmod +x {} \;
find . -name "*.txt" -exec cat {} \;

📌 Quick Cheat Sheet

find / -name "filename" 2>/dev/null
find / -name "*.php" 2>/dev/null
find / -perm -4000 -type f 2>/dev/null          # SUID
find / -writable -type f 2>/dev/null
find / -maxdepth 4 -name "*.conf" 2>/dev/null
find / -name "*.conf" 2>/dev/null | xargs grep -l "pass" 2>/dev/null

Always redirect stderr: 2>/dev/null hides permission-denied noise.