📄 snort_stream5_udp.c
字号:
/**************************************************************************** * * 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. * ****************************************************************************/ #include "debug.h"#include "detect.h"#include "plugbase.h"#include "mstring.h"#include "sfxhash.h"#include "util.h"#include "decode.h"#include "stream5_common.h"#include "stream_api.h"#include "snort_stream5_session.h"#include "stream_ignore.h"#include "plugin_enum.h"#include "rules.h"#include "snort.h"#include "inline.h"#include "portscan.h" /* To know when to create sessions for all UDP */#include "dynamic-plugins/sp_dynamic.h"#include "profiler.h"#ifdef PERF_PROFILINGPreprocStats s5UdpPerfStats;#endif/* M A C R O S **************************************************//* actions */#define ACTION_NOTHING 0x00000000/* sender/responder ip/port dereference */#define udp_sender_ip lwSsn->client_ip#define udp_sender_port lwSsn->client_port#define udp_responder_ip lwSsn->server_ip#define udp_responder_port lwSsn->server_port/* D A T A S T R U C T U R E S ***********************************/typedef struct _UdpSession{ Stream5LWSession *lwSsn; struct timeval ssn_time; //u_int8_t c_ttl; //u_int8_t s_ttl;} UdpSession;typedef struct _Stream5UdpPolicy{ u_int32_t session_timeout; u_int16_t flags; IpAddrSet *bound_addrs;} Stream5UdpPolicy;static u_int8_t udp_ports[MAX_PORTS+1];/* G L O B A L S **************************************************/static Stream5SessionCache *udp_lws_cache;static Stream5UdpPolicy **udpPolicyList = NULL; /* List of Policies configured */static u_int8_t numUdpPolicies = 0;static MemPool udp_session_mempool;/* P R O T O T Y P E S ********************************************/static void Stream5ParseUdpArgs(char *, Stream5UdpPolicy *);static void Stream5PrintUdpConfig(Stream5UdpPolicy *);void UdpSessionCleanup(Stream5LWSession *lwssn);static int ProcessUdp(Stream5LWSession *, Packet *, Stream5UdpPolicy *);void Stream5InitUdp(void){ /* Now UDP */ if((udp_lws_cache == NULL) && s5_global_config.track_udp_sessions) { udp_lws_cache = InitLWSessionCache(s5_global_config.max_udp_sessions, 30, 5, 0, &UdpSessionCleanup); if(!udp_lws_cache) { FatalError("Unable to init stream5 UDP session cache, no UDP " "stream inspection!\n"); } mempool_init(&udp_session_mempool, s5_global_config.max_udp_sessions, sizeof(UdpSession)); }}void Stream5UdpPolicyInit(char *args){ Stream5UdpPolicy *s5UdpPolicy; s5UdpPolicy = (Stream5UdpPolicy *) SnortAlloc(sizeof(Stream5UdpPolicy)); s5UdpPolicy->bound_addrs = (IpAddrSet *) SnortAlloc(sizeof(IpAddrSet)); Stream5ParseUdpArgs(args, s5UdpPolicy); /* Now add this context to the internal list */ if (udpPolicyList == NULL) { numUdpPolicies = 1; udpPolicyList = (Stream5UdpPolicy **)SnortAlloc(sizeof (Stream5UdpPolicy *) * numUdpPolicies); } else { Stream5UdpPolicy **tmpPolicyList = (Stream5UdpPolicy **)SnortAlloc(sizeof (Stream5UdpPolicy *) * (++numUdpPolicies)); memcpy(tmpPolicyList, udpPolicyList, sizeof(Stream5UdpPolicy *) * (numUdpPolicies-1)); free(udpPolicyList); udpPolicyList = tmpPolicyList; } udpPolicyList[numUdpPolicies-1] = s5UdpPolicy; Stream5PrintUdpConfig(s5UdpPolicy); return;}static void Stream5ParseUdpArgs(char *args, Stream5UdpPolicy *s5UdpPolicy){ char **toks; int num_toks; int i; char *index; char **stoks = NULL; int s_toks; char *endPtr = NULL; s5UdpPolicy->session_timeout = S5_DEFAULT_SSN_TIMEOUT; s5UdpPolicy->flags = 0; if(args != NULL && strlen(args) != 0) { toks = mSplit(args, ",", 6, &num_toks, 0); i=0; while(i < num_toks) { index = toks[i]; while(isspace((int)*index)) index++; stoks = mSplit(index, " ", 3, &s_toks, 0); if (s_toks == 0) { FatalError("%s(%d) => Missing parameter in Stream5 UDP config.\n", file_name, file_line); } if(!strcasecmp(stoks[0], "timeout")) { if(stoks[1]) { s5UdpPolicy->session_timeout = strtoul(stoks[1], &endPtr, 10); } if (!stoks[1] || (endPtr == &stoks[1][0])) { FatalError("%s(%d) => Invalid timeout in config file. Integer parameter required.\n", file_name, file_line); } if ((s5UdpPolicy->session_timeout > S5_MAX_SSN_TIMEOUT) || (s5UdpPolicy->session_timeout < S5_MIN_SSN_TIMEOUT)) { FatalError("%s(%d) => Invalid timeout in config file. " "Must be between %d and %d\n", file_name, file_line, S5_MIN_SSN_TIMEOUT, S5_MAX_SSN_TIMEOUT); } if (s_toks > 2) { FatalError("%s(%d) => Invalid Stream5 UDP Policy option. Missing comma?\n", file_name, file_line); } } else if (!strcasecmp(stoks[0], "ignore_any_rules")) { s5UdpPolicy->flags |= STREAM5_CONFIG_IGNORE_ANY; if (s_toks > 1) { FatalError("%s(%d) => Invalid Stream5 UDP Policy option. Missing comma?\n", file_name, file_line); } } else { FatalError("%s(%d) => Invalid Stream5 UDP Policy option\n", file_name, file_line); } mSplitFree(&stoks, s_toks); i++; } mSplitFree(&toks, num_toks); if(s5UdpPolicy->bound_addrs == NULL) { /* allocate and initializes the * IpAddrSet at the same time * set to "any" */ s5UdpPolicy->bound_addrs = (IpAddrSet *) SnortAlloc(sizeof(IpAddrSet)); } } return;}static void Stream5PrintUdpConfig(Stream5UdpPolicy *s5UdpPolicy){ LogMessage("Stream5 UDP Policy config:\n"); LogMessage(" Timeout: %d seconds\n", s5UdpPolicy->session_timeout); if (s5UdpPolicy->flags) { LogMessage(" Options:\n"); if (s5UdpPolicy->flags & STREAM5_CONFIG_IGNORE_ANY) { LogMessage(" Ignore Any -> Any Rules: YES\n"); } } //IpAddrSetPrint(" Bound Addresses:", s5UdpPolicy->bound_addrs);}int Stream5VerifyUdpConfig(void){ if (!udp_lws_cache) return -1; if (numUdpPolicies < 1) return -1; /* Post-process UDP rules to establish UDP ports to inspect. */ setPortFilterList(udp_ports, IPPROTO_UDP, (udpPolicyList[0]->flags & STREAM5_CONFIG_IGNORE_ANY)); //printf ("UDP Ports with Inspection/Monitoring\n"); //s5PrintPortFilter(udp_ports); return 0;}#ifdef DEBUG_STREAM5static void PrintUdpSession(UdpSession *us){ LogMessage("UdpSession:\n"); LogMessage(" ssn_time: %lu\n", us->ssn_time.tv_sec); LogMessage(" sender IP: 0x%08X\n", us->udp_sender_ip); LogMessage(" responder IP: 0x%08X\n", us->udp_responder_ip); LogMessage(" sender port: %d\n", us->udp_sender_port); LogMessage(" responder port: %d\n", us->udp_responder_port); LogMessage(" flags: 0x%X\n", us->lwSsn->session_flags);}#endifStream5LWSession *GetLWUdpSession(SessionKey *key){ return GetLWSessionFromKey(udp_lws_cache, key);}void UdpSessionCleanup(Stream5LWSession *lwssn){ UdpSession *udpssn = NULL; if (lwssn->proto_specific_data) udpssn = (UdpSession *)lwssn->proto_specific_data->data; if (!udpssn) { /* Huh? */ return; } /* Cleanup the proto specific data */ mempool_free(&udp_session_mempool, lwssn->proto_specific_data); lwssn->proto_specific_data = NULL; lwssn->session_state = STREAM5_STATE_NONE; lwssn->session_flags = SSNFLAG_NONE; lwssn->expire_time = 0; lwssn->ignore_direction = 0; s5stats.udp_sessions_released++; RemoveUDPSession(&sfPerf.sfBase);}void Stream5ResetUdp(void){ PurgeLWSessionCache(udp_lws_cache); mempool_clean(&udp_session_mempool);}void Stream5CleanUdp(void){ /* Clean up hash table -- delete all sessions */ DeleteLWSessionCache(udp_lws_cache); udp_lws_cache = NULL; mempool_destroy(&udp_session_mempool);}static int NewUdpSession(Packet *p, Stream5LWSession *lwssn, Stream5UdpPolicy *s5UdpPolicy){ UdpSession *tmp; MemBucket *tmpBucket; /****************************************************************** * create new sessions *****************************************************************/ tmpBucket = mempool_alloc(&udp_session_mempool); if (tmpBucket == NULL) return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -