【参加讨论】5、使用 ngrep来处理tfn2k 攻击
根据使用dns来跟踪tfn2k驻留程序的原理,现在已经出现了称为ngrep的实用工具。经过修改的ngrep(参见附录)可以监听大约五种类型的tfn2k拒绝服务攻击(targa3, syn flood, udp flood, icmp flood 和 smurf),它还有一个循环使用的缓存用来记录dns和icmp请求。如果ngrep发觉有攻击行为的话,它会将其缓存中的内容打印出来并继续记录icmp回应请求。假如攻击者通过ping目标主机的手段来铆定攻击目标的话,在攻击过程中或之后记录icmp的回应请求是一种捕获粗心的攻击者的方法。由于攻击者还很可能使用其他的服务来核实其攻击的效果(例如web),所以对其他的标准服务也应当有尽量详细的日志记录。
还应当注意,ngrep采用的是监听网络的手段,因此,ngrep无法在交换式的环境中使用。但是经过修改的ngrep可以不必和你的dns在同一个网段中,但是他必须位于一个可以监听到所有dns请求的位置。经过修改的ngrep也不关心目标地址,您可以把它放置在dmz网段,使它能够检查横贯该网络的tfn2k攻击。从理论上讲,它也可以很好的检测出对外的tfn2k攻击。
运行 ngrep, 您将看到:
[root@lughnasad ngrep]# ./ngrep
ngrep with tfn detection modifications by wiretrip / www.wiretrip.net
watching dns server: 10.0.0.8
interface: eth0 (10.0.0.0/255.255.0.0)
从这里开始ngrep将监听tfn2k攻击,如果检测到攻击, ngrep将在屏幕上打印:
sun jan 9 17:30:01 2000
a tfn2k udp attack has been detected!
last (5000) dns requests:
《list of ips that made dns requests, up to dns_request_max length》
last (1000) icmp echo requests (pings):
《list of ips that made icmp echo requests, up to icmp_request_max length》
incoming realtime icmp echo requests (pings):
《all icmp echo requests since the attack was detected》
以上的列表并不是唯一的,可以对它进行调整让他不仅显示是谁请求,而且请求多少次,频率为多少等等。在icmp flood事件中,icmp回应请求的报告中将不包括做为tfn2k flood一部分的icmp包。ngrep还可以报告检测出来的除smurf之外的攻击类型(targa, udp, syn, icmp等)。混合式的攻击在缺省情况下表现为icmp攻击,除非你屏蔽了向内的icmp回应请求,这样它就表现为udp或syn攻击。这些攻击的结果都是基本类似的。
6、附录- ngrep.c with tfn2k detection
以下的代码在使用前应当更改一些参数。
#define dns_request_max 5000
#define icmp_request_max 1000
通知ngrep最大的请求跟踪数(在检测攻击之前)。传输较为繁忙的网站应当增加这一数值(网络流量较为繁忙的网站dns的请求数最好在10,000,而icmp请求为2000-3000)
#define flood_threshold 20
用在10秒中内有多少同一类型的攻击包来确认为真正的攻击。数目设计的越大,程序报受攻击的可能性就越小。假如您老是收到错误的警报,那么您应当增加一下这个数值。
#define dns_server_ip "10.0.0.8"
ngrep通过监视dns服务器的53端口的udp包来跟踪向内的dns请求(只有udp)。因此,ngrep需要知道您的dns服务器的ip地址。
我们的设备可能会有多个dns服务器,但我们认为对一台dns服务器的支持足以证明这项技术的能力。
#define ttl_threshold 150
tfn2k syn flood 攻击使用的 ttl值通常在200-255的范围内。估计到攻击者与目标主机之间不止50跳,因此我们可以只查找ttl时间高于150的包。假如您相信攻击者在50跳左右,那么您可以对ttl的限制进行一下更改。
编译更改过的 ngrep
编译和安装都非常简单。您仅需要使用以下之一来取代ngrep.c 文件。处于方便起见,我们可以详细说明。
这段代码只是在redhat 6.1 和mandrake 6.5 linux上测试过。
首先您需要在 http://www.packetfactory.net/ngrep/ 下载ngrep,我们测试的是1.35版。
然后在 ftp://ftp.ee.lbl.gov/libpcap.tar.z下载libpcap 我们使用的是 0.40版。
把文件放在临时文件夹里并解包,tar xvzf libpcap.tar.z
然后进行编译
cd libpcap-0.4; ./configure; make; make install; make install-incl
假如您遇到了困难,可以参见在libpcap-0.4目录里的readme或install文件。根据我们实验的经验,如果/usr/local/include 和/usr/local/include/net目录在linux系统中不存在的话,安装会失败。加入您在安装时遇到了pcap.h 或 bpf.h的错误时你可以运行
mkdir /usr/local/include; mkdir /usr/local/include/net然后重新运行'make install-incl'。然后我们需要编译ngrep (使用我们修改过的版本)。首先解包:
tar xvzf ngrep-1.35.tar.gz
然后进行配置:
cd ngrep; ./configure
然后把ngrep.c复制到ngrep目录里。你可以覆盖也可以备份原始的ngrep.c文件。在这里,您应当回顾在修改过的ngrep.c里的配置,至少您应当把dns_server_ip更改为您所使用的dns的地址。更改完毕后你就可以运行'make',这样就建立了ngrep应用程序。
modified ngrep.c source code
/* this code is available for download from http://www.wiretrip.net/na/ngrep.c */
/*
* $id: ngrep.c,v 1.35 1999/10/13 16:44:16 jpr5 exp $
*
*/
/* tfn detection code added by rain forest puppy / rfp@wiretrip.net
and night axis / na@wiretrip.net */
/********* tfn detection defines *******************************/
/* how many dns and icmp requests to track */
#define dns_request_max 5000
#define icmp_request_max 1000
/* flood threshold is matches per 10 seconds */
#define flood_threshold 20
/* ip of your dns server */
#define dns_server_ip "10.9.100.8"
/* tfn syn uses ttl between 200-255. assuming less than 50 hops,
flag stuff with ttl > ttl_threshold (other critera are used
as well) */
#define ttl_threshold 150
/**************************************************************/
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#ifdef linux
#include <getopt.h>
#endif
#if defined(bsd)