From f66fc94340bdf6a30260a1932e3f2f22d8822304 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 30 Jun 2025 15:38:12 -0400 Subject: Ok sending the auth packet sort of works I need to set up retransmitting because the assoc request only happens if I send the auth response twice apparently. --- .gitignore | 1 + CMakeLists.txt | 7 +- src/addr_list.c | 91 +++++++++++ src/addr_list.h | 21 +++ src/beacon_data.c | 84 ++++++++++ src/beacon_data.cpp | 84 ---------- src/beacon_data.h | 5 + src/main.c | 446 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 210 ------------------------- 9 files changed, 652 insertions(+), 297 deletions(-) create mode 100644 src/addr_list.c create mode 100644 src/addr_list.h create mode 100644 src/beacon_data.c delete mode 100644 src/beacon_data.cpp create mode 100644 src/main.c delete mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore index 567609b..6f31401 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt index deefb21..d784ebc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,9 @@ project (gen4uploader) add_subdirectory(vendor/libwifi) set(SOURCE_FILES - src/main.cpp - src/beacon_data.cpp + src/main.c + src/beacon_data.c + src/addr_list.c ) include_directories( @@ -13,4 +14,4 @@ include_directories( ) add_executable(gen4uploader ${SOURCE_FILES}) -target_link_libraries(gen4uploader wifi) +target_link_libraries(gen4uploader wifi pcap) diff --git a/src/addr_list.c b/src/addr_list.c new file mode 100644 index 0000000..759907e --- /dev/null +++ b/src/addr_list.c @@ -0,0 +1,91 @@ +#include "addr_list.h" + +#include +#include +#include +#include + +void addr_list_init(struct addr_list* list) { + list->top = NULL; +} + +void addr_list_add(struct addr_list* list, unsigned char addr[6]) { + struct addr_list_node* cur = list->top; + struct addr_list_node* prev = NULL; + + while (cur != NULL) { + if (!strncmp(cur->value, addr, 6)) { + return; + } + + prev = cur; + cur = cur->next; + } + + struct addr_list_node* next = (struct addr_list_node*)malloc(sizeof(struct addr_list_node)); + next->next = NULL; + memcpy(next->value, addr, 6); + + if (prev == NULL) { + list->top = next; + } else { + prev->next = next; + } +} + +bool addr_list_contains(struct addr_list* list, unsigned char addr[6]) { + struct addr_list_node* cur = list->top; + + while (cur != NULL) { + if (!strncmp(cur->value, addr, 6)) { + return true; + } + + cur = cur->next; + } + + return false; +} + +void addr_list_remove(struct addr_list* list, unsigned char addr[6]) { + struct addr_list_node* cur = list->top; + struct addr_list_node* prev = NULL; + + if (cur == NULL) { + return; + } + + while (cur != NULL) { + if (!strncmp(cur->value, addr, 6)) { + break; + } + + prev = cur; + cur = cur->next; + + if (cur == NULL) { + return; + } + } + + if (prev == NULL) { + list->top = cur->next; + } else { + prev->next = cur->next; + } + + free(cur); +} + +void addr_list_free(struct addr_list* list) { + struct addr_list_node* cur = list->top; + + while (cur != NULL) { + struct addr_list_node* next = cur->next; + free(cur); + + cur = next; + } + + list->top = NULL; +} diff --git a/src/addr_list.h b/src/addr_list.h new file mode 100644 index 0000000..66e217e --- /dev/null +++ b/src/addr_list.h @@ -0,0 +1,21 @@ +#ifndef G4U_ADDR_LIST_H +#define G4U_ADDR_LIST_H + +#include + +struct addr_list_node { + unsigned char value[6]; + struct addr_list_node* next; +}; + +struct addr_list { + struct addr_list_node* top; +}; + +void addr_list_init(struct addr_list* list); +void addr_list_add(struct addr_list* list, unsigned char addr[6]); +bool addr_list_contains(struct addr_list* list, unsigned char addr[6]); +void addr_list_remove(struct addr_list* list, unsigned char addr[6]); +void addr_list_free(struct addr_list* list); + +#endif /* G4U_ADDR_LIST_H */ diff --git a/src/beacon_data.c b/src/beacon_data.c new file mode 100644 index 0000000..a93eb59 --- /dev/null +++ b/src/beacon_data.c @@ -0,0 +1,84 @@ +#include "beacon_data.h" + +const unsigned char kBeaconPayloads[][128] = + { + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0xfc, 0x00, 0x09, 0x62, 0x00, 0x58, 0x6f, + 0x20, 0x49, 0x62, 0x55, 0xc4, 0x61, 0x46, 0x6e, 0xeb, 0x7e, 0xa4, 0x18, 0xac, 0x00, 0x9e, 0x01, + 0xff, 0x7b, 0x8c, 0x31, 0xfa, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x11, 0x22, 0x22, 0x32, 0x11, 0x22, + 0x22, 0x33, 0x11, 0x22, 0x32, 0x43, 0x11, 0x22, 0x33, 0x44, 0x11, 0x32, 0x43, 0x54, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x33, 0x33, 0x33, 0x33, 0x43, 0x44, 0x44, 0x43, 0x44, + 0x55, 0x55, 0x44, 0x55, 0x61, 0x66, 0x55, 0x61, 0x66, 0x66, 0x64, 0x66, 0xc7, 0x88, 0x11, 0x11}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x75, 0x01, 0x09, 0x62, 0x00, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x33, 0x33, 0x33, 0x22, 0x44, 0x44, 0x34, 0x33, 0x55, 0x55, 0x44, 0x34, + 0x66, 0x16, 0x55, 0x44, 0x66, 0x66, 0x16, 0x55, 0x88, 0x7c, 0x66, 0x46, 0x11, 0x11, 0x11, 0x01, + 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x11, 0x23, 0x22, 0x22, 0x11, 0x33, 0x22, 0x22, 0x11, + 0x34, 0x23, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x45, 0x34, 0x23, 0x11, 0x11, 0x32, 0x43, 0x45, + 0x11, 0x32, 0x44, 0x65, 0x11, 0x33, 0x54, 0x61, 0x11, 0x43, 0x54, 0x66, 0x11, 0x43, 0x15, 0x76, + 0x11, 0x43, 0x65, 0xc6, 0x11, 0x43, 0x65, 0x86, 0x11, 0x43, 0x65, 0x66, 0x66, 0x87, 0x88, 0x88}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x72, 0x02, 0x09, 0x62, 0x00, 0xb6, 0x88, + 0x88, 0x88, 0x87, 0x88, 0x88, 0x88, 0x88, 0x88, 0xb8, 0x67, 0x88, 0x88, 0x6b, 0x66, 0x88, 0xb8, + 0x66, 0x0a, 0x88, 0x78, 0xa6, 0x99, 0x66, 0x66, 0x06, 0x99, 0x88, 0x88, 0x78, 0x66, 0x88, 0x88, + 0x88, 0x6b, 0x88, 0x88, 0x88, 0x78, 0x76, 0x8b, 0x88, 0x88, 0x66, 0xb6, 0x88, 0x88, 0xa0, 0x66, + 0x8b, 0x88, 0x99, 0x6a, 0x87, 0x88, 0x99, 0x60, 0x66, 0x66, 0x54, 0x34, 0x23, 0x11, 0x56, 0x44, + 0x23, 0x11, 0x16, 0x45, 0x33, 0x11, 0x66, 0x45, 0x34, 0x11, 0x67, 0x51, 0x34, 0x11, 0x6c, 0x56, + 0x34, 0x11, 0x68, 0x56, 0x34, 0x11, 0x66, 0x56, 0x34, 0x11, 0x11, 0x43, 0x65, 0x66, 0x11, 0x43}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf2, 0x5f, 0x03, 0x09, 0x62, 0x00, 0x65, 0x96, + 0x11, 0x43, 0x65, 0x06, 0x11, 0x43, 0x15, 0xa6, 0x11, 0x43, 0x54, 0x66, 0x11, 0x33, 0x54, 0x61, + 0x11, 0x32, 0x44, 0x65, 0x11, 0x32, 0x43, 0x45, 0x66, 0x66, 0x06, 0x99, 0x99, 0xa9, 0xa6, 0x99, + 0x99, 0x09, 0x66, 0x0a, 0x99, 0x99, 0x60, 0x66, 0x90, 0x99, 0x09, 0x6a, 0x9a, 0x99, 0x99, 0x99, + 0xa6, 0x99, 0x99, 0x99, 0x66, 0x0a, 0x99, 0x99, 0x99, 0x60, 0x66, 0x66, 0x99, 0x6a, 0x9a, 0x99, + 0xa0, 0x66, 0x90, 0x99, 0x66, 0x06, 0x99, 0x99, 0xa6, 0x90, 0x99, 0x09, 0x99, 0x99, 0x99, 0xa9, + 0x99, 0x99, 0x99, 0x6a, 0x99, 0x99, 0xa0, 0x66, 0x66, 0x56, 0x34, 0x11, 0x69, 0x56, 0x34, 0x11}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x0c, 0x63, 0x04, 0x09, 0x62, 0x00, 0x60, 0x56, + 0x34, 0x11, 0x6a, 0x51, 0x34, 0x11, 0x66, 0x45, 0x34, 0x11, 0x16, 0x45, 0x33, 0x11, 0x56, 0x44, + 0x23, 0x11, 0x54, 0x34, 0x23, 0x11, 0x11, 0x32, 0x43, 0x54, 0x11, 0x22, 0x33, 0x44, 0x11, 0x22, + 0x32, 0x43, 0x11, 0x22, 0x22, 0x33, 0x11, 0x22, 0x22, 0x32, 0x11, 0x22, 0x22, 0x22, 0x11, 0x11, + 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x64, 0x66, 0x0a, 0x90, 0x55, 0x61, 0x66, 0x66, 0x44, 0x55, + 0x61, 0x66, 0x43, 0x44, 0x55, 0x55, 0x33, 0x43, 0x44, 0x44, 0x22, 0x33, 0x33, 0x33, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x09, 0xa0, 0x66, 0x46, 0x66, 0x66, 0x16, 0x55, 0x66, 0x16}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x05, 0x74, 0x83, 0x05, 0x09, 0x62, 0x00, 0x55, 0x44, + 0x55, 0x55, 0x44, 0x34, 0x44, 0x44, 0x34, 0x33, 0x33, 0x33, 0x33, 0x22, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x45, 0x34, 0x23, 0x11, 0x44, 0x33, 0x22, 0x11, 0x34, 0x23, 0x22, 0x11, + 0x33, 0x22, 0x22, 0x11, 0x23, 0x22, 0x22, 0x11, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x01, 0x00, 0x03, 0x57, 0x00, 0x69, 0x00, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x50, 0x00, 0x6f, 0x00, + 0x6b, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x20, 0x00, 0x46, 0x00, 0x61, 0x00}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x06, 0x59, 0xf3, 0x06, 0x09, 0x62, 0x00, 0x72, 0x00, + 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x49, 0x00, 0x4e, 0x00, + 0x54, 0x00, 0x45, 0x00, 0x4e, 0x00, 0x44, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x07, 0x96, 0xf6, 0x07, 0x09, 0x62, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0xaf, 0xf6, 0x08, 0x09, 0x48, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, + 0x86, 0x02, 0x40, 0x00, 0x02, 0x00, 0x00, 0x09, 0xfd, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; diff --git a/src/beacon_data.cpp b/src/beacon_data.cpp deleted file mode 100644 index a93eb59..0000000 --- a/src/beacon_data.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "beacon_data.h" - -const unsigned char kBeaconPayloads[][128] = - { - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0xfc, 0x00, 0x09, 0x62, 0x00, 0x58, 0x6f, - 0x20, 0x49, 0x62, 0x55, 0xc4, 0x61, 0x46, 0x6e, 0xeb, 0x7e, 0xa4, 0x18, 0xac, 0x00, 0x9e, 0x01, - 0xff, 0x7b, 0x8c, 0x31, 0xfa, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x11, 0x22, 0x22, 0x32, 0x11, 0x22, - 0x22, 0x33, 0x11, 0x22, 0x32, 0x43, 0x11, 0x22, 0x33, 0x44, 0x11, 0x32, 0x43, 0x54, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x33, 0x33, 0x33, 0x33, 0x43, 0x44, 0x44, 0x43, 0x44, - 0x55, 0x55, 0x44, 0x55, 0x61, 0x66, 0x55, 0x61, 0x66, 0x66, 0x64, 0x66, 0xc7, 0x88, 0x11, 0x11}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x75, 0x01, 0x09, 0x62, 0x00, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x33, 0x33, 0x33, 0x22, 0x44, 0x44, 0x34, 0x33, 0x55, 0x55, 0x44, 0x34, - 0x66, 0x16, 0x55, 0x44, 0x66, 0x66, 0x16, 0x55, 0x88, 0x7c, 0x66, 0x46, 0x11, 0x11, 0x11, 0x01, - 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x11, 0x23, 0x22, 0x22, 0x11, 0x33, 0x22, 0x22, 0x11, - 0x34, 0x23, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x45, 0x34, 0x23, 0x11, 0x11, 0x32, 0x43, 0x45, - 0x11, 0x32, 0x44, 0x65, 0x11, 0x33, 0x54, 0x61, 0x11, 0x43, 0x54, 0x66, 0x11, 0x43, 0x15, 0x76, - 0x11, 0x43, 0x65, 0xc6, 0x11, 0x43, 0x65, 0x86, 0x11, 0x43, 0x65, 0x66, 0x66, 0x87, 0x88, 0x88}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x72, 0x02, 0x09, 0x62, 0x00, 0xb6, 0x88, - 0x88, 0x88, 0x87, 0x88, 0x88, 0x88, 0x88, 0x88, 0xb8, 0x67, 0x88, 0x88, 0x6b, 0x66, 0x88, 0xb8, - 0x66, 0x0a, 0x88, 0x78, 0xa6, 0x99, 0x66, 0x66, 0x06, 0x99, 0x88, 0x88, 0x78, 0x66, 0x88, 0x88, - 0x88, 0x6b, 0x88, 0x88, 0x88, 0x78, 0x76, 0x8b, 0x88, 0x88, 0x66, 0xb6, 0x88, 0x88, 0xa0, 0x66, - 0x8b, 0x88, 0x99, 0x6a, 0x87, 0x88, 0x99, 0x60, 0x66, 0x66, 0x54, 0x34, 0x23, 0x11, 0x56, 0x44, - 0x23, 0x11, 0x16, 0x45, 0x33, 0x11, 0x66, 0x45, 0x34, 0x11, 0x67, 0x51, 0x34, 0x11, 0x6c, 0x56, - 0x34, 0x11, 0x68, 0x56, 0x34, 0x11, 0x66, 0x56, 0x34, 0x11, 0x11, 0x43, 0x65, 0x66, 0x11, 0x43}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf2, 0x5f, 0x03, 0x09, 0x62, 0x00, 0x65, 0x96, - 0x11, 0x43, 0x65, 0x06, 0x11, 0x43, 0x15, 0xa6, 0x11, 0x43, 0x54, 0x66, 0x11, 0x33, 0x54, 0x61, - 0x11, 0x32, 0x44, 0x65, 0x11, 0x32, 0x43, 0x45, 0x66, 0x66, 0x06, 0x99, 0x99, 0xa9, 0xa6, 0x99, - 0x99, 0x09, 0x66, 0x0a, 0x99, 0x99, 0x60, 0x66, 0x90, 0x99, 0x09, 0x6a, 0x9a, 0x99, 0x99, 0x99, - 0xa6, 0x99, 0x99, 0x99, 0x66, 0x0a, 0x99, 0x99, 0x99, 0x60, 0x66, 0x66, 0x99, 0x6a, 0x9a, 0x99, - 0xa0, 0x66, 0x90, 0x99, 0x66, 0x06, 0x99, 0x99, 0xa6, 0x90, 0x99, 0x09, 0x99, 0x99, 0x99, 0xa9, - 0x99, 0x99, 0x99, 0x6a, 0x99, 0x99, 0xa0, 0x66, 0x66, 0x56, 0x34, 0x11, 0x69, 0x56, 0x34, 0x11}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x0c, 0x63, 0x04, 0x09, 0x62, 0x00, 0x60, 0x56, - 0x34, 0x11, 0x6a, 0x51, 0x34, 0x11, 0x66, 0x45, 0x34, 0x11, 0x16, 0x45, 0x33, 0x11, 0x56, 0x44, - 0x23, 0x11, 0x54, 0x34, 0x23, 0x11, 0x11, 0x32, 0x43, 0x54, 0x11, 0x22, 0x33, 0x44, 0x11, 0x22, - 0x32, 0x43, 0x11, 0x22, 0x22, 0x33, 0x11, 0x22, 0x22, 0x32, 0x11, 0x22, 0x22, 0x22, 0x11, 0x11, - 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x64, 0x66, 0x0a, 0x90, 0x55, 0x61, 0x66, 0x66, 0x44, 0x55, - 0x61, 0x66, 0x43, 0x44, 0x55, 0x55, 0x33, 0x43, 0x44, 0x44, 0x22, 0x33, 0x33, 0x33, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x09, 0xa0, 0x66, 0x46, 0x66, 0x66, 0x16, 0x55, 0x66, 0x16}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x05, 0x74, 0x83, 0x05, 0x09, 0x62, 0x00, 0x55, 0x44, - 0x55, 0x55, 0x44, 0x34, 0x44, 0x44, 0x34, 0x33, 0x33, 0x33, 0x33, 0x22, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x45, 0x34, 0x23, 0x11, 0x44, 0x33, 0x22, 0x11, 0x34, 0x23, 0x22, 0x11, - 0x33, 0x22, 0x22, 0x11, 0x23, 0x22, 0x22, 0x11, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x01, 0x00, 0x03, 0x57, 0x00, 0x69, 0x00, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x50, 0x00, 0x6f, 0x00, - 0x6b, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x20, 0x00, 0x46, 0x00, 0x61, 0x00}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x06, 0x59, 0xf3, 0x06, 0x09, 0x62, 0x00, 0x72, 0x00, - 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x49, 0x00, 0x4e, 0x00, - 0x54, 0x00, 0x45, 0x00, 0x4e, 0x00, 0x44, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x07, 0x96, 0xf6, 0x07, 0x09, 0x62, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0xaf, 0xf6, 0x08, 0x09, 0x48, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x01, 0x00, 0x01, 0x08, 0x86, 0x02, 0x40, 0x00, 0x69, 0x36, 0x70, 0x01, 0xf0, 0x01, 0x08, 0x00, - 0x86, 0x02, 0x40, 0x00, 0x02, 0x00, 0x00, 0x09, 0xfd, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; diff --git a/src/beacon_data.h b/src/beacon_data.h index 25fd734..f9aaa17 100644 --- a/src/beacon_data.h +++ b/src/beacon_data.h @@ -1,3 +1,8 @@ +#ifndef G4U_BEACON_DATA_H +#define G4U_BEACON_DATA_H + #define BEACON_PAYLOAD_LENGTH 128 extern const unsigned char kBeaconPayloads[][128]; + +#endif /* G4U_BEACON_DATA_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b74907d --- /dev/null +++ b/src/main.c @@ -0,0 +1,446 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "addr_list.h" +#include "beacon_data.h" + +static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; + +static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER; + +static pthread_mutex_t seq_mutex = PTHREAD_MUTEX_INITIALIZER; +static int seq_number = 0; + +int get_interface_mac_address(int sock, const char *interface, unsigned char *output) +{ + struct ifreq ifr = {0}; + strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); + + if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) + { + return -1; + } + + memcpy(output, ifr.ifr_hwaddr.sa_data, 6); + return 0; +} + +int get_interface_index(int sock, const char *interface, int *index) +{ + struct ifreq ifr = {0}; + strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); + + if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) + { + return -1; + } + + *index = ifr.ifr_ifindex; + return 0; +} + +int get_next_seq_number() { + pthread_mutex_lock(&seq_mutex); + int next = seq_number++; + pthread_mutex_unlock(&seq_mutex); + + return next; +} + +int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, unsigned char **buf) +{ + struct libwifi_beacon beacon = {0}; + + if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 13)) + { + printf("Could not create beacon frame.\n"); + return -3; + } + + beacon.frame_header.seq_control.sequence_number = seq_number; + beacon.fixed_parameters.capabilities_information = (1 << CAPABILITIES_ESS) | (1 << CAPABILITIES_SHORT_PREAMBLE) | (1 << CAPABILITIES_SHORT_SLOT); + beacon.fixed_parameters.beacon_interval = 200; + + static const unsigned char supported_rates[] = {0x82, 0x84, 0x0b, 0x16, 0x24, 0x30, 0x48, 0x6c}; + if (libwifi_quick_add_tag(&beacon.tags, TAG_SUPP_RATES, supported_rates, 8)) + { + printf("Could not add supported rates tag.\n"); + return -7; + } + + static const unsigned char tim_bitmap[] = {0x01, 0x02, 0x00, 0x00}; + if (libwifi_quick_add_tag(&beacon.tags, TAG_TIM, tim_bitmap, 4)) { + printf("Could not add TIM tag.\n"); + return -7; + } + + static const unsigned char nothing = 0; + libwifi_quick_add_tag(&beacon.tags, TAG_ERP, ¬hing, 1); + libwifi_quick_add_tag(&beacon.tags, 47, ¬hing, 1); + + static const unsigned char extended_supported_rates[] = {0x0c, 0x12, 0x18, 0x60}; + if (libwifi_quick_add_tag(&beacon.tags, TAG_EXTENDED_SUPPORTED_RATES, extended_supported_rates, 4)) + { + printf("Could not add extended supported rates tag.\n"); + return -7; + } + + unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8]; + payload_data[0] = 0x00; + payload_data[1] = 0x09; + payload_data[2] = 0xBF; + payload_data[3] = 0x00; + payload_data[4] = 0xFF; + payload_data[5] = 0xFF; + payload_data[6] = 0x00; + payload_data[7] = 0x00; + memcpy(payload_data + 8, kBeaconPayloads[index], BEACON_PAYLOAD_LENGTH); + + if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8)) + { + printf("Could not add beacon data tag.\n"); + return -6; + } + + size_t beacon_size = libwifi_get_beacon_length(&beacon); + unsigned char *beacon_output = (unsigned char *)malloc(beacon_size); + if (beacon_output == NULL) + { + printf("Could not allocate beacon output.\n"); + return -4; + } + + if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0) + { + printf("Could not dump beacon.\n"); + return -5; + } + + libwifi_free_beacon(&beacon); + + *buf = beacon_output; + + return beacon_size; +} + +int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output) +{ + static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00"; + static const int radiotap_size = 8; + + int output_size = input_size + radiotap_size; + + unsigned char *buf = (unsigned char *)malloc(output_size); + if (buf == NULL) + { + return -1; + } + + memcpy(buf, radiotap, radiotap_size); + memcpy(buf + radiotap_size, input, input_size); + + *output = buf; + + return output_size; +} + +int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) +{ + struct sockaddr_ll socket_address; + socket_address.sll_ifindex = device_index; + socket_address.sll_halen = ETH_ALEN; + memcpy(socket_address.sll_addr, dst_addr, 6); + + pthread_mutex_lock(&socket_mutex); + + int ret = 0; + if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0) + { + printf("Could not send packet: %d\n", errno); + ret = errno; + } + + pthread_mutex_unlock(&socket_mutex); + + return ret; +} + +int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) +{ + struct libwifi_radiotap_info radiotap_info = {0}; + radiotap_info.present = (1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE); + radiotap_info.flags = IEEE80211_RADIOTAP_F_FCS | IEEE80211_RADIOTAP_F_SHORTPRE; + radiotap_info.rate_raw = 4; + + unsigned char radiotap_buffer[256]; + int radiotap_size = libwifi_create_radiotap(&radiotap_info, radiotap_buffer); + + uint32_t fcs = libwifi_calculate_fcs(packet, packet_size); + + int buffer_size = radiotap_size + packet_size + sizeof(uint32_t); + unsigned char *buffer = malloc(buffer_size); + if (buffer == NULL) + { + printf("Could not prepend radiotap.\n"); + return -2; + } + + memcpy(buffer, radiotap_buffer, radiotap_size); + memcpy(buffer + radiotap_size, packet, packet_size); + memcpy(buffer + radiotap_size + packet_size, (unsigned char*)&fcs, sizeof(uint32_t)); + + int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size); + free(buffer); + + return ret; +} + +int send_ack(int sock, int device_index, const unsigned char dst_addr[6]) +{ + struct libwifi_cts ack_frame = {0}; + if (libwifi_create_cts(&ack_frame, dst_addr, 0)) { + printf("Could not create ack packet.\n"); + } + + ack_frame.frame_header.frame_control.subtype = SUBTYPE_ACK; + + int ret = send_packet_with_radiotap(sock, device_index, dst_addr, (unsigned char*)&ack_frame, sizeof(struct libwifi_cts)); + + return ret; +} + +// output buffer size must be at least 18 bytes +void format_mac_address(const unsigned char addr[6], char *output) +{ + sprintf(output, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); +} + +struct thread_info +{ + pthread_t thread_id; + int sock; + unsigned char tx_addr[6]; + int device_index; + + struct addr_list authenticated; +}; + +void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const u_char *bytes) +{ + struct thread_info *tinfo = (struct thread_info *)userdata; + + struct libwifi_frame frame = {0}; + if (libwifi_get_wifi_frame(&frame, bytes, h->caplen, true)) + { + printf("Could not parse frame.\n"); + return; + } + + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_AUTH) + { + if (strncmp(frame.header.data.addr1, tinfo->tx_addr, 6)) { + // This is not for me. Misdelivery. + return; + } + + unsigned char in_addr[18]; + format_mac_address(frame.header.data.addr2, in_addr); + printf("Authentication request from %s\n", in_addr); + + if (addr_list_contains(&tinfo->authenticated, frame.header.data.addr2)) + { + // Already authenticated. + return; + } + + struct libwifi_auth_fixed_parameters *afp = (struct libwifi_auth_fixed_parameters *)frame.body; + if (afp->algorithm_number != AUTH_OPEN || afp->status_code != STATUS_SUCCESS || afp->transaction_sequence != 1) + { + // Incorrect auth details. + return; + } + + //usleep(frame.header.ctrl.duration / 2 * 100); + + send_ack(tinfo->sock, tinfo->device_index, frame.header.data.addr2); + + addr_list_add(&tinfo->authenticated, frame.header.data.addr2); + + struct libwifi_auth auth_resp = {0}; + if (libwifi_create_auth(&auth_resp, frame.header.data.addr2, frame.header.data.addr1, frame.header.data.addr1, AUTH_OPEN, 2, STATUS_SUCCESS)) + { + printf("Could not create auth response.\n"); + return; + } + + auth_resp.frame_header.duration = frame.header.ctrl.duration; + auth_resp.frame_header.seq_control.sequence_number = get_next_seq_number(); + + //static const unsigned char broadcom_tag_value[] = {0x00, 0x10, 0x18, 0x02, 0x00, 0xf0, 0x00, 0x00, 0x00}; + //libwifi_quick_add_tag(&auth_resp.tags, TAG_VENDOR_SPECIFIC, broadcom_tag_value, 9); + + size_t auth_resp_size = libwifi_get_auth_length(&auth_resp); + unsigned char *auth_resp_output = (unsigned char *)malloc(auth_resp_size); + if (auth_resp_output == NULL) + { + printf("Could not allocate auth response output.\n"); + return; + } + + if (libwifi_dump_auth(&auth_resp, auth_resp_output, auth_resp_size) < 0) + { + printf("Could not dump auth response.\n"); + return; + } + + libwifi_free_auth(&auth_resp); + + //usleep(frame.header.ctrl.duration * 100); + + if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) + { + printf("Could not send auth response.\n"); + return; + } + + usleep(5000); + + if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) + { + printf("Could not send auth response.\n"); + return; + } + + printf("Successfully authenticated %s!\n", in_addr); + } +} + +void *polling_thread(void *arg) +{ + struct thread_info *tinfo = (struct thread_info *)arg; + + /*for (;;) { + struct pollfd pfd; + pfd.fd = tinfo->sock; + pfd.events = POLLIN; + + if (poll(&pfd, 1, -1) < 0) { + printf("Error polling.\n"); + } else { + if (pfd.revents & POLLIN) { + unsigned char message[1024]; + int msglen = read(tinfo->sock, message, 1024); + if (msglen < 0) { + printf("Could not read from socket.\n"); + } else { + struct libwifi_frame frame = {0}; + if (libwifi_get_wifi_frame(&frame, message, msglen, true)) { + printf("Could not parse frame.\n"); + } else { + printf("Frame type: %d:%d\n", frame.frame_control.type, frame.frame_control.subtype); + } + } + } + } + }*/ + + char errbuf[PCAP_ERRBUF_SIZE]; + pcap_t *sniffer = pcap_create("mon0", errbuf); + if (sniffer == NULL) + { + printf("Could not open sniffer: %s\n", errbuf); + return NULL; + } + + pcap_set_promisc(sniffer, 1); + pcap_set_immediate_mode(sniffer, 1); + pcap_activate(sniffer); + pcap_loop(sniffer, -1, handle_incoming_packet, (u_char *)tinfo); + + return NULL; +} + +int main(int argc, char **argv) +{ + static const char *interface_name = "mon0"; + + int sock = socket(AF_PACKET, SOCK_RAW, 0); + if (sock == -1) + { + printf("Could not open socket.\n"); + return 1; + } + + unsigned char tx_addr[6]; + if (get_interface_mac_address(sock, interface_name, tx_addr)) + { + printf("Could not get hardware address.\n"); + return 2; + } + + int device_index = 0; + if (get_interface_index(sock, interface_name, &device_index)) + { + printf("Could not get device index.\n"); + return 2; + } + + struct thread_info *tinfo = (struct thread_info *)malloc(sizeof(struct thread_info *)); + tinfo->sock = sock; + memcpy(tinfo->tx_addr, tx_addr, 6); + tinfo->device_index = device_index; + addr_list_init(&tinfo->authenticated); + + pthread_attr_t thread_attr; + if (pthread_attr_init(&thread_attr)) + { + printf("Could not initialize thread attr.\n"); + return 6; + } + + pthread_create(&tinfo->thread_id, &thread_attr, &polling_thread, tinfo); + pthread_attr_destroy(&thread_attr); + + int beacon_index = 0; + for (;;) + { + unsigned char *beacon_output; + int beacon_size = make_beacon_frame(beacon_index, tx_addr, get_next_seq_number(), &beacon_output); + if (beacon_size < 0) + { + return 3; + } + + if (send_packet_with_radiotap(sock, device_index, kBroadcastAddress, beacon_output, beacon_size)) + { + return 4; + } + + free(beacon_output); + + beacon_index = (beacon_index + 1) % 10; + + usleep(1024 * 200); + } + + if (pthread_join(tinfo->thread_id, NULL)) + { + printf("Could not join thread I guess.\n"); + return 6; + } + + free(tinfo); + + return 0; +} diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index d34874c..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "beacon_data.h" - -static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; - -int get_interface_mac_address(int sock, const char *interface, unsigned char *output) -{ - struct ifreq ifr = {0}; - strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); - - if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) - { - return -1; - } - - memcpy(output, ifr.ifr_hwaddr.sa_data, 6); - return 0; -} - -int get_interface_index(int sock, const char *interface, int *index) -{ - struct ifreq ifr = {0}; - strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); - - if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) - { - return -1; - } - - *index = ifr.ifr_ifindex; - return 0; -} - -int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, unsigned char **buf) -{ - struct libwifi_beacon beacon = {0}; - - if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 7)) - { - printf("Could not create beacon frame.\n"); - return -3; - } - - beacon.frame_header.seq_control.sequence_number = seq_number; - - static const unsigned char supported_rates[] = {0x82, 0x84, 0x0b, 0x16, 0x24, 0x30, 0x48, 0x6c}; - if (libwifi_quick_add_tag(&beacon.tags, TAG_SUPP_RATES, supported_rates, 8)) { - printf("Could not add supported rates tag.\n"); - return -7; - } - - static const unsigned char extended_supported_rates[] = {0x0c, 0x12, 0x18, 0x60}; - if (libwifi_quick_add_tag(&beacon.tags, TAG_EXTENDED_SUPPORTED_RATES, extended_supported_rates, 4)) { - printf("Could not add extended supported rates tag.\n"); - return -7; - } - - unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8]; - payload_data[0] = 0x00; - payload_data[1] = 0x09; - payload_data[2] = 0xBF; - payload_data[3] = 0x00; - payload_data[4] = 0xFF; - payload_data[5] = 0xFF; - payload_data[6] = 0x00; - payload_data[7] = 0x00; - memcpy(payload_data + 8, kBeaconPayloads[index], BEACON_PAYLOAD_LENGTH); - - if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8)) - { - printf("Could not add beacon data tag.\n"); - return -6; - } - - size_t beacon_size = libwifi_get_beacon_length(&beacon); - unsigned char *beacon_output = (unsigned char *)malloc(beacon_size); - if (beacon_output == NULL) - { - printf("Could not allocate beacon output.\n"); - return -4; - } - - if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0) - { - printf("Could not dump beacon.\n"); - return -5; - } - - libwifi_free_beacon(&beacon); - - *buf = beacon_output; - - return beacon_size; -} - -int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output) -{ - static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00"; - static const int radiotap_size = 8; - - int output_size = input_size + radiotap_size; - - unsigned char *buf = (unsigned char *)malloc(output_size); - if (buf == NULL) - { - return -1; - } - - memcpy(buf, radiotap, radiotap_size); - memcpy(buf + radiotap_size, input, input_size); - - *output = buf; - - return output_size; -} - -int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) -{ - struct sockaddr_ll socket_address; - socket_address.sll_ifindex = device_index; - socket_address.sll_halen = ETH_ALEN; - memcpy(socket_address.sll_addr, dst_addr, 6); - - if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0) - { - printf("Could not send packet.\n"); - return errno; - } - - return 0; -} - -int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) -{ - unsigned char *buffer; - int buffer_size = prepend_radiotap(packet, packet_size, &buffer); - if (buffer_size < 0) - { - printf("Could not prepend radiotap.\n"); - return -2; - } - - int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size); - free(buffer); - - return ret; -} - -int main(int argc, char **argv) -{ - static const char *interface_name = "mon0"; - - int sock = socket(AF_PACKET, SOCK_RAW, 0); - if (sock == -1) - { - printf("Could not open socket.\n"); - return 1; - } - - unsigned char tx_addr[6]; - if (get_interface_mac_address(sock, interface_name, tx_addr)) - { - printf("Could not get hardware address.\n"); - return 2; - } - - int device_index = 0; - if (get_interface_index(sock, interface_name, &device_index)) - { - printf("Could not get device index.\n"); - return 2; - } - - int beacon_index = 0; - int seq_number = 0; - for (;;) - { - unsigned char *beacon_output; - int beacon_size = make_beacon_frame(beacon_index, tx_addr, seq_number, &beacon_output); - if (beacon_size < 0) - { - return 3; - } - - if (send_packet_with_radiotap(sock, device_index, kBroadcastAddress, beacon_output, beacon_size)) - { - return 4; - } - - free(beacon_output); - - beacon_index = (beacon_index + 1) % 10; - seq_number++; - - usleep(1024 * 100); - } - - return 0; -} -- cgit 1.4.1