krot

raw_socket

Aug 16th, 2016
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // raw_socket.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdio.h"
  6. #include "winsock2.h"
  7. #include "ws2tcpip.h" //IP_HDRINCL is here
  8. #include "conio.h"
  9.  
  10. #pragma comment(lib,"ws2_32.lib") //winsock 2.2 library
  11.  
  12.  
  13. typedef struct ip_hdr
  14. {
  15. unsigned char ip_header_len:4; // 4-bit header length (in 32-bit words) normally=5 (Means 20 Bytes may be 24 also)
  16. unsigned char ip_version :4; // 4-bit IPv4 version
  17. unsigned char ip_tos; // IP type of service
  18. unsigned short ip_total_length; // Total length
  19. unsigned short ip_id; // Unique identifier
  20.  
  21. unsigned char ip_frag_offset :5; // Fragment offset field
  22.  
  23. unsigned char ip_more_fragment :1;
  24. unsigned char ip_dont_fragment :1;
  25. unsigned char ip_reserved_zero :1;
  26.  
  27. unsigned char ip_frag_offset1; //fragment offset
  28.  
  29. unsigned char ip_ttl; // Time to live
  30. unsigned char ip_protocol; // Protocol(TCP,UDP etc)
  31. unsigned short ip_checksum; // IP checksum
  32. unsigned int ip_srcaddr; // Source address
  33. unsigned int ip_destaddr; // Source address
  34. } IPV4_HDR, *PIPV4_HDR, FAR * LPIPV4_HDR;
  35.  
  36. // TCP header
  37. typedef struct tcp_header
  38. {
  39. unsigned short source_port; // source port
  40. unsigned short dest_port; // destination port
  41. unsigned int sequence; // sequence number - 32 bits
  42. unsigned int acknowledge; // acknowledgement number - 32 bits
  43.  
  44. unsigned char ns :1; //Nonce Sum Flag Added in RFC 3540.
  45. unsigned char reserved_part1:3; //according to rfc
  46. unsigned char data_offset:4; /*The number of 32-bit words in the TCP header.
  47. This indicates where the data begins.
  48. The length of the TCP header is always a multiple
  49. of 32 bits.*/
  50.  
  51. unsigned char fin :1; //Finish Flag
  52. unsigned char syn :1; //Synchronise Flag
  53. unsigned char rst :1; //Reset Flag
  54. unsigned char psh :1; //Push Flag
  55. unsigned char ack :1; //Acknowledgement Flag
  56. unsigned char urg :1; //Urgent Flag
  57.  
  58. unsigned char ecn :1; //ECN-Echo Flag
  59. unsigned char cwr :1; //Congestion Window Reduced Flag
  60.  
  61. ////////////////////////////////
  62.  
  63. unsigned short window; // window
  64. unsigned short checksum; // checksum
  65. unsigned short urgent_pointer; // urgent pointer
  66. } TCP_HDR , *PTCP_HDR , FAR * LPTCP_HDR , TCPHeader , TCP_HEADER;
  67.  
  68. int _tmain(int argc, _TCHAR* argv[])
  69. {
  70.  
  71.     char host[100]="reg.ru";
  72.     char buf[1000],*data=NULL;
  73.     char source_ip[20]="192.168.1.2"; //buf is the complete packet
  74. SOCKET s;
  75. int k=1;
  76.  
  77. IPV4_HDR *v4hdr=NULL;
  78. TCP_HDR *tcphdr=NULL;
  79.  
  80. int payload=512 , optval;
  81. SOCKADDR_IN dest;
  82. hostent *server;
  83.  
  84. //Initialise Winsock
  85. WSADATA wsock;
  86. printf("\nInitialising Winsock...");
  87. if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
  88. {
  89. fprintf(stderr,"WSAStartup() failed");
  90. exit(EXIT_FAILURE);
  91. }
  92. printf("Initialised successfully.");
  93. ////////////////////////////////////////////////
  94.  
  95. //Create Raw TCP Packet
  96. printf("\nCreating Raw TCP Socket...");
  97. if((s = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)
  98. {
  99. printf("Creation of raw socket failed.");
  100. return 0;
  101. }
  102. printf("Raw TCP Socket Created successfully.");
  103. ////////////////////////////////////////////////
  104.  
  105. //Put Socket in RAW Mode.
  106. printf("\nSetting the socket in RAW mode...");
  107. if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval))==SOCKET_ERROR)
  108. {
  109. printf("failed to set socket in raw mode.");
  110. return 0;
  111. }
  112. printf("Successful.");
  113. ////////////////////////////////////////////////
  114.  
  115.  
  116. //Target Hostname
  117. //printf("\nEnter hostname : ");
  118. //gets(host);
  119.  
  120. printf("\nResolving Hostname...");
  121. if((server=gethostbyname(host))==0)
  122. {
  123. printf("Unable to resolve.");
  124. return 0;
  125. }
  126. dest.sin_family = AF_INET;
  127. dest.sin_port = htons(50000); //your destination port
  128. memcpy(&dest.sin_addr.s_addr,server->h_addr,server->h_length);
  129. printf("Resolved.");
  130. /////////////////////////////////////////////////
  131.  
  132. /*printf("\nEnter Source IP : ");
  133. gets(source_ip);
  134.  */
  135. v4hdr = (IPV4_HDR *)buf; //lets point to the ip header portion
  136. v4hdr->ip_version=4;
  137. v4hdr->ip_header_len=5;
  138. v4hdr->ip_tos = 0;
  139. v4hdr->ip_total_length = htons ( sizeof(IPV4_HDR) + sizeof(TCP_HDR) + payload );
  140. v4hdr->ip_id = htons(2);
  141. v4hdr->ip_frag_offset = 0;
  142. v4hdr->ip_frag_offset1 = 0;
  143. v4hdr->ip_reserved_zero = 0;
  144. v4hdr->ip_dont_fragment = 1;
  145. v4hdr->ip_more_fragment = 0;
  146. v4hdr->ip_ttl = 8;
  147. v4hdr->ip_protocol = IPPROTO_TCP;
  148. v4hdr->ip_srcaddr = inet_addr(source_ip);
  149. v4hdr->ip_destaddr = inet_addr(inet_ntoa(dest.sin_addr));
  150. v4hdr->ip_checksum = 0;
  151.  
  152. tcphdr = (TCP_HDR *)&buf[sizeof(IPV4_HDR)]; //get the pointer to the tcp header in the packet
  153.  
  154. tcphdr->source_port = htons(1234);
  155. tcphdr->dest_port = htons(50000);
  156.  
  157. tcphdr->cwr=0;
  158. tcphdr->ecn=1;
  159. tcphdr->urg=0;
  160. tcphdr->ack=0;
  161. tcphdr->psh=0;
  162. tcphdr->rst=1;
  163. tcphdr->syn=0;
  164. tcphdr->fin=0;
  165. tcphdr->ns=1;
  166.  
  167. tcphdr->checksum = 0;
  168.  
  169. // Initialize the TCP payload to some rubbish
  170. data = &buf[sizeof(IPV4_HDR) + sizeof(TCP_HDR)];
  171. memset(data, '^', payload);
  172.  
  173. printf("\nSending packet...\n");
  174.  
  175. // u_long               One     = 1;
  176. // ioctlsocket(s,FIONBIO,&One);
  177. // bind(s, (SOCKADDR *)&sa, sizeof(sa));
  178.  
  179.  
  180. while(!_kbhit())
  181. {
  182. printf(" %d packets send\r",k++);
  183. if((sendto(s , buf , sizeof(IPV4_HDR)+sizeof(TCP_HDR) + payload, 0,
  184. (SOCKADDR *)&dest, sizeof(dest)))==SOCKET_ERROR)
  185. {
  186.  
  187. printf("Error sending Packet : %d",WSAGetLastError());
  188. break;
  189. }
  190. }
  191.  
  192.     return 0;
  193. }
  194.  
  195. //libpcap или libnet.http://www.binarytides.com/raw-sockets-using-winsock/
RAW Paste Data