设为首页 收藏本站
查看: 483|回复: 0

[经验分享] Mysql注入***

[复制链接]

尚未签到

发表于 2018-9-28 13:07:21 | 显示全部楼层 |阅读模式
去年sql注入很火,最近好像php注入开始流行了,其实都是最基本的知识的应用,没什么难度~~  i d know about you, oh most knowledgable of sql users, but when i learnt how to build a database driven website from various tutorials around the internet, i never came across the phrase "sql injection"... in fact, i never came across any sql security c at all. the purpose of this article is to inform the sql programmer of the dangers of sql injecti and ways to potentially minimize the severity of any exploits in your code.
  all the examples in this article are going to be using php. the reason for this choice is that php seems to be the server-side solution of choice - it's free and powerful. i'm also using mysql, as it's all i've ever used. i think all the examples here should be applicable across different database servers, but i have menti the particular database servers affected where necessary. i assume knowledge of php database functi
  to start with, i'll create a database and a couple of tables, so i can dem how a certain security risk could be exploited, and to what means. rather than make a graphical representation of these tables, i'll write it in insertion sql statements, so you can stick them on your own server and test it yourself. after all, the best way to learn is to get stuck in.
  代码
  ######################
  # database structure #
  ######################
  create database sqlsecdemo;
  create table users (int userid not null auto_increment primary key, text username not null, text password not null);
  insert into users set username="netjester", password="blahblah123";
  insert into users set username="some password="letmein";
  insert into users set username="mrstheplague", password="god";

  create table messages (int messageid not null auto_increment primary key, int>
  insert into messages set>
  insert into messages set>
  insert into messages set>  create table somemoreinfo (int infoid not null auto_increment primary key, text info);
  insert into somemoreinfo set info="some info here";
  insert into somemoreinfo set info="even more information.";
  insert into somemoreinfo set info="information overload.";
  now we have this foundati i can start off by showing you the most basic of attacks. c this php statement:
  代码

  $result=pg_query($db,"select message from messages where>  this may appear in a script that displays all messages posted by a certain user, reached from a page with a list of users to choose from. the url for a page which displays all of some posted messages would be something like http://www.sqlinjection.com/postedby.php?uid=2 so the full sql statement, with substituted variables would be:
  代码

  select message from messages where>  now c this url: http://www.sqlinjection.com/postedby.php?u...20messages%20--
  if you were to replace the %20's with spaces, as %20 is simply a url-encoded space, you would see that the sql statement sent to the server now reads like this:

  select message from messages where>
  the semicolon ends the first sql statement, and the server is now ready for another... which we provide. the -- is the sql comment syntax - all text following that will be ignored by the server. this would be included by an attacker in case there are more search clauses or ordering directives and such following "where>  mysql is not vulnerable to this attack, as it  allows 1 statement per query, for exactly this reason. postgresql is vulnerable however, and probably others too, as they allow multiple sql statements per query.

  protecting against this type of attack is fairly simple, and can be d in two ways.>
  $result=pg_query($db,"select message from messages where>  so, when our attacker tries the url above, the sql statements becomes:

  select message from messages where>
  the result of this is to put all the users input into a string, which will then be compared against>  http://www.sqlinjection.com/postedby.php?u...20messages%20--
  our sql statement then becomes:

  select message from messages where>  so it looks like we've made absolutely no progress at all. they key is to add backslashes to the user's input before each occcurrence of ' or ". this way, a user can no l open or close a string. with this implemented, the above url generates the sql statement below:

  select message from messages where>
  because of the escaping of the quote, the string compared to the>
  another precaution that can be taken against this kind of attack is simple data validation. as>  代码
  function nj_isinteger($checkstring) {
  if($checkstring!='' && ereg("^[0-9]*$",$checkstring)) {
  return true;
  } else {
  return false;
  }
  }
  if(!nj_isinteger($_get['uid']))
  exit('user inputted uid was not an integer.');
  so there we have a good basis for some slightly more advanced sql injection attacks.
  now, imagine you wanted a page to select and display everything from a particular table. which table it is the user decides. so it might look like this:
  代码
  mysql_query($db,"select * from ".$_get['table']);
  here, injection can be achieved in much the same way. if the user gave the script the value "users where username='netjester'", they would be presented with my password. which is bad.
  the way to protect against injection further than the table to select from, you again, add quotes around the value you're adding, and escape all quotes input by the user.
  however, an important lesson is to be learnt here. if you wish to give a user a choice such as which table will be queried, decide which tables you want to allow the user to access, and then, for example, put them in a php array, and use the particular array element required using a number provided by the user. that way, a user can  indirectly insert those table names you specify in the array.

  ok, now how about we return to the postedby.php>  代码
  
  on first glance,  could be forgiven for thinking that even though this page allows a user to perform pretty much any select query they like on our database, they still wouldn't be able to see their results. this is, however, not true, because of sql's as keyword. if you do not know what as is for, it will be in any good sql tutorial. so, c this url:
  http://www.sqlinjection.com/big/security/h...0from%20users--
  because the password field has been given the name "message", every time the script goes to print a supposedly public message, it actually prints  of our users passwords!
  the rule here is again to delimit any user input with quotes, and escape any quotes within that input. better yet, give the user an indirect choice of what to add to the sql statement, so you still have 100% c of what goes in and out of the server. here's an example of a php script that uses indirection.
  代码
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-603325-1-1.html 上篇帖子: php数据库之mysql (where 、order By 、 Update) 下篇帖子: Mysql性能测试 Mysql性能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表