public override void run() { //System.out.println(this+":run >"); /* * if(thread!=null){ return; } * thread=Thread.currentThread(); */ // Buffer buf=new Buffer(); Buffer buf = new Buffer(rmpsize); Packet packet = new Packet(buf); int i = -1; try { while (isConnected() && thread != null && io != null && io.ins != null) { i = io.ins.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20 // padding and mac ); if (i == 0) { continue; } if (i == -1) { eof(); break; } if (_close) { break; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); session.write(packet, this, i); } } catch (Exception e) { Console.WriteLine("# ChannelSession.run"); Console.WriteLine(e); } if (thread != null) { //lock(thread){ System.Threading.Monitor.PulseAll(this);/*thread.notifyAll();*/ } } thread = null; //System.out.println(this+":run <"); }
public override void run() { // thread=Thread.currentThread(); //System.out.println("rmpsize: "+rmpsize+", lmpsize: "+lmpsize); Buffer buf = new Buffer(rmpsize); // Buffer buf=new Buffer(lmpsize); Packet packet = new Packet(buf); int i = 0; try { while (isConnected() && thread != null && io != null && io.ins != null) { i = io.ins.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20 // padding and mac ); if (i <= 0) { eof(); break; } if (_close) { break; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); session.write(packet, this, i); } } catch { } disconnect(); //System.out.println("connect end"); /* * try{ * packet.reset(); * buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF); * buf.putInt(recipient); * session.write(packet); * } * catch(Exception e){ * } */ // close(); }
public virtual void connect() { if (!session.isConnected()) { throw new SshClientException("session is down"); } try { Buffer buf = new Buffer(100); Packet packet = new Packet(buf); // send // byte SSH_MSG_CHANNEL_OPEN(90) // string channel type // // uint32 sender channel // 0 // uint32 initial window size // 0x100000(65536) // uint32 maxmum packet size // 0x4000(16384) packet.reset(); buf.putByte((byte)90); buf.putString(this.type); buf.putInt(this.id); buf.putInt(this.lwsize); buf.putInt(this.lmpsize); session.write(packet); int retry = 1000; while (this.getRecipient() == -1 && session.isConnected() && retry > 0) { try { ThreadAux.Sleep(50); } catch (Exception) { } retry--; } if (!session.isConnected()) { throw new SshClientException("session is down"); } if (retry == 0) { throw new SshClientException("channel is not opened."); } connected = true; start(); } catch (Exception e) { connected = false; if (e is SshClientException) { throw (SshClientException)e; } } }
internal virtual void eof() { //System.Out.println("EOF!!!! "+this); //Thread.dumpStack(); if (_close) { return; } if (eof_local) { return; } eof_local = true; //close=eof; try { Buffer buf = new Buffer(100); Packet packet = new Packet(buf); packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF); buf.putInt(getRecipient()); session.write(packet); } catch (Exception) { //System.Out.println("Channel.eof"); //e.printStackTrace(); } /* * if(!isConnected()){ disconnect(); } */ }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytes("pty-req")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(StringAux.getBytes("vt100")); buf.putInt(80); buf.putInt(24); buf.putInt(640); buf.putInt(480); buf.putString(StringAux.getBytes("")); session.write(packet); }
//private byte[] f; public override void init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) { this.session = session; this.V_S = V_S; this.V_C = V_C; this.I_S = I_S; this.I_C = I_C; // sha=new SHA1(); // sha.init(); try { Type t = Type.GetType(session.getConfig("sha-1")); sha = (IHASH)(Activator.CreateInstance(t)); sha.Init(); } catch (Exception e) { Console.WriteLine(e); } buf = new LibSterileSSH.SecureShell.Buffer(); packet = new Packet(buf); try { Type t = Type.GetType(session.getConfig("dh")); dh = (IDH)(Activator.CreateInstance(t)); dh.init(); } catch (Exception e) { throw e; } packet.reset(); buf.putByte((byte)0x22); buf.putInt(min); buf.putInt(preferred); buf.putInt(max); session.write(packet); state = SSH_MSG_KEX_DH_GEX_GROUP; }
internal int shift(int len, int mac) { int s = len + 5 + 9; int pad = (-s) & 7; if (pad < 8) { pad += 8; } s += pad; s += mac; Array.Copy(buffer.buffer, len + 5 + 9, buffer.buffer, s, buffer.index - 5 - 9 - len); buffer.index = 10; buffer.putInt(len); buffer.index = len + 5 + 9; return(s); }
public override void run() { thread = ThreadAux.currentThread(); Buffer buf = new Buffer(rmpsize); Packet packet = new Packet(buf); int i = 0; try { while (thread != null && io != null && io.ins != null) { i = io.ins.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20 // padding and mac ); if (i <= 0) { eof(); break; } packet.reset(); if (_close) { break; } buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); session.write(packet, this, i); } } catch //(Exception e) { //System.out.println(e); } //thread=null; //eof(); disconnect(); }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "x11-req" // boolean want reply // 0 // boolean single connection // string x11 authentication protocol // "MIT-MAGIC-COOKIE-1". // string x11 authentication cookie // uint32 x11 screen number packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytes("x11-req")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putByte((byte)0); buf.putString(StringAux.getBytes("MIT-MAGIC-COOKIE-1")); buf.putString(ChannelX11.getFakedCookie(session)); buf.putInt(0); session.write(packet); }
internal static void delPort(Session session, int rport) { lock (pool) { Object[] foo = null; for (int i = 0; i < pool.Count; i++) { Object[] bar = (Object[])(pool[i]); if (bar[0] != session) { continue; } if (((int)bar[1]) != rport) { continue; } foo = bar; break; } if (foo == null) { return; } pool.Remove(foo); } Buffer buf = new Buffer(100); // ?? Packet packet = new Packet(buf); try { // byte SSH_MSG_GLOBAL_REQUEST 80 // string "cancel-tcpip-forward" // boolean want_reply // string address_to_bind (e.g. "127.0.0.1") // uint32 port number to bind packet.reset(); buf.putByte((byte)80 /*SSH_MSG_GLOBAL_REQUEST*/); buf.putString(StringAux.getBytesUTF8("cancel-tcpip-forward")); buf.putByte((byte)0); buf.putString(StringAux.getBytesUTF8("0.0.0.0")); buf.putInt(rport); session.write(packet); } catch //(Exception e) { // throw new JSchException(e.toString()); } }
public void request(Session session, AChannel channel) { Packet packet = session.packet; Buffer buf = session.buf; // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "exec" // boolean want reply // 0 // string command packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytesUTF8("exec")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(StringAux.getBytesUTF8(command)); session.write(packet); }
/* * http://www1.ietf.org/internet-drafts/draft-ietf-secsh-connect-24.txt * * 5.3 Closing a Channel * When a party will no longer send more data to a channel, it SHOULD * send SSH_MSG_CHANNEL_EOF. * * byte SSH_MSG_CHANNEL_EOF * uint32 recipient_channel * * No explicit response is sent to this message. However, the * application may send EOF to whatever is at the other end of the * channel. Note that the channel remains open after this message, and * more data may still be sent In the other direction. This message * does not consume window space and can be sent even if no window space * is available. * * When either party wishes to terminate the channel, it sends * SSH_MSG_CHANNEL_CLOSE. Upon receiving this message, a party MUST * send back a SSH_MSG_CHANNEL_CLOSE unless it has already sent this * message for the channel. The channel is considered closed for a * party when it has both sent and received SSH_MSG_CHANNEL_CLOSE, and * the party may then reuse the channel number. A party MAY send * SSH_MSG_CHANNEL_CLOSE without having sent or received * SSH_MSG_CHANNEL_EOF. * * byte SSH_MSG_CHANNEL_CLOSE * uint32 recipient_channel * * This message does not consume window space and can be sent even if no * window space is available. * * It is recommended that any data sent before this message is delivered * to the actual destination, if possible. */ internal virtual void close() { //System.Out.println("close!!!!"); if (_close) { return; } _close = true; try { Buffer buf = new Buffer(100); Packet packet = new Packet(buf); packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_CLOSE); buf.putInt(getRecipient()); session.write(packet); } catch (Exception) { //e.printStackTrace(); } }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); bool reply = waitForReply(); if (reply) { channel.reply = -1; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytesUTF8("subsystem")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(StringAux.getBytesUTF8(subsystem)); session.write(packet); if (reply) { while (channel.reply == -1) { try { ThreadAux.Sleep(10); } catch (System.Exception) { } } if (channel.reply == 0) { throw new SshClientException("failed to send subsystem request"); } } }
internal void dump(Buffer buf) { buf.putInt(flags); if ((flags & SSH_FILEXFER_ATTR_SIZE) != 0) { buf.putLong(size); } if ((flags & SSH_FILEXFER_ATTR_UIDGID) != 0) { buf.putInt(uid); buf.putInt(gid); } if ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { buf.putInt(permissions); } if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { buf.putInt(atime); } if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { buf.putInt(mtime); } if ((flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) { int count = extended.Length / 2; if (count > 0) { for (int i = 0; i < count; i++) { buf.putString(StringAux.getBytes(extended[i * 2])); buf.putString(StringAux.getBytes(extended[i * 2 + 1])); } } } }
public override bool start(Session session) { //System.out.println("UserAuthKeyboardInteractive: start"); Packet packet = session.packet; Buffer buf = session.buf; String username = session.username; String dest = username + "@" + session.host; if (session.port != 22) { dest += (":" + session.port); } bool cancel = false; byte[] _username = null; try { _username = System.Text.Encoding.UTF8.GetBytes(username); } catch { _username = StringAux.getBytes(username); } while (true) { // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name (ISO-10646 UTF-8, as defined in [RFC-2279]) // string service name (US-ASCII) "ssh-userauth" ? "ssh-connection" // string "keyboard-interactive" (US-ASCII) // string language tag (as defined in [RFC-3066]) // string submethods (ISO-10646 UTF-8) packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST); buf.putString(_username); buf.putString(StringAux.getBytes("ssh-connection")); //buf.putString("ssh-userauth".getBytes()); buf.putString(StringAux.getBytes("keyboard-interactive")); buf.putString(StringAux.getBytes("")); buf.putString(StringAux.getBytes("")); session.write(packet); bool firsttime = true; loop: while (true) { // receive // byte SSH_MSG_USERAUTH_SUCCESS(52) // string service name try { buf = session.read(buf); } catch (SshClientException e) { e.GetType(); return(false); } catch (System.IO.IOException e) { e.GetType(); return(false); } //System.out.println("read: 52 ? "+ buf.buffer[5]); if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS) { return(true); } if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] _message = buf.getString(); byte[] lang = buf.getString(); String message = null; try { message = StringAux.getStringUTF8(_message); } catch { message = StringAux.getString(_message); } if (userinfo != null) { userinfo.showMessage(message); } goto loop; } if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] foo = buf.getString(); int partial_success = buf.getByte(); // System.out.println(new String(foo)+ // " partial_success:"+(partial_success!=0)); if (partial_success != 0) { throw new SshClientPartialAuthException(StringAux.getString(foo)); } if (firsttime) { throw new SshClientException("USERAUTH KI is not supported"); //return false; //cancel=true; // ?? } break; } if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_INFO_REQUEST) { firsttime = false; buf.getInt(); buf.getByte(); buf.getByte(); String name = StringAux.getString(buf.getString()); String instruction = StringAux.getString(buf.getString()); String languate_tag = StringAux.getString(buf.getString()); int num = buf.getInt(); //System.out.println("name: "+name); //System.out.println("instruction: "+instruction); //System.out.println("lang: "+languate_tag); //System.out.println("num: "+num); String[] prompt = new String[num]; bool[] echo = new bool[num]; for (int i = 0; i < num; i++) { prompt[i] = StringAux.getString(buf.getString()); echo[i] = (buf.getByte() != 0); //System.out.println(" "+prompt[i]+","+echo[i]); } String[] response = null; if (num > 0 || (name.Length > 0 || instruction.Length > 0) ) { IUIKeyboardInteractive kbi = (IUIKeyboardInteractive)userinfo; if (userinfo != null) { response = kbi.promptKeyboardInteractive(dest, name, instruction, prompt, echo); } } // byte SSH_MSG_USERAUTH_INFO_RESPONSE(61) // int num-responses // string response[1] (ISO-10646 UTF-8) // ... // string response[num-responses] (ISO-10646 UTF-8) //if(response!=null) //System.out.println("response.length="+response.length); //else //System.out.println("response is null"); packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_INFO_RESPONSE); if (num > 0 && (response == null || // cancel num != response.Length)) { buf.putInt(0); if (response == null) { cancel = true; } } else { buf.putInt(num); for (int i = 0; i < num; i++) { //System.out.println("response: |"+response[i]+"| <- replace here with **** if you need"); buf.putString(StringAux.getBytes(response[i])); } } session.write(packet); if (cancel) { break; } //System.out.println("continue loop"); goto loop; } //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")"); return(false); } if (cancel) { throw new SshClientAuthCancelException("keyboard-interactive"); //break; } } //return false; }
public override void connect() { try { if (!session.isConnected()) { throw new SshClientException("session is down"); } Buffer buf = new Buffer(150); Packet packet = new Packet(buf); // send // byte SSH_MSG_CHANNEL_OPEN(90) // string channel type // // uint32 sender channel // 0 // uint32 initial window size // 0x100000(65536) // uint32 maxmum packet size // 0x4000(16384) packet.reset(); buf.putByte((byte)90); buf.putString(StringAux.getBytes("direct-tcpip")); buf.putInt(id); buf.putInt(lwsize); buf.putInt(lmpsize); buf.putString(StringAux.getBytes(host)); buf.putInt(port); buf.putString(StringAux.getBytes(originator_IP_address)); buf.putInt(originator_port); session.write(packet); int retry = 1000; try { while (this.getRecipient() == -1 && session.isConnected() && retry > 0 && !_eof_remote) { //Thread.sleep(500); ThreadAux.Sleep(50); retry--; } } catch { } if (!session.isConnected()) { throw new SshClientException("session is down"); } if (retry == 0 || this._eof_remote) { throw new SshClientException("channel is not opened."); } /* * if(this.eof_remote){ // failed to open * disconnect(); * return; * } */ connected = true; thread = new ThreadAux(this); thread.start(); } catch (Exception e) { io.close(); io = null; AChannel.del(this); if (e is SshClientException) { throw (SshClientException)e; } } }
public override bool next(LibSterileSSH.SecureShell.Buffer _buf) { int i, j; bool result = false; switch (state) { case SSH_MSG_KEX_DH_GEX_GROUP: // byte SSH_MSG_KEX_DH_GEX_GROUP(31) // mpint p, safe prime // mpint g, generator for subgroup in GF (p) _buf.getInt(); _buf.getByte(); j = _buf.getByte(); if (j != 31) { Console.WriteLine("type: must be 31 " + j); result = false; } p = _buf.getMPInt(); g = _buf.getMPInt(); /* * for(int iii=0; iii<p.length; iii++){ * System.out.println("0x"+Integer.toHexString(p[iii]&0xff)+","); * } * System.out.println(""); * for(int iii=0; iii<g.length; iii++){ * System.out.println("0x"+Integer.toHexString(g[iii]&0xff)+","); * } */ dh.setP(p); dh.setG(g); // The client responds with: // byte SSH_MSG_KEX_DH_GEX_INIT(32) // mpint e <- g^x mod p // x is a random number (1 < x < (p-1)/2) e = dh.getE(); packet.reset(); buf.putByte((byte)0x20); buf.putMPInt(e); session.write(packet); state = SSH_MSG_KEX_DH_GEX_REPLY; result = true; break; case SSH_MSG_KEX_DH_GEX_REPLY: // The server responds with: // byte SSH_MSG_KEX_DH_GEX_REPLY(33) // string server public host key and certificates (K_S) // mpint f // string signature of H j = _buf.getInt(); j = _buf.getByte(); j = _buf.getByte(); if (j != 33) { Console.WriteLine("type: must be 33 " + j); result = false; } K_S = _buf.getString(); // K_S is server_key_blob, which includes .... // string ssh-dss // impint p of dsa // impint q of dsa // impint g of dsa // impint pub_key of dsa //System.out.print("K_S: "); dump(K_S, 0, K_S.length); byte[] f = _buf.getMPInt(); byte[] sig_of_H = _buf.getString(); dh.setF(f); K = dh.getK(); //The hash H is computed as the HASH hash of the concatenation of the //following: // string V_C, the client's version string (CR and NL excluded) // string V_S, the server's version string (CR and NL excluded) // string I_C, the payload of the client's SSH_MSG_KEXINIT // string I_S, the payload of the server's SSH_MSG_KEXINIT // string K_S, the host key // uint32 min, minimal size in bits of an acceptable group // uint32 n, preferred size in bits of the group the server should send // uint32 max, maximal size in bits of an acceptable group // mpint p, safe prime // mpint g, generator for subgroup // mpint e, exchange value sent by the client // mpint f, exchange value sent by the server // mpint K, the shared secret // This value is called the exchange hash, and it is used to authenti- // cate the key exchange. buf.reset(); buf.putString(V_C); buf.putString(V_S); buf.putString(I_C); buf.putString(I_S); buf.putString(K_S); buf.putInt(min); buf.putInt(preferred); buf.putInt(max); buf.putMPInt(p); buf.putMPInt(g); buf.putMPInt(e); buf.putMPInt(f); buf.putMPInt(K); byte[] foo = new byte[buf.getLength()]; buf.getByte(foo); sha.update(foo, 0, foo.Length); H = sha.digest(); // System.out.print("H -> "); dump(H, 0, H.length); i = 0; j = 0; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); String alg = StringAux.getString(K_S, i, j); i += j; if (alg.Equals("ssh-rsa")) { byte[] tmp; byte[] ee; byte[] n; type = RSA; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; ee = tmp; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; n = tmp; // SignatureRSA sig=new SignatureRSA(); // sig.init(); ISignatureRSA sig = null; try { Type t = Type.GetType(session.getConfig("signature.rsa")); sig = (ISignatureRSA)(Activator.CreateInstance(t)); sig.init(); } catch (Exception eee) { Console.WriteLine(eee); } sig.setPubKey(ee, n); sig.update(H); result = sig.verify(sig_of_H); } else if (alg.Equals("ssh-dss")) { byte[] q = null; byte[] tmp; type = DSS; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; p = tmp; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; q = tmp; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; g = tmp; j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) | ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff); tmp = new byte[j]; Array.Copy(K_S, i, tmp, 0, j); i += j; f = tmp; // SignatureDSA sig=new SignatureDSA(); // sig.init(); ISignatureDSA sig = null; try { Type t = Type.GetType(session.getConfig("signature.dss")); sig = (ISignatureDSA)(Activator.CreateInstance(t)); sig.init(); } catch (Exception ee) { Console.WriteLine(ee); } sig.setPubKey(f, p, q, g); sig.update(H); result = sig.verify(sig_of_H); } else { Console.WriteLine("unknow alg"); } state = STATE_END; break; } return(result); }