⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sshio.java

📁 java 平台 telnet 繁体中文版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      break;    case SSH_SMSG_SUCCESS:      if(debug > 0)        System.out.println("SSH_SMSG_SUCCESS (last packet was "+lastPacketSentType+")");      if (lastPacketSentType==SSH_CMSG_SESSION_KEY) {         //we have succefully sent the session key !! (at last :-) )	Send_SSH_CMSG_USER();	break;      }      if (lastPacketSentType==SSH_CMSG_USER) {         // authentication is NOT needed for this user 	Send_SSH_CMSG_REQUEST_PTY(); //request a pseudo-terminal	return "\nEmpty password login.\r\n".getBytes();      }      if (lastPacketSentType==SSH_CMSG_AUTH_PASSWORD) {// password correct !!!	//yahoo	System.out.println("login succesful");	//now we have to start the interactive session ...	Send_SSH_CMSG_REQUEST_PTY(); //request a pseudo-terminal	return "\nLogin & password accepted\r\n".getBytes();      }      if (lastPacketSentType==SSH_CMSG_REQUEST_PTY) {// pty accepted !!        /* we can send data with a pty accepted ... no need for a shell. */        cansenddata = true;	if (dataToSend != null ) {	    Send_SSH_CMSG_STDIN_DATA(dataToSend);	    dataToSend = null;	}	Send_SSH_CMSG_EXEC_SHELL(); //we start a shell	break;      }      if (lastPacketSentType==SSH_CMSG_EXEC_SHELL) {// shell is running ...        /* empty */      }      break;    case SSH_SMSG_FAILURE:      if (lastPacketSentType==SSH_CMSG_AUTH_PASSWORD) {// password incorrect ???        System.out.println("failed to log in");        disconnect();	return "\nLogin & password not accepted\r\n".getBytes();      }      if (lastPacketSentType==SSH_CMSG_USER) {         // authentication is needed for the given user 	// (in most cases that's true)	Send_SSH_CMSG_AUTH_PASSWORD();	break;      }      if (lastPacketSentType==SSH_CMSG_REQUEST_PTY) {// pty not accepted !!	break;      }      break;    case SSH_SMSG_STDOUT_DATA: //receive some data from the server      str = SshMisc.getString(0, packetData);      return str.getBytes();    case SSH_SMSG_STDERR_DATA: //receive some error data from the server      //	if(debug > 1)       str = "Error : " + SshMisc.getString(0, packetData);      System.out.println("SshIO.handlePacket : " + "STDERR_DATA " + str );      //StrByte = new byte[str.length()];      //str.getBytes(0, str.length(),	StrByte, 0);      //return(StrByte);      return str.getBytes();			    case SSH_SMSG_EXITSTATUS: //sent by the server to indicate that                               // the client program has terminated.	//32-bit int   exit status of the command      int value = (packetData[0]<<24)+(packetData[1]<<16)                + (packetData[2]<<8)+(packetData[3]);      Send_SSH_CMSG_EXIT_CONFIRMATION();      System.out.println("SshIO : Exit status " + value );      disconnect();      break;   case SSH_MSG_DEBUG:      str = SshMisc.getString(0, packetData);      if(debug > 0) {        System.out.println("SshIO.handlePacket : " + " DEBUG " + str );      // bad bad bad bad bad. We should not do actions in DEBUG messages,      // but apparently some SSH demons does not send SSH_SMSG_FAILURE for      // just USER CMS./*      if(lastPacketSentType==SSH_CMSG_USER) {         Send_SSH_CMSG_AUTH_PASSWORD();        break;      }*/        return str.getBytes();      }       return "".getBytes();    default:       System.err.print("SshIO.handlePacket : Packet Type unknown: "+packetType);      break;		    }//	switch(b)    return null;  } // handlePacket  private void sendPacket(SshPacket packet) throws IOException {      write(packet.getBytes());     lastPacketSentType = packet.getType();  }   //   // Send_SSH_CMSG_SESSION_KEY  // Create :   // the session_id,   // the session_key,   // the Xored session_key,   // the double_encrypted session key  // send SSH_CMSG_SESSION_KEY  // Turn the encryption on (initialise the block cipher)  //  private byte[] Send_SSH_CMSG_SESSION_KEY(byte[] anti_spoofing_cookie,					   byte[] server_key_public_modulus,					   byte[] host_key_public_modulus,					   byte[] supported_ciphers_mask,					   byte[] server_key_public_exponent,					   byte[] host_key_public_exponent)	throws IOException {		    String str;    int boffset;		    byte cipher_types;		//encryption types    byte[] session_key;		//mp-int    // create the session id    //	session_id = md5(hostkey->n || servkey->n || cookie) //protocol V 1.5. (we use this one)    //	session_id = md5(servkey->n || hostkey->n || cookie) //protocol V 1.1.(Why is it different ??)    //		    byte[] session_id_byte = new byte[host_key_public_modulus.length+server_key_public_modulus.length+anti_spoofing_cookie.length];		System.arraycopy(host_key_public_modulus,0,session_id_byte,0,host_key_public_modulus.length);		System.arraycopy(server_key_public_modulus,0,session_id_byte,host_key_public_modulus.length,server_key_public_modulus.length);		System.arraycopy(anti_spoofing_cookie,0,session_id_byte,host_key_public_modulus.length+server_key_public_modulus.length,anti_spoofing_cookie.length);    byte[] hash_md5 = md5.hash(session_id_byte);     //	SSH_CMSG_SESSION_KEY : Sent by the client    //	    1 byte       cipher_type (must be one of the supported values)    // 	    8 bytes      anti_spoofing_cookie (must match data sent by the server)    //	    mp-int       double-encrypted session key (uses the session-id)    //	    32-bit int   protocol_flags    //    if ((supported_ciphers_mask[3] & (byte)(1<<SSH_CIPHER_BLOWFISH))!=0) {      cipher_types = (byte)SSH_CIPHER_BLOWFISH;      cipher_type = "Blowfish";    } else {      if ((supported_ciphers_mask[3] & (1<<SSH_CIPHER_IDEA)) != 0) {	cipher_types = (byte)SSH_CIPHER_IDEA;	cipher_type = "IDEA";      } else {	if ((supported_ciphers_mask[3] & (1<<SSH_CIPHER_3DES)) != 0) {	  cipher_types = (byte)SSH_CIPHER_3DES;	  cipher_type = "DES3";	} else {	  System.err.println("SshIO: remote server does not supported IDEA or BlowFish, support cypher mask is "+supported_ciphers_mask[3]+".\n");	  disconnect();	  return "\rRemote server does not support IDEA / Blowfish blockcipher, closing connection.\r\n".getBytes();	}      }    }    System.out.println("SshIO: Using "+cipher_type+" blockcipher.\n");    // 	anti_spoofing_cookie : the same     //      double_encrypted_session_key :    //		32 bytes of random bits    //		Xor the 16 first bytes with the session-id    //		encrypt with the server_key_public (small) then the host_key_public(big) using RSA.    //		    //32 bytes of random bits    byte[] random_bits1 = new byte[16], random_bits2 = new byte[16];		    /// java.util.Date date = new java.util.Date(); ////the number of milliseconds since January 1, 1970, 00:00:00 GMT.     //Math.random()   a pseudorandom double between 0.0 and 1.0.     random_bits2 = random_bits1 =      // md5.hash("" + Math.random() * (new java.util.Date()).getDate());      md5.hash("" + Math.random() * (new java.util.Date()).getTime());    random_bits1 = md5.hash(SshMisc.addArrayOfBytes(md5.hash(password+login),                             random_bits1));    random_bits2 = md5.hash(SshMisc.addArrayOfBytes(md5.hash(password+login),                            random_bits2));    // SecureRandom random = new java.security.SecureRandom(random_bits1); //no supported by netscape :-(    // random.nextBytes(random_bits1);    // random.nextBytes(random_bits2);    session_key  = SshMisc.addArrayOfBytes(random_bits1,random_bits2);    //Xor the 16 first bytes with the session-id    byte[] session_keyXored  = SshMisc.XORArrayOfBytes(random_bits1,hash_md5);    session_keyXored = SshMisc.addArrayOfBytes(session_keyXored, random_bits2);    //We encrypt now!!    byte[] encrypted_session_key =       SshCrypto.encrypteRSAPkcs1Twice(session_keyXored,				      server_key_public_exponent,				      server_key_public_modulus,				      host_key_public_exponent,				      host_key_public_modulus);    //	protocol_flags :protocol extension   cf. page 18    byte[] protocol_flags = new  byte[4];	//32-bit int    protocol_flags [0] = protocol_flags [1] =     protocol_flags [2] = protocol_flags [3] = 0;    //set the data    int length = 1 + //cipher_type      anti_spoofing_cookie.length +       encrypted_session_key.length +      protocol_flags.length;    byte[] data = new byte[length];    boffset = 0;    data[boffset++] = (byte) cipher_types;    for (int i=0; i<8; i++)	data[boffset++] = anti_spoofing_cookie[i];    for (int i=0; i<encrypted_session_key.length; i++)	data[boffset++] = encrypted_session_key[i];    for (int i=0; i<4; i++)	data[boffset++] = protocol_flags[i];		    //set the packet_type    byte packet_type = SSH_CMSG_SESSION_KEY;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    crypto = new SshCrypto(cipher_type,session_key);    encryption=true;    return null;  } //Send_SSH_CMSG_SESSION_KEY  /**   * SSH_CMSG_USER   * string   user login name on server   */  private byte[] Send_SSH_CMSG_USER() throws IOException {    if(debug > 0) System.err.println("Send_SSH_CMSG_USER("+login+")");    byte[] data = SshMisc.createString(login);    byte packet_type = SSH_CMSG_USER;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    return null;  } //Send_SSH_CMSG_USER  /**   * Send_SSH_CMSG_AUTH_PASSWORD   * string   user password   */  private byte[] Send_SSH_CMSG_AUTH_PASSWORD() throws IOException {    byte[] data = SshMisc.createString(password);     byte packet_type = SSH_CMSG_AUTH_PASSWORD;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    return null;  } //Send_SSH_CMSG_AUTH_PASSWORD  /**   * Send_SSH_CMSG_EXEC_SHELL   *  (no arguments)   *   Starts a shell (command interpreter), and enters interactive   *   session mode.   */  private byte[] Send_SSH_CMSG_EXEC_SHELL() throws IOException {    byte[] data = null;    byte packet_type = SSH_CMSG_EXEC_SHELL;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    lastPacketSentType = packet_type;    return null;  } //Send_SSH_CMSG_EXEC_SHELL  /**   * Send_SSH_CMSG_STDIN_DATA   *     */  private byte[] Send_SSH_CMSG_STDIN_DATA(String str) throws IOException {    byte[] data = SshMisc.createString(str);    byte packet_type = SSH_CMSG_STDIN_DATA;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    return null;  } //Send_SSH_CMSG_STDIN_DATA	  /**   * Send_SSH_CMSG_REQUEST_PTY   *   string       TERM environment variable value (e.g. vt100)   *   32-bit int   terminal height, rows (e.g., 24)   *   32-bit int   terminal width, columns (e.g., 80)   *   32-bit int   terminal width, pixels (0 if no graphics) (e.g., 480)   */  private byte[] Send_SSH_CMSG_REQUEST_PTY() throws IOException {    byte[] termType = SshMisc.createString(getTerminalType()); // terminal type    byte[] row = new byte[4];    row[3] = (byte) 24;			// terminal height    byte[] col = new byte[4];    col[3] = (byte) 80;			// terminal width    byte[] XPixels = new byte[4];    //XPixels[2] = (byte)(480/256);    //XPixels[3] = (byte)(480%256);    byte[] YPixels = new byte[4];    //YPixels[2] = (byte)(640/256);    //YPixels[3] = (byte)(640%256);	    byte[] terminalModes = new byte[1];    terminalModes[0] =  0;    byte [] data = new byte[termType.length + 4*4 + terminalModes.length];    int offset = 0;    for (int i=0; i<termType.length; i++) data[offset++] = termType[i];    for (int i=0; i<4; i++) data[offset++] = row[i];    for (int i=0; i<4; i++) data[offset++] = col[i];    for (int i=0; i<4; i++) data[offset++] = XPixels[i];    for (int i=0; i<4; i++) data[offset++] = YPixels[i];    for (int i=0; i<terminalModes.length; i++)       data[offset++] = terminalModes[i];    byte packet_type =  SSH_CMSG_REQUEST_PTY;    SshPacket packet = createPacket(packet_type, data);    sendPacket(packet);    return null;  } //Send_SSH_CMSG_REQUEST_PTY  private byte[] Send_SSH_CMSG_EXIT_CONFIRMATION() throws IOException {    byte packet_type = SSH_CMSG_EXIT_CONFIRMATION;    SshPacket packet = createPacket(packet_type, null);    sendPacket(packet);    return null;  }}// class SshIO

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -