def send_ack_scan(ip, ports, delay): sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) while len(ports) > 0: dest_port = ports.pop() ip1 = Ipv4(src_ip=src_addr, dst_ip=ip) tcp1 = TCP(1234, dest_port) tcp1.flags["ACK"] = 1 place_holder = 0 temp_header = pack('!4s4sBBH', bytes(map(int, ip1.src_ip.split('.'))), bytes(map(int, ip1.dst_ip.split('.'))), place_holder, ip1.protocol, 20) temp_header = temp_header + tcp1.header_packer() tcp_checksum = tcp1.calculate_checksum(temp_header) print(f"{temp_header} -> {tcp_checksum}") concat_flags = (tcp1.flags["CWR"] << 7) + (tcp1.flags["ECE"] << 6) + (tcp1.flags["URG"] << 5) + \ (tcp1.flags["ACK"] << 4) + (tcp1.flags["PSH"] << 3) + (tcp1.flags["RST"] << 2) + \ (tcp1.flags["SYN"] << 1) + (tcp1.flags["FYN"]) offset = 5 << 4 tcp_header = pack('!HHLLBBHHH', tcp1.src_port, tcp1.dst_port, tcp1.seq_num, tcp1.ack_num, offset, concat_flags, tcp1.window_size, tcp_checksum, tcp1.urg) datagram = ip1.header_packer() + tcp_header sock.sendto(datagram, (ip, 0)) time.sleep(delay)
def recv_ack_scan(ip, length, delay): connection = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3)) ip1 = Ipv4() tcp1 = TCP() start_time = int(time.time()) while True: if int(delay * length) < int(time.time()) - start_time: break data, addr = connection.recvfrom(65535) ipv4_header = ip1.parser(data[14:]) if (ipv4_header[10] == ip) & (ipv4_header[11] == src_addr) & ( ipv4_header[8] == socket.IPPROTO_TCP): tcp_header = tcp1.parser(ip1.data) if tcp_header[6]["RST"] == 1: ack_list.append(tcp1.src_port)
def recv_windows_scan(ip, length, delay): connection = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3)) ip1 = Ipv4() tcp1 = TCP() start_time = int(time.time()) while True: if int(delay * length) < int(time.time()) - start_time: break data, addr = connection.recvfrom(65535) ipv4_header = ip1.parser(data[14:]) if (ipv4_header[10] == ip) & (ipv4_header[11] == src_addr): if ipv4_header[8] == socket.IPPROTO_TCP: tcp_header = tcp1.parser(ip1.data) if tcp_header[6]["RST"] == 1 and tcp_header[-3] != 0: if windows_dict.get(tcp1.src_port) is None: windows_dict[tcp1.src_port] = 'open' if tcp_header[6]["RST"] == 1 and tcp_header[-3] == 0: if windows_dict.get(tcp1.src_port) is None: windows_dict[tcp1.src_port] = 'closed'