обновленная версия Код (Text): #include "Include/pcap.h" /* Libpcap */ #include "Include/pcap-stdinc.h" #include "Include/Packet32.h" #include "ip.h" /* Internet Protocol */ #include "tcp.h" /* Transmission Control Protocol */ #include <string.h> /* String operations */ #include <stdlib.h> /* Standard library definitions */ #define TCPSYN_LEN 20 /* Pseudoheader (Used to compute TCP checksum. Check RFC 793) */ typedef struct PSEUDOHEADER { unsigned int src; unsigned int dst; unsigned char zero; unsigned char protocol; unsigned short tcplen; } tcp_phdr_t; typedef unsigned short u_int16; typedef unsigned long u_int32; typedef struct ETHHEADER { unsigned char EthDest[6]; /* destination eth addr */ unsigned char EthSource[6]; /* source ether addr */ short EthProto; /* packet type ID field */ } ethhdr; unsigned short in_cksum(unsigned short *addr,int len); unsigned short dst_prt = htons(80); int src_ip = inet_addr("192.168.56.101"); unsigned short src_prt = htons(80); int dst_ip = inet_addr("192.168.56.1"); VOID Send(){ char error[PCAP_ERRBUF_SIZE] = {0}; char *dev; ethhdr EthHeader; pcap_t *descr; /*get device*/ dev=pcap_lookupdev(error); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", error); return; } /*Open the device */ if((descr = pcap_open_live(dev, 100, 1, 1000, error) ) == NULL) { fprintf(stderr,"\nError opening device: %s\n", error); return; } printf("\ndevice: %s\n", dev ); EthHeader.EthDest[0] = 0x0a; EthHeader.EthDest[1] = 0x00; EthHeader.EthDest[2] = 0x27; EthHeader.EthDest[3] = 0x00; EthHeader.EthDest[4] = 0x00; EthHeader.EthDest[5] = 0x0d; EthHeader.EthSource[0] = 0x11; EthHeader.EthSource[1] = 0x11; EthHeader.EthSource[2] = 0x11; EthHeader.EthSource[3] = 0x11; EthHeader.EthSource[4] = 0x11; EthHeader.EthSource[5] = 0x11; EthHeader.EthProto = htons(0x08FF); static int i=0; char one=1; /* R.Stevens says we need this variable for the setsockopt call */ /* Raw socket file descriptor */ /* Buffer for the TCP/IP SYN Packets */ char packet[ sizeof(EthHeader)+ sizeof(ip) +1 ] = {0}; // + sizeof(tcphdr) /* It will point to start of the packet buffer */ ip ipheader; /* It will point to the end of the IP header in packet buffer */ tcphdr tcpheader; /* TPC Pseudoheader (used in checksum) */ tcp_phdr_t pseudohdr; /* TCP Pseudoheader + TCP actual header used for computing the checksum */ char tcpcsumblock[ sizeof(tcp_phdr_t) + TCPSYN_LEN ]; /* IP Header */ ipheader.ip_hl = (sizeof(ip) >> 2); /* Header lenght in octects */ ipheader.ip_v = 4; /* Ip protocol version (IPv4) */ ipheader.ip_len = htons(sizeof(ip));// + sizeof (struct tcphdr) ); ipheader.ip_ttl = 255; /* Time to live: 64 in Linux, 128 in Windows... */ ipheader.ip_p = 6; /* Transport layer prot. TCP=6, UDP=17, ICMP=1... */ ipheader.ip_id = htons( 1337 ); ipheader.ip_src.s_addr = inet_addr("192.168.56.101"); /* Source IP address */ ipheader.ip_dst.s_addr = inet_addr("192.168.56.1"); /* Destination IP address */ /* TCP Header */ tcpheader.th_seq = 0; /* Sequence Number */ tcpheader.th_ack = htonl(1); /* Acknowledgement Number */ tcpheader.th_x2 = 0; /* Variable in 4 byte blocks. (Deprecated) */ tcpheader.th_off = 5; /* Segment offset (Lenght of the header) */ tcpheader.th_flags = TH_RST; /* TCP Flags. We set the Reset Flag */ tcpheader.th_win = htons(4500) + rand()%1000;/* Window size */ tcpheader.th_urp = 0; /* Urgent pointer. */ tcpheader.th_sport = htons(11131); /* Source Port */ tcpheader.th_dport = htons(80); /* Destination Port */ tcpheader.th_sum=0; /* Checksum. (Zero until computed) */ /* Fill the pseudoheader so we can compute the TCP checksum*/ pseudohdr.src = ipheader.ip_src.s_addr; pseudohdr.dst = ipheader.ip_dst.s_addr; pseudohdr.zero = 0; pseudohdr.protocol = ipheader.ip_p; pseudohdr.tcplen = htons( sizeof(tcphdr) ); /* Copy header and pseudoheader to a buffer to compute the checksum */ memcpy(tcpcsumblock, &pseudohdr, sizeof(tcp_phdr_t)); memcpy(tcpcsumblock+sizeof(tcp_phdr_t),&tcpheader, sizeof(tcphdr)); /* Compute the TCP checksum as the standard says (RFC 793) */ tcpheader.th_sum = in_cksum((unsigned short *)(tcpcsumblock), sizeof(tcpcsumblock)); /* Compute the IP checksum as the standard says (RFC 791) */ ipheader.ip_sum = in_cksum((unsigned short *)&ipheader, sizeof(ip)); memcpy(packet, &EthHeader, sizeof(ethhdr)); memcpy(packet+sizeof(ethhdr), &ipheader, sizeof(ip)); ///memcpy(packet+sizeof(EthHeader)+sizeof(struct tcphdr), tcpheader, sizeof(struct tcphdr)); if(pcap_sendpacket(descr,(u_char *)packet,sizeof(packet))==-1) perror("pcap_sendpacket"); else printf("packet sent"); printf("Sent RST Packet:\n"); printf(" SRC: %s:%d\n", inet_ntoa(ipheader.ip_src), ntohs(tcpheader.th_sport)); printf(" DST: %s:%d\n", inet_ntoa(ipheader.ip_dst), ntohs(tcpheader.th_dport)); printf(" Seq=%u\n", ntohl(tcpheader.th_seq)); printf(" Ack=%d\n", ntohl(tcpheader.th_ack)); printf(" TCPsum: %02x\n", tcpheader.th_sum); printf(" IPsum: %02x\n", ipheader.ip_sum); printf(" PACKET=%s\n", packet); } /* This piece of code has been used many times in a lot of differents tools. */ /* I haven't been able to determine the author of the code but it looks like */ /* this is a public domain implementation of the checksum algorithm */ unsigned short in_cksum(unsigned short *addr,int len){ int sum = 0; unsigned short answer = 0; unsigned short *w = addr; int nleft = len; /* * Our algorithm is simple, using a 32-bit accumulator (sum), * we add sequential 16-bit words to it, and at the end, fold back * all the carry bits from the top 16 bits into the lower 16 bits. */ while (nleft > 1) { sum += *w++; nleft -= 2; } /* mop up an odd byte, if necessary */ if (nleft == 1) { *(unsigned char *)(&answer) = *(unsigned char *)w ; sum += answer; } /* add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum &0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = ~sum; /* truncate to 16 bits */ return(answer); }