Python ICMP扫描
ICMP协议
互联网控制消息协议(英语:Internet Control Message Protocol,缩写:ICMP)是互联网协议族的核心协议之一。它用于网际协议(IP)中发送控制消息,提供可能发生在通信环境中的各种问题反馈。通过这些信息,使管理者可以对所发生的问题作出诊断,然后采取适当的措施解决。
Type
ICMP的类型
- 0 回显请求 Echo Request
- 8 回显应答 Echo Reply
- 3 目标不可达 Destination Unreachable
- 11 超时消息 Time Exceeded
- 5 重定向消息 Redirect
ICMP扫描
利用ICMP协议来判断存活的主机
原理
对需要扫描的ip发送icmp回显请求Echo Request
,如果收到回显应答Echo Reply
则判断该ip处于活动状态,简单来说就是ping别人,以是否ping得通来判断是否存活
代码
from random import randint
from scapy.all import *
from optparse import OptionParser
from scapy.layers.inet import IP, ICMP
def Scan(ip):
ip_id = randint(1, 65535)
icmp_id = randint(1, 65535)
icmp_seq = randint(1, 65535)
packet = IP(dst=ip, ttl=64, id=ip_id) / ICMP(id=icmp_id, seq=icmp_seq) / b'rootkit'
result = sr1(packet, timeout=1, verbose=False)
if result:
for rcv in result:
scan_ip = rcv[IP].src
print(scan_ip + "--->" 'Host is up')
else:
print(ip + '--->' 'host is down')
def main():
parser = OptionParser("Usage:%prog -i <target host>")
parser.add_option('-i', type='string', dest='IP', help='specify target host')
options, args = parser.parse_args()
print("Scan report for " + options.IP + '\n')
if '_' in options.IP:
for i in range(int(options.IP.split('_')[0].split('.')[3]), int(options.IP.split('-')[1]) + 1):
Scan(
options.IP.split('.')[0] + '.' + options.IP.split('.')[1] + '.' + options.IP.split('.')[
2] + '.' + str(i))
time.sleep(0.2)
else:
Scan(options.IP)
print('\nScan finished!.....\n')
if __name__ == "__main__":
try:
main()
except Exception:
print("interrupted by user, killing all threads...")