1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-02-22 03:19:04 +01:00

Land #345, implement expand_path on java/android

This commit is contained in:
Brent Cook 2019-06-02 14:09:42 -05:00
commit b874178a34
No known key found for this signature in database
GPG Key ID: 1FFAA0B24B708F96

View File

@ -1,6 +1,8 @@
package com.metasploit.meterpreter.stdapi;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.metasploit.meterpreter.Meterpreter;
import com.metasploit.meterpreter.TLVPacket;
@ -10,6 +12,37 @@ import com.metasploit.meterpreter.command.NotYetImplementedCommand;
public class stdapi_fs_file_expand_path implements Command {
/**
* Pattern for capturing variables. Either $xyz, ${xyz} or ${a.b} but not $a.b, while ignoring "$$"
*/
private static final Pattern VARIABLE = Pattern.compile("\\$([A-Za-z0-9_]+|\\{[A-Za-z0-9_.]+\\}|\\%[A-Za-z0-9_.]+\\%|\\$)");
private static String expandPath(String s) {
int idx=0;
while (true) {
Matcher m = VARIABLE.matcher(s);
if (!m.find(idx)) {
return s;
}
String key = m.group().substring(1);
String value;
if (key.charAt(0) == '$') {
value = "$";
} else {
if (key.charAt(0) == '{' || key.charAt(0) == '%') key = key.substring(1,key.length()-1);
value = System.getenv(key);
}
if (value == null) {
value = "";
}
s = s.substring(0, m.start()) + value + s.substring(m.end());
idx = m.start() + value.length();
}
}
public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket response) throws Exception {
String path = request.getStringValue(TLVType.TLV_TYPE_FILE_PATH);
if (path.equals("%COMSPEC%")) {
@ -19,7 +52,8 @@ public class stdapi_fs_file_expand_path implements Command {
response.add(TLVType.TLV_TYPE_FILE_PATH, System.getenv("TEMP"));
return ERROR_SUCCESS;
} else {
return NotYetImplementedCommand.INSTANCE.execute(meterpreter, request, response);
response.add(TLVType.TLV_TYPE_FILE_PATH, expandPath(path));
return ERROR_SUCCESS;
}
}