生成数据包

发布于 2024-11-28 08:07:06 字数 419 浏览 5 评论 0原文

我想知道如何在c中生成数据包。假设我们有一个类型如下:

struct ipheader {
    int version;
    int hdLen;
    int tos;
    int totLen;
    int id;
    ......
    int dstIp;
}

我们有一个 ipheader 类型:

struct ipheader ip;

//init the ip
.....

如何从“ip”生成一个数据包(只是 ip 标头部分)。谁能告诉我怎么做?

我想知道如何生成包含 mac 地址、ip 标头、tcp 标头、有效负载等信息的数据包。然后我可以使用“pcap_sendpacket”函数发送我生成的数据包。有人可以给我举个小例子吗?

I want to know how to generate a packet in c. Supposed we have a type as follows:

struct ipheader {
    int version;
    int hdLen;
    int tos;
    int totLen;
    int id;
    ......
    int dstIp;
}

And we have a ipheader type:

struct ipheader ip;

//init the ip
.....

How can I generate a packet(just ip header part) from "ip". Could anyone show me how?

I want to know to generate packets with information such as mac address, ip header, tcp header, payload. Then I can use the "pcap_sendpacket" function to send the packets I have generated. Could someone give me a little example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残疾 2024-12-05 08:07:06

您可以创建自定义 ip 数据包,如下所示。下面的代码片段还创建了 ip 数据包内的自定义 TCP 部分。此外,自定义函数校验和会生成数据包的校验和。您可以使用 rawsocket 将此数据包直接发送到网络。我认为代码很简单并且不言自明。如果您有任何疑问,请告诉我。

#include <netinet/ip.h>
#include <netinet/tcp.h>


****************************************
ipv4 packet structure
****************************************
struct ipv4_packet {
    struct iphdr iph;
    struct tcphdr tcph;
    void *data;
};


****************************************
snippet of the IPV4 packet making code
****************************************
struct iphdr ip_head;
struct tcphdr tcp_head;
struct sockaddr_in target;
char packet[2048];
int i;

struct tcp_pseudo /*the tcp pseudo header*/
{
    __u32 src_addr;
    __u32 dst_addr;
    __u8 dummy;
    __u8 proto;
    __u16 length;
} pseudohead;

struct help_checksum /*struct for checksum calculation*/
{
    struct tcp_pseudo pshd;
    struct tcphdr tcphd;
    char tcpdata[1024];
} tcp_chk_construct;

/*Prepare IP header*/
ip_head.ihl = 5; /*headerlength with no options*/
ip_head.version = 4;
ip_head.tos = 0;
ip_head.tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + len);
ip_head.id = htons(31337 + (rand() % 100));
ip_head.frag_off = 0;
ip_head.ttl = 255;
ip_head.protocol = IPPROTO_TCP;
ip_head.check = 0; /*Fill in later*/
ip_head.saddr = htonl(src);
ip_head.daddr = htonl(dst);
ip_head.check = in_cksum((unsigned short *) &ip_head, sizeof(struct iphdr));

/*Prepare TCP header*/
tcp_head.source = htons(src_p);
tcp_head.dest = htons(dst_p);
tcp_head.seq = htonl(seq);
tcp_head.ack_seq = htonl(ack);
tcp_head.doff = 5;

/* set or reset ack, fin or syn flags as needed */
tcp_head.ack = 0;
tcp_head.syn = 0;
tcp_head.fin = 0;

tcp_head.res1 = 0;
tcp_head.urg = 0;
tcp_head.psh = 0;
tcp_head.rst = 0;
tcp_head.res2 = 0;

tcp_head.window = htons(0x7c00);
tcp_head.check = 0; /*Fill in later*/
tcp_head.urg_ptr = 0;

/*Assemble structure for checksum calculation and calculate checksum*/
pseudohead.src_addr = ip_head.saddr;
pseudohead.dst_addr = ip_head.daddr;
pseudohead.dummy = 0;
pseudohead.proto = ip_head.protocol;
pseudohead.length = htons(sizeof(struct tcphdr) + len);

