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

actually fix java stderr output

This commit is contained in:
Tim W 2020-05-26 15:47:06 +08:00
parent 873ad69319
commit 91acbc8f2e
2 changed files with 46 additions and 16 deletions

View File

@ -12,7 +12,7 @@ import java.io.OutputStream;
public class Channel {
public final Meterpreter meterpreter;
private final InputStream in;
protected final InputStream in;
private final OutputStream out;
private final int id;
protected boolean active = false, closed = false, waiting = false;
@ -26,11 +26,25 @@ public class Channel {
* @param out Output stream of the channel, if any
*/
public Channel(Meterpreter meterpreter, InputStream in, OutputStream out) {
this(meterpreter, in, out, false);
}
/**
* Create a new "generic" channel.
*
* @param meterpreter The meterpreter this channel should be assigned to.
* @param in Input stream of the channel
* @param out Output stream of the channel, if any
* @param hasStderr True if the channel has stderr output
*/
public Channel(Meterpreter meterpreter, InputStream in, OutputStream out, boolean hasStderr) {
this.meterpreter = meterpreter;
this.id = meterpreter.registerChannel(this);
this.in = in;
this.out = out;
new InteractThread(in).start();
if (!hasStderr) {
new InteractThread(in).start();
}
}
/**

View File

@ -20,10 +20,10 @@ 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, process.getInputStream(), process.getOutputStream(), true);
this.err = process.getErrorStream();
new StderrThread(err).start();
this.process = process;
new StdoutStderrThread(this.in, this.err).start();
}
/**
@ -51,28 +51,44 @@ public class ProcessChannel extends Channel {
super.close();
}
class StderrThread extends Thread {
private final InputStream stream;
class StdoutStderrThread extends Thread {
private final InputStream in;
private final InputStream err;
public StderrThread(InputStream stream) {
this.stream = stream;
public StdoutStderrThread(InputStream in, InputStream err) {
this.in = in;
this.err = err;
}
public void run() {
try {
byte[] buffer = new byte[1024*1024];
int len;
while ((len = stream.read(buffer)) != -1) {
if (len == 0)
continue;
byte[] data = new byte[len];
System.arraycopy(buffer, 0, data, 0, len);
handleInteract(data);
int inlen;
int errlen;
while (true) {
if ((inlen = in.read(buffer)) != -1) {
if (inlen > 0)
writeBuf(buffer, inlen);
}
if ((errlen = err.read(buffer)) != -1) {
if (errlen > 0)
writeBuf(buffer, errlen);
}
if (inlen == -1 && errlen == -1) {
break;
}
}
handleInteract(null);
} catch (Throwable t) {
t.printStackTrace(meterpreter.getErrorStream());
}
}
private void writeBuf(byte[] buffer, int len) throws IOException, InterruptedException {
byte[] data = new byte[len];
System.arraycopy(buffer, 0, data, 0, len);
handleInteract(data);
}
}
}