Using SOCK_STREAM or SOCK_RAW on sending Multicast IPv6

Hey guys I have a problem, I can't seem to find a documentation on sending raw packets on multicast, all examples, solutions and documentation are centered on UDP usage.

I'm using this as an example: http://svn.python.org/projects/stackless/trunk/Demo/sockets/mcast.py

I only care on the sending part which is looks like this when you optimize the code:

import time
import struct
import socket
import sys

MYPORT = 8123
MYGROUP_6 = 'ff15:7079:7468:6f6e:6465:6d6f:6d63:6173'
MYTTL = 1 # Increase to reach other networks
group = MYGROUP_6

if "-s" in sys.argv[1:]:
    sender(group)
addrinfo = socket.getaddrinfo(group, None)[0]

s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

# Set Time-to-live (optional)
ttl_bin = struct.pack('@i', MYTTL)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)

while True:
    data = repr(time.time())
    s.sendto(data + '\0', (addrinfo[4][0], MYPORT))
    time.sleep(1)

Now that sends a UDP in the Multicast. What I want is to convert the

s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

into

s = socket(AF_PACKET, SOCK_RAW, IPPROTO_ICMPV6)

or at least change the socket.SOCK_DGRAM to socket.SOCK_STREAM to send TCP packet.

My goal is to send a ICMPv6 packet which hopefully I can send it raw because I use VLANS but when I change the SOCK_DGRAM I will receive an error like this:

Traceback (most recent call last):
  File "/root/PycharmProjects/SlaacSecuritySolutionv3/ClassS3/SendTest.py", line 20, in <module>
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 92] Protocol not available

the same thing when I change socket.socket into socket.AF_PACKET, socket.SOCK_RAW

Please help, I am stuck here for days now

Upvotes: 2

Views: 1108

Answers (2)

Koikos
Koikos

Reputation: 182

It looks like your problems are caused by not resolved yet issue: https://bugs.python.org/issue6926

Python compilation on windows platform is missing IPPROTO_IPV6 constant. According to RFC3542 that constant should be equal to 41, so just change socket.IPPROTO_IPV6 to 41.

Upvotes: 0

#!/usr/bin/env python
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import time
import socket
import select
import struct
import select
import time
from impacket import ImpactDecoder, ImpactPacket, IP6, ICMP6, version
from impacket import ImpactPacket
from netaddr import *

#~ Definitions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

maddr = ('ff12::4242', 4242)

def ipv6Multicast(host='::1', maddr=maddr):
    haddr = socket.getaddrinfo(host, maddr[1], socket.AF_INET6, socket.IPPROTO_TCP)[0][-1]
    maddr = socket.getaddrinfo(maddr[0], maddr[1], socket.AF_INET6, socket.IPPROTO_TCP)[0][-1]

    sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 5)

    ifn = struct.pack("I", haddr[3])
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, ifn)

    group = socket.inet_pton(socket.AF_INET6, maddr[0]) + ifn
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)

    #sock.bind(haddr)
    sock.setblocking(False)

    return sock, maddr

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Main
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if __name__=='__main__':
    # change host to use your IPv6 address...eth
    sock, maddr = ipv6Multicast(host='fe80::20c:29ff:fe23:8450')

    # send initial message
    msg = 'IPv6 multicast recipie'
    eth = ImpactPacket.Ethernet() 
    print maddr
    while 1:
         sock.sendto(eth.get_packet(),("ff12::4242",0))
         time.sleep(1

This is my solution on sending IPv6 ICMPv6 Packet to the network,I use impacket on building the packet but there are no data yet.

If execute it will send malform ICMPv6 packet

Upvotes: 1

Related Questions