摘要:在我们使用扫描IP的时候可能会遭受到防火墙的封锁,那么这个时候不妨使用scapy库的ARP协议来扫描,来达到绕过防火墙的木的
目录
基础准备:需要安装scapy库:pip install scapy
一、扫描IP
利用scapy的ARP协议来达到扫描IP的目的
源代码:
import logging
import threading
from scapy.layers.inet import IP, TCP
from scapy.layers.l2 import ARP
from scapy.sendrecv import sr1
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
def scapy_ip(start):
for i in range(start, start+50):
ip = f'10.10.10.{i}' #目标网段
try:
pkg = ARP(psrc='本机IP', pdst=ip) #本机IP
reply = sr1(pkg, timeout=3, verbose=False)#verbose表示不回显这些结果
print(reply[ARP].hwsrc)
print(f"{ip} 在线")
except :
pass
if __name__ == '__main__':
for start in range(1,255,50):
threading.Thread(target=scapy_ip, args=(start,)).start()
二、扫描常用端口
利用TCP握手来扫描端口
源代码:
import logging
from scapy.layers.inet import IP, TCP
from scapy.sendrecv import sr1
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
def scapy_port(ip):
list = [7, 21, 22, 23, 25, 43, 53, 67, 68, 69, 79, 80, 81, 88, 109, 110, 113, 119, 123, 135, 135,
137, 138, 139, 143, 161, 162, 179, 194, 220, 389, 443, 445, 465, 513, 520, 520, 546, 547,
554, 563, 631, 636, 991, 993, 995, 1080, 1194, 1433, 1434, 1494, 1521, 1701, 1723, 1755,
1812, 1813, 1863, 3269, 3306, 3307, 3389, 3544, 4369, 5060, 5061, 5355, 5432, 5671, 5672, 6379,
7001, 8080, 8081, 8088, 8443, 8883, 8888, 9443, 9988, 9988, 15672, 50389, 50636, 61613, 61614]
for port in list:
try:
pkg = IP(src='本机IP', dst=ip)/TCP(dport=port, flags='S')
reply = sr1(pkg, timeout=2, verbose=False)
if reply[TCP].flags == 0x12:
print(f'端口 {port} 开放')
except:
pass
if __name__ == '__main__':
scapy_port('192.168.214.131') #目标IP