Last active
November 19, 2024 19:49
-
-
Save gjyoung1974/dd71fcc6d6c74f166ccdb8b1b8aeb4d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import array | |
import socket | |
import struct | |
def chksum(packet: bytes) -> int: | |
if len(packet) % 2 != 0: | |
packet += b'\0' | |
res = sum(array.array("H", packet)) | |
res = (res >> 16) + (res & 0xffff) | |
res += res >> 16 | |
return (~res) & 0xffff | |
class TCPPacket: | |
def __init__(self, | |
src_host: str, | |
src_port: int, | |
dst_host: str, | |
dst_port: int, | |
flags: int = 0): | |
self.src_host = src_host | |
self.src_port = src_port | |
self.dst_host = dst_host | |
self.dst_port = dst_port | |
self.flags = flags | |
def build(self) -> bytes: | |
packet = struct.pack( | |
'!HHIIBBHHH', | |
self.src_port, # Source Port | |
self.dst_port, # Destination Port | |
0, # Sequence Number | |
0, # Acknoledgement Number | |
5 << 4, # Data Offset | |
self.flags, # Flags | |
8192, # Window | |
0, # Checksum (initial value) | |
0 # Urgent pointer | |
) | |
pseudo_hdr = struct.pack( | |
'!4s4sHH', | |
socket.inet_aton(self.src_host), # Source Address | |
socket.inet_aton(self.dst_host), # Destination Address | |
socket.IPPROTO_TCP, # PTCL | |
len(packet) # TCP Length | |
) | |
checksum = chksum(pseudo_hdr + packet) | |
packet = packet[:16] + struct.pack('H', checksum) + packet[18:] | |
return packet | |
if __name__ == '__main__': | |
dst = '192.168.1.1' | |
pak = TCPPacket( | |
'192.168.1.42', | |
20, | |
dst, | |
666, | |
0b000101010 # Meaning of life | |
) | |
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) | |
s.sendto(pak.build(), (dst, 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment