r/javahelp • • Jun 03 '17

Why is asking if packets can be read using java like saying Voldemort in the Harry Potter dimension?

I have asked similar questions to "can packets be read using java" in the past and very recently. Every time I do this people scatter, or get defensive attacking the reasons why I want to do so. I never actually end up getting any help, rather people telling me to change language without providing reason. Have I stumbled onto a large secret that I seem to be outside the loop on, or do people get defensive because they do not know themselves? It seems to me that packet capture and analysis is a taboo among java developers for whatever reason.

0 Upvotes

14 comments sorted by

6

u/kingatomic Foo Stack Dev Jun 03 '17 edited Jun 03 '17

I think it's just not something that many folks do or worry about.

Packet capture is really more of an OS thing (really a kernel thing) or a hardware thing than application-level (in other words, most Java is executed in user-land). Java is meant to be more OS- and hardware-agnostic so it typically doesn't have that kind of access on its own.

I think your best bet would be to find a java wrapper for a pcap library.

EDIT And just to add, there aren't a lot of developers who work on networking except at the highest layers of abstraction. If you want to just sniff and analyze a single TCP connection (eg, over a single port) then you can use raw sockets and be done. But sniffing all wire traffic is something that requires kernel/hardware support, which is a lower level of abstraction than Java will give you on its own, hence my suggestion for a pcap wrapper. For linux you should use libpcap and for Windows I'd recommend Winpcap. There are java wrappers for both iirc.

1

u/gold328 Jun 03 '17

Will the use of WinPcap allow me to capture and analyse all packets destined for my machine, or even all packets when in promiscuous mode?

1

u/kingatomic Foo Stack Dev Jun 03 '17

In promiscuous mode it will capture everything on your local wire; this may or may not be everything on the network depending on your network topology, subnetting, VLANs, etc.

Pcap is what programs like wireshark are based on.

3

u/morhp Professional Developer Jun 03 '17

Java is a high level programming language running in a vm. It doesn't have (by itself) access to hardware to capture random packets but of course you can easily implement a server application to read packets destined for your application.

1

u/gold328 Jun 03 '17

Is Java the only language that runs in a VM, how about python? would I be able to implement python to complete the packet capture and analysis for me?

1

u/morhp Professional Developer Jun 04 '17

No, there are also other languages runningin VMs. Python is a scripting language, so it's also not very hardware near.

You probably want to use C/C++ or something like that. Or use an existing api like pcap and control it using Java or any other language of you choice. You may need a small wrapper library implemented in C(++) that you control from java that controls pcap.

2

u/hostetcl Professional Brewer Jun 03 '17

Not so much of a secret, but I'd imagine it's because they're trying to understand your use case. Capturing raw packets and analyzing them isn't great if there's a more high-level solution to your problem.

But, to answer your question, yes, Java can most definitely capture raw packets.

1

u/gold328 Jun 03 '17

I was hoping to attempt to determine the services running on open ports of the machines on my network.

1

u/kingatomic Foo Stack Dev Jun 03 '17

If you don't want to reinvent the wheel, nmap can do this for you.

1

u/gold328 Jun 04 '17

I am looking to reinvent the wheel :)

1

u/CountyMcCounterson Jun 03 '17

We do not speak of these things

1

u/gold328 Jun 03 '17

I knew it!

1

u/mikeydoodah Jun 04 '17

JnetPcap (jnetpcap.com) is a libpcap wrapper for Java. It uses JNI to access a native libpcap wrapper, and you will need the correct native library for the platform you're running on.

0

u/[deleted] Jun 03 '17

[deleted]

1

u/gold328 Jun 03 '17

A raw sockets used when I have control over both ends of the socket, or can I use this to analyse all packets coming from a specific port that I connect to?