Powershellで証明書チェックを無視してサイトにアクセスさせてみた

Powershell Powershell

Powershellの [Invoke-WebRequest] コマンドを実行する際に、証明書を無視してサイトにアクセスさせてみました。

広告

自己署名証明書などの証明書を適用しているサイトにはアクセスできない

自己署名証明書などの証明書を適用しているサイトにはアクセスできません。
以下のようにエラーが出ます。

PS C:\> Invoke-WebRequest -Uri "https://hogehoge.com"
Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
At line:1 char:1
+ Invoke-WebRequest -Uri "https://hogehoge.com"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\>
広告

オプション[-SkipCertificateCheck]を利用する

オプション [-SkipCertificateCheck] を利用することで証明書のチェックを無視してアクセスすることができます。

Invoke-WebRequest -Uri "https://hogehoge.com" -SkipCertificateCheck

ただし、Powershellは最新版のバージョンにする必要があります。
Microsoft公式から最新版のPowershellをインストールしてください。
今回は執筆時点の最新版であるPowershell 7.4 を利用しています。

Windows への PowerShell のインストール - PowerShell
Windows への PowerShell のインストールに関する情報

[参考] Powershell 5.1では実行できない

Powershell 5.1では オプション[-SkipCertificateCheck] は利用できませんでした。

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.5328
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.5328
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\>
PS C:\> Invoke-WebRequest -Uri "https://hogehoge.com" -SkipCertificateCheck
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At line:1 char:47
+ Invoke-WebRequest -Uri "https://hogehoge.com" -SkipCertificateCheck
+                                               ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\>

アクセス結果を確認する

自己署名証明書を適用させたサイトを用意して、アクセスできるかどうかを確認します。

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\>
PS C:\>
PS C:\> Invoke-WebRequest -Uri "https://hogehoge.com" -SkipCertificateCheck

StatusCode        : 200
StatusDescription : OK
Content           : <html>
                        <head>
                        <title>
                                Test-HTML-Title!
                        </title>
                        </head>

                        <body>
                                Test-Body!
                        </body>
                    </html>
RawContent        : HTTP/1.1 200 OK
                    Accept-Ranges: bytes
                    ETag: "7a5bb4ab051da1:0"
                    Server: Microsoft-IIS/10.0
                    Date: Sun, 28 Jan 2024 06:35:57 GMT
                    Content-Type: text/html
                    Last-Modified: Sun, 28 Jan 2024 06:07:37 GMT…
Headers           : {[Accept-Ranges, System.String[]], [ETag, System.String[]], [Server, System.String[]], [Date,
                    System.String[]]…}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 110
RelationLink      : {}

PS C:\>

自己署名証明書のサイトにアクセスできました。

オプションを外してみるとエラーとなります。

PS C:\> Invoke-WebRequest -Uri "https://hogehoge.com"
Invoke-WebRequest: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
PS C:\>

コメント

タイトルとURLをコピーしました