顯示具有 PowerShell 標籤的文章。 顯示所有文章
顯示具有 PowerShell 標籤的文章。 顯示所有文章

2025年7月3日 星期四

[研究]PowerShell 檢查電腦中是否有檔案符合某些 SHA256 的 hash 值

[研究]PowerShell 檢查電腦中是否有檔案符合某些 SHA256 的 hash 值

2025-07-03

環境:Windows Server 2019 +Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

顯示 PowerShell 版本,為 5.1

PS C:\temp2> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  7434


PS C:\temp2>

C.ps1 內容 (請用「記事本 (Notepad)」存檔 (ANSI 或 UniCode 編碼存檔都可),不要用 Notepad++ 預設編碼存檔,否則執行會亂碼

# 適用 PowerShell 5.1

# 放最前面
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 定義多個要比對的 SHA256 雜湊值
$targetHashes = @(
    "F6E0A1E2AC41945A9AA7FF8A8AAA0CEBC12A3BCC981A929AD5CF810A090E11AE",
    "CC399D73903F06EE694032AB0538F05634FF7E1CE5E8E50AC330A871484F34CF",
    "3333333333333333333333333333333333333333333333333333333333333333"
)

# 指定起始搜尋路徑
$searchPath = "C:\Temp2"

# 建立 HashSet 結構
$hashSet = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
foreach ($h in $targetHashes) {
    $hashSet.Add($h) | Out-Null
}

# 搜尋檔案並計算 SHA256
Get-ChildItem -Path $searchPath -Recurse -File -ErrorAction SilentlyContinue | ForEach-Object {
    try {
        $hash = Get-FileHash -Path $_.FullName -Algorithm SHA256 -ErrorAction Stop
        if ($hashSet.Contains($hash.Hash)) {
            Write-Output "找到檔案: $($_.FullName) ,SHA256: $($hash.Hash)"
        }
    } catch {
        # 忽略權限不足或鎖定錯誤
    }
}

顯示目前輸出訊息邊碼

PS C:\temp2> [Console]::OutputEncoding


BodyName          : big5
EncodingName      : 繁體中文 (Big5)
HeaderName        : big5
WebName           : big5
WindowsCodePage   : 950
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.InternalEncoderBestFitFallback
DecoderFallback   : System.Text.InternalDecoderBestFitFallback
IsReadOnly        : True
CodePage          : 950



PS C:\temp2> 

執行結果

PS C:\temp2> .\c.ps1
?曉瑼?: C:\Temp2\1.txt 嚗HA256: F6E0A1E2AC41945A9AA7FF8A8AAA0CEBC12A3BCC981A929AD5CF810A090E11AE
?曉瑼?: C:\Temp2\2.txt 嚗HA256: CC399D73903F06EE694032AB0538F05634FF7E1CE5E8E50AC330A871484F34CF

PS C:\temp2> .\C_Ansi.ps1
找到檔案: C:\Temp2\1.txt ,SHA256: F6E0A1E2AC41945A9AA7FF8A8AAA0CEBC12A3BCC981A929AD5CF810A090E11AE
找到檔案: C:\Temp2\2.txt ,SHA256: CC399D73903F06EE694032AB0538F05634FF7E1CE5E8E50AC330A871484F34CF

PS C:\temp2> .\C_Unicode.ps1
找到檔案: C:\Temp2\1.txt ,SHA256: F6E0A1E2AC41945A9AA7FF8A8AAA0CEBC12A3BCC981A929AD5CF810A090E11AE
找到檔案: C:\Temp2\2.txt ,SHA256: CC399D73903F06EE694032AB0538F05634FF7E1CE5E8E50AC330A871484F34CF

PS C:\temp2> 

計算檔案 SHA256 值

PS C:\Users\Administrator> Get-FileHash -Path "C:\Temp2\1.txt" -Algorithm SHA256

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
SHA256          F6E0A1E2AC41945A9AA7FF8A8AAA0CEBC12A3BCC981A929AD5CF810A090E11AE       C:\Temp2\1.txt


PS C:\Users\Administrator> 


(完)

相關

2025年3月21日 星期五

[研究]用 PowerShell 安裝 IIS 和 CGI (Windows Server 2019)

[研究]用 PowerShell 安裝 IIS 和 CGI (Windows Server 2019)

2025-03-21

1. 開啟 PowerShell(以系統管理員身份執行)

按 Win + X,選擇 Windows PowerShell (Admin) 或 Windows Terminal (Admin)。

2. 安裝 IIS 及 CGI

Install-WindowsFeature -Name Web-Server, Web-CGI -IncludeManagementTools

  • 安裝 IIS(Web Server)
  • 安裝 CGI(Common Gateway Interface)
  • 包含 管理工具(IIS Manager)

3. 確認安裝狀態

Get-WindowsFeature -Name Web-Server, Web-CGI

如果 Installed 欄位顯示 True,表示安裝成功。

4. 啟動 IIS 服務

Start-Service W3SVC

Get-Service -Name W3SVC

IIS 預設會開啟 80 端口,您可以透過 http://localhost/ 測試。

5. 允許防火牆規則(如果需要開放外部訪問)

如果此伺服器要被其他裝置訪問,請開啟 HTTP 和 HTTPS 連線:

New-NetFirewallRule -DisplayName "IIS HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow

New-NetFirewallRule -DisplayName "IIS HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow

安裝完成後,您可以在 IIS 管理員 (inetmgr) 中檢視與管理 CGI 設定。

********************************************************************************

實際測試





PS C:\> Install-WindowsFeature -Name Web-Server, Web-CGI -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {應用程式開發, CGI, 一般 HTTP 功能, 預設文...


PS C:\> Get-WindowsFeature -Name Web-Server, Web-CGI

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] 網頁伺服器 (IIS)                                    Web-Server                     Installed
            [X] CGI                                     Web-CGI                        Installed


PS C:\> Start-Service W3SVC
PS C:\> Get-Service -Name W3SVC

Status   Name               DisplayName
------   ----               -----------
Running  W3SVC              World Wide Web Publishing 服務


PS C:\> New-NetFirewallRule -DisplayName "IIS HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow


Name                  : {c890fe1d-7041-4e3d-9855-4e97204d4ebc}
DisplayName           : IIS HTTP
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : 已從存放區成功剖析規則。 (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local



PS C:\> New-NetFirewallRule -DisplayName "IIS HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow


Name                  : {34876d5a-c3de-4033-9646-b77687d756f0}
DisplayName           : IIS HTTPS
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : 已從存放區成功剖析規則。 (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local



PS C:\>


(完)


[研究]用PowerShell安裝Chocolatey 2.4.3 (Windows Server 2019)

[研究]用PowerShell安裝Chocolatey 2.4.3 (Windows Server 2019)

2025-03-21

Chocolatey 是一款適用於 Windows 的包管理器,它能夠讓使用者通過命令行輕鬆安裝、升級、配置及卸載軟體。與 Linux 系統中的包管理器(如 apt 或 yum)類似,Chocolatey 使得 Windows 使用者能夠快速管理軟體安裝,並且能夠從一個中心位置來進行管理,避免手動下載安裝檔案。

使用者只需透過簡單的命令,如 choco install <軟體名稱>,便可安裝指定的軟體。Chocolatey 支援大量常見的應用程式,並且會自動處理安裝過程中的依賴性,讓管理軟體變得更加方便。

它還支援自動化安裝流程,適合開發者和系統管理員進行批量安裝或自動化配置。

Chocolatey => 巧克力口味

官方網站
https://chocolatey.org/

Open Source, Pro, Business 版本比較
https://chocolatey.org/compare

Chocolatey是一個Windows下的軟體套件管理器,讓使用者可以像在類Unix系統中使用Yum和APT一樣使用它,在Windows中實現自動化輕鬆管理Windows軟體的所有方面(安裝,組態,升級和解除安裝)。
https://zh.wikipedia.org/zh-tw/Chocolatey

安裝

Set-ExecutionPolicy Bypass -Scope Process -Force; 

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; 

iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))






PS C:\> Set-ExecutionPolicy Bypass -Scope Process -Force;
PS C:\> [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
PS C:\> iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Downloading https://community.chocolatey.org/api/v2/package/chocolatey/2.4.3 to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip
Not using proxy.
Extracting C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocoInstall
Installing Chocolatey on the local machine
The registry key for .Net 4.8 was not found or this is forced
Downloading 'https://download.visualstudio.microsoft.com/download/pr/2d6bb6b2-226a-4baa-bdec-798822606ff1/8494001c276a4b96804cde7829c04d7f/ndp48-x86-x64-allos-enu.exe' to 'C:\Users\ADMINI~1\AppData\Local\Temp\ndp48-x86-x64-allos-enu.exe' - the installer is 100+ MBs, so this could take a while on a slow connection.
Installing 'C:\Users\ADMINI~1\AppData\Local\Temp\ndp48-x86-x64-allos-enu.exe' - this may take awhile with no output.
警告: .NET Framework 4.8 was installed, but a reboot is required before using Chocolatey CLI.
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
  (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
  and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey CLI folders if they do not already exist.

chocolatey.nupkg file not installed in lib.
 Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
警告: Not setting tab completion: Profile file does not exist at
'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey CLI (choco.exe) is nearly ready.
You need to restart this machine prior to using choco.
Ensuring Chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder

PS C:\> choco -v
2.4.3
PS C:\>



(完)

相關




2025年3月20日 星期四

[研究]PowerShell安裝 (Windows Server 2019)

[研究]PowerShell安裝 (Windows Server 2019) 

2025-03-20

其實Windows Server 2019 安裝完成後,就已經預先安裝了 PowerShell 5.1。
















********************************************************************************

【下載、安裝新版PowerShell】

PowerShell 網站

https://github.com/PowerShell/PowerShell/releases

https://github.com/PowerShell/PowerShell/tags








********************************************************************************

PowerShell 7.5 和 5.1 差異

差異點 PowerShell 5.1 (Windows PowerShell) PowerShell 7.5 (PowerShell Core)
架構 .NET Framework .NET (原 .NET Core)
跨平台 僅支援 Windows 支援 Windows、Linux、macOS
效能 較低 更快、更高效
支援狀態 進入維護模式,不再新增功能 持續開發中
模組相容性 與大多數 Windows 模組相容 透過 WindowsCompatibility 模組提供支援
內建命令 傳統命令 更多現代化命令 (例如 ForEach-Object -Parallel)
JSON 處理 ConvertTo-Json 效能較低 ConvertTo-Json 效能更好
背景作業 Start-Job 需 PowerShell 進程 Start-Job 原生支援跨平台背景工作
SSH 支援 不支援內建 SSH 遠端連線 內建支援 SSH 遠端連線
容器支援 無內建支援 更好的容器支援
更新方式 透過 Windows 更新或手動下載 透過 winget、brew、apt 等套件管理工具安裝

(完)

2025年3月18日 星期二

[研究]套GCB後,Get-MpComputerStatus出現錯誤訊息 :無法執行外來方法

[研究]套GCB後,Get-MpComputerStatus出現錯誤訊息 :無法執行外來方法

2025-03-18

(下圖)沒套GCB的,PowerShell 執行 Get-MpComputerStatus 正常

(下圖)某台套用 GCB 後,執行 Get-MpComputerStatus 出錯


系统管理員:Windows PowerShell

Windows PowerShell
著作権(C)Microsoft Corporation.著作權所有,並保留一切權利。

PS C:\Windows\system32> Get-MpConputerStatus

Get-MpComputerStatus 無法執行外來方法·

位於 線路:1 字元:1

+Get-MpComputerStatus

+ CategoryInfo MetadataError: (MSFT_MpComputerStatus.ROOTMicrosoft)....pComputerStatus) (Get-MpComputer)

Status], CimException

+ FullyQualifiedErrorld : MI RESULT 16,Get-MpComputerStatus

PS C: Windows'lsystem32>


(下圖)但另一台套GCB的,PowerShell 執行 Get-MpComputerStatus 正常


後來又測試很多台套 GCB 的,都正常。
********************************************************************************
2025-03-18 補,想到異常那台帳號曾變更過,其他沒有,測試一下
(下圖)套 GCB 後,變更帳號,admin1改為admin2,重新啟動Windows,Get-MpComputerStatus 仍可執行



(完)

2025年1月22日 星期三

[研究]cmd.exe或PowerShell如何顯示有遠端桌面或本機登入的帳號 ?

[研究]cmd.exe或PowerShell如何顯示有遠端桌面或本機登入的帳號 ?

2025-01-22

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

cmd.exe 顯示可以遠端登入的帳號

C:\Users\Administrator>net localgroup "remote desktop users"
別名     remote desktop users
註解     在這個群組中的成員被授權進行遠端登入

成員

-------------------------------------------------------------------------------
admin1
admin2
命令已經成功完成。


C:\Users\Administrator>

PowerShell 顯示可以遠端登入的帳號

Windows PowerShell
著作權(C) Microsoft Corporation。保留擁有權利。

安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindows

PS C:\Users\Administrator> Get-LocalGroupMember -Group "Remote Desktop Users"

ObjectClass Name           PrincipalSource
----------- ----           ---------------
使用者      WIN2022\admin1 Local
使用者      WIN2022\admin2 Local


PS C:\Users\Administrator>



注意:administrators 群組中的帳號可能不會列出,或會列出,但都有遠端桌面權限。

上面的檢測方法,只是檢測那些帳號在「遠端桌面 (Remote Desktop Users)」群組中,無法真正列出所有能做遠端桌面登入的帳號。

********************************************************************************

cmd.exe 查看那些帳號屬於 administrators 群組

C:\>net localgroup "administrators"
別名 administrators 註解 Administrators 可以完全不受限制地存取電腦/網域 成員 ------------------------------------------------------------------------------- admin1 admin2 Administrator 命令已經成功完成。 C:\>


********************************************************************************

cmd.exe 或 PowerShell 無法檢測那些帳號可本機登入。只能用檢查是否在 Users 或 administrators 做粗略的判斷 (未必正確)。

********************************************************************************

(完)

相關

2024年11月20日 星期三

[研究]PowerShell 替目錄及子目錄所有檔案產生 .SHA256 檔案

[研究]PowerShell 替目錄及子目錄所有檔案產生 .SHA256 檔案

2024-11-20

對本機電腦

$directory = "C:\YourDirectory"
$files = Get-ChildItem -Path $directory -Recurse -File

foreach ($file in $files) {
    $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
    $outputFile = $file.FullName + ".SHA256"
    $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
}


對網路芳鄰其他電腦 (敝人環境好像要先切換到 C: 才正常)

C:
$directory = "\\ComputerName\SharedFolder"
$files = Get-ChildItem -Path $directory -Recurse -File

foreach ($file in $files) {
    $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
    $outputFile = $file.FullName + ".SHA256"
    $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
}


********************************************************************************

對本機電腦,排除已經存在的 .SHA256


$directory = "C:\YourDirectory"  # 指定要處理的目錄
$files = Get-ChildItem -Path $directory -Recurse -File | Where-Object { $_.Extension -ne ".SHA256" }

foreach ($file in $files) {
    $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
    $outputFile = $file.FullName + ".SHA256"
    $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
}

********************************************************************************

對本機電腦,排除已經存在的 .SHA256,處理多個目錄


C:
# 定義多個本機目錄 or 網路共享的目錄 (使用 UNC 路徑)
$directories = @(
    "C:\Directory1",
    "C:\Directory2",
    "C:\Directory3"
    "\\Computer1\SharedFolder1",
    "\\Computer2\SharedFolder2",
    "\\Computer3\SharedFolder3"
)

foreach ($directory in $directories) {
    Write-Host "Processing directory: $directory" -ForegroundColor Green
    try {
        # 獲取目錄中的所有檔案,排除副檔名為 .SHA256 的檔案
        $files = Get-ChildItem -Path $directory -Recurse -File | Where-Object { $_.Extension -ne ".SHA256" }
        
        foreach ($file in $files) {
            # 計算檔案的 SHA256
            $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
            # 生成 .SHA256 檔案的完整路徑
            $outputFile = $file.FullName + ".SHA256"
            # 將哈希值寫入 .SHA256 檔案
            $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
        }
    } catch {
        Write-Host "Failed to process directory: $directory. Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

********************************************************************************

對本機電腦,排除已經存在的 .SHA256,處理多個目錄;失敗時輸出訊息,但仍要繼續對其它檔案進行 SHA256



C:
# 定義多個網路共享的目錄 (使用 UNC 路徑)
$directories = @(
    "C:\Directory1",
    "C:\Directory2",
    "C:\Directory3",
    "\\Computer1\SharedFolder1",
    "\\Computer2\SharedFolder2",
    "\\Computer3\SharedFolder3"
)

# 記錄日誌的檔案路徑
$logFile = "C:\SHA256_ErrorLog.txt"

# 清空日誌檔案
Clear-Content -Path $logFile -ErrorAction SilentlyContinue

foreach ($directory in $directories) {
    Write-Host "Processing directory: $directory" -ForegroundColor Green
    try {
        # 獲取目錄中的所有檔案,排除副檔名為 .SHA256 的檔案
        $files = Get-ChildItem -Path $directory -Recurse -File | Where-Object { $_.Extension -ne ".SHA256" }

        foreach ($file in $files) {
            try {
                # 計算檔案的 SHA256
                $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
                # 生成 .SHA256 檔案的完整路徑
                $outputFile = $file.FullName + ".SHA256"
                # 將哈希值寫入 .SHA256 檔案
                $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
            } catch {
                # 如果某檔案處理失敗,記錄錯誤訊息
                $errorMessage = "Failed to process file: $($file.FullName). Error: $($_.Exception.Message)"
                Write-Host $errorMessage -ForegroundColor Red
                Add-Content -Path $logFile -Value $errorMessage
            }
        }
    } catch {
        # 如果整個目錄無法訪問,記錄錯誤訊息
        $errorMessage = "Failed to process directory: $directory. Error: $($_.Exception.Message)"
        Write-Host $errorMessage -ForegroundColor Red
        Add-Content -Path $logFile -Value $errorMessage
    }
}

********************************************************************************

對本機電腦,排除已經存在的 .SHA256,處理多個目錄;失敗時輸出訊息,但仍要繼續對其它檔案進行 SHA256;如果某檔案 AAA 已經存在 AAA.SHA256,不要再對 AAA 作 SHA256

檔案內容可用"記事本"編輯,儲存成 ANSI格式,不要 UTF8格式

CalcSHA256_ANSI.ps1


C:
# 定義多個網路共享的目錄 (使用 UNC 路徑)
$directories = @(
    "\\Computer1\SharedFolder1",
    "\\Computer2\SharedFolder2",
    "\\Computer3\SharedFolder3"
)

# 記錄日誌的檔案路徑
$logFile = "C:\Temp\SHA256_ErrorLog.txt"

# 確保 Temp 目錄存在
if (-not (Test-Path -Path "C:\Temp")) {
    New-Item -ItemType Directory -Path "C:\Temp" | Out-Null
}

# 清空日誌檔案
Clear-Content -Path $logFile -ErrorAction SilentlyContinue

foreach ($directory in $directories) {
    Write-Host "Processing directory: $directory" -ForegroundColor Green
    try {
        # 獲取目錄中的所有檔案,排除副檔名為 .SHA256 的檔案
        $files = Get-ChildItem -Path $directory -Recurse -File | Where-Object { $_.Extension -ne ".SHA256" }

        foreach ($file in $files) {
            try {
                # 檢查是否存在對應的 .SHA256 檔案
                $outputFile = $file.FullName + ".SHA256"
                if (Test-Path $outputFile) {
                    Write-Host "Skipping file: $($file.FullName) (SHA256 file already exists)" -ForegroundColor Yellow
                    continue
                }

                # 計算檔案的 SHA256
                $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256

                # 將哈希值寫入 .SHA256 檔案
                $hash.Hash | Out-File -FilePath $outputFile -Encoding UTF8
                Write-Host "Generated SHA256 for file: $($file.FullName)" -ForegroundColor Green
            } catch {
                # 如果某檔案處理失敗,記錄錯誤訊息
                $errorMessage = "Failed to process file: $($file.FullName). Error: $($_.Exception.Message)"
                Write-Host $errorMessage -ForegroundColor Red
                Add-Content -Path $logFile -Value $errorMessage
            }
        }
    } catch {
        # 如果整個目錄無法訪問,記錄錯誤訊息
        $errorMessage = "Failed to process directory: $directory. Error: $($_.Exception.Message)"
        Write-Host $errorMessage -ForegroundColor Red
        Add-Content -Path $logFile -Value $errorMessage
    }
}

執行 PowerShell 的 .ps1 方法,先啟動 PowerShell,相對路徑執行方法


.\CalcSHA256_ANSI.ps1

絕對路徑執行方法


C:\Users\YourName\Documents\Scripts\CalcSHA256_ANSI.ps1


如果執行失敗,可用 Get-ExecutionPolicy 指令檢查權限,如果權限不足,可用 Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass 暫時放寬


.PS D:\MySystems> .\CalcSHA256_ANSI.ps1
.\CalcSHA256.ps1 : 因為這個系統上已停用指令碼執行,所以無法載入 D:\MySystems\CalcSHA256.ps1 檔案。如需詳細資訊,請參閱 a
bout_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170。
位於 線路:1 字元:1
+ .\CalcSHA256.ps1
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS D:\MySystems> Get-ExecutionPolicy
Restricted
PS D:\MySystems> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
PS D:\MySystems> Get-ExecutionPolicy
Bypass
PS D:\MySystems>

絕對路徑執行方法

(完)

相關

2024年10月28日 星期一

[研究]某電腦S1的所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

[研究]某電腦S1的所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

2024-10-28

續

[研究]列出 Windows 10預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

[研究]列出 Windows Server 2022預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?https://shaurong.blogspot.com/2024/10/windows-port.html

[研究]Windows Server 2022預設開啟那些 Port (nmap, netstat, tasklist, powershell)
https://shaurong.blogspot.com/2024/10/windows-server-2022-port-nmap-netstat.html

另一台



********************************************************************************

Copilot

這些預設開啟的 port 用途如下:
  • 135/tcp: Microsoft RPC (Remote Procedure Call) 服務,用於遠程過程呼叫。
  • 445/tcp: SMB (Server Message Block) 服務,用於文件和打印共享。
  • 5357/tcp: WSDAPI (Web Services on Devices API),用於設備的網絡通訊。
  • 5985/tcp: WS-Management 服務,用於管理和監控。
  • 47001/tcp: WinRM (Windows Remote Management),用於遠程管理。
  • 49664/tcp-49669/tcp: 這些是未知用途的 port,可能是用於內部服務或應用程式。

Port用途服務名稱檔案名稱程序名稱
135/tcpMicrosoft RPCRPC 服務rpcss.dllsvchost.exe
445/tcpSMB (Server Message Block)文件和打印共享srv2.syssrv2svc.exe
5357/tcpWSDAPI (Web Services on Devices API)WSDAPI 服務wsddsvc.dllwsddsvc.exe
5985/tcpWS-ManagementWindows 管理員工具winrm.exewinrm.exe
47001/tcpWinRM (Windows Remote Management)Windows 遠程管理winrm.exewinrm.exe
49664/tcp-49669/tcp未知用途未知未知未知


********************************************************************************

註:本電腦有安裝  Splunk Universal Forwarder,回傳給 Splunk Server。Universal Forwarder 通常會使用 TCP port 9997(或其他您在配置時設定的 port)來傳送資料到 Splunk Server,而這個 port 是在 Splunk Server 上「接收」資料的埠,不是 Universal Forwarder 的「listen」port。因此,通常情況下,Universal Forwarder 並不會在電腦上開放任何 listen port 來接收來自其他主機的連線。

********************************************************************************

下面軟體名稱 ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?
Process Name : wrapper.exe
Port : 1777
Module Name : arc_winc

ChatGPT 說:

wrapper.exe 通常與 Java Service Wrapper 軟體相關,這是一個通用的應用程式啟動器,主要用於將 Java 應用程式以服務的形式在 Windows 上運行。這裡提到的 Port 1777 和 Module Name : arc_winc 則可能與具體的 Java 應用程式(例如 ArcGIS Server 或某些 GIS 系統服務)相關聯。

以下是相關資訊概覽:

軟體名稱:可能是 ArcGIS Server 或其他基於 Java 的應用程式(具體取決於應用程式使用 Java Service Wrapper 來作為服務啟動器的需求)

用途:Java Service Wrapper 用於管理和監控 Java 應用程式,以確保應用程式可以作為 Windows 服務穩定地運行和重啟。端口 1777 常用於此服務與其他系統組件之間的通信。

服務名稱:通常依應用程式而異(如 ArcGIS Server 中可能命名為 ArcGIS Server),可在服務列表中找到具體名稱。

檔案名稱:wrapper.exe(來自 Java Service Wrapper 程式包)

程序名稱:wrapper.exe

若您需要進一步確認此服務或端口的使用情況,可檢查 wrapper.conf 配置檔案,以查看 arc_winc 模組的具體設定和服務綁定。

註:這是  ArcSight Enterprise Security Manager (ESM) 的用戶端 ArcSight Connector 安裝的。

********************************************************************************

下面軟體名稱 ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

Process Name : dsa.exe
Port : 4118
Module Name : ds_agent

ChatGPT 說:

dsa.exe 通常與 Trend Micro Deep Security Agent (DSA) 相關聯。該代理程式為深度安全管理系統中的一部分,負責在端點設備上提供防護功能,如防病毒、入侵偵測/防護等。以下是詳細資訊:

軟體名稱:Trend Micro Deep Security Agent

用途:提供設備安全防護,包括防病毒、入侵防護、Web 安全、應用程式控制等功能。此代理程式允許 Trend Micro Deep Security 伺服器監控和管理受保護的端點。

服務名稱:ds_agent

檔案名稱:dsa.exe

程序名稱:dsa.exe

Port 4118 用於 Deep Security Agent 與 Deep Security Manager 之間的通訊。此端口協助代理程式向管理伺服器回報狀態、接收政策更新等操作,以確保安全策略的即時性和同步性。

註:本電腦有安裝 趨勢科技 Deep Security,網段會外有開放 
埠:TCP 4118/4119/4120/4122/80/443 雙向

Deep Security 是趨勢科技 Trend Micro 股份有限公司的軟體。為防護動態資料中心的軟體解決方案,具備防毒、封包檢查、防火牆、一致性監控、記錄檔檢查等模組功能。

Deep Security 的 Server 端為 Deep Security Manager (DSM)。

Deep Security 的 Client 端要安裝 Deep Security Agent (DSA)。

********************************************************************************

下面軟體名稱 ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

Process Name : wmcAsset_172_16_xx_xx_Open.exe
Port : 8150
Module Name : Win-Win7

ChatGPT 說:

根據您提供的資訊,wmcAsset_172_16_xx_xx_Open.exe 很可能與 監控軟體、遠端管理、或資產管理相關 的應用程式有關。這類程序通常用於企業環境中的網路設備監控或系統資產管理,且端口 8150 可能用於設備間的數據通信。以下是一般情況下的相關資訊概覽:

軟體名稱:可能是 網路資產管理工具或遠端管理系統(具體軟體名稱可能因不同廠商而異)

用途:監控、管理或進行資產數據收集,通常涉及對本地網路內的資產(如電腦、伺服器、網路設備等)的狀態監控及管理。

服務名稱:wmcAsset 或類似名稱,具體取決於使用的資產管理軟體

檔案名稱:wmcAsset_172_16_xx_xx_Open.exe(檔名中的 IP 表示監控的網路範圍,通常是內部網路 IP 位址)

程序名稱:wmcAsset_172_16_xx_xx_Open.exe

Port 8150 常用於此類遠端管理和數據收集軟體,用以在受控端點和管理伺服器之間建立通信。建議檢查應用程序的相關配置文件或管理控制台,以確認服務的具體用途和設定。

註:本電腦有安裝【神網資訊資產管理(VANS 導入)】

********************************************************************************

註:本電腦有安裝 ArcSight Connector,回傳給另一台安裝 ArcSight Enterprise Security Manager (ESM)的,因在另一網段,開放 Port 844雙向,但不會 Listen Port。

ArcSight ESM 是專為現代SOC 打造的下一代SIEM。 

Server 端是 ArcSight Enterprise Security Manager (ESM)

Client 端是 ArcSight Connector

********************************************************************************

(完)

相關

[研究]列出 Windows 10預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

[研究]列出 Windows Server 2022預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?https://shaurong.blogspot.com/2024/10/windows-port.html

[研究]Windows Server 2022預設開啟那些 Port (nmap, netstat, tasklist, powershell)
https://shaurong.blogspot.com/2024/10/windows-server-2022-port-nmap-netstat.html

[研究]列出 Windows 10預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

[研究]列出 Windows 10預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

2024-10-28

續

[研究]列出 Windows Server 2022預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?https://shaurong.blogspot.com/2024/10/windows-port.html

[研究]Windows Server 2022預設開啟那些 Port (nmap, netstat, tasklist, powershell)
https://shaurong.blogspot.com/2024/10/windows-server-2022-port-nmap-netstat.html

PowerShell

# 合併 Get-NetTCPConnection 和 Get-NetUDPEndpoint 結果
$connections = @()

# 處理 TCP 端口
$tcpConnections = Get-NetTCPConnection | ForEach-Object {
    $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
    $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
    [pscustomobject]@{
        Protocol      = "TCP"
        LocalAddress  = $_.LocalAddress
        LocalPort     = $_.LocalPort
        RemoteAddress = $_.RemoteAddress
        RemotePort    = $_.RemotePort
        State         = $_.State
        ProcessId     = $_.OwningProcess
        ProcessName   = $proc.ProcessName
        FileName      = $proc.Path
        ServiceName   = $service.Name
    }
}

# 處理 UDP 端口
$udpConnections = Get-NetUDPEndpoint | ForEach-Object {
    $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
    $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
    [pscustomobject]@{
        Protocol      = "UDP"
        LocalAddress  = $_.LocalAddress
        LocalPort     = $_.LocalPort
        RemoteAddress = "*"
        RemotePort    = "*"
        State         = "LISTENING"  # UDP 沒有 State,所有端口都類似 LISTENING
        ProcessId     = $_.OwningProcess
        ProcessName   = $proc.ProcessName
        FileName      = $proc.Path
        ServiceName   = $service.Name
    }
}

# 合併 TCP 和 UDP 結果
$connections = $tcpConnections + $udpConnections

# 輸出結果
$connections | Format-Table -AutoSize




結果

Windows PowerShell
Copyright (C) Microsoft Corporation. 著作權所有,並保留一切權利。

請嘗試新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\USER1> cd \
PS C:\> # 合併 Get-NetTCPConnection 和 Get-NetUDPEndpoint 結果
PS C:\> $connections = @()
PS C:\>
PS C:\> # 處理 TCP 端口
PS C:\> $tcpConnections = Get-NetTCPConnection | ForEach-Object {
>>     $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
>>     $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
>>     [pscustomobject]@{
>>         Protocol      = "TCP"
>>         LocalAddress  = $_.LocalAddress
>>         LocalPort     = $_.LocalPort
>>         RemoteAddress = $_.RemoteAddress
>>         RemotePort    = $_.RemotePort
>>         State         = $_.State
>>         ProcessId     = $_.OwningProcess
>>         ProcessName   = $proc.ProcessName
>>         FileName      = $proc.Path
>>         ServiceName   = $service.Name
>>     }
>> }
PS C:\>
PS C:\> # 處理 UDP 端口
PS C:\> $udpConnections = Get-NetUDPEndpoint | ForEach-Object {
>>     $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
>>     $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
>>     [pscustomobject]@{
>>         Protocol      = "UDP"
>>         LocalAddress  = $_.LocalAddress
>>         LocalPort     = $_.LocalPort
>>         RemoteAddress = "*"
>>         RemotePort    = "*"
>>         State         = "LISTENING"  # UDP 沒有 State,所有端口都類似 LISTENING
>>         ProcessId     = $_.OwningProcess
>>         ProcessName   = $proc.ProcessName
>>         FileName      = $proc.Path
>>         ServiceName   = $service.Name
>>     }
>> }
PS C:\>
PS C:\> # 合併 TCP 和 UDP 結果
PS C:\> $connections = $tcpConnections + $udpConnections
PS C:\>
PS C:\> # 輸出結果
PS C:\> $connections | Format-Table -AutoSize

Protocol LocalAddress               LocalPort RemoteAddress   RemotePort       State ProcessId ProcessName        FileName
-------- ------------               --------- -------------   ----------       ----- --------- -----------        --------
TCP      ::                             49669 ::                       0      Listen       636 services
TCP      ::                             49668 ::                       0      Listen      2256 spoolsv
TCP      ::                             49667 ::                       0      Listen      1232 svchost
TCP      ::                             49666 ::                       0      Listen      1132 svchost
TCP      ::                             49665 ::                       0      Listen       496 wininit
TCP      ::                             49664 ::                       0      Listen       652 lsass
TCP      ::                              7680 ::                       0      Listen      9820 svchost
TCP      ::                               445 ::                       0      Listen         4 System
TCP      ::                               135 ::                       0      Listen       868 svchost
TCP      0.0.0.0                        50257 0.0.0.0                  0       Bound      3364 svchost
TCP      0.0.0.0                        50256 0.0.0.0                  0       Bound      3524 backgroundTaskHost C:\Windows\system32\backg...
TCP      0.0.0.0                        50255 0.0.0.0                  0       Bound      3524 backgroundTaskHost C:\Windows\system32\backg...
TCP      0.0.0.0                        50252 0.0.0.0                  0       Bound      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        50250 0.0.0.0                  0       Bound      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        50249 0.0.0.0                  0       Bound      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        50248 0.0.0.0                  0       Bound      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        50238 0.0.0.0                  0       Bound      3364 svchost
TCP      0.0.0.0                        50225 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50222 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50221 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50220 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50213 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50211 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50210 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50209 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50191 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50190 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50187 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50186 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50171 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50166 0.0.0.0                  0       Bound     10388 msedge             C:\Program Files (x86)\Mi...
TCP      0.0.0.0                        50163 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50162 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50152 0.0.0.0                  0       Bound      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      0.0.0.0                        50149 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50148 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50142 0.0.0.0                  0       Bound      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      0.0.0.0                        50140 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50138 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50137 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50136 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50134 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50133 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50129 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50128 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50122 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50121 0.0.0.0                  0       Bound      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      0.0.0.0                        50118 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50117 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50116 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50115 0.0.0.0                  0       Bound      9820 svchost
TCP      0.0.0.0                        50114 0.0.0.0                  0       Bound      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      0.0.0.0                        50074 0.0.0.0                  0       Bound      4280 explorer           C:\Windows\Explorer.EXE
TCP      0.0.0.0                        49865 0.0.0.0                  0       Bound      8584 svchost
TCP      0.0.0.0                        49859 0.0.0.0                  0       Bound      7304 msedgewebview2     C:\Program Files (x86)\Mi...
TCP      0.0.0.0                        49844 0.0.0.0                  0       Bound      3216 svchost
TCP      0.0.0.0                        49729 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49728 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49727 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49726 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49725 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49724 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49723 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49722 0.0.0.0                  0       Bound      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49706 0.0.0.0                  0       Bound      6420 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49705 0.0.0.0                  0       Bound      6420 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49704 0.0.0.0                  0       Bound      7304 msedgewebview2     C:\Program Files (x86)\Mi...
TCP      0.0.0.0                        49696 0.0.0.0                  0       Bound      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      0.0.0.0                        49688 0.0.0.0                  0       Bound      3216 svchost
TCP      192.168.128.130                50257 20.190.166.131         443 Established      3364 svchost
TCP      192.168.128.130                50256 152.195.38.76           80 Established      3524 backgroundTaskHost C:\Windows\system32\backg...
TCP      192.168.128.130                50255 23.220.198.33          443 Established      3524 backgroundTaskHost C:\Windows\system32\backg...
TCP      192.168.128.130                50252 117.18.232.200         443 Established      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                50250 210.71.227.208         443 Established      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                50249 13.107.6.254           443 Established      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                50248 204.79.197.222         443 Established      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                50244 13.107.246.73          443    TimeWait         0 Idle
TCP      192.168.128.130                50238 20.190.166.131         443 Established      3364 svchost
TCP      192.168.128.130                50234 20.189.173.24          443    TimeWait         0 Idle
TCP      192.168.128.130                50225 13.74.187.43           443 Established      9820 svchost
TCP      192.168.128.130                50224 210.71.227.162          80    TimeWait         0 Idle
TCP      192.168.128.130                50223 210.71.227.114          80    TimeWait         0 Idle
TCP      192.168.128.130                50222 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50221 203.69.138.206          80 Established      9820 svchost
TCP      192.168.128.130                50220 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50219 146.75.114.172          80    TimeWait         0 Idle
TCP      192.168.128.130                50213 13.74.187.43           443 Established      9820 svchost
TCP      192.168.128.130                50212 146.75.114.172          80    TimeWait         0 Idle
TCP      192.168.128.130                50211 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50210 151.101.230.172         80 Established      9820 svchost
TCP      192.168.128.130                50209 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50207 52.165.164.15          443    TimeWait         0 Idle
TCP      192.168.128.130                50206 43.159.69.81            80    TimeWait         0 Idle
TCP      192.168.128.130                50205 210.71.227.97           80    TimeWait         0 Idle
TCP      192.168.128.130                50204 210.71.227.184          80    TimeWait         0 Idle
TCP      192.168.128.130                50202 43.159.69.106           80    TimeWait         0 Idle
TCP      192.168.128.130                50201 52.165.164.15          443    TimeWait         0 Idle
TCP      192.168.128.130                50200 210.71.227.162          80    TimeWait         0 Idle
TCP      192.168.128.130                50199 210.71.227.106          80    TimeWait         0 Idle
TCP      192.168.128.130                50198 43.159.69.111           80    TimeWait         0 Idle
TCP      192.168.128.130                50197 43.175.149.11           80    TimeWait         0 Idle
TCP      192.168.128.130                50196 210.71.227.186          80    TimeWait         0 Idle
TCP      192.168.128.130                50195 210.71.227.97           80    TimeWait         0 Idle
TCP      192.168.128.130                50194 43.159.69.81            80    TimeWait         0 Idle
TCP      192.168.128.130                50193 43.159.69.56            80    TimeWait         0 Idle
TCP      192.168.128.130                50191 151.101.230.172         80 Established      9820 svchost
TCP      192.168.128.130                50190 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50189 210.71.227.104          80    TimeWait         0 Idle
TCP      192.168.128.130                50188 210.71.227.184          80    TimeWait         0 Idle
TCP      192.168.128.130                50187 151.101.230.172         80 Established      9820 svchost
TCP      192.168.128.130                50186 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50185 210.71.227.186          80    TimeWait         0 Idle
TCP      192.168.128.130                50184 210.71.227.106          80    TimeWait         0 Idle
TCP      192.168.128.130                50183 52.182.143.211         443    TimeWait         0 Idle
TCP      192.168.128.130                50182 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50181 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50180 43.175.149.12           80    TimeWait         0 Idle
TCP      192.168.128.130                50179 43.159.69.81            80    TimeWait         0 Idle
TCP      192.168.128.130                50177 43.159.69.81            80    TimeWait         0 Idle
TCP      192.168.128.130                50176 146.75.114.172          80    TimeWait         0 Idle
TCP      192.168.128.130                50175 52.175.245.232         443    TimeWait         0 Idle
TCP      192.168.128.130                50174 210.71.227.186          80    TimeWait         0 Idle
TCP      192.168.128.130                50173 210.71.227.114          80    TimeWait         0 Idle
TCP      192.168.128.130                50172 203.77.190.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50171 23.47.5.117            443 Established      9820 svchost
TCP      192.168.128.130                50169 52.175.245.232         443    TimeWait         0 Idle
TCP      192.168.128.130                50167 52.175.245.232         443    TimeWait         0 Idle
TCP      192.168.128.130                50166 204.79.197.239         443 Established     10388 msedge             C:\Program Files (x86)\Mi...
TCP      192.168.128.130                50165 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50164 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50163 203.69.138.206          80 Established      9820 svchost
TCP      192.168.128.130                50162 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50152 20.189.173.6           443 Established      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      192.168.128.130                50151 210.71.227.146          80    TimeWait         0 Idle
TCP      192.168.128.130                50150 210.71.227.114          80    TimeWait         0 Idle
TCP      192.168.128.130                50149 151.101.110.172         80 Established      9820 svchost
TCP      192.168.128.130                50148 104.116.17.179         443 Established      9820 svchost
TCP      192.168.128.130                50146 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50144 43.159.69.56            80    TimeWait         0 Idle
TCP      192.168.128.130                50143 111.119.27.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50142 203.69.81.58            80 Established      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      192.168.128.130                50141 204.79.197.239         443    TimeWait         0 Idle
TCP      192.168.128.130                50140 40.69.76.172           443 Established      9820 svchost
TCP      192.168.128.130                50139 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50138 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50137 146.75.114.172          80 Established      9820 svchost
TCP      192.168.128.130                50136 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50135 111.119.27.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50134 146.75.114.172          80 Established      9820 svchost
TCP      192.168.128.130                50133 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50132 210.71.227.186          80    TimeWait         0 Idle
TCP      192.168.128.130                50131 210.71.227.104          80    TimeWait         0 Idle
TCP      192.168.128.130                50129 146.75.114.172          80 Established      9820 svchost
TCP      192.168.128.130                50128 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50127 20.3.187.198           443    TimeWait         0 Idle
TCP      192.168.128.130                50126 40.69.76.172           443    TimeWait         0 Idle
TCP      192.168.128.130                50125 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50122 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50121 152.195.38.76           80 Established      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      192.168.128.130                50120 210.71.227.146          80    TimeWait         0 Idle
TCP      192.168.128.130                50119 210.71.227.104          80    TimeWait         0 Idle
TCP      192.168.128.130                50118 146.75.114.172          80 Established      9820 svchost
TCP      192.168.128.130                50117 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50116 23.47.5.179            443 Established      9820 svchost
TCP      192.168.128.130                50115 23.47.5.117            443 Established      9820 svchost
TCP      192.168.128.130                50114 13.107.42.16           443 Established      3796 SkypeApp           C:\Program Files\WindowsA...
TCP      192.168.128.130                50113 20.3.187.198           443    TimeWait         0 Idle
TCP      192.168.128.130                50104 43.175.149.12           80    TimeWait         0 Idle
TCP      192.168.128.130                50102 43.159.69.110           80    TimeWait         0 Idle
TCP      192.168.128.130                50101 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50100 203.77.190.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50095 20.190.166.131         443    TimeWait         0 Idle
TCP      192.168.128.130                50094 20.190.166.131         443    TimeWait         0 Idle
TCP      192.168.128.130                50093 20.190.166.131         443    TimeWait         0 Idle
TCP      192.168.128.130                50092 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50090 146.75.114.172          80    TimeWait         0 Idle
TCP      192.168.128.130                50087 210.71.227.106          80    TimeWait         0 Idle
TCP      192.168.128.130                50086 210.71.227.114          80    TimeWait         0 Idle
TCP      192.168.128.130                50084 210.71.227.106          80    TimeWait         0 Idle
TCP      192.168.128.130                50083 210.71.227.186          80    TimeWait         0 Idle
TCP      192.168.128.130                50082 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50080 151.101.230.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50078 203.77.191.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50077 203.77.190.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50076 210.71.227.162          80    TimeWait         0 Idle
TCP      192.168.128.130                50075 210.71.227.106          80    TimeWait         0 Idle
TCP      192.168.128.130                50074 210.61.249.24          443 Established      4280 explorer           C:\Windows\Explorer.EXE
TCP      192.168.128.130                50059 203.77.191.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50057 210.71.227.104          80    TimeWait         0 Idle
TCP      192.168.128.130                50056 210.71.227.146          80    TimeWait         0 Idle
TCP      192.168.128.130                50055 203.69.138.206          80    TimeWait         0 Idle
TCP      192.168.128.130                50051 203.77.191.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50045 20.190.166.131         443    TimeWait         0 Idle
TCP      192.168.128.130                50042 13.74.187.43           443    TimeWait         0 Idle
TCP      192.168.128.130                50038 111.119.27.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50026 111.119.27.0            80    TimeWait         0 Idle
TCP      192.168.128.130                50017 203.69.138.206          80    TimeWait         0 Idle
TCP      192.168.128.130                50013 203.69.138.206          80    TimeWait         0 Idle
TCP      192.168.128.130                50009 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50004 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                50001 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49999 204.79.197.239         443    TimeWait         0 Idle
TCP      192.168.128.130                49996 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49981 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49977 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49974 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49971 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49968 151.101.110.172         80    TimeWait         0 Idle
TCP      192.168.128.130                49865 151.101.230.172         80 Established      8584 svchost
TCP      192.168.128.130                49844 20.90.153.243          443 Established      3216 svchost
TCP      192.168.128.130                49729 152.195.38.76           80   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49728 152.195.38.76           80   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49727 210.71.227.200         443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49726 210.71.227.200         443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49725 210.71.227.200         443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49724 210.71.227.200         443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49723 203.69.138.32          443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49722 104.116.16.155         443   CloseWait      9036 LockApp            C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49696 13.107.246.254         443   CloseWait      5968 SearchApp          C:\Windows\SystemApps\Mic...
TCP      192.168.128.130                49688 20.90.156.32           443 Established      3216 svchost
TCP      0.0.0.0                        49669 0.0.0.0                  0      Listen       636 services
TCP      0.0.0.0                        49668 0.0.0.0                  0      Listen      2256 spoolsv
TCP      0.0.0.0                        49667 0.0.0.0                  0      Listen      1232 svchost
TCP      0.0.0.0                        49666 0.0.0.0                  0      Listen      1132 svchost
TCP      0.0.0.0                        49665 0.0.0.0                  0      Listen       496 wininit
TCP      0.0.0.0                        49664 0.0.0.0                  0      Listen       652 lsass
TCP      0.0.0.0                         5040 0.0.0.0                  0      Listen      3692 svchost
TCP      192.168.128.130                  139 0.0.0.0                  0      Listen         4 System
TCP      0.0.0.0                          135 0.0.0.0                  0      Listen       868 svchost
UDP      ::                             55881 *                        *   LISTENING      3796 SkypeApp           C:\Program Files\WindowsA...
UDP      ::1                            54575 *                        *   LISTENING      8128 svchost
UDP      fe80::9618:b113:b69:61e8%5     54574 *                        *   LISTENING      8128 svchost
UDP      ::                              5355 *                        *   LISTENING      1996 svchost
UDP      ::                              5353 *                        *   LISTENING      1996 svchost
UDP      fe80::9618:b113:b69:61e8%5      1900 *                        *   LISTENING      8128 svchost
UDP      ::1                             1900 *                        *   LISTENING      8128 svchost
UDP      127.0.0.1                      60120 *                        *   LISTENING      3388 svchost
UDP      127.0.0.1                      54577 *                        *   LISTENING      8128 svchost
UDP      192.168.128.130                54576 *                        *   LISTENING      8128 svchost
UDP      0.0.0.0                         5355 *                        *   LISTENING      1996 svchost
UDP      0.0.0.0                         5353 *                        *   LISTENING      1996 svchost
UDP      0.0.0.0                         5050 *                        *   LISTENING      3692 svchost
UDP      192.168.128.130                 1900 *                        *   LISTENING      8128 svchost
UDP      127.0.0.1                       1900 *                        *   LISTENING      8128 svchost
UDP      192.168.128.130                  138 *                        *   LISTENING         4 System
UDP      192.168.128.130                  137 *                        *   LISTENING         4 System


PS C:\>

********************************************************************************
列出 Windows 10預設所有開放的下面 Port 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?
TCP/135
TCP/445
TCP/7680
TCP/49664
TCP/49665
TCP/49666
TCP/49667
TCP/49668
TCP/49669

ChatGPT 說:

以下是 Windows 10 預設開放的指定 TCP Port 的用途、服務名稱、檔案名稱與程序名稱概覽:

TCP/135

用途:DCE/RPC (Distributed Computing Environment / Remote Procedure Calls)
服務名稱:RPC Endpoint Mapper
檔案名稱:svchost.exe
程序名稱:RpcSs
TCP/445

用途:SMB (Server Message Block),用於檔案與列印資源共享
服務名稱:Microsoft-DS (Microsoft Directory Services)
檔案名稱:System
程序名稱:SMB Server
TCP/7680

用途:Delivery Optimization,主要用於 Windows 更新的點對點分發
服務名稱:Delivery Optimization
檔案名稱:svchost.exe
程序名稱:DoSvc
TCP/49664 - TCP/49669(包含連續的動態埠範圍)

用途:RPC 動態埠,這些埠號會隨不同的應用程序需求動態指派,用於支援 RPC-based 的通訊(如 DCOM、WMI)
服務名稱:RPC 動態端點(Dynamic RPC Ports)
檔案名稱:svchost.exe
程序名稱:會依據需求不同,包含 RpcSs 等使用 RPC 的服務
這些端口可能會依據系統狀態或已安裝的應用程序有所變動或額外開啟。一般來說,若需要更高的安全性,可考慮關閉部分服務或僅允許特定應用程序使用。

********************************************************************************
TCP/5040 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

ChatGPT 說:

在 Windows 10 中,TCP/5040 預設開放端口的詳細資訊如下:

用途:TCP/5040 主要用於 Windows 的 Microsoft Direct Push Notification Service (WNS),該服務提供即時推送通知功能,例如應用程式通知和系統通知。
服務名稱:Microsoft-Windows-User-PushNotification
檔案名稱:svchost.exe
程序名稱:WpnService
此服務允許 Windows 應用程式和系統接收通知。若不需要推送通知,可考慮停用該服務來減少網路風險。

********************************************************************************

PowerShell 僅列出 Listen 或 Listening 狀態的 TCP 和 UDP 連接

# 合併 Listen 或 Listening 狀態的 Get-NetTCPConnection 和 Get-NetUDPEndpoint 結果
$connections = @()

# 處理 TCP 端口,只列出 State 為 Listen 的
$tcpConnections = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | ForEach-Object {
    $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
    $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
    [pscustomobject]@{
        Protocol      = "TCP"
        LocalAddress  = $_.LocalAddress
        LocalPort     = $_.LocalPort
        RemoteAddress = $_.RemoteAddress
        RemotePort    = $_.RemotePort
        State         = $_.State
        ProcessId     = $_.OwningProcess
        ProcessName   = $proc.ProcessName
        FileName      = $proc.Path
        ServiceName   = $service.Name
    }
}

# 處理 UDP 端口,所有端口都類似 LISTENING 狀態
$udpConnections = Get-NetUDPEndpoint | ForEach-Object {
    $proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
    $service = Get-Service | Where-Object { $_.Id -eq $_.OwningProcess }
    [pscustomobject]@{
        Protocol      = "UDP"
        LocalAddress  = $_.LocalAddress
        LocalPort     = $_.LocalPort
        RemoteAddress = "*"
        RemotePort    = "*"
        State         = "LISTENING"
        ProcessId     = $_.OwningProcess
        ProcessName   = $proc.ProcessName
        FileName      = $proc.Path
        ServiceName   = $service.Name
    }
}

# 合併 TCP 和 UDP 結果
$connections = $tcpConnections + $udpConnections

# 輸出結果
$connections | Format-Table -AutoSize

結果

Protocol LocalAddress               LocalPort RemoteAddress RemotePort     State ProcessId ProcessName FileName ServiceName
-------- ------------               --------- ------------- ----------     ----- --------- ----------- -------- -----------
TCP      ::                             49669 ::                     0    Listen       636 services             {AarSvc_20e00, AJRouter, AL...
TCP      ::                             49668 ::                     0    Listen      2256 spoolsv              {AarSvc_20e00, AJRouter, AL...
TCP      ::                             49667 ::                     0    Listen      1232 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      ::                             49666 ::                     0    Listen      1132 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      ::                             49665 ::                     0    Listen       496 wininit              {AarSvc_20e00, AJRouter, AL...
TCP      ::                             49664 ::                     0    Listen       652 lsass                {AarSvc_20e00, AJRouter, AL...
TCP      ::                              7680 ::                     0    Listen      9820 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      ::                               445 ::                     0    Listen         4 System               {AarSvc_20e00, AJRouter, AL...
TCP      ::                               135 ::                     0    Listen       868 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49669 0.0.0.0                0    Listen       636 services             {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49668 0.0.0.0                0    Listen      2256 spoolsv              {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49667 0.0.0.0                0    Listen      1232 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49666 0.0.0.0                0    Listen      1132 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49665 0.0.0.0                0    Listen       496 wininit              {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                        49664 0.0.0.0                0    Listen       652 lsass                {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                         5040 0.0.0.0                0    Listen      3692 svchost              {AarSvc_20e00, AJRouter, AL...
TCP      192.168.128.130                  139 0.0.0.0                0    Listen         4 System               {AarSvc_20e00, AJRouter, AL...
TCP      0.0.0.0                          135 0.0.0.0                0    Listen       868 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      ::1                            54575 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      fe80::9618:b113:b69:61e8%5     54574 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      ::                              5355 *                      * LISTENING      1996 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      ::                              5353 *                      * LISTENING      1996 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      fe80::9618:b113:b69:61e8%5      1900 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      ::1                             1900 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      127.0.0.1                      60120 *                      * LISTENING      3388 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      127.0.0.1                      54577 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      192.168.128.130                54576 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      0.0.0.0                         5355 *                      * LISTENING      1996 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      0.0.0.0                         5353 *                      * LISTENING      1996 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      0.0.0.0                         5050 *                      * LISTENING      3692 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      192.168.128.130                 1900 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      127.0.0.1                       1900 *                      * LISTENING      8128 svchost              {AarSvc_20e00, AJRouter, AL...
UDP      192.168.128.130                  138 *                      * LISTENING         4 System               {AarSvc_20e00, AJRouter, AL...
UDP      192.168.128.130                  137 *                      * LISTENING         4 System               {AarSvc_20e00, AJRouter, AL...


PS C:\>

(完)

相關

[研究]列出 Windows 10預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?

[研究]列出 Windows Server 2022預設所有開放的 Port ? 用途 ? 服務名稱 ? 檔案名稱 ? 程序名稱 ?https://shaurong.blogspot.com/2024/10/windows-port.html

[研究]Windows Server 2022預設開啟那些 Port (nmap, netstat, tasklist, powershell)
https://shaurong.blogspot.com/2024/10/windows-server-2022-port-nmap-netstat.html