📄 snort_stream5_tcp.c
字号:
/* $Id$ *//**************************************************************************** * * Copyright (C) 2005-2008 Sourcefire, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License Version 2 as * published by the Free Software Foundation. You may not use, modify or * distribute this program under any other version of the GNU General * Public License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ****************************************************************************//** * @file snort_stream5_tcp.c * @author Martin Roesch <roesch@sourcefire.com> * @author Steven Sturges <ssturges@sourcefire.com> * *//* * TODOs: * - midstream ssn pickup (done, SAS 10/14/2005) * - syn flood protection (done, SAS 9/27/2005) * * - review policy anomaly detection * + URG pointer (TODO) * + data on SYN (done, SAS 10/12/2005) * + data on FIN (done, SAS 10/12/2005) * + data after FIN (done, SAS 10/13/2005) * + window scaling/window size max (done, SAS 10/13/2005) * + PAWS, TCP Timestamps (done, SAS 10/12/2005) * * - session shutdown/Reset handling (done, SAS) * - flush policy for Window/Consumed * - limit on number of overlapping packets (done, SAS) *//* * Copyright (C) 2004-2008 Sourcefire, Inc. */#include "debug.h"#include "detect.h"#include "plugbase.h"#include "mstring.h"#include "sfxhash.h"#include "util.h"#include "sflsq.h"#include "bounds.h"#include "generators.h"#include "event_queue.h"#include "snort.h"#include "parser/IpAddrSet.h"#include "decode.h"#include "log.h"#include "stream5_common.h"#include "stream_api.h"#include "snort_stream5_session.h"#include "stream_ignore.h"#include "inline.h"#include <errno.h>#ifdef TARGET_BASED#include "sftarget_protocol_reference.h"#include "sftarget_hostentry.h"#endif#include "profiler.h"#include "ipv6_port.h"#include "sf_iph.h"#include "sp_preprocopt.h"#ifdef PERF_PROFILINGPreprocStats s5TcpPerfStats;PreprocStats s5TcpNewSessPerfStats;PreprocStats s5TcpStatePerfStats;PreprocStats s5TcpDataPerfStats;PreprocStats s5TcpInsertPerfStats;PreprocStats s5TcpFlushPerfStats;PreprocStats s5TcpBuildPacketPerfStats;PreprocStats s5TcpProcessRebuiltPerfStats;#endif/* M A C R O S **************************************************//* TCP flags */#define TH_FIN 0x01#define TH_SYN 0x02#define TH_RST 0x04#define TH_PUSH 0x08#define TH_ACK 0x10#define TH_URG 0x20#define TH_ECE 0x40#define TH_CWR 0x80#define TH_NORESERVED (TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG)/* TCP states */#define TCP_STATE_NONE 0#define TCP_STATE_LISTEN 1#define TCP_STATE_SYN_RCVD 2#define TCP_STATE_SYN_SENT 3#define TCP_STATE_ESTABLISHED 4#define TCP_STATE_CLOSE_WAIT 5#define TCP_STATE_LAST_ACK 6#define TCP_STATE_FIN_WAIT_1 7#define TCP_STATE_CLOSING 8#define TCP_STATE_FIN_WAIT_2 9#define TCP_STATE_TIME_WAIT 10#define TCP_STATE_CLOSED 11/* Macros to deal with sequence numbers - p810 TCP Illustrated vol 2 */#define SEQ_LT(a,b) ((int)((a) - (b)) < 0)#define SEQ_LEQ(a,b) ((int)((a) - (b)) <= 0)#define SEQ_GT(a,b) ((int)((a) - (b)) > 0)#define SEQ_GEQ(a,b) ((int)((a) - (b)) >= 0)#define SEQ_EQ(a,b) ((int)((a) - (b)) == 0)#define PAWS_WINDOW 60#define PAWS_24DAYS 2073600 /* 24 days in seconds *//* for state transition queuing */#define CHK_SEQ 0#define NO_CHK_SEQ 1#define S5_UNALIGNED 0#define S5_ALIGNED 1/* actions */#define ACTION_NOTHING 0x00000000#define ACTION_FLUSH_SENDER_STREAM 0x00000001#define ACTION_FLUSH_RECEIVER_STREAM 0x00000002#define ACTION_DROP_SESSION 0x00000004#define ACTION_ACK_SENDER_DATA 0x00000008#define ACTION_ACK_RECEIVER_DATA 0x00000010#define ACTION_SET_SSN 0x00000040#define ACTION_COMPLETE_TWH 0x00000080#define ACTION_RST 0x00000100#define ACTION_BAD_SEQ 0x00000200#define ACTION_BAD_PKT 0x00000400/* events */#define EVENT_SYN_ON_EST 0x00000001#define EVENT_DATA_ON_SYN 0x00000002#define EVENT_DATA_ON_CLOSED 0x00000004#define EVENT_BAD_TIMESTAMP 0x00000008#define EVENT_BAD_SEGMENT 0x00000010#define EVENT_WINDOW_TOO_LARGE 0x00000020#define EVENT_EXCESSIVE_TCP_OVERLAPS 0x00000040#define EVENT_DATA_AFTER_RESET 0x00000080#define EVENT_SESSION_HIJACK_CLIENT 0x00000100#define EVENT_SESSION_HIJACK_SERVER 0x00000200#define EVENT_DATA_WITHOUT_FLAGS 0x00000400#define TF_NONE 0x00#define TF_WSCALE 0x01#define TF_TSTAMP 0x02#define TF_TSTAMP_ZERO 0x04#define TF_MSS 0x08#define TF_FORCE_FLUSH 0x10#define TF_MISSING_PKT 0x20#define TF_PKT_MISSED 0x40#define TF_MISSING_PREV_PKT 0x80#define TF_ALL 0xFF#define STREAM_INSERT_OK 0#define STREAM_INSERT_ANOMALY 1#define STREAM_INSERT_TIMEOUT 2#define STREAM_INSERT_FAILED 3#define S5_DEFAULT_TCP_PACKET_MEMCAP 8388608 /* 8MB */#define S5_MIN_OVERLAP_LIMIT 0#define S5_MAX_OVERLAP_LIMIT 255#define REASSEMBLY_POLICY_FIRST 1#define REASSEMBLY_POLICY_LINUX 2#define REASSEMBLY_POLICY_BSD 3#define REASSEMBLY_POLICY_OLD_LINUX 4#define REASSEMBLY_POLICY_LAST 5#define REASSEMBLY_POLICY_WINDOWS 6#define REASSEMBLY_POLICY_SOLARIS 7#define REASSEMBLY_POLICY_HPUX11 8#define REASSEMBLY_POLICY_IRIX 9#define REASSEMBLY_POLICY_MACOS 10#define REASSEMBLY_POLICY_HPUX10 11#define REASSEMBLY_POLICY_VISTA 12#define REASSEMBLY_POLICY_WINDOWS2K3 13#define REASSEMBLY_POLICY_DEFAULT REASSEMBLY_POLICY_BSD#define STREAM_MAX_PACKET (IP_MAXPACKET - (ETHERNET_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN))#ifdef DEBUG_STREAM5#define STREAM5_DEBUG_WRAP(x) DEBUG_WRAP(x)#else#define STREAM5_DEBUG_WRAP(x)#endif/* client/server ip/port dereference */#define tcp_client_ip lwssn->client_ip#define tcp_client_port lwssn->client_port#define tcp_server_ip lwssn->server_ip#define tcp_server_port lwssn->server_port/* D A T A S T R U C T U R E S ***********************************/typedef struct _TcpDataBlock{ snort_ip sip; snort_ip dip; u_int32_t seq; u_int32_t ack; u_int32_t win; u_int32_t end_seq; u_int32_t ts;} TcpDataBlock;typedef struct _StateMgr{ u_int8_t state; u_int8_t state_queue; u_int8_t expected_flags; u_int32_t transition_seq; u_int32_t stq_get_seq;} StateMgr;#define RAND_FLUSH_POINTS 64#ifndef DYNAMIC_RANDOM_FLUSH_POINTStypedef struct _FlushPointList{ u_int8_t current; u_int8_t initialized; u_int32_t flush_range; u_int32_t flush_base; /* Set as value - range/2 */ /* flush_pt is split evently on either side of flush_value, within * the flush_range. flush_pt can be from: * (flush_value - flush_range/2) to (flush_value + flush_range/2) * * For example: * flush_value = 192 * flush_range = 128 * flush_pt will vary from 128 to 256 */ u_int32_t *flush_points;} FlushPointList;#endiftypedef struct _FlushMgr{ u_int32_t flush_pt; u_int8_t flush_policy;} FlushMgr;typedef struct _FlushConfig{ FlushMgr client; FlushMgr server; //SF_LIST *dynamic_policy;#ifdef TARGET_BASED u_int8_t configured;#endif} FlushConfig;typedef struct _CustomFlushPolicy{ FlushMgr client; FlushMgr server; IpAddrSet *bound_addrs;} CustomFlushPolicy;typedef struct _StreamSegment{ struct pcap_pkthdr pkth; u_int8_t *pktOrig; u_int8_t *pkt; u_int32_t cksum; u_int32_t caplen; u_int32_t ts; u_int8_t *data; u_int8_t *payload; u_int16_t size; u_int32_t seq; u_int16_t urg_offset; u_int8_t buffered; u_int8_t blocked; struct _StreamSegment *prev; struct _StreamSegment *next;#ifdef DEBUG int ordinal;#endif} StreamSegment;typedef struct _Stream5TcpPolicy{ u_int16_t policy; u_int16_t reassembly_policy; u_int32_t session_timeout; u_int8_t min_ttl; u_int32_t max_window; u_int32_t overlap_limit; u_int32_t hs_timeout; u_int16_t flags; IpAddrSet *bound_addrs; FlushConfig flush_config[MAX_PORTS];#ifdef TARGET_BASED FlushConfig flush_config_protocol[MAX_PROTOCOL_ORDINAL];#endif FlushPointList flush_point_list; u_int32_t max_queued_bytes; u_int32_t max_queued_segs;} Stream5TcpPolicy;typedef struct _StreamTracker{ u_int16_t os_policy; u_int16_t reassembly_policy; Stream5TcpPolicy *tcp_policy; u_int8_t mac_addr[6]; u_int8_t flags; /* bitmap flags (TF_xxx) */ StateMgr s_mgr; /* state tracking goodies */ FlushMgr flush_mgr; /* please flush twice, it's a long way to * the bitbucket... */ u_int32_t isn; /* initial sequence number */ u_int8_t ttl; /* base ttl at session startup */ u_int32_t ts_last_pkt; /* last packet timestamp we got */ /* tcp option handling */ u_int32_t ts_last; /* last timestamp (for PAWS) */ u_int16_t wscale; /* window scale setting */ u_int16_t mss; /* max segment size */ /* Local in the context of these variables means the local part * of the connection. For example, if this particular StreamTracker * was tracking the client side of a connection, the l_unackd value * would represent the client side of the connection's last unacked * sequence number */ u_int32_t l_unackd; /* local unack'd seq number */ u_int32_t l_nxt_seq; /* local next expected sequence */ u_int32_t l_window; /* local receive window */ u_int32_t r_nxt_ack; /* next expected ack from remote side */ u_int32_t r_win_base; /* remote side window base sequence number * (i.e. the last ack we got) */ u_int32_t gap_seq; /* sequence of next packet after a gap */ StreamSegment *seglist; /* first queued segment */ StreamSegment *seglist_tail; /* last queued segment */ u_int32_t seglist_base_seq; /* seq of first queued segment */ u_int32_t seg_count; /* number of current queued segments */ u_int32_t seg_bytes_total; /* total bytes currently queued */ u_int32_t seg_bytes_logical; /* logical bytes queued (total - overlaps) */ u_int32_t total_bytes_queued; /* total bytes queued (life of session) */ u_int32_t total_segs_queued; /* number of segments queued (life) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -