my_udp #16

Merged
serafim merged 1 commits from my_udp into dev 2025-04-26 19:23:35 +03:00
1 changed files with 76 additions and 4 deletions

View File

@ -21,8 +21,12 @@
#include <getopt.h> // Для struct option и getopt_long() #include <getopt.h> // Для struct option и getopt_long()
#include <curl/curl.h> // Основной заголовок libcurl #include <curl/curl.h> // Основной заголовок libcurl
#include <iostream> // стандартный input output #include <iostream> // стандартный input output
#include <iomanip> #include <iomanip> // Форматированный ввод/вывод
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>// Работа с JSON
#include <stdio.h> // Стандартный ввод/вывод (C)
#include <stdlib.h> // Базовые функции (C)
#include <string.h> // Работа со строками (C)
#include <netinet/in.h> // Сетевые функции, интернет-адреса
/* /*
@ -209,6 +213,15 @@ void my_diag()
case -600: // Неполные данные для Telegram-уведомлений case -600: // Неполные данные для Telegram-уведомлений
printf("Error: To use telegram integration both telegram_id and telegram_token have to be provided!\n.--help for info\n"); printf("Error: To use telegram integration both telegram_id and telegram_token have to be provided!\n.--help for info\n");
break; break;
case -501:
printf("Error: Invalid target IP address\n");
break;
case -502:
printf("Error: Failed to create UDP socket\n");
break;
case -503:
printf("Error: UDP packet send failed\n");
break;
} }
printf("end my_diag\n"); // debug printf("end my_diag\n"); // debug
@ -566,8 +579,67 @@ int my_tcp_syn() {
return 0; return 0;
} }
int my_udp() { void my_udp() {
return 0; // Выполняет UDP портовое сканирование well-known портов
int status;
int sockfd; // Дескриптор сокета
struct sockaddr_in target_addr; // Адрес цели
static int port_idx; // Текущий индекс порта
static const int ports[]; // Список портов
static const int ports_total;
int curr_port; // Текущий порт
const char dummy_data[]; // Данные для отправки
ssize_t send_result; // Результат отправки
// Инициализация структуры адреса
memset(&target_addr, 0, sizeof(target_addr));
curr_port = ports[port_idx];
dummy_data[] = "SCAN";
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(curr_port);
ports_total = sizeof(ports)/sizeof(ports[0])
sockfd = -1;
port_idx = 0;
ports = { // Список портов
53, 67, 68, 69, 123, 161, 162, 389, 443, 500, 514, 520, 1900, 4500
};
// Преобразование IP
if (inet_pton(AF_INET, ip, &target_addr.sin_addr) <= 0) {
n_fail_requests++;
status = -501; // Код ошибки: неверный IP
if (sockfd != -1) close(sockfd);
return status;
}
// Создание сокета
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd < 0) {
n_fail_requests++;
status = -502; // Ошибка создания сокета
if (sockfd != -1) close(sockfd);
return status;
}
// Отправка данных
send_result = sendto(sockfd, dummy_data, sizeof(dummy_data), 0,
(struct sockaddr*)&target_addr, sizeof(target_addr));
if (send_result < 0) {
n_fail_requests++;
status = -503; // Ошибка отправки
} else {
n_ok_requests++;
status = 0; // Успешная отправка
}
// Проверка общего числа запросов
if (n_ok_requests + n_fail_requests >= 1000) {
status = 2; // Условие завершения
}
// Переход к следующему порту
port_idx = (port_idx + 1) % ports_total;
return status;
} }
// Callback для записи ответа от сервера // Callback для записи ответа от сервера