Python

 

 

 

 

Python - Socket - Raw IPv6

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

 

Example 01 > ========================================================

 

In this example, I only created the sendor script and I didn't create the reciever script. I connected two PC with LAN cable and ran this script on sendor PC and used Wireshark on reciever PC to confirm the packet reception.

 

# import necessary package

import socket

import binascii

 

# destination ip. You have the specify the destination IP address which physically exists and connected to the

# sender PC

dst = 'fe80::906d:ef3f:7813:33b6' '

 

# protocol number. This value will be set in NextHeader field in IPv6 Header. .

# possible types of NextHeader seems to be smaller than types of protocols in IPv4.

# I will try to use 253 (Unknown Type), but the resulting packet still put 3A(58) in Next Header field and put error

# code in ICMP payload. I don't know if this is due to Python or Windows or NIC.

proto = 58  # ICMP

 

# A minimal ICMP6 ping request message

bArray = b'\x80\0\0\0\0\0\0\0'

 

# Open a raw socket.

send_socket = socket.socket(socket.AF_INET6, socket.SOCK_RAW,proto)

send_socket.sendto(bArray,(dst,0))

 

 

Result :----------------------------------------------------------------------------

 

< Packet captured by Wireshark on Sender PC >

 

No.     Time               Source                Destination           Protocol Length Info

      1 03:40:08.379684000 fe80::a181:517e:3acf:63c5 fe80::906d:ef3f:7813:33b6 ICMPv6   62     Echo (ping) request id=0x0000, seq=0, hop limit=128 (reply in 2)

 

Frame 1: 62 bytes on wire (496 bits), 62 bytes captured (496 bits) on interface 0

    Interface id: 0

    Encapsulation type: Ethernet (1)

    Arrival Time: Mar 31, 2016 23:40:08.379684000 Eastern Daylight Time

    [Time shift for this packet: 0.000000000 seconds]

    Epoch Time: 1459482008.379684000 seconds

    [Time delta from previous captured frame: 0.000000000 seconds]

    [Time delta from previous displayed frame: 0.000000000 seconds]

    [Time since reference or first frame: 0.000000000 seconds]

    Frame Number: 1

    Frame Length: 62 bytes (496 bits)

    Capture Length: 62 bytes (496 bits)

    [Frame is marked: False]

    [Frame is ignored: False]

    [Protocols in frame: eth:ipv6:icmpv6:data]

    [Coloring Rule Name: ICMP]

    [Coloring Rule String: icmp || icmpv6]

Ethernet II, Src: DellPcba_28:8d:92 (f0:1f:af:28:8d:92), Dst: Anritsu_06:41:e1 (00:00:91:06:41:e1)

    Destination: Anritsu_06:41:e1 (00:00:91:06:41:e1)

        Address: Anritsu_06:41:e1 (00:00:91:06:41:e1)

        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)

        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)

    Source: DellPcba_28:8d:92 (f0:1f:af:28:8d:92)

        Address: DellPcba_28:8d:92 (f0:1f:af:28:8d:92)

        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)

        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)

    Type: IPv6 (0x86dd)

Internet Protocol Version 6, Src: fe80::a181:517e:3acf:63c5 (fe80::a181:517e:3acf:63c5), Dst: fe80::906d:ef3f:7813:33b6 (fe80::906d:ef3f:7813:33b6)

    0110 .... = Version: 6

        [0110 .... = This field makes the filter "ip.version == 6" possible: 6]

    .... 0000 0000 .... .... .... .... .... = Traffic class: 0x00000000

        .... 0000 00.. .... .... .... .... .... = Differentiated Services Field: Default (0x00000000)

        .... .... ..0. .... .... .... .... .... = ECN-Capable Transport (ECT): Not set

        .... .... ...0 .... .... .... .... .... = ECN-CE: Not set

    .... .... .... 0000 0000 0000 0000 0000 = Flowlabel: 0x00000000

    Payload length: 8

    Next header: ICMPv6 (58)

    Hop limit: 128

    Source: fe80::a181:517e:3acf:63c5 (fe80::a181:517e:3acf:63c5)

    Destination: fe80::906d:ef3f:7813:33b6 (fe80::906d:ef3f:7813:33b6)

    [Source GeoIP: Unknown]

    [Destination GeoIP: Unknown]

