2011-10-26 3 views
5

나는 현재 안드로이드 응용 프로그램을 암호를 사용하여 ssh를 통해 내 라우터에 연결하고 있습니다. 키를 사용할 수 있도록 개선하기 위해 찾고 있지만 실제 문제가 있습니다. 내가 안다면 안드로이드에 포함 된 bouncycastle의 버전은 절름 거리는 버전이고이 ssh 키로 인해 jsch에서 작동하지 않습니다. 더 완벽한 구현이라고 주장하는 스폰지 캐슬을 살펴 보았습니다. 아래는 bouncycastle, Auth Fail 사용과 기본적으로 동일한 결과입니다.안드로이드에 bouncycastle 대신 스폰지 캐슬이있는 Jsch


10-26 18:18:23.528: INFO/System.out(10642): Log(jsch,1): Connecting to 192.168.88.1 port 22 
10-26 18:18:23.538: INFO/System.out(10642): Log(jsch,1): Connection established 
10-26 18:18:23.548: INFO/System.out(10642): Log(jsch,1): Remote version string: SSH-2.0-ROSSSH 
10-26 18:18:23.548: INFO/System.out(10642): Log(jsch,1): Local version string: SSH-2.0-JSCH-0.1.44 
10-26 18:18:23.548: INFO/System.out(10642): Log(jsch,1): CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256 
10-26 18:18:23.618: INFO/System.out(10642): Log(jsch,1): SSH_MSG_KEXINIT sent 
10-26 18:18:23.618: INFO/System.out(10642): Log(jsch,1): SSH_MSG_KEXINIT received 
10-26 18:18:23.628: INFO/System.out(10642): Log(jsch,1): kex: server->client aes128-cbc hmac-md5 none 
10-26 18:18:23.628: INFO/System.out(10642): Log(jsch,1): kex: client->server aes128-cbc hmac-md5 none 
10-26 18:18:23.688: INFO/System.out(10642): Log(jsch,1): SSH_MSG_KEXDH_INIT sent 
10-26 18:18:23.688: INFO/System.out(10642): Log(jsch,1): expecting SSH_MSG_KEXDH_REPLY 
10-26 18:18:24.058: INFO/System.out(10642): Log(jsch,1): ssh_dss_verify: signature true 
10-26 18:18:24.058: INFO/System.out(10642): Log(jsch,2): Permanently added '192.168.88.1' (DSA) to the list of known hosts. 
10-26 18:18:24.058: INFO/System.out(10642): Log(jsch,1): SSH_MSG_NEWKEYS sent 
10-26 18:18:24.058: INFO/System.out(10642): Log(jsch,1): SSH_MSG_NEWKEYS received 
10-26 18:18:24.078: INFO/System.out(10642): Log(jsch,1): SSH_MSG_SERVICE_REQUEST sent 
10-26 18:18:24.088: INFO/System.out(10642): Log(jsch,1): SSH_MSG_SERVICE_ACCEPT received 
10-26 18:18:24.108: INFO/System.out(10642): Log(jsch,1): Authentications that can continue: publickey,keyboard-interactive,password 
10-26 18:18:24.108: INFO/System.out(10642): Log(jsch,1): Next authentication method: publickey 
10-26 18:18:24.108: INFO/System.out(10642): Log(jsch,1): Authentications that can continue: password 
10-26 18:18:24.118: INFO/System.out(10642): Log(jsch,1): Next authentication method: password 
10-26 18:18:24.128: INFO/System.out(10642): Log(jsch,1): Disconnecting from 192.168.88.1 port 22 
10-26 18:18:24.138: WARN/System.err(10642): com.jcraft.jsch.JSchException: Auth fail 

jsch의 중요한 로깅 정보가 없으므로 잘못된 것을 해결할 수 있습니다.



static { 
     Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider()); 
    } 

가 한 OnCreate에서 내가



     Security.removeProvider("BC"); 

가 그럼 난 신원을 추가 원래 bouncycastle 제공자를 제거하고있어 바로 연결하기 전에 :

는 나는이 꽤 표준 코드를 사용하고 생각



     jsch.addIdentity(key_filename); 

    Properties sshProp = new Properties(); 
    sshProp.put("StrictHostKeyChecking", "no"); 
    session.setConfig(sshProp); 
    session.connect(); 

이 작업을 성공적으로 완료 한 사람이 있습니까? 나는 무언가를 잊고 있니?

편집 포인트 추가 정보 :

나는 코멘트에 내가 지금은 SSHD 버전 OpenSSH_5.3p1 데비안 3ubuntu7

의 키와 테스트를 전환 할 때 키가 시도조차되지 않는 것을 의심하고있어 말했듯



