How to send an IP packet in Python?

Discussion in 'Python' started by yegorov-p, Dec 2, 2010.

  1. yegorov-p

    yegorov-p Guest

    Hello.

    I have sniffed some packet and now I would like to send it with the
    help of python. It's some simple IGMP packet with VLAN tag.
    (01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00 20 00
    01 00 00 01 02 36 4C C0 A8 00 7B EA 00 43 67 94 04 00 00 16 00 BC 97
    EA 00 43 67)

    At first I wrote that:

    dst = '234.0.67.103'
    # Open a raw socket.
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW,2)
    res=''
    temp='01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00
    20 00 01 00 00 01 02 36 4C C0 A8 00 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))
    print res
    s.sendto(res, (dst, 0))

    But for some reason python send that:
    0x0000 01 00 5E 00 43 67 00 02-B3 C8 7F 44 08 00 45
    00 ..^.Cg..іИD..E.
    0x0010 00 46 07 06 00 00 01 02-C4 25 C0 A8 00 7B EA 00 .F......Д
    %ÐÐ.{к.
    0x0020 43 67 01 00 5E 00 43 67-00 02 B3 C8 7F 44 81 00
    Cg..^.Cg..іИDЃ.
    0x0030 00 DE 08 00 46 00 00 20-00 01 00 00 01 02 36
    4C .Ю..F.. ......6L
    0x0040 C0 A8 00 7B EA 00 43 67-94 04 00 00 16 00 BC 97 ÐÐ.
    {к.Cgâ€.....ј—
    0x0050 EA 00 43
    67 к.Cg

    As you can see, python ignores my headers and creates its own.

    I discussed that problem on stackoverflow.com and one user told me to
    try that:

    import socket

    s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x8100)
    s.bind(('eth0', 0x8100))

    res=''
    temp='01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00
    20 00 01 00 00 01 02 36 4C C0 A8 00 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)

    But under Windows, AF_PACKET is said to be undefined. =( I tried to
    replace it with AF_INET, but now python tells me that that protocol
    (0x8100) is not supported. 0x8100 is IEEE 802.1Q and I just don't
    understand, why it isn't supported. I can generate packet with VLAN
    tag inside and send it with the help of pierf, for example. In fact, I
    can generate and send the packet from my example with pierf and it
    runs just fine. =) I've found a topic with the same problem (http://
    www.computing.net/answers/programming/python-windows-sockets/11884.html)

    So, is there any way to somehow send a packet with ready headers and
    data under Windows with the help of python?
     
    yegorov-p, Dec 2, 2010
    #1
    1. Advertising

  2. yegorov-p

    Nobody Guest

    On Thu, 02 Dec 2010 03:12:42 -0800, yegorov-p wrote:

    > I have sniffed some packet and now I would like to send it with the
    > help of python.


    > But for some reason python send that:


    > As you can see, python ignores my headers and creates its own.


    It isn't Python doing that, but the OS. At least on Unix, you need
    setsockopt(IP_HDRINCL) to tell the OS that the packet includes a header.
    It appears that Windows supports the same option:

    http://msdn.microsoft.com/en-us/library/ms738586(v=VS.85).aspx
     
    Nobody, Dec 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom St Denis

    Re: How to format/send/receive UDP packet in C?

    Tom St Denis, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    3,114
    Tom St Denis
    Jun 24, 2003
  2. Matt Whiteley

    Using Python to send a DNS update packet

    Matt Whiteley, Dec 24, 2003, in forum: Python
    Replies:
    3
    Views:
    2,131
    John P. Speno
    Dec 28, 2003
  3. Gabriel Genellina

    Re: Using Python to send a DNS update packet

    Gabriel Genellina, Dec 24, 2003, in forum: Python
    Replies:
    0
    Views:
    397
    Gabriel Genellina
    Dec 24, 2003
  4. Li Han
    Replies:
    2
    Views:
    531
    bobicanprogram
    Feb 9, 2009
  5. Gelonida N
    Replies:
    4
    Views:
    947
    Gelonida N
    Sep 11, 2011
Loading...

Share This Page