LibCurl是一個開源的免費的多協議數據傳輸開源庫,該框架具備跨平臺性,開源免費,並提供了包括HTTP、FTP、SMTP、POP3等協議的功能,使用libcurl可以方便地進行網絡數據傳輸操作,如發送HTTP請求、下載文件、發送電子郵件等。它被廣泛應用於各種網絡應用開發中,特別是涉及到數據傳輸的場景。本章將是《C++ LibCurl 庫的使用方法》
的擴展篇,在前一篇文章中我們簡單實現了LibCurl對特定頁面的訪問功能,本文將繼續擴展該功能,並以此實現Web隱藏目錄掃描功能。
讀入文件到內存
首先通過讀取字典文件,將每行內容與指定的根網址進行拼接,生成新的URL列表,此處GetCombinationURL
函數的目標是根據傳入的根網址和字典文件,生成一個包含拼接後的URL
列表的std::vector<std::string>
。
函數的實現主要包括以下步驟:
- 打開指定的字典文件,逐行讀取其中的內容。
- 對於每一行內容,去除行末的換行符,並使用
sprintf
將根網址與當前行內容拼接,形成完整的URL。 - 將生成的URL加入std::vector`中。
- 返回包含所有URL的std::vector。
在main
函數中,調用GetCombinationURL
並將生成的URL列表輸出到控制檯。代碼使用了C++中的文件操作和字符串處理,利用std::vector
存儲生成的 URL,以及通過std::cout
在控制檯輸出結果。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 傳入網址和字典名
std::vector<std::string> GetCombinationURL(char root[64],char dict_file[128])
{
char buffer[512] = { 0 };
char this_url[1024] = { 0 };
std::vector<std::string> ref;
FILE *fp = fopen(dict_file, "r");
if (fp != NULL)
{
while (feof(fp) == 0)
{
fgets(buffer, 1024, fp); // 每次讀入一行
strtok(buffer, "\n"); // 去掉行末的 \n
buffer[strcspn(buffer, "\n")] = 0; // 替換所有 \n
sprintf(this_url, "%s%s", root, buffer);
ref.push_back(this_url);
}
}
return ref;
}
int main(int argc, char *argv[])
{
std::vector<std::string> ref = GetCombinationURL("https://www.xxx.com", "./save.log");
for (int x = 0; x < ref.size(); x++)
{
std::cout << "拼接URL: " << ref[x] << std::endl;
}
std::system("pause");
return 0;
}
我們需要新建一個save.log
文件,每行放入一個子目錄地址,例如放入;
/index.php
/phpinfo.php
運行後輸出效果如下圖所示;
增加默認多線程
首先,我們引入了libcurl
庫,代碼中使用libcurl
提供的函數來執行HTTP
請求,獲取返回狀態碼,並通過多線程處理多個URL。
- GetPageStatus 函數:用於獲取指定URL的HTTP狀態碼。使用
libcurl
進行初始化、設置請求頭、執行請求,並最終獲取返回的狀態碼。 - ThreadProc 函數:線程執行函數,通過調用
GetPageStatus
函數獲取URL
的狀態碼,並在控制檯輸出。如果狀態碼爲200,則將URL記錄到日誌文件中。 - main 函數:主函數讀取輸入的URL列表文件,逐行讀取並構造完整的URL。通過
CreateThread
創建線程,每個線程處理一個URL。同時使用互斥鎖確保線程安全。
用戶可以通過在命令行傳遞兩個參數,第一個參數爲根網址,第二個參數爲包含URL
列表的文件路徑。程序將讀取文件中的每個URL,通過libcurl發送HTTP 請求,獲取狀態碼,並輸出到控制檯。狀態碼爲200的URL將被記錄到save.log文件中。
#define CURL_STATICLIB
#define BUILDING_LIBCURL
#include <iostream>
#include <string>
#include "curl/curl.h"
#pragma comment (lib,"libcurl_a.lib")
#pragma comment (lib,"wldap32.lib")
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"Crypt32.lib")
using namespace std;
// 設置鎖
HANDLE hmutex;
// 屏蔽無用的輸出
static size_t write_data(char *d, size_t n, size_t l, void *p){ return 0; }
int GetPageStatus(char *HostUrl)
{
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_WIN32);
if (CURLE_OK != return_code)
return 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (LyShark NT 10.0; Win64; x64; rv:76.0)");
CURL *easy_handle = curl_easy_init();
int retcode = 0;
if (NULL != easy_handle)
{
curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers); // 改協議頭
curl_easy_setopt(easy_handle, CURLOPT_URL, HostUrl); // 請求的網站
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); // 設置回調函數,屏蔽輸出
return_code = curl_easy_perform(easy_handle); // 執行CURL
return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
}
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return retcode;
}
// 線程執行函數
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
char *urls = (char *)(LPVOID)lpParam;
WaitForSingleObject(hmutex, INFINITE);
int ret = GetPageStatus(urls);
if (ret == 200)
{
FILE *fp = fopen("./save.log", "a+");
fwrite(urls, strlen(urls), 1, fp);
fwrite("\n", 2, 1, fp);
fclose(fp);
}
std::cout << "狀態碼: " << ret << " 地址: " << urls << std::endl;
ReleaseMutex(hmutex);
return 0;
}
int main(int argc, char *argv[])
{
if (argc == 3)
{
FILE *fp = fopen(argv[2], "r");
char buffer[1024] = { 0 };
char url[1024] = { 0 };
hmutex = CreateMutex(NULL, TRUE, NULL);
ReleaseMutex(hmutex);
while (feof(fp) == 0)
{
fgets(buffer, 1024, fp);
strtok(buffer, "\n");
buffer[strcspn(buffer, "\n")] = 0;
sprintf(url, "%s%s", argv[1], buffer);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)url, 0, 0);
Sleep(80);
}
}
return 0;
}
使用Boost多線程
如上Web目錄掃描器,雖實現了目錄的掃描,但是有個很大的缺陷,第一是無法跨平臺,第二是無法實現優雅的命令行解析效果,所以我們需要使用boost
讓其支持跨平臺並增加一個輸出界面。
#define CURL_STATICLIB
#define BUILDING_LIBCURL
#include <iostream>
#include <string>
#include "curl/curl.h"
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread_guard.hpp>
#include <boost/program_options.hpp>
#pragma comment (lib,"libcurl_a.lib")
#pragma comment (lib,"wldap32.lib")
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"Crypt32.lib")
using namespace std;
using namespace boost;
namespace opt = boost::program_options;
boost::mutex io_mutex;
void ShowOpt()
{
fprintf(stderr,
"# # # \n"
"# # # \n"
"# # # ##### ###### ###### # ### # ## \n"
"# # # # # # # # ## # # \n"
"# # # #### # # # # # ### \n"
"# ##### # # # # ## # # # \n"
"##### # ##### # # #### # # # ## \n\n"
);
}
// 傳入網址和字典名
std::vector<std::string> GetCombinationURL(char *root, char *dict_file)
{
char buffer[512] = { 0 };
char this_url[1024] = { 0 };
std::vector<std::string> ref;
FILE *fp = fopen(dict_file, "r");
if (fp != NULL)
{
while (feof(fp) == 0)
{
fgets(buffer, 1024, fp); // 每次讀入一行
strtok(buffer, "\n"); // 去掉行末的 \n
buffer[strcspn(buffer, "\n")] = 0; // 替換所有 \n
sprintf(this_url, "%s%s", root, buffer);
ref.push_back(this_url);
}
}
return ref;
}
// 屏蔽無用的輸出
static size_t write_data(char *d, size_t n, size_t l, void *p){ return 0; }
int GetPageStatus(std::string HostUrl)
{
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_WIN32);
if (CURLE_OK != return_code)
return 0;
CURL *easy_handle = curl_easy_init();
int retcode = 0;
if (NULL != easy_handle)
{
curl_easy_setopt(easy_handle, CURLOPT_URL, HostUrl); // 請求的網站
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); // 設置回調函數,屏蔽輸出
return_code = curl_easy_perform(easy_handle); // 執行CURL
return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
}
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return retcode;
}
// 線程執行函數
void ThreadProc(std::string url)
{
boost::lock_guard<boost::mutex> global_mutex(io_mutex);
int ret = GetPageStatus(url);
if (ret == 200)
{
FILE *fp = fopen("./save.log", "a+");
fwrite(url.c_str(), strlen(url.c_str()), 1, fp);
fwrite("\n", 2, 1, fp);
fclose(fp);
}
std::cout << "狀態碼: " << ret << " 地址: " << url << std::endl;
}
int main(int argc, char * argv[])
{
opt::options_description des_cmd("\n Usage: LyShark URL掃描工具 Ver:1.1 \n\n Options");
des_cmd.add_options()
("url,u", opt::value<std::string>(), "指定需要的URL地址")
("dict,d", opt::value<std::string>(), "指定字典")
("help,h", "幫助菜單");
opt::variables_map virtual_map;
try
{
opt::store(opt::parse_command_line(argc, argv, des_cmd), virtual_map);
}
catch (...){ return 0; }
// 定義消息
opt::notify(virtual_map);
// 無參數直接返回
if (virtual_map.empty())
{
ShowOpt();
std::cout << des_cmd << std::endl;
return 0;
}
else if (virtual_map.count("help") || virtual_map.count("h"))
{
ShowOpt();
std::cout << des_cmd << std::endl;
return 0;
}
else if (virtual_map.count("url") && virtual_map.count("dict"))
{
std::string scan_url = virtual_map["url"].as<std::string>();
std::string scan_path = virtual_map["dict"].as<std::string>();
// 由於string與char* 需要轉換,所以拷貝後轉換
char src[1024] = { 0 };
char path[1024] = { 0 };
strcpy(src, scan_url.c_str());
strcpy(path, scan_path.c_str());
std::vector<std::string> get_url = GetCombinationURL(src, path);
boost::thread_group group;
for (int x = 0; x < get_url.size(); x++)
{
group.create_thread(boost::bind(ThreadProc, get_url[x]));
_sleep(50);
}
group.join_all();
}
else
{
std::cout << "參數錯誤" << std::endl;
}
return 0;
}
傳入參數運行,當訪問出現200提示,則自動保存到save.log
中,運行效果如下。
- main.exe --url https://www.lyshark.com --dict c://dict.log