logo

tls-shake

SSL/TLS False Handshake client for testing server’s configurationgit clone https://hacktivis.me/git/tls-shake.git

client-hello.c (2686B)


  1. #include "debug.h" /* debug_printf */
  2. #include "tls-parameters-4.h" /* ciphers */
  3. #include <err.h>
  4. #include <errno.h> // errno
  5. #include <netdb.h> // getaddrinfo()
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <stdlib.h> /* arc4random_buf() */
  9. #include <string.h> /* memset(), strlcpy(), strlcat() */
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <unistd.h> /* close() */
  13. static int SSL_TLS_versions[] = {
  14. 0x0200, /* 2.0: SSLv2 */
  15. 0x0300, /* 3.0: SSLv3 */
  16. 0x0301, /* 3.1: TLSv1.0 */
  17. 0x0302, /* 3.2: TLSv1.1 */
  18. 0x0303, /* 3.3: TLSv1.2 */
  19. };
  20. static void usage(char *command)
  21. {
  22. printf("Usage: %s <host> [port]\n", command);
  23. printf("Default port: 443\n");
  24. exit(0);
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. size_t send_result;
  29. char *handshake, *tls_packet, *client_hello;
  30. char *s_port = "1337", random_bytes[32];
  31. if(argc < 2 || argc > 3) usage(argv[0]);
  32. /* START network connection, copied from manpages of getaddrinfo(3) from OpenBSD and GNU */
  33. struct addrinfo hints, *result, *rp;
  34. int error = 0;
  35. int save_errno = errno;
  36. int s = 0;
  37. memset(&hints, 0, sizeof(hints));
  38. hints.ai_family = AF_UNSPEC;
  39. hints.ai_socktype = SOCK_STREAM;
  40. hints.ai_flags = 0;
  41. hints.ai_protocol = 0;
  42. error = getaddrinfo(argv[1], s_port, &hints, &result);
  43. if(error != 0) errx(1, "getaddrinfo: %s", gai_strerror(error));
  44. for(rp = result; rp != NULL; rp = rp->ai_next)
  45. {
  46. s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  47. if(connect(s, rp->ai_addr, rp->ai_addrlen) != -1)
  48. {
  49. break;
  50. }
  51. else
  52. {
  53. save_errno = errno;
  54. }
  55. close(s);
  56. }
  57. if((s == -1) | (rp == NULL))
  58. {
  59. perror(strerror(save_errno));
  60. exit(EXIT_FAILURE);
  61. }
  62. freeaddrinfo(result);
  63. /* DONE network connection */
  64. arc4random_buf(random_bytes, sizeof(random_bytes));
  65. tls_packet = (uint16_t)'\x01'; /* client hello: 0x01 */
  66. strlcpy(client_hello, (uint32_t)0x0303, 4); /* SSL version max */
  67. strlcat(client_hello, (const char *)&random_bytes, 32);
  68. strlcat(tls_packet, (const char *)sizeof(client_hello), 3);
  69. strlcat(tls_packet, client_hello, sizeof(client_hello));
  70. strlcat(tls_packet, '\x00', 2); /* Session ID lenght */
  71. strlcat(tls_packet, sizeof(ciphers), 2);
  72. strlcat(tls_packet, &ciphers, sizeof(ciphers));
  73. strlcat(tls_packet, '\x01', 1); /* Compression Method */
  74. strlcat(tls_packet, '\x00', 1); /* Compression Methods */
  75. strlcat(tls_packet, "\x00\x00", 2); /* Extension Lenght */
  76. handshake = '\x16';
  77. strlcat(&handshake, 0x0200, 2); /* SSL version min */
  78. strlcat(&handshake, sizeof(tls_packet), 2);
  79. strlcat(&handshake, tls_packet, sizeof(tls_packet));
  80. write(s, handshake, sizeof(handshake));
  81. close(s);
  82. return 0;
  83. }