tcp_chk_construct.pshd = pseudohead;
tcp_chk_construct.tcphd = tcp_head;
memcpy(tcp_chk_construct.tcpdata, buffer, len);

tcp_head.check = in_cksum((unsigned short *) &tcp_chk_construct,
        sizeof(struct tcp_pseudo) + sizeof(struct tcphdr) + len);

/*Assemble packet*/
memcpy(packet, (char *) &ip_head, sizeof(ip_head));
memcpy(packet + sizeof(ip_head), (char *) &tcp_head, sizeof(tcp_head));
memcpy(packet + sizeof(ip_head) + sizeof(tcp_head), buffer, len);

/*Send packet*/
target.sin_family = AF_INET;
target.sin_addr.s_addr = ip_head.daddr;
target.sin_port = tcp_head.dest;
i = sendto(sfd, packet, sizeof(struct iphdr) + sizeof(struct tcphdr) + len,
        0, (struct sockaddr *) &target, sizeof(struct sockaddr_in));
if (i < 0)
    return (-1); /*Error*/
else
    return (i); /*Return number of bytes sent*/


****************************************        
FUNCTION FOR CHECKSUM   
****************************************
/* function to calculate the checksum for the packet */
unsigned short in_cksum(unsigned short *ptr, int nbytes) {

    register long sum; /* assumes long == 32 bits */
    u_short oddbyte;
    register u_short answer; /* assumes u_short == 16 bits */
    /*
     * the algorithm is simple, using a 32-bit accumulator (sum),
     * we add sequential 16-bit words to it, and at the end, fold back
     * all the carry bits from the top 16 bits into the lower 16 bits.
     */
    sum = 0;
    while (nbytes > 1) {
        sum += *ptr++;
        nbytes -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (nbytes == 1) {
        oddbyte = 0; /* make sure top half is zero */
        *((u_char *) &oddbyte) = *(u_char *) ptr; /* one byte only */
        sum += oddbyte;
    }

    /*
     * Add back carry outs from top 16 bits to low 16 bits.
     */
    sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
    sum += (sum >> 16); /* add carry */
    answer = ~sum; /* ones-complement, then truncate to 16 bits */
    return (answer);
}

You can create custom ip packet as below. The below code snippet also makes the custom TCP part which is inside the ip packet. Also the custom function checksum generates the checksum for the packet. You can use rawsocket to send this packet directly to the network. I think the code is easy and self explanatory. Let me know if you have any queries.

#include <netinet/ip.h>
#include <netinet/tcp.h>


****************************************
ipv4 packet structure
****************************************
struct ipv4_packet {
    struct iphdr iph;
    struct tcphdr tcph;
    void *data;
};


****************************************
snippet of the IPV4 packet making code
****************************************
struct iphdr ip_head;
struct tcphdr tcp_head;
struct sockaddr_in target;
char packet[2048];
int i;

struct tcp_pseudo /*the tcp pseudo header*/
{
    __u32 src_addr;
    __u32 dst_addr;
    __u8 dummy;
    __u8 proto;
    __u16 length;
} pseudohead;

struct help_checksum /*struct for checksum calculation*/
{
    struct tcp_pseudo pshd;
    struct tcphdr tcphd;
    char tcpdata[1024];
} tcp_chk_construct;

/*Prepare IP header*/
ip_head.ihl = 5; /*headerlength with no options*/
ip_head.version = 4;
ip_head.tos = 0;
ip_head.tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + len);
ip_head.id = htons(31337 + (rand() % 100));
ip_head.frag_off = 0;
ip_head.ttl = 255;
ip_head.protocol = IPPROTO_TCP;
ip_head.check = 0; /*Fill in later*/
ip_head.saddr = htonl(src);
ip_head.daddr = htonl(dst);
ip_head.check = in_cksum((unsigned short *) &ip_head, sizeof(struct iphdr));

