ref my_dns

This commit is contained in:
tieshagr 2025-04-03 15:49:03 +03:00
parent 811e1692bb
commit fca5e5ac08
1 changed files with 49 additions and 37 deletions

View File

@ -9,14 +9,14 @@
#include <net/if.h> // Определение констант сетевых интерфейсов (IFNAMSIZ) #include <net/if.h> // Определение констант сетевых интерфейсов (IFNAMSIZ)
#include <sys/ioctl.h> // Управление сокетами и интерфейсами (ioctl) #include <sys/ioctl.h> // Управление сокетами и интерфейсами (ioctl)
#include <fcntl.h> // Флаги файловых дескрипторов (fcntl) #include <fcntl.h> // Флаги файловых дескрипторов (fcntl)
#include <curl/curl.h> #include <curl/curl.h> // Библиотека libcurl для HTTP/HTTPS запросов
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>// Библиотека для работы с JSON в C++
// ====== DCL глобальные переменные ====== // // ====== DCL глобальные переменные ====== //
// Параметры // Параметры
int argc // Количество аргументов при вызове программы int argc; // Количество аргументов при вызове программы
char **argv // Массив строк с агрументами char **argv; // Массив строк с агрументами
std::string attack_type; // Тип атаки: scan или syn std::string attack_type; // Тип атаки: scan или syn
std::string domain; // Доменное Имя std::string domain; // Доменное Имя
std::string ip; // Ip жертвы std::string ip; // Ip жертвы
@ -407,7 +407,6 @@ void my_fin()
} }
using json = nlohmann::json;
// curl --http2 "https://dns.google/resolve?name=example.com&type=A" // curl --http2 "https://dns.google/resolve?name=example.com&type=A"
// curl --http2 --header "accept: application/dns-json" "https://1.1.1.1/dns-query?name=cloudflare.com" --next --http2 --header "accept: application/dns-json" "https://1.1.1.1/dns-query?name=yandex.com" // curl --http2 --header "accept: application/dns-json" "https://1.1.1.1/dns-query?name=cloudflare.com" --next --http2 --header "accept: application/dns-json" "https://1.1.1.1/dns-query?name=yandex.com"
void my_dns() void my_dns()
@ -418,12 +417,20 @@ void my_dns()
// -4002 - ошибка запроса CURL // -4002 - ошибка запроса CURL
// -4003 - ошибка парсинга JSON // -4003 - ошибка парсинга JSON
status = 0; CURL* curl; // объект curl
CURL* curl = 0; // объект curl CURLcode res; // результат выполнения запроса
CURLcode res = {}; // результат выполнения запроса
std::string response; // ответ DNS сервера std::string response; // ответ DNS сервера
std::string url = ""; // API DNS сервера std::string url; // API DNS сервера
struct curl_slist* headers = {0}; // Заголовки struct curl_slist* headers; // Заголовки
nlohmann::json json_data;
status = 0;
curl = 0;
res = {};
response = "";
url = "";
headers = {0};
json_data = {0};
printf("start my_dns"); // debug printf("start my_dns"); // debug
@ -432,7 +439,7 @@ void my_dns()
if (!curl) { if (!curl) {
status = -4001; status = -4001;
} }
else {
// Формируем URL для Cloudflare DoH // Формируем URL для Cloudflare DoH
url = "https://1.1.1.1/dns-query?name=" + domain + "&type=A"; url = "https://1.1.1.1/dns-query?name=" + domain + "&type=A";
@ -454,10 +461,10 @@ void my_dns()
} else { } else {
// Парсим JSON и извлекаем IP // Парсим JSON и извлекаем IP
try { try {
auto json_data = json::parse(response); json_data = nlohmann::json::parse(response);
if (json_data.contains("Answer")) { if (json_data.contains("Answer")) {
for (const auto& record : json_data["Answer"]) { for (const nlohmann::json& record : json_data["Answer"]) {
if (record["type"] == 1) { // A-запись (IPv4) if (record["type"] == 1) { // A-запись (IPv4) (Это же и проверка валидного IP)
ip = record["data"].get<std::string>(); ip = record["data"].get<std::string>();
break; break;
} }
@ -467,11 +474,16 @@ void my_dns()
status = -4003; status = -4003;
} }
} }
}
// Освобождаем ресурсы // Освобождаем ресурсы
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (!status) {
status = 1;
}
printf("end my_dns"); // debug printf("end my_dns"); // debug
} }