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_pkt = ans.res[0][1]
if first_respons_pkt[IP].src not in Port_Scan.udp_port_dict:
Port_Scan.udp_port_dict[first_respons_pkt[IP].src] = [first_respons_pkt[UDPerror].dport]
else:
Port_Scan.udp_port_dict[first_respons_pkt[IP].src].append(first_respons_pkt[UDPerror].dport)
def parse_opt():
'''
@说明:通过argparse模块解析程序传入的参数
@return:args
'''
usage = 'python %(prog)s <-p ping扫描类型> <-s 端口发现类型> [-t target] [--port ports]'
description = '简单扫描工具,可以进行存活扫描及端口扫描.\n' \
'存活扫描包括:ARP扫描、ICMP扫描、TCP扫描、UDP扫描.\n' \
'端口扫描包括:TCP SYN扫描、TCP ACK扫描、TCP FIN扫描.'
epilog = '以上做为说明,祝好运!'
parser = argparse.ArgumentParser(usage=usage,description=description,epilog=epilog,version='v1.0')
target_group = parser.add_argument_group('target group',description='用于设置IP、PORT参数')
target_group.add_argument('--target',dest='target',action='store',
help='target为IP或IP段,如192.168.1.1,192.168.1.x,或192.168.1.1-254')
target_group.add_argument('--port',dest='port',action='store',
help='port为待扫描的端口,如21,80,...或21-80')
ping_group = parser.add_argument_group('ping group',description='用于开启存活扫描相关选项')
ping_group.add_argument('-p',dest='ping',action='store_true',help='开启存活扫描')
ping_group.add_argument('--ARP',dest='ARP',action='store_true',help='启动ARP扫描')
ping_group.add_argument('--ICMP',dest='ICMP',action='store_true',help='启动ICMP扫描')
ping_group.add_argument('--TCP',dest='TCP',action='store_true',help='启动TCP扫描')
ping_group.add_argument('--UDP',dest='UDP',action='store_true',help='启动UDP扫描')