這是一個用 Python 實作的網路偵察工具,支援 CIDR 與 IP 範圍掃描,多執行緒並發抓取 HTTP Banner、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
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
time.sleep(0.1))避免拒絕服務