資料庫服務探測風險
MySQL
- 預設綁定所有介面 (含外網),TCP 3306 埠監聽,極易被掃描工具發現。
- 建議:僅綁定內網 IP (
bind-address=127.0.0.1 或內網 IP),防火牆封鎖 3306 對外存取。
SQL Server
- 預設 TCP 1433 監聽,可人為修改埠號。
- 關鍵風險:UDP 1434 (SQL Server Resolution Service) 無法關閉/修改。發送 1 位元組
0x02 封包即可取得完整執行個體資訊:主機名、實例名、版本、命名管道、實際 TCP 埠號。
- 範例回應:
ServerName;Sky;InstanceName;sky;IsClustered;No;Version;8.00.194;tcp;3341;np;\\sky\pipe\MSSQL$XHT310\sql\query → 攻擊者即知實際埠號 3341。
UDP 1434 掃描器原始碼 (C#)
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace ConsoleApplication3
{
class Class1
{
private static UdpClient m_Client;
public static string Listen(string hostip)
{
IPAddress ip = IPAddress.Parse(hostip);
IPEndPoint ep = new IPEndPoint(ip, 1434);
byte[] data = m_Client.Receive(ref ep);
return Encoding.ASCII.GetString(data);
}
public static void Send(string hostip)
{
byte[] buffer = { 0x02 }; // 僅 0x02、0x03、0x04 會回應
int sent = m_Client.Send(buffer, 1, hostip, 1434);
if (sent <= 0) Console.WriteLine("發送失敗: " + sent);
}
public static void OutputInfo(string str)
{
string[] parts = str.Split(new char[]{';',';'});
foreach (string p in parts) Console.Write(p + " ");
}
[STAThread]
static void Main(string[] args)
{
Console.Write("輸入掃描目標 IP: ");
string ip = Console.ReadLine();
m_Client = new UdpClient();
Send(ip);
string info = Listen(ip);
OutputInfo(info);
}
}
}
防禦建議
- 封鎖 UDP 1434:防火牆層級完全封鎖對外 UDP 1434,此為最有效對策。
- 隱藏 SQL Browser 服務:若非叢集/多實例必要,停用 SQL Server Browser 服務。
- 最小權限帳號:服務帳號使用最低權限網域帳號,非內建系統帳號。
- IPsec/防火牆限制來源 IP:僅允許應用伺服器 IP 存取 SQL 連接埠。
- 定期掃描自身:使用 Nmap 等工具驗證對外暴露面。