Librarium Whitehat
Blog Pics
"Inveniam viam aut faciam" : I will either find a way, or I shall make one


I previously did a paper on using md5 checksumming for watching the local files on your linux servers (see it here), but I was often asked about whether it is worth doing because if an attacker manages to get root on the server, he can change the record files anyway. Well there is an easy way to get around this, really. I'll give you a hint .. it is similar to keeping your log files safe. Still not? Oh well, you're in luck, I got tired of answering the same question the whole time, so I figured I would put the solution up here. The trick is to do local file checking remotely.

What do you Need?
First you need to be able to use ssh to remotely run commands on the server you want to check (see here for help on that) from the checking server, which I will call the checksum server. You will need to be able to do something like..
[root@ check]# ssh "uname -a"
Linux localhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

Once you can do that we will need to create the home directory for our checking scripts. I generally use /admin/check, and we will start with the the file.lst which is used to specify what files we will be checking, for this example I only have the following (add any others you want) in the file.lst..

Next we will look at the setup script. This script is called with an argument of the IP of the remote server you want to to checks on in the future. It creates a record file (using the actual IP as part of the file name) of baseline checksums which we will use to check against. The script looks like this..

rm -rf $KEEP

for x in `cat $LST`
  ssh $TGT "md5sum $x" >> $KEEP

chmod 600 $LST $KEEP

You would call it with the IP and it would create a file, like so..
[root@ check]# ./setup
[root@ check]# ls *.db
[root@ check]# cat
4f0c9578ca38d2303fa39e98b5550e6b  /etc/passwd
de83d9e9afa30353188f531cb27fd3f6  /etc/shadow
06cca61eb8946f48f309f23c5de380c4  /etc/group
[root@ check]#

Now that we have a baseline, lets look at the script we call to actually check the checksum on a regular basis. I call it check (original hey?), and you call it with the IP of the system you will be checking. The actual script looks like..

touch $TMP
for x in `cat $LST`
  ssh $TGT "md5sum $x" >> $TMP

if [ `diff $KEEP $TMP | wc -l` = "0" ]
  echo "All seems fine.."
  diff $KEEP $TMP

rm -rf $TMP

It is used as so..
[root@ check]# ./check
All seems fine..

But to test it I will change one of the files we are watching, and then run it again..
[root@ check]# ./check
< 06cca61eb8946f48f309f23c5de380c4  /etc/group
> ed1a14ad9174cbeeebd357e49aa84f47  /etc/group

So at the end of all this we should have a folder with files in it which looks a bit like this (do not forget to set the permissions) ..
[root@ check]# pwd
[root@ check]# ls -l
total 16
-rw-------    1 root     root          137 Jul 20 13:12
-rwx------    1 root     root          268 Jul 20 12:45 check
-rw-------    1 root     root           35 Jul 20 12:40 file.lst
-rwx------    1 root     root          163 Jul 20 12:46 setup
[root@ check]#

Final Thoughts
See, not at all difficult. Doing this means that even if a hacker gains root access on your machine he cannot alter the baseline checksums as they are not there. As always, the scripts are not meant to be works of art, so feel free to improve them if you want. Some suggestions could be..
As always have fun and learn