Send a pre-recorded TLS CLIENT_HELLO to a server. :param dest_host: The destination host to which to send the CLIENT_HELLO. :param dest_port: The destination port to which to send the CLIENT_HELLO. :param split_size: The size of each split packet. 0 means no split, send the whole
(dest_host: str, dest_port: int, split_size: int, num_data_packets: int)
| 69 | |
| 70 | |
| 71 | def send_clienthello(dest_host: str, dest_port: int, split_size: int, num_data_packets: int) -> int: |
| 72 | '''Send a pre-recorded TLS CLIENT_HELLO to a server. |
| 73 | :param dest_host: The destination host to which to send the CLIENT_HELLO. |
| 74 | :param dest_port: The destination port to which to send the CLIENT_HELLO. |
| 75 | :param split_size: The size of each split packet. 0 means no split, |
| 76 | send the whole packet at once. |
| 77 | :param num_data_packets: The number of data packets to send. |
| 78 | :return: 0 on success, non-zero on failure. |
| 79 | ''' |
| 80 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 81 | s.setblocking(True) |
| 82 | timeout = 3 |
| 83 | s.settimeout(timeout) |
| 84 | print(f'Connecting to: {dest_host}:{dest_port}') |
| 85 | try: |
| 86 | s.connect((dest_host, dest_port)) |
| 87 | except socket.error as e: |
| 88 | print(f"Failed to connect to {dest_host}:{dest_port} - {e}") |
| 89 | return 1 |
| 90 | print('Connection successful.') |
| 91 | split_size = split_size if split_size > 0 else len(client_hello) |
| 92 | client_hello_packets = [client_hello[i:i+split_size] \ |
| 93 | for i in range(0, len(client_hello), split_size)] |
| 94 | print( |
| 95 | f'Sending ClientHello of {len(client_hello)} bytes in {split_size} ' |
| 96 | f'byte packets, {len(client_hello_packets)} total packets.') |
| 97 | for packet in client_hello_packets: |
| 98 | print(f'Sending packet of size {len(packet)} bytes.') |
| 99 | s.send(packet) |
| 100 | time.sleep(0.05) |
| 101 | print('CLIENT_HELLO sent, waiting for response.') |
| 102 | try: |
| 103 | buff = s.recv(10240) |
| 104 | except socket.timeout: |
| 105 | print(f"Failed: no response received within {timeout} seconds.") |
| 106 | return 1 |
| 107 | except socket.error as e: |
| 108 | print(f"Failed to receive response - {e}") |
| 109 | return 1 |
| 110 | except Exception as e: |
| 111 | print(f"An unexpected error occurred while receiving response - {e}") |
| 112 | return 1 |
| 113 | print("Response received:") |
| 114 | print(buff.decode('utf-8', errors='ignore')) |
| 115 | |
| 116 | print(f'Sending {num_data_packets} dummy data packets.') |
| 117 | for i in range(num_data_packets): |
| 118 | time.sleep(0.05) |
| 119 | print(f'Sending dummy data packet {i} of size {len(buff)} bytes.') |
| 120 | send_buf = f'data: {i}\n'.encode('utf-8') |
| 121 | try: |
| 122 | s.send(send_buf) |
| 123 | except socket.error as e: |
| 124 | print(f"Failed to send dummy data packet {i} - {e}") |
| 125 | return 1 |
| 126 | # Wait for a response after each data packet |
| 127 | try: |
| 128 | response_buff = s.recv(10240) |