先给出代码
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from scapy.all import *
import argparse
import logging
from scapy.layers.inet import IP, TCP
from scapy.sendrecv import sr1
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.ui=uic.loadUi("./spec_port_scan.ui")
self.ip=self.ui.lineEdit
self.port=self.ui.lineEdit_2
self.textBrowser=self.ui.textBrowser
start=self.ui.pushButton
start.clicked.connect(self.begin)
def tcpScan(self,a,b):
# S 代表发送SYN报文
print("SSSSSSS")
def begin(self):
self.ipp=self.ip.text()
self.portt=self.port.text().split(' ')
print(self.ipp,self.portt)
for p in self.portt:
port = int(p)
print(type(port))
self.target = self.ipp
send = sr1(IP(dst=self.target) / TCP(dport=port, flags="S"), timeout=2, verbose=0)
if (send is None):
self.textBrowser.append("ERROR")
self.textBrowser.repaint()
continue
# 如果是TCP数据包
elif send.haslayer("TCP"):
# 是否是 SYN+ACK 应答
if send["TCP"].flags == "SA":
# 发送ACK+RST数据包完成三次握手
# send_1 = sr1(IP(dst=target) / TCP(dport=port, flags="AR"), timeout=2, verbose=0)
self.textBrowser.append( "[+] 扫描主机: %-13s 端口: %-5s 开放" % (self.target, port))
self.textBrowser.repaint()
elif send["TCP"].flags == "RA":
self.textBrowser.append("[+] 扫描主机: %-13s 端口: %-5s 关闭" % (self.target, port))
self.textBrowser.repaint()
#self.textBrowser.repaint()
#print(self.a)
if __name__=='__main__':
app=QApplication(sys.argv)
w=MyWindow()
w.ui.show()
app.exec()
实现做好ui 界面 后与python相连接
导入 相应的扫描程序 下面给出:有点缝合怪的感觉
from scapy.all import *
import argparse
import logging
from scapy.layers.inet import IP, TCP
from scapy.sendrecv import sr1
def tcpScan(target,ports):
for port in ports:
# S 代表发送SYN报文
send=sr1(IP(dst=target)/TCP(dport=port,flags="S"),timeout=2,verbose=0)
if (send is None):
continue
# 如果是TCP数据包
elif send.haslayer("TCP"):
# 是否是 SYN+ACK 应答
if send["TCP"].flags == "SA":
# 发送ACK+RST数据包完成三次握手
send_1 = sr1(IP(dst=target) / TCP(dport=port, flags="AR"), timeout=2, verbose=0)
print("[+] 扫描主机: %-13s 端口: %-5s 开放" %(target,port))
elif send["TCP"].flags == "RA":
print("[+] 扫描主机: %-13s 端口: %-5s 关闭" %(target,port))
if __name__ == "__main__":
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
# 使用方式: main.py -H 192.168.1.10 -p 80,8080,443,445
parser = argparse.ArgumentParser()
parser.add_argument("-H","--host",dest="host",help="输入一个被攻击主机IP地址")
parser.add_argument("-p","--port",dest="port",help="输入端口列表 [80,443,135]")
args = parser.parse_args()
if args.host and args.port:
tcpScan(args.host,eval(args.port))
else:
parser.print_help()