{"id":1407,"date":"2012-02-07T22:55:49","date_gmt":"2012-02-07T14:55:49","guid":{"rendered":"http:\/\/doophp.sinaapp.com\/?p=1407"},"modified":"2012-02-07T23:02:44","modified_gmt":"2012-02-07T15:02:44","slug":"prevent-sql-injection-attack","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/prevent-sql-injection-attack.html","title":{"rendered":"\u3010\u8bd1\u3011\u5982\u4f55\u9632\u8303SQL\u6ce8\u5165\u653b\u51fb(\u4ee3\u7801\u793a\u4f8b)"},"content":{"rendered":"
\u672c\u6587\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u793a\u4f8b\u89e3\u91ca\u57fa\u672c\u7684SQL\u6ce8\u5165\u653b\u51fb\uff0c\u5e76\u63d0\u4f9b\u65b9\u6cd5\u5982\u4f55\u53bb\u5904\u7406\u8fd9\u4e9b\u653b\u51fb\u95ee\u9898\u3002<\/p>\n
\u5982\u9898\u6240\u793a\uff0c\u9ed1\u5ba2\u80fd\u591f\u901a\u8fc7SQL\u67e5\u8be2\u8bed\u53e5\u8fdb\u884c\u653b\u51fb\u3002\u4e00\u4e9b\u7f51\u7ad9\u7684\u5f00\u53d1\u8005\u8fd8\u4e0d\u592a\u4e86\u89e3\u9ed1\u5ba2\u662f\u5982\u4f55\u901a\u8fc7SQL\u67e5\u8be2\u6765\u653b\u51fb\u7f51\u7ad9\u7684\u3002\u5982\u679c\u7a0b\u5e8f\u5b8c\u5168\u76f8\u4fe1\u7528\u6237\u7684\u8f93\u5165\u4fe1\u606f\uff0c\u5e76\u4e0d\u505a\u4e00\u4e9b\u9002\u5f53\u7684\u8fc7\u6ee4\u5904\u7406\uff0cSQL\u6ce8\u5165\u5b8c\u5168\u53ef\u4ee5\u5b9e\u73b0\uff0cSQL\u6ce8\u5165\u7684\u601d\u8def\u5c31\u662f\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u4e00\u4e9b\u4e0d\u88ab\u671f\u671b\u7684SQL\u67e5\u8be2\u3002
\n
\n\u793a\u4f8b\u6d4b\u8bd5\u73af\u5883\uff1a<\/p>\n
\u5f53\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u540e\uff0c\u5c06\u4f1a\u901a\u8fc7HTTP_POST\u65b9\u6cd5\u63d0\u4ea4\u5230login.php
\n[code]
\n<?php
\n$Host= ‘192.168.1.8’;
\n$Dbname= ‘john’;
\n$User= ‘john’;
\n$Password= ‘xxx’;
\n$Schema = ‘test’; <\/p>\n
$Conection_string="host=$Host dbname=$Dbname user=$User password=$Password"; <\/p>\n
\/* Connect with database asking for a new connection*\/
\n$Connect=pg_connect($Conection_string,$PGSQL_CONNECT_FORCE_NEW); <\/p>\n
\/* Error checking the connection string *\/
\nif (!$Connect) {
\n echo "Database Connection Failure";
\n exit;
\n} <\/p>\n
$query="SELECT * from $Schema.users where user_name=’".$_POST[‘user_name’]."’ and password=’".$_POST[‘pass_word’]."’;"; <\/p>\n
$result=pg_query($Connect,$query);
\n$rows = pg_num_rows($result);
\nif ($rows) {
\n echo "Login Success";
\n}
\nelse {
\n echo "Login Failed";
\n}
\n?>
\n[\/code]
\n\u4e0a\u9762\u7a0b\u5e8f\u7684\u7b2c19\u884c\u4ee3\u7801\u5c06\u4f1a\u88ab\u5229\u7528\u505aSQL\u6ce8\u5165\u653b\u51fb(i.e \u8fd9\u4e00\u884c\u4ece\u8fd9\u91cc\u5f00\u59cb\u201c$query=”SELECT * …”\u201d).\u8fd9\u4e2a\u5730\u65b9\u7684SQL\u67e5\u8be2\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u628a\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u5339\u914d.\u5b83\u5c06\u4f1a\u8fdb\u884c\u5408\u9002\u7684\u5904\u7406\uff0c\u5982\u679c\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u53ef\u7528\u7684\u8bdd\u3002\u4f46\u662f\u9ed1\u5ba2\u53ef\u4ee5\u5de7\u5999\u5730\u901a\u8fc7\u8fd9\u6837\u8f93\u5165\u6765\u201c\u8499\u6df7\u8fc7\u5173\u201d\uff1a<\/p>\n
\u9ed1\u5ba2\u5728username\u5b57\u6bb5\u8f93\u5165\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n
‘ or 1=1;–<\/p><\/blockquote>\n
\u4e4b\u540e\u8ba9\u5bc6\u7801\u5b57\u6bb5\u4e3a\u7a7a.<\/p>\n
\u5f53\u9ed1\u5ba2\u63d0\u4ea4\u8f93\u5165\u540e\uff0clogin.php\u6587\u4ef6\u5c06\u5bf9\u8f93\u5165\u505a\u5982\u4e0b\u5904\u7406\uff0c\u5e76\u751f\u6210\u5982\u4e0bSQL\u8bed\u53e5\uff1a<\/p>\n
SELECT * from test.members where user_name=” or 1=1;–‘ and password=”;<\/p><\/blockquote>\n
\u4e0a\u9762\u7684SQL\u662f\u4e00\u4e2a\u5408\u6cd5\u7684SQL\u67e5\u8be2\u8bed\u53e5\u3002\u5728Postgresql\u5b57\u7b26\u201c-\u201d\u662f\u6ce8\u91ca\u6807\u8bc6\uff0c\u56e0\u6b64\u201c–\u201d\u4e4b\u540e\u7684\u8bed\u53e5\u5c06\u88ab\u5f53\u4f5c\u53e5\u91ca\u5904\u7406\uff0c\u6700\u540e\u6267\u884c\u8bed\u53e5\u5c06\u4f1a\u662f\uff1a<\/p>\n
select * from test.members where user_name=” or 1=1;<\/p><\/blockquote>\n
\u8fd9\u5c06\u4f1a\u8fd4\u56de\u201ctrue”\u5e76\u8fd4\u56de\u767b\u9646\u6210\u529f\u4fe1\u606f\u3002<\/p>\n
\u5982\u679c\u9ed1\u5ba2\u77e5\u9053\u6570\u636e\u5e93\u7684\u8868\u540d\u79f0\uff0c\u751a\u81f3\u53ef\u4ee5\u5220\u9664\u6574\u4e2a\u8868\uff0c\u901a\u8fc7\u5411”username”\u5b57\u6bb5\u8f93\u5165\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n
‘;drop table test.lop;–<\/p><\/blockquote>\n
\u4e00\u4e9b\u767b\u9646\u7a0b\u5e8f\u7ecf\u5e38\u4f1a\u505a\u8fd9\u6837\u7684\u5904\u7406\uff1a<\/p>\n
\n
- \u5c06\u5bc6\u7801\u8fdb\u884cmd5\u52a0\u5bc6\u8fd0\u7b97\u540e\u5b58\u5165\u5728\u6570\u636e\u5e93\u4e2d<\/li>\n
- \u9996\u5148\u6839\u636e\u7528\u6237\u8f93\u5165\u7684username\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u5bf9\u5e94\u7684username\u548cpassword\u5b57\u6bb5\u503c<\/li>\n
- \u7136\u540e\u5bf9\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u8fdb\u884cmd5\u52a0\u5bc6\u5904\u7406\uff0c\u5e76\u8ddf\u6570\u636e\u5e93\u4e2d\u7684password\u8fdb\u884c\u6bd4\u5bf9<\/li>\n
- \u5982\u679c\u6bd4\u5bf9\u5339\u914d\u5219\u767b\u9646\u9a8c\u8bc1\u901a\u8fc7<\/li>\n<\/ul>\n
\u4e0b\u9762\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u901a\u8fc7SQL\u6ce8\u5165\u7ed5\u5f00\u767b\u9646\u9a8c\u8bc1<\/div>\n[code]
\n$query="SELECT user_name,password from $Schema.members where user_name=’".$_POST[‘user_name’]."’;";<\/p>\n$result=pg_query($Connect,$query);<\/p>\n
$row=pg_fetch_array($result,NULL,PGSQL_ASSOC);<\/p>\n
# Find the md5 for the user supplied password.
\n$user_pass = md5($_POST[‘pass_word’]);<\/p>\nif(strcmp($user_pass,$row[‘password’])!=0) {
\n echo "Login Failed\\n";
\n}
\nelse {
\n echo "Login Success\\n";
\n}
\n[\/code]<\/p>\n\u73b0\u5728\u5728username\u5b57\u6bb5\u8f93\u5165\u5982\u4e0b\u5185\u5bb9<\/p>\n
‘ UNION ALL SELECT ‘laksh’,’202cb962ac59075b964b07152d234b70<\/p><\/blockquote>\n
\u5728password\u5b57\u6bb5\u8f93\u5165\u201c123\u201d\u5e76\u63d0\u4ea4.md5(123)\u7ed3\u679c\u662f\u201c202cb962ac59075b964b07152d234b70\u201d<\/p>\n
\u73b0\u5728SQL\u8bed\u53e5\u6700\u540e\u5c06\u4f1a\u662f\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n
SELECT user_name,password from test.members where user_name=” UNION ALL SELECT ‘laksh’,’202cb962ac59075b964b07152d234b70′;<\/p><\/blockquote>\n
\u4e0a\u9762\u7684SQL\u8bed\u53e5\u6267\u884c\u540e\u5c06\u4f1a\u4ee5\u8fd4\u56de”laksh”\u4f5c\u4e3ausername\uff0c\u201c202cb962ac59075b964b07152d234b70\u201d\u4f5c\u4e3apassword\u3002<\/p>\n
\u6211\u4eec\u4e5f\u4f1a\u63d0\u4ea4”123″\u4f5c\u4e3apassword\u5b57\u6bb5\u503c\uff0c\u6b64\u65f6\u8f93\u5165\u4e0e\u6570\u636e\u5e93\u8fd4\u56de\u5339\u914d\uff0c\u767b\u9646\u9a8c\u8bc1\u901a\u8fc7\u3002<\/p>\n
\u4e0a\u9762\u4e24\u4e2a\u53ea\u662f\u4e24\u4e2aSQL\u653b\u51fb\u793a\u4f8b\uff0c\u8fd8\u6709\u5f88\u591a\u53d8\u79cd\u3002<\/p>\n
\u4e0b\u9762\u6211\u4eec\u5927\u6982\u5217\u4e3e\u5e38\u89c1SQL\u6ce8\u5165\u653b\u51fb\u9884\u9632\u624b\u6bb5\uff0c\u53ef\u6700\u5927\u7a0b\u5e8f\u907f\u514dSQL\u6ce8\u5165\u4fb5\u6270\uff1a<\/p>\n
\n
- \u4e25\u683c\u7c7b\u578b\u68c0\u67e5\uff08\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff09<\/li>\n
- \u68c0\u67e5\u7528\u6237\u8f93\u5165username\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd<\/li>\n
- \u8f6c\u4e49\u6216\u8fc7\u6ee4\u7528\u6237\u8f93\u5165\u7684\u7279\u6b8a\u5b57\u7b26<\/li>\n
- \u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u6267\u884c\u67e5\u8be2<\/li>\n
- \u4e00\u6761\u8bed\u53e5\u4e2d\u4e0d\u5141\u8bb8\u591a\u6761\u67e5\u8be2<\/li>\n
- \u4e0d\u8981\u8ba9”syntax errors”\u9519\u8bef\u63d0\u793a\u6cc4\u6f0f\u6570\u636e\u5e93\u4fe1\u606f<\/li>\n<\/ul>\n
\u539f\u6587\u5730\u5740\uff1ahttp:\/\/www.thegeekstuff.com\/2012\/02\/sql-injection-attacks\/<\/a><\/p>\n