Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows CMD 命令行防禦大全:Netsh、IPSec、WMI 腳本與軟體無人值守部署實戰

編輯:Windows XP 系統介紹

命令提示字元 (CMD) 不僅是系統管理入口,更是無圖形介面環境 (Server Core、遠程 Shell、WinPE、滲透測試後滲透) 下的生存利器。本文系統整理網路偵測、配置、防火牆 (IPSec)、系統資訊查詢 (WMI)、進程管理、軟體無人值守安裝等實戰技巧,打造純命令行作戰能力。

一、網路配置利器:Netsh 深度應用

Netsh (Network Shell) 提供腳本化網路配置能力,遠程 Shell 中透過 `netsh -f script.txt` 非互動執行,是自動化首選。

1. 核心查詢腳本模板 (儲存為 .bat 執行)

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

輸出含: 介面名稱、IP/DHCP 狀態、DNS/WINS 伺服器、閘道、MTU — 相當於 `ipconfig /all` 結構化輸出。

2. ARP 快取表 (對應 `arp -a`)

echo interface ip > s & echo show ipnet >> s & netsh -f s & del s

3. TCP/UDP 連線狀態 (對應 `netstat -an`)

echo interface ip > s & echo show tcpconn >> s & echo show udpconn >> s & netsh -f s & del s

4. 網卡硬體資訊 (唯一無替代指令)

echo interface ip > s & echo show interface >> s & netsh -f s & del s

輸出:介面型別、MAC 位址、媒體連線狀態、速度、傳輸佇列長度 — 硬體盤點必備。

二、IPSec 內建防火牆:命令行存取控制實戰

XP/2003 使用 `ipseccmd`,2000 使用 `ipsecpol` (需 `ipsecutil.dll`、`text2pol.dll`)。語法高度一致,以 `ipsecpol` 為例。

核心語法解析

ipsecpol -p  -r  -f  -n  -w reg -x
  • `-p` 策略名 (如 `myfirewall`)
  • `-r` 規則名 (如 `rpc-dcom`)
  • `-f` 篩選器清單:`*+0:Port:Protocol` 格式。`*`=任何來源,`0`=本機目標,`+`=鏡像雙向,`Port`=埠號,`Protocol`=tcp/udp/icmp。
  • `-n` 動作:`BLOCK` (封鎖, 必須大寫) / `PASS` (允許)
  • `-w reg` 寫入登錄檔持久化
  • `-x` 立即啟用策略

實戰規則範例

  1. 防禦 RPC/DCOM 攻擊 (封鎖 135,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`
  2. 防 Ping (封鎖 ICMP 回應請求):
    `ipsecpol -p myfirewall -r antiping -f "*+0::icmp" -n BLOCK -w reg -x` (同時阻止主機主動 Ping)
  3. 後門埠 IP 白名單 (如 DameWare 6129 埠僅允許特定 IP):
    `ipsecpol -p myfw -r dwmrc_block_all -f "*+0:6129:tcp" -n BLOCK -w reg`
    `ipsecpol -p myfw -r dwmrc_pass_me -f "123.45.67.89+0:6129:tcp" -n PASS -w reg -x`
    動態 IP 可用網段通配:`123.45.67.*+0:6129:tcp`

安全操作流程 (防鎖死自己)

  1. 啟用排程任務服務:`net start schedule`
  2. 設定 5 分鐘後自動停用策略留後路:`at 12:39 ipsecpol -p myfw -y -w reg`
  3. 測試連線正常 → 手動啟用永久生效:`ipsecpol -p myfw -x -w reg`
  4. 若測試失敗 → 刪除策略:`ipsecpol -p myfw -o -w reg` (必須先停用才能刪除生效)

查看現有 IPSec 策略

  • XP/2003:`ipseccmd show filters`
  • 2000:`netdiag /debug /test:ipsec` (需 `RemoteRegistry` 服務啟動)

三、WMI 命令行腳本:系統資訊與進程管理自動化

利用 `cscript //nologo` 執行內嵌 VBScript 片段,無需建立臨時 .vbs 檔案 (但示例中為教學生成再刪除)。

常用一鍵指令 (複製到 CMD 直接執行)

功能命令 (單行)
顯示系統版本@echo for each ps in getobject("winmgmts://./root/cimv2:win32_operatingsystem").instances_ : wscript.echo ps.caption & " " & ps.version : next > %tmp%\ps.vbs & cscript //nologo %tmp%\ps.vbs & del %tmp%\ps.vbs
列舉進程 (PID、名稱、路徑)@echo for each ps in getobject("winmgmts://./root/cimv2:win32_process").instances_ : wscript.echo ps.handle & vbtab & ps.name & vbtab & ps.executablepath : next > %tmp%\ps.vbs & cscript //nologo %tmp%\ps.vbs & del %tmp%\ps.vbs
終止指定 PID 進程 (如 123)@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 > %tmp%\pk.vbs & cscript //nologo %tmp%\pk.vbs 123 & del %tmp%\pk.vbs
重啟系統@echo for each os in getobject("winmgmts:{(shutdown)}!//./root/cimv2:win32_operatingsystem").instances_ : os.win32shutdown(2) : next > %tmp%\rb.vbs & cscript //nologo %tmp%\rb.vbs & del %tmp%\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 > %tmp%\sc.vbs & cscript //nologo %tmp%\sc.vbs & del %tmp%\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 > %tmp%\sc.vbs & cscript //nologo %tmp%\sc.vbs & del %tmp%\sc.vbs
顯示最後開機時間@echo for each os in getobject("winmgmts://./root/cimv2:win32_operatingsystem").instances_ : wscript.echo os.lastbootuptime : next > %tmp%\bt.vbs & cscript //nologo %tmp%\bt.vbs & del %tmp%\bt.vbs

四、軟體無人值守命令行部署

範例:WinPcap 3.0a 命令行安裝包自製

核心檔案: `wpcap.dll`、`packet.dll` → `%SystemRoot%\System32\`;`npf.sys` → `%SystemRoot%\System32\drivers\`。
註冊表: 新增系統服務 `NPF` (驅動服務),透過 `.inf` + `rundll32 setupapi,InstallHinfSection` 實現。

; _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 將 5 個檔案打包為自解壓 EXE,設定「解壓後執行」`_wpcap_.bat`,即得命令行安裝包。**此思路適用於多數無需啟動的綠色軟體/工具部署。**

Windows 更新補丁批次靜默安裝

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

參數:`-n` 不備份、`-z` 不重啟、`-q` 靜默模式。配合 WSUSOffline/Windows Update MiniTool 取得離線補丁包,實現離線環境批次打補丁。


總結: 掌握 Netsh 網路偵查、IPSec 埠口存取控制、WMI 系統資訊腳本、INF+Rundll32 驅動部署、補丁批次安裝,即可在無 GUI、弱頻寬、受限 Shell 環境下完成 90% 以上的系統管理與防禦佈署任務。建議將常用腳本封裝為 `.bat`/` .cmd` 攜帶隨身,臨陣磨槍,亦能發光。

Copyright © Windows教程網 All Rights Reserved