Nhiều quản trị viên thường dùng công cụ console netstat hoặc đồ họa TCPView để hiển thị thông tin về kết nối TCP/IP đang hoạt động và cổng TCP mở trong Windows. Thay vì netstat, bạn có thể dùng cmdlet Get-NetTCPConnection trong PowerShell để lấy thông tin về kết nối mạng đang hoạt động trong Windows, cổng TCP mở, và tiến trình đang chạy sử dụng giao thức TCP/IP. PowerShell giúp dễ dàng viết script phức tạp để lấy thông tin và giám sát cổng TCP mở, tiến trình, và kết nối mạng đã thiết lập.
Thử chạy lệnh Get-NetTCPConnection không có bất kỳ tùy chọn nào.
Giống như netstat, lệnh đã hiển thị danh sách tất cả kết nối đang hoạt động với địa chỉ IP local và remote, cổng, trạng thái kết nối (Listen, Established Internet, TimeWait, Bound, CloseWait, SynReceived, SynSent), và ID tiến trình (PID) đang sử dụng kết nối TCP này.
Bạn có thể hiển thị danh sách cổng mở (listening) trên máy tính cục bộ:
Get-NetTCPConnection -State Listen | Select-Object -Property LocalAddress, LocalPort, RemoteAddress, RemotePort, State | Sort-Object LocalPort |ft
Cmdlet Get-NetUDPEndpoint được dùng để lấy thông tin về cổng UDP.
Bạn có thể hiển thị chỉ kết nối bên ngoài (Internet):
Get-NetTCPConnection -AppliedSetting Internet
Bạn có thể hiển thị tên DNS của remote host và tên tiến trình cho kết nối TCP:
Get-NetTCPConnection -State Established |Select-Object -Property LocalAddress, LocalPort,@{name='RemoteHostName';expression={(Resolve-DnsName $_.RemoteAddress).NameHost}},RemoteAddress, RemotePort, State,@{name='ProcessName';expression={(Get-Process -Id $_.OwningProcess). Path}},OffloadState,CreationTime |ft
Script PowerShell này resolve tất cả địa chỉ IP host thành tên DNS và chỉ định tên tiến trình cho tất cả kết nối.
Tên tiến trình cha PID, bạn có thể hiển thị danh sách các dịch vụ Windows liên quan đang sử dụng mạng:
Get-WmiObject Win32_Service | Where-Object -Property ProcessId -In (Get-NetTCPConnection).OwningProcess | Where-Object -Property State -eq Running | Format-Table ProcessId, Name, Caption, StartMode, State, Status, PathName
Bạn có thể xem chỉ các kết nối mạng được khởi tạo bởi tiến trình cụ thể. Để làm điều đó, bạn có thể dùng script PowerShell sau:
$TrackProcessName = "*chrome*"
$EstablishedConnections = Get-NetTCPConnection -State Established |Select-Object -Property LocalAddress, LocalPort,@{name='RemoteHostName';expression={(Resolve-DnsName $_.RemoteAddress).NameHost}},RemoteAddress, RemotePort, State,@{name='ProcessName';expression={(Get-Process -Id $_.OwningProcess). Path}}, OffloadState,CreationTime
Foreach ($Connection in $EstablishedConnections)
{
If ($Connection.ProcessName -like $TrackProcessName)
{
$Connection|ft
}
}
Bạn có thể dùng cmdlet Get-NetTCPConnection trong nhiều kịch bản. Ví dụ, bạn có thể tạo script PowerShell đơn giản để theo dõi xem kết nối có được thiết lập từ địa chỉ IP cụ thể đến cổng local quy định không và hiển thị popup thông báo cho quản trị viên.
Trong ví dụ sau, script PowerShell kiểm tra xem kết nối từ địa chỉ IP quy định có xuất hiện trên cổng RDP mặc định 3389 không. Nếu kết nối xuất hiện, script sẽ hiển thị popup thông báo và ghi ngày giờ kết nối vào file text:
$SourceIP = "192.168.13.125"
$TargetPort ="3389"
$log = "C:\PS\rdp_connection_log.txt"
$EstablishedConnections = Get-NetTCPConnection -State Established
Foreach ($Connection in $EstablishedConnections)
{
If (($Connection.RemoteAddress -eq $SourceIP) -and ($Connection.LocalPort -eq $TargetPort))
{
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = "New RDP connection to your computer from $($Connection.RemoteAddress)"
$balmsg.BalloonTipTitle = "New RDP connection from ($Connection.RemoteAddress)"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(10000)
(Get-Date).ToString() + ' ' + $Connection.RemoteAddress + ' an RDP connection is established ' >> $log
}
}
Theo cách tương tự, bạn có thể giám sát và log kết nối mạng qua bất kỳ giao thức nào khác, như SSH, SMB, FTP, SMTP, v.v. Script PowerShell này có thể chuyển đổi thành Windows service sẽ tự động khởi động.
Bạn có thể dùng script cùng với script chúng ta đã thảo luận trước đó: RDP Brute Force Attack Protection with Powershell.
Bạn có thể lấy danh sách cổng TCP mở và kết nối trên máy tính từ xa bằng cmdlet PowerShell remoting (Enter-PSSession và Invoke-Command).
Invoke-Command -ComputerName be-dc01 {Get-NetTCPConnection -State Established}
Cmdlet Get-NetTCPConnection (cũng như Test-NetConnection) có thể rất hữu ích để theo dõi và chẩn đoán kết nối mạng trong Windows.