< Previous Page Return to Title Page Next Page >

Conditional Logging in Apache

  • A  primitive log monitor can be implemented directly within Apache using the built-in regex matching and conditional logging. The SetEnvIf and LogFormat directives can filter log messages and then send formatted lines of data to an external log monitor... or as commands to a shell! For example:

  •  
    # Flag requests for URIs containing common strings from Nimda-like worms 
    # (including Code Red, sadmind/IIS). Note that the patterns below are regexes;
    # remember to escape dots and other characters with special significance!
    
    SetEnvIf Request_URI "/winnt/system32/cmd\.exe" worm
    SetEnvIf Request_URI "/scripts/root\.exe" worm
    SetEnvIf Request_URI "/MSADC/root\.exe" worm
    # Don't use the following patterns if you use "upreferences" in URIs
    SetEnvIf Request_URI "/\.\." worm
    SetEnvIf Request_URI "\.\./" worm
    # Block attackers who send the patterns above within URIs. The command below
    # uses a blackhole route. It's more efficient to firewall (the command
    # will vary depending upon the firewall in use) or to use SSH to add rules to
    # an upstream firewall to block the attacker, but this method has the
    # advantage that it is relatively independent of configuration. If several 
    # commands must be executed, or if postprocessing of output is desired, it
    # is best to invoke a script or compiled program rather than doing all the 
    # work from within httpd.conf.
    
    CustomLog "|exec sh" "route -nq add -host %a 127.0.0.1 -blackhole" env=worm
    # Note that no input from the client is used in the shell command, so this
    # set of directives is not subject to exploits via crafted strings. If strings
    # from the client were used, stronger input validation would be in order.
  • Unfortunately, what one can do within Apache is very limited. So, it pays to write an independent application that can fully parse the log, gather statistics, and take action when certain conditions are spotted. But will such a program be very large? Not if it is in the right language for the job....