static void per_packet(libtrace_packet_t *packet) { struct sockaddr_storage addr; struct sockaddr *addr_ptr; uint16_t port; uint8_t *mac; /* Get the source mac */ mac = trace_get_source_mac(packet); /* If this packet does not contain a MAC address, print "NULL" */ if (mac == NULL) printf("NULL "); else print_mac(mac); /* Get the source IP address */ /* Note that we pass a casted sockaddr_storage into this function. This * is because we do not know if the IP address we get back will be a * v4 or v6 address. v6 addresses are much larger than v4 addresses and * will not fit within a sockaddr_in structure used for storing IPv4 * addresses, leading to memory corruption and segmentation faults. * * The safest way to avoid this problem is to use a sockaddr_storage * which is guaranteed to be large enough to contain any known address * format. */ addr_ptr = trace_get_source_address(packet, (struct sockaddr *)&addr); /* No IP address? Print "NULL" instead */ if (addr_ptr == NULL) printf("NULL "); else print_ip(addr_ptr); /* Get the source port */ port = trace_get_source_port(packet); /* If the port is zero, libtrace has told us that there is no * legitimate port number present in the packet */ if (port == 0) printf("NULL\n"); else /* Port numbers are simply 16 bit values so we don't need to * do anything special to print them. trace_get_source_port() * even converts it into host byte order for us */ printf("%u\n", port); }
int main(int argc, char *argv[]) { int psize = 0; int error = 0; int count = 0; int level = 0; int expected = 100; libtrace_t *trace; libtrace_out_t *outtrace; libtrace_packet_t *packet; trace = trace_create(lookup_uri("pcap")); iferr(trace); outtrace = trace_create_output(lookup_out_uri("pcap")); iferrout(outtrace); level=0; trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level); iferrout(outtrace); trace_start(trace); iferr(trace); trace_start_output(outtrace); iferrout(outtrace); packet=trace_create_packet(); for (;;) { if ((psize = trace_read_packet(trace, packet)) <0) { error = 1; break; } if (psize == 0) { error = 0; break; } count ++; /* Force promotion */ if (trace_get_source_port(packet)==80) { trace_set_direction(packet,TRACE_DIR_OUTGOING); assert(trace_get_direction(packet)==TRACE_DIR_OUTGOING); assert(trace_get_source_port(packet)==80); } else { trace_set_direction(packet,TRACE_DIR_INCOMING); assert(trace_get_direction(packet)==TRACE_DIR_INCOMING); } /* And then force demotion */ trace_write_packet(outtrace,packet); iferrout(outtrace); if (count>100) break; } trace_destroy_packet(packet); if (error == 0) { if (count != expected) { printf("failure: %d packets expected, %d seen\n",expected,count); error = 1; } } else { iferr(trace); } trace_destroy(trace); trace_destroy_output(outtrace); return error; }
/* Process a trace, counting packets that match filter(s) */ static void run_trace(char *uri) { struct libtrace_packet_t *packet = trace_create_packet(); int i; uint64_t count = 0; uint64_t bytes = 0; uint64_t packets; fprintf(stderr,"%s:\n",uri); trace = trace_create(uri); if (trace_is_err(trace)) { trace_perror(trace,"Failed to create trace"); return; } if (trace_start(trace)==-1) { trace_perror(trace,"Failed to start trace"); return; } for (;;) { int psize; int wlen; int match = 0; if ((psize = trace_read_packet(trace, packet)) <1) { break; } if (done) break; wlen = trace_get_wire_length(packet); for(i=0; i<filter_count; ++i) { if (filters[i].filter == NULL) continue; if(trace_apply_filter(filters[i].filter,packet) > 0) { ++filters[i].count; filters[i].bytes+=wlen; match = 1; } if (trace_is_err(trace)) { trace_perror(trace, "trace_apply_filter"); fprintf(stderr, "Removing filter from filterlist\n"); filters[i].filter = NULL; } } if(match == 1 || filter_count == 0) { libtrace_ip_t *ip_hdr = trace_get_ip(packet); if(ip_hdr != NULL) { char src_ip[INET_ADDRSTRLEN]; char dst_ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &ip_hdr->ip_src, &src_ip[0], INET_ADDRSTRLEN); inet_ntop(AF_INET, &ip_hdr->ip_dst, &dst_ip[0], INET_ADDRSTRLEN); printf("%f\t%s\t%s\t%"PRIu16"\t%"PRIu16"\t" "%"PRIu8"\t%"PRIu16"\t%"PRIu16"\n", trace_get_seconds(packet), src_ip, dst_ip, trace_get_source_port(packet), trace_get_destination_port(packet), ip_hdr->ip_p, ntohs(ip_hdr->ip_id), ntohs(ip_hdr->ip_len) ); } } ++count; bytes+=wlen; } fprintf(stderr, "%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%"); for(i=0; i<filter_count; ++i) { fprintf(stderr, "%30s:\t%12"PRIu64"\t%12"PRIu64"\t%7.03f\n", filters[i].expr,filters[i].count, filters[i].bytes,filters[i].count*100.0/count); filters[i].bytes=0; filters[i].count=0; } packets=trace_get_received_packets(trace); if (packets!=UINT64_MAX) fprintf(stderr,"%30s:\t%12" PRIu64"\n", "Input packets", packets); packets=trace_get_filtered_packets(trace); if (packets!=UINT64_MAX) fprintf(stderr,"%30s:\t%12" PRIu64"\n", "Filtered packets", packets); packets=trace_get_dropped_packets(trace); if (packets!=UINT64_MAX) fprintf(stderr,"%30s:\t%12" PRIu64"\n", "Dropped packets",packets); packets=trace_get_accepted_packets(trace); if (packets!=UINT64_MAX) fprintf(stderr,"%30s:\t%12" PRIu64 "\n", "Accepted Packets",packets); fprintf(stderr, "%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes); totcount+=count; totbytes+=bytes; if (trace_is_err(trace)) trace_perror(trace,"%s",uri); trace_destroy(trace); }
/** Implements the process_packet function of the plugin API */ int corsaro_dos_process_packet(corsaro_t *corsaro, corsaro_packet_t *packet) { libtrace_packet_t *ltpacket = LT_PKT(packet); void *temp = NULL; uint8_t proto; uint32_t remaining; libtrace_ip_t *ip_hdr = NULL; libtrace_icmp_t *icmp_hdr = NULL; libtrace_ip_t *inner_ip_hdr = NULL; /* borrowed from libtrace's protocols.h (used by trace_get_*_port) */ struct ports_t { uint16_t src; /**< Source port */ uint16_t dst; /**< Destination port */ }; uint16_t attacker_port = 0; uint16_t target_port = 0; attack_vector_t findme; int khret; khiter_t khiter; attack_vector_t *vector = NULL; uint8_t *pkt_buf = NULL; libtrace_linktype_t linktype; struct timeval tv; if((packet->state.flags & CORSARO_PACKET_STATE_FLAG_BACKSCATTER) == 0) { /* not a backscatter packet */ return 0; } /* backscatter packet, lets find the flow */ /* check for ipv4 */ /* 10/19/12 ak replaced much more verbose code to get header with this */ if((ip_hdr = trace_get_ip(ltpacket)) == NULL) { /* non-ipv4 packet */ return 0; } /* get the transport header */ if((temp = trace_get_transport(ltpacket, &proto, &remaining)) == NULL) { /* not enough payload */ return 0; } findme.target_ip = 0; if(ip_hdr->ip_p == TRACE_IPPROTO_ICMP && remaining >= 2) { icmp_hdr = (libtrace_icmp_t *)temp; if((icmp_hdr->type == 3 || icmp_hdr->type == 4 || icmp_hdr->type == 5 || icmp_hdr->type == 11 || icmp_hdr->type == 12) && ((temp = trace_get_payload_from_icmp(icmp_hdr, &remaining)) != NULL && remaining >= 20 && (inner_ip_hdr = (libtrace_ip_t *)temp) && inner_ip_hdr->ip_v == 4)) { /* icmp error message */ if(inner_ip_hdr->ip_src.s_addr != ip_hdr->ip_dst.s_addr) { STATE(corsaro)->number_mismatched_packets++; } findme.target_ip = ntohl(inner_ip_hdr->ip_dst.s_addr); /* just extract the first four bytes of payload as ports */ if((temp = trace_get_payload_from_ip(inner_ip_hdr, NULL, &remaining)) != NULL && remaining >= 4) { attacker_port = ntohs(((struct ports_t *)temp)->src); target_port = ntohs(((struct ports_t *)temp)->dst); } } else { findme.target_ip = ntohl(ip_hdr->ip_src.s_addr); attacker_port = ntohs(icmp_hdr->code); target_port = ntohs(icmp_hdr->type); } } else if((ip_hdr->ip_p == TRACE_IPPROTO_TCP || ip_hdr->ip_p == TRACE_IPPROTO_UDP) && remaining >= 4) { findme.target_ip = ntohl(ip_hdr->ip_src.s_addr); attacker_port = trace_get_destination_port(ltpacket); target_port = trace_get_source_port(ltpacket); } if(findme.target_ip == 0) { /* the packet is none of ICMP, TCP or UDP */ return 0; } tv = trace_get_timeval(ltpacket); /* is this vector in the hash? */ assert(STATE(corsaro)->attack_hash != NULL); if((khiter = kh_get(av, STATE(corsaro)->attack_hash, &findme)) != kh_end(STATE(corsaro)->attack_hash)) { /* the vector is in the hash */ vector = kh_key(STATE(corsaro)->attack_hash, khiter); if(attack_vector_is_expired(vector, tv.tv_sec) != 0) { kh_del(av, STATE(corsaro)->attack_hash, khiter); attack_vector_free(vector); vector = NULL; } } if(vector == NULL) { /* create a new vector and fill it */ if((vector = attack_vector_init(corsaro)) == NULL) { corsaro_log(__func__, corsaro, "failed to create new attack vector"); return -1; } /* i think this may be buggy. do it the safe way for now vector->initial_packet = corsaro_mincopy_packet(packet); */ vector->initial_packet_len = trace_get_capture_length(ltpacket); if((vector->initial_packet = malloc(vector->initial_packet_len)) == NULL) { corsaro_log(__func__, corsaro, "could not malloc initial packet"); return -1; } if((pkt_buf = trace_get_packet_buffer(ltpacket, &linktype, NULL)) == NULL) { corsaro_log(__func__, corsaro, "could not get packet buffer"); return -1; } memcpy(vector->initial_packet, pkt_buf, vector->initial_packet_len); vector->attacker_ip = ntohl(ip_hdr->ip_dst.s_addr); vector->responder_ip = ntohl(ip_hdr->ip_src.s_addr); vector->target_ip = findme.target_ip; vector->start_time = tv; vector->ppm_window.window_start = tv.tv_sec; /* add to the hash */ khiter = kh_put(av, STATE(corsaro)->attack_hash, vector, &khret); } assert(vector != NULL); vector->packet_cnt++; vector->interval_packet_cnt++; vector->byte_cnt += ntohs(ip_hdr->ip_len); vector->interval_byte_cnt += ntohs(ip_hdr->ip_len); vector->latest_time = tv; /* update the pps window */ attack_vector_update_ppm_window(vector, tv); /* add the attacker ip to the hash */ kh_put(32xx, vector->attack_ip_hash, ntohl(ip_hdr->ip_dst.s_addr), &khret); /* add the ports to the hashes */ kh_put(32xx, vector->attack_port_hash, attacker_port, &khret); kh_put(32xx, vector->target_port_hash, target_port, &khret); return 0; }
static int per_packet(libtrace_packet_t * pkt, FILE * fp_write, struct time_adjust_record * time_adjust_flow) { // Create a new packet which is a copy of the old packet. //libtrace_packet_t *copy_pkt = trace_copy_packet(pkt); libtrace_ip_t *ip = trace_get_ip(pkt); libtrace_ip6_t *ip6 = trace_get_ip6(pkt); struct sockaddr_storage src_addr; struct sockaddr_storage dest_addr; struct sockaddr *src_addr_ptr; struct sockaddr *dest_addr_ptr; /* L3 data */ void *l3; uint16_t ethertype; /* Transport data */ void *transport; uint8_t proto; /* Payload data */ uint32_t remaining; struct timeval ts; //printf("In per_packet line:%d\n", __LINE__); l3 = trace_get_layer3(pkt,ðertype,&remaining); if (!l3) { /* Probable ARP or something */ return; } /* Get the UDP/TCP/ICMP header from the IPv4/IPv6 packet */ /*switch (ethertype) { case 0x0800: transport = trace_get_payload_from_ip( (libtrace_ip_t*)l3, &proto, &remaining); if (!transport) return; //++v4; break; case 0x86DD: transport = trace_get_payload_from_ip6( (libtrace_ip6_t*)l3, &proto, &remaining); if (!transport) return; //++v6; break; default: return; }*/ // Get packet information //get port numbers int src_port = trace_get_source_port(pkt); int dest_port = trace_get_destination_port(pkt); src_addr_ptr = trace_get_source_address(pkt, (struct sockaddr *) &src_addr); dest_addr_ptr = trace_get_destination_address(pkt, (struct sockaddr *) &dest_addr); if( (NULL == src_addr_ptr) || (NULL == dest_addr_ptr) ) { //printf("In per_packet line:%d\n", __LINE__); return; } //get source ip address char src_ip[100]; if (src_addr_ptr->sa_family == AF_INET) { struct sockaddr_in *src_v4 = (struct sockaddr_in *) src_addr_ptr; inet_ntop(AF_INET, &(src_v4->sin_addr), src_ip, 100); } //get destination ip address char dest_ip[100]; if (dest_addr_ptr->sa_family == AF_INET) { struct sockaddr_in *dest_v4 = (struct sockaddr_in *) dest_addr_ptr; inet_ntop(AF_INET, &(dest_v4->sin_addr), dest_ip, 100); } ts = trace_get_timeval(pkt); struct timeval ts_adjust; ts_adjust.tv_sec = 0; ts_adjust.tv_usec = 0; //printf("1 %s %d %s %d\n", time_adjust_flow->src_ip, time_adjust_flow->src_port, time_adjust_flow->dest_ip, time_adjust_flow->dest_port); //printf("2 %s %d %s %d\n", src_ip, src_port, dest_ip, dest_port); if( (src_port == time_adjust_flow->src_port) && (strcmp(src_ip, time_adjust_flow->src_ip) == 0) && (dest_port == time_adjust_flow->dest_port) && (strcmp(dest_ip, time_adjust_flow->dest_ip) == 0) ) { //printf("CASE1: in per_packet line:%d\n", __LINE__); if(time_adjust_flow->last_direction == -1) { time_adjust_flow->last_direction = 0; ts_adjust.tv_sec = time_adjust_flow->first_pkt_sec; ts_adjust.tv_usec = time_adjust_flow->first_pkt_usec; } else if(time_adjust_flow->last_direction == 0) //update rtt { //add client inter arrival time if(time_adjust_flow->client_inter_usec + time_adjust_flow->last_tv_usec >= 1000000) { ts_adjust.tv_usec = time_adjust_flow->client_inter_usec + time_adjust_flow->last_tv_usec - 1000000; ts_adjust.tv_sec = time_adjust_flow->client_inter_sec + time_adjust_flow->last_tv_sec + 1; } else { ts_adjust.tv_sec = time_adjust_flow->client_inter_sec + time_adjust_flow->last_tv_sec; ts_adjust.tv_usec = time_adjust_flow->client_inter_usec + time_adjust_flow->last_tv_usec; } } else if(time_adjust_flow->last_direction == 1) { //add reaction time if(time_adjust_flow->reaction_usec + time_adjust_flow->last_tv_usec >= 1000000) { ts_adjust.tv_usec = time_adjust_flow->reaction_usec + time_adjust_flow->last_tv_usec - 1000000; ts_adjust.tv_sec = time_adjust_flow->reaction_sec + time_adjust_flow->last_tv_sec + 1; } else { ts_adjust.tv_usec = time_adjust_flow->reaction_usec + time_adjust_flow->last_tv_usec; ts_adjust.tv_sec = time_adjust_flow->reaction_sec + time_adjust_flow->last_tv_sec; } time_adjust_flow->last_direction = 0; } time_adjust_flow->last_tv_sec = ts_adjust.tv_sec; time_adjust_flow->last_tv_usec = ts_adjust.tv_usec; fprintf(fp_write, "%d, %d\n", time_adjust_flow->last_tv_sec, time_adjust_flow->last_tv_usec); //printf("%d, %d\n", time_adjust_flow->last_tv_sec, time_adjust_flow->last_tv_usec); } if( (src_port == time_adjust_flow->dest_port) && (strcmp(src_ip, time_adjust_flow->dest_ip) == 0) && (dest_port == time_adjust_flow->src_port) && (strcmp(dest_ip, time_adjust_flow->src_ip) == 0) ) { //printf("CASE2 in per_packet line:%d\n", __LINE__); if(time_adjust_flow->last_direction == -1) { time_adjust_flow->last_direction = 1; ts_adjust.tv_sec = time_adjust_flow->first_pkt_sec; ts_adjust.tv_usec = time_adjust_flow->first_pkt_usec; } else if(time_adjust_flow->last_direction == 1) //update rtt { //add server inter arrival time if(time_adjust_flow->server_inter_usec + time_adjust_flow->last_tv_usec >= 1000000) { ts_adjust.tv_usec = time_adjust_flow->server_inter_usec + time_adjust_flow->last_tv_usec - 1000000; ts_adjust.tv_sec = time_adjust_flow->server_inter_sec + time_adjust_flow->last_tv_sec + 1; } else { ts_adjust.tv_usec = time_adjust_flow->server_inter_usec + time_adjust_flow->last_tv_usec; ts_adjust.tv_sec = time_adjust_flow->server_inter_sec + time_adjust_flow->last_tv_sec; } //printf("Pkt: %d, last_tv_sec: %d last_tv_usec: %d, ts.tv_sec: %u, ts.tv_usec: %u, rtt_avg_direct0: %f\n", flow_stats.pkt_count, flow_stats.last_tv_sec, flow_stats.last_tv_usec, ts.tv_sec, ts.tv_usec, flow_stats.rtt_avg_direct1); } else if(time_adjust_flow->last_direction == 0) { // add client RTT if(time_adjust_flow->rtt_usec + time_adjust_flow->last_tv_usec >= 1000000) { ts_adjust.tv_usec = time_adjust_flow->rtt_usec + time_adjust_flow->last_tv_usec - 1000000; ts_adjust.tv_sec = time_adjust_flow->rtt_sec + time_adjust_flow->last_tv_sec + 1; } else { ts_adjust.tv_sec = time_adjust_flow->rtt_sec + time_adjust_flow->last_tv_sec; ts_adjust.tv_usec = time_adjust_flow->rtt_usec + time_adjust_flow->last_tv_usec; } time_adjust_flow->last_direction = 1; } time_adjust_flow->last_tv_sec = ts_adjust.tv_sec; time_adjust_flow->last_tv_usec = ts_adjust.tv_usec; fprintf(fp_write, "%d, %d\n", time_adjust_flow->last_tv_sec, time_adjust_flow->last_tv_usec); //printf("%d, %d\n", time_adjust_flow->last_tv_sec, time_adjust_flow->last_tv_usec); } //trace_destroy_packet(copy_pkt); return 0; /*if ( (strcmp(src_ip, "")) || (strcmp(dest_ip, "")) ) { sprintf(OutputBuffer, "sec: %u, usec: %u, src_ip: %s, src_port: %d, dest_ip: %s, dest_port: %d, pkt_size: %d, remaining: %d", ts.tv_sec, ts.tv_usec, src_ip, src_port, dest_ip, dest_port, pkt_size, remaining); }*/ }