hierynomus / sshj

ssh, scp and sftp for java
Apache License 2.0
2.51k stars 601 forks source link

Restricted Shell: Broken transport; encountered EOF #482

Open Jaywann opened 5 years ago

Jaywann commented 5 years ago

I am trying to execute a simple "help" command on a hardware appliance with sshj. As it is common for such appliances only a restricted shell is available. The code used was taken from net/schmizz/sshj/examples/ with no modification other than using the password auth and seetting a PromiscuousVerifier().

The version used is 0.26.0 pulled from maven.

Using the net/schmizz/sshj/examples/Exec.java example results in the following error:

[main] INFO n.s.s.t.random.BouncyCastleRandom - Generating random seed from SecureRandom. [main] INFO n.s.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0.26.0 [main] INFO n.s.sshj.transport.TransportImpl - Server identity string: SSH-1.99-IPSSH-6.7.0 [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [reader] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG c.hierynomus.sshj.transport.kex.DHG - Sending SSH_MSG_KEXDH_INIT [reader] DEBUG c.hierynomus.sshj.transport.kex.DHG - Received SSH_MSG_KEXDH_REPLY [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to true [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] ERROR n.s.sshj.transport.TransportImpl - Dying because - Broken transport; encountered EOF net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF at net.schmizz.sshj.transport.Reader.run(Reader.java:57) [reader] INFO n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN [main] ERROR net.schmizz.concurrent.Promise - <> woke to: net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] INFO n.s.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME Exception in thread "main" net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF at net.schmizz.sshj.transport.Reader.run(Reader.java:57)

The error seams to occur at net/schmizz/sshj/Reader.run because imp.read() returning -1.

 while (!isInterrupted()) {
                int read;
                try {
                    **read = inp.read(recvbuf, 0, needed);**
                } catch(SocketTimeoutException e) {
                    if (isInterrupted()) {
                        throw e;
                    }
                    continue;
                }
                if (read == -1) {
                    throw new TransportException("Broken transport; encountered EOF");
                } else {
                    needed = decoder.received(recvbuf, read);
                }

The odd thing is that when using the example code from net/schmizz/sshj/examples/RudimentaryPTY.java the restricted command prompt appears and commands can be entered:

[main] INFO n.s.s.t.random.BouncyCastleRandom - Generating random seed from SecureRandom. [main] INFO n.s.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0.26.0 [main] INFO n.s.sshj.transport.TransportImpl - Server identity string: SSH-1.99-IPSSH-6.7.0 [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [reader] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG c.hierynomus.sshj.transport.kex.DHG - Sending SSH_MSG_KEXDH_INIT [reader] DEBUG c.hierynomus.sshj.transport.kex.DHG - Received SSH_MSG_KEXDH_REPLY [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Setting <> to null [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <> to true [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<chan#0 / open>> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / open>> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<chan#0 / chanreq for pty-req>> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / chanreq for pty-req>> to SOME [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<chan#0 / chanreq for shell>> [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / chanreq for shell>> to SOME

CLI> help

Using the RudimentaryPTY.java to manually write an InputStream to the StreamCopier appears to work at first but then encounters the same error.

InputStream stream = new ByteArrayInputStream("help\n".getBytes());
new StreamCopier(stream, shell.getOutputStream(), LoggerFactory.DEFAULT)
        .bufSize(shell.getRemoteMaxPacketSize()).copy();

[main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<chan#0 / close>> CLI> help clear disk-error - Clear the error information stored in disks. [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / close>> to SOME [stdout] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [stderr] DEBUG net.schmizz.concurrent.Promise - Setting <> to SOME [reader] ERROR n.s.sshj.transport.TransportImpl - Dying because - Broken transport; encountered EOF net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF at net.schmizz.sshj.transport.Reader.run(Reader.java:57) [reader] INFO n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN

Any help in determining the root cause of this is greatly appreciated.

gsajwan commented 5 years ago

I got this issue with AIX as my target host. using sshj-0.21.1 fixed my issue

softmade-timobruentjen commented 3 years ago

Got the same error, is there any workaround then downgrading? Using 0.31 from Maven. After successful upload, getting this Error Message. Target System is a Windows Server Running an SFTP Service.


Update: I think i got it to work, was using File instead of FileSystemFile