Skip to main content
 首页 » 编程设计

python之检查证书的扭曲的 HTTPS 请求

2025年05月04日15kenshinobiy

在我扭曲的应用程序中,我想向 Akismet 发出异步请求以检查垃圾邮件。 Akismet 合理地使用 HTTPS,所以我一直在关注 web client guide on SSL在文档中。但是这部分让我担心:

Here’s an example which shows how to use Agent to request an HTTPS URL with no certificate verification.

我非常希望通过证书验证来防止中间人攻击。那么如何添加呢?

我未经验证的测试代码是这样的:

from twisted.internet import reactor 
from twisted.web.client import Agent 
from twisted.internet.ssl import ClientContextFactory 
 
class WebClientContextFactory(ClientContextFactory): 
    def getContext(self, hostname, port): 
        print( "getting context for {}:{}".format( hostname, port ) ) 
        # FIXME: no attempt to verify certificates! 
        return ClientContextFactory.getContext(self) 
 
agent = Agent( reactor, WebClientContextFactory() ) 
 
def success( response ): 
    print( "connected!" ) 
def failure( failure ): 
    print( "failure: {}".format( failure ) ) 
def stop( ignored ): 
    reactor.stop() 
 
agent.request( "GET", "https://www.pcwebshop.co.uk/" )\ # uses self-signed cert 
.addCallbacks( success, failure )\ 
.addBoth( stop ) 
 
reactor.run() 

我希望它因无法验证证书而失败。

请您参考如下方法:

我正在使用 Twisted 15.1.0。

实际上,Agent的默认init函数会传入BrowserLikePolicyForHTTPS作为contextFactory,并具备验证服务器证书的能力。

简单地使用这个:

agent = Agent( reactor ) 

会产生以下错误:

failure: [Failure instance: Traceback (failure with no frames):      
<class 'twisted.web._newclient.ResponseNeverReceived'>: 
[<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]] 

确保您使用 pip 安装了 service_identity 包。


如果您需要自定义证书验证,您可以通过传入 pem 来创建自定义策略,如所述here :

customPolicy = BrowserLikePolicyForHTTPS( 
    Certificate.loadPEM(FilePath("your-trust-root.pem").getContent()) 
) 
agent = Agent(reactor, customPolicy)