首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> 關於C

Python 網路工具:HTTP Banner 抓取器 v0.3

     編輯:關於C

多執行緒 HTTP 服務指紋識別工具

這是一個用 Python 實作的網路偵察工具,支援 CIDR 與 IP 範圍掃描,多執行緒並發抓取 HTTP Banner、Server 標頭、頁面標題,適用於資產發現與攻擊面管理。

核心功能

  • 目標格式支援:CIDR (192.168.1.0/24)、IP 範圍 (192.168.1.1-192.168.1.254)
  • 自定義埠口:支援 -p 參數指定埠口(預設 80)
  • 多執行緒並發:大幅提升掃描速度
  • 智慧編碼偵測:chardet + HTML meta 標籤雙重檢測
  • 關鍵資訊提取:狀態碼、Server 標頭、頁面標題

關鍵模組解析

編碼偵測策略

def detect_charset(html_content):
    if not html_content:
        return None
    
    # 1. chardet 統計分析
    detected = chardet.detect(html_content)
    if detected['encoding'] and detected['confidence'] > 0.9:
        return detected['encoding']
    
    # 2. HTML meta 標籤解析
    charset_re = re.compile(r'(?i)charset\s*=\s*["\']?([^"\'>\s]+)')
    match = charset_re.search(html_content[:1000])
    if match:
        return match.group(1)
    
    return None

HTTP Banner 抓取邏輯

def fetch_banner(url):
    try:
        resp = requests.get(url, timeout=2)
        body = resp.content
        charset = detect_charset(body)
        
        # 編碼轉換
        if charset and charset.lower() not in ('ascii', 'unicode'):
            try:
                body = body.decode(charset, errors='replace')
            except:
                body = ''
        
        # 提取資訊
        status = resp.status_code
        server = resp.headers.get('Server', '')[:13]
        title = ''
        if status in (200, 403, 401):
            title_match = re.search(r'(.*?)', body, re.IGNORECASE | re.DOTALL)
            if title_match:
                title = title_match.group(1).strip()
        
        # 執行緒安全輸出
        with print_lock:
            print(f'{url.lstrip("https://")}\t{status}\t{server:<10}\t{title}')
            
    except Exception:
        pass  # 靜默忽略連線錯誤

使用範例

# 掃描 C 段
python http_banner.py 192.168.1.0/24

# 掃描 IP 範圍,指定埠口 8080
python http_banner.py 192.168.1.1-192.168.1.254 -p 8080

輸出格式

IP:PORT       STATUS  SERVER       TITLE
192.168.1.1   200     nginx        Welcome to nginx!
192.168.1.50  403     Apache       403 Forbidden
192.168.1.100 200     IIS 10.0     IIS Windows Server

安全與法律提醒 ⚠️

  • 僅對授權範圍進行掃描
  • 大量掃描可能觸發 IDS/IPS 告警
  • 建議加入速率限制(如 time.sleep(0.1))避免拒絕服務
  • 生產環境建議使用成熟工具(nmap, masscan, httpx)

改進方向

  • 支援 HTTPS (SSL/TLS) 與憑證資訊提取
  • 整合指紋識別資料庫(Wappalyzer, WhatWeb)
  • 輸出結構化格式
  • 加入重試機制與更完善的異常處理
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved