针对滴滴云DC2服务器的预装工具,我们上期已经介绍了性能工具的基本用法,这次我们结合DC2服务器实例,介绍一下网络分析工具的基本用法。
DC2服务器提供了如下8种常用的网络工具,这些工具的功能非常的强大,本文会进行一些基本用法的介绍。如需获得更多用法,请参考相关工具帮助文档。
网络工具 | 功能介绍 |
---|---|
nc | 网络工具中的“瑞士军刀” |
nmap | 网络扫描和嗅探工具 |
nslookup | 常用域名查询工具 |
traceroute | 追踪数据包在网络上的传输时的全部路径 |
mtr | 综合了ping/nslookup/traceroute来判断网络的相关特性 |
iftop | 实时流量监控工具,监控TCP/IP连接 |
tcpdump | 网络数据包截获分析工具 |
ipset | Linux防火墙iptables的一个伴随工具,可实现对一组IP的限制 |
文章内的实际操作均是在DC2云服务器上进行的,相关购买操作请参见上一篇滴滴云DC2云服务器预装工具介绍(一),或者参见滴滴云DC2云服务器创建帮助文档。
登录DC2服务器之后,我们通过ifconfig命令来查看虚拟机的网络接口配置信息,以备后续操作使用。
[dc2-user@10-254-158-32 ~]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.254.158.32 netmask 255.255.0.0 broadcast 10.254.255.255
inet6 fe80::250:56ff:fe4c:3fbb prefixlen 64 scopeid 0x20<link>
ether 00:50:56:4c:3f:bb txqueuelen 1000 (Ethernet)
RX packets 168419 bytes 20518911 (19.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 439029 bytes 53830085 (51.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 27702 bytes 2407940 (2.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 27702 bytes 2407940 (2.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
nc
nc即netcat,是网络中的“瑞士军刀”,它是一款拥有多种功能的CLI工具,能在两台电脑之间建立连接,通过TCP、UDP协议传输读写数据。具体功能包括传输文件、实现简单聊天、模拟ssh登录远程主机或用它作为其他协议的独立客户端等。
nc的基本用法如下:
想要连接到某处: nc [-options] hostname port[s] [ports]
绑定端口等待连接: nc -l port [-options] [hostname] [port]
端口扫描
nc最经典的用法就是端口扫描,端口扫描经常被用来发现在一些机器上开放的端口,识别系统中的漏洞。
如下,我们扫描10.254.96.249的21-25端口
[dc2-user@10-254-164-116 ~]$ nc -v -z -n 10.254.96.249 21-25
nc: connect to 10.254.96.249 port 21 (tcp) failed: Connection refused
Connection to 10.254.96.249 22 port [tcp/*] succeeded!
nc: connect to 10.254.96.249 port 23 (tcp) failed: Connection refused
nc: connect to 10.254.96.249 port 24 (tcp) failed: Connection refused
nc: connect to 10.254.96.249 port 25 (tcp) failed: Connection refused
-v 参数指详细输出
-z 参数连接成功后立即关闭连接,不进行数据交换
-n 参数指不使用DNS反向查询IP地址的域名
简单聊天工具
在A机器上启动一个server:
[dc2-user@10-254-164-116 ~]$ nc -l 2222
A
B
Hello
world
同时在B机器上启用相应的client :
[dc2-user@10-254-96-249 ~]$ nc 10.254.164.116 2222
A
B
Hello
world
A机器上的输入便会显示在B机器上,同样的B机器上的输入也会显示在A机器上。nc命令在A机器的2222端口启动了一个tcp服务,所有的标准输入和输出都会输出到该端口。若想实现UDP方式监听,需要使用参数 -u。
在A机器启动一个UDP服务:
[dc2-user@10-254-164-116 ~]$ nc -lu 2222
A
B
在B机器端启用相应的UDP
[dc2-user@10-254-96-249 ~]$ nc -u 10.254.164.116 2222
A
B
这是A机器上的输入便会显示在B机器上,B机器上的输入也会显示在A机器上
文件传输
在A机器上启动server并重定向输入到1.txt
[dc2-user@10-254-96-249 ~]$ echo "Hello didi" > 1.txt
[dc2-user@10-254-96-249 ~]$ cat 1.txt
Hello didi
[dc2-user@10-254-96-249 ~]$ nc -l 2222 < 1.txt
B机器上启动client进行监听,并将监听到的内容重定向输出到2.txt
[dc2-user@10-254-164-116 ~]$ nc 10.254.96.249 2222 > 2.txt
[dc2-user@10-254-164-116 ~]$ ls
2.txt
[dc2-user@10-254-164-116 ~]$ cat 2.txt
Hello didi
目录传输
如果想要传送整个目录,需要使用压缩工具tar,压缩后发送压缩包。
在A机器上,创建一个tar归档包,然后使用管道重定向给nc,nc可以通过网络进行传送。
[dc2-user@10-254-96-249 ~]$ ls test
A.txt
[dc2-user@10-254-96-249 ~]$ tar -cvf - test | nc -l 2222
test/
test/A.txt
B机器上,通过nc可以获得归档包,查看A机器上的文件
[dc2-user@10-254-164-116 ~]$ nc -n 10.254.96.249 2222 | tar -xvf -
test/
test/A.txt
其中tar的参数解释如下:
-c: 建立压缩档案
-x:解压
-v:显示所有过程
-f:tar必须带的参数
远程shell
若想要使用shell远程控制另一台机器,通常有ssh、telnet与nc。下面介绍nc的使用方法。
在A机器通过hostname显示本机IP,然后启动nc 当连接成功时执行/bin/bash
[dc2-user@10-254-96-249 ~]$ hostname
10-254-96-249
[dc2-user@10-254-96-249 ~]$ nc -l 2222 -e /bin/bash
在B机器上,连接A机器。我们仍通过hostname命令验证,可以看到在B机器上可以查询A机器的IP信息
[dc2-user@10-254-164-116 ~]$ nc 10.254.96.249 2222
hostname
10-254-96-249
另外也可以作反方向shell。在A机器上,启动nc服务
[dc2-user@10-254-96-249 ~]$ nc -l 2222
在B机器上,作为客户端连接A机器
[dc2-user@10-254-164-116 ~]$ nc 10.254.96.249 2222 -e /bin/bash
此时,即可在A机器上对B机器执行shell操作
[dc2-user@10-254-96-249 ~]$ nc -l 2222
hostname
10-254-164-116
这种反向shell操作,经常被用来绕过防火墙的限制,如阻止入站连接
端口转发
设置连接到 80 端口的连接转发到 8080 端口
[dc2-user@10-254-164-116 ~]$ sudo ncat -u -l 80 -c 'ncat -u -l 8080'
nmap
nmap是一款开源的网络探测与安全审核工具,它的设计目标是扫描大型网络。nmap基本功能有三个:扫描主机端口,嗅探所提供的网络服务;探测一组主机是否在线;推断主机所用的操作系统、到达主机经过的路由、系统已开放端口的软件版本。
nmap的基本用法为:
nmap [ip] [-options]
扫描所有tcp端口
如下,我们将扫描10.254.96.249的1-65535端口
[dc2-user@10-254-164-116 ~]$ nmap 10.254.96.249 -p 1-65535
Starting Nmap 6.40 ( http://nmap.org ) at 2018-07-10 16:25 CST
Nmap scan report for 10.254.96.249
Host is up (0.00044s latency).
Not shown: 65533 closed ports
PORT STATE SERVICE
22/tcp open ssh
111/tcp open rpcbind
Nmap done: 1 IP address (1 host up) scanned in 2.05 seconds
指定端口范围使用-p参数,如果不指定要扫描的端口,nmap默认扫描从1到1024再加上nmap-services列出的端口
扫描UDP端口
[dc2-user@10-254-164-116 ~]$ sudo nmap -sU 10.254.96.249 -Pn
Starting Nmap 6.40 ( http://nmap.org ) at 2018-07-10 16:40 CST
Nmap scan report for 10.254.96.249
Host is up (0.00045s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
68/udp open|filtered dhcpc
111/udp open rpcbind
MAC Address: 00:50:56:BF:08:33 (VMware)
Nmap done: 1 IP address (1 host up) scanned in 1085.49 seconds
-sU:表示扫描UDP
-Pn:不判断主机是否在线,直接扫描端口
对于udp端口扫描比较慢,上述扫描花了1085.49s 大约18分钟。
扫描多个IP
[dc2-user@10-254-158-32 ~]$ nmap 10.254.96.249 10.254.164.116 --packet-trace
Starting Nmap 6.40 ( http://nmap.org ) at 2018-07-10 17:15 CST
CONN (0.0640s) TCP localhost > 10.254.164.116:80 => Operation now in progress
CONN (0.0642s) TCP localhost > 10.254.96.249:80 => Operation now in progress
CONN (0.0642s) TCP localhost > 10.254.164.116:443 => Operation now in progress
CONN (0.0642s) TCP localhost > 10.254.96.249:443 => Operation now in progress
CONN (2.0656s) TCP localhost > 10.254.96.249:443 => Operation now in progress
CONN (2.0657s) TCP localhost > 10.254.164.116:443 => Operation now in progress
CONN (2.0657s) TCP localhost > 10.254.96.249:80 => Operation now in progress
CONN (2.0657s) TCP localhost > 10.254.164.116:80 => Operation now in progress
Nmap done: 2 IP addresses (0 hosts up) scanned in 3.07 seconds
–packet-trace:表示显示扫描过程中收发报文统计。上述命令完成了两个IP的扫描,我们可以看出nmap在扫描过程中完成了对不同IP的不同的端口的扫描。除此之外namp还可以完成更多IP的扫描,如下我们进行连续IP的扫描
[dc2-user@10-254-158-32 ~]$ nmap 10.254.164.116-120 -Pn
Starting Nmap 6.40 ( http://nmap.org ) at 2018-07-10 17:29 CST
Nmap scan report for 10.254.164.116
Host is up (0.053s latency).
All 1000 scanned ports on 10.254.164.116 are filtered
Nmap scan report for 10.254.164