简体   繁体   English

jpcap监听数据包

[英]Packet sniffering by jpcap

I am sniffering packets on ethernet (eth0) in java with the help of jpcap library... So, In my project I have a JpcapCaptor ... 我借助于jpcap库在Java中嗅探以太网(eth0)上的数据包...因此,在我的项目中,我有一个JpcapCaptor ...

    //Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
        JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
        captor.setFilter("icmp", true);
        captor.loopPacket(-1, new PacketPrinter()); 

Then I have Packet printer which prints a body of sniffered packets ... 然后我有数据包打印机,它可以打印嗅探到的数据包的主体...

    public class PacketPrinter implements PacketReceiver {
@Override
public void receivePacket(Packet packet) {
    InputStream is = new ByteArrayInputStream(packet.data);
    try {
        String sstr = IOUtils.toString(is, "UTF-8");
        System.out.println("STRING " + sstr);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
       String ss;
    try {
        ss = new String(packet.data, "UTF-8");
        System.out.println("STRING " + ss);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
} 

But there is a problem... packet.data is a byte[]... And console prints it as 但是有一个问题... packet.data是一个byte [] ...控制台将其打印为

    STRING W�xQ��       !"#$%&'()*+,-./01234567
    STRING W�xQ��       !"#$%&'()*+,-./01234567
    STRING W�xQ��       !"#$%&'()*+,-./01234567 

As I understand it is because of problem with encoding??? 据我了解,这是因为编码问题??? What is the solution to decide this problem? 解决此问题的解决方案是什么?

As I understand it is because of problem with encoding? 据我了解,这是因为编码问题吗?

That may be correct. 那可能是正确的。 It also may be that the stuff you are trying to turn into a String is not text at all. 也可能是您要变成字符串的东西根本不是文本。 In fact, if that is a raw network packet that you have sniffed, it is pretty much guaranteed that some of the packet (the IP/ICMP packet headers) won't be text. 实际上,如果那是您嗅探到的原始网络数据包,则几乎可以保证某些数据包(IP / ICMP数据包头) 不会是文本。

What is the solution to this problem? 这个问题有什么解决方案?

The solution is to understand what it is you are trying to decode and whether or not it is appropriate to decode it as if it was encoded text. 解决方案是了解您要尝试解码的内容以及将其解码为编码文本是否合适 If not, you need to decode / display it differently ... depending on what the relevant RFC says about the packets you are trying to display. 如果不是,则需要以不同的方式解码/显示它...取决于相关RFC关于您试图显示的数据包的说法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM