p; if ans[ICMP].type == 0: #ICMP type为0表示是ICMP echo-reply
print '[+]IP:主机%s echo-reply.' % dst
tcp_info = {}
def tcp_scan(self,dst,port):
#TCP SYN,发送TCP SYN包,有响应表示端口开放
ans,unans = sr(IP(dst=dst)/TCP(sport=RandShort(),dport=port,flags='S'),
timeout=self.timeout,retry=self.retry,verbose=False)
if ans.res:
if ans.res[0][0][IP].dst not in Discovery_Scan.tcp_info:
Discovery_Scan.tcp_info[ans.res[0][0][IP].dst] = True
udp_info = {}
def udp_scan(self,dst,port):
#UDP,发送UDP包,有响应表示端口开放
ans,uans = sr(IP(dst=dst)/UDP(sport=RandShort(),dport=port),
timeout=self.timeout,retry=self.retry,verbose=False)
if ans.res:
if ans.res[0][0][IP].dst not in Discovery_Scan.udp_info:
Discovery_Scan.udp_info[ans.res[0][0][IP].dst] = True
class Port_Scan(object):
'''
说明:用于进行端口扫描,判断端口是否开放
'''
def __init__(self,args,timeout=0.5,retry=0):
self.targets = parse_target(args)
self.timeout = timeout
self.retry = retry
syn_port_dict = {}
def syn_port_scan(self,dst,port):
#TCP SYN端口扫描,若SYN包返回携带SYN??ACK(即TCP.flags=18)标志的包,则表明此端口打开。
ans,uans = sr(IP(dst=dst)/TCP(sport=RandShort(),dport=port,flags='S'),
timeout=self.timeout,retry=self.retry,verbose=False)
if ans:
first_respons_pkt = ans.res[0][1]
if first_respons_pkt[TCP] and first_respons_pkt[TCP].flags == 18:
if first_respons_pkt[IP].src not in Port_Scan.syn_port_dict:
Port_Scan.syn_port_dict[first_respons_pkt[IP].src] = [first_respons_pkt[TCP].sport]
else:
Port_Scan.syn_port_dict[first_respons_pkt[IP].src].append(first_respons_pkt[TCP].sport)
udp_port_dict = {}
def udp_port_scan(self,dst,port):
#UDP端口扫描,若UDP端口返回ICMP port-unreachable,则表示端口打开。(排除某些主机对任何UDP端口的探测都响应为ICMP port-unrechable)
ans,uans = sr(IP(dst=dst)/UDP(sport=RandShort(),dport=port),
timeout=self.timeout, retry=self.retry, verbose=False)
if ans.res and ans.res[0][1].haslayer(UDPerror):
first_respons_p