import socket
MCAST_GRP = '224.1.1.2'
MCAST_PORT = 5007
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# specific interface ip and bind to socket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton("192.168.20.1"))
sock.sendto("this is message", (MCAST_GRP, MCAST_PORT))
==== RAW , IGMP
import socket
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x8100)
s.bind(('VLAN1', 0x8100))
res=''
temp='01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 01 08 00 46 00 00 20 00 01 00 00 01 02 36 4C C0 A8 0a 7b EA 00 43 67 94 04 00 00 16 00 BC 97 EA 00 43 67'
for i in temp.split(' '):
res+=chr(int(i, 16))
s.send(res)
範例: 往 192.168.10.10 打 udp packet , src ip 是 192.168.1.0 .. 遞增 1024,UDP dest port 是 8001~8010, UDP src port 是 10001 ~ 10010, payload 是 'hello world',packet 從 eth0 出去 import struct, socket from scapy.all import * #在這邊用 all 速度會變慢一些 ip2int = lambda ipstr: struct.unpack('!I', socket.inet_aton(ipstr))[0] int2ip = lambda n: socket.inet_ntoa(struct.pack('!I', n)) src_ip_base="192.168.1.0" src_ip_list=[ int2ip(a+ip2int("192.168.1.0")) for a in range(1024)] payload='hello world' packet=Ether(dst="00:e0:4c:68:00:66",src="00:1e:8c:74:b9:47")/ \ IP(dst="192.168.10.10",src=src_ip_list)/ \ UDP(sport=[i+10001 for i in range(10)],dport=[i+8001 for i in range(10)])/ \ payload sendp(packet,iface="eth0")
留言