我如何在 Java 中嗅探网络流量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26152931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:55:02  来源:igfitidea点击:

How could I sniff network traffic in Java?

javasocketssniffing

提问by Java Is Cool

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 to view the network traffic going by. I heard of an idea with a Socket, but I don't get how that would work. So anyways, just looking for an API or a way to write it myself.

我只是四处看看如何制作一个程序来嗅探我的 Java 网络流量,但我找不到任何东西。我想知道是否有任何方法可以查看经过的网络流量。我听说过一个Socket的想法,但我不明白它是如何工作的。所以无论如何,只是寻找一个API或一种自己编写的方法。

EDIT:I would gladly like an API, but I would also like clarification on the way to sniff traffic with a Socket.

编辑:我很乐意使用 API,但我也想澄清使用 Socket 嗅探流量的方式。

采纳答案by Byungjoon Lee

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

jpcap, jNetPcap-- 这些是 Java 中的 pcap 包装项目。

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

Kraken- 类似的项目,有大量示例记录。

simple example from the Kraken web site:

来自 Kraken 网站的简单示例:

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

回答by Juned Ahsan

You need a packet sniffer api, maybe netutilsis what you need:

您需要一个数据包嗅探器 api,也许netutils就是您所需要的:

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.

'netutils' 包提供了一个低级 java 网络库。它包含用于嗅探、注入、构建和解析以太网/IP/TCP/UDP/ICMP 数据包的广泛基础设施。

回答by kervin

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

另一个 Java libpcap 包装器是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.

Pcap4J 是一个用于捕获、制作和发送数据包的 Java 库。Pcap4J 通过 JNA 封装了一个本地数据包捕获库(libpcap 或 WinPcap),并为您提供面向 Java 的 API。

回答by murtaza.webdev

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.

不告诉任何 API 或 Java 相关的事情,但如果您真的只想嗅探数据以进行分析,请尝试:WireShark。它是用于网络分析的应用程序。

Its useful if someone is not aware of.

如果有人不知道,它很有用。