学习资料
[1]下载包内的doc/API.html,version:1.24
[2]http://blog.csdn.net/ningxiaowei2013/article/details/53035976
简介
libnids(Network Intrusion Detection System Library),网络入侵监测系统函数库。具有重组TCP数据段、处理IP分片包和监测TCP端 口扫描的功能。
所有的结构体和函数都在在“nids.h”,使用libnids的函数必须包含这个头文件和libnids.a的链接。
一个main函数的伪代码:
int main()
{
application private processing, not related to libnids
optional modification of libnids parameters
if (!nids_init() ) something's wrong, terminate;
registration of callback functions
nids_run();//or nids_next();
// not reached in normal situation
}
IP重组
为了使libnids能接收所有的IP数据包(包括分片包、畸形包等),程序员需要定义如下的回调函数:
void ip_frag_func(struct ip * a_packet, int len)
在调用nids_init()函数初始化后,使用nids的函数进行注册:
nids_register_ip_frag(ip_frag_func);
这样回调函数ip_frag_func会在适当的时候由libnids调用,参数a_packet指针将指向接收到的数据报,len是数据报长度。
类似地,如果仅接收目标主机会接受的数据包(如非碎片包、重组包或头部校验正确的数据包等),需要定义如下回调函数:
void ip_func(struct ip * a_packet, int len)
然后注册:
nids_register_ip(ip_func);
TCP数据流重组
要接收TCP流在交换的数据,必须定义如下回调函数:
void tcp_callback(struct tcp_stream * ns, void ** param)
结构体tcp_stream提供了一个TCP连接的所有信息。例如,它包含了客户端与服务器端的half_stream (named client
and server
)结构。下文会对该结构的字段进行解释。
tcp_stream结构有一个名为nids_state的字段。此字段的数值将决定tcp_callback的操作。
ns->nids_state==NIDS_JUST_EST
ns是一个刚刚建立的连接。tcp_callback可以据此决定是否对该连接的后续数据进行检查。所有的连接参数(ip,端口号等)都可获得。如需要检查,tcp_callback回调函数将通知libnids它希望接收哪些数据(如到客户端的数据、到服务器端的数据、到客户端的紧急数据或到服务器端的紧急数据等),然后返回。
ns->nids_state==NIDS_DATA
此时,新数据已经到达。结构体half_stream(tcp_stream的元素)包含数据buffer。
下面的nids_state状态:
- NIDS_CLOSE
- NIDS_RESET
- NIDS_TIMEOUT
意味着连接已经关闭。若有资源占用的话,tcp_callback需要释放资源。
ns->nids_state==NIDS_EXITING
此时,libnids已经退出。这是程序使用保存在half_stream buffer中数据的最后一次机会。当在抓包文件中读取而不是从网络流读取时,libnids可能永远不会close, reset, or timeout。如果程序有未处理的数据(例如,调用了nids_discard()),这个状态下允许程序去处理这些数据。
示例程序
/*http://libnids.sourceforge.net/printall.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include "nids.h"
#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
// struct tuple4 contains addresses and port numbers of the TCP connections
// the following auxiliary function produces a string looking like
// 10.0.0.1,1024,10.0.0.2,23
char * adres (struct tuple4 addr)
{
static char buf[256];
strcpy (buf, int_ntoa (addr.saddr));
sprintf (buf + strlen (buf), ",%i,", addr.source);
strcat (buf, int_ntoa (addr.daddr));
sprintf (buf + strlen (buf), ",%i", addr.dest);
return buf;
}
void tcp_callback (struct tcp_stream *a_tcp, void ** this_time_not_needed)
{
char buf[1024];
strcpy (buf, adres (a_tcp->addr)); // we put conn params into buf
if (a_tcp->nids_state == NIDS_JUST_EST)
{
// connection described by a_tcp is established
// here we decide, if we wish to follow this stream
// sample condition: if (a_tcp->addr.dest!=23) return;
// in this simple app we follow each stream, so..
a_tcp->client.collect++; // we want data received by a client
a_tcp->server.collect++; // and by a server, too
a_tcp->server.collect_urg++; // we want urgent data received by a
// server
#ifdef WE_WANT_URGENT_DATA_RECEIVED_BY_A_CLIENT
a_tcp->client.collect_urg++; // if we don't increase this value,
// we won't be notified of urgent data
// arrival
#endif
fprintf (stderr, "%s established\n", buf);
return;
}
if (a_tcp->nids_state == NIDS_CLOSE)
{
// connection has been closed normally
fprintf (stderr, "%s closing\n", buf);
return;
}
if (a_tcp->nids_state == NIDS_RESET)
{
// connection has been closed by RST
fprintf (stderr, "%s reset\n", buf);
return;
}
if (a_tcp->nids_state == NIDS_DATA)
{
// new data has arrived; gotta determine in what direction
// and if it's urgent or not
struct half_stream *hlf;
if (a_tcp->server.count_new_urg)
{