Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Windows oddity in java's process launch library #724

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,65 @@ public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket respons
return ERROR_SUCCESS;
}

// On Windows, Java quote-escapes _some_ arguments (like those with spaces), but doesn't deal correctly with some
// edge cases; e.g. empty strings, strings that already have quotes.
protected String escapeArg(String arg) {
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
smashery marked this conversation as resolved.
Show resolved Hide resolved
if (arg == "") {
smashery marked this conversation as resolved.
Show resolved Hide resolved
return "\"\"";
} else {
StringBuilder sb = new StringBuilder();
int numBackslashes = 0;
boolean needsQuoting = false;
for (int i = 0; i < arg.length(); i++) {
char c = arg.charAt(i);
switch (c) {
case '"': {
for (int nb = 0; nb < numBackslashes; nb++) {
sb.append('\\');
}
numBackslashes = 0;
sb.append('\\');
break;
}
case '\\': {
numBackslashes++;
break;
}
case ' ':
case '\t':
case (char)11:
{
needsQuoting = true;
numBackslashes = 0;
break;
}
default: {
numBackslashes = 0;
break;
}
}
sb.append(c);
}
if (needsQuoting) {
for (int nb = 0; nb < numBackslashes; nb++) {
sb.append('\\');
}
return "\"" + sb.toString() + "\"";
}
return sb.toString();
}
} else {
return arg;
}
}

protected Process execute(String cmd, ArrayList<String> args) throws IOException {
ArrayList<String> cmdAndArgs = new ArrayList<String>();
cmdAndArgs.add(cmd);
cmdAndArgs.addAll(args);
for (String arg : args) {
cmdAndArgs.add(escapeArg(arg));
}
ProcessBuilder builder = new ProcessBuilder(cmdAndArgs);
builder.directory(Loader.getCWD());
return builder.start();
Expand Down
Loading