歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下用RAW socket發送syn包

Linux下用RAW socket發送syn包

日期:2017/3/1 11:09:04   编辑:Linux編程
源碼編譯方法:gcc -o syn syn.c

結果:在CentOS 6上成功運行,用tcpdump抓包分析,發送的對端有syn,ack包返回,一切正常。

過程:寫代碼時忘記了對tcph->protocol賦值,計算出得checksum老不對,數據包是成功發出去了,但是對端沒syn,ack包回,查了幾個小時,郁悶死我~~~

疑問:ip->check為0是內核會計算ip頭的checksum,但是計算出得結果和我用ip_fast_csum得到的結果不一致,這是為何?標記一下,有結論再附上。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5. #include <netinet/ip.h>
  6. #include <netinet/tcp.h>
  7. /* 數據包最大長度,無負載 */
  8. #define MAX_PKG_SIZE 80 /* ip header = 20 ~ 40, tcp header = 20 ~ 40; max = 80 */
  9. /* 計算TCP校驗和的最大使用長度 www.linuxidc.com*/
  10. #define MAX_PSD_SIZE 52 /* psd header = 12, tcp header = 20 ~ 40; max = 52 */
  11. /* 數據包構造參數 */
  12. struct st_args{
  13. struct sockaddr_in saddr; /* 源地址 */
  14. struct sockaddr_in daddr; /* 目的地址 */
  15. };
  16. /* 用於計算TCP校驗和的偽頭部 */
  17. struct psdhdr{
  18. uint32_t saddr; /* ip頭部源地址 */
  19. uint32_t daddr; /* ip頭部目的地址 */
  20. uint8_t mbz; /* 補全字段,需為0 */
  21. uint8_t protocol; /* ip頭部協議 */
  22. uint16_t tcpl; /* tcp長度,包括頭部和數據部分 */
  23. };
  24. /*
  25. * 采用匯編計算ip頭部校驗和
  26. * @param[in]: iph, ip頭指針;
  27. * @param[in]: ihl, ip頭長度(4的倍數)
  28. *
  29. * @return 16位校驗和
  30. * */
  31. static inline uint16_t ip_fast_csum(const void *iph, unsigned int ihl)
  32. {
  33. unsigned int sum;
  34. asm(" movl (%1), %0\n"
  35. " subl $4, %2\n"
  36. " jbe 2f\n"
  37. " addl 4(%1), %0\n"
  38. " adcl 8(%1), %0\n"
  39. " adcl 12(%1), %0\n"
  40. "1: adcl 16(%1), %0\n"
  41. " lea 4(%1), %1\n"
  42. " decl %2\n"
  43. " jne 1b\n"
  44. " adcl $0, %0\n"
  45. " movl %0, %2\n"
  46. " shrl $16, %0\n"
  47. " addw %w2, %w0\n"
  48. " adcl $0, %0\n"
  49. " notl %0\n"
  50. "2:"
  51. /* Since the input registers which are loaded with iph and ihl
  52. are modified, we must also specify them as outputs, or gcc
  53. will assume they contain their original values. */
  54. : "=r" (sum), "=r" (iph), "=r" (ihl)
  55. : "1" (iph), "2" (ihl)
  56. : "memory");
  57. return (uint16_t)sum;
  58. }
  59. /*
  60. * 計算校驗和
  61. * @param[in]: buffer, 待計算數據指針
  62. * @param[in]: size, 數據長度
  63. *
  64. * @return 校驗和
  65. * */
  66. uint16_t csum(uint16_t *buffer, int size)
  67. {
  68. unsigned long cksum = 0;
  69. while(size>1)
  70. {
  71. cksum += *buffer++;
  72. size -= sizeof(uint16_t);
  73. }
  74. if(size)
  75. {
  76. cksum += *(unsigned char*)buffer;
  77. }
  78. cksum = (cksum>>16) + (cksum&0xffff);
  79. cksum += (cksum>>16);
  80. return (uint16_t)(~cksum);
  81. }
  82. /*
  83. * 調試用的函數,用於輸出數據
  84. * */
  85. void data_dump(uint8_t *pdata, int len)
  86. {
  87. int i = 0;
  88. printf("len = %d\n", len);
  89. for(i = 0; i < len; ++i)
  90. {
  91. printf("%02X ", *(pdata + i));
  92. if((i + 1) % 4 == 0)
  93. printf("\n");
  94. }
  95. }
  96. /*
  97. * 數據包發送函數,www.linuxidc.com只構造了ip頭+tcp頭大小的長度;
  98. * ip頭和tcp都無選項部分,tcp無負載數據
  99. * @param[in]: parg, 構造數據包是采用的一些參數
  100. *
  101. * @return -1, 發送失敗;0, 發送成功
  102. * */
  103. int send_pkg(struct st_args* parg)
  104. {
  105. uint8_t datagram[MAX_PKG_SIZE] = {0};
  106. uint8_t psdheader[MAX_PSD_SIZE] = {0};
  107. struct iphdr *iph = (struct iphdr*)datagram;
  108. struct tcphdr *tcph = (struct tcphdr*)(datagram + sizeof(struct iphdr));
  109. struct tcp_options *tcpopt = (struct tcp_options*)(datagram + sizeof(struct iphdr)
  110. + sizeof(struct tcphdr));
  111. struct psdhdr *psdh = (struct psdhdr*)psdheader;
  112. struct tcphdr *tcph_psd = (struct tcphdr*)(psdheader + sizeof(struct psdhdr));
  113. int sockfd = -1, ret = 0;
  114. int optval = 1;
  115. const int *poptval = &optval;
  116. sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  117. if(sockfd < 0)
  118. {
  119. perror("create socket failed!\n");
  120. goto err_out;
  121. }
  122. iph->ihl = 5; /* header length, 5 * 4 = 20 Bytes */
  123. iph->version = 4; /* version, ipv4 */
  124. iph->tos = 0; /* type of service, gernarel */
  125. iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr); /* total length, iph + tcph = 32 Bytes */
  126. iph->id = htons(54321); /* identifcation */
  127. iph->frag_off = htons(0x02 << 13); /* fragment offset field */
  128. iph->ttl = 64; /* time to live */
  129. iph->protocol = 6; /* protocol, tcp */
  130. iph->check = 0; /* checksum */
  131. iph->saddr = parg->saddr.sin_addr.s_addr; /* source address */
  132. iph->daddr = parg->daddr.sin_addr.s_addr; /* dest address */
  133. tcph->source = parg->saddr.sin_port; /* source port */
  134. tcph->dest = parg->daddr.sin_port; /* dest port */
  135. tcph->seq = random(); /* current sended packet sequence number */
  136. tcph->ack_seq = 0; /* expect received next packet sequence number */
  137. tcph->doff = sizeof(struct tcphdr) / 4; /* data position in the packet */
  138. tcph->syn = 1; /* syn packet */
  139. tcph->window = htons(65535); /* size of tcp window, FreeBSD uses this value */
  140. tcph->check = 0; /* checksum */
  141. tcph->urg_ptr = 0; /* urgent data position */
  142. psdh->saddr = iph->saddr;
  143. psdh->daddr = iph->daddr;
  144. psdh->mbz = 0;
  145. psdh->protocol = iph->protocol;
  146. psdh->tcpl = htons(sizeof(struct tcphdr));
  147. //data_dump(psdheader, sizeof(struct psdhdr));
  148. memcpy(tcph_psd, tcph, sizeof(struct tcphdr));
  149. tcph->check = csum((uint16_t*)psdheader, sizeof(struct psdhdr) + sizeof(struct tcphdr));
  150. /* iph->check == 0時, 內核會自動計算校驗和 */
  151. //iph->check = ip_fast_csum(datagram, iph->ihl);
  152. if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, poptval, sizeof(optval)) < 0)
  153. {
  154. perror("setsockopt failed!");
  155. goto err_out;
  156. }
  157. ret = sendto(sockfd, datagram, iph->tot_len, 0,
  158. (struct sockaddr*)&(parg->daddr), sizeof(parg->daddr));
  159. if(ret < 0)
  160. {
  161. perror("sendto socket failed!");
  162. goto err_out;
  163. }
  164. //data_dump(datagram, 40);
  165. close(sockfd);
  166. return 0;
  167. err_out:
  168. if(sockfd != -1)
  169. close(sockfd);
  170. return -1;
  171. }
  172. int main(int argc, char **argv)
  173. {
  174. struct st_args args;
  175. #define MAX_IP_SIZE 16
  176. uint8_t sip[MAX_IP_SIZE] = "192.168.1.107";
  177. uint8_t dip[MAX_IP_SIZE] = "192.168.1.1";
  178. uint16_t sport = 55555;
  179. uint16_t dport = 80;
  180. int8_t arg = 0;
  181. struct option lopts[] = {
  182. {"saddr", required_argument, 0, 's'},
  183. {"sport", required_argument, 0, 'p'},
  184. {"daddr", required_argument, 0, 'd'},
  185. {"dport", required_argument, 0, 'f'}
  186. };
  187. while((arg = getopt_long(argc, argv, "s:p:d:f:", lopts, NULL)) != -1)
  188. {
  189. switch(arg)
  190. {
  191. case 's':
  192. memcpy(sip, optarg, MAX_IP_SIZE);
  193. break;
  194. case 'p':
  195. sport = atoi(optarg);
  196. break;
  197. case 'd':
  198. memcpy(dip, optarg, MAX_IP_SIZE);
  199. break;
  200. case 'f':
  201. dport = atoi(optarg);
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. memset(&args, 0, sizeof(struct st_args));
  208. inet_pton(AF_INET, sip, (void*)&args.saddr.sin_addr);
  209. args.saddr.sin_port = htons(sport);
  210. inet_pton(AF_INET, dip, (void*)&args.daddr.sin_addr);
  211. args.daddr.sin_port = htons(dport);
  212. send_pkg(&args);
  213. return 0;
  214. }
Copyright © Linux教程網 All Rights Reserved