Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Windows CMD 環境下的網路安全配置實戰:Netsh、IPSec 與自動化腳本大全

編輯:關於伺服器

Windows 內建命令列工具(netsh、ipseccmd/ipsecpol、netdiag、WMI 腳本)可在無圖形介面的遠端 Shell(如 Telnet、SSH、PsExec、Meterpreter)環境下完成網路偵察、防火牆策略部署、服務安裝與系統管理。本文整理實戰常用指令與自動化範本。

一、Netsh:網路配置瑞士刀

Netsh 支援腳本模式(netsh -f script.txt),適合非互動式遠端執行。

1. 取得 TCP/IP 完整配置(等同 ipconfig /all)

echo interface ip > netsh.txt
echo show config >> netsh.txt
netsh -f netsh.txt
del netsh.txt

2. 檢視 ARP 快取(含介面索引)

echo interface ip > netsh.txt
echo show ipnet >> netsh.txt
netsh -f netsh.txt

3. 列出 TCP/UDP 連線(等同 netstat -an)

echo interface ip > netsh.txt
echo show tcpconn >> netsh.txt
echo show udpconn >> netsh.txt
netsh -f netsh.txt

4. 取得網卡詳細資訊(唯一無 GUI 替代功能)

echo interface ip > netsh.txt
echo show interface >> netsh.txt
netsh -f netsh.txt
輸出包含:介面名稱、索引、類型、MTU、速率、MAC、DHCP 狀態等,適合資產盤點腳本。

二、IPSec 策略命令列部署(Windows 2000/XP/2003)

工具來源:

  • XP/2003:安裝光碟 SUPPORT\TOOLS\SUPPORT.CAB → ipseccmd.exe
  • 2000:Resource Kit → ipsecpol.exe + ipsecutil.dll + text2pol.dll

語法高度相容,下文以 ipsecpol 為例。

1. 防禦 RPC/DCOM 攻擊(封鎖 135/137/138/139/445)

ipsecpol -p myfirewall -r rpc-dcom ^
  -f *+0:135:tcp *+0:135:udp *+0:137:udp *+0:138:udp *+0:139:tcp ^
       *+0:445:tcp *+0:445:udp ^
  -n BLOCK -w reg -x
  • -p 策略名、-r 規則名、-f 篩選器(*=任意來源、0=本機、+=雙向鏡像)。
  • -n BLOCK 必須大寫,-w reg 寫入登錄表持久化,-x 立即生效。

2. 防止被 Ping(同時阻擋主機主動 Ping)

ipsecpol -p myfirewall -r antiping -f *+0::icmp -n BLOCK -w reg -x

3. 後門連接埠 IP 白名單限制(以 DameWare 6129/tcp 為例)

# 先封鎖所有
ipsecpol -p myfw -r dwmrc_block_all -f *+0:6129:tcp -n BLOCK -w reg
# 再放行特定 IP/網段
ipsecpol -p myfw -r dwmrc_pass_me -f 123.45.67.89+0:6129:tcp -n PASS -w reg -x
# 動態 IP 可用網段寫法
ipsecpol -p myfw -r dwmrc_pass_me -f 123.45.67.*+0:6129:tcp -n PASS -w reg -x

4. 安全測試「留後路」:排程任務自動停用策略

net start schedule
at 12:39 ipsecpol -p myfw -y -w reg   # 5 分鐘後自動停用策略
# 測試通過後正式啟用
ipsecpol -p myfw -x -w reg
# 不妥則刪除
ipsecpol -p myfw -o -w reg

5. 查看現有 IPSec 篩選器

  • XP/2003:ipseccmd show filters
  • 2000:net start remoteregistry → netdiag /debug /test:ipsec(輸出極長,建議導向檔案)。

三、命令列軟體無訊息安裝範本

1. 通用 EXE/DLL/驅動安裝(以 WinPcap 為例)

原理:比對安裝前後檔案/登錄表快照 → 製作 .inf + 批次檔 → 打包自解壓縮 EXE。

; _wpcap_.inf
[Version]
Signature="$WINDOWS NT$"
[DefaultInstall.Services]
AddService=NPF,,winpcap_svr
[winpcap_svr]
DisplayName=Netgroup Packet Filter
ServiceType=0x1
StartType=3
ErrorControl=1
ServiceBinary=%12%\npf.sys
; _wpcap_.bat
rundll32.exe setupapi,InstallHinfSection DefaultInstall 128 %CD%\_wpcap_.inf
del _wpcap_.inf
if /i %CD%==%SYSTEMROOT%\system32 goto COPYDRV
copy packet.dll %SYSTEMROOT%\system32\
copy wpcap.dll %SYSTEMROOT%\system32\
del packet.dll
del wpcap.dll
:COPYDRV
if /i %CD%==%SYSTEMROOT%\system32\drivers goto END
copy npf.sys %SYSTEMROOT%\system32\drivers\
del npf.sys
:END
del %0
WinRAR 建立自解壓縮 EXE,「進階自解壓縮選項 → 解壓縮後執行」設為 _wpcap_.bat。批次檔最後一行 del %0 不可有換行,否則無法自我刪除。

2. 修補程式批次無訊息安裝

for %%f in (KB??????.exe) do %%f -n -z -q
for %%f in (KB??????.exe) do del %%f
del %0

-n 不備份、-z 不重開機、-q 安靜模式。

四、WMI 系統管理腳本速查(VBScript/Echo 產生器)

功能單行指令生成腳本並執行
顯示作業系統版本@echo for each ps in getobject("winmgmts:\\.\root\cimv2:win32_operatingsystem").instances_ : wscript.echo ps.caption & " " & ps.version : next > os.vbs & cscript //nologo os.vbs & del os.vbs
列舉進程 (PID, Name, Path)@echo for each ps in getobject("winmgmts:\\.\root\cimv2:win32_process").instances_ : wscript.echo ps.handle & vbtab & ps.name & vbtab & ps.executablepath : next > ps.vbs & cscript //nologo ps.vbs & del ps.vbs
終止指定 PID 進程@echo for each ps in getobject("winmgmts:\\.\root\cimv2:win32_process").instances_ : if ps.handle=wscript.arguments(0) then wscript.echo ps.terminate : end if : next > pk.vbs & cscript //nologo pk.vbs 123 & del pk.vbs
重啟系統@echo for each os in getobject("winmgmts:{(shutdown)}!\\.\root\cimv2:win32_operatingsystem").instances_ : os.win32shutdown(2) : next > rb.vbs & cscript //nologo rb.vbs & del rb.vbs
列舉自動啟動服務@echo for each sc in getobject("winmgmts:\\.\root\cimv2:win32_service").instances_ : if sc.startmode="Auto" then wscript.echo sc.name & " - " & sc.pathname : next > sc.vbs & cscript //nologo sc.vbs & del sc.vbs
列舉正在執行服務@echo for each sc in getobject("winmgmts:\\.\root\cimv2:win32_service").instances_ : if sc.state="Running" then wscript.echo sc.name & " - " & sc.pathname : next > sc.vbs & cscript //nologo sc.vbs & del sc.vbs
顯示上次開機時間@echo for each os in getobject("winmgmts:\\.\root\cimv2:win32_operatingsystem").instances_ : wscript.echo os.lastbootuptime : next > bt.vbs & cscript //nologo bt.vbs & del bt.vbs
實戰提醒: 遠端命令列環境輸出緩衝區有限,大量輸出建議導向檔案(> out.txt)再分段讀取;修改 IPSec 策略前務必用 at 排程留後路,避免自我封鎖導致永久斷線。
Copyright © Windows教程網 All Rights Reserved