gpt4 book ai didi

在 FreeBSD 中创建和发送原始 IP 数据包 - sendto() 错误无效参数

转载 作者:行者123 更新时间:2023-11-30 15:40:45 27 4
gpt4 key购买 nike

我的目标是创建一个只有 header 而没有有效负载的 IP 数据包。我正在使用http://www.enderunix.org/docs/en/rawipspoof/作为指导。当前的问题是我无法安抚 sendto() 并且我不确定如何获得关于哪个参数无效以及如何无效的更详细的反馈。

在我的程序上运行truss

sendto(3,"E\0\0\^T2\^^\0\0@\M^?\0\0\^?\0\0"...,20,0x0,{ AF_INET 127.0.0.1:1337 },0x10) ERR#22 'Invalid argument'

代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>

#include <arpa/inet.h>

int main(int argc, char *argv[])
{
int sockfd;
const int on = 1;
struct sockaddr_in tx;
struct ip pkt_hdr;
u_char *pkt;

//Init IP packet header fields
pkt_hdr.ip_hl = 0x5; // 5 x 32 bit length units
pkt_hdr.ip_v = 0x4; // IPv4 type packet
pkt_hdr.ip_tos = 0x0; // ?? Type of Service, packet precedence ???
pkt_hdr.ip_len = htons(20); // Total Length of Packet
pkt_hdr.ip_id = htons(12830); // Packet ID
pkt_hdr.ip_off = 0x0; // Set fragment offset to 0, don't want fragmentation
pkt_hdr.ip_ttl = 64; // TTL in number of hops
pkt_hdr.ip_p = IPPROTO_RAW; // Protocol
pkt_hdr.ip_sum = 0x0; // No checksum

pkt_hdr.ip_src.s_addr = inet_addr("127.0.0.1");
pkt_hdr.ip_dst.s_addr = inet_addr("127.0.0.1");

//Copy header packet
pkt = (u_char *)malloc(20);
memcpy(pkt, &pkt_hdr, sizeof(pkt_hdr));

//Open raw socket as the intended output write
//destination for a IP packet
if ((sockfd = socket(PF_INET,SOCK_RAW, IPPROTO_RAW)) < 0 )
{
perror("socket");
exit(1);
}

//Tell kernel to not prepare IP header
if (setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on)))
{
perror("setsockop");
exit(1);
}

//Init network IP information
//for routing and transmission
//so kernel can prepare layer 1 data
memset(&tx, 0, sizeof(tx));
tx.sin_family = AF_INET;
tx.sin_port = htons(1337);
tx.sin_addr.s_addr = inet_addr("127.0.0.1");

//Write the packet out to the network pipe
if (sendto(sockfd, pkt, 20, 0, (struct sockaddr *)&tx,sizeof(tx)) < 0 )
{
perror("sendto");
exit(1);
}

return 0;
}

最佳答案

我的代码的问题是它不符合 FreeBSD 内核。我也问过这个question in the FreeBSD forums有人指出我需要以主机字节顺序而不是网络字节顺序提供 ip.len 和 ip.off 值,以及字节顺序值是 sock() 所提示的。

关于在 FreeBSD 中创建和发送原始 IP 数据包 - sendto() 错误无效参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860574/

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