400 lines
12 KiB
C++
400 lines
12 KiB
C++
#include <iostream>
|
|
#include <stdlib.h>
|
|
#include <string>
|
|
#include <chrono>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <getopt.h>
|
|
#include <cctype>
|
|
#include <curl/curl.h>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
// ====== DCL ====== //
|
|
|
|
std::string attack_type;
|
|
std::string domain;
|
|
std::string ip;
|
|
std::string port;
|
|
std::string log_file;
|
|
std::string telegram_id;
|
|
std::string telegram_token;
|
|
int n_ok_requests;
|
|
int n_fail_requests;
|
|
std::chrono::system_clock::time_point start_timestamp;
|
|
std::string log_msg;
|
|
std::string fin_msg;
|
|
std::string msg;
|
|
extern const char* global_ip;
|
|
extern int status;
|
|
extern int n_ok_requests;
|
|
extern int n_fail_requests;
|
|
|
|
int my_check_params(int argc, char **argv)
|
|
{
|
|
std::string debug_msg;
|
|
debug_msg = "";
|
|
for (int i = 0; i < argc; i++) {
|
|
debug_msg += argv[i];
|
|
debug_msg += " ";
|
|
}
|
|
printf("begin my_check_params (argc: %i, argv: %s)\n", argc, debug_msg.c_str());
|
|
|
|
int status;
|
|
int opt;
|
|
const char* short_options = "a:d:i:p:l:t:b:h";
|
|
const struct option long_options[] = {
|
|
{"attack", required_argument, NULL, 'a'},
|
|
{"domain", required_argument, NULL, 'd'},
|
|
{"ip", required_argument, NULL, 'i'},
|
|
{"port", required_argument, NULL, 'p'},
|
|
{"log", required_argument, NULL, 'l'},
|
|
{"telegram", required_argument, NULL, 't'},
|
|
{"token", required_argument, NULL, 'b'},
|
|
{"help", no_argument, NULL, 'h'},
|
|
{NULL, 0, NULL, 0}
|
|
};
|
|
|
|
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
switch (opt) {
|
|
case 'a':
|
|
attack_type = optarg;
|
|
break;
|
|
case 'd':
|
|
domain = optarg;
|
|
break;
|
|
case 'i':
|
|
ip = optarg;
|
|
break;
|
|
case 'p':
|
|
port = optarg;
|
|
break;
|
|
case 'l':
|
|
log_file = optarg;
|
|
break;
|
|
case 't':
|
|
telegram_id = optarg;
|
|
break;
|
|
case 'b':
|
|
telegram_token = optarg;
|
|
break;
|
|
case 'h':
|
|
status = 0;
|
|
break;
|
|
case '?':
|
|
status = -101;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (status != 0 && status != -101)
|
|
{
|
|
if (attack_type != "flood" && attack_type != "scan") {
|
|
status = -1;
|
|
}
|
|
else if (attack_type == "scan" && domain.empty() && ip.empty()) {
|
|
status = -10;
|
|
}
|
|
else if (attack_type == "flood" && domain.empty() && ip.empty()) {
|
|
status = -20;
|
|
}
|
|
else if ((!telegram_id.empty() && telegram_token.empty()) || (telegram_id.empty() && !telegram_token.empty())) {
|
|
status = -600;
|
|
}
|
|
else if (attack_type == "scan") {
|
|
status = 1;
|
|
}
|
|
else if (attack_type == "flood") {
|
|
status = 2;
|
|
}
|
|
}
|
|
|
|
printf("end my_check_params status: %i\n", status);
|
|
return status;
|
|
}
|
|
|
|
void my_udp() {
|
|
// Выполняет UDP портовое сканирование well-known портов
|
|
int sockfd = -1; // Дескриптор сокета
|
|
struct sockaddr_in target_addr; // Адрес цели
|
|
static int port_idx = 0; // Текущий индекс порта
|
|
static const int ports[] = { // Список портов
|
|
53, 67, 68, 69, 123, 161, 162, 389, 443, 500, 514, 520, 1900, 4500
|
|
};
|
|
static const int ports_total = sizeof(ports)/sizeof(ports[0]);
|
|
int curr_port = ports[port_idx]; // Текущий порт
|
|
const char dummy_data[] = "SCAN"; // Данные для отправки
|
|
ssize_t send_result; // Результат отправки
|
|
|
|
// Инициализация структуры адреса
|
|
memset(&target_addr, 0, sizeof(target_addr));
|
|
target_addr.sin_family = AF_INET;
|
|
target_addr.sin_port = htons(curr_port);
|
|
|
|
// Преобразование IP
|
|
if (inet_pton(AF_INET, global_ip, &target_addr.sin_addr) <= 0) {
|
|
n_fail_requests++;
|
|
status = -501; // Код ошибки: неверный IP
|
|
goto cleanup;
|
|
}
|
|
|
|
// Создание сокета
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
if (sockfd < 0) {
|
|
n_fail_requests++;
|
|
status = -502; // Ошибка создания сокета
|
|
goto cleanup;
|
|
}
|
|
|
|
// Отправка данных
|
|
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;
|
|
|
|
cleanup:
|
|
if (sockfd != -1) close(sockfd);
|
|
}
|
|
|
|
void my_diag(int status)
|
|
{
|
|
printf("begin my_diag (status: %i)\n", status);
|
|
switch (status)
|
|
{
|
|
case 0:
|
|
printf("Usage: ./DosAtk [options]\n"
|
|
"Required:\n"
|
|
" -a, --attack TYPE Type of attack (scan|flood)\n"
|
|
" -d, --domain DOMAIN Target domain\n"
|
|
" -i, --ip IP Target IP\n"
|
|
" -p, --port PORT Port. Required only for flood type!\n"
|
|
"Optional:\n"
|
|
" -l, --log FILE Log file\n"
|
|
" -t, --telegram ID Telegram ID\n"
|
|
" -b, --token TOKEN Telegram bot token\n");
|
|
break;
|
|
case -1:
|
|
printf("Error: Invalid attack type!\n--help for more info\n");
|
|
break;
|
|
case -10:
|
|
printf("Error: Missing required parameters for port scanning!\n--help for more info\n");
|
|
break;
|
|
case -20:
|
|
printf("Error: Missing required parameters for tcp syn dos attack!\n--help for more info\n");
|
|
break;
|
|
case -101:
|
|
printf("Error: Unknown option!\n.--help for info\n");
|
|
break;
|
|
case -600:
|
|
printf("Error: To use telegram integration both telegram_id and telegram_token have to be provided!\n.--help for info\n");
|
|
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");
|
|
}
|
|
|
|
void my_msg()
|
|
{
|
|
printf("begin my_msg()\n");
|
|
printf("%s\n", msg.c_str());
|
|
printf("end my_msg");
|
|
}
|
|
|
|
int my_log()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
void my_fin()
|
|
{
|
|
auto end_timestamp = std::chrono::system_clock::now();
|
|
auto end_time_t = std::chrono::system_clock::to_time_t(end_timestamp);
|
|
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_timestamp.time_since_epoch()) % 1000;
|
|
|
|
auto duration = end_timestamp - start_timestamp;
|
|
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
|
|
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration % std::chrono::hours(1));
|
|
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration % std::chrono::minutes(1));
|
|
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration % std::chrono::seconds(1));
|
|
|
|
std::cout << "Worked for ";
|
|
if (duration < std::chrono::minutes(2)) {
|
|
double total_seconds = std::chrono::duration<double>(duration).count();
|
|
std::cout << std::fixed << std::setprecision(3) << total_seconds << " seconds";
|
|
} else {
|
|
if (hours.count() > 0) std::cout << hours.count() << "h ";
|
|
if (minutes.count() > 0) std::cout << minutes.count() << "m ";
|
|
std::cout << seconds.count() << "s " << milliseconds.count() << "ms";
|
|
}
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "Sent " << (n_ok_requests + n_fail_requests) << " requests ("
|
|
<< n_ok_requests << " ok, " << n_fail_requests << " failed)" << std::endl;
|
|
|
|
std::cout << "DosAtk stopped at " << std::put_time(std::localtime(&end_time_t), "%Y-%m-%d %H:%M:%S")
|
|
<< "." << std::setfill('0') << std::setw(3) << end_ms.count() << std::endl;
|
|
|
|
std::exit(0);
|
|
}
|
|
|
|
int my_dns()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int my_tcp_syn()
|
|
{
|
|
return 2;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int check_params_status;
|
|
int log_status;
|
|
int dns_status;
|
|
int udp_status;
|
|
int tcp_syn_status;
|
|
|
|
// ====== Тело программы ====== //
|
|
n_ok_requests = 0;
|
|
n_fail_requests = 0;
|
|
start_timestamp = std::chrono::system_clock::now();
|
|
|
|
time_t now_time_t = std::chrono::system_clock::to_time_t(start_timestamp);
|
|
|
|
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(start_timestamp.time_since_epoch()) % 1000;
|
|
|
|
printf("Starting DosAtk at %04d-%02d-%02d %02d:%02d:%02d.%03ld\n",
|
|
std::localtime(&now_time_t)->tm_year + 1900,
|
|
std::localtime(&now_time_t)->tm_mon + 1,
|
|
std::localtime(&now_time_t)->tm_mday,
|
|
std::localtime(&now_time_t)->tm_hour,
|
|
std::localtime(&now_time_t)->tm_min,
|
|
std::localtime(&now_time_t)->tm_sec,
|
|
ms.count());
|
|
|
|
check_params_status = my_check_params(argc, argv);
|
|
switch (check_params_status)
|
|
{
|
|
case 1:
|
|
dns_status = my_dns();
|
|
if (dns_status == 0)
|
|
{
|
|
while (true)
|
|
{
|
|
if (udp_status == 2)
|
|
{
|
|
break;
|
|
}
|
|
else if (udp_status < 0)
|
|
{
|
|
my_diag(udp_status);
|
|
log_status = my_log();
|
|
if (log_status == 1)
|
|
{
|
|
my_msg();
|
|
}
|
|
}
|
|
}
|
|
log_status = my_log();
|
|
my_msg();
|
|
my_fin();
|
|
}
|
|
else if (dns_status == 1)
|
|
{
|
|
my_diag(check_params_status);
|
|
log_status = my_log();
|
|
if (log_status == 0){
|
|
my_fin();
|
|
}
|
|
else if (log_status == 1)
|
|
{
|
|
my_msg();
|
|
my_fin();
|
|
}
|
|
}
|
|
break;
|
|
case 2:
|
|
dns_status = my_dns();
|
|
if (dns_status == 0)
|
|
{
|
|
while (tcp_syn_status = my_tcp_syn())
|
|
{
|
|
if (tcp_syn_status == 2)
|
|
{
|
|
break;
|
|
}
|
|
else if (tcp_syn_status < 0)
|
|
{
|
|
my_diag(tcp_syn_status);
|
|
log_status = my_log();
|
|
if (log_status == 1)
|
|
{
|
|
my_msg();
|
|
}
|
|
}
|
|
}
|
|
log_status = my_log();
|
|
my_msg();
|
|
my_fin();
|
|
}
|
|
else if (dns_status == 1)
|
|
{
|
|
my_diag(check_params_status);
|
|
log_status = my_log();
|
|
if (log_status == 0)
|
|
{
|
|
my_fin();
|
|
}
|
|
else if (log_status == 1)
|
|
{
|
|
my_msg();
|
|
my_fin();
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
my_diag(check_params_status);
|
|
log_status = my_log();
|
|
if (log_status == 0)
|
|
{
|
|
my_fin();
|
|
}
|
|
else if (log_status == 1)
|
|
{
|
|
my_msg();
|
|
my_fin();
|
|
}
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|