使用带有双因素身份验证的phpseclib无法连接到SFTP(用户名/密码和私钥/密码)

时间:2015-05-25 10:27:56

标签: sftp phpseclib

我正在尝试使用phpseclib和双因素身份验证发送带有SFTP的文件,但我无法连接到服务器。我也尝试过只使用用户名和密码,并使用私钥和密码来获得相同的结果。使用WINSCP,我可以毫无问题地连接。

这是我的代码:

foreach(glob(LIBRARIES_DIR.'PHPSecLib/Crypt/*.php') as $file){
    require_once $file;
}

require_once LIBRARIES_DIR.'PHPSecLib/Math/BigInteger.php';
require_once LIBRARIES_DIR.'PHPSecLib/Net/SFTP.php';
require_once LIBRARIES_DIR.'PHPSecLib/Net/SSH2.php';


define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX); // or NET_SFTP_LOG_SIMPLE

$key = new Crypt_RSA();
$key->setPassword('********');

if(!$key->loadKey(file_get_contents('path/to/key'))) {
    exit ("private key loading failed!");
} else {
    echo "private key loading OK!\n";
    echo "$key";
}

$sftp = new Net_SFTP('********', 22);

if (!$sftp->login($config['username'], $key) && !$sftp->login($config['username'], $config['password'])) {
    echo 'Failed';
    print_r($sftp->getLog());
} else {
    echo 'Success';
}

print_r($sftp->nlist());

print_r($sftp->getSFTPLog());

它加载的私钥很好:

    -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: ******** -----END RSA PRIVATE KEY-----

SSH日志:

<- 
00000000 53:48:2d:32:2e:30:2d:43:65:72:62:65:72:75:73:46 SH-2.0-CerberusF 
00000010 54:50:53:65:72:76:65:72:5f:36:2e:30:0d:0a TPServer_6.0.. 

-> 
00000000 53:48:2d:32:2e:30:2d:70:68:70:73:65:63:6c:69:62 SH-2.0-phpseclib 
00000010 5f:30:2e:33:20:28:62:63:6d:61:74:68:29:0d:0a _0.3 (bcmath).. 

<- 
NET_SSH2_MSG_KEXINIT (since last: 0.2047, network: 0.0675s) 
00000000 be:cb:02:4d:5e:35:7b:cd:b6:fb:a1:8b:ae:55:78:2a ...M^5{......Ux* 
00000010 00:00:00:b7:65:63:64:68:2d:73:68:61:32:2d:6e:69 ....ecdh-sha2-ni 
00000020 73:74:70:32:35:36:2c:65:63:64:68:2d:73:68:61:32 stp256,ecdh-sha2 
00000030 2d:6e:69:73:74:70:33:38:34:2c:65:63:64:68:2d:73 -nistp384,ecdh-s 
00000040 68:61:32:2d:6e:69:73:74:70:35:32:31:2c:64:69:66 ha2-nistp521,dif 
00000050 66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75 fie-hellman-grou 
00000060 70:2d:65:78:63:68:61:6e:67:65:2d:73:68:61:31:2c p-exchange-sha1, 
00000070 64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67 diffie-hellman-g 
00000080 72:6f:75:70:2d:65:78:63:68:61:6e:67:65:2d:73:68 roup-exchange-sh 
00000090 61:32:35:36:2c:64:69:66:66:69:65:2d:68:65:6c:6c a256,diffie-hell 
000000a0 6d:61:6e:2d:67:72:6f:75:70:31:34:2d:73:68:61:31 man-group14-sha1 
000000b0 2c:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d ,diffie-hellman- 
000000c0 67:72:6f:75:70:31:2d:73:68:61:31:00:00:00:07:73 group1-sha1....s 
000000d0 73:68:2d:72:73:61:00:00:00:29:33:64:65:73:2d:63 sh-rsa...)3des-c 
000000e0 62:63:2c:61:65:73:32:35:36:2d:63:62:63:2c:61:65 bc,aes256-cbc,ae 
000000f0 73:31:39:32:2d:63:62:63:2c:61:65:73:31:32:38:2d s192-cbc,aes128- 
00000100 63:62:63:00:00:00:29:33:64:65:73:2d:63:62:63:2c cbc...)3des-cbc, 
00000110 61:65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:39 aes256-cbc,aes19 
00000120 32:2d:63:62:63:2c:61:65:73:31:32:38:2d:63:62:63 2-cbc,aes128-cbc 
00000130 00:00:00:62:68:6d:61:63:2d:73:68:61:31:2c:68:6d ...bhmac-sha1,hm 
00000140 61:63:2d:73:68:61:31:2d:39:36:2c:68:6d:61:63:2d ac-sha1-96,hmac- 
00000150 73:68:61:32:2d:32:35:36:2c:68:6d:61:63:2d:73:68 sha2-256,hmac-sh 
00000160 61:32:2d:32:35:36:2d:39:36:2c:68:6d:61:63:2d:73 a2-256-96,hmac-s 
00000170 68:61:32:2d:35:31:32:2c:68:6d:61:63:2d:73:68:61 ha2-512,hmac-sha 
00000180 32:2d:35:31:32:2d:39:36:2c:68:6d:61:63:2d:6d:64 2-512-96,hmac-md 
00000190 35:2c:6e:6f:6e:65:00:00:00:62:68:6d:61:63:2d:73 5,none...bhmac-s 
000001a0 68:61:31:2c:68:6d:61:63:2d:73:68:61:31:2d:39:36 ha1,hmac-sha1-96 
000001b0 2c:68:6d:61:63:2d:73:68:61:32:2d:32:35:36:2c:68 ,hmac-sha2-256,h 
000001c0 6d:61:63:2d:73:68:61:32:2d:32:35:36:2d:39:36:2c mac-sha2-256-96, 
000001d0 68:6d:61:63:2d:73:68:61:32:2d:35:31:32:2c:68:6d hmac-sha2-512,hm 
000001e0 61:63:2d:73:68:61:32:2d:35:31:32:2d:39:36:2c:68 ac-sha2-512-96,h 
000001f0 6d:61:63:2d:6d:64:35:2c:6e:6f:6e:65:00:00:00:04 mac-md5,none.... 
00000200 6e:6f:6e:65:00:00:00:04:6e:6f:6e:65:00:00:00:05 none....none.... 
00000210 65:6e:2d:55:53:00:00:00:05:65:6e:2d:55:53:00:00 en-US....en-US.. 
00000220 00:00:00 ... 

