python如何通过以太网发送指令_用scapy在python中编写一个以太网桥
我想做這樣的事情:
10.1.1.0/24 10.1.2.0/24
------------ ------------ ------------
| | | | | |
| | | | | |
| A d ------- e B f ------- g C |
| | | | | |
| | | | | |
------------ ------------ ------------
d e f g
10.1.1.1 10.1.1.2 10.1.2.1 10.1.2.2
這樣Acan就可以通過B向C發送數據包了.
我嘗試通過在B上運行一個可以嗅探端口e和f的scapy程序來構建這個東西,并且在每種情況下修改數據包中的目標IP和MAC地址,然后通過另一個接口發送它.就像是:
my_macs = [get_if_hwaddr(i) for i in get_if_list()]
pktcnt = 0
dest_mac_address = discover_mac_for_ip(dest_ip) #
output_mac = get_if_hwaddr(output_interface)
def process_packet(pkt):
# ignore packets that were sent from one of our own interfaces
if pkt[Ether].src in my_macs:
return
pktcnt = 1
p = pkt.copy()
# if this packet has an IP layer, change the dst field
# to our final destination
if IP in p:
p[IP].dst = dest_ip
# if this packet has an ethernet layer, change the dst field
# to our final destination. We have to worry about this since
# we're using sendp (rather than send) to send the packet. We
# also don't fiddle with it if it's a broadcast address.
if Ether in p and p[Ether].dst != 'ff:ff:ff:ff:ff:ff':
p[Ether].dst = dest_mac_address
p[Ether].src = output_mac
# use sendp to avoid ARP'ing and stuff
sendp(p, iface=output_interface)
sniff(iface=input_interface, prn=process_packet)
然而,當我運行這個東西(完整源碼here)時,各種瘋狂的事情開始發生……一些數據包通過,我甚至得到一些響應(用ping測試)但是有一些類型的反饋循環導致要發送一堆重復的數據包……
有什么想法在這里發生了什么?嘗試這樣做是否很瘋狂?
我有點懷疑反饋循環是由于B正在對數據包進行自己的一些處理這一事實…有什么方法可以阻止操作系統在我嗅探后處理數據包?
解決方法:
這樣做有點瘋狂,但花費你的時間并不是一種糟糕的方式.你會學到很多有趣的東西.然而,您可能想要考慮將數據包掛得更低 – 我不認為scapy能夠實際攔截數據包 – 所有libpcap都設置為promisc并讓你看到一切,所以你和內核都得到了相同的東西.如果您轉身并重新發送,那可能是您的數據包風暴的原因.
但是,您可以設置一些創意防火墻規則,將每個接口彼此分開并以此方式處理數據包,或者使用類似divert sockets的內容來實際將數據包從內核中分離出來,以便您可以使用它們.來源:https://www.icode9.com/content-1-435101.html
總結
以上是生活随笔為你收集整理的python如何通过以太网发送指令_用scapy在python中编写一个以太网桥的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 商业承兑汇票计入什么科目
- 下一篇: 公积金几个月可以提现