SANS Digital Forensics and Incident Response Blog

Finer Points of Find

The *NIX "find" command is probably one of the system security tester's best friends on any *NIX system. This command allows the system security tester or digital forensic analyst to process a set of files and/or directories in a file subtree. In particular, the command has the capability to search based on the following parameters:

  • where to search (which pathname and the subtree)
  • what category of file to search for (use "-type" to select directories, data files, links)
  • how to process the files (use "-exec" to run a process against a selected file)
  • the name of the file(s) (the "-name" parameter)
  • perform logical operations on selections (the "-o" and "-a" parameters)

One of the key problems associated with the "find" command is that it can be difficult to use. Many experienced professionals with years of hands-on experience on *NIX systems still find this command to be tricky. Adding to this confusion are the differences between *NIX operating systems. The find command provides a complex subtree traversal capability. This includes the ability to traverse excluded directory tree branches and also to select files and directories with regular expressions. As such, the specific types of file system searched with his command may be selected.

The find utility is designed for the purpose of searching files using directory information. This is in effect also the purpose of the "ls" command but find goes far further. This is where the difficulty comes into play. Find is not typical *NIX command with a large number of parameters, but is rather a miniature language in its own right.

The first option in find consists of setting the starting point or subtrees under which the find process will search. Unlike many commands, find allows multiple points to be set and reads each initial option before the first "-" character. This is, the one command may be used to search multiple directories on a single search. The paper, "Advanced techniques for using the *NIX find command" by B. Zimmerly provides an ideal introduction into the more advanced features of this command and is highly recommended that any system security tester become familiar with this. This section of the chapter is based on much of his work.

The complete language of find is extremely detailed consisting of numerous separate predicates and options. GNU find is a superset of the POSIX version and actually contains an even more detailed language structure. This difference will only be used within complex scripts as it is highly unlikely that this level of complexity would be effectively used interactively.:

  • -name True if pattern matches the current file name. Simple regex (shell regex) may be used. A backslash (\) is used as an escape character within the pattern. The pattern should be escaped or quoted. If you need to include parts of the path in the pattern in GNU find you should use predicate "wholename"
  • "-(a,c,m)time" as possible search may file is last "access time", "file status" and "modification time", measured in days or minutes. This is done using the time interval in parameters -ctime, -mtime and -atime. These values are either positive or negative integers.
  • -fstype type True if the filesystem to which the file belongs is of type type. For example on Solaris mounted local filesystems have type ufs (Solaris 10 added zfs). For AIX local filesystem is jfs or jfs2 (journalled file system). If you want to traverse NFS filesystems you can use nfs (network file system). If you want to avoid traversing network and special filesystems you should use predicate local and in certain circumstances mount
  • "-local" This option is true where the file system type is not a remote file system type.
  • "-mount" This option restricts the search to the file system containing the directory specified. The option does not list mount points to other file systems.
  • "-newer/-anewer/-cnewer baseline" The time of modification, access time or creation time are compared with the same timestamp in the file used as a baseline.
  • "-perm permissions" Locates files with certain permission settings. This is an important command to use when searching for world-writable files or SUID files.
  • "-regex regex" The GNU version of find allows for file name matches using regular expressions. This is a match on the whole pathname not a filename. The "-iregex" option provides the means to ignore case.
  • "-user" This option locates files that have specified ownership. The option" —nouser" locates files without ownership. In the case where there is no user in "/etc/passwd" this search option will find matches to a file's numeric user ID (UID). Files are often created in this way when extracted from a tar acrchive.
  • "-group" This option locates files that are owned by specified group. The option, "-nogroup" is used to refer to searches where the desired result relates to no group that matches the file's numeric group ID (GID) of the file
  • "-xattr" This is a logical function that returns true if the file has extended attributes.
  • "-xdev " Same as the parameter "-mount primary". This option prevents the find command from traversing a file system different from the one specified by the Path parameter.
  • "-size" This parameter is used to search for files with a specified size. The "-size" attribute allows the creation of a search that can specify how large (or small) the files should be to match. You can specify your size in kilobytes and optionally also use + or - to specify size greater than or less than specified argument. For instance:
    • find /usr/home -name "*.txt" -size 4096k
    • find /export/home -name "*.html" -size +100k
    • find /usr/home -name "*.gif" -size -100k
    • "-ls" list current file in "ls —dlis" format on standard output.
    • "-type" Locates a certain type of file. The most typical options for -type are:
      • d A Directory
      • f A File
      • l A Link

