初学python,之前在Freebuf上找到了一个UDP端口扫描的代码:
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=53
dst_timeout=10
def udp_scan(dst_ip,dst_port,dst_timeout):
udp_scan_resp = sr1(IP(dst=dst_ip)/UDP(dport=dst_port),timeout=dst_timeout)
if (str(type(udp_scan_resp))==""):
retrans = []
for count in range(0,3):
retrans.append(sr1(IP(dst=dst_ip)/UDP(dport=dst_port),timeout=dst_timeout))
for item in retrans:
if (str(type(item))!=""):
udp_scan(dst_ip,dst_port,dst_timeout)
return "Open|Filtered"
elif (udp_scan_resp.haslayer(UDP)):
return "Open"
elif(udp_scan_resp.haslayer(ICMP)):
if(int(udp_scan_resp.getlayer(ICMP).type)==3 and int(udp_scan_resp.getlayer(ICMP).code)==3):
return "Closed"
elif(int(udp_scan_resp.getlayer(ICMP).type)==3 and int(udp_scan_resp.getlayer(ICMP).code) in [1,2,9,10,13]):
return "Filtered"
print udp_scan(dst_ip,dst_port,dst_timeout)
这段代码在实际测试时发现一直在循环扫描,发现udp_scan_resp
中的值始终都是udp(不管目标机器的udp端口是否开放,这个变量存储的返回值始终都是None),在网上找了很多udp端口扫描的脚本都是一样都在这里返回了none,来请问各位这里到底应该怎么使用?
明白了,我是在BT5虚机对实机进行测试的,网卡是同一块,测试其他机器就没问题了