1

Question: how can I find all world-writable files that are referenced in the startup scripts found in the /etc/init.d directory of a RHEL 5 server?

Constraints:

  • BASH solutions only (this is part of a larger script)
  • No additional tools/packages can be installed

Here's what I have so far.

worldWritable=$(find / -type f -perm -002)
startupScripts=$(ls -l /etc/init.d/* | tr '\011' ' ' | tr -s ' ' | cut -f 9,9 -d " ")
for file in $worldWritable
do
    for line in $startupScripts
    do
        grep -w "$file" $line | grep -v "^#" >> outFile
    done
done

This sort of works, but it takes a LONG time, and it includes a lot that's not correct (at least not what I'm looking for). I just need "outFile" to contain a list of the world-writable files found to be referenced in any script in the /etc/init.d directory.

I don't mind completely abandoning this approach if anyone can offer a better solution. I just need something faster and more reliable. Thanks!

jjfromnh
  • 109
  • 10
  • I'm not sure exactly but maybe something like `find / -type f -perm -002 -exec grep -l 'searchstring' {} \;` could reduce the amount of time. – I0_ol Oct 31 '16 at 17:04

1 Answers1

0

The major slowness certainly comes from the find /, scanning the entire filesystem. It will be faster to do the converse:

  • Extract all the absolute paths from the init scripts
    • Using an appropriate regex, for example excluding matches where a # occurs earlier on the same line
  • For each extracted potential path, check that:
    • The file actually exists
    • The file is world writable

The result should be significantly faster.

janos
  • 120,954
  • 29
  • 226
  • 236
  • Yeah I had a similar thought, but I tested the `find /` and it only takes a little over 1 minute. The big slowdown is the loop--it took just under an hour. I'll try the reverse approach you suggest and see if I have any more luck. – jjfromnh Oct 31 '16 at 18:17
  • Yep, that ended up being a much better approach. Thanks for the feedback. – jjfromnh Nov 01 '16 at 11:51