sourcecode

SSL/TLS 보안 채널에서 Powershell Invoke-WebRequest 실패

copyscript 2023. 4. 19. 23:23
반응형

SSL/TLS 보안 채널에서 Powershell Invoke-WebRequest 실패

이 powershell 명령어를 실행하려고 합니다.

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

이 에러가 표시됩니다."Invoke-WebRequest : 요청이 중단되었습니다. SSL/TLS 시큐어 채널을 작성할 수 없습니다." https 요청은 정상적으로 동작하고 있는 것 같습니다("https://google.com") 그러나 이 요청은 해당되지 않습니다.이 기능을 활성화하거나 다른 powershell 명령을 사용하여 페이지 내용을 읽으려면 어떻게 해야 합니까?

이것을 사용해 보다

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

표를 훔치려는 뻔뻔한 시도로SecurityProtocol는 입니다.Enum와 함께[Flags]기여하다.다음과 같이 할 수 있습니다.

[Net.ServicePointManager]::SecurityProtocol = 
  [Net.SecurityProtocolType]::Tls12 -bor `
  [Net.SecurityProtocolType]::Tls11 -bor `
  [Net.SecurityProtocolType]::Tls

또는 PowerShell이므로 문자열을 구문 분석할 수 있습니다.

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

그러면 기술적으로 TLS 버전을 알 필요가 없습니다.

이 답을 읽은 후 작성한 스크립트에서 복사하여 붙여 넣었습니다. 사용할 수 있는 모든 프로토콜을 검색하여 사용할 수 있는 프로토콜을 찾고 싶지 않았기 때문입니다.물론 네가 원한다면 그렇게 할 수 있어.

마지막 메모 - PowerShell 프로파일에 원본(SO 편집 제외) 스테이트먼트가 있기 때문에 지금 시작하는 모든 세션에 적용됩니다.아직 실패하는 사이트도 있기 때문에 완전히 잘못된 것은 아니지만, 문제의 메시지를 보는 빈도는 훨씬 낮습니다.

이 에러의 원인은 디폴트로 Powershell이 TLS 1.0을 사용하여 웹 사이트에 접속하기 때문입니다.단, 웹 사이트 보안에는 TLS 1.2가 필요합니다.아래 명령 중 하나를 실행하여 이 동작을 변경하여 모든 프로토콜을 사용할 수 있습니다.단일 프로토콜을 지정할 수도 있습니다.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Ssl3
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"

다음 명령을 실행한 후 명령을 실행하십시오.

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

효과가 있을 거야

위의 어느 것도 기능하지 않는 경우는, 특히 TLS보다 낮은 버전만을 시험해 보는 것도 좋을지도 모릅니다.다음 두 가지를 모두 시도해 봤지만 문제가 해결되지 않은 것 같습니다.

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls

마지막으로 TLS 1.0을 대상으로 했을 때(특히 코드에서 1.1과 1.2를 삭제)에만 동작했습니다.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls

리모트 서버(서드파티에 의해 TLS 1.2로 「확인」되어 있던 것)는 문제가 없는 것 같습니다만, TLS 1.2에서는 문제가 없는 것 같습니다.

이게 도움이 됐으면 좋겠네요.

나한텐 효과가 있어...

if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
    {
    $certCallback = @"
        using System;
        using System.Net;
        using System.Net.Security;
        using System.Security.Cryptography.X509Certificates;
        public class ServerCertificateValidationCallback
        {
            public static void Ignore()
            {
                if(ServicePointManager.ServerCertificateValidationCallback ==null)
                {
                    ServicePointManager.ServerCertificateValidationCallback += 
                        delegate
                        (
                            Object obj, 
                            X509Certificate certificate, 
                            X509Chain chain, 
                            SslPolicyErrors errors
                        )
                        {
                            return true;
                        };
                }
            }
        }
    "@
        Add-Type $certCallback
     }
    [ServerCertificateValidationCallback]::Ignore()

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

먼저 SHEL을 전환해 주세요.

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
RUN Invoke-WebRequest -UseBasicParsing -Uri  'https://github.com/git-for-windows/git/releases/download/v2.25.1.windows.1/Git-2.25.1-64-bit.exe' -OutFile 'outfile.exe'

그 이유를 파악하지 못하고 있지만, 재설치하고 있습니다..pfx증명서(현재 사용자와 로컬머신 모두)가 유효합니다.

언급URL : https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel

반응형