python+scapy实现扫描工具(扫描主机、端口)

2023-11-04

python+scapy实现扫描工具(扫描主机、端口)

需要下载的库

可以直接通过pip下载

pip install scapy

扫描工具功能

我这里代码是在kali-linux里面运行的,向在通一虚拟内网中的虚拟靶机metasploitable2-linux(我这里的IP地址为192.168.44.130,其中下面测试的80、53端口都是开放的,而8080、123端口关闭的)发送包实现扫描的作用(并且是在虚拟靶机防火墙关闭的情况下调试的,当防火墙开启时,基本上发送的包都是没有响应的)。
扫描工具功能(前两个扫描主机发现,后面扫描主机端口发现。):ARP扫描、ICMP扫描、SYN扫描、XMAS扫描、FIN扫描、NULL扫描、ACK扫描、UDP扫描(由于ACK、UDP扫描有些问题,就不发了)。
别忘了引入scapy库:from scapy.all import

ARP扫描

本机向目标主机发送ARP请求:
在这里插入图片描述
如果目标主机会送响应,则目标主机在运行。
在这里插入图片描述
如果未收到响应,则目标主机可能关闭。
源代码:

#ARP scanning
def arp_scan(ip):
	p=ARP(pdst=ip)
	ans=sr1(p,timeout=1)
	if ans !=None:
		ans.display()
		print(ip,"host is up.")
	else:
		print(ip,"host is down.")

运行结果:
在这里插入图片描述

ICMP扫描

本机向目标主机发送ICMP回送请求(也就是平常用的ping命令):
本机向目标主机发送ICMP会送请求,如果目标主机回送响应,则目标主机是在运行的。
如果目标主机回送响应,则目标主机是在运行的。
在这里插入图片描述如果未收到响应,则目标主机可能关闭。
源代码:

#ICMP scanning
def icmp_scan(ip):
	p=IP(dst=ip)/ICMP()
	ans=sr1(p,timeout=1)
	if ans !=None:
		ans.display()
		print(ip,"host is up.")
	else:
		print(ip,"host is down.")

运行结果
在这里插入图片描述

SYN扫描

本机向目标主机的目标端口发送包含SYN请求建立连接的TCP包
在这里插入图片描述
如果收到SYN/ACK连接响应,则目标主机的目标端口开放。
在这里插入图片描述
如果收到RST(表明出现严重差错(如由于主机崩溃或其他原因),必须释放连接,然后再重建传输连接,其还用来拒绝一个非法的报文段或拒绝打开一个连接。)的回应,说明目标端口关闭。
源代码:

#SYN scanning
def syn_scan(ip,port):
	p=IP(dst=ip)/TCP(dport=int(port))
	ans=sr1(p,timeout=1,verbose=1)
	if ans[TCP].flags=='SA':
		print(ip,"port",port,"is open.")
	else:
		print(ip,"port",port,"is closed.")

运行结果:
在这里插入图片描述

FIN扫描

本机向目标主机的目标端口发送包含FIN(终止比特,用来释放连接)=1的TCP包,以此释放传输连接。
在这里插入图片描述
如果没有收到回应,则说明目标主机的目标端口开放或被过滤。
在这里插入图片描述
如果收到RST的响应,则说明目标端口关闭。
源代码:

#FIN scanning
def fin_scan(ip,port):
	p=IP(dst=ip)/TCP(dport=int(port),flags="F")
	ans=sr1(p,timeout=1,verbose=1)
	if ans==None:
		print(ip,"port",port,"is open.")
	elif ans!=None and ans[TCP].flags=='RA':
		ans.display()
		print(ip,"port",port,"is closed.")

运行结果:
在这里插入图片描述
在这里插入图片描述

XMAS扫描

XMAS扫描和NULL扫描是FIN扫描的两个变种,XMAS扫描打开FIN URG ACK PSH RST SYN标记并且全部置1。(原理和SYN差不多,就不贴图了。)

#XMAS scanning
def xmas_scan(ip,port):
	p=IP(dst=ip)/TCP(dport=int(port),flags="FPU")
	ans=sr1(p,timeout=1,verbose=1)
	if ans==None:
		print(ip,"port",port,"is open.")
	elif ans!=None and ans[TCP].flags=='RA':
		ans.display()
		print(ip,"port",port,"is closed.")

运行结果:
在这里插入图片描述
在这里插入图片描述

NULL扫描

NULL扫描关闭所有标记,并置为0。

运行结果:
在这里插入图片描述
在这里插入图片描述

主函数

def main():
	choice=input("Choose one scanning to discover host or port:\nA.ARP B.ICMP C.SYN D.XMAS\nE.FIN F.NULL\n")
	if choice=='A':
		ip=input("Input ip address:")
		arp_scan(ip)
	elif choice=='B':
		ip=input("Input ip address:")
		icmp_scan(ip)
	elif choice=='C':
		ip,port=input("Input ip address and port: ").split()
		syn_scan(ip,port)
	elif choice=='D':
		ip,port=input("Input ip address and port: ").split()
		xmas_scan(ip,port)
	elif choice=='E':
		ip,port=input("Input ip address and port: ").split()
		fin_scan(ip,port)
	elif choice=='F':
		ip,port=input("Input ip address and port: ").split()
		null_scan(ip,port)
	print()

输入A~H选择一种扫描模式,然后根据提示输入IP地址(和端口)(请忽略掉G、H,因为这两个扫描有些问题就不放上来了。):
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python+scapy实现扫描工具(扫描主机、端口) 的相关文章

随机推荐