Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. <?php
  2.  
  3. class ssh2
  4. {
  5.  
  6. private $host = 'host';
  7. private $user = 'user';
  8. private $port = '22';
  9. private $password = 'password';
  10. private $con = null;
  11. private $pubKeyFile = null;
  12. private $privKeyFile = null;
  13. private $shell_type = 'xterm';
  14. private $shell = null;
  15. private $log = array();
  16. private $sftp = null;
  17. private $knownHost = null;
  18.  
  19. function __construct($host = '', $port = '', $knownHost = NULL)
  20. {
  21. if ($host != '') {
  22. $this->host = $host;
  23. }
  24. if ($port != '') {
  25. $this->port = $port;
  26. }
  27. if (!is_null($knownHost)) {
  28. $this->knownHost = $knownHost;
  29. }
  30.  
  31. $this->con = ssh2_connect($this->host, $this->port, array('hostkey'=>'ssh-rsa,ssh-dss'));
  32.  
  33. if (!$this->con) {
  34. $this->log[] = 'Connection failed!';
  35. $this->con = NULL;
  36. }
  37. else {
  38. //If a host fingerprint has been required, check it.
  39. if (!is_null($this->knownHost)) {
  40. $fingerprint = ssh2_fingerprint($this->con, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
  41.  
  42. if (is_string($fingerprint)) {
  43. if (strcasecmp($fingerprint, $this->knownHost) != 0) {
  44. $this->log[] = 'HOSTKEY MISMATCH! (Expected "' . $this->knownHost . '" and received "' . $fingerprint . '" [comparison is case-insensitive]; possible Man-In-The-Middle Attack?';
  45. $this->con = NULL;
  46. }
  47. }
  48. else {
  49. $this->log[] = 'Could not determine remote host fingerprint (returned value was "' . $fingerprint . '"); aborting connection';
  50. $this->con = NULL;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. function __destruct()
  57. {
  58. $this->disconnect();
  59. }
  60.  
  61. function getCon()
  62. {
  63. return $this->con;
  64. }
  65.  
  66. function getSftp()
  67. {
  68. return $this->sftp;
  69. }
  70.  
  71. function authKeyFile($user, $pubKeyFile, $privKeyFile)
  72. {
  73. if ($user != '') {
  74. $this->user = $user;
  75. }
  76. if ($pubKeyFile != '') {
  77. $this->pubKeyFile = $pubKeyFile;
  78. }
  79. if ($privKeyFile != '') {
  80. $this->privKeyFile = $privKeyFile;
  81. }
  82.  
  83. if (!ssh2_auth_pubkey_file($this->con, $this->user, $this->pubKeyFile, $this->privKeyFile)) {
  84. $this->log[] = 'Public key authorization failed (public key "' . $this->pubKeyFile . '" and private key "' . $this->privKeyFile . '")!';
  85. return FALSE;
  86. }
  87. else {
  88. return TRUE;
  89. }
  90. }
  91.  
  92. function sftpCon()
  93. {
  94. global $startTime;
  95.  
  96. echo 'Calling ssh2_sftp(); time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  97.  
  98. $sftp = ssh2_sftp($this->con);
  99.  
  100. $this->sftp = $sftp;
  101. }
  102.  
  103. function getLog()
  104. {
  105. return $this->log;
  106. }
  107.  
  108. function disconnect()
  109. {
  110. ssh2_disconnect($this->con);
  111. }
  112.  
  113. }
  114.  
  115. class SshTest
  116. {
  117.  
  118. public static function connectToSshServer($server)
  119. {
  120. global $startTime;
  121.  
  122. //Establish an SSH session, so we can use file_exists()
  123. //on the remote server (requires sftp wrapper).
  124.  
  125. echo 'Calling "new ssh2()"; time elapsed: ' . ((microtime(true) - $startTime)) . PHP_EOL;
  126.  
  127. $ssh = new ssh2($server['hostname'], '22', $server['fingerprint']);
  128.  
  129. echo 'Calling "$ssh->getCon()"; time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  130.  
  131. if ($ssh->getCon() !== NULL) {
  132. //We were able to open an SSH session and the remote
  133. //server has the fingerprint that we expect. It's safe
  134. //to supply authentication credentials (as safe as possible, anyway).
  135.  
  136. echo 'Calling "$ssh->authKeyFile()"; time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  137.  
  138. if ($ssh->authKeyFile($server['sshUser'], $server['publicKey'], $server['privateKey'])) {
  139. //If authenticaton was successful, establish
  140. //an SFTP session so that sftp wrappers are available.
  141.  
  142. echo 'Calling "$ssh->sftpCon()"; time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  143.  
  144. $ssh->sftpCon();
  145. }
  146. }
  147.  
  148. echo 'Returning from SshTest::connectToSshServer(); time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  149.  
  150. return $ssh;
  151. }
  152.  
  153. }
  154.  
  155. ################################################################################
  156.  
  157. $testSshServers = array(
  158. 1 => array(
  159. 'hostname' => 'localhost',
  160. 'fingerprint' => 'DF551756B1D7AE3592AD574AD4D9F462',
  161. 'sshUser' => 'user',
  162. 'publicKey' => '/var/www/example.com/.ssh/user.pub',
  163. 'privateKey' => '/var/www/example.com/.ssh/user',
  164. ),
  165. );
  166.  
  167. global $startTime;
  168.  
  169. $startTime = microtime(true);
  170.  
  171. $ssh = SshTest::connectToSshServer($testSshServers[1]);
  172.  
  173. var_dump($ssh);
  174.  
  175. echo 'Attempting ssh2_disconnect(); time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  176.  
  177. $ssh->disconnect();
  178.  
  179. echo 'Exiting; time elapsed: ' . (microtime(true) - $startTime) . PHP_EOL;
  180.  
  181. exit;
  182.  
  183. /*
  184. Typical output:
  185.  
  186. php -f /var/www/example.com/web/php7-ssh2-segfault-test.php
  187. Calling "new ssh2()"; time elapsed: 6.9141387939453E-6
  188. Calling "$ssh->getCon()"; time elapsed: 0.054915904998779
  189. Calling "$ssh->authKeyFile()"; time elapsed: 0.055253982543945
  190. Calling "$ssh->sftpCon()"; time elapsed: 0.066303968429565
  191. Calling ssh2_sftp(); time elapsed: 0.066792964935303
  192. Returning from SshTest::connectToSshServer(); time elapsed: 0.25690102577209
  193. object(ssh2)#1 (12) {
  194. ["host":"ssh2":private]=>
  195. string(9) "localhost"
  196. ["user":"ssh2":private]=>
  197. string(3) "user"
  198. ["port":"ssh2":private]=>
  199. string(2) "22"
  200. ["password":"ssh2":private]=>
  201. string(8) "password"
  202. ["con":"ssh2":private]=>
  203. resource(4) of type (SSH2 Session)
  204. ["pubKeyFile":"ssh2":private]=>
  205. string(29) "/var/www/example.com/.ssh/user.pub"
  206. ["privKeyFile":"ssh2":private]=>
  207. string(25) "/var/www/example.com/.ssh/user"
  208. ["shell_type":"ssh2":private]=>
  209. string(5) "xterm"
  210. ["shell":"ssh2":private]=>
  211. NULL
  212. ["log":"ssh2":private]=>
  213. array(0) {
  214. }
  215. ["sftp":"ssh2":private]=>
  216. resource(5) of type (SSH2 SFTP)
  217. ["knownHost":"ssh2":private]=>
  218. string(32) "DF551756B1D7AE3592AD574AD4D9F462"
  219. }
  220. Attempting ssh2_disconnect(); time elapsed: 0.26224684715271
  221. Exiting; time elapsed: 0.26915097236633
  222. Segmentation fault (core dumped)
  223. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement