From cd1df65dc36ac35d526de195284d5ebf18e1f92b Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 17 Dec 2021 18:52:36 +0000 Subject: test: Add ctests for generation functions. This commit also enforces error code checking on functions inside of the generation functions, such as for `libwifi_quick_add_tag`. --- .github/workflows/x86_64.yml | 37 ++ src/libwifi/gen/management/assoc_request.c | 11 +- src/libwifi/gen/management/assoc_response.c | 19 +- src/libwifi/gen/management/assoc_response.h | 4 +- src/libwifi/gen/management/authentication.c | 4 +- src/libwifi/gen/management/authentication.h | 2 +- src/libwifi/gen/management/beacon.c | 32 +- src/libwifi/gen/management/beacon.h | 6 +- src/libwifi/gen/management/disassociation.c | 4 +- src/libwifi/gen/management/disassociation.h | 2 +- src/libwifi/gen/management/probe_request.c | 11 +- src/libwifi/gen/management/probe_request.h | 2 +- src/libwifi/gen/management/probe_response.c | 43 +- src/libwifi/gen/management/probe_response.h | 6 +- src/libwifi/gen/management/reassoc_request.c | 10 +- src/libwifi/gen/management/reassoc_response.c | 23 +- src/libwifi/gen/management/reassoc_response.h | 4 +- src/libwifi/gen/management/timing_ad.c | 9 +- src/libwifi/gen/management/timing_ad.h | 2 +- test/CMakeLists.txt | 71 +- test/README.md | 69 ++ test/src/.clang-format | 8 - test/src/action_tests.c | 87 +++ test/src/assoc_req_tests.c | 91 +++ test/src/assoc_resp_tests.c | 91 +++ test/src/atim_tests.c | 35 + test/src/auth_tests.c | 91 +++ test/src/beacon_tests.c | 91 +++ test/src/deauth_tests.c | 91 +++ test/src/disassoc_tests.c | 91 +++ test/src/helpers.c | 31 - test/src/helpers.h | 60 -- test/src/probe_req_tests.c | 91 +++ test/src/probe_resp_tests.c | 91 +++ test/src/reassoc_req_tests.c | 91 +++ test/src/reassoc_resp_tests.c | 91 +++ test/src/test_generation.c | 924 -------------------------- test/src/test_misc.c | 30 - test/src/test_parsing.c | 613 ----------------- test/src/timing_ad_tests.c | 57 ++ utils/.clang-format | 8 + utils/CMakeLists.txt | 15 + utils/src/helpers.c | 31 + utils/src/helpers.h | 60 ++ utils/src/test_generation.c | 924 ++++++++++++++++++++++++++ utils/src/test_misc.c | 30 + utils/src/test_parsing.c | 613 +++++++++++++++++ 47 files changed, 3079 insertions(+), 1728 deletions(-) create mode 100644 test/README.md delete mode 100644 test/src/.clang-format create mode 100644 test/src/action_tests.c create mode 100644 test/src/assoc_req_tests.c create mode 100644 test/src/assoc_resp_tests.c create mode 100644 test/src/atim_tests.c create mode 100644 test/src/auth_tests.c create mode 100644 test/src/beacon_tests.c create mode 100644 test/src/deauth_tests.c create mode 100644 test/src/disassoc_tests.c delete mode 100644 test/src/helpers.c delete mode 100644 test/src/helpers.h create mode 100644 test/src/probe_req_tests.c create mode 100644 test/src/probe_resp_tests.c create mode 100644 test/src/reassoc_req_tests.c create mode 100644 test/src/reassoc_resp_tests.c delete mode 100644 test/src/test_generation.c delete mode 100644 test/src/test_misc.c delete mode 100644 test/src/test_parsing.c create mode 100644 test/src/timing_ad_tests.c create mode 100644 utils/.clang-format create mode 100644 utils/CMakeLists.txt create mode 100644 utils/src/helpers.c create mode 100644 utils/src/helpers.h create mode 100644 utils/src/test_generation.c create mode 100644 utils/src/test_misc.c create mode 100644 utils/src/test_parsing.c diff --git a/.github/workflows/x86_64.yml b/.github/workflows/x86_64.yml index 844a109..cc90e1d 100644 --- a/.github/workflows/x86_64.yml +++ b/.github/workflows/x86_64.yml @@ -22,6 +22,43 @@ jobs: run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - name: Build X86_64 + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config $BUILD_TYPE + + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + shell: bash working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build X86_64 shell: bash + working-directory: ${{github.workspace}}/build run: cmake --build . --config $BUILD_TYPE + + - name: Create Test Environment + run: cmake -E make_directory ${{github.workspace}}/test/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake ${{github.workspace}}/test + + - name: Build Tests + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake --build . + + - name: Test X86_64 + shell: bash + working-directory: ${{github.workspace}}/test/build + run: ctest --output-on-failure diff --git a/src/libwifi/gen/management/assoc_request.c b/src/libwifi/gen/management/assoc_request.c index 8ba3585..268b167 100644 --- a/src/libwifi/gen/management/assoc_request.c +++ b/src/libwifi/gen/management/assoc_request.c @@ -47,8 +47,15 @@ int libwifi_create_assoc_req(struct libwifi_assoc_req *assoc_req, const unsigned assoc_req->fixed_parameters.capabilities_information = BYTESWAP16(LIBWIFI_DEFAULT_AP_CAPABS); assoc_req->fixed_parameters.listen_interval = BYTESWAP16(LIBWIFI_DEFAULT_LISTEN_INTERVAL); - libwifi_quick_add_tag(&assoc_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); - libwifi_quick_add_tag(&assoc_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); + int ret = libwifi_quick_add_tag(&assoc_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); + if (ret != 0) { + return ret; + } + + ret = libwifi_quick_add_tag(&assoc_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); + if (ret != 0) { + return ret; + } return 0; } diff --git a/src/libwifi/gen/management/assoc_response.c b/src/libwifi/gen/management/assoc_response.c index be00511..b3077de 100644 --- a/src/libwifi/gen/management/assoc_response.c +++ b/src/libwifi/gen/management/assoc_response.c @@ -41,21 +41,28 @@ size_t libwifi_get_assoc_resp_length(struct libwifi_assoc_resp *assoc_resp) { * Simple helper function to set the channel of an association response by removing and re-adding the * DS tagged parameter. */ -void libwifi_set_assoc_resp_channel(struct libwifi_assoc_resp *assoc_resp, uint8_t channel) { +int libwifi_set_assoc_resp_channel(struct libwifi_assoc_resp *assoc_resp, uint8_t channel) { + int ret = 0; + if (assoc_resp->tags.length != 0) { - libwifi_remove_tag(&assoc_resp->tags, TAG_DS_PARAMETER); + ret = libwifi_remove_tag(&assoc_resp->tags, TAG_DS_PARAMETER); + if (ret != 0) { + return ret; + } } const unsigned char *chan = (const unsigned char *) &channel; - libwifi_quick_add_tag(&assoc_resp->tags, TAG_DS_PARAMETER, chan, 1); + ret = libwifi_quick_add_tag(&assoc_resp->tags, TAG_DS_PARAMETER, chan, 1); + + return ret; } /** * The generated association response frame is made with sane defaults defined in common.h and core/types.h. * Two tagged parameters are also added to the association response: Channel and Supported Rates. */ -void libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsigned char receiver[6], +int libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsigned char receiver[6], const unsigned char transmitter[6], uint8_t channel) { memset(assoc_resp, 0, sizeof(struct libwifi_assoc_resp)); @@ -71,7 +78,9 @@ void libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsi libwifi_set_assoc_resp_channel(assoc_resp, channel); const unsigned char supported_rates[] = LIBWIFI_DEFAULT_SUPP_RATES; - libwifi_quick_add_tag(&assoc_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + int ret = libwifi_quick_add_tag(&assoc_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + + return ret; } /** diff --git a/src/libwifi/gen/management/assoc_response.h b/src/libwifi/gen/management/assoc_response.h index 9162d1c..07ad4b4 100644 --- a/src/libwifi/gen/management/assoc_response.h +++ b/src/libwifi/gen/management/assoc_response.h @@ -24,7 +24,7 @@ * @param assoc_resp A libwifi_assoc_resp * @param channel The new channel */ -void libwifi_set_assoc_resp_channel(struct libwifi_assoc_resp *assoc_resp, uint8_t channel); +int libwifi_set_assoc_resp_channel(struct libwifi_assoc_resp *assoc_resp, uint8_t channel); /** * Calculate the length of a given libwifi_assoc_resp @@ -46,7 +46,7 @@ size_t libwifi_get_assoc_resp_length(struct libwifi_assoc_resp *assoc_resp); * @param channel The desired channel of the assoc_resp * */ -void libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsigned char receiver[6], +int libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsigned char receiver[6], const unsigned char transmitter[6], uint8_t channel); /** diff --git a/src/libwifi/gen/management/authentication.c b/src/libwifi/gen/management/authentication.c index 7fcaa22..e8ffea2 100644 --- a/src/libwifi/gen/management/authentication.c +++ b/src/libwifi/gen/management/authentication.c @@ -32,7 +32,7 @@ size_t libwifi_get_auth_length(struct libwifi_auth *auth) { /** * The generated authentication frame is made with sane defaults defined in common.h. */ -void libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver[6], +int libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver[6], const unsigned char transmitter[6], uint16_t algorithm_number, uint16_t transaction_sequence, uint16_t status_code) { memset(auth, 0, sizeof(struct libwifi_auth)); @@ -47,6 +47,8 @@ void libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver auth->fixed_parameters.algorithm_number = algorithm_number; auth->fixed_parameters.transaction_sequence = transaction_sequence; auth->fixed_parameters.status_code = status_code; + + return 0; } /** diff --git a/src/libwifi/gen/management/authentication.h b/src/libwifi/gen/management/authentication.h index 4328f95..75e8dcf 100644 --- a/src/libwifi/gen/management/authentication.h +++ b/src/libwifi/gen/management/authentication.h @@ -40,7 +40,7 @@ size_t libwifi_get_auth_length(struct libwifi_auth *auth); * @param algorithm_number Algorithm type to use * */ -void libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver[6], +int libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver[6], const unsigned char transmitter[6], uint16_t algorithm_number, uint16_t transaction_sequence, uint16_t status_code); diff --git a/src/libwifi/gen/management/beacon.c b/src/libwifi/gen/management/beacon.c index 4eb394a..e678fd7 100644 --- a/src/libwifi/gen/management/beacon.c +++ b/src/libwifi/gen/management/beacon.c @@ -39,32 +39,46 @@ size_t libwifi_get_beacon_length(struct libwifi_beacon *beacon) { /** * Simple helper to set the beacon SSID tag by removing it and then adding it back with the new value. */ -void libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid) { +int libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid) { + int ret = 0; + if (beacon->tags.length != 0) { - libwifi_remove_tag(&beacon->tags, TAG_SSID); + ret = libwifi_remove_tag(&beacon->tags, TAG_SSID); + if (ret != 0) { + return ret; + } } - libwifi_quick_add_tag(&beacon->tags, TAG_SSID, (void *) ssid, strlen(ssid)); + ret = libwifi_quick_add_tag(&beacon->tags, TAG_SSID, (void *) ssid, strlen(ssid)); + + return ret; } /** * Simple helper to set the beacon DS tag by removing it and then adding it back with the new value. */ -void libwifi_set_beacon_channel(struct libwifi_beacon *beacon, uint8_t channel) { +int libwifi_set_beacon_channel(struct libwifi_beacon *beacon, uint8_t channel) { + int ret = 0; + if (beacon->tags.length != 0) { - libwifi_remove_tag(&beacon->tags, TAG_DS_PARAMETER); + ret = libwifi_remove_tag(&beacon->tags, TAG_DS_PARAMETER); + if (ret != 0) { + return ret; + } } const unsigned char *chan = (const unsigned char *) &channel; - libwifi_quick_add_tag(&beacon->tags, TAG_DS_PARAMETER, chan, 1); + ret = libwifi_quick_add_tag(&beacon->tags, TAG_DS_PARAMETER, chan, 1); + + return ret; } /** * The generated beacon frame is made with sane defaults defined in common.h. * Three tagged parameters are also added to the beacon: SSID, Channel and Supported Rates. */ -void libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char receiver[6], +int libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char receiver[6], const unsigned char transmitter[6], const char *ssid, uint8_t channel) { memset(beacon, 0, sizeof(struct libwifi_beacon)); @@ -82,7 +96,9 @@ void libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char re libwifi_set_beacon_channel(beacon, channel); const unsigned char supported_rates[] = LIBWIFI_DEFAULT_SUPP_RATES; - libwifi_quick_add_tag(&beacon->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + int ret = libwifi_quick_add_tag(&beacon->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + + return ret; } /** diff --git a/src/libwifi/gen/management/beacon.h b/src/libwifi/gen/management/beacon.h index 3da342b..971df88 100644 --- a/src/libwifi/gen/management/beacon.h +++ b/src/libwifi/gen/management/beacon.h @@ -24,7 +24,7 @@ * @param beacon A struct libwifi_beacon * @param ssid The new SSID */ -void libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid); +int libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid); /** * Set the channel of a struct libwifi_beacon. @@ -32,7 +32,7 @@ void libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid); * @param beacon A struct libwifi_beacon * @param channel The new channel */ -void libwifi_set_beacon_channel(struct libwifi_beacon *beacon, uint8_t channel); +int libwifi_set_beacon_channel(struct libwifi_beacon *beacon, uint8_t channel); /** * Calculate the length of a given struct libwifi_beacon @@ -55,7 +55,7 @@ size_t libwifi_get_beacon_length(struct libwifi_beacon *beacon); * @param channel The desired channel of the beacon * */ -void libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char receiver[6], +int libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char receiver[6], const unsigned char transmitter[6], const char *ssid, uint8_t channel); /** diff --git a/src/libwifi/gen/management/disassociation.c b/src/libwifi/gen/management/disassociation.c index a885efd..d6cf237 100644 --- a/src/libwifi/gen/management/disassociation.c +++ b/src/libwifi/gen/management/disassociation.c @@ -33,7 +33,7 @@ size_t libwifi_get_disassoc_length(struct libwifi_disassoc *disassoc) { * The generated disassociation frame contains only the supplied receiver, transmitter and reason_code by * default. */ -void libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned char receiver[6], +int libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned char receiver[6], const unsigned char transmitter[6], uint16_t reason_code) { memset(disassoc, 0, sizeof(struct libwifi_disassoc)); @@ -46,6 +46,8 @@ void libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned c disassoc->frame_header.seq_control.sequence_number = (rand() % 4096); memcpy(&disassoc->fixed_parameters.reason_code, &reason_code, sizeof(reason_code)); + + return 0; } /** diff --git a/src/libwifi/gen/management/disassociation.h b/src/libwifi/gen/management/disassociation.h index fada9f0..10f1db9 100644 --- a/src/libwifi/gen/management/disassociation.h +++ b/src/libwifi/gen/management/disassociation.h @@ -40,7 +40,7 @@ size_t libwifi_get_disassoc_length(struct libwifi_disassoc *disassoc); * @param reason_code The disassoc reason code * */ -void libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned char receiver[6], +int libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned char receiver[6], const unsigned char transmitter[6], uint16_t reason_code); /** diff --git a/src/libwifi/gen/management/probe_request.c b/src/libwifi/gen/management/probe_request.c index 7c5b99e..95cdcdb 100644 --- a/src/libwifi/gen/management/probe_request.c +++ b/src/libwifi/gen/management/probe_request.c @@ -31,7 +31,7 @@ size_t libwifi_get_probe_req_length(struct libwifi_probe_req *probe_req) { * The generated probe request frame is made with sane defaults defined in common.h. * Two tagged parameters are also added to the beacon: SSID and Channel. */ -void libwifi_create_probe_req(struct libwifi_probe_req *probe_req, const unsigned char receiver[6], +int libwifi_create_probe_req(struct libwifi_probe_req *probe_req, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char bssid[6], const char *ssid, uint8_t channel) { memset(probe_req, 0, sizeof(struct libwifi_probe_req)); @@ -43,8 +43,13 @@ void libwifi_create_probe_req(struct libwifi_probe_req *probe_req, const unsigne memcpy(&probe_req->frame_header.addr3, bssid, 6); probe_req->frame_header.seq_control.sequence_number = (rand() % 4096); - libwifi_quick_add_tag(&probe_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); - libwifi_quick_add_tag(&probe_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); + int ret = libwifi_quick_add_tag(&probe_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); + if (ret != 0) { + return ret; + } + + ret = libwifi_quick_add_tag(&probe_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); + return ret; } /** diff --git a/src/libwifi/gen/management/probe_request.h b/src/libwifi/gen/management/probe_request.h index f98b70a..c71897b 100644 --- a/src/libwifi/gen/management/probe_request.h +++ b/src/libwifi/gen/management/probe_request.h @@ -40,7 +40,7 @@ size_t libwifi_get_probe_req_length(struct libwifi_probe_req *probe_req); * @param reason_code The probe_req reason code * */ -void libwifi_create_probe_req(struct libwifi_probe_req *probe_req, const unsigned char receiver[6], +int libwifi_create_probe_req(struct libwifi_probe_req *probe_req, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char bssid[6], const char *ssid, uint8_t channel); diff --git a/src/libwifi/gen/management/probe_response.c b/src/libwifi/gen/management/probe_response.c index 403a8bc..1528313 100644 --- a/src/libwifi/gen/management/probe_response.c +++ b/src/libwifi/gen/management/probe_response.c @@ -39,32 +39,46 @@ size_t libwifi_get_probe_resp_length(struct libwifi_probe_resp *probe_resp) { /** * Simple helper to set the probe response SSID tag by removing it and then adding it back with the new value. */ -void libwifi_set_probe_resp_ssid(struct libwifi_probe_resp *probe_resp, const char *ssid) { +int libwifi_set_probe_resp_ssid(struct libwifi_probe_resp *probe_resp, const char *ssid) { + int ret = 0; + if (probe_resp->tags.length != 0) { - libwifi_remove_tag(&probe_resp->tags, TAG_SSID); + ret = libwifi_remove_tag(&probe_resp->tags, TAG_SSID); + if (ret != 0) { + return ret; + } } - libwifi_quick_add_tag(&probe_resp->tags, TAG_SSID, (void *) ssid, strlen(ssid)); + ret = libwifi_quick_add_tag(&probe_resp->tags, TAG_SSID, (void *) ssid, strlen(ssid)); + + return ret; } /** * Simple helper to set the probe response DS tag by removing it and then adding it back with the new value. */ -void libwifi_set_probe_resp_channel(struct libwifi_probe_resp *probe_resp, uint8_t channel) { +int libwifi_set_probe_resp_channel(struct libwifi_probe_resp *probe_resp, uint8_t channel) { + int ret = 0; + if (probe_resp->tags.length != 0) { - libwifi_remove_tag(&probe_resp->tags, TAG_DS_PARAMETER); + ret = libwifi_remove_tag(&probe_resp->tags, TAG_DS_PARAMETER); + if (ret != 0) { + return ret; + } } const unsigned char *chan = (const unsigned char *) &channel; - libwifi_quick_add_tag(&probe_resp->tags, TAG_DS_PARAMETER, chan, 1); + ret = libwifi_quick_add_tag(&probe_resp->tags, TAG_DS_PARAMETER, chan, 1); + + return ret; } /** * The generated probe response frame is made with sane defaults defined in common.h. * Three tagged parameters are also added to the probe response: SSID, Channel and Supported Rates. */ -void libwifi_create_probe_resp(struct libwifi_probe_resp *probe_resp, const unsigned char receiver[6], +int libwifi_create_probe_resp(struct libwifi_probe_resp *probe_resp, const unsigned char receiver[6], const unsigned char transmitter[6], const char *ssid, uint8_t channel) { memset(probe_resp, 0, sizeof(struct libwifi_probe_resp)); @@ -79,11 +93,20 @@ void libwifi_create_probe_resp(struct libwifi_probe_resp *probe_resp, const unsi probe_resp->fixed_parameters.probe_resp_interval = BYTESWAP16(probe_resp_interval); probe_resp->fixed_parameters.capabilities_information = BYTESWAP16(LIBWIFI_DEFAULT_AP_CAPABS); - libwifi_set_probe_resp_ssid(probe_resp, ssid); - libwifi_set_probe_resp_channel(probe_resp, channel); + int ret = libwifi_set_probe_resp_ssid(probe_resp, ssid); + if (ret != 0) { + return ret; + } + + ret = libwifi_set_probe_resp_channel(probe_resp, channel); + if (ret != 0) { + return ret; + } const unsigned char supported_rates[] = LIBWIFI_DEFAULT_SUPP_RATES; - libwifi_quick_add_tag(&probe_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + ret = libwifi_quick_add_tag(&probe_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + + return ret; } /** diff --git a/src/libwifi/gen/management/probe_response.h b/src/libwifi/gen/management/probe_response.h index 56243ee..80f5451 100644 --- a/src/libwifi/gen/management/probe_response.h +++ b/src/libwifi/gen/management/probe_response.h @@ -24,7 +24,7 @@ * @param probe_resp A libwifi_probe_resp * @param ssid The new SSID */ -void libwifi_set_probe_resp_ssid(struct libwifi_probe_resp *probe_resp, const char *ssid); +int libwifi_set_probe_resp_ssid(struct libwifi_probe_resp *probe_resp, const char *ssid); /** * Set the channel of a libwifi_probe_resp. @@ -32,7 +32,7 @@ void libwifi_set_probe_resp_ssid(struct libwifi_probe_resp *probe_resp, const ch * @param probe_resp A libwifi_probe_resp * @param channel The new channel */ -void libwifi_set_probe_resp_channel(struct libwifi_probe_resp *probe_resp, uint8_t channel); +int libwifi_set_probe_resp_channel(struct libwifi_probe_resp *probe_resp, uint8_t channel); /** * Calculate the length of a given libwifi_probe_resp @@ -55,7 +55,7 @@ size_t libwifi_get_probe_resp_length(struct libwifi_probe_resp *probe_resp); * @param channel The desired channel of the probe_resp * */ -void libwifi_create_probe_resp(struct libwifi_probe_resp *probe_resp, const unsigned char receiver[6], +int libwifi_create_probe_resp(struct libwifi_probe_resp *probe_resp, const unsigned char receiver[6], const unsigned char transmitter[6], const char *ssid, uint8_t channel); /** diff --git a/src/libwifi/gen/management/reassoc_request.c b/src/libwifi/gen/management/reassoc_request.c index f678caf..9e9bcd8 100644 --- a/src/libwifi/gen/management/reassoc_request.c +++ b/src/libwifi/gen/management/reassoc_request.c @@ -49,10 +49,14 @@ int libwifi_create_reassoc_req(struct libwifi_reassoc_req *reassoc_req, const un reassoc_req->fixed_parameters.listen_interval = BYTESWAP16(LIBWIFI_DEFAULT_LISTEN_INTERVAL); memcpy(&reassoc_req->fixed_parameters.current_ap_address, current_ap, 6); - libwifi_quick_add_tag(&reassoc_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); - libwifi_quick_add_tag(&reassoc_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); + int ret = libwifi_quick_add_tag(&reassoc_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid)); + if (ret != 0) { + return ret; + } + + ret = libwifi_quick_add_tag(&reassoc_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1); - return 0; + return ret; } /** diff --git a/src/libwifi/gen/management/reassoc_response.c b/src/libwifi/gen/management/reassoc_response.c index b9acbb6..3279f52 100644 --- a/src/libwifi/gen/management/reassoc_response.c +++ b/src/libwifi/gen/management/reassoc_response.c @@ -41,21 +41,27 @@ size_t libwifi_get_reassoc_resp_length(struct libwifi_reassoc_resp *reassoc_resp * Simple helper to set the reassociation response DS tag by removing it and then adding it back with the new * value. */ -void libwifi_set_reassoc_resp_channel(struct libwifi_reassoc_resp *reassoc_resp, uint8_t channel) { +int libwifi_set_reassoc_resp_channel(struct libwifi_reassoc_resp *reassoc_resp, uint8_t channel) { + int ret = 0; + if (reassoc_resp->tags.length != 0) { - libwifi_remove_tag(&reassoc_resp->tags, TAG_DS_PARAMETER); + ret = libwifi_remove_tag(&reassoc_resp->tags, TAG_DS_PARAMETER); + if (ret != 0) { + return ret; + } } const unsigned char *chan = (const unsigned char *) &channel; + ret = libwifi_quick_add_tag(&reassoc_resp->tags, TAG_DS_PARAMETER, chan, 1); - libwifi_quick_add_tag(&reassoc_resp->tags, TAG_DS_PARAMETER, chan, 1); + return ret; } /** * The generated reassoc_resp frame is made with sane defaults defined in common.h. * Three tagged parameters are also added to the reassoc_resp: SSID, Channel and Supported Rates. */ -void libwifi_create_reassoc_resp(struct libwifi_reassoc_resp *reassoc_resp, const unsigned char receiver[6], +int libwifi_create_reassoc_resp(struct libwifi_reassoc_resp *reassoc_resp, const unsigned char receiver[6], const unsigned char transmitter[6], uint8_t channel) { memset(reassoc_resp, 0, sizeof(struct libwifi_reassoc_resp)); @@ -68,10 +74,15 @@ void libwifi_create_reassoc_resp(struct libwifi_reassoc_resp *reassoc_resp, cons reassoc_resp->fixed_parameters.status_code = STATUS_SUCCESS; reassoc_resp->fixed_parameters.association_id = rand() % 4096; - libwifi_set_reassoc_resp_channel(reassoc_resp, channel); + int ret = libwifi_set_reassoc_resp_channel(reassoc_resp, channel); + if (ret != 0) { + return ret; + } const unsigned char supported_rates[] = LIBWIFI_DEFAULT_SUPP_RATES; - libwifi_quick_add_tag(&reassoc_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + ret = libwifi_quick_add_tag(&reassoc_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1); + + return ret; } /** diff --git a/src/libwifi/gen/management/reassoc_response.h b/src/libwifi/gen/management/reassoc_response.h index f0a2da2..420ed66 100644 --- a/src/libwifi/gen/management/reassoc_response.h +++ b/src/libwifi/gen/management/reassoc_response.h @@ -24,7 +24,7 @@ * @param reassoc_resp A libwifi_reassoc_resp * @param channel The new channel */ -void libwifi_set_reassoc_resp_channel(struct libwifi_reassoc_resp *reassoc_resp, uint8_t channel); +int libwifi_set_reassoc_resp_channel(struct libwifi_reassoc_resp *reassoc_resp, uint8_t channel); /** * Calculate the length of a given libwifi_reassoc_resp @@ -46,7 +46,7 @@ size_t libwifi_get_reassoc_resp_length(struct libwifi_reassoc_resp *reassoc_resp * @param channel The desired channel of the reassoc_resp * */ -void libwifi_create_reassoc_resp(struct libwifi_reassoc_resp *reassoc_resp, const unsigned char receiver[6], +int libwifi_create_reassoc_resp(struct libwifi_reassoc_resp *reassoc_resp, const unsigned char receiver[6], const unsigned char transmitter[6], uint8_t channel); /** diff --git a/src/libwifi/gen/management/timing_ad.c b/src/libwifi/gen/management/timing_ad.c index 6e67a6b..61b9003 100644 --- a/src/libwifi/gen/management/timing_ad.c +++ b/src/libwifi/gen/management/timing_ad.c @@ -19,10 +19,11 @@ #include "../../core/misc/epoch.h" #include "../../core/frame/tag.h" +#include #include #include -void libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsigned char destination[6], +int libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsigned char destination[6], const unsigned char transmitter[6], struct libwifi_timing_advert_fields *adv_fields, const char country[3], uint16_t max_reg_power, uint8_t max_tx_power, uint8_t tx_power_used, uint8_t noise_floor) { @@ -45,7 +46,7 @@ void libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsig adv->fixed_parameters.noise_floor = noise_floor; if (adv_fields == NULL) { - return; + return -EINVAL; } // Maximum element size is 17 @@ -78,7 +79,9 @@ void libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsig element_data_len = offset; - libwifi_quick_add_tag(&adv->tags, TAG_TIME_ADVERTISEMENT, element_data, element_data_len); + int ret = libwifi_quick_add_tag(&adv->tags, TAG_TIME_ADVERTISEMENT, element_data, element_data_len); + + return ret; } size_t libwifi_get_timing_advert_length(struct libwifi_timing_advert *adv) { diff --git a/src/libwifi/gen/management/timing_ad.h b/src/libwifi/gen/management/timing_ad.h index cdcb827..51c7729 100644 --- a/src/libwifi/gen/management/timing_ad.h +++ b/src/libwifi/gen/management/timing_ad.h @@ -18,7 +18,7 @@ #include "../../core/frame/management/timing_ad.h" -void libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsigned char destination[6], +int libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsigned char destination[6], const unsigned char transmitter[6], struct libwifi_timing_advert_fields *adv_fields, const char country[3], uint16_t max_reg_power, uint8_t max_tx_power, uint8_t tx_power_used, uint8_t noise_floor); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b418f93..32934a5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,10 +7,69 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") -add_executable(test_misc src/helpers.c src/test_misc.c) -add_executable(test_generation src/helpers.c src/test_generation.c) -add_executable(test_parsing src/helpers.c src/test_parsing.c) +link_directories("../build/") -target_link_libraries(test_misc wifi) -target_link_libraries(test_generation wifi pcap) -target_link_libraries(test_parsing wifi pcap) +add_executable(action_tests src/action_tests.c) +target_link_libraries(action_tests wifi) +add_executable(assoc_req_tests src/assoc_req_tests.c) +target_link_libraries(assoc_req_tests wifi) +add_executable(assoc_resp_tests src/assoc_resp_tests.c) +target_link_libraries(assoc_resp_tests wifi) +add_executable(atim_tests src/atim_tests.c) +target_link_libraries(atim_tests wifi) +add_executable(auth_tests src/auth_tests.c) +target_link_libraries(auth_tests wifi) +add_executable(beacon_tests src/beacon_tests.c) +target_link_libraries(beacon_tests wifi) +add_executable(deauth_tests src/deauth_tests.c) +target_link_libraries(deauth_tests wifi) +add_executable(disassoc_tests src/disassoc_tests.c) +target_link_libraries(disassoc_tests wifi) +add_executable(probe_req_tests src/probe_req_tests.c) +target_link_libraries(probe_req_tests wifi) +add_executable(probe_resp_tests src/probe_resp_tests.c) +target_link_libraries(probe_resp_tests wifi) +add_executable(reassoc_req_tests src/reassoc_req_tests.c) +target_link_libraries(reassoc_req_tests wifi) +add_executable(reassoc_resp_tests src/reassoc_resp_tests.c) +target_link_libraries(reassoc_resp_tests wifi) +add_executable(timing_ad_tests src/timing_ad_tests.c) +target_link_libraries(timing_ad_tests wifi) + +enable_testing() +add_test(NAME test_action_gen_full COMMAND action_tests --action-gen-full) +add_test(NAME test_action_gen_details COMMAND action_tests --action-gen-details) + +add_test(NAME test_assoc_req_gen_full COMMAND assoc_req_tests --assoc_req-gen-full) +add_test(NAME test_assoc_req_gen_tags COMMAND assoc_req_tests --assoc_req-gen-tags) + +add_test(NAME test_assoc_resp_gen_full COMMAND assoc_resp_tests --assoc_resp-gen-full) +add_test(NAME test_assoc_resp_gen_tags COMMAND assoc_resp_tests --assoc_resp-gen-tags) + +add_test(NAME test_atim_gen_full COMMAND atim_tests --atim-gen-full) + +add_test(NAME test_auth_gen_full COMMAND auth_tests --auth-gen-full) +add_test(NAME test_auth_gen_tags COMMAND auth_tests --auth-gen-tags) + +add_test(NAME test_beacon_gen_full COMMAND beacon_tests --beacon-gen-full) +add_test(NAME test_beacon_gen_tags COMMAND beacon_tests --beacon-gen-tags) + +add_test(NAME test_deauth_gen_full COMMAND deauth_tests --deauth-gen-full) +add_test(NAME test_deauth_gen_tags COMMAND deauth_tests --deauth-gen-tags) + +add_test(NAME test_disassoc_gen_full COMMAND disassoc_tests --disassoc-gen-full) +add_test(NAME test_disassoc_gen_tags COMMAND disassoc_tests --disassoc-gen-tags) + +add_test(NAME test_probe_req_gen_full COMMAND probe_req_tests --probe_req-gen-full) +add_test(NAME test_probe_req_gen_tags COMMAND probe_req_tests --probe_req-gen-tags) + +add_test(NAME test_probe_resp_gen_full COMMAND probe_resp_tests --probe_resp-gen-full) +add_test(NAME test_probe_resp_gen_tags COMMAND probe_resp_tests --probe_resp-gen-tags) + +add_test(NAME test_reassoc_req_gen_full COMMAND reassoc_req_tests --reassoc_req-gen-full) +add_test(NAME test_reassoc_req_gen_tags COMMAND reassoc_req_tests --reassoc_req-gen-tags) + +add_test(NAME test_reassoc_resp_gen_full COMMAND reassoc_resp_tests --reassoc_resp-gen-full) +add_test(NAME test_reassoc_resp_gen_tags COMMAND reassoc_resp_tests --reassoc_resp-gen-tags) + +add_test(NAME test_timing_ad_gen_tags COMMAND timing_ad_tests --timing_ad-gen-full) diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..402fa87 --- /dev/null +++ b/test/README.md @@ -0,0 +1,69 @@ +# libwifi Tests +libwifi uses CMakes testing functionalities. The tests are in the `src/` directory, and can be used with `make test`. + +## Running Tests +``` +>> mkdir build +>> cd build +>> cmake .. +>> make && make test +``` + +## Expected Output +``` +>> make test +Running tests... +Test project libwifi/test/build + Start 1: test_action_gen_full + 1/24 Test #1: test_action_gen_full ............. Passed 0.00 sec + Start 2: test_action_gen_details + 2/24 Test #2: test_action_gen_details .......... Passed 0.00 sec + Start 3: test_assoc_req_gen_full + 3/24 Test #3: test_assoc_req_gen_full .......... Passed 0.00 sec + Start 4: test_assoc_req_gen_tags + 4/24 Test #4: test_assoc_req_gen_tags .......... Passed 0.00 sec + Start 5: test_assoc_resp_gen_full + 5/24 Test #5: test_assoc_resp_gen_full ......... Passed 0.00 sec + Start 6: test_assoc_resp_gen_tags + 6/24 Test #6: test_assoc_resp_gen_tags ......... Passed 0.00 sec + Start 7: test_atim_gen_full + 7/24 Test #7: test_atim_gen_full ............... Passed 0.00 sec + Start 8: test_auth_gen_full + 8/24 Test #8: test_auth_gen_full ............... Passed 0.00 sec + Start 9: test_auth_gen_tags + 9/24 Test #9: test_auth_gen_tags ............... Passed 0.00 sec + Start 10: test_beacon_gen_full +10/24 Test #10: test_beacon_gen_full ............. Passed 0.00 sec + Start 11: test_beacon_gen_tags +11/24 Test #11: test_beacon_gen_tags ............. Passed 0.00 sec + Start 12: test_deauth_gen_full +12/24 Test #12: test_deauth_gen_full ............. Passed 0.00 sec + Start 13: test_deauth_gen_tags +13/24 Test #13: test_deauth_gen_tags ............. Passed 0.00 sec + Start 14: test_disassoc_gen_full +14/24 Test #14: test_disassoc_gen_full ........... Passed 0.00 sec + Start 15: test_disassoc_gen_tags +15/24 Test #15: test_disassoc_gen_tags ........... Passed 0.00 sec + Start 16: test_probe_req_gen_full +16/24 Test #16: test_probe_req_gen_full .......... Passed 0.00 sec + Start 17: test_probe_req_gen_tags +17/24 Test #17: test_probe_req_gen_tags .......... Passed 0.00 sec + Start 18: test_probe_resp_gen_full +18/24 Test #18: test_probe_resp_gen_full ......... Passed 0.00 sec + Start 19: test_probe_resp_gen_tags +19/24 Test #19: test_probe_resp_gen_tags ......... Passed 0.00 sec + Start 20: test_reassoc_req_gen_full +20/24 Test #20: test_reassoc_req_gen_full ........ Passed 0.00 sec + Start 21: test_reassoc_req_gen_tags +21/24 Test #21: test_reassoc_req_gen_tags ........ Passed 0.00 sec + Start 22: test_reassoc_resp_gen_full +22/24 Test #22: test_reassoc_resp_gen_full ....... Passed 0.00 sec + Start 23: test_reassoc_resp_gen_tags +23/24 Test #23: test_reassoc_resp_gen_tags ....... Passed 0.00 sec + Start 24: test_timing_ad_gen_tags +24/24 Test #24: test_timing_ad_gen_tags .......... Passed 0.00 sec + +100% tests passed, 0 tests failed out of 24 + +Total Test time (real) = 0.06 sec +``` diff --git a/test/src/.clang-format b/test/src/.clang-format deleted file mode 100644 index 111249f..0000000 --- a/test/src/.clang-format +++ /dev/null @@ -1,8 +0,0 @@ ---- -BasedOnStyle: LLVM -IndentWidth: '4' -SpaceAfterCStyleCast: 'true' -ColumnLimit: 400 -AllowShortFunctionsOnASingleLine: None -IndentCaseLabels: 'true' -... diff --git a/test/src/action_tests.c b/test/src/action_tests.c new file mode 100644 index 0000000..997095d --- /dev/null +++ b/test/src/action_tests.c @@ -0,0 +1,87 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_action_gen_full() { + struct libwifi_action action = {0}; + + int ret = libwifi_create_action(&action, bcast, to, ACTION_HT); + if (ret != 0) { + fprintf(stderr, "Failed to create action: %s\n", strerror(ret)); + return ret; + } + + int action_len = libwifi_get_action_length(&action); + if (action_len <= 0) { + fprintf(stderr, "Invalid action length: %d\n", action_len); + return action_len; + } + + unsigned char *buf = malloc(action_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_action(&action, buf, action_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump action\n"); + return ret; + } + + return 0; +} + +int test_action_add_detail() { + struct libwifi_action action = {0}; + + int ret = libwifi_create_action(&action, bcast, to, ACTION_HT); + if (ret != 0) { + fprintf(stderr, "Failed to create action: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_add_action_detail(&action.fixed_parameters.details, (const unsigned char *) "HELLO WORLD", strlen("HELLO WORLD")); + + int action_len = libwifi_get_action_length(&action); + if (action_len <= 0) { + fprintf(stderr, "Invalid action length: %d\n", action_len); + return action_len; + } + + unsigned char *buf = malloc(action_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_action(&action, buf, action_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump action\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--action-gen-full") == 0) { + return test_action_gen_full(); + } else if (strcmp(argv[1], "--action-gen-details") == 0) { + return test_action_add_detail(); + } + + return -1; +} diff --git a/test/src/assoc_req_tests.c b/test/src/assoc_req_tests.c new file mode 100644 index 0000000..fc6379f --- /dev/null +++ b/test/src/assoc_req_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_assoc_req_gen_full() { + struct libwifi_assoc_req assoc_req = {0}; + + int ret = libwifi_create_assoc_req(&assoc_req, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create assoc_req: %s\n", strerror(ret)); + return ret; + } + + int assoc_req_len = libwifi_get_assoc_req_length(&assoc_req); + if (assoc_req_len <= 0) { + fprintf(stderr, "Invalid assoc_req length: %d\n", assoc_req_len); + return assoc_req_len; + } + + unsigned char *buf = malloc(assoc_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_assoc_req(&assoc_req, buf, assoc_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump assoc_req\n"); + return ret; + } + + return 0; +} + +int test_assoc_req_add_tag() { + struct libwifi_assoc_req assoc_req = {0}; + + int ret = libwifi_create_assoc_req(&assoc_req, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create assoc_req: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&assoc_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add assoc_req tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int assoc_req_len = libwifi_get_assoc_req_length(&assoc_req); + if (assoc_req_len <= 0) { + fprintf(stderr, "Invalid assoc_req length: %d\n", assoc_req_len); + return assoc_req_len; + } + + unsigned char *buf = malloc(assoc_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_assoc_req(&assoc_req, buf, assoc_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump assoc_req\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--assoc_req-gen-full") == 0) { + return test_assoc_req_gen_full(); + } else if (strcmp(argv[1], "--assoc_req-gen-tags") == 0) { + return test_assoc_req_add_tag(); + } + + return -1; +} diff --git a/test/src/assoc_resp_tests.c b/test/src/assoc_resp_tests.c new file mode 100644 index 0000000..3a261ed --- /dev/null +++ b/test/src/assoc_resp_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_assoc_resp_gen_full() { + struct libwifi_assoc_resp assoc_resp = {0}; + + int ret = libwifi_create_assoc_resp(&assoc_resp, bcast, to, 11); + if (ret != 0) { + fprintf(stderr, "Failed to create assoc_resp: %s\n", strerror(ret)); + return ret; + } + + int assoc_resp_len = libwifi_get_assoc_resp_length(&assoc_resp); + if (assoc_resp_len <= 0) { + fprintf(stderr, "Invalid assoc_resp length: %d\n", assoc_resp_len); + return assoc_resp_len; + } + + unsigned char *buf = malloc(assoc_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_assoc_resp(&assoc_resp, buf, assoc_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump assoc_resp\n"); + return ret; + } + + return 0; +} + +int test_assoc_resp_add_tag() { + struct libwifi_assoc_resp assoc_resp = {0}; + + int ret = libwifi_create_assoc_resp(&assoc_resp, bcast, to, 11); + if (ret != 0) { + fprintf(stderr, "Failed to create assoc_resp: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&assoc_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add assoc_resp tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int assoc_resp_len = libwifi_get_assoc_resp_length(&assoc_resp); + if (assoc_resp_len <= 0) { + fprintf(stderr, "Invalid assoc_resp length: %d\n", assoc_resp_len); + return assoc_resp_len; + } + + unsigned char *buf = malloc(assoc_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_assoc_resp(&assoc_resp, buf, assoc_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump assoc_resp\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--assoc_resp-gen-full") == 0) { + return test_assoc_resp_gen_full(); + } else if (strcmp(argv[1], "--assoc_resp-gen-tags") == 0) { + return test_assoc_resp_add_tag(); + } + + return -1; +} diff --git a/test/src/atim_tests.c b/test/src/atim_tests.c new file mode 100644 index 0000000..43f2265 --- /dev/null +++ b/test/src/atim_tests.c @@ -0,0 +1,35 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_atim_gen_full() { + struct libwifi_atim atim = {0}; + + int ret = libwifi_create_atim(&atim, bcast, to, to); + if (ret != 0) { + fprintf(stderr, "Failed to create atim: %s\n", strerror(ret)); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--atim-gen-full") == 0) { + return test_atim_gen_full(); + } + + return -1; +} diff --git a/test/src/auth_tests.c b/test/src/auth_tests.c new file mode 100644 index 0000000..f78aeed --- /dev/null +++ b/test/src/auth_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_auth_gen_full() { + struct libwifi_auth auth = {0}; + + int ret = libwifi_create_auth(&auth, bcast, to, 0, 100, STATUS_SUCCESS); + if (ret != 0) { + fprintf(stderr, "Failed to create auth: %s\n", strerror(ret)); + return ret; + } + + int auth_len = libwifi_get_auth_length(&auth); + if (auth_len <= 0) { + fprintf(stderr, "Invalid auth length: %d\n", auth_len); + return auth_len; + } + + unsigned char *buf = malloc(auth_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_auth(&auth, buf, auth_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump auth\n"); + return ret; + } + + return 0; +} + +int test_auth_add_tag() { + struct libwifi_auth auth = {0}; + + int ret = libwifi_create_auth(&auth, bcast, to, 0, 100, STATUS_SUCCESS); + if (ret != 0) { + fprintf(stderr, "Failed to create auth: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&auth.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add auth tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int auth_len = libwifi_get_auth_length(&auth); + if (auth_len <= 0) { + fprintf(stderr, "Invalid auth length: %d\n", auth_len); + return auth_len; + } + + unsigned char *buf = malloc(auth_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_auth(&auth, buf, auth_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump auth\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--auth-gen-full") == 0) { + return test_auth_gen_full(); + } else if (strcmp(argv[1], "--auth-gen-tags") == 0) { + return test_auth_add_tag(); + } + + return -1; +} diff --git a/test/src/beacon_tests.c b/test/src/beacon_tests.c new file mode 100644 index 0000000..1c4e17e --- /dev/null +++ b/test/src/beacon_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_beacon_gen_full() { + struct libwifi_beacon beacon = {0}; + + int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret)); + return ret; + } + + int beacon_len = libwifi_get_beacon_length(&beacon); + if (beacon_len <= 0) { + fprintf(stderr, "Invalid beacon length: %d\n", beacon_len); + return beacon_len; + } + + unsigned char *buf = malloc(beacon_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump beacon\n"); + return ret; + } + + return 0; +} + +int test_beacon_add_tag() { + struct libwifi_beacon beacon = {0}; + + int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add beacon tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int beacon_len = libwifi_get_beacon_length(&beacon); + if (beacon_len <= 0) { + fprintf(stderr, "Invalid beacon length: %d\n", beacon_len); + return beacon_len; + } + + unsigned char *buf = malloc(beacon_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump beacon\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--beacon-gen-full") == 0) { + return test_beacon_gen_full(); + } else if (strcmp(argv[1], "--beacon-gen-tags") == 0) { + return test_beacon_add_tag(); + } + + return -1; +} diff --git a/test/src/deauth_tests.c b/test/src/deauth_tests.c new file mode 100644 index 0000000..9033574 --- /dev/null +++ b/test/src/deauth_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_deauth_gen_full() { + struct libwifi_deauth deauth = {0}; + + int ret = libwifi_create_deauth(&deauth, bcast, to, REASON_STA_LEAVING); + if (ret != 0) { + fprintf(stderr, "Failed to create deauth: %s\n", strerror(ret)); + return ret; + } + + int deauth_len = libwifi_get_deauth_length(&deauth); + if (deauth_len <= 0) { + fprintf(stderr, "Invalid deauth length: %d\n", deauth_len); + return deauth_len; + } + + unsigned char *buf = malloc(deauth_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_deauth(&deauth, buf, deauth_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump deauth\n"); + return ret; + } + + return 0; +} + +int test_deauth_add_tag() { + struct libwifi_deauth deauth = {0}; + + int ret = libwifi_create_deauth(&deauth, bcast, to, REASON_STA_LEAVING); + if (ret != 0) { + fprintf(stderr, "Failed to create deauth: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&deauth.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add deauth tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int deauth_len = libwifi_get_deauth_length(&deauth); + if (deauth_len <= 0) { + fprintf(stderr, "Invalid deauth length: %d\n", deauth_len); + return deauth_len; + } + + unsigned char *buf = malloc(deauth_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_deauth(&deauth, buf, deauth_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump deauth\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--deauth-gen-full") == 0) { + return test_deauth_gen_full(); + } else if (strcmp(argv[1], "--deauth-gen-tags") == 0) { + return test_deauth_add_tag(); + } + + return -1; +} diff --git a/test/src/disassoc_tests.c b/test/src/disassoc_tests.c new file mode 100644 index 0000000..c5e27de --- /dev/null +++ b/test/src/disassoc_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_disassoc_gen_full() { + struct libwifi_disassoc disassoc = {0}; + + int ret = libwifi_create_disassoc(&disassoc, bcast, to, REASON_STA_LEAVING); + if (ret != 0) { + fprintf(stderr, "Failed to create disassoc: %s\n", strerror(ret)); + return ret; + } + + int disassoc_len = libwifi_get_disassoc_length(&disassoc); + if (disassoc_len <= 0) { + fprintf(stderr, "Invalid disassoc length: %d\n", disassoc_len); + return disassoc_len; + } + + unsigned char *buf = malloc(disassoc_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_disassoc(&disassoc, buf, disassoc_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump disassoc\n"); + return ret; + } + + return 0; +} + +int test_disassoc_add_tag() { + struct libwifi_disassoc disassoc = {0}; + + int ret = libwifi_create_disassoc(&disassoc, bcast, to, REASON_STA_LEAVING); + if (ret != 0) { + fprintf(stderr, "Failed to create disassoc: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&disassoc.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add disassoc tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int disassoc_len = libwifi_get_disassoc_length(&disassoc); + if (disassoc_len <= 0) { + fprintf(stderr, "Invalid disassoc length: %d\n", disassoc_len); + return disassoc_len; + } + + unsigned char *buf = malloc(disassoc_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_disassoc(&disassoc, buf, disassoc_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump disassoc\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--disassoc-gen-full") == 0) { + return test_disassoc_gen_full(); + } else if (strcmp(argv[1], "--disassoc-gen-tags") == 0) { + return test_disassoc_add_tag(); + } + + return -1; +} diff --git a/test/src/helpers.c b/test/src/helpers.c deleted file mode 100644 index 9fc9d0b..0000000 --- a/test/src/helpers.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "helpers.h" -#include - -void hexdump(void *data, size_t size) { - char ascii[17]; - size_t i, j; - ascii[16] = '\0'; - for (i = 0; i < size; ++i) { - printf("%02X ", ((unsigned char *) data)[i]); - if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { - ascii[i % 16] = ((unsigned char *) data)[i]; - } else { - ascii[i % 16] = '.'; - } - if ((i + 1) % 8 == 0 || i + 1 == size) { - printf(" "); - if ((i + 1) % 16 == 0) { - printf("| %s \n", ascii); - } else if (i + 1 == size) { - ascii[(i + 1) % 16] = '\0'; - if ((i + 1) % 16 <= 8) { - printf(" "); - } - for (j = (i + 1) % 16; j < 16; ++j) { - printf(" "); - } - printf("| %s \n", ascii); - } - } - } -} diff --git a/test/src/helpers.h b/test/src/helpers.h deleted file mode 100644 index 99a5329..0000000 --- a/test/src/helpers.h +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] -#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" - -static const uint8_t radiotap_data[] = { - 0x00, - 0x00, // <-- radiotap version (ignore this) - 0x18, - 0x00, // <-- number of bytes in our header (count the number of "0x"s) - - /** - * The next field is a bitmap of which options we are including. - * The full list of which field is which option is in ieee80211_radiotap.h, - * but I've chosen to include: - * 0x00 0x01: timestamp - * 0x00 0x02: flags - * 0x00 0x03: rate - * 0x00 0x04: channel - * 0x80 0x00: tx flags (seems silly to have this AND flags, but oh well) - */ - 0x0f, - 0x80, - 0x00, - 0x00, - - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, // <-- timestamp - - /** - * This is the first set of flags, and we've set the bit corresponding to - * IEEE80211_RADIOTAP_F_FCS, meaning we want the card to add a FCS at the - * end of our buffer for us. - */ - 0x10, - - 0x00, // <-- rate - 0x00, - 0x00, - 0x00, - 0x00, // <-- channel - - /** - * This is the second set of flags, specifically related to transmissions. - * The bit we've set is IEEE80211_RADIOTAP_F_TX_NOACK, which means the card - * won't wait for an ACK for this frame, and that it won't retry if it - * doesn't get one. - */ - 0x08, - 0x00, -}; - -void hexdump(void *data, size_t size); diff --git a/test/src/probe_req_tests.c b/test/src/probe_req_tests.c new file mode 100644 index 0000000..553c9d9 --- /dev/null +++ b/test/src/probe_req_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_probe_req_gen_full() { + struct libwifi_probe_req probe_req = {0}; + + int ret = libwifi_create_probe_req(&probe_req, bcast, to, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create probe_req: %s\n", strerror(ret)); + return ret; + } + + int probe_req_len = libwifi_get_probe_req_length(&probe_req); + if (probe_req_len <= 0) { + fprintf(stderr, "Invalid probe_req length: %d\n", probe_req_len); + return probe_req_len; + } + + unsigned char *buf = malloc(probe_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_probe_req(&probe_req, buf, probe_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump probe_req\n"); + return ret; + } + + return 0; +} + +int test_probe_req_add_tag() { + struct libwifi_probe_req probe_req = {0}; + + int ret = libwifi_create_probe_req(&probe_req, bcast, to, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create probe_req: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&probe_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add probe_req tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int probe_req_len = libwifi_get_probe_req_length(&probe_req); + if (probe_req_len <= 0) { + fprintf(stderr, "Invalid probe_req length: %d\n", probe_req_len); + return probe_req_len; + } + + unsigned char *buf = malloc(probe_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_probe_req(&probe_req, buf, probe_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump probe_req\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--probe_req-gen-full") == 0) { + return test_probe_req_gen_full(); + } else if (strcmp(argv[1], "--probe_req-gen-tags") == 0) { + return test_probe_req_add_tag(); + } + + return -1; +} diff --git a/test/src/probe_resp_tests.c b/test/src/probe_resp_tests.c new file mode 100644 index 0000000..463a90a --- /dev/null +++ b/test/src/probe_resp_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_probe_resp_gen_full() { + struct libwifi_probe_resp probe_resp = {0}; + + int ret = libwifi_create_probe_resp(&probe_resp, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create probe_resp: %s\n", strerror(ret)); + return ret; + } + + int probe_resp_len = libwifi_get_probe_resp_length(&probe_resp); + if (probe_resp_len <= 0) { + fprintf(stderr, "Invalid probe_resp length: %d\n", probe_resp_len); + return probe_resp_len; + } + + unsigned char *buf = malloc(probe_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_probe_resp(&probe_resp, buf, probe_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump probe_resp\n"); + return ret; + } + + return 0; +} + +int test_probe_resp_add_tag() { + struct libwifi_probe_resp probe_resp = {0}; + + int ret = libwifi_create_probe_resp(&probe_resp, bcast, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create probe_resp: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add probe_resp tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int probe_resp_len = libwifi_get_probe_resp_length(&probe_resp); + if (probe_resp_len <= 0) { + fprintf(stderr, "Invalid probe_resp length: %d\n", probe_resp_len); + return probe_resp_len; + } + + unsigned char *buf = malloc(probe_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_probe_resp(&probe_resp, buf, probe_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump probe_resp\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--probe_resp-gen-full") == 0) { + return test_probe_resp_gen_full(); + } else if (strcmp(argv[1], "--probe_resp-gen-tags") == 0) { + return test_probe_resp_add_tag(); + } + + return -1; +} diff --git a/test/src/reassoc_req_tests.c b/test/src/reassoc_req_tests.c new file mode 100644 index 0000000..00e2b53 --- /dev/null +++ b/test/src/reassoc_req_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_reassoc_req_gen_full() { + struct libwifi_reassoc_req reassoc_req = {0}; + + int ret = libwifi_create_reassoc_req(&reassoc_req, bcast, to, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create reassoc_req: %s\n", strerror(ret)); + return ret; + } + + int reassoc_req_len = libwifi_get_reassoc_req_length(&reassoc_req); + if (reassoc_req_len <= 0) { + fprintf(stderr, "Invalid reassoc_req length: %d\n", reassoc_req_len); + return reassoc_req_len; + } + + unsigned char *buf = malloc(reassoc_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_reassoc_req(&reassoc_req, buf, reassoc_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump reassoc_req\n"); + return ret; + } + + return 0; +} + +int test_reassoc_req_add_tag() { + struct libwifi_reassoc_req reassoc_req = {0}; + + int ret = libwifi_create_reassoc_req(&reassoc_req, bcast, to, to, "Some SSID", 11); + if (ret != 0) { + fprintf(stderr, "Failed to create reassoc_req: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&reassoc_req.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add reassoc_req tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int reassoc_req_len = libwifi_get_reassoc_req_length(&reassoc_req); + if (reassoc_req_len <= 0) { + fprintf(stderr, "Invalid reassoc_req length: %d\n", reassoc_req_len); + return reassoc_req_len; + } + + unsigned char *buf = malloc(reassoc_req_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_reassoc_req(&reassoc_req, buf, reassoc_req_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump reassoc_req\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--reassoc_req-gen-full") == 0) { + return test_reassoc_req_gen_full(); + } else if (strcmp(argv[1], "--reassoc_req-gen-tags") == 0) { + return test_reassoc_req_add_tag(); + } + + return -1; +} diff --git a/test/src/reassoc_resp_tests.c b/test/src/reassoc_resp_tests.c new file mode 100644 index 0000000..8167916 --- /dev/null +++ b/test/src/reassoc_resp_tests.c @@ -0,0 +1,91 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_reassoc_resp_gen_full() { + struct libwifi_reassoc_resp reassoc_resp = {0}; + + int ret = libwifi_create_reassoc_resp(&reassoc_resp, bcast, to, 11); + if (ret != 0) { + fprintf(stderr, "Failed to create reassoc_resp: %s\n", strerror(ret)); + return ret; + } + + int reassoc_resp_len = libwifi_get_reassoc_resp_length(&reassoc_resp); + if (reassoc_resp_len <= 0) { + fprintf(stderr, "Invalid reassoc_resp length: %d\n", reassoc_resp_len); + return reassoc_resp_len; + } + + unsigned char *buf = malloc(reassoc_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_reassoc_resp(&reassoc_resp, buf, reassoc_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump reassoc_resp\n"); + return ret; + } + + return 0; +} + +int test_reassoc_resp_add_tag() { + struct libwifi_reassoc_resp reassoc_resp = {0}; + + int ret = libwifi_create_reassoc_resp(&reassoc_resp, bcast, to, 11); + if (ret != 0) { + fprintf(stderr, "Failed to create reassoc_resp: %s\n", strerror(ret)); + return ret; + } + + ret = libwifi_quick_add_tag(&reassoc_resp.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + if (ret != 0) { + fprintf(stderr, "Failed to add reassoc_resp tagged parameter: %s\n", strerror(ret)); + return ret; + } + + int reassoc_resp_len = libwifi_get_reassoc_resp_length(&reassoc_resp); + if (reassoc_resp_len <= 0) { + fprintf(stderr, "Invalid reassoc_resp length: %d\n", reassoc_resp_len); + return reassoc_resp_len; + } + + unsigned char *buf = malloc(reassoc_resp_len); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate buffer\n"); + return -1; + } + + int dump_len = libwifi_dump_reassoc_resp(&reassoc_resp, buf, reassoc_resp_len); + if (dump_len <= 0) { + fprintf(stderr, "Failed to dump reassoc_resp\n"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--reassoc_resp-gen-full") == 0) { + return test_reassoc_resp_gen_full(); + } else if (strcmp(argv[1], "--reassoc_resp-gen-tags") == 0) { + return test_reassoc_resp_add_tag(); + } + + return -1; +} diff --git a/test/src/test_generation.c b/test/src/test_generation.c deleted file mode 100644 index 407e87f..0000000 --- a/test/src/test_generation.c +++ /dev/null @@ -1,924 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "helpers.h" - -#define LIVE_INJECT 0 -#define OFFLINE_DUMP 1 - -#define MODE_BEACON 0 -#define MODE_PROBE_RESPONSE 1 -#define MODE_PROBE_REQUEST 2 -#define MODE_DEAUTH 3 -#define MODE_DISASSOC 4 -#define MODE_ASSOC_RESPONSE 5 -#define MODE_ASSOC_REQUEST 6 -#define MODE_REASSOC_RESPONSE 7 -#define MODE_REASSOC_REQUEST 8 -#define MODE_AUTH 9 -#define MODE_RTS 10 -#define MODE_CTS 11 -#define MODE_RANDOM_BEACON 12 -#define MODE_ACTION 13 -#define MODE_ACTION_NOACK 14 -#define MODE_TIMING_AD 15 -#define MODE_ATIM 16 - -#define SNAPLEN 96 -#define CHANNEL 11 -#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" -#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" -#define FROM_MAC "\x00\x20\x91\x11\x22\x33" -#define REASSOC_MAC "\xAA\xBB\xCC\xDD\xEE\xFF" -#define BEACON_SSID "libwifi-beacon" -#define PROBE_RESP_SSID "libwifi-probe-resp" -#define PROBE_REQ_SSID "libwifi-probe-req" -#define ASSOC_REQ_SSID "libwifi-assoc-req" -#define REASSOC_REQ_SSID "libwifi-reassoc-req" - -pcap_t *handle = NULL; -pcap_dumper_t *outputHandle = NULL; -FILE *filename = NULL; - -static unsigned char to[] = TO_MAC; -static unsigned char from[] = FROM_MAC; -static unsigned char bcast[] = BCAST_MAC; -static unsigned char reassoc_mac[] = REASSOC_MAC; -static unsigned char tag_data[] = "\x00\x00\00\x01This is a 221 tag from libwifi.\n"; - -static int mode = 0; -static int inject_mode = 0; - -void handle_interupt(int signal) { - if (signal == SIGINT) { - int oldmode = inject_mode; - mode = -1; - inject_mode = -1; - - if (oldmode == LIVE_INJECT) { - pcap_close(handle); - printf("\n\nClosed Capture Handle!\n"); - } else if (oldmode == OFFLINE_DUMP) { - pcap_dump_flush(outputHandle); - pcap_dump_close(outputHandle); - printf("\n\nDumped and Closed Output File!\n"); - } - - exit(EXIT_SUCCESS); - } -} - -void inject_frame(void *buf, size_t buf_sz) { - struct libwifi_radiotap_info info = {0}; - info.present = 0x0000002e; // 0x002e: Flags, Rate, Channel, dBm Ant Signal - info.channel.flags = 0x0140; // OFDM, 5GHz - info.channel.freq = 5180; // Channel 46 - info.flags = 0x0000; // No Flags - info.rate = 1; // 1 Mbit - info.rate_raw = info.rate * 2; // Radiotap uses 500kb/s increments - info.signal = -20; // Signal in dBm - - char *rtap = NULL; - rtap = malloc(LIBWIFI_MAX_RADIOTAP_LEN); - if (rtap == NULL) { - printf("malloc failure: %s\n", strerror(errno)); - return; - } - memset(rtap, 0, LIBWIFI_MAX_RADIOTAP_LEN); - - int rtap_len = libwifi_create_radiotap(&info, rtap); - if (rtap_len == -1) { - printf("error generating radiotap header\n"); - return; - } - - void *frame = NULL; - size_t frame_sz = rtap_len + buf_sz; - frame = malloc(frame_sz); - if (frame == NULL) { - printf("malloc failure: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - memcpy(frame, rtap, rtap_len); - memcpy(frame + rtap_len, buf, buf_sz); - - hexdump(rtap, rtap_len); - printf("-----\n"); - hexdump(frame, frame_sz); - - if (inject_mode == LIVE_INJECT) { - pcap_inject(handle, frame, frame_sz); - } else if (inject_mode == OFFLINE_DUMP) { - struct pcap_pkthdr hdr = {0}; - hdr.caplen = frame_sz; - hdr.len = frame_sz; - struct timeval tv; - gettimeofday(&tv, NULL); - hdr.ts = tv; - pcap_dump((unsigned char *) outputHandle, &hdr, frame); - } - - free(rtap); - free(frame); -} - -void inject_beacons(int random_mac) { - while (1) { - printf("Sending 50 beacons...\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_beacon beacon; - unsigned char txmac[6] = {0}; - memset(&beacon, 0, sizeof(struct libwifi_beacon)); - - if (random_mac) { - libwifi_random_mac(txmac, NULL); - } else { - memcpy(txmac, FROM_MAC, 6); - } - libwifi_create_beacon(&beacon, bcast, txmac, BEACON_SSID, CHANNEL); - libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data)); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_beacon_length(&beacon); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting beacon with:\n"); - printf("\tSSID: %s\n", BEACON_SSID); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(txmac)); - printf("\tDestination: " MACSTR "\n", MAC2STR(bcast)); - - libwifi_dump_beacon(&beacon, buf, buf_sz); - inject_frame(buf, buf_sz); - - libwifi_free_beacon(&beacon); - free(buf); - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_probe_responses() { - while (1) { - printf("Sending 50 probe responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_probe_resp probe_resp; - memset(&probe_resp, 0, sizeof(struct libwifi_probe_resp)); - - libwifi_create_probe_resp(&probe_resp, to, from, PROBE_RESP_SSID, CHANNEL); - libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data)); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_probe_resp_length(&probe_resp); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting probe responses with:\n"); - printf("\tSSID: %s\n", PROBE_RESP_SSID); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_probe_resp(&probe_resp, buf, buf_sz); - inject_frame(buf, buf_sz); - - libwifi_free_probe_resp(&probe_resp); - free(buf); - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_probe_requests() { - while (1) { - printf("Sending 50 probe responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_probe_req probe; - memset(&probe, 0, sizeof(struct libwifi_probe_req)); - - libwifi_create_probe_req(&probe, to, from, to, PROBE_REQ_SSID, CHANNEL); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_probe_req_length(&probe); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting probe requests with:\n"); - printf("\tSSID: %s\n", PROBE_REQ_SSID); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_probe_req(&probe, buf, buf_sz); - inject_frame(buf, buf_sz); - - libwifi_free_probe_req(&probe); - free(buf); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_deauths() { - while (1) { - printf("Sending 50 probe responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_deauth deauth; - memset(&deauth, 0, sizeof(struct libwifi_deauth)); - - libwifi_create_deauth(&deauth, to, from, REASON_STA_LEAVING); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_deauth_length(&deauth); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting deauths with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tReason: %d\n", REASON_STA_LEAVING); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_deauth(&deauth, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_disassocs() { - while (1) { - printf("Sending 50 probe responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_disassoc disassoc; - memset(&disassoc, 0, sizeof(struct libwifi_disassoc)); - - libwifi_create_disassoc(&disassoc, to, from, REASON_STA_LEAVING); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_disassoc_length(&disassoc); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting disassocs with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tReason: %d\n", REASON_STA_LEAVING); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_disassoc(&disassoc, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_assoc_requests() { - while (1) { - printf("Sending 50 association requests, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_assoc_req assoc_req; - memset(&assoc_req, 0, sizeof(struct libwifi_assoc_req)); - - libwifi_create_assoc_req(&assoc_req, to, from, ASSOC_REQ_SSID, CHANNEL); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_assoc_req_length(&assoc_req); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting association requests with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_assoc_req(&assoc_req, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - libwifi_free_assoc_req(&assoc_req); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_assoc_responses() { - while (1) { - printf("Sending 50 association responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_assoc_resp assoc_resp; - memset(&assoc_resp, 0, sizeof(struct libwifi_assoc_req)); - - libwifi_create_assoc_resp(&assoc_resp, to, from, CHANNEL); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_assoc_resp_length(&assoc_resp); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting association responses with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_assoc_resp(&assoc_resp, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - libwifi_free_assoc_resp(&assoc_resp); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_reassoc_requests() { - while (1) { - printf("Sending 50 reassociation requests, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_reassoc_req reassoc_req; - memset(&reassoc_req, 0, sizeof(struct libwifi_assoc_req)); - - libwifi_create_reassoc_req(&reassoc_req, to, from, reassoc_mac, REASSOC_REQ_SSID, CHANNEL); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_reassoc_req_length(&reassoc_req); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting reassociation requests with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - printf("\tPrevious BSSID: " MACSTR "\n", MAC2STR(reassoc_mac)); - - libwifi_dump_reassoc_req(&reassoc_req, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - libwifi_free_reassoc_req(&reassoc_req); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_reassoc_responses() { - while (1) { - printf("Sending 50 reassociation responses, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_reassoc_resp reassoc_resp; - memset(&reassoc_resp, 0, sizeof(struct libwifi_assoc_req)); - - libwifi_create_reassoc_resp(&reassoc_resp, to, from, CHANNEL); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_reassoc_resp_length(&reassoc_resp); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting reassociation responses with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_reassoc_resp(&reassoc_resp, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - libwifi_free_reassoc_resp(&reassoc_resp); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_auths() { - while (1) { - printf("Sending 50 auth frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_auth auth; - memset(&auth, 0, sizeof(struct libwifi_deauth)); - - libwifi_create_auth(&auth, to, from, AUTH_OPEN, 0, STATUS_SUCCESS); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_auth_length(&auth); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - libwifi_dump_auth(&auth, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - - memset(&auth, 0, sizeof(struct libwifi_deauth)); - - libwifi_create_auth(&auth, from, to, AUTH_OPEN, 1, STATUS_SUCCESS); - - buf = NULL; - buf_sz = libwifi_get_auth_length(&auth); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting auths with:\n"); - printf("\tChannel: %d\n", CHANNEL); - printf("\tAlgorithm: %d\n", AUTH_OPEN); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_auth(&auth, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_timing_ads() { - while (1) { - printf("Sending 50 timing advertisement frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_timing_advert time_ad = {0}; - struct libwifi_timing_advert_fields ad_fields = {0}; - - ad_fields.timing_capabilities = 2; - memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5); - memcpy(ad_fields.time_update, "\xBB", 1); - memcpy(ad_fields.time_value, - "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10); - - libwifi_create_timing_advert(&time_ad, to, from, &ad_fields, "GB", -56, -56, -30, -20); - - unsigned char *buf = NULL; - size_t buf_len = libwifi_get_timing_advert_length(&time_ad); - buf = malloc(buf_len); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - printf("buf_len: %zu\n", buf_len); - - size_t ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len); - if (ret < 0) { - printf("error dump: %zu\n", ret); - exit(EXIT_FAILURE); - } - hexdump(buf, buf_len); - inject_frame(buf, buf_len); - - free(buf); - libwifi_free_timing_advert(&time_ad); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_action_noacks() { - while (1) { - printf("Sending 50 action no ack frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_action action; - memset(&action, 0, sizeof(struct libwifi_action)); - - libwifi_create_action_no_ack(&action, to, from, ACTION_FAST_BSS_TRANSITION); - - unsigned char *action_buf = malloc(256); - memset(action_buf, 0, 256); - - size_t offset = 0; - size_t w = 0; - - memcpy(action_buf, "\x01", 1); // Fast BSS Request - offset += 1; - memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address - offset += 6; - memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address - offset += 6; - - unsigned char *tag_tmp = malloc(256); - memset(tag_tmp, 0, 256); - - struct libwifi_tagged_parameter rsne = {0}; - size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20); - w = libwifi_dump_tag(&rsne, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - - - struct libwifi_tagged_parameter mobdom = {0}; - tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3); - memset(tag_tmp, 0, tsz); - w = libwifi_dump_tag(&mobdom, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - libwifi_free_tag(&mobdom); - - struct libwifi_tagged_parameter fbss = {0}; - tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88); - memset(tag_tmp, 0, tsz); - w = libwifi_dump_tag(&fbss, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - libwifi_free_tag(&fbss); - - libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_action_length(&action); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting actions with:\n"); - printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_action(&action, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_actions() { - while (1) { - printf("Sending 50 action frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_action action; - memset(&action, 0, sizeof(struct libwifi_action)); - - libwifi_create_action(&action, to, from, ACTION_FAST_BSS_TRANSITION); - - unsigned char *action_buf = malloc(256); - memset(action_buf, 0, 256); - - size_t offset = 0; - size_t w = 0; - - memcpy(action_buf, "\x01", 1); // Fast BSS Request - offset += 1; - memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address - offset += 6; - memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address - offset += 6; - - unsigned char *tag_tmp = malloc(256); - memset(tag_tmp, 0, 256); - - struct libwifi_tagged_parameter rsne = {0}; - size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20); - w = libwifi_dump_tag(&rsne, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - - - struct libwifi_tagged_parameter mobdom = {0}; - tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3); - memset(tag_tmp, 0, tsz); - w = libwifi_dump_tag(&mobdom, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - libwifi_free_tag(&mobdom); - - struct libwifi_tagged_parameter fbss = {0}; - tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88); - memset(tag_tmp, 0, tsz); - w = libwifi_dump_tag(&fbss, tag_tmp, tsz); - memcpy(action_buf + offset, tag_tmp, w); - offset += w; - libwifi_free_tag(&fbss); - - libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset); - - unsigned char *buf = NULL; - size_t buf_sz = libwifi_get_action_length(&action); - - buf = malloc(buf_sz); - if (buf == NULL) { - printf("malloc failure: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - printf("Injecting actions with:\n"); - printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION); - printf("\tSource: " MACSTR "\n", MAC2STR(from)); - printf("\tDestination: " MACSTR "\n", MAC2STR(to)); - - libwifi_dump_action(&action, buf, buf_sz); - inject_frame(buf, buf_sz); - - free(buf); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_atim() { - while (1) { - printf("Sending 50 ATIM frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_atim atim = {0}; - - libwifi_create_atim(&atim, to, from, from); - - inject_frame(&atim, sizeof(struct libwifi_atim)); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_rts() { - while (1) { - printf("Sending 50 RTS frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_rts rts = {0}; - - libwifi_create_rts(&rts, to, from, 32); - - inject_frame(&rts, sizeof(struct libwifi_rts)); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void inject_cts() { - while (1) { - printf("Sending 50 CTS frames, then sleeping for 1 second\n"); - for (int i = 0; i < 50; ++i) { - struct libwifi_cts cts = {0}; - - libwifi_create_cts(&cts, to, 32); - - inject_frame(&cts, sizeof(struct libwifi_cts)); - - usleep(1e4); // 10ms - } - sleep(1); - } -} - -void help(const char *name) { - fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\t%s --interface [interface] [--mode]\n", name); - fprintf(stderr, "\t\tor\n"); - fprintf(stderr, "\t%s --file [output file] [--mode]\n", name); - fprintf(stderr, "\n"); - fprintf(stderr, "Modes:\n"); - fprintf(stderr, "\t--beacon\n"); - fprintf(stderr, "\t--random-beacon\n"); - fprintf(stderr, "\t--probe-req\n"); - fprintf(stderr, "\t--probe-resp\n"); - fprintf(stderr, "\t--deauth\n"); - fprintf(stderr, "\t--disassoc\n"); - fprintf(stderr, "\t--assoc-req\n"); - fprintf(stderr, "\t--assoc-resp\n"); - fprintf(stderr, "\t--reassoc-req\n"); - fprintf(stderr, "\t--reassoc-resp\n"); - fprintf(stderr, "\t--auth\n"); - fprintf(stderr, "\t--timing-ad\n"); - fprintf(stderr, "\t--atim\n"); - fprintf(stderr, "\t--rts\n"); - fprintf(stderr, "\t--cts\n"); -} - -void handle_args(int argc, const char *argv[]) { - char errbuf[PCAP_ERRBUF_SIZE]; - memset(errbuf, 0, PCAP_ERRBUF_SIZE); - - if (argc < 4) { - help(argv[0]); - exit(EXIT_SUCCESS); - } - - if (strcmp(argv[1], "--file") == 0) { - inject_mode = OFFLINE_DUMP; - - filename = fopen(argv[2], "w+"); - if ((handle = pcap_open_dead(DLT_IEEE802_11_RADIO, BUFSIZ)) == NULL) { - fprintf(stderr, "1 %s: %s\n", argv[2], errbuf); - exit(EXIT_FAILURE); - } - if ((outputHandle = pcap_dump_fopen(handle, filename)) == NULL) { - fprintf(stderr, "2 %s: %s\n", argv[2], errbuf); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[1], "--interface") == 0) { - inject_mode = LIVE_INJECT; - - if ((handle = pcap_create(argv[2], errbuf)) == NULL) { - fprintf(stderr, "Couldn't open interface %s: %s\n", argv[2], errbuf); - exit(EXIT_FAILURE); - } - if (pcap_activate(handle) == 0) { - printf("Sniffing on %s\n", argv[2]); - } else { - fprintf(stderr, "Couldn't activate %s: %s\n", argv[2], pcap_geterr(handle)); - exit(EXIT_FAILURE); - } - } else { - help(argv[0]); - exit(EXIT_SUCCESS); - } - - if (strcmp(argv[3], "--beacon") == 0) { - mode = MODE_BEACON; - } else if (strcmp(argv[3], "--random-beacon") == 0) { - mode = MODE_RANDOM_BEACON; - } else if (strcmp(argv[3], "--probe-resp") == 0) { - mode = MODE_PROBE_RESPONSE; - } else if (strcmp(argv[3], "--probe-req") == 0) { - mode = MODE_PROBE_REQUEST; - } else if (strcmp(argv[3], "--deauth") == 0) { - mode = MODE_DEAUTH; - } else if (strcmp(argv[3], "--disassoc") == 0) { - mode = MODE_DISASSOC; - } else if (strcmp(argv[3], "--assoc-resp") == 0) { - mode = MODE_ASSOC_RESPONSE; - } else if (strcmp(argv[3], "--assoc-req") == 0) { - mode = MODE_ASSOC_REQUEST; - } else if (strcmp(argv[3], "--reassoc-resp") == 0) { - mode = MODE_REASSOC_RESPONSE; - } else if (strcmp(argv[3], "--reassoc-req") == 0) { - mode = MODE_REASSOC_REQUEST; - } else if (strcmp(argv[3], "--auth") == 0) { - mode = MODE_AUTH; - } else if (strcmp(argv[3], "--timing-ad") == 0) { - mode = MODE_TIMING_AD; - } else if (strcmp(argv[3], "--action") == 0) { - mode = MODE_ACTION; - } else if (strcmp(argv[3], "--action-noack") == 0) { - mode = MODE_ACTION_NOACK; - } else if (strcmp(argv[3], "--atim") == 0) { - mode = MODE_ATIM; - } else if (strcmp(argv[3], "--rts") == 0) { - mode = MODE_RTS; - } else if (strcmp(argv[3], "--cts") == 0) { - mode = MODE_CTS; - } else { - help(argv[0]); - exit(EXIT_SUCCESS); - } -} - -int main(int argc, const char *argv[]) { - signal(SIGINT, handle_interupt); - handle_args(argc, argv); - - printf("Starting in 5 seconds...\n"); - - sleep(5); - - switch (mode) { - case MODE_BEACON: - inject_beacons(0); - break; - case MODE_RANDOM_BEACON: - inject_beacons(1); - break; - case MODE_PROBE_RESPONSE: - inject_probe_responses(); - break; - case MODE_PROBE_REQUEST: - inject_probe_requests(); - break; - case MODE_DEAUTH: - inject_deauths(); - break; - case MODE_DISASSOC: - inject_disassocs(); - break; - case MODE_ASSOC_REQUEST: - inject_assoc_requests(); - break; - case MODE_ASSOC_RESPONSE: - inject_assoc_responses(); - break; - case MODE_REASSOC_REQUEST: - inject_reassoc_requests(); - break; - case MODE_REASSOC_RESPONSE: - inject_reassoc_responses(); - break; - case MODE_AUTH: - inject_auths(); - break; - case MODE_ACTION: - inject_actions(); - break; - case MODE_ACTION_NOACK: - inject_action_noacks(); - break; - case MODE_TIMING_AD: - inject_timing_ads(); - break; - case MODE_ATIM: - inject_atim(); - break; - case MODE_RTS: - inject_rts(); - break; - case MODE_CTS: - inject_cts(); - break; - } - - return 0; -} diff --git a/test/src/test_misc.c b/test/src/test_misc.c deleted file mode 100644 index f103455..0000000 --- a/test/src/test_misc.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include - -void gen_macs() { - printf("Getting 10 random MAC addresses:\n"); - for(int i = 0; i < 10; i++) { - unsigned char mac[6] = {0}; - libwifi_random_mac(mac, NULL); - printf(MACSTR "\n", MAC2STR(mac)); - } - - printf("Generating 10 random MAC addresses with 00:20:91 OUI:\n"); - for(int i = 0; i < 10; i++) { - unsigned char mac[6] = {0}; - libwifi_random_mac(mac, (unsigned char *) "\x00\x20\x91"); - printf(MACSTR "\n", MAC2STR(mac)); - } - printf("\n"); -} - -int main() { - libwifi_dummy(); - - printf("libwifi version: %s\n\n", libwifi_get_version()); - - gen_macs(); - - return 0; -} diff --git a/test/src/test_parsing.c b/test/src/test_parsing.c deleted file mode 100644 index c345346..0000000 --- a/test/src/test_parsing.c +++ /dev/null @@ -1,613 +0,0 @@ -#include "helpers.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define PCAP_SAVEFILE "/tmp/debug.pcap" -#define FILTER "" -#define MODE_BEACON 1 -#define MODE_PROBE_RESPONSE 2 -#define MODE_PROBE_REQUEST 3 -#define MODE_EAPOL 4 -#define MODE_DEAUTH 5 -#define MODE_DISASSOC 6 -#define MODE_ASSOC_RESPONSE 7 -#define MODE_ASSOC_REQUEST 8 -#define MODE_REASSOC_REQUEST 9 -#define MODE_REASSOC_RESPONSE 10 -#define MODE_DATA 11 -#define MODE_ALL 99 - -static pcap_t *handle; -pcap_dumper_t *pd; -static struct bpf_program *filter; -static int got_radiotap; -static unsigned long packet_num = 0; -static int mode = 0; -static int parse_radiotap_header = 0; - -struct libwifi_bss bss = {0}; -struct libwifi_sta sta = {0}; - -void help(const char *); -void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet); -void print_bss_info(struct libwifi_bss *bss); -void print_sta_info(struct libwifi_sta *sta); -void print_tag_info(unsigned char *data, size_t data_len); - -void interrupted(int signum) { - pcap_dump_close(pd); - pcap_close(handle); -} - -void print_bss_info(struct libwifi_bss *bss) { - if (bss == NULL) { - return; - } - - printf("=== BSS Parsing ===\n"); - printf("ESSID: %s\n", bss->hidden ? "(hidden)" : bss->ssid); - printf("BSSID: " MACSTR "\n", MAC2STR(bss->bssid)); - printf("Receiver: " MACSTR "\n", MAC2STR(bss->receiver)); - printf("Transmitter: " MACSTR "\n", MAC2STR(bss->transmitter)); - printf("Channel: %d\n", bss->channel); - printf("WPS: %s\n", bss->wps ? "yes" : "no"); - - char sec_buf[LIBWIFI_SECURITY_BUF_LEN]; - libwifi_get_security_type(bss, sec_buf); - printf("Encryption: %s\n", sec_buf); - - libwifi_get_group_ciphers(bss, sec_buf); - printf("\tGroup Ciphers: %s\n", sec_buf); - - libwifi_get_pairwise_ciphers(bss, sec_buf); - printf("\tPairwise Ciphers: %s\n", sec_buf); - - libwifi_get_auth_key_suites(bss, sec_buf); - printf("\tAuth Key Suites: %s\n", sec_buf); - - if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) { - printf("\tMFP Capable: Yes\n"); - } - if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) { - printf("\tMFP Required: Yes\n"); - } - - if (bss->tags.length) { - printf("Tagged Parameters:\n"); - print_tag_info(bss->tags.parameters, bss->tags.length); - } else { - printf("Tagged Parameters: None\n"); - } - - printf("=== BSS End ===\n"); - printf("\n\n"); -} - -void print_sta_info(struct libwifi_sta *sta) { - if (sta == NULL) { - return; - } - - printf("=== STA Parsing ===\n"); - - if (sta->broadcast_ssid) { - printf("ESSID: \n"); - } else { - printf("ESSID: %s\n", sta->ssid); - } - printf("Channel: %u\n", sta->channel); - printf("BSSID: " MACSTR "\n", MAC2STR(sta->bssid)); - printf("MAC: " MACSTR "\n", MAC2STR(sta->transmitter)); - - printf("=== STA End ===\n"); - printf("\n\n"); -} - -void print_tag_info(unsigned char *data, size_t data_len) { - struct libwifi_tag_iterator it; - if (libwifi_tag_iterator_init(&it, data, data_len) != 0) { - printf("Couldn't initialise tag iterator\n"); - return; - } - do { - printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len); - - int max_size = 16; - if (it.tag_header->tag_len < 16) { - max_size = it.tag_header->tag_len; - } - printf("\t%d bytes of Tag Data: ", max_size); - for (size_t i = 0; i < max_size; i++) { - printf("%02x ", it.tag_data[i]); - } - printf("\n"); - } while (libwifi_tag_iterator_next(&it) != -1); -} - -void parse_radiotap(const unsigned char *packet) { - struct libwifi_radiotap_info rtap_info; - libwifi_parse_radiotap_info(&rtap_info, packet); - - printf("=== Radiotap Parsing ===\n"); - printf("Radiotap Channel: %d\n", rtap_info.channel.freq); - printf("Radiotap Channel Flags: 0x%04x\n", rtap_info.channel.flags); - printf("Radiotap Rate: %.2f Mb/s\n", rtap_info.rate); - printf("Radiotap Rate Raw: 0x%02x\n", rtap_info.rate_raw); - printf("Radiotap Signal: %d dBm\n", rtap_info.signal); - for (int i = 0; i < rtap_info.antenna_count; i++) { - printf("Radiotap Antenna %d: %d dBm\n", rtap_info.antennas[i].antenna_number, rtap_info.antennas[i].signal); - } - printf("Radiotap Flags: 0x%04x\n", rtap_info.flags); - printf("Radiotap Extended Flags: 0x%08x\n", rtap_info.extended_flags); - printf("Radiotap RX Flags: 0x%04x\n", rtap_info.rx_flags); - printf("Radiotap TX Flags: 0x%04x\n", rtap_info.tx_flags); - printf("Radiotap TX Power: %d\n", rtap_info.tx_power); - printf("Radiotap RTS Retries: %d\n", rtap_info.rts_retries); - printf("Radiotap Data Retries: %d\n", rtap_info.data_retries); - printf("=== Radiotap End ===\n"); -} - -void parse_beacon(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_beacon(&bss, &frame); - if (ret != 0) { - printf("Failed to parse beacon: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_bss_info(&bss); - } -} - -void parse_probe_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_REQ) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_probe_req(&sta, &frame); - if (ret != 0) { - printf("Failed to parse probe request: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_sta_info(&sta); - } -} -void parse_probe_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_RESP) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_probe_resp(&bss, &frame); - if (ret != 0) { - printf("Failed to parse probe response: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_bss_info(&bss); - } -} -void parse_deauth(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DEAUTH) { - printf("Packet : %lu\n", packet_num); - struct libwifi_parsed_deauth deauth; - int ret = libwifi_parse_deauth(&deauth, &frame); - if (ret != 0) { - printf("Failed to parse deauthentication: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - printf("=== Deauthentication Frame ===\n"); - if (deauth.ordered) { - printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr1)); - printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr2)); - printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr3)); - } else { - printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr1)); - printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr2)); - printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr3)); - } - - printf("Reason: %d (0x%04x)\n", deauth.fixed_parameters.reason_code, deauth.fixed_parameters.reason_code); - - if (deauth.tags.length) { - printf("Tagged Parameters:\n"); - print_tag_info(deauth.tags.parameters, deauth.tags.length); - } else { - printf("Tagged Parameters: None\n"); - } - - printf("=== End Deauthentication Frame ===\n"); - printf("\n\n"); - } -} -void parse_disassoc(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DISASSOC) { - printf("Packet : %lu\n", packet_num); - struct libwifi_parsed_disassoc disassoc; - int ret = libwifi_parse_disassoc(&disassoc, &frame); - if (ret != 0) { - printf("Failed to parse diassociation: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - printf("=== Disassociation Frame ===\n"); - if (disassoc.ordered) { - printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr1)); - printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr2)); - printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr3)); - } else { - printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr1)); - printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr2)); - printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr3)); - } - - printf("Reason: %d (0x%04x)\n", disassoc.fixed_parameters.reason_code, disassoc.fixed_parameters.reason_code); - - printf("Tagged Parameters:\n"); - if (disassoc.tags.length == 0) { - printf("\tNo Tags\n"); - } else { - printf("\tTags Found\n"); - } - - printf("=== End Disassociation Frame ===\n"); - printf("\n\n"); - } -} -void parse_assoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_REQ) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_assoc_req(&sta, &frame); - if (ret != 0) { - printf("Failed to parse association request: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_sta_info(&sta); - } -} -void parse_assoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_RESP) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_assoc_resp(&bss, &frame); - if (ret != 0) { - printf("Failed to parse association response: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_bss_info(&bss); - } -} -void parse_reassoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_REQ) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_reassoc_req(&sta, &frame); - if (ret != 0) { - printf("Failed to parse reassociation request: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_sta_info(&sta); - } -} -void parse_reassoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_RESP) { - printf("Packet : %lu\n", packet_num); - int ret = libwifi_parse_reassoc_resp(&bss, &frame); - if (ret != 0) { - printf("Failed to parse reassociation response: %d\n", ret); - pcap_dump(args, header, packet); - return; - } - - if (got_radiotap && parse_radiotap_header) { - parse_radiotap(packet); - } - - print_bss_info(&bss); - } -} -void parse_data_eapol(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_DATA) { - if (libwifi_check_wpa_handshake(&frame) > 0) { - printf("=== EAPOL ===\n"); - printf("WPA Handshake\n"); - int part = libwifi_check_wpa_message(&frame); - printf("WPA Handshake Message: %s\n", libwifi_get_wpa_message_string(&frame)); - - struct libwifi_wpa_auth_data data = {0}; - libwifi_get_wpa_data(&frame, &data); - - printf("EAPOL: Version: %d\n", data.version); - printf("EAPOL: Type: %d\n", data.type); - printf("EAPOL: Length: %d\n", data.length); - printf("EAPOL: Descriptor: %d\n", data.descriptor); - printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information); - printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length); - printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter); - printf("EAPOL: Key Info: Nonce: "); - for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) printf("%02x ", data.key_info.nonce[i]); - printf("\n"); - printf("EAPOL: Key Info: IV: "); - for (size_t i = 0; i < sizeof(data.key_info.iv); ++i) printf("%02x ", data.key_info.iv[i]); - printf("\n"); - printf("EAPOL: Key Info: RSC: "); - for (size_t i = 0; i < sizeof(data.key_info.rsc); ++i) printf("%02x ", data.key_info.rsc[i]); - printf("\n"); - printf("EAPOL: Key Info: ID: "); - for (size_t i = 0; i < sizeof(data.key_info.id); ++i) printf("%02x ", data.key_info.id[i]); - printf("\n"); - printf("EAPOL: Key Info: MIC: "); - for (size_t i = 0; i < sizeof(data.key_info.mic); ++i) printf("%02x ", data.key_info.mic[i]); - printf("\n"); - printf("EAPOL: Key Info: Key Data Length: %d\n", data.key_info.key_data_length); - if (data.key_info.key_data_length) { - printf("EAPOL: Key Info: Key Data: "); - for (size_t i = 0; i < data.key_info.key_data_length; ++i) printf("%02x ", data.key_info.key_data[i]); - printf("\n"); - } - - libwifi_free_wpa_data(&data); - - printf("\n\n"); - } - } -} - -void parse_data(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - if (frame.frame_control.type == TYPE_DATA) { - if (frame.flags & LIBWIFI_FLAGS_IS_QOS) { - printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr1)); - printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr2)); - } else { - printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data.addr1)); - printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data.addr2)); - } - printf("Body Length: %zu\n", frame.len - frame.header_len); - printf("Body:\n"); - hexdump(frame.body, frame.len - frame.header_len); - } -} - -void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { - ++packet_num; - unsigned long data_len = header->caplen; - unsigned char *data = (unsigned char *) packet; - - struct libwifi_frame frame = {0}; - int ret = libwifi_get_wifi_frame(&frame, data, data_len, 1); - if (ret != 0) { - printf("[!] Error getting libwifi_frame: %d\n", ret); - return; - } - - memset(&bss, 0, sizeof(struct libwifi_bss)); - memset(&sta, 0, sizeof(struct libwifi_sta)); - - switch (mode) { - case MODE_BEACON: - parse_beacon(frame, args, header, packet); - break; - case MODE_PROBE_REQUEST: - parse_probe_request(frame, args, header, packet); - break; - case MODE_PROBE_RESPONSE: - parse_probe_response(frame, args, header, packet); - break; - case MODE_DEAUTH: - parse_deauth(frame, args, header, packet); - break; - case MODE_DISASSOC: - parse_disassoc(frame, args, header, packet); - break; - case MODE_ASSOC_REQUEST: - parse_assoc_request(frame, args, header, packet); - break; - case MODE_ASSOC_RESPONSE: - parse_assoc_response(frame, args, header, packet); - break; - case MODE_REASSOC_REQUEST: - parse_reassoc_request(frame, args, header, packet); - break; - case MODE_REASSOC_RESPONSE: - parse_reassoc_response(frame, args, header, packet); - break; - case MODE_EAPOL: - parse_data_eapol(frame, args, header, packet); - break; - case MODE_DATA: - parse_data(frame, args, header, packet); - break; - case MODE_ALL: - parse_beacon(frame, args, header, packet); - parse_probe_request(frame, args, header, packet); - parse_probe_response(frame, args, header, packet); - parse_deauth(frame, args, header, packet); - parse_disassoc(frame, args, header, packet); - parse_assoc_request(frame, args, header, packet); - parse_assoc_response(frame, args, header, packet); - parse_reassoc_request(frame, args, header, packet); - parse_reassoc_response(frame, args, header, packet); - parse_data_eapol(frame, args, header, packet); - parse_data(frame, args, header, packet); - default: - break; - } - - libwifi_free_bss(&bss); - libwifi_free_wifi_frame(&frame); -} - -void help(const char *name) { - fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\t%s --interface [interface] [--mode] [--radiotap]\n", name); - fprintf(stderr, "\t\tor\n"); - fprintf(stderr, "\t%s --file [capture file] [--mode] [--radiotap]\n", name); - fprintf(stderr, "\n"); - fprintf(stderr, "Modes:\n"); - fprintf(stderr, "\t--beacon\n"); - fprintf(stderr, "\t--probe-req\n"); - fprintf(stderr, "\t--probe-resp\n"); - fprintf(stderr, "\t--deauth\n"); - fprintf(stderr, "\t--disassoc\n"); - fprintf(stderr, "\t--assoc-req\n"); - fprintf(stderr, "\t--assoc-resp\n"); - fprintf(stderr, "\t--reassoc-req\n"); - fprintf(stderr, "\t--reassoc-resp\n"); - fprintf(stderr, "\t--eapol\n"); -} - -void handle_args(int argc, const char *argv[]) { - char errbuf[PCAP_ERRBUF_SIZE]; - - if (argc < 4) { - help(argv[0]); - exit(EXIT_SUCCESS); - } - - if (strcmp(argv[1], "--file") == 0) { - if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) { - fprintf(stderr, "Couldn't read file %s: %s\n", argv[2], errbuf); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[1], "--interface") == 0) { - if ((handle = pcap_create(argv[2], errbuf)) == NULL) { - fprintf(stderr, "Failed to open interface \"%s\" for sniffing: %s\n", argv[2], errbuf); - exit(EXIT_FAILURE); - } - if (pcap_activate(handle) == 0) { - printf("[+] Started sniffing on %s\n", argv[2]); - } else { - fprintf(stderr, "[!] Couldn't activate capture: %s.\n", pcap_geterr(handle)); - pcap_close(handle); - exit(EXIT_FAILURE); - } - } else { - help(argv[0]); - exit(EXIT_SUCCESS); - } - - if (strcmp(argv[3], "--beacon") == 0) { - mode = MODE_BEACON; - } else if (strcmp(argv[3], "--probe-req") == 0) { - mode = MODE_PROBE_REQUEST; - } else if (strcmp(argv[3], "--probe-resp") == 0) { - mode = MODE_PROBE_RESPONSE; - } else if (strcmp(argv[3], "--deauth") == 0) { - mode = MODE_DEAUTH; - } else if (strcmp(argv[3], "--disassoc") == 0) { - mode = MODE_DISASSOC; - } else if (strcmp(argv[3], "--assoc-req") == 0) { - mode = MODE_ASSOC_REQUEST; - } else if (strcmp(argv[3], "--assoc-resp") == 0) { - mode = MODE_ASSOC_RESPONSE; - } else if (strcmp(argv[3], "--reassoc-req") == 0) { - mode = MODE_REASSOC_REQUEST; - } else if (strcmp(argv[3], "--reassoc-resp") == 0) { - mode = MODE_REASSOC_RESPONSE; - } else if (strcmp(argv[3], "--eapol") == 0) { - mode = MODE_EAPOL; - } else if (strcmp(argv[3], "--data") == 0) { - mode = MODE_DATA; - } else if (strcmp(argv[3], "--all") == 0) { - mode = MODE_ALL; - } else { - help(argv[0]); - exit(EXIT_SUCCESS); - } - - if (argc > 4) { - if (strcmp(argv[4], "--radiotap") == 0) { - parse_radiotap_header = 1; - } - } -} - -int main(int argc, const char *argv[]) { - packet_num = 0; - char errbuf[PCAP_ERRBUF_SIZE]; - - handle_args(argc, argv); - - int linktype = pcap_datalink(handle); - if (linktype == DLT_IEEE802_11_RADIO) { - got_radiotap = 1; - } else if (linktype == DLT_IEEE802_11) { - got_radiotap = 0; - } else { - fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle)); - pcap_close(handle); - exit(EXIT_FAILURE); - } - - if ((filter = malloc(sizeof(struct bpf_program))) == NULL) { - perror("Malloc failure"); - pcap_close(handle); - exit(EXIT_FAILURE); - } - printf("[*] Compiling and optimizing frame filter, this can take a second\n"); - if (pcap_compile(handle, filter, FILTER, 0, 0) != 0) { - fprintf(stderr, "[!] Couldn't compile filter: %s\n", pcap_geterr(handle)); - pcap_close(handle); - free(filter); - exit(EXIT_FAILURE); - } - if (pcap_setfilter(handle, filter) != 0) { - fprintf(stderr, "[!] Couldn't set filter: %s\n", pcap_geterr(handle)); - pcap_close(handle); - free(filter); - exit(EXIT_FAILURE); - } - printf("[+] Complete\n"); - - pd = pcap_dump_open(handle, PCAP_SAVEFILE); - pcap_loop(handle, -1 /*INFINITY*/, &parse_packet, (unsigned char *) pd); - - return 0; -} diff --git a/test/src/timing_ad_tests.c b/test/src/timing_ad_tests.c new file mode 100644 index 0000000..59d20eb --- /dev/null +++ b/test/src/timing_ad_tests.c @@ -0,0 +1,57 @@ +#include "../../src/libwifi.h" + +#include +#include +#include + +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +const unsigned char to[] = TO_MAC; +const unsigned char bcast[] = BCAST_MAC; + +int test_timing_ad_gen_full() { + struct libwifi_timing_advert time_ad = {0}; + struct libwifi_timing_advert_fields ad_fields = {0}; + + ad_fields.timing_capabilities = 2; + memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5); + memcpy(ad_fields.time_update, "\xBB", 1); + memcpy(ad_fields.time_value, + "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10); + + int ret = libwifi_create_timing_advert(&time_ad, to, to, &ad_fields, "GB", -56, -56, -30, -20); + if (ret != 0) { + fprintf(stderr, "Failed to create timing advert\n"); + return ret; + } + + unsigned char *buf = NULL; + size_t buf_len = libwifi_get_timing_advert_length(&time_ad); + buf = malloc(buf_len); + if (buf == NULL) { + fprintf(stderr, "Failed to create buffer\n"); + return -1; + } + printf("buf_len: %zu\n", buf_len); + + ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len); + if (ret < 0) { + fprintf(stderr, "Failed to dump advert"); + return ret; + } + + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2) { + printf("Specify test\n"); + return -1; + } + + if (strcmp(argv[1], "--timing_ad-gen-full") == 0) { + return test_timing_ad_gen_full(); + } + + return -1; +} diff --git a/utils/.clang-format b/utils/.clang-format new file mode 100644 index 0000000..111249f --- /dev/null +++ b/utils/.clang-format @@ -0,0 +1,8 @@ +--- +BasedOnStyle: LLVM +IndentWidth: '4' +SpaceAfterCStyleCast: 'true' +ColumnLimit: 400 +AllowShortFunctionsOnASingleLine: None +IndentCaseLabels: 'true' +... diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt new file mode 100644 index 0000000..bdf7227 --- /dev/null +++ b/utils/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.18) + +project(libwifi_tests VERSION 0.1) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") + +add_executable(test_misc src/helpers.c src/test_misc.c) +add_executable(test_generation src/helpers.c src/test_generation.c) +add_executable(test_parsing src/helpers.c src/test_parsing.c) +target_link_libraries(test_misc wifi) +target_link_libraries(test_generation wifi pcap) +target_link_libraries(test_parsing wifi pcap) diff --git a/utils/src/helpers.c b/utils/src/helpers.c new file mode 100644 index 0000000..9fc9d0b --- /dev/null +++ b/utils/src/helpers.c @@ -0,0 +1,31 @@ +#include "helpers.h" +#include + +void hexdump(void *data, size_t size) { + char ascii[17]; + size_t i, j; + ascii[16] = '\0'; + for (i = 0; i < size; ++i) { + printf("%02X ", ((unsigned char *) data)[i]); + if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { + ascii[i % 16] = ((unsigned char *) data)[i]; + } else { + ascii[i % 16] = '.'; + } + if ((i + 1) % 8 == 0 || i + 1 == size) { + printf(" "); + if ((i + 1) % 16 == 0) { + printf("| %s \n", ascii); + } else if (i + 1 == size) { + ascii[(i + 1) % 16] = '\0'; + if ((i + 1) % 16 <= 8) { + printf(" "); + } + for (j = (i + 1) % 16; j < 16; ++j) { + printf(" "); + } + printf("| %s \n", ascii); + } + } + } +} diff --git a/utils/src/helpers.h b/utils/src/helpers.h new file mode 100644 index 0000000..99a5329 --- /dev/null +++ b/utils/src/helpers.h @@ -0,0 +1,60 @@ +#include +#include + +#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] +#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" + +static const uint8_t radiotap_data[] = { + 0x00, + 0x00, // <-- radiotap version (ignore this) + 0x18, + 0x00, // <-- number of bytes in our header (count the number of "0x"s) + + /** + * The next field is a bitmap of which options we are including. + * The full list of which field is which option is in ieee80211_radiotap.h, + * but I've chosen to include: + * 0x00 0x01: timestamp + * 0x00 0x02: flags + * 0x00 0x03: rate + * 0x00 0x04: channel + * 0x80 0x00: tx flags (seems silly to have this AND flags, but oh well) + */ + 0x0f, + 0x80, + 0x00, + 0x00, + + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, // <-- timestamp + + /** + * This is the first set of flags, and we've set the bit corresponding to + * IEEE80211_RADIOTAP_F_FCS, meaning we want the card to add a FCS at the + * end of our buffer for us. + */ + 0x10, + + 0x00, // <-- rate + 0x00, + 0x00, + 0x00, + 0x00, // <-- channel + + /** + * This is the second set of flags, specifically related to transmissions. + * The bit we've set is IEEE80211_RADIOTAP_F_TX_NOACK, which means the card + * won't wait for an ACK for this frame, and that it won't retry if it + * doesn't get one. + */ + 0x08, + 0x00, +}; + +void hexdump(void *data, size_t size); diff --git a/utils/src/test_generation.c b/utils/src/test_generation.c new file mode 100644 index 0000000..407e87f --- /dev/null +++ b/utils/src/test_generation.c @@ -0,0 +1,924 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "helpers.h" + +#define LIVE_INJECT 0 +#define OFFLINE_DUMP 1 + +#define MODE_BEACON 0 +#define MODE_PROBE_RESPONSE 1 +#define MODE_PROBE_REQUEST 2 +#define MODE_DEAUTH 3 +#define MODE_DISASSOC 4 +#define MODE_ASSOC_RESPONSE 5 +#define MODE_ASSOC_REQUEST 6 +#define MODE_REASSOC_RESPONSE 7 +#define MODE_REASSOC_REQUEST 8 +#define MODE_AUTH 9 +#define MODE_RTS 10 +#define MODE_CTS 11 +#define MODE_RANDOM_BEACON 12 +#define MODE_ACTION 13 +#define MODE_ACTION_NOACK 14 +#define MODE_TIMING_AD 15 +#define MODE_ATIM 16 + +#define SNAPLEN 96 +#define CHANNEL 11 +#define BCAST_MAC "\xff\xff\xff\xff\xff\xff" +#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" +#define FROM_MAC "\x00\x20\x91\x11\x22\x33" +#define REASSOC_MAC "\xAA\xBB\xCC\xDD\xEE\xFF" +#define BEACON_SSID "libwifi-beacon" +#define PROBE_RESP_SSID "libwifi-probe-resp" +#define PROBE_REQ_SSID "libwifi-probe-req" +#define ASSOC_REQ_SSID "libwifi-assoc-req" +#define REASSOC_REQ_SSID "libwifi-reassoc-req" + +pcap_t *handle = NULL; +pcap_dumper_t *outputHandle = NULL; +FILE *filename = NULL; + +static unsigned char to[] = TO_MAC; +static unsigned char from[] = FROM_MAC; +static unsigned char bcast[] = BCAST_MAC; +static unsigned char reassoc_mac[] = REASSOC_MAC; +static unsigned char tag_data[] = "\x00\x00\00\x01This is a 221 tag from libwifi.\n"; + +static int mode = 0; +static int inject_mode = 0; + +void handle_interupt(int signal) { + if (signal == SIGINT) { + int oldmode = inject_mode; + mode = -1; + inject_mode = -1; + + if (oldmode == LIVE_INJECT) { + pcap_close(handle); + printf("\n\nClosed Capture Handle!\n"); + } else if (oldmode == OFFLINE_DUMP) { + pcap_dump_flush(outputHandle); + pcap_dump_close(outputHandle); + printf("\n\nDumped and Closed Output File!\n"); + } + + exit(EXIT_SUCCESS); + } +} + +void inject_frame(void *buf, size_t buf_sz) { + struct libwifi_radiotap_info info = {0}; + info.present = 0x0000002e; // 0x002e: Flags, Rate, Channel, dBm Ant Signal + info.channel.flags = 0x0140; // OFDM, 5GHz + info.channel.freq = 5180; // Channel 46 + info.flags = 0x0000; // No Flags + info.rate = 1; // 1 Mbit + info.rate_raw = info.rate * 2; // Radiotap uses 500kb/s increments + info.signal = -20; // Signal in dBm + + char *rtap = NULL; + rtap = malloc(LIBWIFI_MAX_RADIOTAP_LEN); + if (rtap == NULL) { + printf("malloc failure: %s\n", strerror(errno)); + return; + } + memset(rtap, 0, LIBWIFI_MAX_RADIOTAP_LEN); + + int rtap_len = libwifi_create_radiotap(&info, rtap); + if (rtap_len == -1) { + printf("error generating radiotap header\n"); + return; + } + + void *frame = NULL; + size_t frame_sz = rtap_len + buf_sz; + frame = malloc(frame_sz); + if (frame == NULL) { + printf("malloc failure: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + memcpy(frame, rtap, rtap_len); + memcpy(frame + rtap_len, buf, buf_sz); + + hexdump(rtap, rtap_len); + printf("-----\n"); + hexdump(frame, frame_sz); + + if (inject_mode == LIVE_INJECT) { + pcap_inject(handle, frame, frame_sz); + } else if (inject_mode == OFFLINE_DUMP) { + struct pcap_pkthdr hdr = {0}; + hdr.caplen = frame_sz; + hdr.len = frame_sz; + struct timeval tv; + gettimeofday(&tv, NULL); + hdr.ts = tv; + pcap_dump((unsigned char *) outputHandle, &hdr, frame); + } + + free(rtap); + free(frame); +} + +void inject_beacons(int random_mac) { + while (1) { + printf("Sending 50 beacons...\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_beacon beacon; + unsigned char txmac[6] = {0}; + memset(&beacon, 0, sizeof(struct libwifi_beacon)); + + if (random_mac) { + libwifi_random_mac(txmac, NULL); + } else { + memcpy(txmac, FROM_MAC, 6); + } + libwifi_create_beacon(&beacon, bcast, txmac, BEACON_SSID, CHANNEL); + libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data)); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_beacon_length(&beacon); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting beacon with:\n"); + printf("\tSSID: %s\n", BEACON_SSID); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(txmac)); + printf("\tDestination: " MACSTR "\n", MAC2STR(bcast)); + + libwifi_dump_beacon(&beacon, buf, buf_sz); + inject_frame(buf, buf_sz); + + libwifi_free_beacon(&beacon); + free(buf); + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_probe_responses() { + while (1) { + printf("Sending 50 probe responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_probe_resp probe_resp; + memset(&probe_resp, 0, sizeof(struct libwifi_probe_resp)); + + libwifi_create_probe_resp(&probe_resp, to, from, PROBE_RESP_SSID, CHANNEL); + libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data)); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_probe_resp_length(&probe_resp); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting probe responses with:\n"); + printf("\tSSID: %s\n", PROBE_RESP_SSID); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_probe_resp(&probe_resp, buf, buf_sz); + inject_frame(buf, buf_sz); + + libwifi_free_probe_resp(&probe_resp); + free(buf); + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_probe_requests() { + while (1) { + printf("Sending 50 probe responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_probe_req probe; + memset(&probe, 0, sizeof(struct libwifi_probe_req)); + + libwifi_create_probe_req(&probe, to, from, to, PROBE_REQ_SSID, CHANNEL); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_probe_req_length(&probe); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting probe requests with:\n"); + printf("\tSSID: %s\n", PROBE_REQ_SSID); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_probe_req(&probe, buf, buf_sz); + inject_frame(buf, buf_sz); + + libwifi_free_probe_req(&probe); + free(buf); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_deauths() { + while (1) { + printf("Sending 50 probe responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_deauth deauth; + memset(&deauth, 0, sizeof(struct libwifi_deauth)); + + libwifi_create_deauth(&deauth, to, from, REASON_STA_LEAVING); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_deauth_length(&deauth); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting deauths with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tReason: %d\n", REASON_STA_LEAVING); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_deauth(&deauth, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_disassocs() { + while (1) { + printf("Sending 50 probe responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_disassoc disassoc; + memset(&disassoc, 0, sizeof(struct libwifi_disassoc)); + + libwifi_create_disassoc(&disassoc, to, from, REASON_STA_LEAVING); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_disassoc_length(&disassoc); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting disassocs with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tReason: %d\n", REASON_STA_LEAVING); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_disassoc(&disassoc, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_assoc_requests() { + while (1) { + printf("Sending 50 association requests, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_assoc_req assoc_req; + memset(&assoc_req, 0, sizeof(struct libwifi_assoc_req)); + + libwifi_create_assoc_req(&assoc_req, to, from, ASSOC_REQ_SSID, CHANNEL); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_assoc_req_length(&assoc_req); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting association requests with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_assoc_req(&assoc_req, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + libwifi_free_assoc_req(&assoc_req); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_assoc_responses() { + while (1) { + printf("Sending 50 association responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_assoc_resp assoc_resp; + memset(&assoc_resp, 0, sizeof(struct libwifi_assoc_req)); + + libwifi_create_assoc_resp(&assoc_resp, to, from, CHANNEL); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_assoc_resp_length(&assoc_resp); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting association responses with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_assoc_resp(&assoc_resp, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + libwifi_free_assoc_resp(&assoc_resp); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_reassoc_requests() { + while (1) { + printf("Sending 50 reassociation requests, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_reassoc_req reassoc_req; + memset(&reassoc_req, 0, sizeof(struct libwifi_assoc_req)); + + libwifi_create_reassoc_req(&reassoc_req, to, from, reassoc_mac, REASSOC_REQ_SSID, CHANNEL); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_reassoc_req_length(&reassoc_req); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting reassociation requests with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + printf("\tPrevious BSSID: " MACSTR "\n", MAC2STR(reassoc_mac)); + + libwifi_dump_reassoc_req(&reassoc_req, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + libwifi_free_reassoc_req(&reassoc_req); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_reassoc_responses() { + while (1) { + printf("Sending 50 reassociation responses, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_reassoc_resp reassoc_resp; + memset(&reassoc_resp, 0, sizeof(struct libwifi_assoc_req)); + + libwifi_create_reassoc_resp(&reassoc_resp, to, from, CHANNEL); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_reassoc_resp_length(&reassoc_resp); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting reassociation responses with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_reassoc_resp(&reassoc_resp, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + libwifi_free_reassoc_resp(&reassoc_resp); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_auths() { + while (1) { + printf("Sending 50 auth frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_auth auth; + memset(&auth, 0, sizeof(struct libwifi_deauth)); + + libwifi_create_auth(&auth, to, from, AUTH_OPEN, 0, STATUS_SUCCESS); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_auth_length(&auth); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + libwifi_dump_auth(&auth, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + + memset(&auth, 0, sizeof(struct libwifi_deauth)); + + libwifi_create_auth(&auth, from, to, AUTH_OPEN, 1, STATUS_SUCCESS); + + buf = NULL; + buf_sz = libwifi_get_auth_length(&auth); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting auths with:\n"); + printf("\tChannel: %d\n", CHANNEL); + printf("\tAlgorithm: %d\n", AUTH_OPEN); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_auth(&auth, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_timing_ads() { + while (1) { + printf("Sending 50 timing advertisement frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_timing_advert time_ad = {0}; + struct libwifi_timing_advert_fields ad_fields = {0}; + + ad_fields.timing_capabilities = 2; + memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5); + memcpy(ad_fields.time_update, "\xBB", 1); + memcpy(ad_fields.time_value, + "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10); + + libwifi_create_timing_advert(&time_ad, to, from, &ad_fields, "GB", -56, -56, -30, -20); + + unsigned char *buf = NULL; + size_t buf_len = libwifi_get_timing_advert_length(&time_ad); + buf = malloc(buf_len); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + printf("buf_len: %zu\n", buf_len); + + size_t ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len); + if (ret < 0) { + printf("error dump: %zu\n", ret); + exit(EXIT_FAILURE); + } + hexdump(buf, buf_len); + inject_frame(buf, buf_len); + + free(buf); + libwifi_free_timing_advert(&time_ad); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_action_noacks() { + while (1) { + printf("Sending 50 action no ack frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_action action; + memset(&action, 0, sizeof(struct libwifi_action)); + + libwifi_create_action_no_ack(&action, to, from, ACTION_FAST_BSS_TRANSITION); + + unsigned char *action_buf = malloc(256); + memset(action_buf, 0, 256); + + size_t offset = 0; + size_t w = 0; + + memcpy(action_buf, "\x01", 1); // Fast BSS Request + offset += 1; + memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address + offset += 6; + memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address + offset += 6; + + unsigned char *tag_tmp = malloc(256); + memset(tag_tmp, 0, 256); + + struct libwifi_tagged_parameter rsne = {0}; + size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20); + w = libwifi_dump_tag(&rsne, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + + + struct libwifi_tagged_parameter mobdom = {0}; + tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3); + memset(tag_tmp, 0, tsz); + w = libwifi_dump_tag(&mobdom, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + libwifi_free_tag(&mobdom); + + struct libwifi_tagged_parameter fbss = {0}; + tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88); + memset(tag_tmp, 0, tsz); + w = libwifi_dump_tag(&fbss, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + libwifi_free_tag(&fbss); + + libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_action_length(&action); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting actions with:\n"); + printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_action(&action, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_actions() { + while (1) { + printf("Sending 50 action frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_action action; + memset(&action, 0, sizeof(struct libwifi_action)); + + libwifi_create_action(&action, to, from, ACTION_FAST_BSS_TRANSITION); + + unsigned char *action_buf = malloc(256); + memset(action_buf, 0, 256); + + size_t offset = 0; + size_t w = 0; + + memcpy(action_buf, "\x01", 1); // Fast BSS Request + offset += 1; + memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address + offset += 6; + memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address + offset += 6; + + unsigned char *tag_tmp = malloc(256); + memset(tag_tmp, 0, 256); + + struct libwifi_tagged_parameter rsne = {0}; + size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20); + w = libwifi_dump_tag(&rsne, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + + + struct libwifi_tagged_parameter mobdom = {0}; + tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3); + memset(tag_tmp, 0, tsz); + w = libwifi_dump_tag(&mobdom, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + libwifi_free_tag(&mobdom); + + struct libwifi_tagged_parameter fbss = {0}; + tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88); + memset(tag_tmp, 0, tsz); + w = libwifi_dump_tag(&fbss, tag_tmp, tsz); + memcpy(action_buf + offset, tag_tmp, w); + offset += w; + libwifi_free_tag(&fbss); + + libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset); + + unsigned char *buf = NULL; + size_t buf_sz = libwifi_get_action_length(&action); + + buf = malloc(buf_sz); + if (buf == NULL) { + printf("malloc failure: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("Injecting actions with:\n"); + printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION); + printf("\tSource: " MACSTR "\n", MAC2STR(from)); + printf("\tDestination: " MACSTR "\n", MAC2STR(to)); + + libwifi_dump_action(&action, buf, buf_sz); + inject_frame(buf, buf_sz); + + free(buf); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_atim() { + while (1) { + printf("Sending 50 ATIM frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_atim atim = {0}; + + libwifi_create_atim(&atim, to, from, from); + + inject_frame(&atim, sizeof(struct libwifi_atim)); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_rts() { + while (1) { + printf("Sending 50 RTS frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_rts rts = {0}; + + libwifi_create_rts(&rts, to, from, 32); + + inject_frame(&rts, sizeof(struct libwifi_rts)); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void inject_cts() { + while (1) { + printf("Sending 50 CTS frames, then sleeping for 1 second\n"); + for (int i = 0; i < 50; ++i) { + struct libwifi_cts cts = {0}; + + libwifi_create_cts(&cts, to, 32); + + inject_frame(&cts, sizeof(struct libwifi_cts)); + + usleep(1e4); // 10ms + } + sleep(1); + } +} + +void help(const char *name) { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "\t%s --interface [interface] [--mode]\n", name); + fprintf(stderr, "\t\tor\n"); + fprintf(stderr, "\t%s --file [output file] [--mode]\n", name); + fprintf(stderr, "\n"); + fprintf(stderr, "Modes:\n"); + fprintf(stderr, "\t--beacon\n"); + fprintf(stderr, "\t--random-beacon\n"); + fprintf(stderr, "\t--probe-req\n"); + fprintf(stderr, "\t--probe-resp\n"); + fprintf(stderr, "\t--deauth\n"); + fprintf(stderr, "\t--disassoc\n"); + fprintf(stderr, "\t--assoc-req\n"); + fprintf(stderr, "\t--assoc-resp\n"); + fprintf(stderr, "\t--reassoc-req\n"); + fprintf(stderr, "\t--reassoc-resp\n"); + fprintf(stderr, "\t--auth\n"); + fprintf(stderr, "\t--timing-ad\n"); + fprintf(stderr, "\t--atim\n"); + fprintf(stderr, "\t--rts\n"); + fprintf(stderr, "\t--cts\n"); +} + +void handle_args(int argc, const char *argv[]) { + char errbuf[PCAP_ERRBUF_SIZE]; + memset(errbuf, 0, PCAP_ERRBUF_SIZE); + + if (argc < 4) { + help(argv[0]); + exit(EXIT_SUCCESS); + } + + if (strcmp(argv[1], "--file") == 0) { + inject_mode = OFFLINE_DUMP; + + filename = fopen(argv[2], "w+"); + if ((handle = pcap_open_dead(DLT_IEEE802_11_RADIO, BUFSIZ)) == NULL) { + fprintf(stderr, "1 %s: %s\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + if ((outputHandle = pcap_dump_fopen(handle, filename)) == NULL) { + fprintf(stderr, "2 %s: %s\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + } else if (strcmp(argv[1], "--interface") == 0) { + inject_mode = LIVE_INJECT; + + if ((handle = pcap_create(argv[2], errbuf)) == NULL) { + fprintf(stderr, "Couldn't open interface %s: %s\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + if (pcap_activate(handle) == 0) { + printf("Sniffing on %s\n", argv[2]); + } else { + fprintf(stderr, "Couldn't activate %s: %s\n", argv[2], pcap_geterr(handle)); + exit(EXIT_FAILURE); + } + } else { + help(argv[0]); + exit(EXIT_SUCCESS); + } + + if (strcmp(argv[3], "--beacon") == 0) { + mode = MODE_BEACON; + } else if (strcmp(argv[3], "--random-beacon") == 0) { + mode = MODE_RANDOM_BEACON; + } else if (strcmp(argv[3], "--probe-resp") == 0) { + mode = MODE_PROBE_RESPONSE; + } else if (strcmp(argv[3], "--probe-req") == 0) { + mode = MODE_PROBE_REQUEST; + } else if (strcmp(argv[3], "--deauth") == 0) { + mode = MODE_DEAUTH; + } else if (strcmp(argv[3], "--disassoc") == 0) { + mode = MODE_DISASSOC; + } else if (strcmp(argv[3], "--assoc-resp") == 0) { + mode = MODE_ASSOC_RESPONSE; + } else if (strcmp(argv[3], "--assoc-req") == 0) { + mode = MODE_ASSOC_REQUEST; + } else if (strcmp(argv[3], "--reassoc-resp") == 0) { + mode = MODE_REASSOC_RESPONSE; + } else if (strcmp(argv[3], "--reassoc-req") == 0) { + mode = MODE_REASSOC_REQUEST; + } else if (strcmp(argv[3], "--auth") == 0) { + mode = MODE_AUTH; + } else if (strcmp(argv[3], "--timing-ad") == 0) { + mode = MODE_TIMING_AD; + } else if (strcmp(argv[3], "--action") == 0) { + mode = MODE_ACTION; + } else if (strcmp(argv[3], "--action-noack") == 0) { + mode = MODE_ACTION_NOACK; + } else if (strcmp(argv[3], "--atim") == 0) { + mode = MODE_ATIM; + } else if (strcmp(argv[3], "--rts") == 0) { + mode = MODE_RTS; + } else if (strcmp(argv[3], "--cts") == 0) { + mode = MODE_CTS; + } else { + help(argv[0]); + exit(EXIT_SUCCESS); + } +} + +int main(int argc, const char *argv[]) { + signal(SIGINT, handle_interupt); + handle_args(argc, argv); + + printf("Starting in 5 seconds...\n"); + + sleep(5); + + switch (mode) { + case MODE_BEACON: + inject_beacons(0); + break; + case MODE_RANDOM_BEACON: + inject_beacons(1); + break; + case MODE_PROBE_RESPONSE: + inject_probe_responses(); + break; + case MODE_PROBE_REQUEST: + inject_probe_requests(); + break; + case MODE_DEAUTH: + inject_deauths(); + break; + case MODE_DISASSOC: + inject_disassocs(); + break; + case MODE_ASSOC_REQUEST: + inject_assoc_requests(); + break; + case MODE_ASSOC_RESPONSE: + inject_assoc_responses(); + break; + case MODE_REASSOC_REQUEST: + inject_reassoc_requests(); + break; + case MODE_REASSOC_RESPONSE: + inject_reassoc_responses(); + break; + case MODE_AUTH: + inject_auths(); + break; + case MODE_ACTION: + inject_actions(); + break; + case MODE_ACTION_NOACK: + inject_action_noacks(); + break; + case MODE_TIMING_AD: + inject_timing_ads(); + break; + case MODE_ATIM: + inject_atim(); + break; + case MODE_RTS: + inject_rts(); + break; + case MODE_CTS: + inject_cts(); + break; + } + + return 0; +} diff --git a/utils/src/test_misc.c b/utils/src/test_misc.c new file mode 100644 index 0000000..f103455 --- /dev/null +++ b/utils/src/test_misc.c @@ -0,0 +1,30 @@ +#include +#include +#include + +void gen_macs() { + printf("Getting 10 random MAC addresses:\n"); + for(int i = 0; i < 10; i++) { + unsigned char mac[6] = {0}; + libwifi_random_mac(mac, NULL); + printf(MACSTR "\n", MAC2STR(mac)); + } + + printf("Generating 10 random MAC addresses with 00:20:91 OUI:\n"); + for(int i = 0; i < 10; i++) { + unsigned char mac[6] = {0}; + libwifi_random_mac(mac, (unsigned char *) "\x00\x20\x91"); + printf(MACSTR "\n", MAC2STR(mac)); + } + printf("\n"); +} + +int main() { + libwifi_dummy(); + + printf("libwifi version: %s\n\n", libwifi_get_version()); + + gen_macs(); + + return 0; +} diff --git a/utils/src/test_parsing.c b/utils/src/test_parsing.c new file mode 100644 index 0000000..c345346 --- /dev/null +++ b/utils/src/test_parsing.c @@ -0,0 +1,613 @@ +#include "helpers.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PCAP_SAVEFILE "/tmp/debug.pcap" +#define FILTER "" +#define MODE_BEACON 1 +#define MODE_PROBE_RESPONSE 2 +#define MODE_PROBE_REQUEST 3 +#define MODE_EAPOL 4 +#define MODE_DEAUTH 5 +#define MODE_DISASSOC 6 +#define MODE_ASSOC_RESPONSE 7 +#define MODE_ASSOC_REQUEST 8 +#define MODE_REASSOC_REQUEST 9 +#define MODE_REASSOC_RESPONSE 10 +#define MODE_DATA 11 +#define MODE_ALL 99 + +static pcap_t *handle; +pcap_dumper_t *pd; +static struct bpf_program *filter; +static int got_radiotap; +static unsigned long packet_num = 0; +static int mode = 0; +static int parse_radiotap_header = 0; + +struct libwifi_bss bss = {0}; +struct libwifi_sta sta = {0}; + +void help(const char *); +void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet); +void print_bss_info(struct libwifi_bss *bss); +void print_sta_info(struct libwifi_sta *sta); +void print_tag_info(unsigned char *data, size_t data_len); + +void interrupted(int signum) { + pcap_dump_close(pd); + pcap_close(handle); +} + +void print_bss_info(struct libwifi_bss *bss) { + if (bss == NULL) { + return; + } + + printf("=== BSS Parsing ===\n"); + printf("ESSID: %s\n", bss->hidden ? "(hidden)" : bss->ssid); + printf("BSSID: " MACSTR "\n", MAC2STR(bss->bssid)); + printf("Receiver: " MACSTR "\n", MAC2STR(bss->receiver)); + printf("Transmitter: " MACSTR "\n", MAC2STR(bss->transmitter)); + printf("Channel: %d\n", bss->channel); + printf("WPS: %s\n", bss->wps ? "yes" : "no"); + + char sec_buf[LIBWIFI_SECURITY_BUF_LEN]; + libwifi_get_security_type(bss, sec_buf); + printf("Encryption: %s\n", sec_buf); + + libwifi_get_group_ciphers(bss, sec_buf); + printf("\tGroup Ciphers: %s\n", sec_buf); + + libwifi_get_pairwise_ciphers(bss, sec_buf); + printf("\tPairwise Ciphers: %s\n", sec_buf); + + libwifi_get_auth_key_suites(bss, sec_buf); + printf("\tAuth Key Suites: %s\n", sec_buf); + + if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) { + printf("\tMFP Capable: Yes\n"); + } + if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) { + printf("\tMFP Required: Yes\n"); + } + + if (bss->tags.length) { + printf("Tagged Parameters:\n"); + print_tag_info(bss->tags.parameters, bss->tags.length); + } else { + printf("Tagged Parameters: None\n"); + } + + printf("=== BSS End ===\n"); + printf("\n\n"); +} + +void print_sta_info(struct libwifi_sta *sta) { + if (sta == NULL) { + return; + } + + printf("=== STA Parsing ===\n"); + + if (sta->broadcast_ssid) { + printf("ESSID: \n"); + } else { + printf("ESSID: %s\n", sta->ssid); + } + printf("Channel: %u\n", sta->channel); + printf("BSSID: " MACSTR "\n", MAC2STR(sta->bssid)); + printf("MAC: " MACSTR "\n", MAC2STR(sta->transmitter)); + + printf("=== STA End ===\n"); + printf("\n\n"); +} + +void print_tag_info(unsigned char *data, size_t data_len) { + struct libwifi_tag_iterator it; + if (libwifi_tag_iterator_init(&it, data, data_len) != 0) { + printf("Couldn't initialise tag iterator\n"); + return; + } + do { + printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len); + + int max_size = 16; + if (it.tag_header->tag_len < 16) { + max_size = it.tag_header->tag_len; + } + printf("\t%d bytes of Tag Data: ", max_size); + for (size_t i = 0; i < max_size; i++) { + printf("%02x ", it.tag_data[i]); + } + printf("\n"); + } while (libwifi_tag_iterator_next(&it) != -1); +} + +void parse_radiotap(const unsigned char *packet) { + struct libwifi_radiotap_info rtap_info; + libwifi_parse_radiotap_info(&rtap_info, packet); + + printf("=== Radiotap Parsing ===\n"); + printf("Radiotap Channel: %d\n", rtap_info.channel.freq); + printf("Radiotap Channel Flags: 0x%04x\n", rtap_info.channel.flags); + printf("Radiotap Rate: %.2f Mb/s\n", rtap_info.rate); + printf("Radiotap Rate Raw: 0x%02x\n", rtap_info.rate_raw); + printf("Radiotap Signal: %d dBm\n", rtap_info.signal); + for (int i = 0; i < rtap_info.antenna_count; i++) { + printf("Radiotap Antenna %d: %d dBm\n", rtap_info.antennas[i].antenna_number, rtap_info.antennas[i].signal); + } + printf("Radiotap Flags: 0x%04x\n", rtap_info.flags); + printf("Radiotap Extended Flags: 0x%08x\n", rtap_info.extended_flags); + printf("Radiotap RX Flags: 0x%04x\n", rtap_info.rx_flags); + printf("Radiotap TX Flags: 0x%04x\n", rtap_info.tx_flags); + printf("Radiotap TX Power: %d\n", rtap_info.tx_power); + printf("Radiotap RTS Retries: %d\n", rtap_info.rts_retries); + printf("Radiotap Data Retries: %d\n", rtap_info.data_retries); + printf("=== Radiotap End ===\n"); +} + +void parse_beacon(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_beacon(&bss, &frame); + if (ret != 0) { + printf("Failed to parse beacon: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_bss_info(&bss); + } +} + +void parse_probe_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_REQ) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_probe_req(&sta, &frame); + if (ret != 0) { + printf("Failed to parse probe request: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_sta_info(&sta); + } +} +void parse_probe_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_RESP) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_probe_resp(&bss, &frame); + if (ret != 0) { + printf("Failed to parse probe response: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_bss_info(&bss); + } +} +void parse_deauth(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DEAUTH) { + printf("Packet : %lu\n", packet_num); + struct libwifi_parsed_deauth deauth; + int ret = libwifi_parse_deauth(&deauth, &frame); + if (ret != 0) { + printf("Failed to parse deauthentication: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + printf("=== Deauthentication Frame ===\n"); + if (deauth.ordered) { + printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr1)); + printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr2)); + printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr3)); + } else { + printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr1)); + printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr2)); + printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr3)); + } + + printf("Reason: %d (0x%04x)\n", deauth.fixed_parameters.reason_code, deauth.fixed_parameters.reason_code); + + if (deauth.tags.length) { + printf("Tagged Parameters:\n"); + print_tag_info(deauth.tags.parameters, deauth.tags.length); + } else { + printf("Tagged Parameters: None\n"); + } + + printf("=== End Deauthentication Frame ===\n"); + printf("\n\n"); + } +} +void parse_disassoc(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DISASSOC) { + printf("Packet : %lu\n", packet_num); + struct libwifi_parsed_disassoc disassoc; + int ret = libwifi_parse_disassoc(&disassoc, &frame); + if (ret != 0) { + printf("Failed to parse diassociation: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + printf("=== Disassociation Frame ===\n"); + if (disassoc.ordered) { + printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr1)); + printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr2)); + printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr3)); + } else { + printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr1)); + printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr2)); + printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr3)); + } + + printf("Reason: %d (0x%04x)\n", disassoc.fixed_parameters.reason_code, disassoc.fixed_parameters.reason_code); + + printf("Tagged Parameters:\n"); + if (disassoc.tags.length == 0) { + printf("\tNo Tags\n"); + } else { + printf("\tTags Found\n"); + } + + printf("=== End Disassociation Frame ===\n"); + printf("\n\n"); + } +} +void parse_assoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_REQ) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_assoc_req(&sta, &frame); + if (ret != 0) { + printf("Failed to parse association request: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_sta_info(&sta); + } +} +void parse_assoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_RESP) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_assoc_resp(&bss, &frame); + if (ret != 0) { + printf("Failed to parse association response: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_bss_info(&bss); + } +} +void parse_reassoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_REQ) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_reassoc_req(&sta, &frame); + if (ret != 0) { + printf("Failed to parse reassociation request: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_sta_info(&sta); + } +} +void parse_reassoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_RESP) { + printf("Packet : %lu\n", packet_num); + int ret = libwifi_parse_reassoc_resp(&bss, &frame); + if (ret != 0) { + printf("Failed to parse reassociation response: %d\n", ret); + pcap_dump(args, header, packet); + return; + } + + if (got_radiotap && parse_radiotap_header) { + parse_radiotap(packet); + } + + print_bss_info(&bss); + } +} +void parse_data_eapol(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_DATA) { + if (libwifi_check_wpa_handshake(&frame) > 0) { + printf("=== EAPOL ===\n"); + printf("WPA Handshake\n"); + int part = libwifi_check_wpa_message(&frame); + printf("WPA Handshake Message: %s\n", libwifi_get_wpa_message_string(&frame)); + + struct libwifi_wpa_auth_data data = {0}; + libwifi_get_wpa_data(&frame, &data); + + printf("EAPOL: Version: %d\n", data.version); + printf("EAPOL: Type: %d\n", data.type); + printf("EAPOL: Length: %d\n", data.length); + printf("EAPOL: Descriptor: %d\n", data.descriptor); + printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information); + printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length); + printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter); + printf("EAPOL: Key Info: Nonce: "); + for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) printf("%02x ", data.key_info.nonce[i]); + printf("\n"); + printf("EAPOL: Key Info: IV: "); + for (size_t i = 0; i < sizeof(data.key_info.iv); ++i) printf("%02x ", data.key_info.iv[i]); + printf("\n"); + printf("EAPOL: Key Info: RSC: "); + for (size_t i = 0; i < sizeof(data.key_info.rsc); ++i) printf("%02x ", data.key_info.rsc[i]); + printf("\n"); + printf("EAPOL: Key Info: ID: "); + for (size_t i = 0; i < sizeof(data.key_info.id); ++i) printf("%02x ", data.key_info.id[i]); + printf("\n"); + printf("EAPOL: Key Info: MIC: "); + for (size_t i = 0; i < sizeof(data.key_info.mic); ++i) printf("%02x ", data.key_info.mic[i]); + printf("\n"); + printf("EAPOL: Key Info: Key Data Length: %d\n", data.key_info.key_data_length); + if (data.key_info.key_data_length) { + printf("EAPOL: Key Info: Key Data: "); + for (size_t i = 0; i < data.key_info.key_data_length; ++i) printf("%02x ", data.key_info.key_data[i]); + printf("\n"); + } + + libwifi_free_wpa_data(&data); + + printf("\n\n"); + } + } +} + +void parse_data(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + if (frame.frame_control.type == TYPE_DATA) { + if (frame.flags & LIBWIFI_FLAGS_IS_QOS) { + printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr1)); + printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr2)); + } else { + printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data.addr1)); + printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data.addr2)); + } + printf("Body Length: %zu\n", frame.len - frame.header_len); + printf("Body:\n"); + hexdump(frame.body, frame.len - frame.header_len); + } +} + +void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { + ++packet_num; + unsigned long data_len = header->caplen; + unsigned char *data = (unsigned char *) packet; + + struct libwifi_frame frame = {0}; + int ret = libwifi_get_wifi_frame(&frame, data, data_len, 1); + if (ret != 0) { + printf("[!] Error getting libwifi_frame: %d\n", ret); + return; + } + + memset(&bss, 0, sizeof(struct libwifi_bss)); + memset(&sta, 0, sizeof(struct libwifi_sta)); + + switch (mode) { + case MODE_BEACON: + parse_beacon(frame, args, header, packet); + break; + case MODE_PROBE_REQUEST: + parse_probe_request(frame, args, header, packet); + break; + case MODE_PROBE_RESPONSE: + parse_probe_response(frame, args, header, packet); + break; + case MODE_DEAUTH: + parse_deauth(frame, args, header, packet); + break; + case MODE_DISASSOC: + parse_disassoc(frame, args, header, packet); + break; + case MODE_ASSOC_REQUEST: + parse_assoc_request(frame, args, header, packet); + break; + case MODE_ASSOC_RESPONSE: + parse_assoc_response(frame, args, header, packet); + break; + case MODE_REASSOC_REQUEST: + parse_reassoc_request(frame, args, header, packet); + break; + case MODE_REASSOC_RESPONSE: + parse_reassoc_response(frame, args, header, packet); + break; + case MODE_EAPOL: + parse_data_eapol(frame, args, header, packet); + break; + case MODE_DATA: + parse_data(frame, args, header, packet); + break; + case MODE_ALL: + parse_beacon(frame, args, header, packet); + parse_probe_request(frame, args, header, packet); + parse_probe_response(frame, args, header, packet); + parse_deauth(frame, args, header, packet); + parse_disassoc(frame, args, header, packet); + parse_assoc_request(frame, args, header, packet); + parse_assoc_response(frame, args, header, packet); + parse_reassoc_request(frame, args, header, packet); + parse_reassoc_response(frame, args, header, packet); + parse_data_eapol(frame, args, header, packet); + parse_data(frame, args, header, packet); + default: + break; + } + + libwifi_free_bss(&bss); + libwifi_free_wifi_frame(&frame); +} + +void help(const char *name) { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "\t%s --interface [interface] [--mode] [--radiotap]\n", name); + fprintf(stderr, "\t\tor\n"); + fprintf(stderr, "\t%s --file [capture file] [--mode] [--radiotap]\n", name); + fprintf(stderr, "\n"); + fprintf(stderr, "Modes:\n"); + fprintf(stderr, "\t--beacon\n"); + fprintf(stderr, "\t--probe-req\n"); + fprintf(stderr, "\t--probe-resp\n"); + fprintf(stderr, "\t--deauth\n"); + fprintf(stderr, "\t--disassoc\n"); + fprintf(stderr, "\t--assoc-req\n"); + fprintf(stderr, "\t--assoc-resp\n"); + fprintf(stderr, "\t--reassoc-req\n"); + fprintf(stderr, "\t--reassoc-resp\n"); + fprintf(stderr, "\t--eapol\n"); +} + +void handle_args(int argc, const char *argv[]) { + char errbuf[PCAP_ERRBUF_SIZE]; + + if (argc < 4) { + help(argv[0]); + exit(EXIT_SUCCESS); + } + + if (strcmp(argv[1], "--file") == 0) { + if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) { + fprintf(stderr, "Couldn't read file %s: %s\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + } else if (strcmp(argv[1], "--interface") == 0) { + if ((handle = pcap_create(argv[2], errbuf)) == NULL) { + fprintf(stderr, "Failed to open interface \"%s\" for sniffing: %s\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + if (pcap_activate(handle) == 0) { + printf("[+] Started sniffing on %s\n", argv[2]); + } else { + fprintf(stderr, "[!] Couldn't activate capture: %s.\n", pcap_geterr(handle)); + pcap_close(handle); + exit(EXIT_FAILURE); + } + } else { + help(argv[0]); + exit(EXIT_SUCCESS); + } + + if (strcmp(argv[3], "--beacon") == 0) { + mode = MODE_BEACON; + } else if (strcmp(argv[3], "--probe-req") == 0) { + mode = MODE_PROBE_REQUEST; + } else if (strcmp(argv[3], "--probe-resp") == 0) { + mode = MODE_PROBE_RESPONSE; + } else if (strcmp(argv[3], "--deauth") == 0) { + mode = MODE_DEAUTH; + } else if (strcmp(argv[3], "--disassoc") == 0) { + mode = MODE_DISASSOC; + } else if (strcmp(argv[3], "--assoc-req") == 0) { + mode = MODE_ASSOC_REQUEST; + } else if (strcmp(argv[3], "--assoc-resp") == 0) { + mode = MODE_ASSOC_RESPONSE; + } else if (strcmp(argv[3], "--reassoc-req") == 0) { + mode = MODE_REASSOC_REQUEST; + } else if (strcmp(argv[3], "--reassoc-resp") == 0) { + mode = MODE_REASSOC_RESPONSE; + } else if (strcmp(argv[3], "--eapol") == 0) { + mode = MODE_EAPOL; + } else if (strcmp(argv[3], "--data") == 0) { + mode = MODE_DATA; + } else if (strcmp(argv[3], "--all") == 0) { + mode = MODE_ALL; + } else { + help(argv[0]); + exit(EXIT_SUCCESS); + } + + if (argc > 4) { + if (strcmp(argv[4], "--radiotap") == 0) { + parse_radiotap_header = 1; + } + } +} + +int main(int argc, const char *argv[]) { + packet_num = 0; + char errbuf[PCAP_ERRBUF_SIZE]; + + handle_args(argc, argv); + + int linktype = pcap_datalink(handle); + if (linktype == DLT_IEEE802_11_RADIO) { + got_radiotap = 1; + } else if (linktype == DLT_IEEE802_11) { + got_radiotap = 0; + } else { + fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle)); + pcap_close(handle); + exit(EXIT_FAILURE); + } + + if ((filter = malloc(sizeof(struct bpf_program))) == NULL) { + perror("Malloc failure"); + pcap_close(handle); + exit(EXIT_FAILURE); + } + printf("[*] Compiling and optimizing frame filter, this can take a second\n"); + if (pcap_compile(handle, filter, FILTER, 0, 0) != 0) { + fprintf(stderr, "[!] Couldn't compile filter: %s\n", pcap_geterr(handle)); + pcap_close(handle); + free(filter); + exit(EXIT_FAILURE); + } + if (pcap_setfilter(handle, filter) != 0) { + fprintf(stderr, "[!] Couldn't set filter: %s\n", pcap_geterr(handle)); + pcap_close(handle); + free(filter); + exit(EXIT_FAILURE); + } + printf("[+] Complete\n"); + + pd = pcap_dump_open(handle, PCAP_SAVEFILE); + pcap_loop(handle, -1 /*INFINITY*/, &parse_packet, (unsigned char *) pd); + + return 0; +} -- cgit 1.4.1 From 8fc20c9765e8a2459b276ec2d3c0271c60e35d2f Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 17 Dec 2021 19:35:13 +0000 Subject: util: Move pcaps out of test directory, into utils --- test/pcaps/assoc_req.pcap | Bin 4374 -> 0 bytes test/pcaps/assoc_resp.pcap | Bin 3924 -> 0 bytes test/pcaps/auth.pcap | Bin 6524 -> 0 bytes test/pcaps/beacons_with_221.pcap | Bin 7674 -> 0 bytes test/pcaps/deauth.pcap | Bin 3074 -> 0 bytes test/pcaps/deauth_with_221.pcap | Bin 5024 -> 0 bytes test/pcaps/disassoc.pcap | Bin 3074 -> 0 bytes test/pcaps/probe_req.pcap | Bin 4074 -> 0 bytes test/pcaps/probe_resp_with_221.pcap | Bin 7874 -> 0 bytes test/pcaps/reassoc_req.pcap | Bin 4674 -> 0 bytes test/pcaps/reassoc_resp.pcap | Bin 3924 -> 0 bytes utils/pcaps/assoc_req.pcap | Bin 0 -> 4374 bytes utils/pcaps/assoc_resp.pcap | Bin 0 -> 3924 bytes utils/pcaps/auth.pcap | Bin 0 -> 6524 bytes utils/pcaps/beacons_with_221.pcap | Bin 0 -> 7674 bytes utils/pcaps/deauth.pcap | Bin 0 -> 3074 bytes utils/pcaps/deauth_with_221.pcap | Bin 0 -> 5024 bytes utils/pcaps/disassoc.pcap | Bin 0 -> 3074 bytes utils/pcaps/probe_req.pcap | Bin 0 -> 4074 bytes utils/pcaps/probe_resp_with_221.pcap | Bin 0 -> 7874 bytes utils/pcaps/reassoc_req.pcap | Bin 0 -> 4674 bytes utils/pcaps/reassoc_resp.pcap | Bin 0 -> 3924 bytes 22 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/pcaps/assoc_req.pcap delete mode 100644 test/pcaps/assoc_resp.pcap delete mode 100644 test/pcaps/auth.pcap delete mode 100644 test/pcaps/beacons_with_221.pcap delete mode 100644 test/pcaps/deauth.pcap delete mode 100644 test/pcaps/deauth_with_221.pcap delete mode 100644 test/pcaps/disassoc.pcap delete mode 100644 test/pcaps/probe_req.pcap delete mode 100644 test/pcaps/probe_resp_with_221.pcap delete mode 100644 test/pcaps/reassoc_req.pcap delete mode 100644 test/pcaps/reassoc_resp.pcap create mode 100644 utils/pcaps/assoc_req.pcap create mode 100644 utils/pcaps/assoc_resp.pcap create mode 100644 utils/pcaps/auth.pcap create mode 100644 utils/pcaps/beacons_with_221.pcap create mode 100644 utils/pcaps/deauth.pcap create mode 100644 utils/pcaps/deauth_with_221.pcap create mode 100644 utils/pcaps/disassoc.pcap create mode 100644 utils/pcaps/probe_req.pcap create mode 100644 utils/pcaps/probe_resp_with_221.pcap create mode 100644 utils/pcaps/reassoc_req.pcap create mode 100644 utils/pcaps/reassoc_resp.pcap diff --git a/test/pcaps/assoc_req.pcap b/test/pcaps/assoc_req.pcap deleted file mode 100644 index 87ad19b..0000000 Binary files a/test/pcaps/assoc_req.pcap and /dev/null differ diff --git a/test/pcaps/assoc_resp.pcap b/test/pcaps/assoc_resp.pcap deleted file mode 100644 index 16f6fad..0000000 Binary files a/test/pcaps/assoc_resp.pcap and /dev/null differ diff --git a/test/pcaps/auth.pcap b/test/pcaps/auth.pcap deleted file mode 100644 index e667432..0000000 Binary files a/test/pcaps/auth.pcap and /dev/null differ diff --git a/test/pcaps/beacons_with_221.pcap b/test/pcaps/beacons_with_221.pcap deleted file mode 100644 index 5cc20ff..0000000 Binary files a/test/pcaps/beacons_with_221.pcap and /dev/null differ diff --git a/test/pcaps/deauth.pcap b/test/pcaps/deauth.pcap deleted file mode 100644 index c8fb77c..0000000 Binary files a/test/pcaps/deauth.pcap and /dev/null differ diff --git a/test/pcaps/deauth_with_221.pcap b/test/pcaps/deauth_with_221.pcap deleted file mode 100644 index ba53dd1..0000000 Binary files a/test/pcaps/deauth_with_221.pcap and /dev/null differ diff --git a/test/pcaps/disassoc.pcap b/test/pcaps/disassoc.pcap deleted file mode 100644 index f217150..0000000 Binary files a/test/pcaps/disassoc.pcap and /dev/null differ diff --git a/test/pcaps/probe_req.pcap b/test/pcaps/probe_req.pcap deleted file mode 100644 index b039e4b..0000000 Binary files a/test/pcaps/probe_req.pcap and /dev/null differ diff --git a/test/pcaps/probe_resp_with_221.pcap b/test/pcaps/probe_resp_with_221.pcap deleted file mode 100644 index 3d664b0..0000000 Binary files a/test/pcaps/probe_resp_with_221.pcap and /dev/null differ diff --git a/test/pcaps/reassoc_req.pcap b/test/pcaps/reassoc_req.pcap deleted file mode 100644 index f5bd99e..0000000 Binary files a/test/pcaps/reassoc_req.pcap and /dev/null differ diff --git a/test/pcaps/reassoc_resp.pcap b/test/pcaps/reassoc_resp.pcap deleted file mode 100644 index c3e657f..0000000 Binary files a/test/pcaps/reassoc_resp.pcap and /dev/null differ diff --git a/utils/pcaps/assoc_req.pcap b/utils/pcaps/assoc_req.pcap new file mode 100644 index 0000000..87ad19b Binary files /dev/null and b/utils/pcaps/assoc_req.pcap differ diff --git a/utils/pcaps/assoc_resp.pcap b/utils/pcaps/assoc_resp.pcap new file mode 100644 index 0000000..16f6fad Binary files /dev/null and b/utils/pcaps/assoc_resp.pcap differ diff --git a/utils/pcaps/auth.pcap b/utils/pcaps/auth.pcap new file mode 100644 index 0000000..e667432 Binary files /dev/null and b/utils/pcaps/auth.pcap differ diff --git a/utils/pcaps/beacons_with_221.pcap b/utils/pcaps/beacons_with_221.pcap new file mode 100644 index 0000000..5cc20ff Binary files /dev/null and b/utils/pcaps/beacons_with_221.pcap differ diff --git a/utils/pcaps/deauth.pcap b/utils/pcaps/deauth.pcap new file mode 100644 index 0000000..c8fb77c Binary files /dev/null and b/utils/pcaps/deauth.pcap differ diff --git a/utils/pcaps/deauth_with_221.pcap b/utils/pcaps/deauth_with_221.pcap new file mode 100644 index 0000000..ba53dd1 Binary files /dev/null and b/utils/pcaps/deauth_with_221.pcap differ diff --git a/utils/pcaps/disassoc.pcap b/utils/pcaps/disassoc.pcap new file mode 100644 index 0000000..f217150 Binary files /dev/null and b/utils/pcaps/disassoc.pcap differ diff --git a/utils/pcaps/probe_req.pcap b/utils/pcaps/probe_req.pcap new file mode 100644 index 0000000..b039e4b Binary files /dev/null and b/utils/pcaps/probe_req.pcap differ diff --git a/utils/pcaps/probe_resp_with_221.pcap b/utils/pcaps/probe_resp_with_221.pcap new file mode 100644 index 0000000..3d664b0 Binary files /dev/null and b/utils/pcaps/probe_resp_with_221.pcap differ diff --git a/utils/pcaps/reassoc_req.pcap b/utils/pcaps/reassoc_req.pcap new file mode 100644 index 0000000..f5bd99e Binary files /dev/null and b/utils/pcaps/reassoc_req.pcap differ diff --git a/utils/pcaps/reassoc_resp.pcap b/utils/pcaps/reassoc_resp.pcap new file mode 100644 index 0000000..c3e657f Binary files /dev/null and b/utils/pcaps/reassoc_resp.pcap differ -- cgit 1.4.1 From 11c7393bebe4df6e2061f69787f4a7dd5c31f077 Mon Sep 17 00:00:00 2001 From: Marc Date: Sun, 9 Jan 2022 15:20:45 +0000 Subject: test: Add beacon parser test --- test/CMakeLists.txt | 1 + test/src/beacon_tests.c | 51 ++++++++++++++++++++++++++++++++++++++++++++- utils/src/test_generation.c | 5 ----- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 32934a5..64f8f36 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,6 +53,7 @@ add_test(NAME test_auth_gen_tags COMMAND auth_tests --auth-gen-tags) add_test(NAME test_beacon_gen_full COMMAND beacon_tests --beacon-gen-full) add_test(NAME test_beacon_gen_tags COMMAND beacon_tests --beacon-gen-tags) +add_test(NAME test_beacon_parse COMMAND beacon_tests --beacon-parse) add_test(NAME test_deauth_gen_full COMMAND deauth_tests --deauth-gen-full) add_test(NAME test_deauth_gen_tags COMMAND deauth_tests --deauth-gen-tags) diff --git a/test/src/beacon_tests.c b/test/src/beacon_tests.c index 1c4e17e..d152d5e 100644 --- a/test/src/beacon_tests.c +++ b/test/src/beacon_tests.c @@ -9,6 +9,18 @@ const unsigned char to[] = TO_MAC; const unsigned char bcast[] = BCAST_MAC; +const unsigned char beacon[] = "\x00\x00\x18\x00\x8e\x58\x00\x00\x10\x02\x6c\x09\xa0\x00\x54\x00" \ + "\x00\x2b\x00\x00\x9f\x61\xc9\x5c\x80\x00\x00\x00\xff\xff\xff\xff" \ + "\xff\xff\x00\x0c\x41\x82\xb2\x55\x00\x0c\x41\x82\xb2\x55\x50\xf8" \ + "\x89\xf1\xd4\x1b\x01\x00\x00\x00\x64\x00\x11\x04\x00\x07\x43\x6f" \ + "\x68\x65\x72\x65\x72\x01\x08\x82\x84\x8b\x96\x24\x30\x48\x6c\x03" \ + "\x01\x01\x05\x04\x00\x01\x00\x00\x2a\x01\x02\x2f\x01\x02\x30\x18" \ + "\x01\x00\x00\x0f\xac\x02\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x02" \ + "\x01\x00\x00\x0f\xac\x02\x00\x00\x32\x04\x0c\x12\x18\x60\xdd\x06" \ + "\x00\x10\x18\x02\x00\x04\xdd\x1c\x00\x50\xf2\x01\x01\x00\x00\x50" \ + "\xf2\x02\x02\x00\x00\x50\xf2\x04\x00\x50\xf2\x02\x01\x00\x00\x50" \ + "\xf2\x02\x00\x00\x9f\x61\xc9\x5c"; + int test_beacon_gen_full() { struct libwifi_beacon beacon = {0}; @@ -48,7 +60,10 @@ int test_beacon_add_tag() { return ret; } - ret = libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, (const unsigned char *) "\x00\x11\x22\xAAHello World", 15); + ret = libwifi_quick_add_tag(&beacon.tags, + TAG_VENDOR_SPECIFIC, + (const unsigned char *) "\x00\x11\x22\xAAHello World", + 15); if (ret != 0) { fprintf(stderr, "Failed to add beacon tagged parameter: %s\n", strerror(ret)); return ret; @@ -75,6 +90,38 @@ int test_beacon_add_tag() { return 0; } +int test_beacon_parse() { + struct libwifi_frame frame = {0}; + struct libwifi_bss bss = {0}; + + int ret = libwifi_get_wifi_frame(&frame, beacon, sizeof(beacon), 1); + if (ret != 0) { + return ret; + } + + if (frame.frame_control.type != TYPE_MANAGEMENT || frame.frame_control.subtype != SUBTYPE_BEACON) { + return -1; + } + + ret = libwifi_parse_beacon(&bss, &frame); + if (ret != 0) { + return ret; + } + + if (strcmp(bss.ssid, "Coherer") != 0) { + return -2; + } + + if (bss.channel != 1) { + return -3; + } + + libwifi_free_bss(&bss); + libwifi_free_wifi_frame(&frame); + + return 0; +} + int main(int argc, char **argv) { if (argc < 2) { printf("Specify test\n"); @@ -85,6 +132,8 @@ int main(int argc, char **argv) { return test_beacon_gen_full(); } else if (strcmp(argv[1], "--beacon-gen-tags") == 0) { return test_beacon_add_tag(); + } else if (strcmp(argv[1], "--beacon-parse") == 0) { + return test_beacon_parse(); } return -1; diff --git a/utils/src/test_generation.c b/utils/src/test_generation.c index 407e87f..314ce34 100644 --- a/utils/src/test_generation.c +++ b/utils/src/test_generation.c @@ -1,10 +1,5 @@ #include #include -#include -#include -#include -#include -#include #include #include #include -- cgit 1.4.1