python使用scapy模塊實現(xiàn)ARP掃描的過程
前言
上篇文章主要寫了利用scapy實現(xiàn)ping掃描,這篇文章主要是利用scapy模塊實現(xiàn)內(nèi)網(wǎng)ARP掃描
實現(xiàn)過程
上篇文章中介紹了通過scapy來偽造包,那么ARP包的偽造過程這里不再詳述,說一工具的簡單整體流程
1.首先,實現(xiàn)單個IP的ARP包的發(fā)送和接收
2.其次,實現(xiàn)多進(jìn)程同時多個ARP包的發(fā)送和接收
整體的實現(xiàn)流程與上個腳本的實現(xiàn)較為相似
單進(jìn)程ARP包
包的構(gòu)造:
def scapy_arp_one(ip_address, queue=None): Packet = Ether(dst='FF:FF:FF:FF:FF:FF') / ARP(op=1, hwdst='00:00:00:00:00:00:', pdst=ip_address)
請求包的發(fā)送:
arp = srp(Packet, timeout=0.2, verbose=False)
返回包的接收
try: reply_list = arp[0].res if queue is None: return reply_list[0][1].getlayer(ARP).fields['hwsrc'] else: queue.out((ip_address, reply_list[0][1].getlayer(ARP).fields['hwsrc'])) except: return
這里采用隊列的模式,主要是為了后面多進(jìn)程同時發(fā)送做準(zhǔn)備,涉及到ARP包的接受。這里我簡單描述一下ARP包接受的情況,當(dāng)我們發(fā)送ARP廣播包的之后,我們就會接受到一個返回包。所以正確的情況就是,一個發(fā)送包一個接收包,一個發(fā)送包一個接收包,所以這里采用隊列的方式,一個一個IP地址的接收,并使用列表進(jìn)行存儲,然后過濾ARP把內(nèi)容,接收ARP中的硬件MAC地址
完整代碼:
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # Author:Ameng, jlx-love.com from scapy.all import * import sys def scapy_arp_one(ip_address, queue=None): Packet = Ether(dst='FF:FF:FF:FF:FF:FF') / ARP(op=1, hwdst='00:00:00:00:00:00:', pdst=ip_address) arp = srp(Packet, timeout=0.2, verbose=False) try: reply_list = arp[0].res if queue is None: return reply_list[0][1].getlayer(ARP).fields['hwsrc'] else: queue.out((ip_address, reply_list[0][1].getlayer(ARP).fields['hwsrc'])) except: return if __name__ == '__main__': scapy_arp_one(sys.argv[1])
進(jìn)一步完善
那么既然我們已經(jīng)了解了使用ARP進(jìn)行基本的發(fā)包和收包,那么我們接下來就來編寫一個能夠掃描整個網(wǎng)段的ARP檢測
理一下思路,首先,我們需要將ip地址編程一個網(wǎng)段中的所有ip,可以借助ipaddress模塊實現(xiàn),其次我們知道我們接收響應(yīng)包是一對的,所以我們可以從一對一對的響應(yīng)包中接收我們需要的目標(biāo)IP和源MAC地址,其次就是優(yōu)化代碼,輸出結(jié)果,這里我不再采用sys模塊進(jìn)行傳參,而是采用一個新的模塊optparse,具體使用方法可以百度百科
完整代碼:
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # Author:Ameng, jlx-love.com import time from scapy.all import * import ipaddress import optparse def scapy_arp_scan(network, ifname): net = ipaddress.ip_network(network) ip_addr = [] for ip in net: ip = str(ip) ip_addr.append(ip) time.sleep(1) Packet = Ether(dst='FF:FF:FF:FF:FF:FF')/ARP(op=1, hwdst='00:00:00:00:00:00', pdst=ip_addr) arp = srp(Packet, iface = ifname, timeout = 1, verbose = False) arp_list = arp[0].res IP_MAC_LIST = [] for n in range(len(arp_list)): IP = arp_list[n][1][1].fields['psrc'] MAC = arp_list[n][1][1].fields['hwsrc'] IP_MAC = [IP, MAC] IP_MAC_LIST.append(IP_MAC) return IP_MAC_LIST if __name__ == '__main__': t1 = time.time() parser = optparse.OptionParser('用法:\n python3 scapy_arp_scan.py --network 掃描網(wǎng)段 --ifname 網(wǎng)卡名稱') parser.add_option('--network', dest = 'network', type = 'string', help = '掃描網(wǎng)段') parser.add_option('--ifname', dest = 'ifname', type = 'string', help = '網(wǎng)卡名稱') (options, args) = parser.parse_args() network = options.network ifname = options.ifname if network == None or ifname == None: print(parser.usage) else: active_ip_mac = scapy_arp_scan(network, ifname) print('存活的IP地址及對應(yīng)MAC:') for ip, mac in active_ip_mac: print(ip, mac) t2 = time.time() print('所用時間為:{}'.format(int(t2 - t1)))
運行結(jié)果
到此這篇關(guān)于python使用scapy模塊實現(xiàn)ARP掃描的過程的文章就介紹到這了,更多相關(guān)python實現(xiàn)ARP掃描內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的
這篇文章主要介紹了Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04詳解python OpenCV學(xué)習(xí)筆記之直方圖均衡化
本篇文章主要介紹了詳解python OpenCV學(xué)習(xí)筆記之直方圖均衡化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02詳解python opencv、scikit-image和PIL圖像處理庫比較
這篇文章主要介紹了詳解python opencv、scikit-image和PIL圖像處理庫比較,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12