debug1: userauth-request for user root service ssh-connection method publickey 
debug1: attempt 1 failures 0 
debug2: input_userauth_request: try method publickey 
debug1: test whether pkalg/pkblob are acceptable 
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048 
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048 
debug1: temporarily_use_uid: 0/0 (e=0/0) 
debug1: trying public key file /root/.ssh/authorized_keys 
debug1: restore_uid: 0/0 
debug1: temporarily_use_uid: 0/0 (e=0/0) 
debug1: trying public key file /root/.ssh/authorized_keys2 
debug1: fd 4 clearing O_NONBLOCK 
debug1: matching key found: file /root/.ssh/authorized_keys2, line 2 
PC에서 나는 키 방법이 사용되는 것을 볼 수있는 반면

debug2: mac_setup: found hmac-md5 
debug1: kex: client->server aes128-ctr hmac-md5 none 
debug2: mac_setup: found hmac-md5 
debug1: kex: server->client aes128-ctr hmac-md5 none 
debug2: dh_gen_key: priv key bits set: 122/256 
debug2: bits set: 519/1024 
debug1: expecting SSH2_MSG_KEXDH_INIT 
debug2: bits set: 537/1024 
debug2: kex_derive_keys 
debug2: set_newkeys: mode 1 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug2: monitor_read: 5 used once, disabling now 
debug2: set_newkeys: mode 0 
debug1: SSH2_MSG_NEWKEYS received 
debug1: KEX done 
debug1: userauth-request for user root service ssh-connection method none 
debug1: attempt 0 failures 0 
debug2: parse_server_config: config reprocess config len 638 
debug2: input_userauth_request: setting up authctxt for root 
debug2: input_userauth_request: try method none 
debug2: monitor_read: 7 used once, disabling now 
debug1: PAM: initializing for "root" 
debug1: PAM: setting PAM_RHOST to "nexus" 
debug1: PAM: setting PAM_TTY to "ssh" 
debug2: monitor_read: 50 used once, disabling now 
debug2: monitor_read: 3 used once, disabling now 
Failed none for root from 192.168.88.31 port 37807 ssh2 
debug1: userauth-request for user root service ssh-connection method password 
debug1: attempt 1 failures 0 
debug2: input_userauth_request: try method password 
debug1: PAM: password authentication failed for root: Authentication failure 
Failed password for root from 192.168.88.31 port 37807 ssh2 
Received disconnect from 192.168.88.31: 3: com.jcraft.jsch.JSchException: Auth fail 
debug1: do_cleanup 
debug1: do_cleanup 
debug1: PAM: cleanup 

나는 키를 사용하려는 시도를 볼 수 있습니다. 아래 코드는 테스트를 위해 사용하는 코드이며 꽤 기능적이지는 않습니다. 나는 끔찍한 알고 있지만 암호가 authType이 == AUTHENTICATION_METHOD_KEY



public static String testSSHCommand (String username, String password, String hostname, int port, String command, int authtype) throws Exception {  

    JSch jsch = new JSch(); 

    JSch.setLogger(new Logger() { 
      public boolean isEnabled(int i) { 
       return true; 
      } 

      public void log(int i, String s) { 
       System.out.println("Log(jsch," + i + "): " + s); 
      } 
    }); 


    if (authtype != AUTHENTICATION_METHOD_PASSWORD) { 
     Log.v("AUTHMETHOD","authmethod was "+authtype+" with key filename of "+password); 
     jsch.addIdentity(password); 
    } 

    Session session = jsch.getSession(username, hostname, 22); 

    if (authtype != AUTHENTICATION_METHOD_KEY) { 
     session.setPassword(password); 
    } 


    Properties prop = new Properties(); 
    prop.put("StrictHostKeyChecking", "no"); 
    session.setConfig(prop); 

    session.connect(); 

    if (session.isConnected()) { 
     ChannelExec channelssh = (ChannelExec)   
     session.openChannel("exec");  
     ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     channelssh.setOutputStream(os); 
     channelssh.setCommand(command); 
     channelssh.connect();   
     channelssh.disconnect(); 

     return os.toString(); 
    } else { 
     return ""; 
    } 
} 
+0

잘못된 공개 키를 사용하고있는 것 같습니다. 동일한 키 파일 (public + private)을 사용하는 로그인이 일반 PC Java 프로그램에서 작동합니까? –

+0

나는 PC 기반의 자바 개발을하지 않지만 키는 괜찮다. 테스트 할 서버 박스에 ssh로 내 PC에서 사용할 수있다. 나는 실제로 키가 시도조차되지 않는다고 의심하기 시작했습니다.(편집 참조) – Fuzzy

