最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++使用LibCurl實現(xiàn)Web隱藏目錄掃描功能

 更新時間:2023年11月21日 11:54:48   作者:微軟技術(shù)分享  
LibCurl是一個開源的免費的多協(xié)議數(shù)據(jù)傳輸開源庫,該框架具備跨平臺性,開源免費,并提供了包括HTTP、FTP、SMTP、POP3等協(xié)議的功能,本文將給大家介紹C++使用LibCurl實現(xiàn)Web隱藏目錄掃描功能

讀入文件到內(nèi)存

首先通過讀取字典文件,將每行內(nèi)容與指定的根網(wǎng)址進(jìn)行拼接,生成新的URL列表,此處GetCombinationURL 函數(shù)的目標(biāo)是根據(jù)傳入的根網(wǎng)址和字典文件,生成一個包含拼接后的URL列表的std::vector<std::string>

函數(shù)的實現(xiàn)主要包括以下步驟:

  • 打開指定的字典文件,逐行讀取其中的內(nèi)容。
  • 對于每一行內(nèi)容,去除行末的換行符,并使用sprintf將根網(wǎng)址與當(dāng)前行內(nèi)容拼接,形成完整的URL。
  • 將生成的URL加入std::vector`中。
  • 返回包含所有URL的std::vector。

main函數(shù)中,調(diào)用GetCombinationURL并將生成的URL列表輸出到控制臺。代碼使用了C++中的文件操作和字符串處理,利用std::vector存儲生成的 URL,以及通過std::cout在控制臺輸出結(jié)果。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// 傳入網(wǎng)址和字典名
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

運行后輸出效果如下圖所示;

增加默認(rèn)多線程

首先,我們引入了libcurl庫,代碼中使用libcurl提供的函數(shù)來執(zhí)行HTTP請求,獲取返回狀態(tài)碼,并通過多線程處理多個URL。

  • GetPageStatus 函數(shù):用于獲取指定URL的HTTP狀態(tài)碼。使用libcurl進(jìn)行初始化、設(shè)置請求頭、執(zhí)行請求,并最終獲取返回的狀態(tài)碼。
  • ThreadProc 函數(shù):線程執(zhí)行函數(shù),通過調(diào)用GetPageStatus函數(shù)獲取URL的狀態(tài)碼,并在控制臺輸出。如果狀態(tài)碼為200,則將URL記錄到日志文件中。
  • main 函數(shù):主函數(shù)讀取輸入的URL列表文件,逐行讀取并構(gòu)造完整的URL。通過CreateThread創(chuàng)建線程,每個線程處理一個URL。同時使用互斥鎖確保線程安全。

用戶可以通過在命令行傳遞兩個參數(shù),第一個參數(shù)為根網(wǎng)址,第二個參數(shù)為包含URL列表的文件路徑。程序?qū)⒆x取文件中的每個URL,通過libcurl發(fā)送HTTP 請求,獲取狀態(tài)碼,并輸出到控制臺。狀態(tài)碼為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;

// 設(shè)置鎖
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);         // 改協(xié)議頭
    curl_easy_setopt(easy_handle, CURLOPT_URL, HostUrl);                // 請求的網(wǎng)站
    curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);   // 設(shè)置回調(diào)函數(shù),屏蔽輸出
    return_code = curl_easy_perform(easy_handle);                       // 執(zhí)行CURL
    return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
  }
  curl_easy_cleanup(easy_handle);
  curl_global_cleanup();
  return retcode;
}

// 線程執(zhí)行函數(shù)
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 << "狀態(tài)碼: " << 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目錄掃描器,雖實現(xiàn)了目錄的掃描,但是有個很大的缺陷,第一是無法跨平臺,第二是無法實現(xiàn)優(yōu)雅的命令行解析效果,所以我們需要使用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"
    );
}

// 傳入網(wǎng)址和字典名
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);                // 請求的網(wǎng)站
    curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);   // 設(shè)置回調(diào)函數(shù),屏蔽輸出
    return_code = curl_easy_perform(easy_handle);                       // 執(zhí)行CURL
    return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
  }
  curl_easy_cleanup(easy_handle);
  curl_global_cleanup();
  return retcode;
}

// 線程執(zhí)行函數(shù)
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 << "狀態(tài)碼: " << 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);

  // 無參數(shù)直接返回
  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* 需要轉(zhuǎn)換,所以拷貝后轉(zhuǎn)換
    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 << "參數(shù)錯誤" << std::endl;
  }
  return 0;
}

傳入?yún)?shù)運行,當(dāng)訪問出現(xiàn)200提示,則自動保存到save.log中,運行效果如下。

  • main.exe --url https://www.lyshark.com --dict c://dict.log

以上就是C++使用LibCurl實現(xiàn)Web隱藏目錄掃描功能的詳細(xì)內(nèi)容,更多關(guān)于C++ LibCurl目錄掃描的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文帶你搞懂C++中的流量控制

    一文帶你搞懂C++中的流量控制

    限流可以認(rèn)為服務(wù)降級的一種,限流就是限制系統(tǒng)的輸入和輸出流量已達(dá)到保護(hù)系統(tǒng)的目的,這篇文章小編就來帶大家深入了解一下如何利用C++實現(xiàn)流量控制吧
    2023-10-10
  • C/C++中獲取重載函數(shù)地址的方法

    C/C++中獲取重載函數(shù)地址的方法

    函數(shù)重載是函數(shù)的一種特殊情況,C++允許在同一作用域中聲明幾個功能類似的同名函數(shù),這 些同名函數(shù)的形參列表不同,常用來處理實現(xiàn)功能類似數(shù)據(jù)類型不同的問題,本文給大家介紹了C/C++中獲取重載函數(shù)地址的方法,需要的朋友可以參考下
    2024-04-04
  • C語言超詳細(xì)講解文件的操作

    C語言超詳細(xì)講解文件的操作

    C語言文件操作的方法有很多,函數(shù)也有很多你知道哪些呢?下面是小編為大家?guī)淼腃語言文件操作的方法,歡迎閱讀
    2022-04-04
  • C語言實現(xiàn)自動分配地址的示例

    C語言實現(xiàn)自動分配地址的示例

    本文介紹了兩種自動分配地址的方法,包括通過宏定義實現(xiàn)地址分配和將EE地址作為一個結(jié)構(gòu)體,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • C語言圖書管理系統(tǒng)簡潔版

    C語言圖書管理系統(tǒng)簡潔版

    這篇文章主要為大家詳細(xì)介紹了C語言圖書管理系統(tǒng)簡潔版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++實現(xiàn)動態(tài)分配const對象實例

    C++實現(xiàn)動態(tài)分配const對象實例

    這篇文章主要介紹了C++實現(xiàn)動態(tài)分配const對象實例,包括了const對象的創(chuàng)建、刪除及應(yīng)用實例,需要的朋友可以參考下
    2014-10-10
  • C++實現(xiàn)LeetCode(兩個有序數(shù)組的中位數(shù))

    C++實現(xiàn)LeetCode(兩個有序數(shù)組的中位數(shù))

    這篇文章主要介紹了C++實現(xiàn)LeetCode(兩個有序數(shù)組的中位數(shù)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++深入探究用NULL來初始化空指針是否合適

    C++深入探究用NULL來初始化空指針是否合適

    在C++11新特性中,我們用nullptr來表示指針空值,這是為什么呢?好好地NULL為什么不繼續(xù)使用呢?說明在創(chuàng)造C++的大佬們一定發(fā)現(xiàn)了什么Bug,本篇我們就一起來討論一下吧
    2022-05-05
  • 淺析bilateral filter雙邊濾波器的理解

    淺析bilateral filter雙邊濾波器的理解

    這篇文章主要介紹了bilateral filter雙邊濾波器的通俗理解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 使用UART與PC通信實現(xiàn)msp430g2553單片機(jī)超聲波測距示例

    使用UART與PC通信實現(xiàn)msp430g2553單片機(jī)超聲波測距示例

    這篇文章主要介紹了使用UART與PC通信實現(xiàn)msp430g2553單片機(jī)超聲波測距示例,需要的朋友可以參考下
    2014-05-05

最新評論

嘉峪关市| 邛崃市| 通州市| 宁城县| 丹江口市| 金阳县| 河北区| 大埔区| 政和县| 石城县| 巴林左旗| 盘锦市| 西充县| 株洲市| 新巴尔虎右旗| 双鸭山市| 青川县| 临夏县| 河池市| 太湖县| 秦安县| 平江县| 弥渡县| 鞍山市| 临武县| 文昌市| 台东市| 龙游县| 道孚县| 金秀| 即墨市| 门头沟区| 法库县| 高碑店市| 桃园县| 汾西县| 赞皇县| 安徽省| 繁峙县| 南郑县| 若尔盖县|