【问题标题】:get website name from packet using Jnetpcap使用 Jnetpcap 从数据包中获取网站名称
【发布时间】:2016-04-29 03:33:36
【问题描述】:

我已经向 Jnetpcap 论坛提出了这个问题。并且没有得到任何回应。 我一直在尝试从请求/响应数据包中获取网站名称。 这是我尝试过的:

PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  
    final Tcp tcp = new Tcp();  
    final Http http = new Http();  
    final Ip4 ip = new Ip4();

    public void nextPacket(PcapPacket packet, String user) {
        if (packet.hasHeader(http)) {    
            packet.getHeader(http);  
            final String content_length =     http.fieldValue(Response.Content_Length);  
            final String response_code = http.fieldValue(Response.ResponseCode);  
            //Find if the given packet is a Request/Response Pkt : First get the TCP header   
            packet.getHeader(tcp);  
            Integer int_tcp_source = new Integer(tcp.source());  
            Integer int_tcp_destination = new Integer(tcp.destination());  
            if(int_tcp_source!=80 && content_length==null){  
                //It is a Request pkt :   
                packet.getHeader(http);  
                final String ref = http.fieldValue(Request.Referer);  
                final String req_url = http.fieldValue(Request.RequestUrl);  
                String page_url = http.fieldValue(Request.Host);   
                System.out.printf("\n Referer  " +ref +req_url );//Get the URL  
                System.out.printf("\nHost " +page_url);
            }
        }
    }
};

但它甚至没有进入:

if (packet.hasHeader(http)) { 

我使用的是 Windows 笔记本电脑和无线连接。

如果我尝试:

if (packet.hasHeader(ip)) {//Ip4

我可以进入 if 块 并可以检索源 IP 地址和目标 IP 地址。

我基本上需要做和http://jnetpcap.com/?q=node/937一样的事情。

我真的被困住了,真的需要别人的帮助。

或者有人可以帮我弄清楚如何从数据包中获取网站名称吗? 一些代码 sn-p 会很棒。

谁能帮忙。

提前致谢。

【问题讨论】:

  • 面临类似问题。你能解决问题吗?

标签: java packet-sniffers jnetpcap


【解决方案1】:

以下代码有效:

PcapPacketHandler<String> handler = new PcapPacketHandler<String>() {
    // Protocol handlers
    private final Tcp tcp = new Tcp();
    private final Http http = new Http();

    @Override
    public void nextPacket(PcapPacket packet, String userString) {
        if (!packet.hasHeader(tcp)) {
            return; // not a TCP package, skip
        }
        if (!packet.hasHeader(http)) {
            return; // not a HTTP package, skip
        }
        if (http.isResponse()) {
            return; // not a HTTP request, skip
        }

        System.out.println("Referer: " + http.fieldValue(Request.Referer));
        System.out.println("Request URL: " + http.fieldValue(Request.RequestUrl));
        System.out.println("Host: " + http.fieldValue(Request.Host));
    }
}

如果我为http://www.stackoverflow.com 运行它,我会得到以下输出:

Referer: https://www.google.com/
Request URL: /
Host: stackoverflow.com
Referer: http://stackoverflow.com/
Request URL: /Js/stub.en.js?v=eb1d547a9670
Host: cdn.sstatic.net
Referer: http://stackoverflow.com/
Request URL: /Sites/stackoverflow/all.css?v=4e41902aa7a6
Host: cdn.sstatic.net
...

您确定您没有尝试访问 HTTPS 资源吗?在这种情况下,对packet.hasHeader(http) 的调用将失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2020-05-11
    • 1970-01-01
    • 2010-12-11
    • 2014-02-09
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多