From time to time paranoia catches up with me, and while I run chkrootkit from time to time, any rootkit I would write would remove it automatically from any cron job or simply remove or hack the chkrootkit script.

That's why I extracted the most interesting functionality of chkrootkit into a Perl script.

The following Perl script checks if there are processes running (in /proc/$pid/exe) for which the binary does not exist anymore. Most likely this is either a service that I upgraded and forgot to restart or it is a trojan/rootkit that got installed, started itself and then removed the binaries to hide the traces.

This will not protect against loadable kernel modules or any other code that dosen't live within its own separate process.

#!/usr/bin/perl -w use strict; my $i; for $i (0..65536) { #test -f $i/cmdline && (cat $i/cmdline; echo $i); if (-f "/proc/$i/exe") { my $target = readlink "/proc/$i/exe"; if (! -f $target) { open F, "< /proc/$i/cmdline"; my @data = <F>; print "ALERT: '@data' is running (pid $i)\n"; close F; local $/ = "\x00"; open F, "< /proc/$i/environ"; @data = <F>; print "ALERT: 'environment: @data'\n"; close F; }; }; };

Update: theorbtwo noted the bug about the missing localization of $/, which will play bad with the my @data = <F> as soon as you have more than one file.

As a lesson, you should always use local on the punctuation variables, unless you have a compelling reason.

Update 2: PodMaster noted s!(doesn't) (within)!$1 live $2!

Update 3: People seem to be reading this note - s!dosen't!doesn't!


In reply to Checking for unlinked process executables by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.