+0

이것을 일반적인 PC Java와 비교하면 문제를 격리하는 데 도움이됩니다. 즉, 암호화 공급자 또는 사용자 코드와 관련이있는 경우입니다. –

답변

1

나는 class with main method로 포장하여 방법을 테스트 할 때 사용하는 키의 파일 이름과 경로를 포함하지,이와 함께, 그것은 정상 작동 Java 6 JRE (BouncyCastle 없음), JSch-0.1.45 (더 많은 로그 출력이있는 수정 된 버전) 및 "OpenSSH_5.8p1 Debian-1ubuntu3"sshd.

나는 session.disconnect()을 추가했으며 일부는 코드에 잔다.

여기에는 Android가 없으므로 차이가 있는지 확인해야합니다.

여기 내 로컬 출력 :

authmethod was 2 with key filename of /home/paulo/.ssh/id_rsa 
Log(jsch,1): Connecting to localhost port 22 
Log(jsch,1): Connection established 
Log(jsch,1): Remote version string: SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3 
Log(jsch,1): Local version string: SSH-2.0-JSCH-0.1.45 
Log(jsch,1): CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256 
Log(jsch,1): aes256-ctr is not available. 
Log(jsch,1): aes192-ctr is not available. 
Log(jsch,1): aes256-cbc is not available. 
Log(jsch,1): aes192-cbc is not available. 
Log(jsch,1): arcfour256 is not available. 
Log(jsch,1): CheckKexes: diffie-hellman-group14-sha1 
Log(jsch,1): diffie-hellman-group14-sha1 is not available. 
Log(jsch,1): SSH_MSG_KEXINIT sent 
Log(jsch,1): SSH_MSG_KEXINIT received 
Log(jsch,1): kex: server->client aes128-ctr hmac-md5 none 
Log(jsch,1): kex: client->server aes128-ctr hmac-md5 none 
Log(jsch,1): SSH_MSG_KEXDH_INIT sent 
Log(jsch,1): expecting SSH_MSG_KEXDH_REPLY 
Log(jsch,1): ssh_rsa_verify: signature true 
Log(jsch,2): Permanently added 'localhost' (RSA) to the list of known hosts. 
Log(jsch,1): SSH_MSG_NEWKEYS sent 
Log(jsch,1): SSH_MSG_NEWKEYS received 
Log(jsch,1): SSH_MSG_SERVICE_REQUEST sent 
Log(jsch,1): SSH_MSG_SERVICE_ACCEPT received 
Log(jsch,1): Authentications that can continue: publickey,keyboard-interactive,password 
Log(jsch,1): Next authentication method: publickey 
Log(jsch,1): Authentication succeeded (publickey). 
Log(jsch,0): packet received, type: 91 
Log(jsch,1): SSH_MSG_CHANNEL_OPEN_CONFIRMATION received, channel: 0 
Log(jsch,0): packet received, type: 94 
Log(jsch,1): SSH_MSG_CHANNEL_DATA received, channel: 0, len: 13 
Log(jsch,0): packet received, type: 96 
Log(jsch,1): SSH_MSG_CHANNEL_EOF received, channel: 0 
Log(jsch,0): packet received, type: 98 
Log(jsch,1): SSH_MSG_CHANNEL_REQUEST received, channel: 0, type: [[email protected], want reply: false 
Log(jsch,0): packet received, type: 97 
Log(jsch,1): SSH_MSG_CHANNEL_CLOSE received, channel: 0 
Log(jsch,1): Disconnecting from localhost port 22 
Log(jsch,1): Caught an exception, leaving main loop due to Socket closed 
Output: »Hallo, Welt! 
« 

(나는 인수 paulo -key /home/paulo/.ssh/id_rsa localhost 22 hello와 프로그램이라고, 그래서 이것은 내 구성 로케일 있기 때문에, 대상 시스템에 기본 안녕하세요 프로그램을 실행하고, 독일어한다.)

+0

그래서 jsch가 안드로이드의 키와 작동하지 않는다고 말하고 있습니까? – Fuzzy

+0

나는 그 이유를 모르겠다 고 말하고있다. 비교를 위해 컴퓨터에서 키를 사용하여 테스트 프로그램을 시험해 볼 수도 있습니다. (그리고 더 많은 정보를 얻었는지 확인하기 위해 링크 된 GitHub 저장소에 JSch 디버깅 출력 버전을 테스트 해보십시오.) –

+0

오늘 내가 극도로 어둡게 느껴졌습니다. 업데이트 된 jar 파일을 링크에서 찾을 수 없습니다. , 직접 링크를 제공 할 수 있습니까? – Fuzzy