low level networking in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxim Veksler

    #1

    low level networking in python

    Hello,

    I wish to do some low level network stuff using python.

    I've googled somewhat and came up with pylibpcap[1], trouble is I
    can't compile it on my Ubuntu 6.10 workstation. Can someone please
    suggest a way to read some bits from random ports? I'm looking to
    write a simple pen-testing tool that would try from one side
    connecting to ports and from the other side sniff traffic to see on
    what ports traffic is coming.

    For pylibpcap I'm getting:

    """
    hq4ever@lucky:~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
    setup.py build
    running build
    running build_ext
    building '_pcapmodule' extension
    swig -python -shadow -ISWIG -o pcap.c pcap.i
    pcap.i:72: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    pcap.i:77: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    pcap.i:82: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    /usr/bin/python ./build-tools/docify.py pcap.c
    /usr/bin/python ./build-tools/docify-shadow.py pcap.py
    Traceback (most recent call last):
    File "./build-tools/docify-shadow.py", line 30, in ?
    raise 'source file doesn\'t look like swigged shadow class code'
    source file doesn't look like swigged shadow class code
    error: command '/usr/bin/python' failed with exit status 1
    """

    [1] http://pylibpcap.sourceforge.net/
    [2] http://py.vaults.ca/apyllo.py/126307487

    --
    Cheers,
    Maxim Veksler

    "Free as in Freedom" - Do u GNU ?
  • kyosohma@gmail.com

    #2
    Re: low level networking in python

    On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
    Hello,
    >
    I wish to do some low level network stuff using python.
    >
    I've googled somewhat and came up with pylibpcap[1], trouble is I
    can't compile it on my Ubuntu 6.10 workstation. Can someone please
    suggest a way to read some bits from random ports? I'm looking to
    write a simple pen-testing tool that would try from one side
    connecting to ports and from the other side sniff traffic to see on
    what ports traffic is coming.
    >
    For pylibpcap I'm getting:
    >
    """
    hq4ever@lucky:~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
    setup.py build
    running build
    running build_ext
    building '_pcapmodule' extension
    swig -python -shadow -ISWIG -o pcap.c pcap.i
    pcap.i:72: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    pcap.i:77: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    pcap.i:82: Warning(124): Specifying the language name in %typemap is
    deprecated - use #ifdef SWIG<LANGinstea d.
    /usr/bin/python ./build-tools/docify.py pcap.c
    /usr/bin/python ./build-tools/docify-shadow.py pcap.py
    Traceback (most recent call last):
    File "./build-tools/docify-shadow.py", line 30, in ?
    raise 'source file doesn\'t look like swigged shadow class code'
    source file doesn't look like swigged shadow class code
    error: command '/usr/bin/python' failed with exit status 1
    """
    >
    [1]http://pylibpcap.sourc eforge.net/
    [2]http://py.vaults.ca/apyllo.py/126307487
    >
    --
    Cheers,
    Maxim Veksler
    >
    "Free as in Freedom" - Do u GNU ?
    I would assume you could use the socket module. This post details
    someone else who opened ports with Python:



    Here's another resource using some python servers:



    Finally, a Socket programming howto:


    I'm also told that the Twisted framework is excellent for this sort of
    thing.

    Mike

    Comment

    • Steve Holden

      #3
      Re: low level networking in python

      kyosohma@gmail. com wrote:
      On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
      >Hello,
      >>
      >I wish to do some low level network stuff using python.
      >>
      >I've googled somewhat and came up with pylibpcap[1], trouble is I
      >can't compile it on my Ubuntu 6.10 workstation. Can someone please
      >suggest a way to read some bits from random ports? I'm looking to
      >write a simple pen-testing tool that would try from one side
      >connecting to ports and from the other side sniff traffic to see on
      >what ports traffic is coming.
      >>
      >For pylibpcap I'm getting:
      >>
      >"""
      >hq4ever@lucky: ~/development/personal/pylibpcap/pylibpcap-0.5.1$ python
      >setup.py build
      >running build
      >running build_ext
      >building '_pcapmodule' extension
      >swig -python -shadow -ISWIG -o pcap.c pcap.i
      >pcap.i:72: Warning(124): Specifying the language name in %typemap is
      >deprecated - use #ifdef SWIG<LANGinstea d.
      >pcap.i:77: Warning(124): Specifying the language name in %typemap is
      >deprecated - use #ifdef SWIG<LANGinstea d.
      >pcap.i:82: Warning(124): Specifying the language name in %typemap is
      >deprecated - use #ifdef SWIG<LANGinstea d.
      >/usr/bin/python ./build-tools/docify.py pcap.c
      >/usr/bin/python ./build-tools/docify-shadow.py pcap.py
      >Traceback (most recent call last):
      > File "./build-tools/docify-shadow.py", line 30, in ?
      > raise 'source file doesn\'t look like swigged shadow class code'
      >source file doesn't look like swigged shadow class code
      >error: command '/usr/bin/python' failed with exit status 1
      >"""
      >>
      >[1]http://pylibpcap.sourc eforge.net/
      >[2]http://py.vaults.ca/apyllo.py/126307487
      >>
      >--
      >Cheers,
      >Maxim Veksler
      >>
      >"Free as in Freedom" - Do u GNU ?
      >
      I would assume you could use the socket module. This post details
      someone else who opened ports with Python:
      >

      >
      Here's another resource using some python servers:
      >

      >
      Finally, a Socket programming howto:

      >
      I'm also told that the Twisted framework is excellent for this sort of
      thing.
      >
      The problem with dealing with ports is that you'd have to open each port
      individually. So the OP, I presume, is trying to capture packets
      generically as they arrive at a particular point int he processing cycle
      to avoid that necessity.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://del.icio.us/steve.holden
      Recent Ramblings http://holdenweb.blogspot.com

      Comment

      • Maxim Veksler

        #4
        Re: low level networking in python

        On 3 Apr 2007 08:43:57 -0700, kyosohma@gmail. com <kyosohma@gmail .comwrote:
        On Apr 3, 10:29 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
        Hello,

        I wish to do some low level network stuff using python.

        --
        Cheers,
        Maxim Veksler

        "Free as in Freedom" - Do u GNU ?
        >
        I would assume you could use the socket module. This post details
        someone else who opened ports with Python:
        >

        >
        Here's another resource using some python servers:
        >

        >
        Finally, a Socket programming howto:

        >
        I'm also told that the Twisted framework is excellent for this sort of
        thing.
        >
        Thanks for the heads-up. The sockets howto was great help.

        I'm trying to bind a non-blocking socket, here is my code:
        """
        #!/usr/bin/env python

        import socket, select
        from time import sleep

        s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        s_nb10000.setbl ocking(0)

        s_nb10000.bind( ('192.168.2.106 ', 10002))
        s_nb10000.liste n(5)

        while 1:
        conn, addr = s_nb10000.accep t()
        ready_to_read, ready_to_write, in_error = select.select([conn], [], [], 0)
        print (ready_to_read, ready_to_write, in_error)
        sleep(100)

        s_nb10000.close ()
        """

        And this is the exception I'm getting:
        """
        python non_blocking_so cket.py
        Traceback (most recent call last):
        File "non_blocking_s ocket.py", line 13, in ?
        conn, addr = s_nb10000.accep t()
        File "/usr/lib/python2.4/socket.py", line 161, in accept
        sock, addr = self._sock.acce pt()
        socket.error: (11, 'Resource temporarily unavailable')
        """

        What am I doing wrong here?

        p.s.
        I've looked at twisted before posting this post. I've seen they
        impelement alot of application level protocols but I didn't see much
        treatment for low level "raw" network data, not to mention that it's a
        way way over kill for what I'm asking to achieve. Twisted does have a
        subproject called "Twisted Pair: Low-level networking" but sadly it's
        unmaintained and undocumented.
        Mike
        >
        Maxim.


        --
        Cheers,
        Maxim Veksler

        "Free as in Freedom" - Do u GNU ?

        Comment

        • Irmen de Jong

          #5
          Re: low level networking in python

          Maxim Veksler wrote:
          I'm trying to bind a non-blocking socket, here is my code:
          """
          #!/usr/bin/env python
          >
          import socket, select
          from time import sleep
          >
          s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
          s_nb10000.setbl ocking(0)
          >
          s_nb10000.bind( ('192.168.2.106 ', 10002))
          s_nb10000.liste n(5)
          >
          while 1:
          conn, addr = s_nb10000.accep t()
          ready_to_read, ready_to_write, in_error = select.select([conn], [],
          [], 0)
          print (ready_to_read, ready_to_write, in_error)
          sleep(100)
          >
          s_nb10000.close ()
          """
          >
          And this is the exception I'm getting:
          """
          python non_blocking_so cket.py
          Traceback (most recent call last):
          File "non_blocking_s ocket.py", line 13, in ?
          conn, addr = s_nb10000.accep t()
          File "/usr/lib/python2.4/socket.py", line 161, in accept
          sock, addr = self._sock.acce pt()
          socket.error: (11, 'Resource temporarily unavailable')
          """
          >
          What am I doing wrong here?
          Nothing.
          Any operation on a non-blocking socket that is usually blocking
          (this includes accept(), bind(), connect(), recv with MSG_WAITALL)
          can possibly return a socket.error with errno set to EAGAIN.
          ('resource temporarily unavailable').
          If this happens you should use a select() on the socket to
          wait until it's done with the requested operation.

          --Irmen
          >
          p.s.
          I've looked at twisted before posting this post. I've seen they
          impelement alot of application level protocols but I didn't see much
          treatment for low level "raw" network data, not to mention that it's a
          way way over kill for what I'm asking to achieve. Twisted does have a
          subproject called "Twisted Pair: Low-level networking" but sadly it's
          unmaintained and undocumented.
          >
          >Mike
          >>
          >
          Maxim.
          >
          >

          Comment

          • billiejoex

            #6
            Re: low level networking in python

            I wish to do some low level network stuff using python.
            I've googled somewhat and came up with pylibpcap[1], trouble is I
            can't compile it on my Ubuntu 6.10 workstation.
            I would suggest pcapy:
            http://oss.coresecurity.com/projects/pcapy.html
            Higher level, easier Object-Oriented API and support for multi-
            threading.
            Here's an example showing a simple sniffer app:
            http://oss.coresecurity.com/impacket/sniff.py
            Anyway, what kind of 'low-level network stuff' we're talking about?

            Comment

            • Maxim Veksler

              #7
              Re: low level networking in python

              On 4/4/07, Irmen de Jong <irmen.NOSPAM@x s4all.nlwrote:
              Maxim Veksler wrote:
              >
              I'm trying to bind a non-blocking socket, here is my code:
              """
              #!/usr/bin/env python

              import socket, select
              from time import sleep

              s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
              s_nb10000.setbl ocking(0)

              s_nb10000.bind( ('192.168.2.106 ', 10002))
              s_nb10000.liste n(5)

              while 1:
              conn, addr = s_nb10000.accep t()
              ready_to_read, ready_to_write, in_error = select.select([conn], [],
              [], 0)
              print (ready_to_read, ready_to_write, in_error)
              sleep(100)

              s_nb10000.close ()
              """

              And this is the exception I'm getting:
              """
              python non_blocking_so cket.py
              Traceback (most recent call last):
              File "non_blocking_s ocket.py", line 13, in ?
              conn, addr = s_nb10000.accep t()
              File "/usr/lib/python2.4/socket.py", line 161, in accept
              sock, addr = self._sock.acce pt()
              socket.error: (11, 'Resource temporarily unavailable')
              """

              What am I doing wrong here?
              >
              Nothing.
              Any operation on a non-blocking socket that is usually blocking
              (this includes accept(), bind(), connect(), recv with MSG_WAITALL)
              can possibly return a socket.error with errno set to EAGAIN.
              ('resource temporarily unavailable').
              If this happens you should use a select() on the socket to
              wait until it's done with the requested operation.
              >
              Hello everyone, I would like to thank you all for the helping tips so
              far, with your help I managed to improve the previous code to not give
              the error, I believe it's now working.

              The non blocking echo socket code:
              """
              #!/usr/bin/env python

              import socket, select

              s_nb10000 = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
              s_nb10000.setbl ocking(0)

              s_nb10000.bind( ('0.0.0.0', 10002))
              s_nb10000.liste n(5)

              while 1:
              ready_to_read, ready_to_write, in_error =
              select.select([s_nb10000], [], [], 0)
              if s_nb10000 in ready_to_read:
              conn, addr = s_nb10000.accep t()
              while 1:
              data = conn.recv(1024)
              if not data: break
              conn.send(data)
              conn.close()

              s_nb10000.close ()
              """
              --Irmen
              >
              Maxim.


              --
              Cheers,
              Maxim Veksler

              "Free as in Freedom" - Do u GNU ?

              Comment

              Working...