-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe_analyzer.c
81 lines (68 loc) · 2.46 KB
/
frame_analyzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @file frame_analyzer.c
* @author risinek ([email protected])
* @date 2021-04-05
* @copyright Copyright (c) 2021
*
* @brief Implements frame analysis
*/
#include "frame_analyzer.h"
#include <stdint.h>
#include <string.h>
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "esp_log.h"
#include "esp_err.h"
#include "esp_event.h"
#include "wifi_controller.h"
#include "frame_analyzer_parser.h"
static const char *TAG = "frame_analyzer";
static uint8_t target_bssid[6];
static search_type_t search_type = -1;
/**
* @brief Analyzes data frames from sniffer.
*
* @param args
* @param event_base
* @param event_id
* @param event_data
*/
static void data_frame_handler(void *args, esp_event_base_t event_base, int32_t event_id, void *event_data) {
ESP_LOGV(TAG, "Handling DATA frame");
wifi_promiscuous_pkt_t *frame = (wifi_promiscuous_pkt_t *) event_data;
if(!is_frame_bssid_matching(frame, target_bssid)){
ESP_LOGV(TAG, "Not matching BSSIDs.");
return;
}
eapol_packet_t *eapol_packet = parse_eapol_packet((data_frame_t *) frame->payload);
if(eapol_packet == NULL){
ESP_LOGV(TAG, "Not an EAPOL packet.");
return;
}
eapol_key_packet_t *eapol_key_packet = parse_eapol_key_packet(eapol_packet);
if(eapol_key_packet == NULL){
ESP_LOGV(TAG, "Not an EAPOL-Key packet");
return;
}
if(search_type == SEARCH_HANDSHAKE){
// TODO handle timeouts properly by e.g. for cycle
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_post(FRAME_ANALYZER_EVENTS, DATA_FRAME_EVENT_EAPOLKEY_FRAME, frame, sizeof(wifi_promiscuous_pkt_t) + frame->rx_ctrl.sig_len, portMAX_DELAY));
return;
}
if(search_type == SEARCH_PMKID){
pmkid_item_t *pmkid_items;
if((pmkid_items = parse_pmkid(eapol_key_packet)) == NULL){
return;
}
ESP_ERROR_CHECK(esp_event_post(FRAME_ANALYZER_EVENTS, DATA_FRAME_EVENT_PMKID, &pmkid_items, sizeof(pmkid_item_t *), portMAX_DELAY));
return;
}
}
void frame_analyzer_capture_start(search_type_t search_type_arg, const uint8_t *bssid){
ESP_LOGI(TAG, "Frame analysis started...");
search_type = search_type_arg;
memcpy(&target_bssid, bssid, 6);
ESP_ERROR_CHECK(esp_event_handler_register(SNIFFER_EVENTS, SNIFFER_EVENT_CAPTURED_DATA, &data_frame_handler, NULL));
}
void frame_analyzer_capture_stop(){
ESP_ERROR_CHECK(esp_event_handler_unregister(ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, &data_frame_handler));
}