1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| from scapy.all import * from IPy import IP as IPY from scapy.layers.inet import ICMP, IP, TCP from scapy.layers.l2 import ARP
def preview(list): preview = input("预览(y/n):") if preview == 'y' or 'Y': for li in list: print(li)
def arp(ipy): dst_ip_list = IPY(ipy) alive_ip_list = []
for dst_ip in dst_ip_list: pkt = ARP(pdst=str(dst_ip)) ans = sr1(pkt, timeout=1, verbose=False) if ans is not None: alive_ip_list.append(dst_ip) print(dst_ip, 'is up') else: print(dst_ip, 'is closed')
return alive_ip_list
def ping(ipy): dst_ip_list = IPY(ipy) alive_ip_list = [] for dst_ip in dst_ip_list: pkt = IP(dst=str(dst_ip)) / ICMP() / b'hello' ans = sr1(pkt, timeout=1, verbose=False) if ans is not None: if ans[ICMP].type == 0: if ans[IP].ttl <= 64: print(dst_ip, '(Linux)', 'is up') elif ans[IP].ttl <= 128: print(dst_ip, '(Windows)', 'is up') else: print(dst_ip, '(Mac/Unix)', 'is up') alive_ip_list.append(dst_ip) elif ans[ICMP].type == 11: print(dst_ip, 'is timeout') elif ans[ICMP].type == 3: print(dst_ip, 'is unreachable') elif ans[ICMP].type == 4: print(dst_ip, 'source quench') elif ans[ICMP].type == 5: print(dst_ip, 'redirect') elif ans[ICMP].type == 12: print(dst_ip, '参数问题') else: print(dst_ip, '其他问题:[ICMP].type', ans[ICMP].type) else: print(dst_ip, 'is probably filtered(no response)')
return alive_ip_list
def syn_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i)) ans = sr1(pkt, timeout=1, verbose=False) if ans is not None: if ans[TCP].flags == 'SA': print(i, 'port is open') alive_port_list.append(i) rst = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags='R') sr1(rst, timeout=1, verbose=False) else: print(i, 'port is closed') else: print(i, 'port is filtered')
return alive_port_list
def ack_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags='A') ans = sr1(pkt, timeout=0.2, verbose=False) if ans is None: print(i, 'port is filtered') else: print(i, 'port is closed')
return alive_port_list
def fin_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags='F') ans = sr1(pkt, timeout=1, verbose=False) if ans is None: print(i, 'port is open or filtered') alive_port_list.append(i) else: print(i, 'port is closed')
return alive_port_list
def null_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags=0) ans = sr1(pkt, timeout=1, verbose=False) if ans is None: print(i, 'port is open or filtered') alive_port_list.append(i) else: print(i, 'port is closed')
return alive_port_list
def xmas_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags=1) ans = sr1(pkt, timeout=1, verbose=False) if ans is None: print(i, 'port is open or filtered') alive_port_list.append(i) else: print(i, 'port is closed')
return alive_port_list
def windows_scan(dst_ip, n, m): alive_port_list = [] for i in range(n, m): pkt = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags='S') ans = sr1(pkt, timeout=1, verbose=False) if ans is not None: if ans[TCP].window != 0: print(i, 'port is open') alive_port_list.append(i) rst = IP(dst=str(dst_ip)) / TCP(dport=int(i), flags='R') sr1(rst, timeout=1, verbose=False) else: print(i, 'port is closed') else: print(i, 'port is filtered')
return alive_port_list
if __name__ == '__main__':
while True: print('-------net scanner-------') service = input('你选择主机扫描或是端口扫描?(host/port):') if service == 'host': way = input('请输入扫描方式(arp/ping):') if way == 'ping': dst_ip = input('请输入你要查询的主机或者范围:') alive_ip_list = ping(dst_ip) preview(alive_ip_list) elif way == 'arp': dst_ip = input('请输入你要查询的主机或者范围:') alive_ip_list = arp(dst_ip) preview(alive_ip_list) elif service == 'port': way = input('请输入扫描方式(syn/windows/ack/fin/null/xmas):') if way == 'syn': dst_ip = input('请输入你要查询的主机:') alive_port_list = syn_scan(dst_ip, 1, 1024) preview(alive_port_list) elif way == 'windows': dst_ip = input('请输入你要查询的主机:') alive_port_list = windows_scan(dst_ip, 1, 1024) preview(alive_port_list) elif way == 'ack': dst_ip = input('请输入你要查询的主机:') alive_port_list = ack_scan(dst_ip, 1, 1024) preview(alive_port_list) elif way == 'fin': dst_ip = input('请输入你要查询的主机:') alive_port_list = fin_scan(dst_ip, 1, 1024) preview(alive_port_list) elif way == 'null': dst_ip = input('请输入你要查询的主机:') alive_port_list = null_scan(dst_ip, 1, 1024) preview(alive_port_list) elif way == 'xmas': dst_ip = input('请输入你要查询的主机:') alive_port_list = xmas_scan(dst_ip, 1, 1024) preview(alive_port_list) else: print('i am a teapot')
|