2 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
3 # You may do everything with this code except misrepresent its origin.
4 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6 # The following list should contain the mount points of all filesystems
7 # that are to be scanned as a space-separated list within parentheses.
8 # / will usually be in this list and if you have /usr
9 # on a separate partition, it will also be in this list. Other non-special
10 # filesystems where suspicious files could be located should also be put in
12 # Mount points whose filesystems are special, such as procfs or sysfs should
13 # not be in this list.
28 # Files with a path prefix found in the following list are ignored.
29 # DO !!!!NOT!!! PUT /usr/src OR WHATEVER THE HOME DIRECTORY prefix is for your
30 # package users into this list!!! You DO want to scan those directories in
31 # order to spot e.g. world-writable tarballs and other abominations that
33 # Ideally, this list should be empty.
35 prune_prefixes=(/root /*/lost+found) #NO TRAILING SLASHES!!!
40 # /{,*/{,*/}}lost+found \
41 # /etc/polkit-1/localauthority \
44 # /var/lib/{polkit-1,sasl,net-snmp,udisks} \
45 # /var/log/syslog-ng \
49 # If the following variable is set to "yes", then files that contain
50 # control characters or other non-printable characters (except for space)
51 # will be reported as suspicious.
52 # This test slows down the search considerably!
56 # suppress ugly debug output from shell
59 # "-false" as 1st argument is used when called by list_suspicious_files_from
60 if [ $# -ge 1 -a "$1" != "-false" ]; then
62 echo 1>&2 "USAGE: ${0##*/}"
64 echo 1>&2 ' Outputs a categorized list of files and directories with properties'
65 echo 1>&2 ' that could mean trouble and should be investigated.'
71 usergroupmatch=(-true)
72 if [ "$1" = "-false" ]; then
73 usergroupmatch=(\( "$@" \))
76 #construct find commands that match the prune_prefixes. Each prefix will be
77 #matched as -path <prefix> -or -path <prefix>/*
78 #so that the directory itself and all subdirectories are matched.
80 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
84 y[${#y[@]}]="${prune_prefixes[$i]}"
87 y[${#y[@]}]="${prune_prefixes[$i]}/*"
91 illchars=( $'\x1' $'\x2' $'\x3' $'\x4' $'\x5' $'\x6' $'\x7' $'\x8'
92 $'\x9' $'\xA' $'\xB' $'\xC' $'\xD' $'\xE' $'\xF' $'\x10' $'\x11'
93 $'\x12' $'\x13' $'\x14' $'\x15' $'\x16' $'\x17' $'\x18' $'\x19'
94 $'\x1A' $'\x1B' $'\x1C' $'\x1D' $'\x1E' $'\x1F' $'\x7f' $'\x80'
95 $'\x81' $'\x82' $'\x83' $'\x84' $'\x85' $'\x86' $'\x87' $'\x88'
96 $'\x89' $'\x8A' $'\x8B' $'\x8C' $'\x8D' $'\x8E' $'\x8F' $'\x90'
97 $'\x91' $'\x92' $'\x93' $'\x94' $'\x95' $'\x96' $'\x97' $'\x98'
98 $'\x99' $'\x9A' $'\x9B' $'\x9C' $'\x9D' $'\x9E' $'\x9F' )
101 if [ "$enable_illchars" = yes ]; then
104 for ((i=0; $i<${#illchars[@]}; i=$i+1))
106 #handle bash \x7f error
107 if [ "*${illchars[$i]}*" = "**" ]; then
108 illchars[$i]=$'\x80' #'
110 illname[${#illname[@]}]='-or'
111 illname[${#illname[@]}]=-name
112 illname[${#illname[@]}]="*${illchars[$i]}*"
114 illname[${#illname[@]}]=')'
117 for ((i=0; $i<${#illchars[@]}; i=$i+1))
119 illlink[${#illlink[@]}]='-or'
120 illlink[${#illlink[@]}]=-lname
121 illlink[${#illlink[@]}]="*${illchars[$i]}*"
123 illlink[${#illlink[@]}]=')'
124 else #if [ "$enable_illchars" = no ]
133 echo -printf "increment_code_here"
135 echo "1 ${1}\\n" | sed 's/ /\\040/g'
136 echo -printf "insert_code_here"
140 echo "2 %i 1 ${2}\\n" | sed 's/ /\\040/g'
141 echo -printf "insert_code_here"
143 echo "2 %i 2 " | sed 's/ /\\040/g'
145 echo -printf "2\\040"
148 echo -exec ls -T 0 -ladQ {} \;
152 filegoodperm=(\( -perm 644 -or -perm 755 -or -perm 555 -or -perm 444 -or -perm 600 -or -perm 700 -or -perm 640 \))
153 dirgoodperm=(\( -perm 755 -or -perm 555 -or -perm 700 -or -perm 750 \))
156 -not \( -not -type d -links +1 \)
157 -not -nouser -not -nogroup
158 -not \( "${illname[@]}" \)
159 -not \( "${illlink[@]}" \)
163 \( -type f -not -group install "${filegoodperm[@]}" \)
164 -or \( -type d -not -group install "${dirgoodperm[@]}" \)
165 -or \( -type d -group install \( -perm 1775 \) \)
166 -or \( -type d -group root -user root -path "/tmp" \( -perm 1777 \) \)
167 -or \( -type d -group root -user root -path "/var/tmp" \( -perm 1777 \) \)
168 -or \( -not -type d -not -type f -not -type l -path "/dev/*" \)
169 -or \( -type l \( -xtype b -or -xtype c -or -xtype d -or -xtype p -or -xtype f \) \)
174 \( "${illname[@]}" $(report "NON-PRINTABLE CHAR IN NAME") \)
175 OP \( "${illlink[@]}" $(report "NON-PRINTABLE-CHAR IN LINK-TARGET") \)
176 OP \( -type f -perm -4000 $(report "SETUID FILES") \)
177 OP \( -type f -perm -2000 $(report "SETGID FILES") \)
178 OP \( -type f -perm -1000 $(report "STICKY FILES") \)
179 OP \( -type d -perm -2000 $(report "GROUP-KEEPING DIRECTORIES") \)
180 OP \( -type d -not -group install -perm -1000 $(report "STICKY DIRECTORIES") \)
181 OP \( -type f -perm -g+w $(report "GROUP-WRITABLE FILES") \)
182 OP \( -type f -perm -o+w $(report "WORLD-WRITABLE FILES") \)
183 OP \( -type d -perm -g+w $(report "GROUP-WRITABLE DIRECTORIES") \)
184 OP \( -type d -perm -o+w $(report "WORLD-WRITABLE DIRECTORIES") \)
185 OP \( -not \( -type f -or -type l -or -type d \) -not -path "/dev/*" $(report "SPECIAL FILES OUTSIDE /dev") \)
186 OP \( -type d -group install -not -perm 1755 $(report "INSTALL DIRECTORIES WITH UNUSUAL PERMISSIONS") \)
187 OP \( -type f -group install $(report "FILES ASSIGNED TO GROUP INSTALL") \)
188 OP \( -type l -not \( -xtype b -or -xtype c -or -xtype d -or -xtype p -or -xtype f \) $(report "SYMLINKS POSSIBLY BROKEN OR LOOP") \)
189 OP \( -not -type d -links +1 $(report "HARDLINKED FILES" "Inode %i is shared by %n files, including") \)
190 OP \( -nouser $(report "THINGS HAVING UID WITH NO ASSIGNED USER NAME") \)
191 OP \( -nogroup $(report "THINGS HAVING GID WITH NO ASSIGNED GROUP NAME") \)
192 OP \( -type f -not -group install -not "${filegoodperm[@]}" $(report "FILES WITH UNUSUAL PERMISSIONS") \)
193 OP \( -type d -not -group install -not "${dirgoodperm[@]}" $(report "DIRECTORIES WITH UNUSUAL PERMISSIONS") \)
196 #insert unique codes for the messages
198 for ((i=0; $i<${#bad[@]}; i=$i+1))
200 if [ "${bad[$i]}" = "increment_code_here" ]; then
203 elif [ "${bad[$i]}" = "insert_code_here" ]; then
208 allbad=() #all bad matches are reported
209 onebad=() #only the first bad match is reported
210 for ((i=0; $i<${#bad[@]}; i=$i+1))
212 if [ "${bad[$i]}" = "OP" ]; then
216 allbad[$i]="${bad[$i]}"
217 onebad[$i]="${bad[$i]}"
221 #Add a default case to onebad.
222 #This should never be hit, because the explicit cases should catch all
223 #files, but just in case I've missed something, this will catch it.
224 onebad=("${onebad[@]}" -or $(report "WEIRD SHIT") )
226 #make allbad always return false
227 allbad=("${allbad[@]}" , -false)
229 cmd=( "${usergroupmatch[@]}" -and
230 \( \( "${good[@]}" \) -or \( "${allbad[@]}" \) -or \( "${onebad[@]}" \) \)
233 #In the following find command, the part
234 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
235 #is responsible for preventing the files that match prune_prefixes from
236 #being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
237 #-prune has no effect and is always false when -depth is used.
238 find "${fs_to_scan[@]}" -xdev -noleaf \
239 -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
240 -and \( "${cmd[@]}" \) 2>/dev/null |
241 sed 's/^\(...2\) \([0-9]\+ 2 \)\?\([^ ]\+\) \+[^ ]\+ \+\([^ ]\+\) \+\([^ ]\+\) \+[^"]\+\(".\+\)/\1 \2\3 \6 \4:\5/' |
243 sed 's/^...1 /\'$'\n''/;s/^...2 [0-9]\+ 1 /\'$'\n'' /;s/^...2 [0-9]\+ 2 / /;s/^...2 / /'