Internet Control Message Protocol v6

    Type: Echo (ping) request (128)

    Code: 0

    Checksum: 0xc5af [correct]

    Identifier: 0x0000

    Sequence: 0

    [Response In: 2]

 

0000  00 00 91 06 41 e1 f0 1f af 28 8d 92 86 dd 60 00   ....A....(....`.

0010  00 00 00 08 3a 80 fe 80 00 00 00 00 00 00 a1 81   ....:...........

0020  51 7e 3a cf 63 c5 fe 80 00 00 00 00 00 00 90 6d   Q~:.c..........m

0030  ef 3f 78 13 33 b6 80 00 c5 af 00 00 00 00         .?x.3.........

 

 

No.     Time               Source                Destination           Protocol Length Info

      2 03:40:08.379956000 fe80::906d:ef3f:7813:33b6 fe80::a181:517e:3acf:63c5 ICMPv6   62     Echo (ping) reply id=0x0000, seq=0, hop limit=128 (request in 1)

 

Frame 2: 62 bytes on wire (496 bits), 62 bytes captured (496 bits) on interface 0

    Interface id: 0

    Encapsulation type: Ethernet (1)

    Arrival Time: Mar 31, 2016 23:40:08.379956000 Eastern Daylight Time

    [Time shift for this packet: 0.000000000 seconds]

    Epoch Time: 1459482008.379956000 seconds

    [Time delta from previous captured frame: 0.000272000 seconds]

    [Time delta from previous displayed frame: 0.000272000 seconds]

    [Time since reference or first frame: 0.000272000 seconds]

    Frame Number: 2

    Frame Length: 62 bytes (496 bits)

    Capture Length: 62 bytes (496 bits)

    [Frame is marked: False]

    [Frame is ignored: False]

    [Protocols in frame: eth:ipv6:icmpv6:data]

    [Coloring Rule Name: ICMP]

    [Coloring Rule String: icmp || icmpv6]

Ethernet II, Src: Anritsu_06:41:e1 (00:00:91:06:41:e1), Dst: DellPcba_28:8d:92 (f0:1f:af:28:8d:92)

    Destination: DellPcba_28:8d:92 (f0:1f:af:28:8d:92)

        Address: DellPcba_28:8d:92 (f0:1f:af:28:8d:92)

        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)

        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)

    Source: Anritsu_06:41:e1 (00:00:91:06:41:e1)

        Address: Anritsu_06:41:e1 (00:00:91:06:41:e1)

        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)

        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)

    Type: IPv6 (0x86dd)

Internet Protocol Version 6, Src: fe80::906d:ef3f:7813:33b6 (fe80::906d:ef3f:7813:33b6), Dst: fe80::a181:517e:3acf:63c5 (fe80::a181:517e:3acf:63c5)

    0110 .... = Version: 6

        [0110 .... = This field makes the filter "ip.version == 6" possible: 6]

    .... 0000 0000 .... .... .... .... .... = Traffic class: 0x00000000

        .... 0000 00.. .... .... .... .... .... = Differentiated Services Field: Default (0x00000000)

        .... .... ..0. .... .... .... .... .... = ECN-Capable Transport (ECT): Not set

        .... .... ...0 .... .... .... .... .... = ECN-CE: Not set

    .... .... .... 0000 0000 0000 0000 0000 = Flowlabel: 0x00000000

    Payload length: 8

    Next header: ICMPv6 (58)

    Hop limit: 128

    Source: fe80::906d:ef3f:7813:33b6 (fe80::906d:ef3f:7813:33b6)

    Destination: fe80::a181:517e:3acf:63c5 (fe80::a181:517e:3acf:63c5)

    [Source GeoIP: Unknown]

    [Destination GeoIP: Unknown]

Internet Control Message Protocol v6

    Type: Echo (ping) reply (129)

    Code: 0

    Checksum: 0xc4af [correct]

    Identifier: 0x0000

    Sequence: 0

    [Response To: 1]

    [Response Time: 0.272 ms]

 

0000  f0 1f af 28 8d 92 00 00 91 06 41 e1 86 dd 60 00   ...(......A...`.

0010  00 00 00 08 3a 80 fe 80 00 00 00 00 00 00 90 6d   ....:..........m

0020  ef 3f 78 13 33 b6 fe 80 00 00 00 00 00 00 a1 81   .?x.3...........

0030  51 7e 3a cf 63 c5 81 00 c4 af 00 00 00 00         Q~:.c.........