@Override public void initialize(PcapIf pcapIf) { packets = new ArrayList<JPacket>(); // extract source MAC from injection interface byte[] srcMac; try { srcMac = pcapIf.getHardwareAddress(); } catch (IOException ioe) { log.error("Error obtaining injection interface address: ", ioe); return; } // extract a source IPv4 address from injection interface byte[] srcIp = NetUtils.getLinkLocal6Addr(pcapIf); if (srcIp == null) { log.error("Failed to find a IPv6 link-local source address"); return; } // get a list of pcap files we'll use for injection String[] pcapFiles = JNetPcapUtils.getPcapFilenames(PCAP_DIR); // packet headers Ethernet eth = new Ethernet(); Ip6 ip = new Ip6(); Icmp6 icmp = new Icmp6(); // load pcap files one-by-one for (String pcapFile : pcapFiles) { PcapPacket packet = JNetPcapUtils.pcapPacketFromFile(pcapFile); if (packet == null) { log.warn("No packet found in [{}]", pcapFile); continue; } // validate packet if (!packet.hasHeader(eth) || !packet.hasHeader(ip) || !packet.hasHeader(icmp)) { log.error("Invalid packet type in [{}]", pcapFile); continue; } // set the source MAC/IP in the packet eth.source(srcMac); ip.setByteArray(8, srcIp); // set destination mac/ip for (String[] dst : dstMacIps) { eth.destination(NetUtils.getMacBytes(dst[0])); ip.setByteArray(24, NetUtils.getAddressBytes(dst[1])); // calculate checksums eth.calculateChecksum(); icmp.calculateChecksum(); // make a deep copy of the packet and add it to the list packets.add(new PcapPacket(packet)); } } }
/** * Initialize the network packet from the pcap packet. * * @param packet The captured packet in pcap format. */ private void parsePcapPacket(final PcapPacket packet) { this.length = packet.getTotalSize(); this.timestamp = new Date(packet.getCaptureHeader().timestampInMillis()); final Tcp tcp = new Tcp(); if (packet.hasHeader(tcp)) { packet.getHeader(tcp); this.destinationPort = tcp.destination(); this.sourcePort = tcp.source(); this.isAck = tcp.flags_ACK(); this.isRst = tcp.flags_RST(); this.isSyn = tcp.flags_SYN(); } final Ip4 ip = new Ip4(); if (packet.hasHeader(ip)) { packet.getHeader(ip); this.destinationIp = FormatUtils.ip(ip.destination()); this.sourceIp = FormatUtils.ip(ip.source()); } final JBuffer storage = new JBuffer(JMemory.Type.POINTER); final JBuffer buffer = tcp.peerPayloadTo(storage); if (buffer != null) { this.data = buffer.getByteArray(0, buffer.size()); } }
public void nextPacket(PcapPacket pcappacket, String user) { if (pcappacket.hasHeader(ip)) { if (FormatUtils.ip(ip.source()) != FormatUtils.ip(myinet) && FormatUtils.ip(ip.destination()) != FormatUtils.ip(myinet)) { System.out.println(); System.out.println("IP type:\t" + ip.typeEnum()); System.out.println("IP src:\t-\t" + FormatUtils.ip(ip.source())); System.out.println("IP dst:\t-\t" + FormatUtils.ip(ip.destination())); readdata = true; } } if (pcappacket.hasHeader(eth) && readdata == true) { System.out.println("Ethernet type:\t" + eth.typeEnum()); System.out.println("Ethernet src:\t" + FormatUtils.mac(eth.source())); System.out.println("Ethernet dst:\t" + FormatUtils.mac(eth.destination())); } if (pcappacket.hasHeader(tcp) && readdata == true) { System.out.println("TCP src port:\t" + tcp.source()); System.out.println("TCP dst port:\t" + tcp.destination()); } else if (pcappacket.hasHeader(udp) && readdata == true) { System.out.println("UDP src port:\t" + udp.source()); System.out.println("UDP dst port:\t" + udp.destination()); } /* if (pcappacket.hasHeader(rip) && readdata == true) { System.out.println("RIP count:\t" + rip.count()); System.out.println("RIP header:\t" + rip.getHeader()); } */ if (pcappacket.hasHeader(arp) && readdata == true) { // System.out.println("ARP decode header:\t" + arp.decodeHeader()); // System.out.println("ARP hardware type:\t" + arp. hardwareType()); // System.out.println("ARP hw type descr:\t" + arp.hardwareTypeDescription()); // System.out.println("ARP hw type enum:\t" + arp.hardwareTypeEnum()); // System.out.println("ARP hlen:\t-\t" + arp.hlen()); // System.out.println("ARP operation:\t-\t" + arp.operation()); // System.out.println("ARP plen:\t-\t" + arp.plen()); // System.out.println("ARP protocol type:\t" + arp.protocolType()); // System.out.println("ARP prtcl type descr:\t" + arp.protocolTypeDescription()); // System.out.println("ARP prtcl type enum:\t" + arp.protocolTypeEnum()); // System.out.println("ARP sha:\t-\t" + FormatUtils.mac(arp.sha())); // System.out.println("ARP sha length:\t-\t" + arp.shaLength()); // System.out.println("ARP spa:\t-\t" + FormatUtils.ip(arp.spa())); // System.out.println("ARP spa length:\t-\t" + arp.spaLength()); // System.out.println("ARP spa offset:\t-\t" + arp.spaOffset()); // System.out.println("ARP tha:\t-\t" + FormatUtils.mac(arp.tha())); // System.out.println("ARP tha length:\t-\t" + arp.thaLength()); // System.out.println("ARP tha offset:\t-\t" + arp.thaOffset()); // System.out.println("ARP tpa:\t-\t" + FormatUtils.ip(arp.tpa())); // System.out.println("ARP tpa length:\t-\t" + arp.tpaLength()); // System.out.println("ARP tpa offset:\t-\t" + arp.tpaOffset()); System.out.println("ARP Packet!"); readdata = true; } if (pcappacket.hasHeader(payload) && readdata == true) { payloadContent = payload.getPayload(); System.out.println("Payload:\n"); for (int x = 0; x < payloadContent.length; x++) { System.out.print(payload.toHexdump()); } } if (readdata == true) System.out.println("-\t-\t-\t-\t-"); readdata = false; }