From ae6c98a48da409d040604aeffb84a38155fb5bac Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 30 Nov 2021 22:39:26 +0000 Subject: Initial Commit Signed-off-by: Marc --- examples/generate_beacon/README.md | 19 +++++++ examples/generate_beacon/generate_beacon.c | 80 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 examples/generate_beacon/README.md create mode 100644 examples/generate_beacon/generate_beacon.c (limited to 'examples/generate_beacon') diff --git a/examples/generate_beacon/README.md b/examples/generate_beacon/README.md new file mode 100644 index 0000000..4b7103d --- /dev/null +++ b/examples/generate_beacon/README.md @@ -0,0 +1,19 @@ +# Generating 802.11 Beacons +This example shows the reader how to generate an 802.11 Beacon, with an SSID and Channel element. It also adds a tagged parameter with the string "libwifi-tag" inside. + +# Building and Using +``` +>> cd examples/generate_beacon/ +>> make +clang -Wall -Werror -O3 -o generate_beacon -c -o generate_beacon.o generate_beacon.c +clang -Wall -Werror -O3 -o generate_beacon generate_beacon.c -lpcap -lwifi +>> ./generate_beacon --file beacon.pcap +[+] Setup Complete +[*] Creating Beacon Frame +[*] Writing Beacon Frame to pcap +>> tshark -r beacon.pcap + 1 0.000000 ca:38:6d:6d:3f:bd → Broadcast 802.11 78 Beacon frame, SN=1383, FN=0, Flags=........, BI=100, SSID=libwifi-beacon +>> +``` +# Output +![image](https://user-images.githubusercontent.com/4153572/143600844-ce7dee11-46b0-40a5-a12c-881d79bd584d.png) diff --git a/examples/generate_beacon/generate_beacon.c b/examples/generate_beacon/generate_beacon.c new file mode 100644 index 0000000..2dad709 --- /dev/null +++ b/examples/generate_beacon/generate_beacon.c @@ -0,0 +1,80 @@ +#include + +#include + +#include +#include +#include +#include +#include +#include + +pcap_dumper_t *filedumper = NULL; + +void create_write_beacon() { + printf("[*] Creating Beacon Frame\n"); + struct libwifi_beacon beacon = {0}; + unsigned char transmitter[6] = {0}; + + libwifi_random_mac(transmitter); + unsigned char receiver[6] = "\xFF\xFF\xFF\xFF\xFF\xFF"; + + libwifi_create_beacon(&beacon, receiver, transmitter, "libwifi-beacon", 6); + libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, + (unsigned char *) "libwifi-tag", strlen("libwifi-tag")); + + unsigned char *buf = NULL; + size_t buf_len = libwifi_get_beacon_length(&beacon); + buf = malloc(buf_len); + if (buf == NULL) { + fprintf(stderr, "[!] Couldn't allocate buffer for beacon dump.\n"); + exit(EXIT_FAILURE); + } + memset(buf, 0, buf_len); + libwifi_dump_beacon(&beacon, buf, buf_len); + + printf("[*] Writing Beacon Frame to pcap\n"); + struct pcap_pkthdr pkt_hdr = {0}; + struct timeval tv = {0}; + pkt_hdr.caplen = buf_len; + pkt_hdr.len = buf_len; + gettimeofday(&tv, NULL); + pkt_hdr.ts = tv; + pcap_dump((unsigned char *) filedumper, &pkt_hdr, buf); +} + +void helpexit() { + fprintf(stderr, "[!] Usage: ./generate_beacon --file \n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { + pcap_t *handle = NULL; + char errbuf[PCAP_ERRBUF_SIZE] = {0}; + FILE *pcapfile = NULL; + + if (argc < 2) { + helpexit(); + } + if (strcmp(argv[1], "--file") == 0) { + pcapfile = fopen(argv[2], "w+"); + if ((handle = pcap_open_dead(DLT_IEEE802_11, BUFSIZ)) == NULL) { + fprintf(stderr, "[!] Error opening dead capture (%s)\n", errbuf); + exit(EXIT_FAILURE); + } + if ((filedumper = pcap_dump_fopen(handle, pcapfile)) == NULL) { + fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf); + exit(EXIT_FAILURE); + } + } else { + helpexit(); + } + + printf("[+] Setup Complete\n"); + + create_write_beacon(); + + pcap_dump_close(filedumper); + pcap_close(handle); + return 0; +} -- cgit 1.4.1