Making magic with the network stack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

  1. import socket
  2. # Sending a SYN+ACK packet
  3. def init_socket():
  4. s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  5. s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  6. return s
  7. def send_raw_packet():
  8. ip_header = b'\x45\x00\x00\x28' # Version, IHL, Type of Service | Total Length
  9. ip_header += b'\x00\x01\x00\x00' # Identification | Flags, Fragment Offset
  10. ip_header += b'\x28\x06\x95\x45' # TTL = 40s, Protocol = 6 for TCP | Header Checksum
  11. ip_header += b'\xc8\xcc\x0a\x0e' # Source Address: 200.204.10.14
  12. ip_header += b'\xc8\xcc\x0a\x0c' # Destination Address: 200.204.10.12
  13. tcp_header = b'\x11\x5c\x04\xD2' # Source Port | Destination Port
  14. tcp_header += b'\x00\x00\x00\x00' # Sequence Number
  15. tcp_header += b'\x00\x00\x00\x00' # Acknowledgement Number
  16. tcp_header += b'\x50\x11\x00\x00' # Data Offset, Reserved, Flags | Window Size
  17. tcp_header += b'\xa5\x41\x00\x00' # Checksum | Urgent Pointer
  18. packet = ip_header + tcp_header
  19. return packet
  20. if __name__=="__main__":
  21. dst_IP = '200.204.10.12'
  22. s = init_socket()
  23. packet = send_raw_packet()
  24. s.sendto(packet, (dst_IP, 0))