Skip to content

Commit

Permalink
Merge pull request #105 from Spooks4576/Development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Spooks4576 authored Nov 28, 2024
2 parents f01f365 + 7233a34 commit fa00f52
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 79 deletions.
2 changes: 1 addition & 1 deletion components/mdns/include/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *servi
* - ESP_ERR_NO_MEM memory error
* - ESP_ERR_INVALID_ARG parameter error
*/
esp_err_t mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t *addr);
mdns_result_t* mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t *addr);

#if CONFIG_LWIP_IPV6
/**
Expand Down
35 changes: 18 additions & 17 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ static bool _mdns_service_match(const mdns_service_t *srv, const char *service,
if (!service || !proto || !srv->hostname) {
return false;
}

if (!_str_null_or_empty(hostname) && !strcasecmp(hostname, "*")) // * wildcard for no filter
{
return true;
}

return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
(_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
}
Expand Down Expand Up @@ -409,6 +415,12 @@ static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *
if (!service || !proto) {
return false;
}

if (!_str_null_or_empty(hostname) && !strcasecmp(hostname, "*"))
{
return true;
}

// instance==NULL -> _mdns_instance_name_match() will check the default instance
// hostname==NULL -> matches if instance, service and proto matches
return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
Expand Down Expand Up @@ -4863,7 +4875,7 @@ static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *s, mdns_na
continue;
}
if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) {
if (!strcasecmp(name->host, s->instance)) {
if (!strcasecmp(name->host, s->instance) || !strcasecmp(s->instance, "*")) {
return s;
}
s = s->next;
Expand Down Expand Up @@ -6650,13 +6662,13 @@ esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *servi
}

#ifdef CONFIG_LWIP_IPV4
esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
mdns_result_t* mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
{
mdns_result_t *result = NULL;
esp_err_t err;

if (_str_null_or_empty(name)) {
return ESP_ERR_INVALID_ARG;
return NULL;
}

if (strstr(name, ".local")) {
Expand All @@ -6666,25 +6678,14 @@ esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr)
err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result);

if (err) {
return err;
return NULL;
}

if (!result) {
return ESP_ERR_NOT_FOUND;
}

mdns_ip_addr_t *a = result->addr;
while (a) {
if (a->addr.type == ESP_IPADDR_TYPE_V4) {
addr->addr = a->addr.u_addr.ip4.addr;
mdns_query_results_free(result);
return ESP_OK;
}
a = a->next;
return NULL;
}

mdns_query_results_free(result);
return ESP_ERR_NOT_FOUND;
return result;
}
#endif /* CONFIG_LWIP_IPV4 */

Expand Down
2 changes: 2 additions & 0 deletions include/managers/wifi_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void wifi_manager_auto_deauth();

void wifi_manager_stop_beacon();

void wifi_manager_start_ip_lookup();

void wifi_manager_connect_wifi(const char* ssid, const char* password);

void wifi_manager_stop_monitor_mode();
Expand Down
5 changes: 5 additions & 0 deletions main/core/commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ void handle_tp_link_test(int argc, char **argv)
}
}

void handle_ip_lookup(int argc, char** argv) {
wifi_manager_start_ip_lookup();
}

