简体   繁体   English

UDP原始套接字设置消息

[英]UDP Raw Socket set message

So, I have the raw sockets set up with some copypasta, it sends data, that part is working fine. 因此,我为原始套接字设置了一些copypasta,它发送数据,该部分工作正常。 But how would I set the data send over the socket? 但是如何设置通过套接字发送的数据? I'm looking to make a DNS request, if that helps. 我正在寻找一个DNS请求,如果有帮助的话。 Code below. 下面的代码。

int main(int argc, char *argv[])
{
    if (!argv[1])
    {
        printf("Target not specified!\nUsage: ");
        printf(argv[0]);
        printf(" <target>\n");
        exit(1);
    }

    struct ip ip;
    struct udphdr udp;
    int sd;
    const int on = 1;
    struct sockaddr_in sin;
    //char msg[] = "\x03\xF0\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01";
    u_char *packet;
    packet = (u_char *)malloc(120);


    ip.ip_hl = 0x5;
    ip.ip_v = 0x4;
    ip.ip_tos = 0x0;
    ip.ip_len = 60;
    ip.ip_id = htons(12830);
    ip.ip_off = 0x0;
    ip.ip_ttl = 64;
    ip.ip_p = IPPROTO_UDP;
    ip.ip_sum = 0x0;
    ip.ip_src.s_addr = inet_addr(argv[1]);
    ip.ip_dst.s_addr = inet_addr("67.228.44.4");
    ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip));
    memcpy(packet, &ip, sizeof(ip));

    udp.source = htons(80);
    udp.dest = htons(53);
    udp.len = htons(22);
    udp.check = 0;
    udp.check = in_cksum_udp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&udp, sizeof(udp));
    memcpy(packet + 20, &udp, sizeof(udp));

    if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
        perror("raw socket");
        exit(1);
    }

    if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
        perror("setsockopt");
        exit(1);
    }
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = ip.ip_dst.s_addr;

    if (sendto(sd, packet, 120, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0)  
    {
        perror("sendto");
        exit(1);
    }
}

Hmmm...I think you're wondering how to set the payload in your message? 嗯...我想您想知道如何在邮件中设置有效载荷? Basically, you want to offset from the IP and UDP headers and start writing your payload data at that point. 基本上,您想从IP和UDP标头中偏移,然后开始写入有效负载数据。

A hastily thrown together example of this: 一个匆忙投掷的例子:

int offset = packet + sizeof(struct ip) + sizeof(struct udphdr);

Then you can write your payload as follows: 然后,您可以按以下方式编写有效负载:

strcpy(offset, "1234");

Here's some working ICMP code that is effectively writing out the data over a RAW IP socket: 这是一些有效的ICMP代码,可以通过RAW IP套接字有效地写出数据:

struct icmphdr *icmp_hdr; 
char *datapart; 

icmp_hdr = (struct icmphdr *) icmp_data; 
icmp_hdr->i_type = ICMP_ECHO; 
icmp_hdr->i_code = 0; 
icmp_hdr->i_id = (unsigned short) getpid();
icmp_hdr->i_cksum = 0; 
icmp_hdr->i_seq = 0; 
datapart = icmp_data + sizeof(struct icmphdr); 
memset(datapart, 'E', datasize - sizeof(struct icmphdr));  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM