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,45 +439,50 @@ void my_dns()
if (!curl) { if (!curl) {
status = -4001; status = -4001;
} }
else {
// Формируем URL для Cloudflare DoH
url = "https://1.1.1.1/dns-query?name=" + domain + "&type=A";
// Формируем URL для Cloudflare DoH // Настройки CURL
url = "https://1.1.1.1/dns-query?name=" + domain + "&type=A"; curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); // HTTP/2
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // Проверка SSL
// Настройки CURL // Устанавливаем заголовок
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); headers = curl_slist_append(headers, "accept: application/dns-json");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); // HTTP/2
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // Проверка SSL
// Устанавливаем заголовок // Выполняем запрос
headers = curl_slist_append(headers, "accept: application/dns-json"); res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); if (res != CURLE_OK) {
status = -4002;
// Выполняем запрос } else {
res = curl_easy_perform(curl); // Парсим JSON и извлекаем IP
if (res != CURLE_OK) { try {
status = -4002; json_data = nlohmann::json::parse(response);
} else { if (json_data.contains("Answer")) {
// Парсим JSON и извлекаем IP for (const nlohmann::json& record : json_data["Answer"]) {
try { if (record["type"] == 1) { // A-запись (IPv4) (Это же и проверка валидного IP)
auto json_data = json::parse(response); ip = record["data"].get<std::string>();
if (json_data.contains("Answer")) { break;
for (const auto& record : json_data["Answer"]) { }
if (record["type"] == 1) { // A-запись (IPv4)
ip = record["data"].get<std::string>();
break;
} }
} }
} catch (const std::exception& e) {
status = -4003;
} }
} catch (const std::exception& e) {
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
} }