小白日记8:kali渗透测试之主动信息收集(二)三层发现:ping、traceroute、scapy、nmap、fping、Hping

 三层发现

三层协议有:IP以及ICMP协议(internet管理协议).icmp的作用是用来实现intenet管理的,进行路径的发现,网路通信情况,或者目标主机的状态;在三层发现中主要使用icmp协议,arp协议属于二层协议,它是基于广播的,所以不可路由。而ICMP协议是可以路由的,理论上可以使用icmp协议发现全球的ip,如果没有边界防火墙(禁止icmp的探测包)进行过滤的话,对目标主机进行扫描,则会收到相应的响应,从而进行捕捉【有边界防火墙的现象比较普遍】,但是三层发现的扫描速度也较二层要慢些。
注:不要完全相信扫描结果,因为会存在漏报、误报,当有边界防火墙时,可能活的主机不响应,也可能宕机响应

1、ping命令
ping <ip>   #linux与windows的ping包,有很大区别:windows默认发四个,linux默认不停,可用-c指定包数量
ping -R <ip>   #也可以做路由追踪
ping命令不支持IP段扫描,使用shell脚本  #会乱序ping
#!/bin/bash
if [ "$#" -ne 1 ];then
  echo "Usage - ./ping.sh [interface]"
  echo "Excample - ./ping.sh 192.168.1.0"
  echo "Example will perform an ARP scan of the local subnet to which eth0 is assigned"
  exit
fi

prefix=$(echo $1 | cut -d '.' -f 1-3)

for addr in $(seq 1 254);do
   ping -c 1 $prefix.$addr | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1
done


2、traceroute 路由追踪

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


root@kali:~# traceroute www.sina.com
traceroute to www.sina.com (121.14.1.189), 30 hops max, 60 byte packets
 1  DD-WRT (192.168.1.1)  1.976 ms  3.157 ms  5.440 ms                  #第一跳
 2  10.12.66.254 (10.12.66.254)  10.196 ms  9.982 ms  9.967 ms
 3  * 10.12.1.54 (10.12.1.54)  11.568 ms  12.779 ms
 4  172.16.254.14 (172.16.254.14)  9.570 ms  16.018 ms  16.016 ms
 5  10.0.3.13 (10.0.3.13)  9.559 ms  15.970 ms  15.954 ms
 6  10.0.4.6 (10.0.4.6)  15.949 ms  5.061 ms  4.393 ms
 7  120.236.177.1 (120.236.177.1)  5.012 ms  5.007 ms  4.999 ms
 8  120.196.2.9 (120.196.2.9)  9.313 ms  9.304 ms 120.196.2.97 (120.196.2.97)  9.290 ms
 9  120.196.240.41 (120.196.240.41)  4.962 ms 120.196.240.93 (120.196.240.93)  9.233 ms 120.196.240.41 (120.196.240.41)  6.145 ms
10  221.183.26.53 (221.183.26.53)  9.873 ms 221.183.26.125 (221.183.26.125)  9.212 ms  9.207 ms
11  221.176.22.182 (221.176.22.182)  9.857 ms 221.176.18.254 (221.176.18.254)  57.810 ms 221.176.22.130 (221.176.22.130)  17.121 ms
12  202.97.15.13 (202.97.15.13)  16.437 ms 221.176.22.130 (221.176.22.130)  16.386 ms 221.176.23.62 (221.176.23.62)  16.385 ms
13  202.97.60.138 (202.97.60.138)  16.373 ms  15.208 ms  12.096 ms
14  202.97.60.138 (202.97.60.138)  13.646 ms 113.108.208.38 (113.108.208.38)  13.625 ms  13.605 ms
15  113.108.209.162 (113.108.209.162)  11.998 ms  12.773 ms  12.261 ms
16  58.63.232.122 (58.63.232.122)  13.024 ms  9.457 ms 113.108.209.162 (113.108.209.162)  15.283 ms
17  * 121.14.1.189 (121.14.1.189)  8.790 ms  8.770 ms

3、scapy
先定义一个ip包头,再定义一个icmp包头,最后组合成一个ping包
root@kali:~# scapy
WARNING: No route found for IPv6 destination :: (no default route?)
Welcome to Scapy (2.3.2)
>>> i=IP()                #定义变量i继承IP包
>>> p=ICMP()              #定义变量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
  dst= 127.0.0.1
  \options\