-> 
NET_SSH2_MSG_KEXINIT (since last: 0.0003, network: 0s) 
00000000 c3:d0:92:66:5e:26:bf:d6:f5:47:7b:37:8d:74:d0:d8 ...f^&...G{7.t.. 
00000010 00:00:00:36:64:69:66:66:69:65:2d:68:65:6c:6c:6d ...6diffie-hellm 
00000020 61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:2c:64 an-group1-sha1,d 
00000030 69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72 iffie-hellman-gr 
00000040 6f:75:70:31:34:2d:73:68:61:31:00:00:00:0f:73:73 oup14-sha1....ss 
00000050 68:2d:72:73:61:2c:73:73:68:2d:64:73:73:00:00:00 h-rsa,ssh-dss... 
00000060 76:61:72:63:66:6f:75:72:32:35:36:2c:61:72:63:66 varcfour256,arcf 
00000070 6f:75:72:31:32:38:2c:61:72:63:66:6f:75:72:2c:61 our128,arcfour,a 
00000080 65:73:31:32:38:2d:63:62:63:2c:61:65:73:31:39:32 es128-cbc,aes192 
00000090 2d:63:62:63:2c:61:65:73:32:35:36:2d:63:62:63:2c -cbc,aes256-cbc, 
000000a0 61:65:73:31:32:38:2d:63:74:72:2c:61:65:73:31:39 aes128-ctr,aes19 
000000b0 32:2d:63:74:72:2c:61:65:73:32:35:36:2d:63:74:72 2-ctr,aes256-ctr 
000000c0 2c:33:64:65:73:2d:63:74:72:2c:33:64:65:73:2d:63 ,3des-ctr,3des-c 
000000d0 62:63:2c:6e:6f:6e:65:00:00:00:76:61:72:63:66:6f bc,none...varcfo 
000000e0 75:72:32:35:36:2c:61:72:63:66:6f:75:72:31:32:38 ur256,arcfour128 
000000f0 2c:61:72:63:66:6f:75:72:2c:61:65:73:31:32:38:2d ,arcfour,aes128- 
00000100 63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61 cbc,aes192-cbc,a 
00000110 65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:32:38 es256-cbc,aes128 
00000120 2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c -ctr,aes192-ctr, 
00000130 61:65:73:32:35:36:2d:63:74:72:2c:33:64:65:73:2d aes256-ctr,3des- 
00000140 63:74:72:2c:33:64:65:73:2d:63:62:63:2c:6e:6f:6e ctr,3des-cbc,non 
00000150 65:00:00:00:30:68:6d:61:63:2d:73:68:61:31:2d:39 e...0hmac-sha1-9 
00000160 36:2c:68:6d:61:63:2d:73:68:61:31:2c:68:6d:61:63 6,hmac-sha1,hmac 
00000170 2d:6d:64:35:2d:39:36:2c:68:6d:61:63:2d:6d:64:35 -md5-96,hmac-md5 
00000180 2c:6e:6f:6e:65:00:00:00:30:68:6d:61:63:2d:73:68 ,none...0hmac-sh 
00000190 61:31:2d:39:36:2c:68:6d:61:63:2d:73:68:61:31:2c a1-96,hmac-sha1, 
000001a0 68:6d:61:63:2d:6d:64:35:2d:39:36:2c:68:6d:61:63 hmac-md5-96,hmac 
000001b0 2d:6d:64:35:2c:6e:6f:6e:65:00:00:00:04:6e:6f:6e -md5,none....non 
000001c0 65:00:00:00:04:6e:6f:6e:65:00:00:00:00:00:00:00 e....none....... 
000001d0 00:00:00:00:00:00 ...... 

-> 
NET_SSH2_MSG_KEXDH_INIT (since last: 0.0073, network: 0s) 
00000000 00:00:00:81:00:fe:d4:38:c4:82:21:38:1a:94:99:f4 .......8..!8.... 
00000010 84:72:58:f4:79:32:33:8e:f2:8a:31:1c:f2:be:76:77 .rX.y23...1...vw 
00000020 ca:e4:84:5b:70:96:6f:12:e9:47:55:75:97:c5:ea:bf ...[p.o..GUu.... 
00000030 5d:08:75:09:fa:90:4c:c9:a2:d1:58:01:89:ee:85:fd ].u...L...X..... 
00000040 56:a2:8c:8b:56:34:96:4b:f5:bb:91:03:39:ea:5c:02 V...V4.K....9.\. 
00000050 86:94:17:0f:be:8e:ad:f4:9f:03:7d:01:9f:48:d3:e8 ..........}..H.. 
00000060 87:82:b6:bc:67:d2:3a:0d:01:cb:dd:ed:ff:67:ca:fd ....g.:......g.. 
00000070 b7:b3:95:ed:f6:d1:cf:43:af:ed:32:ba:19:e2:e2:6e .......C..2....n 
00000080 f9:f0:39:96:78 ..9.x 

