已解决
python 使用 scapy 扫描内网IP或端口
来自网友在路上 141841提问 提问时间:2023-09-29 12:50:35阅读次数: 41
最佳答案 问答题库418位专家为你答疑解惑
地址信息在IP层, 可以利用 ICMP 或 ARP 协议数据包探测IP信息.
ICMP协议可以利用ping工具发送数据包, 但是防火墙有可能禁止ICMP, 无法有效探测, 可以考虑使用ARP探测.
利用ICMP协议探测内网IP
def ping_ip(ip_fex):# 扫描范围: 128~254for i in range(128, 255):ip = f'{ip_fex}.{i}'print(f'\r{ip}', end='')output = os.popen(f'ping -n 1 -w 100 {ip} | findstr TTL=').read()if len(output) > 0:print(f"\n{ip} online")if __name__ == '__main__':ping_ip('192.168.110')
利用ARP协议探测内网IP
def ip_thread(start, ip_fex):for i in range(start, start + 20):ip = f'{ip_fex}.{i}' # 目标iptry:pkg = ARP(psrc=f'{ip_fex}.1', pdst=ip) # 伪造ARP广播reply = sr1(pkg, timeout=1, verbose=False) # 发送ARP并获取响应包if reply:print(f'\n{ip}->{reply[ARP].hwsrc}') # 显示MAC地址else:print(f'\r{ip} ...', end='')except Exception as e:print(e)def ip_scan(ip_fex):# 关闭警告import logginglogging.getLogger("scapy.runtime").setLevel(logging.ERROR)# 端口范围 1~254for i in range(1, 255, 20): threading.Thread(target=ip_thread, args=(i, ip_fex)).start()
利用TCP协议探测端口
端口信息在TCP层, 可以使用TCP协议数据包探测端口是否开放
伪造 SYN 数据包, 根据响应数据中的标志位 flags 来判断端口是否正常响应.
SYN: 0x002
ACK: 0x010
SYN-ACK: 0x012
def scan_port(ip):for port in range(22, 100):try:pkg = IP(src='192.168.112.123', dst=ip) / TCP(dport=port, flags='S')reply = sr1(pkg, timeout=1, verbose=False)if reply:if reply[TCP].flags == 0x12: # SYN-ACKprint(f'port->[{port}]')except Exception as e:print(e)
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"python 使用 scapy 扫描内网IP或端口":http://eshow365.cn/6-15413-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!