【问题标题】:Libnet11 build IPv6 packet manuallyLibnet11 手动构建 IPv6 数据包
【发布时间】:2012-10-23 00:19:29
【问题描述】:

我正在尝试使用 Libnet11 功能:

int libnet_write_raw_ipv6 (libnet_t *l, u_int8_t *packet, u_int32_t size)

在网络层注入 IPv6 数据包。 我创建了 IPv6 数据包并捕获了它 与 Wireshark。 Wireshark 报道: 格式错误的数据包(wireshark 说接下来 IPv6 中的标头值错误且有效负载 我认为尺寸太大了)

我希望,有人可以通过最小的代码示例帮助我, 展示如何手动构建 IPv6 数据包(使用 ICMPv6 扩展头)与 libnet11 (libnet_write_raw_ipv6())。

我假设最小的代码可能如下所示:

packet_len = 40 + 16; // 40B ~ IPv6 packet, 16B ~ ICMPv6 header
u_char *buf = NULL;

struct ip6_hdr *ip6 = NULL;
struct icmp6_hdr *icmp6 = NULL;

l = libnet_init();
if ( (buf = malloc(packet_len)) == NULL ) {
    // error
}

// create IPv6 header
ip6 = (struct ip6_hdr *) buf;
ip6->ip6_flow   = 0;
ip6->ip6_vfc    = 6 << 4;
ip6->ip6_plen   = 16;              // ICMPv6 packet size
ip6->ip6_nxt    = IPPROTO_ICMPV6;  // 0x3a
ip6->ip6_hlim   = 64;
memcpy(&(ip6->ip6_src), &src_addr, sizeof(struct in6_addr));
memcpy(&(ip6->ip6_dst), &dst_addr, sizeof(struct in6_addr));

// create ICMPv6 header
icmp6 = (struct icmp6_hdr *) (buf + 40); // 40B ~ IPv6 packet size
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_cksum= 0;
icmp6->icmp6_data32[0] = 0;

libnet_do_checksum(l, (u_int8_t *)buf, IPPROTO_ICMPV6, packet_len);

written = libnet_write_raw_ipv6(l, buf, packet_len);
if ( written != packet_len )
    perror("Failed to send packet");

libnet_destroy(l);
free(buf);

我试图找到代码示例但没有成功。 提前谢谢你。

马丁

【问题讨论】:

  • wireshark 为下一个标头显示什么?如果字节顺序是 0x3A00 而不是 0x003A,您可能需要调用 htnos() 来翻转字节顺序。

标签: clang ipv6 icmp libnet packet-injection


【解决方案1】:

您可以使用原始套接字创建它。我也必须做类似的事情,但找不到任何参考。

要使用原始套接字,link 会给你一个很好的解释

【讨论】:

    【解决方案2】:

    如果您使用的是 C++,那么我建议您使用 libtins,这是一个构建嗅探库的数据包。这个简短的 sn-p 完全符合您的要求:

    #include <tins/tins.h>
    
    using namespace Tins;
    
    void test(const IPv6Address &dst, const IPv6Address &src) {
        PacketSender sender;
        IPv6 ipv6 = IPv6(dst, src) / ICMPv6();
        ipv6.hop_limit(64);
        sender.send(ipv6);
    }
    
    int main() {
        // now use it
        test("f0ef:1234::1", "f000::1");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2016-11-26
      相关资源
      最近更新 更多