/*Prepare TCP header*/
tcp_head.source = htons(src_p);
tcp_head.dest = htons(dst_p);
tcp_head.seq = htonl(seq);
tcp_head.ack_seq = htonl(ack);
tcp_head.doff = 5;

/* set or reset ack, fin or syn flags as needed */
tcp_head.ack = 0;
tcp_head.syn = 0;
tcp_head.fin = 0;

tcp_head.res1 = 0;
tcp_head.urg = 0;
tcp_head.psh = 0;
tcp_head.rst = 0;
tcp_head.res2 = 0;

tcp_head.window = htons(0x7c00);
tcp_head.check = 0; /*Fill in later*/
tcp_head.urg_ptr = 0;

/*Assemble structure for checksum calculation and calculate checksum*/
pseudohead.src_addr = ip_head.saddr;
pseudohead.dst_addr = ip_head.daddr;
pseudohead.dummy = 0;
pseudohead.proto = ip_head.protocol;
pseudohead.length = htons(sizeof(struct tcphdr) + len);

tcp_chk_construct.pshd = pseudohead;
tcp_chk_construct.tcphd = tcp_head;
memcpy(tcp_chk_construct.tcpdata, buffer, len);

tcp_head.check = in_cksum((unsigned short *) &tcp_chk_construct,
        sizeof(struct tcp_pseudo) + sizeof(struct tcphdr) + len);

/*Assemble packet*/
memcpy(packet, (char *) &ip_head, sizeof(ip_head));
memcpy(packet + sizeof(ip_head), (char *) &tcp_head, sizeof(tcp_head));
memcpy(packet + sizeof(ip_head) + sizeof(tcp_head), buffer, len);

/*Send packet*/
target.sin_family = AF_INET;
target.sin_addr.s_addr = ip_head.daddr;
target.sin_port = tcp_head.dest;
i = sendto(sfd, packet, sizeof(struct iphdr) + sizeof(struct tcphdr) + len,
        0, (struct sockaddr *) &target, sizeof(struct sockaddr_in));
if (i < 0)
    return (-1); /*Error*/
else
    return (i); /*Return number of bytes sent*/


****************************************        
FUNCTION FOR CHECKSUM   
****************************************
/* function to calculate the checksum for the packet */
unsigned short in_cksum(unsigned short *ptr, int nbytes) {

    register long sum; /* assumes long == 32 bits */
    u_short oddbyte;
    register u_short answer; /* assumes u_short == 16 bits */
    /*
     * the algorithm is simple, using a 32-bit accumulator (sum),
     * we add sequential 16-bit words to it, and at the end, fold back
     * all the carry bits from the top 16 bits into the lower 16 bits.
     */
    sum = 0;
    while (nbytes > 1) {
        sum += *ptr++;
        nbytes -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (nbytes == 1) {
        oddbyte = 0; /* make sure top half is zero */
        *((u_char *) &oddbyte) = *(u_char *) ptr; /* one byte only */
        sum += oddbyte;
    }

    /*
     * Add back carry outs from top 16 bits to low 16 bits.
     */
    sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
    sum += (sum >> 16); /* add carry */
    answer = ~sum; /* ones-complement, then truncate to 16 bits */
    return (answer);
}
客…行舟 2024-12-05 08:07:06

由于您没有指定详细信息(对我来说也看起来像家庭作业),我所能做的就是给出指示。

  1. 查看内存中的结构是如何形成的。
  2. 查找 Ip 标头格式,它将显示 Ip 标头中的哪个成员。
  3. 查找原始套接字。

因此,如果您正确构建结构并使用原始套接字,则只需将此结构写入套接字即可。

Since you haven't specified the details (looks suspiciously like homework to me as well), all i can do is give pointers.

  1. Look up how structures are formed in the memory.
  2. Look up Ip header format which will show you what member is where in an Ip header.
  3. Look up Raw sockets.

So, provided you properly structure your struct and use raw sockets, you only need to write this struct to the socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文