⚠️ 關鍵限制: 僅適用於 Internet Explorer** 瀏覽器(需 ActiveXObject 支持)。現代瀏覽器(Chrome、Firefox、Edge Chromium)出於安全考慮,完全禁止 JavaScript 訪問系統註冊表。本方案僅適用於基於 IE 內核的老舊企業內部系統、HTA 應用或特定舊版 IE 環境。
| 類型 | 註冊表路徑 |
|---|---|
| 域名 | HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains |
| IP 地址 | HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges |
信任站點區域標識值為 2(十六進制 0x2)。
navigator.userAgent.indexOf("MSIE") !== -1)window.location.hostname)WScript.Shell ActiveX 對象RegRead 讀取值,捕獲異常判斷是否存在https:// 或 https:// 開頭)<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>信任站點檢測</title>
<style>
.mainContent { margin: 100px auto 0 100px; }
</style>
<script src="js/testRegister.js"></script>
</head>
<body>
<div class="mainContent">
<input type="button" value="檢測是否為信任站點" id="testRegister" />
</div>
</body>
</html>
/* 判斷可信任站點(域名/IP) */
window.onload = function() {
var btn = document.getElementById("testRegister");
btn.onclick = function() {
// 1. 瀏覽器檢測
if (navigator.userAgent.indexOf("MSIE") === -1) {
alert("僅支持 IE 瀏覽器!");
return;
}
var hostname = window.location.hostname;
var shell;
try {
shell = new ActiveXObject("WScript.Shell");
} catch (e) {
alert("無法創建 WScript.Shell,請檢查 ActiveX 設置或降低安全級別。");
return;
}
// IP 正則
var ipRegex = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
if (hostname !== "localhost" && !ipRegex.test(hostname)) {
// ===== 域名邏輯 =====
checkDomainTrust(shell, hostname);
} else {
// ===== IP 邏輯 =====
checkIpTrust(shell, hostname);
}
};
};
// 域名信任檢測
function checkDomainTrust(shell, hostname) {
var prefix = "", suffix = "";
var dotIndex = hostname.indexOf(".");
if (dotIndex !== -1) {
prefix = hostname.substring(0, dotIndex);
suffix = hostname.substring(dotIndex + 1);
}
var basePath = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\";
var found = false;
var invalidProtocol = false;
// 1. 精確域名 + http
if (readRegSafe(shell, basePath + hostname + "\\http")) found = true;
// 2. 通配符域名 *.suffix + http
if (!found && suffix) {
if (readRegSafe(shell, basePath + suffix + "\\" + prefix + "\\http")) found = true;
}
// 3. 協議合法性檢查:必須有 http/https
if (!found) {
if (readRegSafe(shell, basePath + (suffix ? suffix + "\\" + prefix : hostname) + "\\*")) {
invalidProtocol = true;
}
}
if (invalidProtocol) {
alert("信任站點協議不合法,必須以 https:// 或 https:// 開頭!");
} else if (!found) {
alert("域名 " + hostname + " 不在信任站點列表中!");
} else {
alert("存在信任站點!");
}
}
// IP 信任檢測
function checkIpTrust(shell, hostname) {
var basePath = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\";
var found = false;
// 枚舉 Ranges 下的 Range1, Range2... (實際上限不固定,這裡設 2000 為上限)
for (var i = 1; i < 2000; i++) {
try {
var rangeIp = shell.RegRead(basePath + "Range" + i + "\\:Range");
if (rangeIp === hostname) {
found = true;
break;
}
} catch (e) {
// 讀取失敗通常意味著該項不存在,繼續下一個
}
}
if (found) {
alert("存在信任站點!");
} else {
alert("IP " + hostname + " 不在信任站點列表中!");
}
}
// 安全讀取註冊表(捕獲異常返回 null)
function readRegSafe(shell, path) {
try {
var val = shell.RegRead(path);
return val === 2 || val === 0x2; // 信任站點區域值為 2
} catch (e) {
return false;
}
}
new ActiveXObject("WScript.Shell") 需要:
Domains
example.com
www
http = 2 (DWORD)
https = 2
*
http = 2
*.example.com (通配符前綴存儲方式不同版本有差異)
Ranges
Range1
:Range = "192.168.1.100" (字符串)
http = 2
Range2
...
RegRead 讀取不存在的鍵會拋出異常,利用 try-catch 將「異常=不存在」轉化為業務邏輯。
現代 Web 開發應避免依賴客戶端註冊表:
| 需求 | 現代方案 |
|---|---|
| 判斷用戶是否授信 | 服務端會話/Token 驗證、OAuth、JWT |
| 記住用戶偏好 | localStorage、IndexedDB、Cookie(SameSite=Lax) |
| 內網系統免登 | Windows 集成認證 (Kerberos/NTLM)、SPNEGO、Azure AD |
| 企業策略分發 | Group Policy、MDM、瀏覽器擴展/企業策略 |
通過 WScript.Shell 讀取註冊表檢測 IE 信任站點是典型的「舊時代」技術。它揭示了 IE 時代安全區域模型的實現細節,但已完全不適用於現代 Web 開發。理解其原理有助於維護遺留系統,但新項目應堅決使用標準化、跨平台、安全的認證授權方案。