void handle_capture_scan(int argc, char** argv)
{
if (argc != 2) {
Expand Down Expand Up @@ -793,6 +797,7 @@ void register_commands() {
register_command("help", handle_help);
register_command("scanap", cmd_wifi_scan_start);
register_command("scansta", handle_sta_scan);
register_command("scanlocal", handle_ip_lookup);
register_command("stopscan", cmd_wifi_scan_stop);
register_command("attack", handle_attack_cmd);
register_command("list", handle_list);
Expand Down
2 changes: 1 addition & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <esp_log.h>

#ifdef CONFIG_WITH_ETHERNET

// TODO
#endif

#ifdef CONFIG_WITH_SCREEN
Expand Down
7 changes: 7 additions & 0 deletions main/managers/views/options_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const char* options_menu_type_to_string(EOptionsMenuType menuType) {

static const char *wifi_options[] = {
"Scan Access Points",
"Scan LAN Devices",
"Start Deauth Attack",
"Beacon Spam - Random",
"Beacon Spam - Rickroll",
Expand Down Expand Up @@ -280,6 +281,12 @@ void option_event_cb(const char* Selected_Option) {
simulateCommand("beaconspam -rr");
}

if (strcmp(Selected_Option, "Scan LAN Devices") == 0) {
display_manager_switch_view(&terminal_view);
vTaskDelay(pdMS_TO_TICKS(10));
simulateCommand("scanlocal");
}


if (strcmp(Selected_Option, "Beacon Spam - List") == 0) {
if (scanned_aps)
Expand Down
118 changes: 110 additions & 8 deletions main/managers/wifi_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@
#include "esp_timer.h"
#include <ctype.h>
#include <stdio.h>
#include <mdns.h>
#include <math.h>
#include <dhcpserver/dhcpserver.h>
#include "esp_http_client.h"
#include "lwip/lwip_napt.h"
#include "lwip/etharp.h"
#include <esp_http_server.h>
#include <core/dns_server.h>
#include "esp_crt_bundle.h"
#ifdef WITH_SCREEN
#include "managers/views/music_visualizer.h"
#endif


// Include Outside so we have access to the Terminal View Macro
#include "managers/views/terminal_screen.h"





#define MAX_DEVICES 255
#define CHUNK_SIZE 8192
#define MDNS_NAME_BUF_LEN 65
#define ARP_DELAY_MS 500

uint16_t ap_count;
wifi_ap_record_t* scanned_aps;
Expand All @@ -51,6 +50,32 @@ dns_server_handle_t dns_handle;
esp_netif_t* wifiAP;
esp_netif_t* wifiSTA;

struct service_info {
const char *query;
const char *type;
};


struct service_info services[] = {
{"_http", "Web Server Enabled Device"},
{"_ssh", "SSH Server"},
{"_ipp", "Printer (IPP)"},
{"_googlecast", "Google Cast"},
{"_raop", "AirPlay"},
{"_smb", "SMB File Sharing"},
{"_hap", "HomeKit Accessory"},
{"_spotify-connect", "Spotify Connect Device"},
{"_printer", "Printer (Generic)"},
{"_mqtt", "MQTT Broker"}
};

#define NUM_SERVICES (sizeof(services) / sizeof(services[0]))

struct DeviceInfo {
struct ip4_addr ip;
struct eth_addr mac;
};

typedef enum {
COMPANY_DLINK,
COMPANY_NETGEAR,
Expand Down Expand Up @@ -1231,18 +1256,21 @@ void wifi_manager_select_ap(int index)

if (ap_count == 0) {
printf("No access points found\n");
TERMINAL_VIEW_ADD_TEXT("No access points found\n");
return;
}


if (scanned_aps == NULL) {
printf("No AP info available (scanned_aps is NULL)\n");
TERMINAL_VIEW_ADD_TEXT("No AP info available (scanned_aps is NULL)\n");
return;
}


if (index < 0 || index >= ap_count) {
printf("Invalid index: %d. Index should be between 0 and %d\n", index, ap_count - 1);
TERMINAL_VIEW_ADD_TEXT("Invalid index: %d. Index should be between 0 and %d\n", index, ap_count - 1);
return;
}

Expand Down Expand Up @@ -1591,6 +1619,7 @@ void wifi_manager_stop_deauth()
void wifi_manager_print_scan_results_with_oui() {
if (scanned_aps == NULL) {
printf("AP information not available\n");
TERMINAL_VIEW_ADD_TEXT("AP information not available\n");
return;
}

Expand Down Expand Up @@ -1737,16 +1766,89 @@ void wifi_manager_stop_beacon()
ap_manager_start_services();
} else {
printf("No beacon transmission is running.\n");
TERMINAL_VIEW_ADD_TEXT("No beacon transmission is running.\n");
}
}

void wifi_manager_start_ip_lookup()
{
wifi_ap_record_t ap_info;
if (esp_wifi_sta_get_ap_info(&ap_info) != ESP_OK || ap_info.rssi == 0) {
printf("Not connected to an Access Point.\n");
TERMINAL_VIEW_ADD_TEXT("Not connected to an Access Point.\n");
return;
}

esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info) == ESP_OK) {
printf("Connected. Proceeding with IP lookup...\n");
TERMINAL_VIEW_ADD_TEXT("Connected. Proceeding with IP lookup...\n");

int device_count = 0;
struct DeviceInfo devices[MAX_DEVICES];

for (int s = 0; s < NUM_SERVICES; s++) {
int retries = 0;
mdns_result_t* mdnsresult = NULL;

if (mdnsresult == NULL)
{
while (retries < 5 && mdnsresult == NULL) {
mdns_query_ptr(services[s].query, "_tcp", 2000, 30, &mdnsresult);

if (mdnsresult == NULL) {
retries++;
TERMINAL_VIEW_ADD_TEXT("Retrying mDNS query for service: %s (Attempt %d)\n", services[s].query, retries);
printf("Retrying mDNS query for service: %s (Attempt %d)\n", services[s].query, retries);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}

if (mdnsresult != NULL) {
printf("mDNS query succeeded for service: %s\n", services[s].query);
TERMINAL_VIEW_ADD_TEXT("mDNS query succeeded for service: %s\n", services[s].query);

mdns_result_t* current_result = mdnsresult;
while (current_result != NULL && device_count < MAX_DEVICES) {
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &current_result->addr->addr.u_addr.ip4, ip_str, INET_ADDRSTRLEN);

printf("Device at: %s\n", ip_str);
printf(" Name: %s\n", current_result->hostname);
printf(" Type: %s\n", services[s].type);
printf(" Port: %u\n", current_result->port);
TERMINAL_VIEW_ADD_TEXT("Device at: %s\n", ip_str);
TERMINAL_VIEW_ADD_TEXT(" Name: %s\n", current_result->hostname);
TERMINAL_VIEW_ADD_TEXT(" Type: %s\n", services[s].type);
TERMINAL_VIEW_ADD_TEXT(" Port: %u\n", current_result->port);
device_count++;

current_result = current_result->next;
}

mdns_query_results_free(mdnsresult);
} else {
printf("Failed to find devices for service: %s after %d retries\n", services[s].query, retries);
TERMINAL_VIEW_ADD_TEXT("Failed to find devices for service: %s after %d retries\n", services[s].query, retries);
}
}
} else {
printf("Could not get network interface info.\n");
TERMINAL_VIEW_ADD_TEXT("Could not get network interface info.\n");
}

printf("IP Scan Done.\n");
TERMINAL_VIEW_ADD_TEXT("IP Scan Done...\n");
}

void wifi_manager_connect_wifi(const char* ssid, const char* password)
{
wifi_config_t wifi_config = { //WIFI_AUTH_WPA2_PSK
wifi_config_t wifi_config = {
.sta = {
.ssid = "",
.password = "",
.threshold.authmode = strlen(password) > 8 ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN, // Set to WPA2-PSK authentication
.threshold.authmode = strlen(password) > 8 ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN,
.pmf_cfg = {
.capable = true,
.required = false
Expand Down
Loading

0 comments on commit fa00f52

Please sign in to comment.