solveed all week02

This commit is contained in:
2024-11-10 19:46:45 +09:00
parent be44ad3718
commit 857c731089
2 changed files with 5 additions and 5 deletions

View File

@ -14,8 +14,8 @@ from random import randrange
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
TCP_CLIENTS = {} # ((IP, port) -> [sent_packets]) TCP_CLIENTS = {} # ((IP, port) -> [sent_packets])
# SERVER_IP = '131.159.15.68' # don't use the domain name in this case SERVER_IP = '131.159.15.68' # don't use the domain name in this case
SERVER_IP = '192.168.1.4' # don't use the domain name in this case # SERVER_IP = '192.168.1.4' # don't use the domain name in this case
SERVER_PORT = 20102 SERVER_PORT = 20102
COOKIE_SECRET = 'TASTY_COOKIES123' COOKIE_SECRET = 'TASTY_COOKIES123'
INITIAL_SEQ = 1337 INITIAL_SEQ = 1337
@ -31,7 +31,7 @@ def handle_packet(packet: Packet):
if packet.haslayer(TCP) and packet[TCP].sport == SERVER_PORT and packet[TCP].dport == SRC_PORT and packet[TCP].flags == "SA": if packet.haslayer(TCP) and packet[TCP].sport == SERVER_PORT and packet[TCP].dport == SRC_PORT and packet[TCP].flags == "SA":
print("received SA packett") print("received SA packett")
ip = IP(dst=SERVER_IP) ip = IP(dst=SERVER_IP)
syn = TCP(sport=SRC_PORT, dport=SERVER_PORT, flags='SA', seq=COOKIE, ack=packet[TCP].seq) syn = TCP(sport=SRC_PORT, dport=SERVER_PORT, flags='A', seq=COOKIE, ack=packet[TCP].seq)
resp = (ip / syn) resp = (ip / syn)
send(resp) send(resp)
if packet.haslayer(TCP) and packet[TCP].sport == SERVER_PORT and packet[TCP].dport == SRC_PORT and packet[TCP].flags == "A": if packet.haslayer(TCP) and packet[TCP].sport == SERVER_PORT and packet[TCP].dport == SRC_PORT and packet[TCP].flags == "A":
@ -48,7 +48,7 @@ def start_sniffing():
prn=handle_packet, prn=handle_packet,
store=False, store=False,
monitor=True, monitor=True,
iface='eth0', # set to your interface. IMPORTANT: SET TO enX0 FOR AUTOGRADER!!! iface='enX0', # set to your interface. IMPORTANT: SET TO enX0 FOR AUTOGRADER!!!
) )
COOKIE = generate_syn_cookie(SERVER_IP, SERVER_PORT, COOKIE_SECRET) COOKIE = generate_syn_cookie(SERVER_IP, SERVER_PORT, COOKIE_SECRET)

View File

@ -73,7 +73,7 @@ def handle_packet(packet: Packet):
send(initial_syn) send(initial_syn)
else: else:
if (0x02 in packet[TCP].flags) or (not seq == expected_cookie) or (not ack == INITIAL_SEQ): if ("S" in packet[TCP].flags) or (not seq == expected_cookie) or (not ack == INITIAL_SEQ):
print(f'Invalid cookie {seq}, expected {expected_cookie}') print(f'Invalid cookie {seq}, expected {expected_cookie}')
rst = get_rst(src_ip, src_port, seq) rst = get_rst(src_ip, src_port, seq)
send(rst) send(rst)