如何在 Invoke-WebRequest 中显示 TLS 握手信息和 CONNECT 请求

Fra*_* Yu 5 powershell ssl http-proxy

当我通过 HTTPS 和/或使用 HTTP 代理访问站点时,Linux 中的 cURL 提供-v/--verbose标志来显示CONNECT对代理的请求,以及 SSL/TLS 握手过程(包括证书),例如

* Rebuilt URL to: https://www.example.com/
*   Trying 192.168.2.1...
* Connected to my-proxy.local (192.168.2.1) port 8080 (#0)
* Establish HTTP proxy tunnel to www.example.com:443
> CONNECT www.example.com:443 HTTP/1.1
> Host: www.example.com:443
> User-Agent: curl/7.47.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
* Proxy replied OK to CONNECT request
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 597 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
*        server certificate verification OK
*        server certificate status verification SKIPPED
*        common name: www.example.org (matched)
*        server certificate expiration date OK
*        server certificate activation date OK
*        certificate public key: RSA
*        certificate version: #3
*        subject: C=US,ST=California,L=Los Angeles,O=Internet Corporation for Assigned Names and Numbers,OU=Technology,CN=www.example.org
*        start date: Tue, 03 Nov 2015 00:00:00 GMT
*        expire date: Wed, 28 Nov 2018 12:00:00 GMT
*        issuer: C=US,O=DigiCert Inc,OU=www.digicert.com,CN=DigiCert SHA2 High Assurance Server CA
*        compression: NULL
* ALPN, server accepted to use http/1.1
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: max-age=604800
< Content-Type: text/html
< Date: Mon, 27 Nov 2017 23:08:55 GMT
< Etag: "359670651+gzip+ident"
< Expires: Mon, 04 Dec 2017 23:08:55 GMT
< Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
< Server: ECS (ord/4C84)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1270
<
< (body...)
Run Code Online (Sandbox Code Playgroud)

无论如何,在使用时是否可以获得类似的信息Invoke-WebRequest?或者我应该使用另一个 CmdLet?我试过-Debug-Verbose,两者都没有显示任何信息。甚至原始内容也只包含代理后的实际请求,即在上面的示例中GET / HTTP/1.1

简而言之,我想看到类似这条线的东西

> CONNECT www.example.com:443 HTTP/1.1
Run Code Online (Sandbox Code Playgroud)

* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
Run Code Online (Sandbox Code Playgroud)

Fox*_*loy 4

.RawContent您可以从的属性中获取部分此类信息Invoke-WebRequest。不幸的是,如果您选择Invoke-RestMethod,PowerShell 基本上会丢弃您感兴趣的所有 HTTP 信息。

对于此示例,我将使用https://jsonplaceholder.typicode.com/posts,这是一个很好的测试 REST 端点,用于摆弄此类内容。

首先,我将连接到该站点并将其存储在变量中$response

$response = Invoke-WebRequest -uri https://jsonplaceholder.typicode.com/posts 
Run Code Online (Sandbox Code Playgroud)

现在我可以询问并提取一些有用的字段来获取您正在寻找的一些信息。

$response.BaseResponse 


IsMutuallyAuthenticated : False
Cookies                 : {__cfduid=d84018de2d621df9d53eb52d97cd33a651511881763}
Headers                 : {Transfer-Encoding, Connection, Vary, Access-Control-Allow-Credentials...}
SupportsHeaders         : True
ContentLength           : -1
ContentEncoding         : 
ContentType             : application/json; charset=utf-8
CharacterSet            : utf-8
Server                  : cloudflare-nginx
LastModified            : 11/28/2017 10:17:27 AM
StatusCode              : OK
StatusDescription       : OK
ProtocolVersion         : 1.1
ResponseUri             : https://jsonplaceholder.typicode.com/posts
Method                  : GET
IsFromCache             : False
Run Code Online (Sandbox Code Playgroud)

我们还可以在该属性的前 25 行左右获得一些有用的信息RawContent,如下所示。 RawContent是,嗯,raw,所以我在换行符上应用分割,然后使用数组索引(如 所示)[0..20]来选择前 21 行。

$response.RawContent.Split("`n")[0..20]
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Pragma: no-cache
X-Content-Type-Options: nosniff
CF-Cache-Status: HIT
CF-RAY: 3c4e3f804f9d82f7-ATL
Cache-Control: public, max-age=14400
Content-Type: application/json; charset=utf-8
Date: Tue, 28 Nov 2017 15:09:23 GMT
Expires: Tue, 28 Nov 2017 19:09:23 GMT
ETag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
Set-Cookie: __cfduid=d84018de2d621df9d53eb52d97cd33a651511881763; expires=Wed, 28-Nov-18 15:09:23 GMT; path=/; domain=.typicode.com; HttpOnly
Server: cloudflare-nginx
Via: 1.1 vegur
X-Powered-By: Express
Run Code Online (Sandbox Code Playgroud)

我同意能够取回这些信息也很好。我将在 github.com/PowerShell 存储库上打开一个问题,看看将来是否可以添加类似的内容,并且链接将添加到此答案中。