gpt4 book ai didi

c - UDP 原始套接字设置消息

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:08:33 26 4
gpt4 key购买 nike

所以,我用一些 copypasta 设置了原始套接字,它发送数据,那部分工作正常。但是我如何设置通过套接字发送的数据呢?如果有帮助,我正在寻找 DNS 请求。代码如下。

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

最佳答案

嗯……我想你想知道如何在你的消息中设置负载?基本上,您想要从 IP 和 UDP header 偏移并在该点开始写入您的有效负载数据。

一个匆忙拼凑的例子:

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

然后你可以这样写你的payload:

strcpy(offset, "1234");

这里有一些有效的 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));

关于c - UDP 原始套接字设置消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com