def callback(header, data): src_ip = None dst_ip = None src_port = None dst_port = None layer4Type = "" output = "" payloadSize = 0 extract = ExtractData() # Parse packet decoder = ImpactDecoder.EthDecoder() packet = decoder.decode(data) # Parse IP packet inside ethernet one iphdr = packet.child() if isinstance(iphdr, IP): # Parse TCP packet inside IP one hdr = iphdr.child() if isinstance(hdr, TCP) or isinstance(hdr, UDP): if isinstance(hdr, TCP): layer4Type = "TCP" # Only look at SYN packets, not ACK ones if hdr.get_SYN() and not hdr.get_ACK(): # Get src and dest IPs src_ip = iphdr.get_ip_src() dst_ip = iphdr.get_ip_dst() src_port = hdr.get_th_dport() dst_port = hdr.get_th_sport() payloadSize = hdr.get_size() - hdr.get_header_size() elif isinstance(hdr, UDP): layer4Type = "UDP" # Get src and dest IPs src_ip = iphdr.get_ip_src() dst_ip = iphdr.get_ip_dst() src_port = hdr.get_th_dport() dst_port = hdr.get_th_sport() payloadSize = hdr.get_size() - hdr.get_header_size() # Results are printed output = "(%s) Connection attempted from: %s:%s to: %s:%s\n" % ( layer4Type, src_ip, src_port, dst_ip, dst_port, ) if payloadSize != 0: output += "\nPayload size: %d\n----%s----\n----\n" % (payloadSize, hdr.get_data_as_string()) print output if src_ip and dst_ip: extract.writeToFile("packetOutput.txt", output, "a") else: print "\nIP header doesn't exist.\n" iphdr = None
def callback(header, data): src_ip = None dst_ip = None src_port = None dst_port = None layer4Type = "" output = "" payloadSize = 0 extract = ExtractData() #Parse packet decoder = ImpactDecoder.EthDecoder() packet = decoder.decode(data) #Parse IP packet inside ethernet one iphdr = packet.child() if isinstance(iphdr, IP): #Parse TCP packet inside IP one hdr = iphdr.child() if isinstance(hdr, TCP) or isinstance(hdr, UDP): if isinstance(hdr, TCP): layer4Type = "TCP" #Only look at SYN packets, not ACK ones if hdr.get_SYN() and not hdr.get_ACK(): #Get src and dest IPs src_ip = iphdr.get_ip_src() dst_ip = iphdr.get_ip_dst() src_port = hdr.get_th_dport() dst_port = hdr.get_th_sport() payloadSize = hdr.get_size() - hdr.get_header_size() elif isinstance(hdr, UDP): layer4Type = "UDP" #Get src and dest IPs src_ip = iphdr.get_ip_src() dst_ip = iphdr.get_ip_dst() src_port = hdr.get_th_dport() dst_port = hdr.get_th_sport() payloadSize = hdr.get_size() - hdr.get_header_size() #Results are printed output = "(%s) Connection attempted from: %s:%s to: %s:%s\n" % ( layer4Type, src_ip, src_port, dst_ip, dst_port) if (payloadSize != 0): output += "\nPayload size: %d\n----%s----\n----\n" % ( payloadSize, hdr.get_data_as_string()) print output if (src_ip and dst_ip): extract.writeToFile("packetOutput.txt", output, "a") else: print "\nIP header doesn't exist.\n" iphdr = None