<- 
NET_SSH2_MSG_KEXDH_REPLY (since last: 0.3573, network: 0.3572s) 
00000000 00:00:01:17:00:00:00:07:73:73:68:2d:72:73:61:00 ........ssh-rsa. 
00000010 00:00:03:01:00:01:00:00:01:01:00:a5:97:f8:31:2b ..............1+ 
00000020 a4:bb:79:80:c6:90:17:29:49:53:ac:cd:7e:73:57:90 ..y....)IS..~sW. 
00000030 8c:d2:a8:8a:6a:7e:19:2b:d7:9c:6a:66:ba:84:6b:87 ....j~.+..jf..k. 
00000040 03:dc:c6:36:f5:a7:15:96:4c:f3:0f:3f:18:b0:5c:1e ...6....L..?..\. 
00000050 88:ac:56:dd:db:15:b6:ce:6c:1f:f5:ce:69:ff:a0:8e ..V.....l...i... 
00000060 7d:8f:e5:4f:56:2a:a5:c8:23:ec:50:f2:3d:74:93:38 }..OV*..#.P.=t.8 
00000070 ad:20:2e:c7:c2:f4:99:28:f5:15:4a:b3:18:c8:47:90 . .....(..J...G. 
00000080 30:c4:41:d0:9d:e8:22:ad:a9:d5:ac:ea:08:b4:32:d8 0.A...".......2. 
00000090 78:5a:4b:5f:6d:ac:23:f0:11:4b:46:04:84:d6:c5:5a xZK_m.#..KF....Z 
000000a0 81:0e:95:3d:82:53:4d:b2:19:e5:4f:76:d9:e6:44:a3 ...=.SM...Ov..D. 
000000b0 65:3d:60:04:1b:78:2e:1d:e6:65:81:88:68:d3:a5:29 e=`..x...e..h..) 
000000c0 cb:eb:73:9c:b8:44:0f:bb:15:64:b6:44:01:3d:35:5e ..s..D...d.D.=5^ 
000000d0 f9:72:0b:d2:a8:79:ad:1b:94:5e:76:d0:84:22:24:4a .r...y...^v.."$J 
000000e0 73:b0:36:bd:79:36:b7:26:42:47:b7:e6:da:2e:81:a5 s.6.y6.&BG...... 
000000f0 b0:aa:71:ec:94:4a:28:91:03:0b:25:58:4f:21:f6:e1 ..q..J(...%XO!.. 
00000100 96:7f:5d:9e:2f:9d:d7:27:78:04:15:26:3c:6c:64:5e ..]./..'x..&.ld^ 
00000110 f0:73:7a:bc:b4:7d:d6:9c:ca:12:d5:00:00:00:81:00 .sz..}.......... 
00000120 dc:df:d7:91:ce:f1:ca:ab:cb:85:2b:ea:b5:f6:74:6b ..........+...tk 
00000130 f2:67:ab:46:d1:28:88:37:42:29:d5:f8:d2:92:f8:90 .g.F.(.7B)...... 
00000140 06:d9:2f:82:f4:1d:60:9f:66:dc:5f:e7:81:81:e5:97 ../...`.f._..... 
00000150 a8:57:f1:0e:cb:2f:93:ac:5c:d9:05:e4:b6:cd:b9:7f .W.../..\....... 
00000160 62:8f:3b:92:54:a7:e9:f2:6b:2c:f2:18:01:5f:a1:1c b.;.T...k,..._.. 
00000170 94:c1:bb:ce:9e:37:39:51:e6:4e:e3:60:81:4d:3d:77 .....79Q.N.`.M=w 
00000180 df:99:80:74:c5:34:ec:2f:d8:3c:00:0c:81:86:5d:f8 ...t.4./......]. 
00000190 9d:51:67:be:da:19:8d:e1:95:6a:13:9d:d5:62:ab:fb .Qg......j...b.. 
000001a0 00:00:01:0f:00:00:00:07:73:73:68:2d:72:73:61:00 ........ssh-rsa. 
000001b0 00:01:00:32:36:ae:ff:1c:6f:37:04:c4:92:55:cb:32 ...26...o7...U.2 
000001c0 ef:1a:27:58:26:3e:5f:e1:c2:95:c3:94:68:7b:db:af ..'X&>_.....h{.. 
000001d0 d1:6e:6c:5b:7e:23:a6:13:d0:c7:97:76:f9:38:87:dc .nl[~#.....v.8.. 
000001e0 d2:06:b8:1d:1f:2c:3f:d9:ab:e5:be:53:46:06:da:1a .....,?....SF... 
000001f0 5a:20:d3:21:33:23:1a:b3:fe:e3:b6:74:40:b4:5a:28 Z .!3#.....t@.Z( 
00000200 0f:e5:24:68:a2:13:bf:75:25:ca:19:51:6b:4e:88:c5 ..$h...u%..QkN.. 
00000210 ad:8a:eb:db:16:ef:89:f0:85:bb:18:79:1a:be:76:9c ...........y..v. 
00000220 4d:56:ca:e3:ba:f3:5b:62:6d:a3:96:6b:b2:21:99:d4 MV....[bm..k.!.. 
00000230 a6:2e:a8:d9:99:f3:fe:38:02:72:69:31:75:a7:05:7b .......8.ri1u..{ 
00000240 86:93:01:73:0a:0d:1a:30:0a:a6:b7:ee:75:36:0b:99 ...s...0....u6.. 
00000250 14:96:b5:ca:a1:ea:26:0d:31:4d:6e:fa:7d:a9:16:a3 ......&.1Mn.}... 
00000260 03:f1:da:37:bb:54:0c:f1:1f:67:65:9d:96:27:d8:02 ...7.T...ge..'.. 
00000270 2b:96:46:f7:d6:bd:62:c1:7d:97:b5:7c:1a:e5:e0:98 +.F...b.}..|.... 
00000280 75:83:76:5a:a1:de:6a:dd:bb:4e:7a:45:e3:b2:aa:dc u.vZ..j..NzE.... 
00000290 d4:1f:c6:f3:83:33:2e:2c:76:cd:ea:5c:f9:d3:30:23 .....3.,v..\..0# 
000002a0 a6:84:af:cb:af:64:e8:06:f9:79:21:23:19:bc:35:0f .....d...y!#..5. 
000002b0 c2:e2:08 ... 

-> 
NET_SSH2_MSG_NEWKEYS (since last: 0.0091, network: 0s) 

<- 
NET_SSH2_MSG_NEWKEYS (since last: 0.0675, network: 0.0674s)

我正在将我的日志与具有相同问题的others进行比较,并看到我的日志中没有显示NET_SSH2_MSG_SERVICE_REQUEST,NET_SSH2_MSG_SERVICE_ACCEPT,NET_SSH2_MSG_USERAUTH_REQUEST的消息...由于某种原因,日志不完整,我可以看不到ssh用户身份验证请求的痕迹,在web服务器或php的配置中省略了什么?

提前感谢任何建议

1 个答案:

答案 0 :(得分:0)

$ssh = new Net_SFTP('ip.add.rre.ss:22'); // ip address with port address 
$key = new Crypt_RSA();
$key->setPassword('keyphrase');
$key->loadKey(file_get_contents('key.ppk'));
if (!$ssh->login($username, $key) && !$ssh->login($username, $password) )
{
    exit('Could Not login With These Credentials');
}
else
{
     echo "Connected SuccessFully";
}