kali渗透测试之主动信息收集2——三层发现(ping、traceroute、scapy、fping、Hping)

三层主机发现

  • 原理:使用IP/ICMP协议
  • 优点:相对于二层可以路由,速度比较快
  • 缺点:相比二层速度慢,易被边界防火墙过滤掉
  • 方法:ping、traceroute、scapy、fping、Hping
       ICMP 的作用是用来实现 Intenet 管理的,进行路径的发现,网路通信情况,或者目标主机的状态;在三层发现中主要使用 ICMP协议,ARP 协议属于二层协议,它是基于广播的,所以不可路由。而ICMP协议是可以路由的,理论上使用ICMP协议可以发现世界上所有存活的目标主机,前提是目标主机得接收我发给它的ICMP协议,同时会对我的ICMP探测做出相应的响应,由此可以判断出目标主机是否存活。

       不管是二层扫描还是三层扫描,都有可能存在误报或者漏报,当有边界防火墙时,可能活的主机不响应,也可能宕机响应,因此扫描发现结果只能作为一个参考,不能完全相信。

1、ping命令

  • ICMP报头的Type字段一共有0 ~ 15个定义,每种定义表示不同的包的类型,用途也不一样。ping 包发出的是Type 8类型的数据包,而接收到ping包的机器,如果没有防火墙过滤的话,正常ping包响应会返回一个Type 0类型的数据包。
  • Linux和Windows发出的ping包不同,Windows系统默认发4个ping包就结束,而Linux会一直发ping包,直到Ctrl+C结束,或者 -c 指定发包数量。
  • ping -R <ip>   #可以路由追踪
root@kali:~# ping 192.168.247.129 -c 4   //-c 指定发包数量
PING 192.168.247.129 (192.168.247.129) 56(84) bytes of data.
64 bytes from 192.168.247.129: icmp_seq=1 ttl=128 time=0.606 ms
64 bytes from 192.168.247.129: icmp_seq=2 ttl=128 time=0.795 ms
64 bytes from 192.168.247.129: icmp_seq=3 ttl=128 time=0.772 ms
64 bytes from 192.168.247.129: icmp_seq=4 ttl=128 time=0.866 ms

--- 192.168.247.129 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3017ms
rtt min/avg/max/mdev = 0.606/0.759/0.866/0.101 ms

#使用管道过滤可以得到存活主机的IP地址
root@kali:~# ping 192.168.247.129 -c 1 | grep 'bytes from' | cut -d" " -f4 | cut -d":" -f1
192.168.247.129

 抓包分析:

ping命令不支持IP段扫描,可使用shell脚本扫描指定网段

#!/bin/bash
prefix=192.168.247
for addr in $(seq 1 254)
do
	ping -c 1 $prefix.$addr | grep 'bytes from' | cut -d' ' -f4 | cut -d':' -f1
done
root@kali:~# sh ping.sh
192.168.247.1
192.168.247.2
192.168.247.129
192.168.247.157

2、traceroute  路由追踪

不但可以发现目标主机是否在线,还可以发现其经过多少跳路由

root@kali:~# traceroute www.sina.com
traceroute to www.sina.com (58.205.212.208), 30 hops max, 60 byte packets
 1  _gateway (192.168.247.2)  7.054 ms  6.456 ms  0.233 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
...
...
29  * * *
30  * * *    //边界或网络路由设备会屏蔽ICMP协议

3、Scapy

#先定义一个IP()包头,再定义一个ICMP包头,最后组合成一个ping包
root@kali:~# scapy
WARNING: No route found for IPv6 destination :: (no default route?)
INFO: Can't import python ecdsa lib. Disabled certificate manipulation tools
Welcome to Scapy (2.3.3)
>>> i=IP()
>>> p=ICMP()
>>> ping=(i/p)       //把IP包和ICMP包组合成ping包
>>> ping.display()   //查看包头结构
###[ IP ]### 
  version= 4
  ihl= None
  tos= 0x0
  len= None
  id= 1
  flags= 
  frag= 0
  ttl= 64
  proto= icmp
  chksum= None
  src= 127.0.0.1                    //默认源IP和目标IP为127.0.0.1
  dst= 127.0.0.1
  \options\
###[ ICMP ]### 
     type= echo-request
     code= 0
     chksum= None
     id= 0x0
     seq= 0x0

>>> ping[IP].dst="192.168.247.129"  //设置目标IP为192.168.247.129
>>> ping.display()
###[ IP ]### 
  version= 4
  ihl= None
  tos= 0x0
  len= None
  id= 1
  flags= 
  frag= 0
  ttl= 64
  proto= icmp
  chksum= None
  src= 192.168.247.157              //自动检测本地网卡
  dst= 192.168.247.129
  \options\
