eapol-fuzzer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * wpa_supplicant - EAPOL fuzzer
  3. * Copyright (c) 2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "eapol_supp/eapol_supp_sm.h"
  12. #include "rsn_supp/wpa.h"
  13. struct arg_ctx {
  14. const char *fname;
  15. struct wpa_sm *wpa;
  16. struct eapol_sm *eapol;
  17. };
  18. static void test_send_eapol(void *eloop_data, void *user_ctx)
  19. {
  20. struct arg_ctx *ctx = eloop_data;
  21. char *data;
  22. size_t len;
  23. u8 src[ETH_ALEN] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 };
  24. u8 wpa_ie[200];
  25. size_t wpa_ie_len;
  26. wpa_printf(MSG_INFO, "eapol-fuzzer: Send '%s'", ctx->fname);
  27. data = os_readfile(ctx->fname, &len);
  28. if (!data) {
  29. wpa_printf(MSG_ERROR, "Could not read '%s'", ctx->fname);
  30. goto out;
  31. }
  32. wpa_hexdump(MSG_MSGDUMP, "fuzzer - EAPOL", data, len);
  33. eapol_sm_notify_portEnabled(ctx->eapol, TRUE);
  34. wpa_sm_set_param(ctx->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  35. wpa_sm_set_param(ctx->wpa, WPA_PARAM_RSN_ENABLED, 1);
  36. wpa_sm_set_param(ctx->wpa, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  37. wpa_sm_set_param(ctx->wpa, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  38. wpa_sm_set_param(ctx->wpa, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  39. wpa_ie_len = sizeof(wpa_ie);
  40. wpa_sm_set_assoc_wpa_ie_default(ctx->wpa, wpa_ie, &wpa_ie_len);
  41. if (eapol_sm_rx_eapol(ctx->eapol, src, (u8 *) data, len) <= 0)
  42. wpa_sm_rx_eapol(ctx->wpa, src, (u8 *) data, len);
  43. out:
  44. os_free(data);
  45. eloop_terminate();
  46. }
  47. static void * get_network_ctx(void *arg)
  48. {
  49. return (void *) 1;
  50. }
  51. static void set_state(void *arg, enum wpa_states state)
  52. {
  53. }
  54. static void deauthenticate(void *arg, int reason_code)
  55. {
  56. }
  57. static u8 * alloc_eapol(void *arg, u8 type,
  58. const void *data, u16 data_len,
  59. size_t *msg_len, void **data_pos)
  60. {
  61. struct ieee802_1x_hdr *hdr;
  62. *msg_len = sizeof(*hdr) + data_len;
  63. hdr = os_malloc(*msg_len);
  64. if (hdr == NULL)
  65. return NULL;
  66. hdr->version = 2;
  67. hdr->type = type;
  68. hdr->length = host_to_be16(data_len);
  69. if (data)
  70. os_memcpy(hdr + 1, data, data_len);
  71. else
  72. os_memset(hdr + 1, 0, data_len);
  73. if (data_pos)
  74. *data_pos = hdr + 1;
  75. return (u8 *) hdr;
  76. }
  77. static int ether_send(void *arg, const u8 *dest, u16 proto,
  78. const u8 *buf, size_t len)
  79. {
  80. return 0;
  81. }
  82. static int get_bssid(void *ctx, u8 *bssid)
  83. {
  84. return -1;
  85. }
  86. static int eapol_send(void *ctx, int type, const u8 *buf, size_t len)
  87. {
  88. return 0;
  89. }
  90. static int init_wpa(struct arg_ctx *arg)
  91. {
  92. struct wpa_sm_ctx *ctx;
  93. ctx = os_zalloc(sizeof(*ctx));
  94. if (ctx == NULL) {
  95. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  96. return -1;
  97. }
  98. ctx->ctx = arg;
  99. ctx->msg_ctx = arg;
  100. ctx->get_network_ctx = get_network_ctx;
  101. ctx->set_state = set_state;
  102. ctx->deauthenticate = deauthenticate;
  103. ctx->alloc_eapol = alloc_eapol;
  104. ctx->ether_send = ether_send;
  105. ctx->get_bssid = get_bssid;
  106. arg->wpa = wpa_sm_init(ctx);
  107. return arg->wpa ? 0 : -1;
  108. }
  109. static int init_eapol(struct arg_ctx *arg)
  110. {
  111. struct eapol_ctx *ctx;
  112. ctx = os_zalloc(sizeof(*ctx));
  113. if (ctx == NULL) {
  114. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  115. return -1;
  116. }
  117. ctx->ctx = arg;
  118. ctx->msg_ctx = arg;
  119. ctx->eapol_send = eapol_send;
  120. arg->eapol = eapol_sm_init(ctx);
  121. return arg->eapol ? 0 : -1;
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. struct arg_ctx ctx;
  126. int ret = -1;
  127. if (argc < 2) {
  128. printf("usage: %s <file>\n", argv[0]);
  129. return -1;
  130. }
  131. if (os_program_init())
  132. return -1;
  133. wpa_debug_level = 0;
  134. wpa_debug_show_keys = 1;
  135. if (eloop_init()) {
  136. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  137. return -1;
  138. }
  139. os_memset(&ctx, 0, sizeof(ctx));
  140. ctx.fname = argv[1];
  141. if (init_wpa(&ctx) || init_eapol(&ctx))
  142. goto fail;
  143. eloop_register_timeout(0, 0, test_send_eapol, &ctx, NULL);
  144. wpa_printf(MSG_DEBUG, "Starting eloop");
  145. eloop_run();
  146. wpa_printf(MSG_DEBUG, "eloop done");
  147. ret = 0;
  148. fail:
  149. if (ctx.wpa)
  150. wpa_sm_deinit(ctx.wpa);
  151. if (ctx.eapol)
  152. eapol_sm_deinit(ctx.eapol);
  153. eloop_destroy();
  154. os_program_deinit();
  155. return ret;
  156. }