今朝目覚めると、ロシアからサービス拒否 (DOS) 攻撃を受けていました。彼らは何十もの IP ブロックから私を攻撃していました。IP の大規模なプールまたは何らかのプロキシ リスト/サービスがあったに違いありません。IP をブロックするたびに、別の IP が表示されました。最後に、スクリプトを探したところ、独自のソリューションを作成する必要があることがわかりました。以下は少しアグレッシブですが、TOP LOAD LEVEL を 200 を超えて実行していました。
これは、リアルタイムで DOS をブロックするために私が書いた簡単なスクリプトです。
cat **"output of the logs"** | php ipchains.php **"something unique in the logs"**
==> PHP スクリプト:
<?php
$ip_arr = array();
while(1)
{
$line = trim(fgets(STDIN)); // reads one line from STDIN
$ip = trim( strtok( $line, " ") );
if( !array_key_exists( $ip, $ip_arr ) )
$ip_arr[$ip] = 0;
$regex = sprintf( "/%s/", $argv[1] );
$cnt = preg_match_all( $regex, $line );
if( $cnt < 1 ) continue;
$ip_arr[$ip] += 1;
if( $ip_arr[$ip] == 1 )
{
// printf( "%s\n", $argv[1] );
// printf( "%d\n", $cnt );
// printf( "%s\n", $line );
printf( "-A BLOCK1 -s %s/24 -j DROP\n", $ip );
$cmd = sprintf( "/sbin/iptables -I BLOCK1 -d %s/24 -j DROP", $ip );
system( $cmd );
}
}
?>
仮定:
1) BLOCK1 is a Chain already created.
2) BLOCK1 is a Chain that is run/called from the INPUT CHAIN
3) Periodically you will need to run "ipchains -S BLOCK1" and put output in /etc/sysconfig file.
4) You are familiar with PHP
5) You understand web log line items/fields and output.