如何查看Boto3 HTTPS请求字符串

35

我已经能够查看botocore发送的PreparedRequest的属性,但是我想知道如何查看发送到AWS的确切请求字符串。我需要确切的请求字符串才能将其与我正在测试AWS调用的另一个应用程序进行比较。

2个回答

75

您还可以在boto3中启用调试日志记录。 这将记录所有请求和响应以及许多其他内容。 启用它有点晦涩:

import boto3
boto3.set_stream_logger(name='botocore')

您需要指定botocore作为记录名称的原因是所有实际请求和响应都发生在botocore层。


我之前尝试过使用代理。问题在于它不会记录发送到服务器的实际完整请求,只记录用于计算签名、服务等组件。 - zachhilbert
2
当我调试dynamodb请求时,我使用这种技术取得了成功;我能够在日志中找到整个请求体以及所有响应。 - killthrush
8
您也可以使用 import logging logging.getLogger('botocore').setLevel(logging.DEBUG) 来达到同样的效果。 - Craig Ringer

8

所以你可能想要做的是通过代理(mitmproxy、squid)发送请求。然后检查代理发送了什么。由于HTTPS数据是加密的,您必须首先解密它,然后记录响应,再将其加密并发送到AWS。其中一个选项是使用mitmproxy。(安装非常容易)

  1. Run mitmproxy
  2. Open up another terminal and point proxy to mitmproxys port:

    export http_proxy=127.0.0.1:8080
    export https_proxy=$http_proxy
    
  3. Then set verify=False when creating session/client

    In [1]: import botocore.session
    
    In [2]: client = botocore.session.Session().create_client('elasticache', verify=False)
    
  4. Send request and look at the output of mitmproxy

    In [3]: client.describe_cache_engine_versions()
    
  5. The result should be similar to this:

    Host:             elasticache.us-east-1.amazonaws.com
    Accept-Encoding:  identity
    Content-Length:   53
    Content-Type:     application/x-www-form-urlencoded
    Authorization:    AWS4-HMAC-SHA256 Credential=FOOOOOO/20150428/us-east-1/elasticache/aws4_request, SignedHeaders=host;user-agent;x-amz-date, Signature=BAAAAAAR
    X-Amz-Date:       20150428T213004Z
    User-Agent:       Botocore/0.103.0 Python/2.7.6 Linux/3.13.0-49-generic
    
<?xml version='1.0' encoding='UTF-8'?>
<DescribeCacheEngineVersionsResponse
xmlns="http://elasticache.amazonaws.com/doc/2015-02-02/">  
<DescribeCacheEngineVersionsResult>
    <CacheEngineVersions>
      <CacheEngineVersion>
      <CacheParameterGroupFamily>memcached1.4</CacheParameterGroupFamily>
    <Engine>memcached</Engine>
    <CacheEngineVersionDescription>memcached version 1.4.14</CacheEngineVersionDescription>
    <CacheEngineDescription>memcached</CacheEngineDescription>
    <EngineVersion>1.4.14</EngineVersion>

我无法通过pip快速使mitmproxy工作,但是我使用了你的想法,并使用Burp Suite运行了请求。我能够看到错误的来源。谢谢你的建议! - zachhilbert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接