<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Behind the Code &#187; security</title>
	<atom:link href="http://yinwm.com/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://yinwm.com</link>
	<description>Just Do It</description>
	<lastBuildDate>Wed, 04 Jan 2012 03:54:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>网络安全通讯（5）&#8212;-支持客户端验证的python web server</title>
		<link>http://yinwm.com/2008/04/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%885%ef%bc%89-%e6%94%af%e6%8c%81%e5%ae%a2%e6%88%b7%e7%ab%af%e9%aa%8c%e8%af%81%e7%9a%84python-web-server/</link>
		<comments>http://yinwm.com/2008/04/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%885%ef%bc%89-%e6%94%af%e6%8c%81%e5%ae%a2%e6%88%b7%e7%ab%af%e9%aa%8c%e8%af%81%e7%9a%84python-web-server/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 12:49:39 +0000</pubDate>
		<dc:creator>yinwm</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[安全]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://yinwm.cn/wordpress/?p=45</guid>
		<description><![CDATA[前面我们说道很多的场景是需要客户端验证（Client Certificate）的，而python的web server如何支持呢？大多数的python web server都是符合WSGI标准的，也就是他们提供WSGI标准支持的功能，也就是他们几乎都不支持WSGI不支持的功能。很遗憾，客户端验证不在此列。换句话说，Cherrypy，webpy等常用的python web server都无法支持客户端验证。 我终于找到了Twisted（他怎么一下从2.5就蹦到了8.0.1）的web模块可以作为一个单独的web server。而这个web模块是包装的很底层的，使用pyopenssl，并且不是WSGI的一个实现。也就是说，他很可能是支持客户端验证的。经过一番google，发现了支持的办法。如下：首先我们看一下如何让Twisted支持HTTPS，他需要执行reactor.listenSSL方法， class Hello(Resource) :&#160;&#160;&#160; isLeaf = True&#160;&#160;&#160; def render(self, request) :&#160;&#160;&#160;&#160;&#160;&#160;&#160; return &#8216;Hello, SSL&#8217;root = server.Site(Hello())ctxFactory = ssl.DefaultOpenSSLContextFactory(&#160;&#160;&#160; &#8216;/path/to/server.key&#8217;,&#160;&#160;&#160; &#8216;/path/to/server.crt&#8217;&#160;&#160;&#160; )reactor.listenSSL(443, root, contextFactory = ctxFactory)reactor.run() 这是最基本的支持HTTPS的方法，我们看，他需要一个contextFactory，深入到Twisted里面发现这个contextFactory是SSL进行设置。我们想要他支持客户端验证，就要从这里下手了。DefaultOpenSSLContextFactory的cacheContext方法是对context进行的操作。我们重写这个方法。 class SSLClientCertificateFactory(ssl.DefaultOpenSSLContextFactory) : &#160;&#160;&#160; def __init__(self, privateKeyFileName, certificateFileName, sslmethod=SSL.SSLv23_METHOD) :&#160;&#160;&#160;&#160;&#160;&#160;&#160; ssl.DefaultOpenSSLContextFactory.__init__(self, privateKeyFileName, certificateFileName, sslmethod) &#160;&#160;&#160; def _verify(self, connection, x509, errnum, errdepth, [...]]]></description>
			<content:encoded><![CDATA[<p>前面我们说道很多的场景是需要客户端验证（Client Certificate）的，而python的web server如何支持呢？<br />大多数的python web server都是符合WSGI标准的，也就是他们提供WSGI标准支持的功能，也就是他们几乎都不支持WSGI不支持的功能。很遗憾，客户端验证不在此列。换句话说，Cherrypy，webpy等常用的python web server都无法支持客户端验证。</p>
<p>我终于找到了<a href="http://twistedmatrix.com/" target="blank">Twisted</a>（他怎么一下从2.5就蹦到了8.0.1）的web模块可以作为一个单独的web server。而这个web模块是包装的很底层的，使用pyopenssl，并且不是WSGI的一个实现。也就是说，他很可能是支持客户端验证的。<br />经过一番google，发现了支持的办法。如下：<br />首先我们看一下如何让Twisted支持HTTPS，他需要执行reactor.listenSSL方法，</p>
<p>class Hello(Resource) :<br />&nbsp;&nbsp;&nbsp; isLeaf = True<br />&nbsp;&nbsp;&nbsp; def render(self, request) :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#8216;Hello, SSL&#8217;<br />root = server.Site(Hello())<br />ctxFactory = ssl.DefaultOpenSSLContextFactory(<br />&nbsp;&nbsp;&nbsp; &#8216;/path/to/server.key&#8217;,<br />&nbsp;&nbsp;&nbsp; &#8216;/path/to/server.crt&#8217;<br />&nbsp;&nbsp;&nbsp; )<br />reactor.listenSSL(443, root, contextFactory = ctxFactory)<br />reactor.run()</p>
<p>这是最基本的支持HTTPS的方法，我们看，他需要一个contextFactory，深入到Twisted里面发现这个contextFactory是SSL进行设置。我们想要他支持客户端验证，就要从这里下手了。<br />DefaultOpenSSLContextFactory的cacheContext方法是对context进行的操作。我们重写这个方法。</p>
<p>class SSLClientCertificateFactory(ssl.DefaultOpenSSLContextFactory) :</p>
<p>&nbsp;&nbsp;&nbsp; def __init__(self, privateKeyFileName, certificateFileName, sslmethod=SSL.SSLv23_METHOD) :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ssl.DefaultOpenSSLContextFactory.__init__(self, privateKeyFileName, certificateFileName, sslmethod)</p>
<p>&nbsp;&nbsp;&nbsp; def _verify(self, connection, x509, errnum, errdepth, ok):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;&#8221;"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#8216;_verify (ok=%d):&#8217; % ok<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#8216;&nbsp; subject:&#8217;, x509.get_subject()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#8216;&nbsp; issuer:&#8217;, x509.get_issuer()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &#8216;&nbsp; errnum %s, errdepth %d&#8217; % (errnum, errdepth)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;&#8221;"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ok<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; def cacheContext(self) :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ssl.DefaultOpenSSLContextFactory.cacheContext(self)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ctx = self._context<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ctx.load_verify_locations(&#8216;/path/to/ca.crt&#8217;)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ctx.set_verify(SSL.VERIFY_PEER|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, self._verify)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self._context = ctx</p>
<p>我们在通过默认的contextFactory生成context之后，执行ctx.set_verify(SSL.VERIFY_PEER|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, self._verify)，这样就加入和客户端验证，并且在self._verify函数里面我们还可以具体的验证证书的情况。<br />改写最上面的程序，<br />class Hello(Resource) :<br />
&nbsp;&nbsp;&nbsp; isLeaf = True<br />
&nbsp;&nbsp;&nbsp; def render(self, request) :<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#8216;Hello, SSL&#8217;<br />
root = server.Site(Hello())<br />
ctxFactory = SSLClientCertificateFactory(<br />
&nbsp;&nbsp;&nbsp; &#8216;/path/to/server.key&#8217;,<br />
&nbsp;&nbsp;&nbsp; &#8216;/path/to/server.crt&#8217;<br />
&nbsp;&nbsp;&nbsp; )<br />
reactor.listenSSL(443, root, contextFactory = ctxFactory)<br />reactor.run()</p>
<p>好了，此时我们的python web server已经支持客户端验证了</p>
<p>后记：这里我有一个思考，就是为什么WSGI（暂时）不支持客户端验证？我想应该不是设计者忽视了这个问题。一般的，需要客户端验证这样的应用场景，很难只是假设一个python的web server，前面应该都有一个Apache等纯粹的服务器，那么这些验证之类的工作完全交付给Apache就足够好了，python没必要自己来处理。也许这就是原因吧。<br />那么，从这里我们也能看出，作为一个纯粹的网络应用组件，Twisted至少从功能上还是非常强大的。</p>
]]></content:encoded>
			<wfw:commentRss>http://yinwm.com/2008/04/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%885%ef%bc%89-%e6%94%af%e6%8c%81%e5%ae%a2%e6%88%b7%e7%ab%af%e9%aa%8c%e8%af%81%e7%9a%84python-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>网络安全通讯（4）&#8212;-为Python Webserver添加HTTPS支持</title>
		<link>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%884%ef%bc%89-%e4%b8%bapython-webserver%e6%b7%bb%e5%8a%a0https%e6%94%af%e6%8c%81/</link>
		<comments>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%884%ef%bc%89-%e4%b8%bapython-webserver%e6%b7%bb%e5%8a%a0https%e6%94%af%e6%8c%81/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 10:22:00 +0000</pubDate>
		<dc:creator>yinwm</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[安全]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://yinwm.cn/wordpress/?p=44</guid>
		<description><![CDATA[前面我们说的都是为Apache服务器添加HTTPS的支持，今天我们讨论一下为Python的Webserver添加HTTPS的支持。 Python的Webserver基本上都是遵循Python的WSGI标准，包括我使用的CherryPy和webpy。在这两个Webserver的源代码中都有一个package叫做wsgiserver，里面只有__init__.py。这两个Webserver的wsgiserver内容几乎一样，而且明显有CherryPy的痕迹，看来CherryPy可能是WSGI标准的一个（半）官方实现，就好像Tomcat至于Servlet。 WSGI标准写的很是笼统，他只是说可以支持SSL，无他。只好阅读源代码，代码里面写的倒不是很差。需要同时给出ssl需要的服务器端的私钥和证书，分别使用如下两个变量存放。ssl_certificate: the filename of the server SSL certificate.&#160;&#160;&#160;&#160;&#160; 服务器端的SSL证书ssl_privatekey: the filename of the server&#8217;s private key file.&#160;&#160;&#160; 服务器端的私钥我们只要遵循每个服务器的文档设置上就可以了。我们以CherryPy为例。（好吧，我说实话，我只会这个。） 在CherryPy的配置文件里面，可以设置这两个变量，分别指向文件系统的文件。[global]server.socket_port : 8081server.socket_host : &#8220;0.0.0.0&#8243;server.ssl_certificate : &#8220;conf/cert/server.crt&#8221;server.ssl_private_key : &#8220;conf/cert/server.key&#8221;这样，当我们重新启动的时候，我们的webserver就已经开始支持HTTPS了。如果这两个变量有一个没有设置的话，HTTPS就不会起作用。 这里我不得不说一下，CherryPy的文档已经算是不错的了，但是还是不行，只有在ServerAPI里面稍微提及了一下这两个变量而已。想要完整的搞明白，就只能祭出hack源代码大发，还得有连蒙带唬的功能。 至于webpy，就最好看看他的文档了。]]></description>
			<content:encoded><![CDATA[<p>前面我们说的都是为Apache服务器添加HTTPS的支持，今天我们讨论一下为Python的Webserver添加HTTPS的支持。</p>
<p>Python的Webserver基本上都是遵循Python的<a href="http://www.python.org/dev/peps/pep-0333/" target="_blank">WSGI标准</a>，包括我使用的CherryPy和webpy。在这两个Webserver的源代码中都有一个package叫做wsgiserver，里面只有__init__.py。这两个Webserver的wsgiserver内容几乎一样，而且明显有CherryPy的痕迹，看来CherryPy可能是WSGI标准的一个（半）官方实现，就好像Tomcat至于Servlet。</p>
<p>WSGI标准写的很是笼统，他只是说可以支持SSL，无他。只好阅读源代码，代码里面写的倒不是很差。需要同时给出ssl需要的服务器端的私钥和证书，分别使用如下两个变量存放。<br />ssl_certificate: the filename of the server SSL certificate.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 服务器端的SSL证书<br />ssl_privatekey: the filename of the server&#8217;s private key file.&nbsp;&nbsp;&nbsp; 服务器端的私钥<br />我们只要遵循每个服务器的文档设置上就可以了。我们以CherryPy为例。（好吧，我说实话，我只会这个。）</p>
<p>在CherryPy的配置文件里面，可以设置这两个变量，分别指向文件系统的文件。<br />[global]<br />server.socket_port : 8081<br />server.socket_host : &#8220;0.0.0.0&#8243;<br />server.ssl_certificate : &#8220;conf/cert/server.crt&#8221;<br />server.ssl_private_key : &#8220;conf/cert/server.key&#8221;<br />这样，当我们重新启动的时候，我们的webserver就已经开始支持HTTPS了。如果这两个变量有一个没有设置的话，HTTPS就不会起作用。</p>
<p>这里我不得不说一下，CherryPy的文档已经算是不错的了，但是还是不行，只有在ServerAPI里面稍微提及了一下这两个变量而已。想要完整的搞明白，就只能祭出hack源代码大发，还得有连蒙带唬的功能。</p>
<p>至于webpy，就最好看看他的文档了。</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%884%ef%bc%89-%e4%b8%bapython-webserver%e6%b7%bb%e5%8a%a0https%e6%94%af%e6%8c%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>网络安全通讯（1）&#8212;-给你的Webserver启用SSL</title>
		<link>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%881%ef%bc%89-%e7%bb%99%e4%bd%a0%e7%9a%84webserver%e5%90%af%e7%94%a8ssl/</link>
		<comments>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%881%ef%bc%89-%e7%bb%99%e4%bd%a0%e7%9a%84webserver%e5%90%af%e7%94%a8ssl/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 06:07:05 +0000</pubDate>
		<dc:creator>yinwm</dc:creator>
				<category><![CDATA[安全]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://yinwm.cn/wordpress/?p=38</guid>
		<description><![CDATA[互联网很危险，你还是回火星吧。好吧，我们还有SSL，你可以等一下了。 先说一下这里面用到的库OpenSSL 0.9.8gApache Httpd 2.2 SSL是一个可以保证你的通讯安全的机制，采用公钥私钥的非对加密机制，具体机制我就不说了，网上一抓一大把。你可以使用openssl建立自己的密钥，并使用在Apache Webserver上，让你的服务器支持https。 建立你自己的服务器私钥，一个1024位的就够了openssl genrsa -des3 -out server.key 1024 生成CSR文件，Certificate Signing Requestopenssl req -new -key server.key -out server.csr 生成证书CRT文件，X509协议openssl x509 -req -in server.csr -signkey server.key -out server.crt注意，这是一个自签名的证书 修改你apache的ssl配置，SSLCertificateFile &#8220;/path-to/server.crt&#8221;SSLCertificateKeyFile &#8220;/path-to/server.key&#8221; 启动你的apache，使用https访问试试看，是不是要你确认是否允许证书通过 这时候，你的服务器已经支持SSL了，刚才浏览器问你是否通过你服务器提供的证书是表示客户端（浏览器）信任了你的服务器，这里也就是服务器认证。你们之间的通讯已经是加密的了。]]></description>
			<content:encoded><![CDATA[<p>互联网很危险，你还是回火星吧。<br />好吧，我们还有SSL，你可以等一下了。</p>
<p>先说一下这里面用到的库<br />OpenSSL 0.9.8g<br />Apache Httpd 2.2</p>
<p><a target="_blank" href="http://en.wikipedia.org/wiki/Secure_Sockets_Layer">SSL</a>是一个可以保证你的通讯安全的机制，采用公钥私钥的非对加密机制，具体机制我就不说了，网上一抓一大把。<br />你可以使用openssl建立自己的密钥，并使用在Apache Webserver上，让你的服务器支持https。
<ol>
<li>建立你自己的服务器私钥，一个1024位的就够了<br />openssl genrsa -des3 -out server.key 1024</li>
<li><span class="Body-0020Text-002cBody-0020Text-0020Char--Char" style="font-weight: bold;"></span><span class="Body-0020Text-002cBody-0020Text-0020Char--Char" style="font-weight: bold;"></span>生成CSR文件，<a target="_blank" href="http://en.wikipedia.org/wiki/Certificate_signing_request">Certificate Signing Request</a><br />openssl req -new -key server.key -out server.csr</li>
<li>生成证书CRT文件，X509协议<br />openssl x509 -req -in server.csr -signkey server.key -out server.crt<br />注意，这是一个自签名的证书</li>
<li>修改你apache的ssl配置，<br />SSLCertificateFile &#8220;/path-to/server.crt&#8221;<br />SSLCertificateKeyFile &#8220;/path-to/server.key&#8221;</li>
<li>启动你的apache，使用https访问试试看，是不是要你确认是否允许证书通过</li>
</ol>
<p>这时候，你的服务器已经支持SSL了，刚才浏览器问你是否通过你服务器提供的证书是表示客户端（浏览器）信任了你的服务器，这里也就是服务器认证。你们之间的通讯已经是加密的了。</p>
]]></content:encoded>
			<wfw:commentRss>http://yinwm.com/2008/03/%e7%bd%91%e7%bb%9c%e5%ae%89%e5%85%a8%e9%80%9a%e8%ae%af%ef%bc%881%ef%bc%89-%e7%bb%99%e4%bd%a0%e7%9a%84webserver%e5%90%af%e7%94%a8ssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

