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

时间:2017-11-28 14:19:51

标签: 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...)

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

简而言之,我希望看到像

这样的内容
> CONNECT www.example.com:443 HTTP/1.1

* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256

1 个答案:

答案 0 :(得分:1)

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

对于这个例子,我将使用https://jsonplaceholder.typicode.com/posts,这是一个很好的测试REST端点,用于摆弄这样的事情。

首先,我将建立与该网站的连接并将其存储在变量Invoke-RestMethod中。

$response

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

$response = Invoke-WebRequest -uri https://jsonplaceholder.typicode.com/posts 

我们也可以在$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 属性的前25行左右获得一些好的信息,如此处所示。 RawContent原始,因此我在新行上应用拆分,然后使用RawContent所示的数组索引来选择前21行。

[0..20]

我同意能够获得这些信息也很好。我将在github.com/PowerShell repo上打开一个问题,看看我们是否可以在将来添加这样的内容,并将链接添加到此答案中。