本文介绍了使用 Scapy 与 C 套接字交谈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个 UDP 连接并在侦听端口 (localhost),我也尝试从 localhost 发送 Scapy 数据包.出于某种原因,我的 C 代码从未真正捕获过数据包,但是我可以在 Wireshark 中看到数据包显示得很好.我已经有一段时间没有使用套接字了,但是是否有一些特殊的套接字选项我必须设置,或者为什么我能够在 Wireshark 中看到数据包,但不能通过 C 套接字看到?

I have a UDP connection up and listening on a port (localhost) and I am trying to send a Scapy packet from localhost as well. For some reason, my C code never actually captures the packet, however I can see the packet show up in Wireshark just fine. It's been awhile since I've used sockets, but is there some special socket options I have to set or why would I be able to see the packet in Wireshark just fine but not by the C socket?

注意:当我编写相应的套接字代码以发送数据包(从本地主机)时,我能够成功捕获数据包,但是当从另一台计算机发送时,我仍然无法获取侦听代码以捕获数据包.

Note: I was able to successfully catch a packet when I wrote corresponding socket code to send out packets (from localhost) however I am still unable to get the listening code to catch the packet when sent from another computer.

我发现了一个类似的问题但是当我尝试了他们的方法(使用 UDP 而不是 TCP),我仍然无法让 netcat 捕获 Scapy 数据包.

I have found a similar question but when I tried their approach (using UDP instead of TCP), I still couldn't get netcat to catch the Scapy packet.

C 代码(为了清晰起见而精简)

int main() {
    int sock, dataLen, inLen;
    struct sockaddr_in inAddr;
    short listen_port = 8080;
    char buffer[2048];

    if (sock = socket(AF_INET,SOCK_DGRAM,0) < 0) {
        printf("ERROR: unable to establish socket\n");
    return -1;
    }

    // zero out address structure
    memset(&inAddr, 0, sizeof(inAddr));

    inAddr.sin_family = AF_INET;
    inAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    inAddr.sin_port = htons(listen_port);

    if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0) {
    printf("ERROR: unable to bind\n");
    return -1;
    }

    inLen = sizeof(inAddr);

    printf("Now listening on port %d\n", listen_port);
    while(1) {
    dataLen = recvfrom(sock, buffer, 1500, 0, (struct sockaddr*)&inAddr, &inLen);

    if (dataLen < 0)
        printf("Error receiving datagram\n");
    else
        printf("Received packet of length %d\n", dataLen);

    }
    return 0;
}

Scapy 脚本

# set interface
conf.iface="lo0"

# create IP packet
ip_pkt = IP()/UDP()
ip_pkt.payload = "payload test message"
ip_pkt.dport = 8080
ip_pkt.dst = "127.0.0.1"
ip_pkt.src = "127.0.0.1"

# send out packet
send(ip_pkt)

推荐答案

Scapy 需要稍微不同的配置才能在 Loopback 接口上工作,请参阅 http://www.secdev.org/projects/scapy/doc/troubleshooting.html 在标题我无法 ping 127.0.0.1. Scapy 不适用于 127.0.0.1 或环回接口"

Scapy needs to be configured slightly differently to work on the Loopback interface, see http://www.secdev.org/projects/scapy/doc/troubleshooting.html under the heading "I can’t ping 127.0.0.1. Scapy does not work with 127.0.0.1 or on the loopback interface"

我使用了那里给出的代码并发送了一个收到 C 套接字的 scapy 数据包,具体是:

I used the code given there and sent a scapy packet which was received a C Socket, this was specifically:

from scapy.all import *
conf.L3socket=L3RawSocket
packet=IP()/UDP(dport=32000)/"HELLO WORLD"
send(packet)

然后在绑定到端口 32000 上的 lo 的 UDP C 套接字上收到此消息(Scapy 默认通过环回接口发送 IP 数据包).

This was then received on a UDP C Socket bound to lo on port 32000 (Scapy defaults to sending IP packets over the loopback interface).

这篇关于使用 Scapy 与 C 套接字交谈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:05