Skip to content

Instantly share code, notes, and snippets.

@ZacFran
Last active July 11, 2023 17:12
Show Gist options
  • Save ZacFran/91fdd0a09bd7f9f50e1507c8b62a65e5 to your computer and use it in GitHub Desktop.
Save ZacFran/91fdd0a09bd7f9f50e1507c8b62a65e5 to your computer and use it in GitHub Desktop.

Notes

Sockets

  1. User Space Sockets
  • Stream Sockets = TCP
  • Datagram Socket = UDP
  1. kernel Space Socket
  • Raw socket = Direct sent packet without protocol-specific formation.

Python

  • Python3 Libraries and References

Socket https://docs.python.org/3/library/socket.html

Struct https://docs.python.org/3/library/struct.html

Sys https://docs.python.org/3/library/sys.html

  • Python3 Libraries and References (Cont)

Errors https://docs.python.org/3/tutorial/errors.html

Exceptions https://docs.python.org/3/library/exceptions.html

Stream Socket example

#!/bin/python3

import socket
# This can also be accomplished by using s = socket.socket() due to defaults
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ipaddr = `127.0.0.1`
port = `54321`



s.connect((ipaddr, port))

# To send a string as a bytes-like object, add the prefix b to the string.
s.send(b'Hello\n`)
# It is recommend that the buffersize used with recbfrom is a power of 2 and not a very largy number of bits

response, conn = s.recvfrom(1024)

# In order to receive a message that is sent as a bytes like obhect you must decode into utf-8
print(response.decode())


s.close()

DGRAM socket example

#!/bin/python3
import socket

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

ipaddr = `127.0.0.1`
port = 54321

s.sendto(b'Hello\n`, (ipaddr,port)

response, conn = s.recvfrom(1024)

# Decode defaults utf-8
print(response,decode())

Making a RAW ipv4 header

#!/bin/python

import socket # building the socket
import sys # system level commands
from struck import * # establishing the structure of the packet

# Creating the raw socket.
try:
  s = socket.socket(socket.AF_INET, sockiet.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error as msg:
  print(msg)
  sys.exit()
  
  
packet = ''

src_ip = "10.1.0.2"
dst_ip = "10.3.0.2

# IP4 header information

ip_ver_ihl = 69 # This is putting the decimal conversion of 0x45
ip_tos = 0      # This combines th DSCP and ECN fields
ip_len = 0      # The kernel will fill in the actual length of the packet
ip_id  = 12345  # The IP Identification for the packet
ip_frag= 0      # This sets fragemention to off
ip_ttl = 64     # This determines th TTL
ip_prot = 16    # This sets the IP protocol to CHAOS
ip_check = 0    #The kernel will fill in the checksum for the packet
ip_srcadd = socket.inet_aton(src_ip) # inet aton(string) will convert an IP address to a 32 bit binary number
ip_dstadd = socket.inet_aton(dst_ip) # same

ip_header = pack('!BBHHHBBH4s4s' , ip_ver_ihl, ip_tos, ip_len, ip_id, ip_frag, ip_ttl, ip_proto, ip_check, ip_srcadd, ip_dstadd) # builds the header

message = b'This is a message!'
packet = ip_header + message

# Send the packet
s.sendto(packet, (dst_ip, 0))

RAW TCP

#!/bin/python

import socket # building the socket
import sys # system level commands
import array # for doing an checksum
from struct import * # establishing the structure of the packet


# Creating the raw socket.
try:
  s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error as msg:
  print(msg)
  sys.exit()


packet = ''

src_ip = "10.1.0.2"
dst_ip = "10.3.0.2"

# IP4 header information

ip_ver_ihl = 69 # This is putting the decimal conversion of 0x45
ip_tos = 0      # This combines th DSCP and ECN fields
ip_len = 0      # The kernel will fill in the actual length of the packet
ip_id  = 12345  # The IP Identification for the packet
ip_frag= 0      # This sets fragemention to off
ip_ttl = 64     # This determines th TTL
ip_prot = 6    # This sets the IP protocol to CHAOS
ip_check = 0    #The kernel will fill in the checksum for the packet
ip_srcadd = socket.inet_aton(src_ip) # inet aton(string) will convert an IP address to a 32 bit binary number
ip_dstadd = socket.inet_aton(dst_ip) # same

ip_header = pack('!BBHHHBBH4s4s' , ip_ver_ihl, ip_tos, ip_len, ip_id, ip_frag, ip_ttl, ip_proto, ip_check, ip_srcadd, ip_dstadd) # builds the header

# TCP header fields
tcp_src = 54321 # Source port
tcp_dst = 7777 # Destination port
tcp_seq = 454 # Sequence number
tcp_ake_seq = 0 # Tcp act sequence num
tcp_data_off =5 # Data offset specifying the size of the TCP header *4 which is 20
tcp_reserve = 0 # the 3 reserve bits +ns flag in reserve field
tcp_flags = 0 #TCP flags field befor the bits are turned on
tcp_win =65535 # Maximum allowed window size reorded to network order 
tcp_chk = 0 # TCPchecksum which will be calculated later on 
tcp_urg_ptr = 0 # Urget po9inter only if URGflag is set

# Combine the lefe shifted 4 bit TCP offest and the reserve field
tcp_off_res = (tcp_data_off << 4) + tcp_reserve

#TCPflags by bit 
tcp_fin = 0
tcp_syn = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment