Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I sniff network traffic in Java?

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.

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

like image 283
Java Is Cool Avatar asked Oct 02 '14 00:10

Java Is Cool


People also ask

How do you sniff network traffic?

To capture network traffic, you can use tcpdump. This is a packet sniffer that can observe and record network traffic on an interface. In the following example, we capture 1,000 packets using tcpdump. An easier way to analyze network traffic is to use an actual network traffic analyzer, such as Wireshark.

Which tool can be used for network sniffing?

A network sniffer, also known as a packet analyzer, is either software or hardware that can intercept data packets as they travel across a network. Admins use network sniffers to monitor network traffic at the packet level, helping ensure network health and security.

What is a technique that could be used to sniff traffic on a switched network?

arpspoof redirects packets from a target host (or all hosts) on the LAN intended for another host on the LAN by forging ARP replies. This is an extremely effective way of sniffing traffic on a switch.

How do you do packet sniffing?

Packet sniffing is done by using tools called packet sniffer. It can be either filtered or unfiltered. Filtered is used when only specific data packets have to be captured and Unfiltered is used when all the packets have to be captured. WireShark, SmartSniff are examples of packet sniffing tools.


2 Answers

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);
    }
}
like image 170
Byungjoon Lee Avatar answered Oct 12 '22 16:10

Byungjoon Lee


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.

like image 23
kervin Avatar answered Oct 12 '22 16:10

kervin