地址信息在IP层, 可以利用 ICMP 或 ARP 协议数据包探测IP信息.
 ICMP协议可以利用ping工具发送数据包, 但是防火墙有可能禁止ICMP, 无法有效探测, 可以考虑使用ARP探测.
利用ICMP协议探测内网IP
def ping_ip(ip_fex):
    # 扫描范围: 128~254
    for 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}'  # 目标ip
        try:
            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 logging
    logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
	# 端口范围 1~254
    for 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-ACK
                    print(f'port->[{port}]')
        except Exception as e:
            print(e)










