📄 snort_stream5_udp.c
字号:
tmp = tmpBucket->data; DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Creating new session tracker!\n");); tmp->ssn_time.tv_sec = p->pkth->ts.tv_sec; tmp->ssn_time.tv_usec = p->pkth->ts.tv_usec; lwssn->session_flags |= SSNFLAG_SEEN_SENDER; DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "adding UdpSession to lightweight session\n");); lwssn->proto_specific_data = tmpBucket; lwssn->protocol = GET_IPH_PROTO(p); lwssn->direction = FROM_SENDER; tmp->lwSsn = lwssn;#ifdef DEBUG_STREAM5 PrintUdpSession(tmp);#endif Stream5SetExpire(p, lwssn, s5UdpPolicy->session_timeout); s5stats.udp_sessions_created++; AddUDPSession(&sfPerf.sfBase); return 0;}/* * Main entry point for UDP */int Stream5ProcessUdp(Packet *p){ Stream5UdpPolicy *s5UdpPolicy = NULL; SessionKey skey; Stream5LWSession *lwssn = NULL; int policyIndex;#ifdef SUP_IP6// XXX-IPv6 Stream5ProcessUDP debugging#else DEBUG_WRAP( DebugMessage((DEBUG_STREAM|DEBUG_STREAM_STATE), "Got UDP Packet 0x%X:%d -> 0x%X:%d\n " "dsize: %lu\n" "active sessions: %lu\n", p->iph->ip_src.s_addr, p->sp, p->iph->ip_dst.s_addr, p->dp, p->dsize, sfxhash_count(udp_lws_cache->hashTable)); );#endif /* Find an Udp policy for this packet */ for (policyIndex = 0; policyIndex < numUdpPolicies; policyIndex++) { s5UdpPolicy = udpPolicyList[policyIndex]; /* * Does this policy handle packets to this IP address? */ if(IpAddrSetContains(s5UdpPolicy->bound_addrs, GET_DST_ADDR(p))) { DEBUG_WRAP(DebugMessage(DEBUG_STREAM, "[Stream5] Found udp policy in IpAddrSet\n");); break; } else { s5UdpPolicy = NULL; } } if (!s5UdpPolicy) { DEBUG_WRAP(DebugMessage(DEBUG_STREAM, "[Stream5] Could not find Udp Policy context " "for IP %s\n", inet_ntoa(GET_DST_ADDR(p)));); return 0; } if (isPacketFilterDiscard(p, s5UdpPolicy->flags & STREAM5_CONFIG_IGNORE_ANY) == PORT_MONITOR_PACKET_DISCARD) { //ignore the packet UpdateFilteredPacketStats(&sfPerf.sfBase, IPPROTO_UDP); return 0; } /* UDP Sessions required */ if ((lwssn = GetLWSession(udp_lws_cache, p, &skey)) == NULL) { /* Create a new session, mark SENDER seen */ lwssn = NewLWSession(udp_lws_cache, p, &skey); s5stats.total_udp_sessions++; } else { DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5: Retrieved existing session object.\n");); } if (!lwssn) { LogMessage("Stream5: Failed to retrieve session object. Out of memory?\n"); return -1; } p->ssnptr = lwssn; /* * Check if the session is expired. * Should be done before we do something with the packet... * ie, Insert a packet, or handle state change SYN, FIN, RST, etc. */ if ((lwssn->session_state & STREAM5_STATE_TIMEDOUT) || Stream5Expire(p, lwssn)) { lwssn->session_flags |= SSNFLAG_TIMEDOUT; /* Session is timed out */ DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5 UDP session timedout!\n");); /* Clean it up */ UdpSessionCleanup(lwssn); ProcessUdp(lwssn, p, s5UdpPolicy); } else { ProcessUdp(lwssn, p, s5UdpPolicy); DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Finished Stream5 UDP cleanly!\n" "---------------------------------------------------\n");); } MarkupPacketFlags(p, lwssn); Stream5SetExpire(p, lwssn, s5UdpPolicy->session_timeout); return 0;}static int ProcessUdp(Stream5LWSession *lwssn, Packet *p, Stream5UdpPolicy *s5UdpPolicy){ char ignore = 0; UdpSession *udpssn = NULL; DEBUG_WRAP( char *t = NULL; char *l = NULL; ); if (lwssn->proto_specific_data != NULL) udpssn = (UdpSession *)lwssn->proto_specific_data->data; if (lwssn->protocol != IPPROTO_UDP) { DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Lightweight session not UDP on UDP packet\n");); return ACTION_NOTHING; } if (lwssn->session_flags & (SSNFLAG_DROP_CLIENT|SSNFLAG_DROP_SERVER)) { /* figure out direction of this packet */ GetLWPacketDirection(p, lwssn); /* Got a packet on a session that was dropped (by a rule). */ /* TODO: Send reset to other side if not already done for inline mode */ //if (!(lwssn->session_flags & SSNFLAG_SERVER_RESET) //{ // Send Server Reset // lwssn->session_state |= STREAM5_STATE_SERVER_RESET; //} //if (!(lwssn->session_flags & SSNFLAG_CLIENT_RESET) //{ // Send Client Reset // lwssn->session_state |= STREAM5_STATE_CLIENT_RESET; //} /* Drop this packet */ if (((p->packet_flags & PKT_FROM_SERVER) && (lwssn->session_flags & SSNFLAG_DROP_SERVER)) || ((p->packet_flags & PKT_FROM_CLIENT) && (lwssn->session_flags & SSNFLAG_DROP_CLIENT))) { DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Blocking %s packet as session was blocked\n", p->packet_flags & PKT_FROM_SERVER ? "server" : "client");); DisableDetect(p); /* Still want to add this number of bytes to totals */ SetPreprocBit(p, PP_PERFMONITOR); InlineDrop(p); return ACTION_NOTHING; } } if (udpssn == NULL) { lwssn->direction = FROM_SENDER; IP_COPY_VALUE(lwssn->client_ip, GET_SRC_IP(p)); lwssn->client_port = p->udph->uh_sport; IP_COPY_VALUE(lwssn->server_ip, GET_DST_IP(p)); lwssn->server_port = p->udph->uh_dport; lwssn->session_state |= STREAM5_STATE_SENDER_SEEN; if (NewUdpSession(p, lwssn, s5UdpPolicy) == -1) return ACTION_NOTHING; udpssn = (UdpSession *)lwssn->proto_specific_data->data; } /* figure out direction of this packet */ GetLWPacketDirection(p, lwssn); if (((p->packet_flags & PKT_FROM_SERVER) && (lwssn->ignore_direction & SSN_DIR_CLIENT)) || ((p->packet_flags & PKT_FROM_CLIENT) && (lwssn->ignore_direction & SSN_DIR_SERVER))) { Stream5DisableInspection(lwssn, p); DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5 Ignoring packet from %d. " "Session marked as ignore\n", p->packet_flags & PKT_FROM_CLIENT? "sender" : "responder");); return ACTION_NOTHING; } /* Check if the session is to be ignored */ ignore = CheckIgnoreChannel(p); if (ignore) { /* Set the directions to ignore... */ lwssn->ignore_direction = ignore; DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5: Ignoring packet from %d. " "Marking session marked as ignore.\n", p->packet_flags & PKT_FROM_CLIENT? "sender" : "responder");); Stream5DisableInspection(lwssn, p); return ACTION_NOTHING; } /* if both seen, mark established */ if(p->packet_flags & PKT_FROM_SERVER) { DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5: Updating on packet from responder\n");); lwssn->session_flags |= SSNFLAG_SEEN_RESPONDER; DEBUG_WRAP( t = "Responder"; l = "Sender"); } else { DEBUG_WRAP(DebugMessage(DEBUG_STREAM_STATE, "Stream5: Updating on packet from client\n");); /* if we got here we had to see the SYN already... */ lwssn->session_flags |= SSNFLAG_SEEN_SENDER; DEBUG_WRAP( t = "Sender"; l = "Responder"); } if (!(lwssn->session_flags & SSNFLAG_ESTABLISHED)) { if ((lwssn->session_flags & SSNFLAG_SEEN_SENDER) && (lwssn->session_flags & SSNFLAG_SEEN_RESPONDER)) { lwssn->session_flags |= SSNFLAG_ESTABLISHED; } } return ACTION_NOTHING;}void UdpUpdateDirection(Stream5LWSession *ssn, char dir, snort_ip_p ip, u_int16_t port){ UdpSession *udpssn = (UdpSession *)ssn->proto_specific_data->data; snort_ip tmpIp; u_int16_t tmpPort;#ifdef SUP_IP6 if (IP_EQUALITY(&udpssn->udp_sender_ip, ip) && (udpssn->udp_sender_port == port)) { if ((dir == SSN_DIR_SENDER) && (ssn->direction == SSN_DIR_SENDER)) { /* Direction already set as SENDER */ return; } } else if (IP_EQUALITY(&udpssn->udp_responder_ip, ip) && (udpssn->udp_responder_port == port)) { if ((dir == SSN_DIR_RESPONDER) && (ssn->direction == SSN_DIR_RESPONDER)) { /* Direction already set as RESPONDER */ return; } }#else if (IP_EQUALITY(udpssn->udp_sender_ip, ip) && (udpssn->udp_sender_port == port)) { if ((dir == SSN_DIR_SENDER) && (ssn->direction == SSN_DIR_SENDER)) { /* Direction already set as SENDER */ return; } } else if (IP_EQUALITY(udpssn->udp_responder_ip, ip) && (udpssn->udp_responder_port == port)) { if ((dir == SSN_DIR_RESPONDER) && (ssn->direction == SSN_DIR_RESPONDER)) { /* Direction already set as RESPONDER */ return; } }#endif /* Swap them -- leave ssn->direction the same */ tmpIp = udpssn->udp_sender_ip; tmpPort = udpssn->udp_sender_port; udpssn->udp_sender_ip = udpssn->udp_responder_ip; udpssn->udp_sender_port = udpssn->udp_responder_port; udpssn->udp_responder_ip = tmpIp; udpssn->udp_responder_port = tmpPort;}void s5UdpSetPortFilterStatus( unsigned short port, int status ){ udp_ports[port] |= status;}int s5UdpGetPortFilterStatus( unsigned short port ){ return udp_ports[port];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -