1
mirror of https://github.com/rapid7/metasploit-payloads synced 2024-11-20 14:39:22 +01:00

Land #421, fix java stderr output in cmd_exec

This commit is contained in:
Tim W 2020-07-09 11:22:47 +08:00
commit 2d91d49a08
No known key found for this signature in database
GPG Key ID: 217FBA50ABBAABEF
2 changed files with 33 additions and 23 deletions

View File

@ -32,16 +32,21 @@ public class Channel {
this.id = meterpreter.registerChannel(this);
this.in = in;
this.out = out;
new InteractThread(in).start();
if (in != null) {
new InteractThread(in, true).start();
}
}
/**
* Close this channel and deregister it from the meterpreter.
*/
public synchronized void close() throws IOException {
in.close();
if (out != null)
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
meterpreter.channelClosed(id);
active = false;
closed = true;
@ -160,9 +165,11 @@ public class Channel {
*/
protected class InteractThread extends Thread {
private final InputStream stream;
private final boolean handleClose;
public InteractThread(InputStream stream) {
public InteractThread(InputStream stream, boolean handleClose) {
this.stream = stream;
this.handleClose = handleClose;
}
public void run() {
@ -176,7 +183,9 @@ public class Channel {
System.arraycopy(buffer, 0, data, 0, len);
handleInteract(data);
}
handleInteract(null);
if (handleClose) {
handleInteract(null);
}
} catch (Throwable t) {
t.printStackTrace(meterpreter.getErrorStream());
}

View File

@ -12,6 +12,7 @@ import java.io.PrintStream;
public class ProcessChannel extends Channel {
private final Process process;
private final InputStream inputStream;
private final InputStream err;
/**
@ -21,10 +22,13 @@ public class ProcessChannel extends Channel {
* @param process Process of the channel
*/
public ProcessChannel(Meterpreter meterpreter, Process process) {
super(meterpreter, process.getInputStream(), process.getOutputStream());
this.process = process;
super(meterpreter, null, process.getOutputStream());
this.inputStream = process.getInputStream();
this.err = process.getErrorStream();
new StderrThread(err, meterpreter.getErrorStream()).start();
this.process = process;
Thread stdinThread = new InteractThread(this.inputStream, false);
Thread stderrThread = new InteractThread(this.err, false);
new CloseThread(stdinThread, stderrThread).start();
}
/**
@ -48,30 +52,27 @@ public class ProcessChannel extends Channel {
public void close() throws IOException {
process.destroy();
inputStream.close();
err.close();
super.close();
}
class StderrThread extends Thread {
private final InputStream inputStream;
private final PrintStream outputStream;
class CloseThread extends Thread {
private final Thread stdinThread;
private final Thread stderrThread;
public StderrThread(InputStream inputStream, PrintStream outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
public CloseThread(Thread stdinThread, Thread stderrThread) {
this.stdinThread = stdinThread;
this.stderrThread = stderrThread;
}
public void run() {
try {
byte[] buffer = new byte[1024*1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
if (len == 0)
continue;
byte[] data = new byte[len];
System.arraycopy(buffer, 0, data, 0, len);
handleInteract(data);
}
stdinThread.start();
stderrThread.start();
stdinThread.join();
stderrThread.join();
handleInteract(null);
} catch (Throwable t) {
t.printStackTrace(this.outputStream);
}