###[ ICMP ]###
     type= echo-request
     code= 0
     chksum= None
     id= 0x0
     seq= 0x0
>>> 
ping包设置
>>> ping[IP].dst="192.168.1.1"        #设置目标IP为192.168.1.1
>>> 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.1.127               #自动检测本地网卡
  dst= 192.168.1.1
  \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= 23488
  flags= 
  frag= 0L
  ttl= 64
  proto= icmp
  chksum= 0x9b50
  src= 192.168.1.1
  dst= 192.168.1.127
  \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.1.1")/ICMP())   <span style="color:#ff0000;">#<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span></span><pre name="code" class="plain" style="display: inline !important;">sr1(IP(dst="192.168.1.1")/ICMP(),timeout=1)

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=23489 flags= frag=0L ttl=64 proto=icmp chksum=0x9b4f src=192.168.1.1 dst=192.168.1.127 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' |>>>>>>
 当目标IP在网络中不存在,scapy会一直等待响应,需加上timeout=1 
 
python脚本
#!/usr/bin/python

import logging
import subprocess
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import*

if len( sys.argv ) !=2:                               #minglingcanshubugou2
   print "Usage - ./pingger.py [/24 network address]"
   print "Example - ./pinger.py 172.16.36.0"
   print "Example will perform an ICMP scan of the 192.168.1.0/24 range"
   sys.exit()

address = str(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)<span style="font-weight: bold;">
</span>
从文件中读取
#!/usr/bin/python

import logging
import subprocess
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import*

if len( sys.argv ) !=2:                             
   print "Usage - ./pingger.py [/24 network address]"
   print "Example - ./pinger.py 172.16.36.0"
   print "Example will perform an ICMP scan of the 192.168.1.0/24 range"
   sys.exit()

filename = str(sys.argv[1])
file=open(filename,"r")

for addr in file:
   answer=sr1(IP(dst=addr.strip())/ICMP(),timeout=0.1,verbose=0)
   if answer ==None:
     pass;
   else:
     print addr.strip()<strong>       #strip()方法用于移除字符串头尾指定的字符(默认为空格)
</strong>
4、nmap  

 #抓包分析ICMP包   #-sn可指定IP段 -iL <txt> -sn
root@kali:~# nmap -sn 211.144.145.1

Starting Nmap 7.01 ( https://nmap.org ) at 2016-09-10 23:38 CST
Nmap scan report for 211.144.145.1
Host is up (0.11s latency).
Nmap done: 1 IP address (1 host up) scanned in 0.32 seconds

5、Fping
Fping类似于ping,但比ping强大。Fping与ping不同的地方在于,fping可以在命令行中指定要ping的主机数量范围,也可以指定含有要ping的主机列表文件。
与ping要等待某一主机连接超时或发回反馈信息不同,fping给一个主机发送完数据包后,马上给下一个主机发送数据包,实现多主机同时ping。如果某一主机ping通,则此主机将被打上标记,并从等待列表中移除,如果没ping通,说明主机无法到达,主机仍然留在等待列表中,等待后续操作

<strong>root@kali:~# fping -g 192.168.1.100 192.168.1.200 -c 1    #-g指定范围,从100-200 -c只发一个包</strong>
也可 -g 192.168.1.0/24,可用grep提取
-f <file>  #指定文件扫描

6、Hping
能够发送几乎任意TCP/IP 包,Hping常被用于检测网络和主机,其功能非常强大,但每次只能发一个包,可以发大量定制ping包,可做一定程度拒绝服务攻击。
root@kali:~# hping3 192.168.1.1 --icmp -c 2
HPING 192.168.1.1 (eth0 192.168.1.1): icmp mode set, 28 headers + 0 data bytes
len=46 ip=192.168.1.1 ttl=64 id=27384 icmp_seq=0 rtt=36.8 ms
len=46 ip=192.168.1.1 ttl=64 id=27385 icmp_seq=1 rtt=2.4 ms

--- 192.168.1.1 hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 2.4/19.6/36.8 ms
可用脚本或组合行实现循环
for addr in $(seq 1 254); do hping3 1.1.1.$addr --icmp -c 1 >> handle.txt & done<strong>#结果输出到文本文件使结果清晰</strong>
cat handletxt | grep ^len     #筛选活着的IP



小白日记,未完待续……


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值