---
title: "协议分析之TCP旁路阻断"
author: "yzyzero"
date: "2014-09-23T17:38:24Z"
tags: []
categories: ["开源软件"]
slug: "tcp"
draft: false
lastmod: "2014-09-23T17:38:24Z"
id: 2119701
url: "https://yzyzero.iteye.com/blog/2119701"
---

一、阻断未建立起来的连接

      我们知道TCP的建立要经过3次握手，假设客户端C向服务器S请求连接

      1、C发送带有SEQ\_C（随机）初始序列号的SYN报文给S

      2、S回复带有SEQ\_S（随机）初始序列号和确认序列号ACK\_S（必须是SEQ\_C+1）的SYN报文给C

      3、C回复确认序列号ACK\_C（取值为SEQ\_S）给S

      整个过程如果正确的话，连接将会建立。

      通常需要进行阻断的情况是审计控制系统旁路监听内网。旁路监听的方式一般是将主交换机的数据镜像到控制系统，控制系统可以采用

libpcap捕获数据包。

     在这种情况下要阻断tcp连接的建立只要在监听到第一次握手的时候，控制系统伪造服务器发起第二次握手回应，就能阻断客户端与服务器连接的建立。因为我们的系统在内网，发出的报文肯定比服务器快，这样客户端接收到我们伪造的报文以后会回应第三次握手，当服务器真正的报文到达的时候客户端将不再处理，此时客户端再向服务器请求数据，因为seq号和ack号出错，服务器不会受理客户端的请求。

    

