萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> 重灌系統教學

CMD網路管理命令大全:netsh與腳本自動化實戰

Windows命令列工具分為網路檢測、連線、配置三類。本文聚焦netsh網路配置與WMI腳本自動化,提供防禦、巡檢、滲透測試實用指令集。

一、WMI腳本快速查詢系統資訊(Echo單行版)

將以下內容存為.bat即可執行:

@echo off
:: 1. 顯示作業系統版本
for /f "tokens=2 delims==" %%a in ('wmic os get Caption /value') do set OS=%%a
for /f "tokens=2 delims==" %%b in ('wmic os get Version /value') do set VER=%%b
echo %OS% %VER%

:: 2. 列舉進程(PID、名稱、路徑)
wmic process get ProcessId,Name,ExecutablePath /format:table

:: 3. 終止進程(需PID參數)
if "%1"=="" (echo 用法: %0 PID & exit /b)
wmic process where ProcessId=%1 call terminate

:: 4. 重啟系統
wmic os where Primary=TRUE call Win32Shutdown 2

:: 5. 列舉自啟動服務
wmic service where StartMode="Auto" get Name,PathName /format:table

:: 6. 列舉運行中服務
wmic service where State="Running" get Name,PathName /format:table

:: 7. 最後開機時間
wmic os get LastBootUpTime /value

二、netsh網路配置深度應用

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

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

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

可獲取:網卡數量、IP/DHCP狀態、內網IP、DNS、閘道等。

2. ARP表增強版

echo interface ip > arp.txt
echo show ipnet >> arp.txt
netsh -f arp.txt & del arp.txt

比arp -a多介面索引、類型資訊。

3. TCP/UDP連線狀態(等同netstat -an)

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

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

echo interface ip > nic.txt
echo show interface >> nic.txt
netsh -f nic.txt & del nic.txt

顯示介面名稱、型號、MAC、MTU、速度、狀態等關鍵屬性。

三、IPSec命令列防禦實戰(XP/2003適用)

工具:ipseccmd.exe(XP安裝盤SUPPORT\TOOLS\SUPPORT.CAB內)。

案例1:封鎖RPC/DCOM高危埠(135,137-139,445)

ipseccmd -p myfw -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(雙向)

ipseccmd -p myfw -r anti-ping -f *+0::icmp -n BLOCK -w reg -x

案例3:後門埠IP白名單(如DameWare 6129/tcp)

:: 全域封鎖
ipseccmd -p myfw -r block-all -f *+0:6129:tcp -n BLOCK -w reg
:: 允許特定IP/網段
ipseccmd -p myfw -r allow-me -f 123.45.67.89+0:6129:tcp -n PASS -w reg -x
:: 動態IP用網段
ipseccmd -p myfw -r allow-me -f 123.45.67.*+0:6129:tcp -n PASS -w reg -x

安全測試技巧:計劃任務留後門

:: 5分鐘後自動停用策略,防鎖死自己
at 12:39 ipseccmd -p myfw -y -w reg
:: 測試通過後正式啟用
ipseccmd -p myfw -x -w reg
:: 查看現有篩選器
ipseccmd show filters

四、軟體無人值守安裝原理

本質:複製檔案到指定目錄 + 寫入註冊表。掌握檔案列表與註冊鍵值即可用批次檔/腳本實現靜默部署(如WinPcap、Nmap等工具)。

熟練netsh與WMI腳本,讓命令列成為網路運維、安全稽核的利器。

copyright © 萬盛學電腦網 all rights reserved