|
NAMElibnids - network intrusion detection system E-box librarySYNOPSIS#include <nids.h> extern struct nids_prm nids_params; extern char nids_errbuf[]; int nids_init(void); void nids_register_ip_frag(void (*ip_frag_func)(struct ip *pkt, int len)); void nids_unregister_ip_frag(void (*ip_frag_func)(struct ip *pkt, int len)); void nids_register_ip(void (*ip_func)(struct ip *pkt, int len)); void nids_unregister_ip(void (*ip_func)(struct ip *pkt, int len)); void nids_register_udp(void (*udp_func)(struct tuple4 *addr, u_char *data, int len, struct ip *pkt)); void nids_unregister_udp(void (*udp_func)(struct tuple4 *addr, u_char *data, int len, struct ip *pkt)); void nids_register_tcp(void (*tcp_func)(struct tcp_stream *ts, void **param)); void nids_unregister_tcp(void (*tcp_func)(struct tcp_stream *ts, void **param)); void nids_killtcp(struct tcp_stream *ts); void nids_discard(struct tcp_stream *ts, int numbytes); void nids_run(void); int nids_dispatch(int cnt); int nids_next(void); int nids_getfd(void); int nids_register_chksum_ctl(struct nids_chksum_ctl *, int); void nids_pcap_handler(u_char *par, struct pcap_pkthdr *hdr, u_char *data); struct tcp_stream * nids_find_tcp_stream(struct tuple4 *addr); DESCRIPTIONlibnids provides the functionality of a network intrusion detection system (NIDS) E-box component. It currently performs:1. IP defragmentation 2. TCP stream reassembly 3. TCP port scan detection libnids performs TCP/IP reassembly in exactly the same way as Linux 2.0.36 kernels, and correctly handles all of the attacks implemented in fragrouter(8) (plus many other attacks as well). ROUTINESnids_init() initializes the application for sniffing, based on the values set in the global variable nids_params, declared as follows:struct nids_prm { int n_tcp_streams; int n_hosts; char *device; char *filename; int sk_buff_size; int dev_addon; void (*syslog)(int type, int err, struct ip *iph, void *data); int syslog_level; int scan_num_hosts; int scan_num_ports; int scan_delay; void (*no_mem)(void); int (*ip_filter)(struct ip *iph); char *pcap_filter; int promisc; int one_loop_less; int pcap_timeout; int multiproc; int queue_limit; int tcp_workarounds; pcap_t *pcap_desc; } nids_params; The members of this structure are:
Returns 1 on success, 0 on failure (in which case nids_errbuf contains an appropriate error message). nids_register_ip_frag() registers a user-defined callback function to process all incoming IP packets (including IP fragments, packets with invalid checksums, etc.). nids_unregister_ip_frag() unregisters a user-defined callback function to process all incoming IP packets. nids_register_ip() registers a user-defined callback function to process IP packets validated and reassembled by libnids. nids_unregister_ip() unregisters a user-defined callback function to process IP packets. nids_register_udp() registers a user-defined callback function to process UDP packets validated and reassembled by libnids. nids_unregister_udp() unregisters a user-defined callback function to process UDP packets. nids_register_tcp() registers a user-defined callback function to process TCP streams validated and reassembled by libnids. The tcp_stream structure is defined as follows: struct tcp_stream { struct tuple4 { u_short source; u_short dest; u_int saddr; u_int daddr; } addr; char nids_state; struct half_stream { char state; char collect; char collect_urg; char *data; u_char urgdata; int count; int offset; int count_new; char count_new_urg; ... } client; struct half_stream server; ... void *user; }; The members of the tuple4 structure identify a unique TCP connection:
The members of the half_stream structure describe each half of a TCP connection (client and server):
The value of the nids_state field provides information about the state of the TCP connection, to be used by the TCP callback function:
The param pointer passed by libnids as argument to the TCP callback function may be set to save a pointer to user-defined connection-specific data to pass to subsequent invocations of the TCP callback function (ex. the current working directory for an FTP control connection, etc.). The user pointer in the tcp_stream structure has the same purpose except it is global to the stream, whereas the param pointer is different from one callback function to the other even though they were called for the same stream. nids_unregister_tcp() unregisters a user-defined callback function to process TCP streams. nids_killtcp() tears down the specified TCP connection with symmetric RST packets between client and server. nids_discard() may be called from the TCP callback function to specify the number of bytes to discard from the beginning of the data buffer (updating the offset value accordingly) after the TCP callback function exits. Otherwise, the new data (totalling count_new bytes) will be discarded by default. nids_run() starts the packet-driven application, reading packets in an endless loop, and invoking registered callback functions to handle new data as it arrives. This function does not return. nids_dispatch() attempts to process cnt packets before returning, with a cnt of -1 understood as all packets available in one pcap buffer, or all packets in a file when reading offline. On success, returns the count of packets processed, which may be zero upon EOF (offline read) or upon hitting pcap_timeout (if supported by your platform). On failure, returns -1, putting an appropriate error message in nids_errbuf. nids_next() process the next available packet before returning. Returns 1 on success, 0 if no packet was processed, setting nids_effbuf appropriately if an error prevented packet processing. nids_getfd() may be used by an application sleeping in select(2) to snoop for a socket file descriptor present in the read fd_set. Returns the file descriptor on success, -1 on failure (in which case nids_errbuf contains an appropriate error message). nids_register_chksum_ctl() takes as arguments an array of struct nids_chksum_ctl elements and the number of elements in the array. A nids_chksum_ctl element is defined as follows: struct nids_chksum_ctl { u_int netaddr; u_int mask; u_int action; /* private members */ }; Internal checksumming functions will first check elements of this array one by one, and if the source ip SRCIP of the current packet satisfies condition (SRCIP&chksum_ctl_array[i].mask)==chksum_ctl_array[i].netaddr then if the action field is NIDS_DO_CHKSUM, the packet will be checksummed; if the action field is NIDS_DONT_CHKSUM, the packet will not be checksummed. If the packet matches none of the array elements, the default action is to perform checksumming. nids_pcap_handler() may be used by an application already running a capture with libpcap, in order to pass frames to libnids interactively (frame per frame) instead of having libnids itself do the capture. nids_find_tcp_stream() returns a pointer to the tcp_stream structure corresponding to the tuple passed as argument if libnids knows about this TCP connection already, otherwise it returns NULL. nids_free_tcp_stream() removes the given tcp_stream from the list of streams tracked by libnids. Warning: its usage can result in crashes! See comments in the API.html file. SEE ALSOpcap(3), libnet(3), fragrouter(8)AUTHORRafal Wojtczuk <nergal@icm.edu.pl>Manpage by Dug Song <dugsong@monkey.org>, minor updates by Michael Pomraning <mjp@pilcrow.madison.wi.us> Visit the GSP FreeBSD Man Page Interface. |