使用ORACLE 的JAVA儲存過程修改作業系統密碼

guoge發表於2016-01-22

問題:

有一臺windows 2003 ORACLE11G 資料庫伺服器,可以連線資料庫,但忘記了administartor 的口令。由於硬碟都做了RAID, 使用WINPE 啟動後,看不到C盤。網上的一種做法是使用支援RAID 驅動的WINPE 引導,但伺服器時限久,已找不到RAID驅動。

 

思路:

   使用ORACLEJAVA儲存過程執行作業系統的命令。

過程:

SQL> conn sys/oracle@hisserver as sysdba

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as SYS


SQL> create or replace and compile java source named oscmd as

  2  import java.io.*;

  3  import java.lang.*;

  4  public class OSCmd extends Object {

  5      public static int OSCmd(String args) {

  6          Runtime rt = Runtime.getRuntime();

  7          int rc = -1;

  8          try {

  9              Process p = rt.exec(args);

 10              int bufSize = 4096;

 11              BufferedInputStream bis = new BufferedInputStream(p.getInputStream(), bufSize);

 12          int len;

 13          byte buffer[] = new byte[bufSize];

 14          while ((len = bis.read(buffer, 0, bufSize)) != -1)

 15                  System.out.write(buffer, 0, len);

 16              rc = p.waitFor();

 17          } catch (Exception e) {

 18              e.printStackTrace();

 19              rc = -1;

 20          } finally {

 21              return rc;

 22          }

 23      }

 24  }

 25  /

 

Java created

SQL> CREATE OR REPLACE FUNCTION Run_Cmd(p_Cmd IN VARCHAR2) RETURN NUMBER

  2  AS LANGUAGE JAVA NAME

  3   'OSCmd.OSCmd(java.lang.String) return integer';

  4  /

 

Function created

 

SQL> set serveroutput on

SQL> exec dbms_java.set_output(100000);

 

PL/SQL procedure successfully completed

SQL> variable x number;

SQL> exec :x := RUN_CMD('net user administrator 2);

 

PL/SQL procedure successfully completed

x

---------

0

             

如果使用普通使用者,則需給其授一定許可權:

exec dbms_java.grant_permission('USERNAME','SYS:java.io.FilePermission','<<ALL FILE>>','read,write,execute,delete');

exec dbms_java.grant_permission('USERNAME','SYS:java.lang.RuntimePermission','*','WriteFileDescriptor');

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9036/viewspace-1980921/,如需轉載,請註明出處,否則將追究法律責任。

相關文章