Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import socket
  2. import struct
  3.  
  4. def _checksum_func(data: bytes) -> int:
  5.     """
  6.    _checksum_func is an internal method to perform 1s compliment checksum calculation for TCP/UDP headers
  7.  
  8.    :rtype: int
  9.    :param data: A byte object which represents the header for checksum calculation
  10.    :return: checksum: an int representing the checksum of the bytes/header.
  11.    """
  12.     # https://github.com/houluy/UDP/blob/master/udp.py#L120
  13.     checksum = 0
  14.     data_len = len(data)
  15.     if data_len % 2:
  16.         data_len += 1
  17.         data += struct.pack("!B", 0)
  18.     for i in range(0, data_len, 2):
  19.         w = (data[i] << 8) + (data[i + 1])
  20.         checksum += w
  21.     checksum = (checksum >> 16) + (checksum & 0xFFFF)
  22.     checksum = ~checksum & 0xFFFF
  23.     return checksum
  24.  
  25.  
  26.  
  27. def chksum(packet: bytes) -> int:
  28.     s = 0       # Binary Sum
  29.     # loop taking 2 characters at a time
  30.     for i in range(0, len(packet), 2):
  31.         if (i+1) < len(packet):
  32.             a = packet[i]
  33.             b = packet[i+1]
  34.             s = s + (a+(b << 8))
  35.         elif (i+1)==len(packet):
  36.             s += packet[i]
  37.         else:
  38.             raise "Something Wrong here"
  39.     # One's Complement
  40.     s = s + (s >> 16)
  41.     s = ~s & 0xffff
  42.     return s
  43.  
  44.  
  45. # IPv4 Header
  46. ip_ver = 4
  47. ip_ihl = 5
  48. ttl = 5
  49. ip_tos = 0
  50. ip_tot_len = 0 ## Reset this once UDP header is written.
  51. ip_id = 257
  52. ip_frag_off = 0
  53. l4_proto = 17
  54. ip_check = 0
  55. ip_saddr = socket.inet_aton("192.168.0.80")
  56. ip_daddr = socket.inet_aton(socket.gethostbyname("1.1.1.1"))
  57.  
  58. ip_ihl_ver = (ip_ver << 4) + ip_ihl
  59.  
  60. ip_header = struct.pack(
  61.     "!BBHHHBBH4s4s",
  62.     ip_ihl_ver,
  63.     ip_tos,
  64.     ip_tot_len,
  65.     ip_id,
  66.     ip_frag_off,
  67.     ttl,
  68.     l4_proto,
  69.     ip_check,
  70.     ip_saddr,
  71.     ip_daddr,
  72. )
  73.  
  74.  
  75.  
  76. udp_src_port = 34500
  77. udp_dst_port = 34500
  78.  
  79. data = "01234567".encode()
  80. # UDP is a bit stupid, and takes a lower layer info as part of it's checksum. Specifically src/dst IP addr.
  81. # This is called the pseudo header
  82. pseudo_header = struct.pack("!BBH", 0, socket.getprotobyname("udp"), len(data) + 8 )
  83. pseudo_header = ip_saddr + ip_daddr + pseudo_header
  84. # Set the checksum to 0, so we can generate a header, then calculate the checksum and re-apply
  85. checksum = 0
  86. udp_header = struct.pack("!4H", udp_src_port, udp_dst_port, len(data) + 8 , checksum)
  87. checksum = _checksum_func(pseudo_header + udp_header + data)
  88. udp_header = struct.pack("!4H", udp_src_port, udp_dst_port, len(data) + 8 , checksum)
  89.  
  90.  
  91. ip_tot_len = len(ip_header) + len(udp_header)
  92.  
  93. ip_header = struct.pack(
  94.     "!BBHHHBBH4s4s",
  95.     ip_ihl_ver,
  96.     ip_tos,
  97.     ip_tot_len,
  98.     ip_id,
  99.     ip_frag_off,
  100.     ttl,
  101.     l4_proto,
  102.     ip_check,
  103.     ip_saddr,
  104.     ip_daddr,
  105. )
  106.  
  107. ip_check = chksum(ip_header)
  108.  
  109. ip_header = struct.pack(
  110.     "!BBHHHBBH4s4s",
  111.     ip_ihl_ver,
  112.     ip_tos,
  113.     ip_tot_len,
  114.     ip_id,
  115.     ip_frag_off,
  116.     ttl,
  117.     l4_proto,
  118.     ip_check,
  119.     ip_saddr,
  120.     ip_daddr,
  121. )
  122.  
  123.  
  124.  
  125. raw_sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
  126. raw_sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  127. raw_sock.sendto(ip_header + udp_header + data, ("1.1.1.1", 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement