- User Space Sockets
- Stream Sockets = TCP
- Datagram Socket = UDP
- kernel Space Socket
- Raw socket = Direct sent packet without protocol-specific formation.
- 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
#!/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()
#!/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())
#!/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))
#!/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