Dies ist ein Memo, da ich ein Python-Skript geschrieben habe, das das SYN-Paket sendet, das beim Herstellen einer Verbindung mit TCP gesendet wurde.
syn.py
import socket, struct
from ctypes import *
SRC_IP = "127.0.0.1"
DST_IP = "127.0.0.1"
SRC_PORT = 1234
DST_PORT = 5678
WINDOWSIZE = 512
class tcphdr(Structure):
    _fields_ = [
            ("src_port", c_uint16),
            ("dst_port", c_uint16),
            ("seq", c_uint32),
            ("ack_seq", c_uint32),
            ("res1", c_uint16, 4),#little endian. from here 
            ("doff", c_uint16, 4),
            ("fin", c_uint16, 1),
            ("syn", c_uint16, 1),
            ("rst", c_uint16, 1),
            ("psh", c_uint16, 1),
            ("ack", c_uint16, 1),
            ("urg", c_uint16, 1),
            ("ece", c_uint16, 1),
            ("cwr", c_uint16, 1),#to here
            #("doff", c_uint16, 4),#big endian. from here
            #("res1", c_uint16, 4),
            #("cwr", c_uint16, 1),
            #("ece", c_uint16, 1),
            #("urg", c_uint16, 1),
            #("ack", c_uint16, 1),
            #("psh", c_uint16, 1),
            #("rst", c_uint16, 1),
            #("syn", c_uint16, 1),
            #("fin", c_uint16, 1),#to here
            ("window", c_uint16),
            ("check", c_uint16),
            ("urg_ptr", c_uint16),
            ]
    def pack(self):
        return buffer(self)[:]
class dummy_iphdr(Structure):
    _fields_ = [
            ("src_ip", c_uint32),
            ("dst_ip", c_uint32),
            ("pad", c_uint8),
            ("protocol", c_uint8),
            ("len", c_uint16),
            ]
    def pack(self):
        return buffer(self)[:]
def ip2int(ip_addr):
    #return struct.unpack("!I", socket.inet_aton(ip_addr))[0]
    #inet_aton returns ip_addr in network byte order. so the '!' above is not required.
    return struct.unpack("I", socket.inet_aton(ip_addr))[0]
def calc_checksum(TCPheader):
    IPheader = dummy_iphdr()
    IPheader.src_ip = ip2int(SRC_IP)
    IPheader.dst_ip = ip2int(DST_IP)
    IPheader.pad = 0
    IPheader.protocol = 6
    IPheader.len = socket.htons(20)
    IPheader_packed = IPheader.pack()
    checksum = 0
    for i in xrange(len(IPheader_packed) / 2):
        temp = struct.unpack("H", IPheader_packed[:2])[0]
        checksum += temp
        IPheader_packed = IPheader_packed[2:]
    TCPheader_packed = TCPheader.pack()
    for i in xrange(len(TCPheader_packed) / 2):
        temp = struct.unpack("H", TCPheader_packed[:2])[0]
        checksum += temp
        TCPheader_packed = TCPheader_packed[2:]
    carry = (checksum >> 16) & 0xffff
    checksum = ~((checksum & 0xffff) + carry)
    return checksum
def build_tcp_syn_packet():
    TCPheader = tcphdr()
    TCPheader.src_port = socket.htons(SRC_PORT)
    TCPheader.dst_port = socket.htons(DST_PORT)
    TCPheader.seq = 1#a random number mihgt be better
    TCPheader.doff = len(TCPheader.pack()) / 4
    TCPheader.fin = 0
    TCPheader.syn = 1
    TCPheader.rst = 0
    TCPheader.psh = 0
    TCPheader.ack = 0
    TCPheader.urg = 0
    TCPheader.ece = 0
    TCPheader.cwr = 0
    TCPheader.check = 0
    TCPheader.window = socket.htons(WINDOWSIZE)
    TCPheader.urg_ptr = 0
    TCPheader.check = calc_checksum(TCPheader)
    return TCPheader
def send_syn_packet():
    syn_packet = build_tcp_syn_packet().pack()
    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
    sock.sendto(syn_packet, (DST_IP, DST_PORT))
    sock.close()
def main():
    send_syn_packet()
if __name__ == "__main__":
    main()
Socket setzt die Protokollnummer des IP-Pakets automatisch auf TCP, auch wenn Sie 0 für die Protokollnummer angeben, wenn SOCK_STREAM für den Socket-Typ angegeben ist, jedoch explizit, wenn SOCK_RAW angegeben ist. Es scheint, dass Sie IPPROTO_TCP angeben müssen.
Ich war süchtig nach dem Teil, der die Schecksumme berechnet. Es hat nicht funktioniert, weil inet_aton die Netzwerkbyte-Reihenfolge zurückgegeben, sie aber erneut in die Netzwerkbyte-Reihenfolge entpackt hat (war es schließlich die native Byte-Reihenfolge?).
Nach dem Schreiben fand ich ein cooleres Python-Skript (http://www.binarytides.com/python-syn-flood-program-raw-sockets-linux/).
http://telracsmoratori.blog.fc2.com/blog-entry-116.html https://ja.wikipedia.org/wiki/Transmission_Control_Protocol
Recommended Posts