How could I sniff network traffic in Java?

前端 未结 4 1474
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 16:01

I was just looking around to find out how to make a program that would sniff my network traffic in Java, but I couldn\'t find anything. I wanted to know if there was any way

相关标签:
4条回答
  • 2020-12-10 16:13

    You need a packet sniffer api, maybe netutils is what you need:

    The 'netutils' package gives a low level java network library. It contains extensive infrastructure for sniffing, injecting, building and parsing Ethernet/IP/TCP/UDP/ICMP packets.

    0 讨论(0)
  • 2020-12-10 16:27

    Another Java libpcap wrapper is https://github.com/kaitoy/pcap4j

    Pcap4J is a Java library for capturing, crafting and sending packets. Pcap4J wraps a native packet capture library (libpcap or WinPcap) via JNA and provides you Java-Oriented APIs.

    0 讨论(0)
  • 2020-12-10 16:31

    jpcap, jNetPcap -- those are pcap wrapper projects in Java.

    Kraken -- similar project, well documented with lots of examples.

    simple example from the Kraken web site:

    public static void main(String[] args) throws Exception {
        File f = new File("sample.pcap");
    
        EthernetDecoder eth = new EthernetDecoder();
        IpDecoder ip = new IpDecoder();
        TcpDecoder tcp = new TcpDecoder(new TcpPortProtocolMapper());
        UdpDecoder udp = new UdpDecoder(new UdpPortProtocolMapper());
    
        eth.register(EthernetType.IPV4, ip);
        ip.register(InternetProtocol.TCP, tcp);
        ip.register(InternetProtocol.UDP, udp);
    
        PcapInputStream is = new PcapFileInputStream(f);
        while (true) {
            // getPacket() will throws EOFException and you should call is.close() 
            PcapPacket packet = is.getPacket();
            eth.decode(packet);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 16:34

    Not telling any API or java related thing but if you really want to only sniff data for analysis purpose then give try: WireShark. Its an application used for network analyse.

    Its useful if someone is not aware of.

    0 讨论(0)
提交回复
热议问题