**\[cpp]** [view plain](http://blog.csdn.net/pluton/article/details/5816227 "view plain")[copy](http://blog.csdn.net/pluton/article/details/5816227 "copy")

 

01. typedef struct tcp\_header {
02. u\_int16\_t   th\_sport;       /* source port \*/
03. u\_int16\_t   th\_dport;       /* destination port \*/
04. tcp\_seq     th\_seq;         /* sequence number \*/
05. tcp\_seq     th\_ack;         /* acknowledgement number \*/
06. u\_int8\_t    th\_offx2;       /* data offset, rsvd \*/
07. #define TH\_OFF(th)  (((th)-&gt;th\_offx2 & 0xf0) &gt;&gt; 4)
08. u\_int8\_t    th\_flags;
09. #define TH\_FIN  0x01
10. #define TH\_SYN  0x02
11. #define TH\_RST  0x04
12. #define TH\_PUSH 0x08
13. #define TH\_ACK  0x10
14. #define TH\_URG  0x20
15. #define TH\_ECNECHO  0x40    /* ECN Echo \*/
16. #define TH\_CWR      0x80    /* ECN Cwnd Reduced \*/
17. u\_int16\_t   th\_win;         /* window \*/
18. u\_int16\_t   th\_sum;         /* checksum \*/
19. u\_int16\_t   th\_urp;         /* urgent pointer \*/
20. }tcp\_header;

 

 

    判断第一次握手的方式flag = tcp\_h-&gt;th\_flags;当flag等于0x02的时候说明客户端发起tcp握手，伪装第二次握手的代码：

  

**\[cpp]** [view plain](http://blog.csdn.net/pluton/article/details/5816227 "view plain")[copy](http://blog.csdn.net/pluton/article/details/5816227 "copy")

 

01. int ForgedSYN(char \*srcIP, char \*dstIP, int srcPort, int dstPort)
02. {
03. unsigned char buff\[2048] = {0};
04. unsigned char \*ptr = buff;
05. char options\[4] = {0x02, 0x04, 0x05, 0x64};
06. ip\_header iph;
07. tcp\_header tcph;
08. psd\_header psdh;
09. int num = 0;
10. unsigned int isrcip;
11. unsigned int idstip;
12. unsigned short iIPSize = sizeof(ip\_header) / sizeof(unsigned long);
13. unsigned short iIPVersion = 4;
14. unsigned short iTotalSize = sizeof(ip\_header) + sizeof(tcp\_header) + 4;
15. unsigned short iTcpSize = sizeof(tcp\_header) + 4;
16. struct sockaddr\_in server;
17. if(createSocket()&lt;0) return -1;
18. iph.ip\_vhl = (iIPVersion &lt;&lt; 4) | iIPSize;
19. iph.ip\_tos = 0;
20. iph.ip\_len = htons(iTotalSize);
21. iph.ip\_id = htons(17393);
22. iph.ip\_off = 0;
23. iph.ip\_ttl = 118;
24. iph.ip\_p = IPPROTO\_TCP;
25. iph.ip\_sum = 0;
26. isrcip = inet\_addr(srcIP);
27. idstip = inet\_addr(dstIP);
28. memcpy(&iph.ip\_src, &isrcip, 4);
29. memcpy(&iph.ip\_dst, &idstip, 4);
30. iph.ip\_sum = checksum((unsigned short \*)&iph, 20);
31. tcph.th\_sport = htons(srcPort);
32. tcph.th\_dport = htons(dstPort);
33. tcph.th\_seq = htonl(0x581A784D);
34. tcph.th\_ack = htonl(g\_seq+1);
35. tcph.th\_offx2 = (24/4&lt;&lt;4|0);
36. tcph.th\_flags = 0x12;
37. tcph.th\_win = htons(16384);
38. tcph.th\_sum = 0;
39. tcph.th\_urp = 0;
40. psdh.s\_addr = isrcip;
41. psdh.d\_addr = idstip;
42. psdh.mbz = 0;
43. psdh.protocol = IPPROTO\_TCP;
44. psdh.tcpl = htons(iTcpSize);
45. memcpy(buff, &psdh, sizeof(psd\_header));
46. memcpy(buff+sizeof(psd\_header), &tcph, sizeof(tcp\_header));
47. memcpy(buff+sizeof(psd\_header)+sizeof(tcp\_header), options, 4);
48. tcph.th\_sum = checksum((unsigned short \*)buff, sizeof(psd\_header)+sizeof(tcp\_header)+4);
49. memset(buff, 0x00, 2048);
50. ptr = buff;
51. memcpy(ptr, &iph, sizeof(ip\_header));
52. ptr += sizeof(ip\_header);
53. memcpy(ptr, &tcph, sizeof(tcp\_header));
54. ptr += sizeof(tcp\_header);
55. memcpy(ptr, options, 4);
56. server.sin\_family = AF\_INET;
57. server.sin\_port = htons(dstPort);
58. server.sin\_addr.s\_addr = inet\_addr(dstIP);
59. num = sendto(sockfd, buff, iTotalSize, 0, (struct sockaddr \*)&server, sizeof(struct sockaddr));
60. return num;
61. }

 

  数据伪造的时候ip头部和tcp头部要进行校验：

 

  

**\[cpp]** [view plain](http://blog.csdn.net/pluton/article/details/5816227 "view plain")[copy](http://blog.csdn.net/pluton/article/details/5816227 "copy")

 

01. unsigned short checksum(unsigned short \*buffer, int size)
02. {
03. unsigned long cksum=0;
04. while (size &gt; 1)
05. {
06. cksum += \*buffer++;
07. size  -= sizeof(unsigned short);
08. }
09. if (size)
10. {
11. cksum += \*(unsigned char \*)buffer;
12. }
13. cksum = (cksum &gt;&gt; 16) + (cksum & 0xffff);
14. cksum += (cksum &gt;&gt;16);
15. return (unsigned short)(~cksum);
16. }

 

 

二、阻断已经建立起来的连接

      对于已经建立起来的连接只要伪造服务器发送rst包迫使客户端重新进行连接，或者fin包直接中断连接。

      正常通行中的tcp报文seq和ack存在如下关系，假设C向S请求数据，seq是seq1，ack是ack1，服务器返回给客户端的seq2和ack2必须存在这种关系：

      seq2 = ack1

      ack2 = seq1+datalen(服务返回报文的长度，不包括ip头和tcp头)