3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 # many updates/tweaks --SY.
10 # The following list should contain the mount points of all filesystems
11 # that are to be scanned as a space-separated list within parentheses.
12 # / will usually be in this list and if you have /usr
13 # on a separate partition, it will also be in this list. Other non-special
14 # filesystems where suspicious files could be located should also be put in
16 # Mount points whose filesystems are special, such as procfs or sysfs should
17 # not be in this list.
29 # Files with a path prefix found in the following list are ignored.
30 # DO !!!!NOT!!! PUT /usr/src OR WHATEVER THE HOME DIRECTORY prefix is for your
31 # package users into this list!!! You DO want to scan those directories in
32 # order to spot e.g. world-writable tarballs and other abominations that
34 # Ideally, this list should be empty.
36 prune_prefixes=(/root /{,*/{,*/}}lost+found) #NO TRAILING SLASHES!!!
40 # /{,*/{,*/}}lost+found \
43 # /opt/sql-ledger/{spool,templates,users,css} \
44 # /etc/apache/ssl.key \
45 # /etc/audisp/plugins.d \
49 # /etc/polkit-1/rules.d \
53 # /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
56 # /usr/share/polkit-1/rules.d \
58 # /var/{cache,chroot,db,run,snmp,spool} \
59 # /var/lib/{sshd,nfs,spamassassin,pulse,machines}) #NO TRAILING SLASHES!!!!
61 # Set the following to `-noleaf' if you are scanning non-UNIX filesystems
62 # like MS-DOS, CD-ROM etc. But only do so if you really need it as it
63 # will slow the search significantly.
67 # If the following variable is set to "yes", then files that contain
68 # control characters or other non-printable characters (except for space)
69 # will be reported as suspicious.
70 # This test slows down the search considerably!
75 # suppress ugly debug output from shell
78 # "-false" as 1st argument is used when called by list_suspicious_files_from
79 if [ $# -ge 1 -a "$1" != "-false" ]; then
81 echo 1>&2 "USAGE: ${0##*/}"
83 echo 1>&2 ' Outputs a categorized list of files and directories with properties'
84 echo 1>&2 ' that could mean trouble and should be investigated.'
90 usergroupmatch=(-true)
91 if [ "$1" = "-false" ]; then
92 usergroupmatch=(\( "$@" \))
95 # construct find commands that match the prune_prefixes. Each prefix will be
96 # matched as -path <prefix> -or -path <prefix>/*
97 # so that the directory itself and all subdirectories are matched.
99 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
103 y[${#y[@]}]="${prune_prefixes[$i]}"
106 y[${#y[@]}]="${prune_prefixes[$i]}/*"
110 illchars=( $'\x1' $'\x2' $'\x3' $'\x4' $'\x5' $'\x6' $'\x7' $'\x8'
111 $'\x9' $'\xA' $'\xB' $'\xC' $'\xD' $'\xE' $'\xF' $'\x10' $'\x11'
112 $'\x12' $'\x13' $'\x14' $'\x15' $'\x16' $'\x17' $'\x18' $'\x19'
113 $'\x1A' $'\x1B' $'\x1C' $'\x1D' $'\x1E' $'\x1F' $'\x7f' $'\x80'
114 $'\x81' $'\x82' $'\x83' $'\x84' $'\x85' $'\x86' $'\x87' $'\x88'
115 $'\x89' $'\x8A' $'\x8B' $'\x8C' $'\x8D' $'\x8E' $'\x8F' $'\x90'
116 $'\x91' $'\x92' $'\x93' $'\x94' $'\x95' $'\x96' $'\x97' $'\x98'
117 $'\x99' $'\x9A' $'\x9B' $'\x9C' $'\x9D' $'\x9E' $'\x9F' )
120 if [ "$enable_illchars" = yes ]; then
123 for ((i=0; $i<${#illchars[@]}; i=$i+1))
125 #handle bash \x7f error
126 if [ "*${illchars[$i]}*" = "**" ]; then
127 illchars[$i]=$'\x80' #'
129 illname[${#illname[@]}]='-or'
130 illname[${#illname[@]}]=-name
131 illname[${#illname[@]}]="*${illchars[$i]}*"
133 illname[${#illname[@]}]=')'
136 for ((i=0; $i<${#illchars[@]}; i=$i+1))
138 illlink[${#illlink[@]}]='-or'
139 illlink[${#illlink[@]}]=-lname
140 illlink[${#illlink[@]}]="*${illchars[$i]}*"
142 illlink[${#illlink[@]}]=')'
143 else #if [ "$enable_illchars" = no ]
152 echo -printf "increment_code_here"
154 echo "1 ${1}\\n" | sed 's/ /\\040/g'
155 echo -printf "insert_code_here"
159 echo "2 %i 1 ${2}\\n" | sed 's/ /\\040/g'
160 echo -printf "insert_code_here"
162 echo "2 %i 2 " | sed 's/ /\\040/g'
164 echo -printf "2\\040"
167 echo -exec ls -T 0 -ladQ {} \;
171 filegoodperm=(\( -perm 644 -or -perm 755 -or -perm 555 -or -perm 444 -or -perm 600 -or -perm 700 -or -perm 640 \))
172 dirgoodperm=(\( -perm 755 -or -perm 555 -or -perm 700 -or -perm 750 \))
175 -not \( -not -type d -links +1 \)
176 -not -nouser -not -nogroup
177 -not \( "${illname[@]}" \)
178 -not \( "${illlink[@]}" \)
182 \( -type f -not -group install "${filegoodperm[@]}" \)
183 -or \( -type d -not -group install "${dirgoodperm[@]}" \)
184 -or \( -type d -group install \( -perm 1775 \) \)
185 -or \( -type d -group root -user root -path "/tmp" \( -perm 1777 \) \)
186 -or \( -type d -group root -user root -path "/var/tmp" \( -perm 1777 \) \)
187 -or \( -not -type d -not -type f -not -type l -path "/dev/*" \)
188 -or \( -type l \( -xtype b -or -xtype c -or -xtype d -or -xtype p -or -xtype f \) \)
193 \( "${illname[@]}" $(report "NON-PRINTABLE CHAR IN NAME") \)
194 OP \( "${illlink[@]}" $(report "NON-PRINTABLE-CHAR IN LINK-TARGET") \)
195 OP \( -type f -perm -4000 $(report "SETUID FILES") \)
196 OP \( -type f -perm -2000 $(report "SETGID FILES") \)
197 OP \( -type f -perm -1000 $(report "STICKY FILES") \)
198 OP \( -type d -perm -2000 $(report "GROUP-KEEPING DIRECTORIES") \)
199 OP \( -type d -not -group install -perm -1000 $(report "STICKY DIRECTORIES") \)
200 OP \( -type f -perm -g+w $(report "GROUP-WRITABLE FILES") \)
201 OP \( -type f -perm -o+w $(report "WORLD-WRITABLE FILES") \)
202 OP \( -type d -perm -g+w $(report "GROUP-WRITABLE DIRECTORIES") \)
203 OP \( -type d -perm -o+w $(report "WORLD-WRITABLE DIRECTORIES") \)
204 OP \( -not \( -type f -or -type l -or -type d \) -not -path "/dev/*" $(report "SPECIAL FILES OUTSIDE /dev") \)
205 OP \( -type d -group install -not -perm 1755 $(report "INSTALL DIRECTORIES WITH UNUSUAL PERMISSIONS") \)
206 OP \( -type f -group install $(report "FILES ASSIGNED TO GROUP INSTALL") \)
207 OP \( -type l -not \( -xtype b -or -xtype c -or -xtype d -or -xtype p -or -xtype f \) $(report "SYMLINKS POSSIBLY BROKEN OR LOOP") \)
208 OP \( -not -type d -links +1 $(report "HARDLINKED FILES" "Inode %i is shared by %n files, including") \)
209 OP \( -nouser $(report "THINGS HAVING UID WITH NO ASSIGNED USER NAME") \)
210 OP \( -nogroup $(report "THINGS HAVING GID WITH NO ASSIGNED GROUP NAME") \)
211 OP \( -type f -not -group install -not "${filegoodperm[@]}" $(report "FILES WITH UNUSUAL PERMISSIONS") \)
212 OP \( -type d -not -group install -not "${dirgoodperm[@]}" $(report "DIRECTORIES WITH UNUSUAL PERMISSIONS") \)
215 # insert unique codes for the messages
217 for ((i=0; $i<${#bad[@]}; i=$i+1))
219 if [ "${bad[$i]}" = "increment_code_here" ]; then
222 elif [ "${bad[$i]}" = "insert_code_here" ]; then
227 allbad=() #all bad matches are reported
228 onebad=() #only the first bad match is reported
229 for ((i=0; $i<${#bad[@]}; i=$i+1))
231 if [ "${bad[$i]}" = "OP" ]; then
235 allbad[$i]="${bad[$i]}"
236 onebad[$i]="${bad[$i]}"
240 # Add a default case to onebad.
241 # This should never be hit, because the explicit cases should catch all
242 # files, but just in case I've missed something, this will catch it.
243 onebad=("${onebad[@]}" -or $(report "WEIRD SHIT") )
245 # make allbad always return false
246 allbad=("${allbad[@]}" , -false)
248 cmd=( "${usergroupmatch[@]}" -and
249 \( \( "${good[@]}" \) -or \( "${allbad[@]}" \) -or \( "${onebad[@]}" \) \)
252 # In the following find command, the part
253 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
254 # is responsible for preventing the files that match prune_prefixes from
255 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
256 # -prune has no effect and is always false when -depth is used.
257 find "${fs_to_scan[@]}" -xdev $NOLEAF \
258 -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
259 -and \( "${cmd[@]}" \) 2>/dev/null |
260 sed 's/^\(...2\) \([0-9]\+ 2 \)\?\([^ ]\+\) \+[^ ]\+ \+\([^ ]\+\) \+\([^ ]\+\) \+[^"]\+\(".\+\)/\1 \2\3 \6 \4:\5/' |
262 sed 's/^...1 /\'$'\n''/;s/^...2 [0-9]\+ 1 /\'$'\n'' /;s/^...2 [0-9]\+ 2 / /;s/^...2 / /'