大家下午好,
我的编码技能还不够我自己进行套接字编程所需的水平,我想知道这里有人可以帮助我解决我所需要的吗?这很简单:
我想将“虚拟” PIM加入数据包发送到相邻设备,而不必费心维护任何PIM邻接关系/(hellos)。理想情况下,我想结束一个接受一些参数的函数:
def send_dummy_join(group, rp, source_ip):
// send 1 join every time this function is called
理想情况下,我需要使用python3而不是外部模块(例如scapy)来运行它
我还有一个不太了解如何解决的问题是,制作一个源地址不属于系统任何网络接口的数据包。我知道船长可以做到这一点,我只是不太了解如何自己制作。
谢谢您的帮助。非常感谢!
一些外部信息。
http://www.networksorcery.com/enp/protocol/pim.htm
我很可能会(虽然完全正确;;-])被公开私刑以获取以下代码,但是作为一名网络工程师,另一人请检查以下代码。
需要记住的几点-您需要先“建立邻接关系”(交换一些hello数据包),否则上游路由器将忽略您的PIM连接(因此添加了虚拟hello功能)。
此外,在发送PIM加入时,“上游邻居IP”也是必需的。
import socket
from struct import pack, iter_unpack
from functools import reduce
def checksum(data):
sum = reduce(lambda x, y: x + y, iter_unpack("!H",data))
s = reduce(lambda x, y: x + y, sum)
while (s >> 16):
s = (s & 0xFFFF) + (s >> 16)
s = ~s & 0xffff
return s
def send_dummy_hello():
PIM_GROUP = '224.0.0.13'
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_PIM)
my_packet=pack('!BBH3H4H4H4H', 32, 0, 24707, 1, 2, 105, 20, 4, 16121,
16078, 19, 4, 0, 1, 21, 4, 256, 0)
sock.sendto(my_packet, (PIM_GROUP,0))
def send_dummy_join(group, rp_addr, neighbor_ip):
PIM_GROUP = '224.0.0.13'
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_PIM)
pim_ver = 35
pim_res = 0
pktchksum = 0
pim_opts = 256
neighbor = socket.inet_aton(neighbor_ip)
ngroups = 1
holdtime = 210
subnet = socket.inet_aton('1.0.0.32')
group_a = socket.inet_aton(group)
num_joins = 1
num_prunes = 0
rrp_subnet = socket.inet_aton('1.0.7.32')
rp_address = socket.inet_aton(rp_addr)
my_packet = pack('!BB2H4sHH4s4sHH4s4s', pim_ver, pim_res, pktchksum, pim_opts,
neighbor, ngroups, holdtime, subnet, group_a, num_joins, num_prunes,
rrp_subnet, rp_address)
pktchksum = checksum(my_packet)
my_packet = pack('!BB2H4sHH4s4sHH4s4s', pim_ver, pim_res, pktchksum, pim_opts,
neighbor, ngroups, holdtime, subnet, group_a, num_joins, num_prunes,
rrp_subnet, rp_address)
sock.sendto(my_packet, (PIM_GROUP,0))
print (f'PIM Join sent to {neighbor_ip} for {group} & RP {rp_addr}')
>>>send_dummy_hello()
>>>send_dummy_join('239.10.20.30','192.168.0.1','10.0.0.102')
我用EVE-NG进行了测试,(很神奇!)起作用了:-)
编辑:
现在对代码进行编辑以支持标头校验和计算。我认为这已经完成! :-)