Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Sockets(in Python)
  • chroniccommand
    Posts: 1,389
    [-----------------------]
    Paper: Sockets(in Python)
    Author: Chroniccommand
    [-----------------------]
    Contents:
    1 - Intro
    2 - Network overview
    3 - TCP in python
    4 - Using sockets

    [--Intro--]
    This is a paper I've decided to write for those who don't know much about sockets. I will be going over what is a socket, what types of sockets are there, etc. I will also be using Python for the main language here.

    [--Network overview--]
    So what exactly is a network? Well to put it in a bit easier to understand terms, a network is a connection from one machine to another(and to another and another....). The Internet, is a series of tubes(loljk). The Internet consists of millions of networks connected. Machines in a LAN network talk to each other using a MAC address(A 48-bit serial number), that the computer has assigned to it. I will be using the examples computer A, computer B and computer C. Computers A and B are on the same network. Computer C is a computer in lets say, Taiwan. All computers on the Internet are assigned an IP(Internet Protocol) address. If machine A wants to communicate with machine C in Taiwan, machine A sends to the router first. Then that router sends to another one, to another one, to another one until it reaches computer C's network. The router puts C's MAC address in the packet and sends it out on the local Network. Computer C will see that packet with it's MAC address in it and accept it. Now to explain ports. A port can be a physical port on the computer(Like a USB port). But we're talking about network ports. Ports are essentially processes on a machine that handle connections to that network. Lower ports are usually reserved for more known ports, such as:
    21 [align=center]FTP[/align]
    22 [align=center]SSH[/align]
    23 [align=center]Telnet[/align]
    80 [align=center]HTTP[/align]
    110 [align=center]POP3[/align]

    And there's of course a lot more "famous" ports.

    [--What is a socket--]
    Well what exactly is a socket you ask? Well simply, a socket allows for easy communication over a network. Originally developed by Berkely as a UNIX only feature, sockets are widely used in almost everything now. Think of a socket as a phone call. You dial a number(host), with an extension(port). Then you can use that connection between "phones" to talk to each other and communicate.

    [--TCP(in Python)--]
    So what is TCP? Well it stands for Transmission Control Protocol. TCP is one of the core components of the Internet Protocol Suite. The other is UDP. TCP is more reliable than UDP, but UDP is still used in some cases(I'll get to that later). So TCP is usually used. What happens with TCP is that a message from machine A is first broken into pieces and sent separately. At machine C, the TCP layer of the network will check the pieces for errors, re-assemble the pieces and then deliver the message to C. Everything that is sent from A to C is considered one big message. This can make things a bit complicated on the receiving side. This is why it's useful to just have a while loop until it receives a message saying C has finished receiving the message. Now lets see a simple server in python.
    ----server.py----

    #!/usr/bin/python
    import socket, sys
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create an TCP socket
    #Associate socket with port
    host = ''
    port = int(sys.argv[1])
    s.bind((host,port))

    s.listen(1) #Listen for one connection(From the client)
    conn, addr = s.accept()
    print 'Client is ', addr

    data = conn.recv(1024)
    data = 10000 * data
    conn.send(data)
    conn.close()

    ------------
    Fairly simple. Create socket, associate host and port to socket, listen for client, accept connection from client, receive up to 1024 bytes of data from client. Then we close the socket. Now for the client.
    ----client.py----

    #!/usr/bin/python
    import sys, socket

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = sys.argv[1]
    port = int(sys.argv[2])
    s.connect((host, port))
    s.send(sys.argv[3])

    #Now the while loop
    i = 0
    while(1):
    data = s.recv(1024)
    i += 1
    if (i < 5):
    print data
    if not data:
    break
    s.close()

    ----------
    Also simple. Create TCP socket, connect to host and port, send a string. Loop to receive data from the server until data is 5(looking at the first part of the message). So you'd run the server on one machine:
    python server.py 4073

    Then the client on another:
    python client.py ip_of_server 4073 hello

    This sends the string "hello" to the server who is accepting connections on port 4073.

    Now of course, the message is regarded by the OS as splits of a message which gets re-assembled into one big message.


    [--Using sockets--]
    Python is wonderful for using socket's. It's much easier than socket programming in C. If you take a look at the server and client python files you should see the basic syntax of using sockets. To create a socket, you would do this:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


    This is creating a TCP socket and setting it as s. If you want a UDP socket, you change SOCK_STREAM to SOCK_DGRAM. For the server, we see we run the code s.bind(). This binds the host and the port to the server. The listen() function listens for clients. For the server, it listens for just 1 connection. We can change that number to accept more clients at one time. Coding socket in python is pretty easy. To learn more, visit the documentation page at http://docs.python.org/library/socket.html

    [--UDP sockets(in Python)--]
    UDP stands for User Datagram Protocol. UDP is less popular but is still used. UDP sends data in one connection, allowing room for more errors. Basically theres a chance that the message will end up scrambled or not even make it to the destination at all. This is why TCP is more widely used as it is more reliable. To create a UDP socket in python, you'd use the following syntax:

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM

    The one major change we see from using TCP sockets is SOCK_DGRAM. This simply declares the socket as a UDP, or datagram socket.

    [--UNIX sockets--]
    Now I just wanted to go over this briefly in this paper. A UNIX socket is a special type of socket. Obviously, it's only available in a UNIX system, so it wont work on Windows. But a UNIX socket is a type of socket that allows for inter process communication on a UNIX type system. You can declare a UNIX socket using:

    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

    This will create a UNIX TCP socket. Fairly simple.

    [--IPv6 sockets--]
    Another useful thing you can do with sockets is create an IPv6 socket. IPv6 is more advanced than IPv4, and allows for a lot more possible addresses. Note that soon enough, IPv6 will be used over IPv4. To declare an IPv6 socket in Python, you'd do something like this:

    s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM_

    The big difference here is the AF_INET6. This will create an INET6 socket instead of INET, which is IPv4.


    --EOF
  • Xin
    Posts: 3,251
    Nice article chronic, keep them coming.
    Xin
  • nu11byte
    Posts: 53
    Very nicely written tutorial. Shows why Python is such a dependable programming language when it comes to writing exploits.
  • Nice little code and explanation. Python was my first language because of it's easy syntax and great readability. This is probably the best explained tutorial I have seen involving sockets and Python.