{"id":2345,"date":"2015-07-31T14:58:40","date_gmt":"2015-07-31T06:58:40","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=2345"},"modified":"2022-05-29T20:46:37","modified_gmt":"2022-05-29T12:46:37","slug":"php-%e4%bd%bf%e7%94%a8-pdo-statement-%e9%a0%90%e8%99%95%e7%90%86%e5%a6%82%e4%bd%95%e6%9f%a5%e9%a9%97-sql-%e8%aa%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=2345","title":{"rendered":"PHP \u4f7f\u7528 PDO Statement \u9810\u8655\u7406,\u5982\u4f55\u67e5\u9a57 SQL \u8a9e\u6cd5"},"content":{"rendered":"<p>--<\/p>\n<h2>PDO Statement<\/h2>\n<p>\u5728\u57f7\u884c MySQL \u7684 SQL \u8a9e\u53e5\u6642\uff0c\u9664\u4e86\u8a9e\u6cd5\u6b63\u78ba\u5916\u9084\u8981\u6ce8\u610f<\/p>\n<ol>\n<li>\u95dc\u9375\u5b57\u6df7\u7528<\/li>\n<li>SQL injection<\/li>\n<\/ol>\n<p>\u6240\u4ee5\u67e5\u9a57\u4f7f\u7528\u8005\u5e33\u865f\u3001\u5bc6\u78bc\u7684 SQL \u8a9e\u53e5\u53ef\u80fd\u6703\u9577\u9019\u6a23\uff1a<\/p>\n<pre class=\"lang:php decode:true\">$sql = \"SELECT * FROM `user` WHERE `username`='\". $mysqli-&gt;real_escape_string($username) .\"' AND `password`='\". $mysqli-&gt;real_escape_string($password) .\"'\";<\/pre>\n<ul>\n<li>password \u662f\u95dc\u9375\u5b57\uff0c\u6240\u4ee5\u5fc5\u9808\u4f7f\u7528 ` \u5305\u4f4f<\/li>\n<li>\u50b3\u905e\u8b8a\u6578\u7686\u9808\u9810\u9632 ' \u9019\u985e\u5b57\u5143\u4e2d\u65b7\u6b63\u5e38\u7684 SQL \u8a9e\u53e5\u57f7\u884c\uff0c\u5728\u6b64\u4f7f\u7528 MySQLi \u7684 real_escape_string \u65b9\u6cd5<\/li>\n<\/ul>\n<p>\u5982\u6b64\u5c31\u53ef\u4ee5\u5f97\u5230\u6b63\u78ba\u7684\u57f7\u884c\u7d50\u679c\uff0c\u53c8\u4e0d\u6703\u56e0\u70ba\u758f\u5ffd\u7522\u751f\u8cc7\u8a0a\u5b89\u5168\u554f\u984c\u3002<\/p>\n<p>\u5982\u679c\u89ba\u5f97\u9019\u6a23\u5b50\u8655\u7406\u300c\u4e0d\u9177\u300d\u7684\u8a71\uff0c\u90a3\u53ef\u4ee5\u8003\u616e<strong>\u9810\u8655\u7406<\/strong>\u7684\u65b9\u6848\uff0c\u4e00\u6a23\u7684\u9700\u6c42\u4f7f\u7528\u9810\u8655\u7406\u7684\u540c\u4e00\u884c SQL \u8a9e\u53e5\u9577\u9019\u6a23\uff1a<\/p>\n<pre class=\"lang:php decode:true\">$sql = \"SELECT * FROM user WHERE username=:username AND password=:password\";<\/pre>\n<p>\u5148\u8b93\u8cc7\u6599\u5eab\u5f97\u5230\u300c\u7d50\u69cb\u300d\uff0c\u518d\u5c07\u8cc7\u6599\u586b\u5165\uff0c\u5982\u6b64\u8cc7\u6599\u5eab\u53ea\u6703\u300c\u60f3\u8fa6\u6cd5\u586b\u5165\u8cc7\u6599\u300d\u800c\u4e0d\u6703\u7834\u58de\u8a9e\u6cd5\u7d50\u69cb\uff0c\u9032\u800c\u7522\u751f\u5b89\u5168\u554f\u984c\u3002<\/p>\n<p>\u6240\u4ee5\u8cc7\u6599\u5eab\u6703\u6bd4\u5c0d\u6b04\u4f4d\u77e5\u9053 password \u662f\u6b04\u4f4d\uff0c\u5f8c\u7e8c\u7684 :username \u548c :password \u7684\u8cc7\u6599\u6703\u5e36\u5165\uff0c\u300c\u4e0d\u6703\u66f4\u52d5 SQL \u8a9e\u53e5\u7d50\u69cb\u300d<\/p>\n<p>\u4e5f\u5c31\u662f\u4ee5\u4e0b\u7684\u7d05\u8272\u90e8\u4efd\u4e0d\u6703\u6e1b\u5c11\u4e5f\u4e0d\u80fd\u589e\u52a0<\/p>\n<p><strong><span style=\"color: #ff0000;\">SELECT * FROM user WHERE username=<\/span><\/strong>:username <strong><span style=\"color: #ff0000;\">AND password=<\/span><\/strong>:password<\/p>\n<p>\u56e0\u70ba\u4e00\u822c\u7684 SQL injection \u5c31\u662f\u4f7f\u7528\u300c\u589e\u52a0 SQL \u8a9e\u6cd5\u300d\u7834\u58de\u4e86\u539f\u672c\u7684\u67b6\u69cb\u4f86\u9054\u5230\u76ee\u7684\uff0c\u4f8b\u5982<\/p>\n<p>SELECT * FROM user WHERE username='<strong><span style=\"color: #ff0000;\">root' or '1=1<\/span><\/strong>'\u00a0 AND password='xxx'<\/p>\n<p>\u7d05\u8272\u70ba\u4f7f\u7528\u8005\u586b\u5165\u8cc7\u6599\uff0c\u5982\u6b64\u4e00\u4f86 SQL \u7684\u8a9e\u53e5\u7d50\u69cb\u5df2\u7d93\u6539\u8b8a\u3002<\/p>\n<p>--<\/p>\n<h2>\u5982\u4f55\u67e5\u9a57 SQL \u8a9e\u6cd5<\/h2>\n<p>\u4f7f\u7528\u9810\u8655\u7406\u60f3\u8981\u5f97\u5230\u6700\u5f8c\u57f7\u884c SQL \uff0c\u4e26\u4e0d\u80fd\u7c21\u55ae\u7684\u4f7f\u7528 echo print \u9019\u985e\u7684 function \uff0c\u7576\u7136 PDO \u672c\u8eab\u7684\u51fd\u6578\u4e5f\u6c92\u4ec0\u9ebc\u5375\u7528\uff0c\u4f8b\u5982\uff1a\u00a0<a href=\"http:\/\/php.net\/manual\/zh\/pdostatement.debugdumpparams.php\">PDOStatement::debugDumpParams<\/a><\/p>\n<p>\u552f\u4e00\u53ef\u4ee5\u4f9d\u9760\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u67e5\u770b MySQL \u8cc7\u6599\u5eab\u5be6\u969b\u57f7\u884c\u7684 SQL query<\/p>\n<p>\u63a8\u85a6\u7684\u65b9\u5f0f\u662f\u6253\u958b general_log \u5f8c\uff0c\u8a2d\u5b9a\u8f38\u51fa\u5728 mysql \u8cc7\u6599\u5eab\uff0c\u539f\u56e0\u662f\u96d6\u7136\u8aaa\u662f\u8cc7\u6599\u5eab\u4f46\u662f\u9810\u8a2d\u8cc7\u6599\u8868\u578b\u614b\u662f CSV \uff0c\u6240\u4ee5\u53ef\u4ee5\u540c\u6642\u6587\u5b57\u6a94\u6848\u548c\u8cc7\u6599\u5eab\u5169\u8005\u7684\u512a\u9ede<\/p>\n<pre class=\"lang:mysql decode:true\">SET GLOBAL log_output = 'TABLE';\r\nSET GLOBAL general_log = 'ON';<\/pre>\n<p>\u4f7f\u7528 tail \u4f86\u8ffd\u8e64\u6700\u5f8c\u66f4\u65b0\u7684\u8cc7\u6599<\/p>\n<pre class=\"lang:sh decode:true \">sudo tail -f \/var\/lib\/mysq\/mysql\/general_log.CSV<\/pre>\n<p>-<\/p>\n<p>\u9810\u8655\u7406\u7684\u597d\u8655\u00a0<a href=\"http:\/\/www.trueeyu.com\/?p=1382\">mysql prepare statement(mysql\u9884\u5904\u7406\uff09<\/a><\/p>\n<pre class=\"\">\u9884\u5904\u7406\u7684\u610f\u601d\u662f\u5148\u63d0\u4ea4sql\u8bed\u53e5\u5230mysql\u670d\u52a1\u7aef\uff0c\u6267\u884c\u9884\u7f16\u8bd1\uff0c\u5ba2\u6237\u7aef\u6267\u884csql\u8bed\u53e5\u65f6\uff0c\u53ea\u9700\u4e0a\u4f20\u8f93\u5165\u53c2\u6570\u5373\u53ef\u3002\r\n \u4ece 5.1\u5f00\u59cb\uff0cmysql\u652f\u6301\u670d\u52a1\u5668\u7aef\u7684Prepared Statements\uff0c\u4ed6\u4f7f\u7528\u5728client\/server\u66f4\u6709\u4f18\u52bf\u7684binary protocol\u3002\uff08mysql\u7684\u4f20\u7edf\u7684\u534f\u8bae\u4e2d\uff0c\u5728\u628a\u6570\u636e\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u524d\uff0c\u9700\u8981\u628a\u4e00\u5207\u6570\u636e\u90fd\u8f6c\u6362\u6210strings\uff0c\u8fd9\u6837\u5c31\u6bd4\u539f\u59cb\u6570\u636e\u5927\u5f88\u591a\uff0c\u6700\u540e\uff0c\u5728\u670d\u52a1\u5668\u7aef\uff0c\u8fd8\u5fc5\u987b\u628astring\u8f6c\u5316\u6210\u6b63\u786e\u7684\u6570\u636e\u683c\u5f0f\u800cbinary protocol\u53bb\u9664\u4e86\u8f6c\u6362\u7684\u5f00\u9500\uff0c\u5728\u88ab\u4f20\u8f93\u524d\uff0c\u6240\u6709\u7c7b\u578b\u90fd\u8f6c\u6362\u6210\u672c\u5730\u7684binary\u7c7b\u578b\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86cpu\u8f6c\u6362\u7684\u5f00\u9500\u8ddf\u7f51\u7edc\u7684\u4f7f\u7528\uff09<\/pre>\n<p>-<\/p>\n<h2>PHP \u5b98\u7db2\u6587\u4ef6<\/h2>\n<p>PHP \u7684\u512a\u9ede\u5c31\u662f\u5b98\u7db2\u64c1\u6709\u6700\u5b8c\u6574\u7684\u8aaa\u660e\u548c\u8cc7\u8a0a\uff0c\u4eca\u5929\u82b1\u4e86\u4e00\u9ede\u6642\u9593\u5728\u00a0<a href=\"http:\/\/php.net\/manual\/zh\/security.php\">\u5b89\u5168<\/a>\u00a0\u9019\u500b\u9805\u76ee\u4e0a\u9762\uff0c\u5c0d\u4e00\u4e9b\u5b89\u5168\u8b70\u984c\u6709\u4e86\u66f4\u597d\u7684\u8a8d\u8b58\u3002<\/p>\n<p>-<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"2345\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;4,776&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- PDO Statemen...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"2345\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;4,776&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[260],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2345"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2345"}],"version-history":[{"count":10,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2345\/revisions"}],"predecessor-version":[{"id":10960,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2345\/revisions\/10960"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}