###[ ICMP ]### 
     type= echo-request
     code= 0
     chksum= None
     id= 0x0
     seq= 0x0

>>> a=sr1(ping)                     //发包,用a接收响应包
Begin emission:
.*Finished to send 1 packets.

Received 2 packets, got 1 answers, remaining 0 packets
>>> a.display()
###[ IP ]### 
  version= 4L
  ihl= 5L
  tos= 0x0
  len= 28
  id= 15461
  flags= 
  frag= 0L
  ttl= 128
  proto= icmp
  chksum= 0x8e0b
  src= 192.168.247.129
  dst= 192.168.247.157
  \options\
###[ ICMP ]### 
     type= echo-reply
     code= 0
     chksum= 0xffff
     id= 0x0
     seq= 0x0
###[ Padding ]### 
        #数据包不足位,补位
        load= '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'


#将上面的命令组合成一个命令
>>> sr1(IP(dst="192.168.247.129")/ICMP())     #目标网络存活
Begin emission:
.*Finished to send 1 packets.

Received 2 packets, got 1 answers, remaining 0 packets
<IP  version=4L ihl=5L tos=0x0 len=28 id=15466 flags= frag=0L ttl=128 proto=icmp chksum=0x8e06 src=192.168.247.129 dst=192.168.247.157 options=[] |<ICMP  type=echo-reply code=0 chksum=0xffff id=0x0 seq=0x0 |<Padding  load='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |>>>

#当目标主机不存活,scapy会一直等待响应,需加上timeout=1进行限制
>>> sr1(IP(dst="192.168.247.11")/ICMP(),timeout=1)
Begin emission:
...WARNING: Mac address to reach destination not found. Using broadcast.
.Finished to send 1 packets.
..
Received 6 packets, got 0 answers, remaining 1 packets

python脚本

#!/usr/bin/python
from scapy.all import *
import sys

address = sys.argv[1]

prefix = address.split(".")[0] + '.' + address.split(".")[1] + '.' + address.split(".")[2] + '.'

for addr in range(0,254):
	answer=sr1(IP(dst=prefix+str(addr))/ICMP(),timeout=0.1,verbose=0)
	if answer == None:
		pass
	else:
		print prefix+str(addr)
root@kali:~# chmod +x scapy.py     //对scapy.py文件赋予执行权限
root@kali:~# ./scapy.py 192.168.247.0
192.168.247.1
192.168.247.2
192.168.247.129

抓包分析

4、nmap

nmap本网段发ARP包,不同网段发ICMP包。