Logical Operations

Searches using "find" may be created using multiple logical conditions connected using the logical operations ("AND", "OR" etc). By default options are concatenated using AND. In order to have multiple search options connected using a logical "OR" the code is generally contained in brackets to ensure proper order of evaluation.

For instance \(-perm -2000 -o -perm -4000 \)

The symbol "!" is used to negate a condition (it means logical NOT) . "NOT" should be specified with a backslash before exclamation point ( \! ).

For instance find . \! -name "*.tgz" -exec gzip {} \;

The "\( expression \)" format is used in cases where there is a complex condition.

For instance find / -type f \( -perm -2000 -o -perm -4000 \) -exec /mnt/cdrom/bin/ls -al {} \;

Output Options

The find command can also perform a number of actions on the files or directories that are returned. Some possibilities are detailed below:

  • "-print" The "print" option displays the names of the files on standard output. The output can also be piped to a script for post-processing. This is the default action.
  • "-exec" The "exec" option executes the specified command. This option is most appropriate for executing moderately simple commands.

Find can execute one or more commands for each file it has returned using the "-exec" parameter. Unfortunately, one cannot simply enter the command.

For instance:

  • find . -type d -exec ls -lad {} \;
  • find . -type f -exec chmod 750 {} ';'
  • find . -name "*rc.conf" -exec chmod o+r '{}' \;
  • find . -name core -ctime +7 -exec /bin/rm -f {} \;
  • find /tmp -exec grep "search_string" '{}' /dev/null \; -print

An alternative to the "-exec" parameter is to pipe the output into the "xargs" command. This section has only just touched on find and it is recommended that the system security tester investigate this command further.

A commonly overlooked aspect of the "find" command is in locating files that have been modified recently. The command:

find / -mtime -7 —print

displays files on the system recursively from the ?/' directory up sorted by the last modified time. The command:

find / -atime -7 —print

does the same for last access time. When access is granted to a system and with ever file that is run, the file times change. Each change to a file updates the modified time and each time a file is executed or read, the last accessed time is updated.

These (the last modified and accessed times) can be updated using the touch command.

A Summary of the find command

Effective use of the find command can make any security assessment much simpler. Some key points to consider when searching for files a detailed below:

  • Consider where to search and what subtrees will be used in the command remembering that multiple piles may be selected
    • find /tmp /usr /bin /sbin /opt -name sar
  • The find command allows for the ability to match a variety of criteria
  • -name search using the name of the file(s). This can be a simple regex.
  • -type what type of file to search for ( d — directories, f — files, l — links)
  • -fstype typ allows for the capability to search a specific filesystem type
  • -mtime x File was modified "x" days ago
  • -atime x File was accessed "x" days ago
  • -ctime x File was created "x" days ago
  • -size x File is "x" 512-byte blocks big
  • -user user The file's owner is "user"
  • -group group The file's group owner is "group"
  • -perm p The file's access mode is "p" (as either an integer/symbolic expression)
    • Think about what you will actually use the command for and consider the options available to either display the output or the sender to other commands for further processing
    • -print display pathname (default)
    • -exec allows for the capability to process listed files ( {} expands to current found file )
      • Combine matching criteria (predicated) into complex expressions using logical operations -o and -a (default binding) of predicates specified.

Craig Wright is a Director with Information Defense in Australia. He holds both the GSE-Malware and GSE-Compliance certifications from GIAC and completed the GSE as well. He is a perpetual student with numerous post graduate degrees including an LLM specializing in international commercial lawand ecommerce law, A Masters Degree in mathematical statistics from Newcastle as well as working on his 4th IT focused Masters degree (Masters in System Development) from Charles Stuart University where he lectures subjects in a Masters degree in digital forensics. He is writing his second doctorate, a PhD on the quantification of information system risk at CSU.