SHARE
TWEET

Untitled

a guest Oct 11th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     import socket
  2.     from struct import *
  3.     import binascii
  4.  
  5.  
  6.     class Packet:
  7.         def __init__(self, dest_ip, dest_port):
  8.             # https://docs.python.org/3.7/library/struct.html#format-characters
  9.             # all values need to be at least one byte long (-> we need to add up some values)
  10.  
  11.             ############
  12.             # IP segment
  13.             self.version = 0x4
  14.             self.ihl = 0x5
  15.             self.type_of_service = 0x0
  16.             self.total_length = 0x28
  17.             self.identification = 0xabcd
  18.             self.flags = 0x0
  19.             self.fragment_offset = 0x0
  20.             self.ttl = 0x40
  21.             self.protocol = 0x6
  22.             self.header_checksum = 0x0
  23.             self.src_ip = socket.gethostbyname(socket.getfqdn())
  24.             self.dest_ip = dest_ip
  25.             self.src_addr = socket.inet_aton(self.src_ip)
  26.             self.dest_addr = socket.inet_aton(dest_ip)
  27.             self.v_ihl = (self.version << 4) + self.ihl
  28.             self.f_fo = (self.flags << 13) + self.fragment_offset
  29.  
  30.             #############
  31.             # TCP segment
  32.             self.src_port = 0x3039
  33.             self.dest_port = dest_port
  34.             self.seq_no = 0x0
  35.             self.ack_no = 0x0
  36.             self.data_offset = 0x5
  37.             self.reserved = 0x0
  38.             self.ns, self.cwr, self.ece, self.urg, self.ack, self.psh, self.rst, self.syn, self.fin = 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0
  39.             self.window_size = 0x7110
  40.             self.checksum = 0x0
  41.             self.urg_pointer = 0x0
  42.             self.data_offset_res_flags = (self.data_offset << 12) + (self.reserved << 9) + (self.ns << 8) + (
  43.                         self.cwr << 7) + (self.ece << 6) + (self.urg << 5) + (self.ack << 4) + (self.psh << 3) + (
  44.                                                      self.rst << 2) + (self.syn << 1) + self.fin
  45.  
  46.             ########
  47.             # packet
  48.             self.tcp_header = b""
  49.            self.ip_header = b""
  50.             self.packet = b""
  51.  
  52.        def calc_checksum(self, msg):
  53.            s = 0
  54.            for i in range(0, len(msg), 2):
  55.                w = (msg[i] << 8) + msg[i + 1]
  56.                s = s + w
  57.            # s = 0x119cc
  58.            s = (s >> 16) + (s & 0xffff)
  59.            # s = 0x19cd
  60.            s = ~s & 0xffff
  61.            # s = 0xe632
  62.            return s
  63.  
  64.        def generate_tmp_ip_header(self):
  65.            tmp_ip_header = pack("!BBHHHBBH4s4s", self.v_ihl, self.type_of_service, self.total_length,
  66.                self.identification, self.f_fo,
  67.                self.ttl, self.protocol, self.header_checksum,
  68.                self.src_addr,
  69.                self.dest_addr)
  70.            return tmp_ip_header
  71.  
  72.        def generate_tmp_tcp_header(self):
  73.            tmp_tcp_header = pack("!HHLLHHHH", self.src_port, self.dest_port,
  74.                self.seq_no,
  75.                self.ack_no,
  76.                self.data_offset_res_flags, self.window_size,
  77.                self.checksum, self.urg_pointer)
  78.            return tmp_tcp_header
  79.  
  80.        def generate_packet(self):
  81.            # IP header + checksum
  82.            final_ip_header = pack("!BBHHHBBH4s4s", self.v_ihl, self.type_of_service, self.total_length,
  83.                self.identification, self.f_fo,
  84.                self.ttl, self.protocol, self.calc_checksum(self.generate_tmp_ip_header()),
  85.                self.src_addr,
  86.                self.dest_addr)
  87.            # TCP header + checksum
  88.            tmp_tcp_header = self.generate_tmp_tcp_header()
  89.            pseudo_header = pack("!4s4sBBH", self.src_addr, self.dest_addr, self.checksum, self.protocol,
  90.                len(tmp_tcp_header))
  91.            psh = pseudo_header + tmp_tcp_header
  92.            final_tcp_header = pack("!HHLLHHHH", self.src_port, self.dest_port,
  93.                self.seq_no,
  94.                self.ack_no,
  95.                self.data_offset_res_flags, self.window_size,
  96.                self.calc_checksum(psh), self.urg_pointer)
  97.  
  98.            self.ip_header = final_ip_header
  99.            self.tcp_header = final_tcp_header
  100.            self.packet = final_ip_header + final_tcp_header
  101.  
  102.        def send_packet(self):
  103.            s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  104.            s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  105.            s.sendto(self.packet, (self.dest_ip, 1))
  106.            data = s.recv(1024)
  107.            s.close()
  108.            return data
  109.  
  110.    if __name__ == '__main__':
  111.        p = Packet("8.8.8.8", 80)
  112.        p.generate_packet()
  113.        result = p.send_packet()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top