{"id":165,"date":"2016-04-17T14:32:06","date_gmt":"2016-04-17T06:32:06","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=165"},"modified":"2016-04-17T14:32:06","modified_gmt":"2016-04-17T06:32:06","slug":"8-9-%e5%83%8fpython%e8%a1%a8%e8%be%be%e5%bc%8f%e4%b8%80%e6%a0%b7%e8%af%84%e4%bc%b0%e9%9a%8f%e6%84%8f%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=165","title":{"rendered":"8.9 \u50cfpython\u8868\u8fbe\u5f0f\u4e00\u6837\u8bc4\u4f30\u968f\u610f\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\uff081\uff09eval()\u51fd\u6570\uff0c\u7edf\u4e00\u7684python\u8bc4\u4f30\u5de5\u5177\u3002<\/p>\n<p><code> &gt;&gt;&gt; eval('1 + 1 == 2')<\/code><br \/>\n<code>True<\/code><br \/>\n<code>&gt;&gt;&gt; eval('1 + 1 == 3')<\/code><br \/>\n<code>False<\/code><br \/>\n<code>&gt;&gt;&gt; eval('9567 + 1085 == 10652')<\/code><br \/>\n<code>True<\/code><\/p>\n<p>eval()\u51fd\u6570\u4e0d\u9650\u4e8e\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5b83\u80fd\u5904\u7406\u4efb\u4f55python\u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><code> &gt;&gt;&gt; eval('\"A\" + \"B\"')<\/code><br \/>\n<code>'AB'<\/code><br \/>\n<code>&gt;&gt;&gt; eval('\"MARK\".translate({65: 79})')<\/code><br \/>\n<code>'MORK'<\/code><br \/>\n<code>&gt;&gt;&gt; eval('\"AAAAA\".count(\"A\")')<\/code><br \/>\n<code>5<\/code><br \/>\n<code>&gt;&gt;&gt; eval('[\"*\"] * 5')<\/code><br \/>\n<code>['*', '*', '*', '*', '*']<\/code><\/p>\n<p><code> &gt;&gt;&gt; x = 5<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"x * 5\") <\/code><br \/>\n<code>25<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"pow(x, 2)\")<\/code><br \/>\n<code>25<\/code><br \/>\n<code>&gt;&gt;&gt; import math<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"math.sqrt(x)\")<\/code><br \/>\n<code>2.2360679774997898<\/code><\/p>\n<p><code> &gt;&gt;&gt; import subprocess<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"subprocess.getoutput('ls ~')\") \u2460<\/code><br \/>\n<code>'Desktop \u00a0\u00a0 Library \u00a0\u00a0 Pictures \\<\/code><br \/>\n<code>Documents\u00a0 \u00a0 Movies \u00a0\u00a0 Public \\<\/code><br \/>\n<code>Music Sites'<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"subprocess.getoutput('rm \/some\/random\/file')\") \u2461<\/code><\/p>\n<ol>\n<li>subprocess\u6a21\u5757\u5141\u8bb8\u4f60\u8fd0\u884c\u4efb\u4f55shell\u547d\u4ee4\u5e76\u4e14\u4ee5\u5b57\u7b26\u4e32\u65b9\u5f0f\u5f97\u5230\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<li>\u76f8\u540c\u7684\u547d\u4ee4\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u7ed3\u679c\u3002<br \/>\n\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u590d\u6742\u7684\u65b9\u6cd5\u5b8c\u6210\u4e0a\u9762\u7684\u547d\u4ee4\uff0c__import__()\u51fd\u6570\u4f7f\u7528\u4e00\u4e2a\u6a21\u5757\u7684\u540d\u5b57\u6765\u52a0\u8f7d\u5b83\uff0c\u7136\u540e\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5b83\uff1a<br \/>\n<code>&gt;&gt;&gt;eval(\"__import__('subprocess').getoutput('rm \/some\/random\/file')\")<\/code><\/li>\n<\/ol>\n<h2><strong>eval()\u662f\u90aa\u6076\u7684\uff01<\/strong><\/h2>\n<p>\u90aa\u6076\u7684\u90e8\u5206\u662feval\u8bc4\u4f30\u4efb\u610f\u6765\u81ea\u672a\u4fe1\u4efb\u6e90\u7684\u8868\u8fbe\u5f0f\u3002\u5e94\u8be5\u53ea\u5728\u4fe1\u4efb\u7684\u8f93\u5165\u4e0a\u4f7f\u7528eval()\u51fd\u6570\u3002\u5f53\u7136\uff0c\u5176\u4e2d\u7684\u6280\u5de7\u5c31\u662f\u5982\u4f55\u627e\u51fa\u4ec0\u4e48\u662f\u201c\u4fe1\u4efb\u7684\u201d\u3002<\/p>\n<p><code> &gt;&gt;&gt; x = 5<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"x * 5\", {}, {})\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 \u2460<\/code><br \/>\n<code>Traceback (most recent call last):<\/code><br \/>\n<code>File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>File \"&lt;string&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>NameError: name 'x' is not defined<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"x * 5\", {\"x\": x}, {}) \u00a0 \u00a0 \u00a0 \u2461<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\" x*5\", {\"x\":5} , {})<\/code><br \/>\n<code>25<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\" x * 5\", {}, {\"x\":5})<\/code><br \/>\n<code>25<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"x * 5\", {\"x\":5}, {\"x\":3}) <\/code><br \/>\n<code>15<\/code><br \/>\n<code>&gt;&gt;&gt; import math<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"math.sqrt(x)\", {\"x\": x}, {})\u00a0 \u2462<\/code><br \/>\n<code>Traceback (most recent call last):<\/code><br \/>\n<code>File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>File \"&lt;string&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>NameError: name 'math' is not defined<\/code><\/p>\n<ol>\n<li>\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u9012\u5230eval()\u51fd\u6570\u4e2d\uff0c\u505a\u4e3a\u5168\u5c40\u53c2\u6570\u548c\u672c\u5730\u53c2\u6570\u5bf9\u8868\u8fbe\u5f0f\u8fdb\u884c\u8bc4\u4f30\u3002\u5728\u672c\u4f8b\u4e2d\u4e24\u4e2a\u90fd\u662f\u7a7a\u7684\uff0c\u88ab\u8bc4\u4f30\u7684\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u662f\u201cx * 5\u201d\uff0c\u56e0\u4e3a\u6ca1\u6709\u7d22\u5f15\u6307\u5411x\uff0c\u6240\u4ee5eval\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/li>\n<li>\u53ef\u4ee5\u6709\u9009\u62e9\u6027\u7684\u5305\u542b\u6307\u5b9a\u7684\u503c\u5728\u5168\u5c40\u7a7a\u95f4\u6216\u672c\u5730\u7a7a\u95f4\u4e2d\uff0c\u7136\u540e\u5c31\u4f1a\u7528\u8fd9\u4e9b\u503c\u53bb\u8bc4\u4f30\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>\u867d\u7136\u5df2\u7ecf\u5bfc\u5165math\u6a21\u5757\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u5305\u542b\u5728\u540d\u5b57\u7a7a\u95f4\u4e2d\u4f20\u9012\u7ed9eval()\u51fd\u6570\uff0c\u6240\u4ee5\u540c\u6837\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<p><code> &gt;&gt;&gt; eval(\"pow(5, 2)\", {}, {}) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u2460<\/code><br \/>\n<code>25<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"__import__('math').sqrt(5)\", {}, {})\u00a0 \u2461<\/code><br \/>\n<code>2.2360679774997898<\/code><\/p>\n<ol>\n<li>\u867d\u7136\u5728\u5168\u5c40\u548c\u672c\u5730\u7a7a\u95f4\u90fd\u662f\u7a7adictionary\uff0c\u4f46\u662fpython3\u7684\u5185\u5efa\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u3002\u6240\u4ee5<code>pow(5, 2)<\/code>\u53ef\u4ee5\u5de5\u4f5c\u3002<\/li>\n<li>\u5f88\u4e0d\u5e78\uff08\u4e3a\u4ec0\u4e48\u4e0d\u5e78\u5462\uff1f\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\uff09\uff0c__import__()\u51fd\u6570\u540c\u6837\u662f\u5185\u5efa\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u5de5\u4f5c\u3002\u662f\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4ecd\u7136\u53ef\u4ee5\u505a\u4e00\u4e9b\u4e0d\u597d\u7684\u4e8b\u60c5\uff08\u522b\u60f3\u6b6a\u4e86\uff0c\u8fd9\u91cc\u539f\u6587\u662fthat means you can still do nasty things\uff09\uff0c\u5373\u4f7f\u4f60\u5df2\u7ecf\u660e\u663e\u7684\u8bbe\u7f6e\u4e86\u7a7a\u7684\u5168\u5c40\u548c\u672c\u5730\u540d\u5b57\u7a7a\u95f4\uff1a<br \/>\n<code>&gt;&gt;&gt; eval(\"__import__('subprocess').getoutput('rm \/some\/random\/file')\", {}, {})<\/code><\/li>\n<\/ol>\n<p><code> &gt;&gt;&gt; eval(\"__import__('math').sqrt(5)\",<\/code><br \/>\n<code>... {\"__builtins__\":None}, {})\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2460<\/code><br \/>\n<code>Traceback (most recent call last):<\/code><br \/>\n<code>File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>File \"&lt;string&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>NameError: name '__import__' is not defined<\/code><br \/>\n<code>&gt;&gt;&gt; eval(\"__import__('subprocess').getoutput('rm -rf \/')\",<\/code><br \/>\n<code>... {\"__builtins__\":None}, {})\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2461<\/code><br \/>\n<code>Traceback (most recent call last):<\/code><br \/>\n<code>File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>File \"&lt;string&gt;\", line 1, in &lt;module&gt;<\/code><br \/>\n<code>NameError: name '__import__' is not defined<\/code><\/p>\n<ol>\n<li>\u53bb\u5b89\u5168\u5730\u8bc4\u4f30\u4e00\u4e2a\u672a\u4fe1\u4efb\u7684\u8868\u8fbe\u5f0f\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u540d\u5b57\u7a7a\u95f4\u5b57\u5178\uff0c\u5b83\u6620\u5c04\u201d__builtins__\u201c\u5230none\uff0cpython\u7684\u7a7a\u503c\u3002built-in\u51fd\u6570\u88ab\u5305\u542b\u5728\u4e00\u4e2a\u88ab\u53eb\u505a\u201d__builtins__\u201c\u7684\u9884\u5b9a\u4e49\u6a21\u5757\u91cc\u3002\u8fd9\u4e2a\u9884\u5b9a\u4e49\u6a21\u5757\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u53ef\u4ee5\u7528\u6765\u8bc4\u4f30\u8868\u8fbe\u5f0f\u7684\uff0c\u9664\u975e\u663e\u793a\u7684\u8986\u76d6\u5b83\u3002<\/li>\n<li>\u8bb0\u4f4f\u662f__builtins__\uff0c\u4e0d\u8981\u62fc\u9519\u4e86\uff0c\u5199\u9519\u4e86\u5c31\u4e0d\u4f1a\u8fbe\u5230\u8986\u76d6\u5185\u5efa\u6a21\u5757\u7684\u4f5c\u7528\u4e86\u3002<\/li>\n<\/ol>\n<p>\u90a3\u8fd9\u6837\u662f\u4e0d\u662f\u5c31\u53ef\u4ee5\u5b89\u5168\u7684\u4f7f\u7528<code>eval()<\/code>\u4e86\u5462\uff1f<\/p>\n<p><code> &gt;&gt;&gt; eval(\"2 ** 2147483647\",<\/code><br \/>\n<code>... {\"__builtins__\":None}, {}) \u2460<\/code><\/p>\n<ol>\n<li>\u5373\u4f7f\u4e0d\u53bb\u8bbf\u95ee__builtins__\uff0c\u4f60\u4ecd\u7136\u53ef\u80fd\u906d\u5230\u62d2\u7edd\u670d\u52a1\u7684\u529f\u51fb\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u8ba1\u7b972\u76842147483647\u6b21\u5e73\u65b9\uff0c\u8fd9\u5c06\u4f1a\u4f7f\u4f60\u7684CPU\u5229\u7528\u7387\u8fbe\u5230100%\u76f8\u5f53\u957f\u7684\u4e00\u6bb5\u65f6\u95f4\u3002\u6280\u672f\u4e0a\u8bf4\u8fd9\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u4f60\u7684\u4e3b\u673a\u5c06\u4f1a\u6709\u5f88\u957f\u7684\u4e00\u6bb5\u65f6\u95f4\u4ec0\u4e48\u90fd\u4e0d\u80fd\u5e72\u3002<\/li>\n<\/ol>\n<p>\u603b\u4e4b\u4f7f\u7528eval\u8981\u5c0f\u5fc3\u884c\u4e8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff081\uff09eval()\u51fd\u6570\uff0c\u7edf\u4e00\u7684python\u8bc4\u4f30\u5de5\u5177\u3002 &gt;&gt;&gt; eval(&#8216;1 + 1 == 2&#8217;) True &gt;&gt;&gt; eva&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=165\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-dive-into-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":3,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}