#同一网段:nmap <IP> -sn 会发出一个ARP数据包来探测目标IP是否在线
root@kali:~# nmap 192.168.247.129 -sn
Starting Nmap 7.70 ( https://nmap.org ) at 2019-04-12 10:07 CST
Nmap scan report for bogon (192.168.247.129)
Host is up (0.00031s latency).
MAC Address: 00:0C:29:8F:74:74 (VMware)
Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

#不同网段:nmap <IP> -sn 会发出ICMP数据包来探测目标IP是否在线
root@kali:~# nmap 119.75.217.109 -sn
Starting Nmap 7.70 ( https://nmap.org ) at 2019-04-12 10:13 CST
Nmap scan report for 119.75.217.109
Host is up (0.0026s latency).
Nmap done: 1 IP address (1 host up) scanned in 0.10 seconds

nmap通常会使用大量综合的技术对目标进行探测,所以我们可以看到nmap在进行三层主机发现时,也会使用四层协议TCP。 

5、fping

         fping类似于ping,但比ping功能强大。两者区别在于,ping在命令行不支持对IP地址段进行扫描,fping在命令行可针对地址段进行扫描,也可以使用 -f 指定含有要ping的主机列表文件。

         与ping要等待某一主机连接超时或发回反馈信息不同,fping给一个主机发送完数据包后,马上给下一个主机发送数据包,实现多主机同时ping。如果某一主机ping通,则此主机将被打上标记,并从等待列表中移除,如果没ping通,说明主机无法到达,主机仍然留在等待列表中,等待后续操作。

root@kali:~# fping 192.168.247.129 -c 1                       //-c 指定fping次数
192.168.247.129 : [0], 84 bytes, 0.39 ms (0.39 avg, 0% loss)

192.168.247.129 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.39/0.39/0.39

root@kali:~# fping -g 192.168.247.125 192.168.247.130 -c 1    //-g  指定网段
192.168.247.129 : [0], 84 bytes, 0.70 ms (0.70 avg, 0% loss)

192.168.247.125 : xmt/rcv/%loss = 1/0/100%
192.168.247.126 : xmt/rcv/%loss = 1/0/100%
192.168.247.127 : xmt/rcv/%loss = 1/0/100%
192.168.247.128 : xmt/rcv/%loss = 1/0/100%
192.168.247.129 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.70/0.70/0.70
192.168.247.130 : xmt/rcv/%loss = 1/0/100%

#经过多次实验发现,如果直接在fping命令后对其进行过滤,不能实现相应的效果。比如下面这个命令:
root@kali:~# fping -g 192.168.247.125 192.168.247.130 -c 1 | grep 'min/avg/max'

192.168.247.126 : xmt/rcv/%loss = 1/0/100%
192.168.247.127 : xmt/rcv/%loss = 1/0/100%
192.168.247.128 : xmt/rcv/%loss = 1/0/100%
192.168.247.129 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 3.27/3.27/3.27
192.168.247.130 : xmt/rcv/%loss = 1/0/100%

#因此如果想要只输出存活的主机IP,我们可以将结果保存在result.txt文件中,然后再对该文件中的内容进行过滤,便可以得到相应的结果。
root@kali:~# fping -g 192.168.247.125 192.168.247.130 -c 1 >> result.txt

192.168.247.126 : xmt/rcv/%loss = 1/0/100%
192.168.247.127 : xmt/rcv/%loss = 1/0/100%
192.168.247.128 : xmt/rcv/%loss = 1/0/100%
192.168.247.129 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.22/0.22/0.22
192.168.247.130 : xmt/rcv/%loss = 1/0/100%
root@kali:~# cat result.txt | grep 'min/avg/max'
root@kali:~# cat result.txt
192.168.247.129 : [0], 84 bytes, 0.22 ms (0.22 avg, 0% loss)
fping -g 192.168.247.0/24  //扫描192.168.247.0整个网段
fping -f iplist.txt        //-f  指定扫描的文件

6、Hping

        能够发送几乎任意TCP/IP包,常被用于检测网络和主机,其功能很强大,但每次只能扫描一个目标。可以发大量定制ping包,可做到一定程度拒绝服务攻击。

root@kali:~# hping3 192.168.247.129 -c 2
HPING 192.168.247.129 (eth0 192.168.247.129): NO FLAGS are set, 40 headers + 0 data bytes
len=46 ip=192.168.247.129 ttl=128 id=15792 sport=0 flags=RA seq=0 win=0 rtt=4.8 ms
len=46 ip=192.168.247.129 ttl=128 id=15793 sport=0 flags=RA seq=1 win=0 rtt=4.4 ms

--- 192.168.247.129 hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 4.4/4.6/4.8 ms

#指定使用ICMP协议进行扫描
root@kali:~# hping3 192.168.247.129 --icmp -c 2
HPING 192.168.247.129 (eth0 192.168.247.129): icmp mode set, 28 headers + 0 data bytes
len=46 ip=192.168.247.129 ttl=128 id=15773 icmp_seq=0 rtt=8.3 ms
len=46 ip=192.168.247.129 ttl=128 id=15774 icmp_seq=1 rtt=6.1 ms

--- 192.168.247.129 hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 6.1/7.2/8.3 ms

#指定使用udp协议进行扫描
root@kali:~# hping3 192.168.247.129 --udp -c 2
HPING 192.168.247.129 (eth0 192.168.247.129): udp mode set, 28 headers + 0 data bytes
ICMP Port Unreachable from ip=192.168.247.129 get hostname...
--- 192.168.247.129 hping statistic ---
2 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms

#使用一个命令,完成一个网段的扫描,并将结果输出到handle.txt文件中
root@kali:~#  for addr in $(seq 1 254); do hping3 192.168.247.$addr --icmp -c 1 >> handle.txt & done

root@kali:~# cat handle.txt                                              //由于篇幅问题,这里只列举两个结果
HPING 192.168.247.1 (eth0 192.168.247.1): icmp mode set, 28 headers + 0 data bytes
len=46 ip=192.168.247.1 ttl=128 id=26260 icmp_seq=0 rtt=29.6 ms          //存活主机返回的结果都是以len开头的
... ...

root@kali:~# cat handle.txt | grep ^len | cut -d' ' -f2 | cut -d'=' -f2  //过滤出存活的主机IP
192.168.247.1
192.168.247.2
192.168.247.129

猜你喜欢

转载自blog.csdn.net/weixin_43625577/article/details/89107698
今日推荐