From 649ece40c6483cec63f876cad8fad3c7b80a828c Mon Sep 17 00:00:00 2001 From: Pirata Date: Sun, 26 May 2024 00:07:26 -0300 Subject: [PATCH] PingSweep --- .vscode/settings.json | 6 +- platformio.ini | 4 +- src/main.cpp | 2 +- src/scan_hosts.cpp | 187 +++++++++++++----------------------------- src/scan_hosts.h | 181 ++-------------------------------------- 5 files changed, 72 insertions(+), 308 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9fbfbc0b..d30ca8f6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "idf.portWin": "COM3" + "idf.portWin": "COM4", + "files.associations": { + "iosfwd": "cpp", + "iostream": "cpp" + } } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index bbca7aaf..72aa25c7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ lib_deps = Time LibSSH-ESP32 PCA9554 + https://github.com/bmorcelli/ESPping/ [env:m5stack-cplus2] @@ -107,7 +108,7 @@ framework = arduino board_build.partitions = custom_4Mb.csv build_flags = ${common.build_flags} - + -DCORE_DEBUG_LEVEL=5 -DSTICK_C_PLUS=1 -DROTATION=3 -DHAS_BTN=1 @@ -167,6 +168,7 @@ framework = arduino board_build.partitions = custom_8Mb.csv build_flags = ${common.build_flags} + -DCORE_DEBUG_LEVEL=5 -DCARDPUTER=1 -DROTATION=1 diff --git a/src/main.cpp b/src/main.cpp index fb52471e..3a77466c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -197,7 +197,7 @@ void loop() { options.push_back({"Raw Sniffer", [=]() { sniffer_setup(); }}); options.push_back({"DPWO-ESP32", [=]() { dpwo_setup(); }}); options.push_back({"Evil Portal", [=]() { startEvilPortal(); }}); - options.push_back({"Scan Hosts", [=]() { local_net_scan_setup(); }}); + options.push_back({"Scan Hosts", [=]() { local_scan_setup(); }}); options.push_back({"Wireguard", [=]() { wg_setup(); }}); options.push_back({"Main Menu", [=]() { backToMenu(); }}); delay(200); diff --git a/src/scan_hosts.cpp b/src/scan_hosts.cpp index 64f95e49..6a839fec 100644 --- a/src/scan_hosts.cpp +++ b/src/scan_hosts.cpp @@ -1,142 +1,71 @@ -/* ICMP echo example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ - -#include -#include -#include "sdkconfig.h" -//#include "lwip/inet.h" -//#include "lwip/netdb.h" -//#include "lwip/sockets.h" -#include "esp_console.h" -#include "esp_event.h" -#include "nvs_flash.h" -#include "argtable3/argtable3.h" -//#include "protocol_examples_common.h" -#include "ping/ping_sock.h" -#include "display.h" #include "globals.h" +#include "scan_hosts.h" +#include "display.h" +#include "mykeyboard.h" +#include "wifi_common.h" -//#define IPADDR_NONE ((uint32_t)0xffffffffUL) -static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) -{ - uint8_t ttl; - uint16_t seqno; - uint32_t elapsed_time, recv_len; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); - tft.printf("%" PRIu32 " bytes from %s icmp_seq=%" PRIu16 " ttl=%" PRIu16 " time=%" PRIu32 " ms\n", - recv_len, ipaddr_ntoa((ip_addr_t*)&target_addr), seqno, ttl, elapsed_time); -} -void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args) -{ - uint16_t seqno; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - tft.printf("From %s icmp_seq=%d timeout\n",ipaddr_ntoa((ip_addr_t*)&target_addr), seqno); +void logPingResult(IPAddress host, bool responded) { + char buffer[64]; // Tamanho do buffer ajustável conforme necessário + if (responded) { + sprintf(buffer, "Host %s respondeu ao ping.", host.toString().c_str()); + } else { + sprintf(buffer, "Host %s não respondeu ao ping.", host.toString().c_str()); + } + log_d("%s",buffer); } -void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args) -{ - ip_addr_t target_addr; - uint32_t transmitted; - uint32_t received; - uint32_t total_time_ms; - uint32_t loss; +bool pingHost(IPAddress host) { + int count = Ping.ping(host, 1); // Ping com 1 tentativa + bool responded = (count > 0); + logPingResult(host, responded); + return responded; +} - esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); - esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); +void local_scan_setup() { + if(!wifiConnected) wifiConnectMenu(); + + IPAddress gatewayIP; + IPAddress subnetMask; + std::vector responderam; + drawMainBorder(); + tft.setTextSize(FP); + tft.setCursor(8,30); + + gatewayIP = WiFi.gatewayIP(); + subnetMask = WiFi.subnetMask(); + + IPAddress network = WiFi.localIP(); + network[3] = 0; // Define o endereço de rede para o primeiro host + + int numHosts = 254 - subnetMask[3]; // Calcula o número de hosts possíveis na rede + tft.println("Probing " + String(numHosts) + " hosts (" + String(numHosts/40 + 1).substring(0,4) + " lines)"); + tft.fillRect(0,38,WIDTH,LH*(numHosts/40 + 1), BGCOLOR); + tft.drawRect(0,38,WIDTH,LH*(numHosts/40 + 1), FGCOLOR); + tft.fillRect(6,38,WIDTH-12,LH*(numHosts/40 + 1), BGCOLOR); + + for (int i = 1; i <= numHosts; i++) { + IPAddress currentIP = network; + currentIP[3] = i; + + if (pingHost(currentIP)) { + tft.print("x"); + responderam.push_back(currentIP); + } else tft.print("."); + + if(checkEscPress()) i=256; //ends for loop - if (transmitted > 0) { - loss = (uint32_t)((1 - ((float)received) / transmitted) * 100); - } else { - loss = 0; } - if (IP_IS_V4(&target_addr)) { - //tft.printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr))); - } else { - //tft.printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr))); + options = {}; + log_d("Hosts que responderam ao ping:"); + for (IPAddress ip : responderam) { + String txt = "..." + String(ip[2]) + "." + String(ip[3]); + options.push_back({ txt.c_str(), [=](){ displayInfo(ip.toString().c_str()); }}); } - tft.printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n", - transmitted, received, loss, total_time_ms); - // delete the ping sessions, so that we clean up all resources and can create a new ping session - // we don't have to call delete function in the callback, instead we can call delete function from other tasks - esp_ping_delete_session(hdl); -} - -int do_ping_cmd() -{ - esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); - - ip_addr_t target_addr; - memset(&target_addr, 0, sizeof(target_addr)); - - - esp_ping_handle_t ping; - //esp_ping_new_session(&config, &cbs, &ping); - esp_ping_start(ping); - return 0; -} - - - -static esp_console_repl_t *s_repl = NULL; - -/* handle 'quit' command */ -int do_cmd_quit() -{ - tft.printf("ByeBye\r\n"); - s_repl->del(s_repl); - return 0; -} - -esp_err_t register_quit() -{ - esp_console_cmd_t command = { - .command = "quit", - .help = "Quit REPL environment", - // .func = &do_cmd_quit - }; - return esp_console_cmd_register(&command); -} - -void local_scan_setup() -{ - ip_addr_t target_addr; - esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); - esp_ping_callbacks_t cbs = { - .on_ping_success = cmd_ping_on_ping_success, - .on_ping_timeout = cmd_ping_on_ping_timeout, - .on_ping_end = cmd_ping_on_ping_end, - // .cb_args = NULL, - }; - - for (int i = 1; i <= 254; i++) { - char ip_addr_str[16]; - tft.printf(ip_addr_str, "192.168.0.%d", i); - ip_addr_t addr; - ipaddr_aton(ip_addr_str, &addr); - config.target_addr = addr; - - esp_ping_handle_t ping; - memset(&target_addr, 0, sizeof(target_addr)); - esp_ping_new_session(&config, &cbs, &ping); - esp_ping_start(ping); - } + loopOptions(options); + while(!checkEscPress()) yield(); + } diff --git a/src/scan_hosts.h b/src/scan_hosts.h index 91052dfb..63a6222d 100644 --- a/src/scan_hosts.h +++ b/src/scan_hosts.h @@ -1,180 +1,9 @@ -/* - * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include "esp_err.h" -//#include "lwip/ip_addr.h" +#include +#include #include - -/** -* @brief Type of "ping" session handle -* -*/ -typedef void *esp_ping_handle_t; - -/** -* @brief Type of "ping" callback functions -* -*/ -typedef struct { - /** - * @brief arguments for callback functions - * - */ - // void *cb_args; - - /** - * @brief Invoked by internal ping thread when received ICMP echo reply packet - * - */ - void (*on_ping_success)(esp_ping_handle_t hdl, void *args); - - /** - * @brief Invoked by internal ping thread when receive ICMP echo reply packet timeout - * - */ - void (*on_ping_timeout)(esp_ping_handle_t hdl, void *args); - - /** - * @brief Invoked by internal ping thread when a ping session is finished - * - */ - void (*on_ping_end)(esp_ping_handle_t hdl, void *args); -} esp_ping_callbacks_t; - -/** -* @brief Type of "ping" configuration -* -*/ -typedef struct { - uint32_t count; /*!< A "ping" session contains count procedures */ - uint32_t interval_ms; /*!< Milliseconds between each ping procedure */ - uint32_t timeout_ms; /*!< Timeout value (in milliseconds) of each ping procedure */ - uint32_t data_size; /*!< Size of the data next to ICMP packet header */ - int tos; /*!< Type of Service, a field specified in the IP header */ - int ttl; /*!< Time to Live,a field specified in the IP header */ - ip_addr_t target_addr; /*!< Target IP address, either IPv4 or IPv6 */ - uint32_t task_stack_size; /*!< Stack size of internal ping task */ - uint32_t task_prio; /*!< Priority of internal ping task */ - uint32_t interface; /*!< Netif index, interface=0 means NETIF_NO_INDEX*/ -} esp_ping_config_t; - -/** - * @brief Default ping configuration - * - */ -#define ESP_PING_DEFAULT_CONFIG() \ - { \ - .count = 5, \ - .interval_ms = 1000, \ - .timeout_ms = 1000, \ - .data_size = 64, \ - .tos = 0, \ - .ttl = IP_DEFAULT_TTL, \ - .target_addr = *(IP_ANY_TYPE), \ - .task_stack_size = ESP_TASK_PING_STACK, \ - .task_prio = 2, \ - .interface = 0,\ - } - -#define ESP_PING_COUNT_INFINITE (0) /*!< Set ping count to zero will ping target infinitely */ - -/** -* @brief Profile of ping session -* -*/ -typedef enum { - ESP_PING_PROF_SEQNO, /*!< Sequence number of a ping procedure */ - ESP_PING_PROF_TOS, /*!< Type of service of a ping procedure */ - ESP_PING_PROF_TTL, /*!< Time to live of a ping procedure */ - ESP_PING_PROF_REQUEST, /*!< Number of request packets sent out */ - ESP_PING_PROF_REPLY, /*!< Number of reply packets received */ - ESP_PING_PROF_IPADDR, /*!< IP address of replied target */ - ESP_PING_PROF_SIZE, /*!< Size of received packet */ - ESP_PING_PROF_TIMEGAP, /*!< Elapsed time between request and reply packet */ - ESP_PING_PROF_DURATION /*!< Elapsed time of the whole ping session */ -} esp_ping_profile_t; - -/** - * @brief Create a ping session - * - * @param config ping configuration - * @param cbs a bunch of callback functions invoked by internal ping task - * @param hdl_out handle of ping session - * @return - * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. configuration is null, etc) - * - ESP_ERR_NO_MEM: out of memory - * - ESP_FAIL: other internal error (e.g. socket error) - * - ESP_OK: create ping session successfully, user can take the ping handle to do follow-on jobs - */ -esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_callbacks_t *cbs, esp_ping_handle_t *hdl_out); - -/** - * @brief Delete a ping session - * - * @param hdl handle of ping session - * @return - * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc) - * - ESP_OK: delete ping session successfully - */ -esp_err_t esp_ping_delete_session(esp_ping_handle_t hdl); - -/** - * @brief Start the ping session - * - * @param hdl handle of ping session - * @return - * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc) - * - ESP_OK: start ping session successfully - */ -esp_err_t esp_ping_start(esp_ping_handle_t hdl); - -/** - * @brief Stop the ping session - * - * @param hdl handle of ping session - * @return - * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc) - * - ESP_OK: stop ping session successfully - */ -esp_err_t esp_ping_stop(esp_ping_handle_t hdl); - -/** - * @brief Get runtime profile of ping session - * - * @param hdl handle of ping session - * @param profile type of profile - * @param data profile data - * @param size profile data size - * @return - * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc) - * - ESP_ERR_INVALID_SIZE: the actual profile data size doesn't match the "size" parameter - * - ESP_OK: get profile successfully - */ -esp_err_t esp_ping_get_profile(esp_ping_handle_t hdl, esp_ping_profile_t profile, void *data, uint32_t size); - -#ifdef __cplusplus -} -#endif - - -static void cmd_ping_on_ping_success(esp_ping_handle_t hdl); - -void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl); - -void cmd_ping_on_ping_end(esp_ping_handle_t hdl); - -int do_cmd_quit(); - -esp_err_t register_quit(); +#include +#include +#include void local_scan_setup(); \ No newline at end of file