Exemplo n.º 1
0
  @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));
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Handles the packet specified, and forward packet to the responsible chained handler once done.
   * In this case, the handler parses the packet and creates a <code>GOOSEMessage</code>
   *
   * @param packet the packet to parse to create the <code>GOOSEMessage</code>
   */
  @Override
  public void handlePacket(JPacket packet) {
    Ethernet ethernetHeader = new Ethernet();
    if (packet.hasHeader(ethernetHeader)) {
      // If it's not a 61850 message then don't handle the packet
      if (ethernetHeader.type() != GOOSEMessage.GOOSE_MSG) {
        return;
      }

      try {
        if (gooseMessage == null) {
          gooseMessage = new GOOSEMessage();
        }
        gooseMessage.init();

        // set destination and source mac
        gooseMessage.setDst(ethernetHeader.destination());
        gooseMessage.setSrc(ethernetHeader.source());

        // set ethernet type
        switch (ethernetHeader.type()) {
          case GOOSEMessage.GOOSE_MSG: // GOOSE
            gooseMessage.setMessageType(GOOSEMessage.GOOSE_MESSAGE);
            // set goose payload
            byte[] payload = ethernetHeader.getPayload();
            GOOSEMessageGoosePDU goosePDU = GOOSEMessageGoosePDU.getInstance(payload, 8);
            // GOOSEMessageGoosePDU goosePDU = GOOSEMessageGoosePDU.getInstance(
            // payload, 7 );
            gooseMessage.setAPDU(goosePDU);
            break;
          case GOOSEMessage.GSE_MGMT_MSG: // GSE
            gooseMessage.setMessageType(GOOSEMessage.GSE_MANAGEMENT_MESSAGE);
            // TODO: handle GSE management message
            break;
          case GOOSEMessage.SVM_MSG: // SVM
            gooseMessage.setMessageType(GOOSEMessage.SV_MESSAGE);
            // TODO: handle SVM message
            break;
          default: // not GOOSE traffic so ignore the frame
            gooseMessage = null;
            return;
        }
      } catch (MessageException me) {
        // TODO - write error message
        return;
      }
    }

    // chain to next handler
    if (this._handler != null) {
      this._handler.handlePacket(packet);
    }

    return;
  }
        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;
        }