Agent.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Pure-PHP ssh-agent client.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * Here are some examples of how to use this library:
  8. * <code>
  9. * <?php
  10. * include 'System/SSH/Agent.php';
  11. * include 'Net/SSH2.php';
  12. *
  13. * $agent = new System_SSH_Agent();
  14. *
  15. * $ssh = new Net_SSH2('www.domain.tld');
  16. * if (!$ssh->login('username', $agent)) {
  17. * exit('Login Failed');
  18. * }
  19. *
  20. * echo $ssh->exec('pwd');
  21. * echo $ssh->exec('ls -la');
  22. * ?>
  23. * </code>
  24. *
  25. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  26. * of this software and associated documentation files (the "Software"), to deal
  27. * in the Software without restriction, including without limitation the rights
  28. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  29. * copies of the Software, and to permit persons to whom the Software is
  30. * furnished to do so, subject to the following conditions:
  31. *
  32. * The above copyright notice and this permission notice shall be included in
  33. * all copies or substantial portions of the Software.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  36. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  38. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  39. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  40. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  41. * THE SOFTWARE.
  42. *
  43. * @category System
  44. * @package System_SSH_Agent
  45. * @author Jim Wigginton <terrafrost@php.net>
  46. * @copyright 2014 Jim Wigginton
  47. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  48. * @link http://phpseclib.sourceforge.net
  49. * @internal See http://api.libssh.org/rfc/PROTOCOL.agent
  50. */
  51. /**#@+
  52. * Message numbers
  53. *
  54. * @access private
  55. */
  56. // to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
  57. define('SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES', 11);
  58. // this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
  59. define('SYSTEM_SSH_AGENT_IDENTITIES_ANSWER', 12);
  60. define('SYSTEM_SSH_AGENT_FAILURE', 5);
  61. // the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
  62. define('SYSTEM_SSH_AGENTC_SIGN_REQUEST', 13);
  63. // the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
  64. define('SYSTEM_SSH_AGENT_SIGN_RESPONSE', 14);
  65. /**#@-*/
  66. /**@+
  67. * Agent forwarding status
  68. *
  69. * @access private
  70. */
  71. // no forwarding requested and not active
  72. define('SYSTEM_SSH_AGENT_FORWARD_NONE', 0);
  73. // request agent forwarding when opportune
  74. define('SYSTEM_SSH_AGENT_FORWARD_REQUEST', 1);
  75. // forwarding has been request and is active
  76. define('SYSTEM_SSH_AGENT_FORWARD_ACTIVE', 2);
  77. /**#@-*/
  78. /**@+
  79. * Signature Flags
  80. *
  81. * See https://tools.ietf.org/html/draft-miller-ssh-agent-00#section-5.3
  82. *
  83. * @access private
  84. */
  85. define('SYSTEM_SSH_AGENT_RSA2_256', 2);
  86. define('SYSTEM_SSH_AGENT_RSA2_512', 4);
  87. /**#@-*/
  88. /**
  89. * Pure-PHP ssh-agent client identity object
  90. *
  91. * Instantiation should only be performed by System_SSH_Agent class.
  92. * This could be thought of as implementing an interface that Crypt_RSA
  93. * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
  94. * The methods in this interface would be getPublicKey, setSignatureMode
  95. * and sign since those are the methods phpseclib looks for to perform
  96. * public key authentication.
  97. *
  98. * @package System_SSH_Agent
  99. * @author Jim Wigginton <terrafrost@php.net>
  100. * @access internal
  101. */
  102. class System_SSH_Agent_Identity
  103. {
  104. /**
  105. * Key Object
  106. *
  107. * @var Crypt_RSA
  108. * @access private
  109. * @see self::getPublicKey()
  110. */
  111. var $key;
  112. /**
  113. * Key Blob
  114. *
  115. * @var string
  116. * @access private
  117. * @see self::sign()
  118. */
  119. var $key_blob;
  120. /**
  121. * Socket Resource
  122. *
  123. * @var resource
  124. * @access private
  125. * @see self::sign()
  126. */
  127. var $fsock;
  128. /**
  129. * Signature flags
  130. *
  131. * @var int
  132. * @access private
  133. * @see self::sign()
  134. * @see self::setHash()
  135. */
  136. var $flags = 0;
  137. /**
  138. * Default Constructor.
  139. *
  140. * @param resource $fsock
  141. * @return System_SSH_Agent_Identity
  142. * @access private
  143. */
  144. function __construct($fsock)
  145. {
  146. $this->fsock = $fsock;
  147. }
  148. /**
  149. * PHP4 compatible Default Constructor.
  150. *
  151. * @see self::__construct()
  152. * @param resource $fsock
  153. * @access public
  154. */
  155. function System_SSH_Agent_Identity($fsock)
  156. {
  157. $this->__construct($fsock);
  158. }
  159. /**
  160. * Set Public Key
  161. *
  162. * Called by System_SSH_Agent::requestIdentities()
  163. *
  164. * @param Crypt_RSA $key
  165. * @access private
  166. */
  167. function setPublicKey($key)
  168. {
  169. $this->key = $key;
  170. $this->key->setPublicKey();
  171. }
  172. /**
  173. * Set Public Key
  174. *
  175. * Called by System_SSH_Agent::requestIdentities(). The key blob could be extracted from $this->key
  176. * but this saves a small amount of computation.
  177. *
  178. * @param string $key_blob
  179. * @access private
  180. */
  181. function setPublicKeyBlob($key_blob)
  182. {
  183. $this->key_blob = $key_blob;
  184. }
  185. /**
  186. * Get Public Key
  187. *
  188. * Wrapper for $this->key->getPublicKey()
  189. *
  190. * @param int $format optional
  191. * @return mixed
  192. * @access public
  193. */
  194. function getPublicKey($format = null)
  195. {
  196. return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
  197. }
  198. /**
  199. * Set Signature Mode
  200. *
  201. * Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
  202. * ssh-agent's only supported mode is CRYPT_RSA_SIGNATURE_PKCS1
  203. *
  204. * @param int $mode
  205. * @access public
  206. */
  207. function setSignatureMode($mode)
  208. {
  209. }
  210. /**
  211. * Set Hash
  212. *
  213. * ssh-agent doesn't support using hashes for RSA other than SHA1
  214. *
  215. * @param string $hash
  216. * @access public
  217. */
  218. function setHash($hash)
  219. {
  220. $this->flags = 0;
  221. switch ($hash) {
  222. case 'sha1':
  223. break;
  224. case 'sha256':
  225. $this->flags = SYSTEM_SSH_AGENT_RSA2_256;
  226. break;
  227. case 'sha512':
  228. $this->flags = SYSTEM_SSH_AGENT_RSA2_512;
  229. break;
  230. default:
  231. user_error('The only supported hashes for RSA are sha1, sha256 and sha512');
  232. }
  233. }
  234. /**
  235. * Create a signature
  236. *
  237. * See "2.6.2 Protocol 2 private key signature request"
  238. *
  239. * @param string $message
  240. * @return string
  241. * @access public
  242. */
  243. function sign($message)
  244. {
  245. // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
  246. $packet = pack('CNa*Na*N', SYSTEM_SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, $this->flags);
  247. $packet = pack('Na*', strlen($packet), $packet);
  248. if (strlen($packet) != fputs($this->fsock, $packet)) {
  249. user_error('Connection closed during signing');
  250. return false;
  251. }
  252. $temp = fread($this->fsock, 4);
  253. if (strlen($temp) != 4) {
  254. user_error('Connection closed during signing');
  255. return false;
  256. }
  257. $length = current(unpack('N', $temp));
  258. $type = ord(fread($this->fsock, 1));
  259. if ($type != SYSTEM_SSH_AGENT_SIGN_RESPONSE) {
  260. user_error('Unable to retreive signature');
  261. [2020-12-13 01:26:28] local.ERROR: include_once(Math/BigInteger.php): failed to open stream: No such file or directory {"exception":"[object] (ErrorException(code: 0): include_once(Math/BigInteger.php): failed to open stream: No such file or directory at /www/wwwroot/suichou_api/thrid/phpseclib1/Crypt/RSA.php:501)
  262. [stacktrace]
  263. #0 /www/wwwroot/suichou_api/thrid/phpseclib1/Crypt/RSA.php(501): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'include_once(Ma...', '/www/wwwroot/su...', 501, Array)
  264. #1 /www/wwwroot/suichou_api/thrid/phpseclib1/Crypt/RSA.php(501): include_once()
  265. #2 /www/wwwroot/suichou_api/app/Dao/PaymentCenter/Platform/Sxbank/SxBankApi.php(153): Crypt_RSA->__construct()
  266. #3 /www/wwwroot/suichou_api/app/Http/Controllers/TestController.php(15): App\\Dao\\PaymentCenter\\Platform\\Sxbank\\SxBankApi->sendSms(NULL)
  267. #4 [internal function]: App\\Http\\Controllers\\TestController->sxtest(Object(Illuminate\\Http\\Request))
  268. #5 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
  269. #6 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('sxtest', Array)
  270. #7 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\TestController), 'sxtest')
  271. #8 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\\Routing\\Route->runController()
  272. #9 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\\Routing\\Route->run()
  273. #10 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  274. #11 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  275. #12 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
  276. #13 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
  277. #14 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
  278. #15 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
  279. #16 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
  280. #17 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
  281. #18 /www/wwwroot/suichou_api/app/Http/Middleware/CommonHandle.php(77): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  282. #19 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): App\\Http\\Middleware\\CommonHandle->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  283. #20 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  284. #21 /www/wwwroot/suichou_api/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  285. #22 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  286. #23 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  287. #24 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  288. #25 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  289. #26 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  290. #27 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  291. #28 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  292. #29 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  293. #30 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  294. #31 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  295. #32 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  296. #33 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  297. #34 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
  298. #35 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
  299. #36 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
  300. #37 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
  301. #38 /www/wwwroot/suichou_api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
  302. #39 /www/wwwroot/suichou_api/public/index.php(67): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
  303. #40 {main}
  304. "}
  305. [2020-12-13 01:26:28] local.ERROR: 访问路径:http://www.sc.cn/sx , 访问IP:172.17.0.1接口报错信息: include_once(Math/BigInteger.php): failed to open stream: No such file or directory ,报错行数:501
  306. if (!$ssh->_send_binary_packet($packet)) {
  307. return false;
  308. }
  309. $response = $ssh->_get_channel_packet($request_channel);
  310. if ($response === false) {
  311. return false;
  312. }
  313. $ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN;
  314. $this->forward_status = SYSTEM_SSH_AGENT_FORWARD_ACTIVE;
  315. return true;
  316. }
  317. /**
  318. * On successful channel open
  319. *
  320. * This method is called upon successful channel
  321. * open to give the SSH Agent an opportunity
  322. * to take further action. i.e. request agent forwarding
  323. *
  324. * @param Net_SSH2 $ssh
  325. * @access private
  326. */
  327. function _on_channel_open($ssh)
  328. {
  329. if ($this->forward_status == SYSTEM_SSH_AGENT_FORWARD_REQUEST) {
  330. $this->_request_forwarding($ssh);
  331. }
  332. }
  333. /**
  334. * Forward data to SSH Agent and return data reply
  335. *
  336. * @param string $data
  337. * @return data from SSH Agent
  338. * @access private
  339. */
  340. function _forward_data($data)
  341. {
  342. if ($this->expected_bytes > 0) {
  343. $this->socket_buffer.= $data;
  344. $this->expected_bytes -= strlen($data);
  345. } else {
  346. $agent_data_bytes = current(unpack('N', $data));
  347. $current_data_bytes = strlen($data);
  348. $this->socket_buffer = $data;
  349. if ($current_data_bytes != $agent_data_bytes + 4) {
  350. $this->expected_bytes = ($agent_data_bytes + 4) - $current_data_bytes;
  351. return false;
  352. }
  353. }
  354. if (strlen($this->socket_buffer) != fwrite($this->fsock, $this->socket_buffer)) {
  355. user_error('Connection closed attempting to forward data to SSH agent');
  356. return false;
  357. }
  358. $this->socket_buffer = '';
  359. $this->expected_bytes = 0;
  360. $temp = fread($this->fsock, 4);
  361. if (strlen($temp) != 4) {
  362. user_error('Connection closed while reading data response');
  363. return false;
  364. }
  365. $agent_reply_bytes = current(unpack('N', $temp));
  366. $agent_reply_data = fread($this->fsock, $agent_reply_bytes);
  367. if (strlen($agent_reply_data) != $agent_reply_bytes) {
  368. user_error('Connection closed while reading data response');
  369. return false;
  370. }
  371. $agent_reply_data = current(unpack('a*', $agent_reply_data));
  372. return pack('Na*', $agent_reply_bytes, $agent_reply_data);
  373. }
  374. }