123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351 |
- <?php
- define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001);
- define('NET_SSH2_MASK_LOGIN_REQ', 0x00000002);
- define('NET_SSH2_MASK_LOGIN', 0x00000004);
- define('NET_SSH2_MASK_SHELL', 0x00000008);
- define('NET_SSH2_CHANNEL_EXEC', 0);
- define('NET_SSH2_CHANNEL_SHELL',1);
- define('NET_SSH2_LOG_SIMPLE', 1);
- define('NET_SSH2_LOG_COMPLEX', 2);
- define('NET_SSH2_LOG_REALTIME', 3);
- define('NET_SSH2_LOG_REALTIME_FILE', 4);
- define('NET_SSH2_READ_SIMPLE', 1);
- define('NET_SSH2_READ_REGEX', 2);
- define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024);
- class Net_SSH2 {
-
- var $identifier = 'SSH-2.0-phpseclib_0.3';
-
- var $fsock;
-
- var $bitmap = 0;
-
- var $errors = array();
-
- var $server_identifier = '';
-
- var $kex_algorithms;
-
- var $server_host_key_algorithms;
-
- var $encryption_algorithms_client_to_server;
-
- var $encryption_algorithms_server_to_client;
-
- var $mac_algorithms_client_to_server;
-
- var $mac_algorithms_server_to_client;
-
- var $compression_algorithms_client_to_server;
-
- var $compression_algorithms_server_to_client;
-
- var $languages_server_to_client;
-
- var $languages_client_to_server;
-
- var $encrypt_block_size = 8;
-
- var $decrypt_block_size = 8;
-
- var $decrypt = false;
-
- var $encrypt = false;
-
- var $hmac_create = false;
-
- var $hmac_check = false;
-
- var $hmac_size = false;
-
- var $server_public_host_key;
-
- var $session_id = false;
-
- var $exchange_hash = false;
-
- var $message_numbers = array();
-
- var $disconnect_reasons = array();
-
- var $channel_open_failure_reasons = array();
-
- var $terminal_modes = array();
-
- var $channel_extended_data_type_codes = array();
-
- var $send_seq_no = 0;
-
- var $get_seq_no = 0;
-
- var $server_channels = array();
-
- var $channel_buffers = array();
-
- var $channel_status = array();
-
- var $packet_size_client_to_server = array();
-
- var $message_number_log = array();
-
- var $message_log = array();
-
- var $window_size = 0x7FFFFFFF;
-
- var $window_size_server_to_client = array();
-
- var $signature = '';
-
- var $signature_format = '';
-
- var $interactiveBuffer = '';
-
- var $log_size;
-
- var $timeout;
-
- var $curTimeout;
-
- var $realtime_log_file;
-
- var $realtime_log_size;
-
- var $signature_validated = false;
-
- var $realtime_log_wrap;
-
- var $quiet_mode = false;
-
- var $last_packet;
-
- var $exit_status;
-
- var $request_pty = false;
-
- var $in_request_pty_exec = false;
-
- var $stdErrorLog;
-
- var $last_interactive_response = '';
-
- var $keyboard_requests_responses = array();
-
- var $banner_message = '';
-
- var $is_timeout = false;
-
- function Net_SSH2($host, $port = 22, $timeout = 10)
- {
-
-
- if (!class_exists('Math_BigInteger')) {
- require_once('Math/BigInteger.php');
- }
- if (!function_exists('crypt_random_string')) {
- require_once('Crypt/Random.php');
- }
- if (!class_exists('Crypt_Hash')) {
- require_once('Crypt/Hash.php');
- }
- $this->last_packet = strtok(microtime(), ' ') + strtok('');
- $this->message_numbers = array(
- 1 => 'NET_SSH2_MSG_DISCONNECT',
- 2 => 'NET_SSH2_MSG_IGNORE',
- 3 => 'NET_SSH2_MSG_UNIMPLEMENTED',
- 4 => 'NET_SSH2_MSG_DEBUG',
- 5 => 'NET_SSH2_MSG_SERVICE_REQUEST',
- 6 => 'NET_SSH2_MSG_SERVICE_ACCEPT',
- 20 => 'NET_SSH2_MSG_KEXINIT',
- 21 => 'NET_SSH2_MSG_NEWKEYS',
- 30 => 'NET_SSH2_MSG_KEXDH_INIT',
- 31 => 'NET_SSH2_MSG_KEXDH_REPLY',
- 50 => 'NET_SSH2_MSG_USERAUTH_REQUEST',
- 51 => 'NET_SSH2_MSG_USERAUTH_FAILURE',
- 52 => 'NET_SSH2_MSG_USERAUTH_SUCCESS',
- 53 => 'NET_SSH2_MSG_USERAUTH_BANNER',
- 80 => 'NET_SSH2_MSG_GLOBAL_REQUEST',
- 81 => 'NET_SSH2_MSG_REQUEST_SUCCESS',
- 82 => 'NET_SSH2_MSG_REQUEST_FAILURE',
- 90 => 'NET_SSH2_MSG_CHANNEL_OPEN',
- 91 => 'NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION',
- 92 => 'NET_SSH2_MSG_CHANNEL_OPEN_FAILURE',
- 93 => 'NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST',
- 94 => 'NET_SSH2_MSG_CHANNEL_DATA',
- 95 => 'NET_SSH2_MSG_CHANNEL_EXTENDED_DATA',
- 96 => 'NET_SSH2_MSG_CHANNEL_EOF',
- 97 => 'NET_SSH2_MSG_CHANNEL_CLOSE',
- 98 => 'NET_SSH2_MSG_CHANNEL_REQUEST',
- 99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS',
- 100 => 'NET_SSH2_MSG_CHANNEL_FAILURE'
- );
- $this->disconnect_reasons = array(
- 1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
- 2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
- 3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
- 4 => 'NET_SSH2_DISCONNECT_RESERVED',
- 5 => 'NET_SSH2_DISCONNECT_MAC_ERROR',
- 6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR',
- 7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE',
- 8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED',
- 9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE',
- 10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST',
- 11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION',
- 12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS',
- 13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER',
- 14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
- 15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
- );
- $this->channel_open_failure_reasons = array(
- 1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED'
- );
- $this->terminal_modes = array(
- 0 => 'NET_SSH2_TTY_OP_END'
- );
- $this->channel_extended_data_type_codes = array(
- 1 => 'NET_SSH2_EXTENDED_DATA_STDERR'
- );
- $this->_define_array(
- $this->message_numbers,
- $this->disconnect_reasons,
- $this->channel_open_failure_reasons,
- $this->terminal_modes,
- $this->channel_extended_data_type_codes,
- array(60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'),
- array(60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'),
- array(60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
- 61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE')
- );
- $start = strtok(microtime(), ' ') + strtok('');
- $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
- if (!$this->fsock) {
- user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
- return;
- }
- $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
- $timeout-= $elapsed;
- if ($timeout <= 0) {
- user_error(rtrim("Cannot connect to $host. Timeout error"));
- return;
- }
- $read = array($this->fsock);
- $write = $except = NULL;
- $sec = floor($timeout);
- $usec = 1000000 * ($timeout - $sec);
-
-
- if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- user_error(rtrim("Cannot connect to $host. Banner timeout"));
- return;
- }
-
- $temp = '';
- $extra = '';
- while (!feof($this->fsock) && !preg_match('#^SSH-(\d\.\d+)#', $temp, $matches)) {
- if (substr($temp, -2) == "\r\n") {
- $extra.= $temp;
- $temp = '';
- }
- $temp.= fgets($this->fsock, 255);
- }
- if (feof($this->fsock)) {
- user_error('Connection closed by server');
- return false;
- }
- $ext = array();
- if (extension_loaded('mcrypt')) {
- $ext[] = 'mcrypt';
- }
- if (extension_loaded('gmp')) {
- $ext[] = 'gmp';
- } else if (extension_loaded('bcmath')) {
- $ext[] = 'bcmath';
- }
- if (!empty($ext)) {
- $this->identifier.= ' (' . implode(', ', $ext) . ')';
- }
- if (defined('NET_SSH2_LOGGING')) {
- $this->_append_log('<-', $extra . $temp);
- $this->_append_log('->', $this->identifier . "\r\n");
- }
- $this->server_identifier = trim($temp, "\r\n");
- if (strlen($extra)) {
- $this->errors[] = utf8_decode($extra);
- }
- if ($matches[1] != '1.99' && $matches[1] != '2.0') {
- user_error("Cannot connect to SSH $matches[1] servers");
- return;
- }
- fputs($this->fsock, $this->identifier . "\r\n");
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return;
- }
- if (ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
- user_error('Expected SSH_MSG_KEXINIT');
- return;
- }
- if (!$this->_key_exchange($response)) {
- return;
- }
- $this->bitmap = NET_SSH2_MASK_CONSTRUCTOR;
- }
-
- function _key_exchange($kexinit_payload_server)
- {
- static $kex_algorithms = array(
- 'diffie-hellman-group1-sha1',
- 'diffie-hellman-group14-sha1'
- );
- static $server_host_key_algorithms = array(
- 'ssh-rsa',
- 'ssh-dss'
- );
- static $encryption_algorithms = array(
-
- 'arcfour256',
- 'arcfour128',
- 'arcfour',
-
- 'aes128-ctr',
- 'aes192-ctr',
- 'aes256-ctr',
- 'blowfish-ctr',
- 'twofish128-ctr',
- 'twofish192-ctr',
- 'twofish256-ctr',
- 'aes128-cbc',
- 'aes192-cbc',
- 'aes256-cbc',
- 'blowfish-cbc',
- 'twofish128-cbc',
- 'twofish192-cbc',
- 'twofish256-cbc',
- 'twofish-cbc',
-
- '3des-ctr',
- '3des-cbc',
- 'none'
- );
- static $mac_algorithms = array(
- 'hmac-sha1-96',
- 'hmac-sha1',
- 'hmac-md5-96',
- 'hmac-md5',
- 'none'
- );
- static $compression_algorithms = array(
- 'none'
-
- );
-
- switch ($this->server_identifier) {
- case 'SSH-2.0-SSHD':
- $mac_algorithms = array_values(array_diff(
- $mac_algorithms,
- array('hmac-sha1-96', 'hmac-md5-96')
- ));
- }
- static $str_kex_algorithms, $str_server_host_key_algorithms,
- $encryption_algorithms_server_to_client, $mac_algorithms_server_to_client, $compression_algorithms_server_to_client,
- $encryption_algorithms_client_to_server, $mac_algorithms_client_to_server, $compression_algorithms_client_to_server;
- if (empty($str_kex_algorithms)) {
- $str_kex_algorithms = implode(',', $kex_algorithms);
- $str_server_host_key_algorithms = implode(',', $server_host_key_algorithms);
- $encryption_algorithms_server_to_client = $encryption_algorithms_client_to_server = implode(',', $encryption_algorithms);
- $mac_algorithms_server_to_client = $mac_algorithms_client_to_server = implode(',', $mac_algorithms);
- $compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms);
- }
- $client_cookie = crypt_random_string(16);
- $response = $kexinit_payload_server;
- $this->_string_shift($response, 1);
- $server_cookie = $this->_string_shift($response, 16);
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->server_host_key_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->encryption_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->encryption_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->mac_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->mac_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->compression_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->compression_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->languages_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->languages_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
- extract(unpack('Cfirst_kex_packet_follows', $this->_string_shift($response, 1)));
- $first_kex_packet_follows = $first_kex_packet_follows != 0;
-
- $kexinit_payload_client = pack('Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN',
- NET_SSH2_MSG_KEXINIT, $client_cookie, strlen($str_kex_algorithms), $str_kex_algorithms,
- strlen($str_server_host_key_algorithms), $str_server_host_key_algorithms, strlen($encryption_algorithms_client_to_server),
- $encryption_algorithms_client_to_server, strlen($encryption_algorithms_server_to_client), $encryption_algorithms_server_to_client,
- strlen($mac_algorithms_client_to_server), $mac_algorithms_client_to_server, strlen($mac_algorithms_server_to_client),
- $mac_algorithms_server_to_client, strlen($compression_algorithms_client_to_server), $compression_algorithms_client_to_server,
- strlen($compression_algorithms_server_to_client), $compression_algorithms_server_to_client, 0, '', 0, '',
- 0, 0
- );
- if (!$this->_send_binary_packet($kexinit_payload_client)) {
- return false;
- }
-
-
- for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_server_to_client); $i++);
- if ($i == count($encryption_algorithms)) {
- user_error('No compatible server to client encryption algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
-
- $decrypt = $encryption_algorithms[$i];
- switch ($decrypt) {
- case '3des-cbc':
- case '3des-ctr':
- $decryptKeyLength = 24;
- break;
- case 'aes256-cbc':
- case 'aes256-ctr':
- case 'twofish-cbc':
- case 'twofish256-cbc':
- case 'twofish256-ctr':
- $decryptKeyLength = 32;
- break;
- case 'aes192-cbc':
- case 'aes192-ctr':
- case 'twofish192-cbc':
- case 'twofish192-ctr':
- $decryptKeyLength = 24;
- break;
- case 'aes128-cbc':
- case 'aes128-ctr':
- case 'twofish128-cbc':
- case 'twofish128-ctr':
- case 'blowfish-cbc':
- case 'blowfish-ctr':
- $decryptKeyLength = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- $decryptKeyLength = 16;
- break;
- case 'arcfour256':
- $decryptKeyLength = 32;
- break;
- case 'none';
- $decryptKeyLength = 0;
- }
- for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_client_to_server); $i++);
- if ($i == count($encryption_algorithms)) {
- user_error('No compatible client to server encryption algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $encrypt = $encryption_algorithms[$i];
- switch ($encrypt) {
- case '3des-cbc':
- case '3des-ctr':
- $encryptKeyLength = 24;
- break;
- case 'aes256-cbc':
- case 'aes256-ctr':
- case 'twofish-cbc':
- case 'twofish256-cbc':
- case 'twofish256-ctr':
- $encryptKeyLength = 32;
- break;
- case 'aes192-cbc':
- case 'aes192-ctr':
- case 'twofish192-cbc':
- case 'twofish192-ctr':
- $encryptKeyLength = 24;
- break;
- case 'aes128-cbc':
- case 'aes128-ctr':
- case 'twofish128-cbc':
- case 'twofish128-ctr':
- case 'blowfish-cbc':
- case 'blowfish-ctr':
- $encryptKeyLength = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- $encryptKeyLength = 16;
- break;
- case 'arcfour256':
- $encryptKeyLength = 32;
- break;
- case 'none';
- $encryptKeyLength = 0;
- }
- $keyLength = $decryptKeyLength > $encryptKeyLength ? $decryptKeyLength : $encryptKeyLength;
-
- for ($i = 0; $i < count($kex_algorithms) && !in_array($kex_algorithms[$i], $this->kex_algorithms); $i++);
- if ($i == count($kex_algorithms)) {
- user_error('No compatible key exchange algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- switch ($kex_algorithms[$i]) {
-
-
- case 'diffie-hellman-group1-sha1':
- $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
- '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
- '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
- break;
-
- case 'diffie-hellman-group14-sha1':
- $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
- '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
- '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05' .
- '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB' .
- '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' .
- 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' .
- '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF';
- break;
- }
-
-
- $g = new Math_BigInteger(2);
- $prime = new Math_BigInteger($prime, 16);
- $kexHash = new Crypt_Hash('sha1');
-
-
- $one = new Math_BigInteger(1);
- $keyLength = min($keyLength, $kexHash->getLength());
- $max = $one->bitwise_leftShift(16 * $keyLength)->subtract($one);
- $x = $one->random($one, $max);
- $e = $g->modPow($x, $prime);
- $eBytes = $e->toBytes(true);
- $data = pack('CNa*', NET_SSH2_MSG_KEXDH_INIT, strlen($eBytes), $eBytes);
- if (!$this->_send_binary_packet($data)) {
- user_error('Connection closed by server');
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- if ($type != NET_SSH2_MSG_KEXDH_REPLY) {
- user_error('Expected SSH_MSG_KEXDH_REPLY');
- return false;
- }
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->server_public_host_key = $server_public_host_key = $this->_string_shift($response, $temp['length']);
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $public_key_format = $this->_string_shift($server_public_host_key, $temp['length']);
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $fBytes = $this->_string_shift($response, $temp['length']);
- $f = new Math_BigInteger($fBytes, -256);
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->signature = $this->_string_shift($response, $temp['length']);
- $temp = unpack('Nlength', $this->_string_shift($this->signature, 4));
- $this->signature_format = $this->_string_shift($this->signature, $temp['length']);
- $key = $f->modPow($x, $prime);
- $keyBytes = $key->toBytes(true);
- $this->exchange_hash = pack('Na*Na*Na*Na*Na*Na*Na*Na*',
- strlen($this->identifier), $this->identifier, strlen($this->server_identifier), $this->server_identifier,
- strlen($kexinit_payload_client), $kexinit_payload_client, strlen($kexinit_payload_server),
- $kexinit_payload_server, strlen($this->server_public_host_key), $this->server_public_host_key, strlen($eBytes),
- $eBytes, strlen($fBytes), $fBytes, strlen($keyBytes), $keyBytes
- );
- $this->exchange_hash = $kexHash->hash($this->exchange_hash);
- if ($this->session_id === false) {
- $this->session_id = $this->exchange_hash;
- }
- for ($i = 0; $i < count($server_host_key_algorithms) && !in_array($server_host_key_algorithms[$i], $this->server_host_key_algorithms); $i++);
- if ($i == count($server_host_key_algorithms)) {
- user_error('No compatible server host key algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- if ($public_key_format != $server_host_key_algorithms[$i] || $this->signature_format != $server_host_key_algorithms[$i]) {
- user_error('Server Host Key Algorithm Mismatch');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $packet = pack('C',
- NET_SSH2_MSG_NEWKEYS
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- if ($type != NET_SSH2_MSG_NEWKEYS) {
- user_error('Expected SSH_MSG_NEWKEYS');
- return false;
- }
- switch ($encrypt) {
- case '3des-cbc':
- if (!class_exists('Crypt_TripleDES')) {
- require_once('Crypt/TripleDES.php');
- }
- $this->encrypt = new Crypt_TripleDES();
-
- break;
- case '3des-ctr':
- if (!class_exists('Crypt_TripleDES')) {
- require_once('Crypt/TripleDES.php');
- }
- $this->encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
-
- break;
- case 'aes256-cbc':
- case 'aes192-cbc':
- case 'aes128-cbc':
- if (!class_exists('Crypt_AES')) {
- require_once('Crypt/AES.php');
- }
- $this->encrypt = new Crypt_AES();
- $this->encrypt_block_size = 16;
- break;
- case 'aes256-ctr':
- case 'aes192-ctr':
- case 'aes128-ctr':
- if (!class_exists('Crypt_AES')) {
- require_once('Crypt/AES.php');
- }
- $this->encrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
- $this->encrypt_block_size = 16;
- break;
- case 'blowfish-cbc':
- if (!class_exists('Crypt_Blowfish')) {
- require_once('Crypt/Blowfish.php');
- }
- $this->encrypt = new Crypt_Blowfish();
- $this->encrypt_block_size = 8;
- break;
- case 'blowfish-ctr':
- if (!class_exists('Crypt_Blowfish')) {
- require_once('Crypt/Blowfish.php');
- }
- $this->encrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
- $this->encrypt_block_size = 8;
- break;
- case 'twofish128-cbc':
- case 'twofish192-cbc':
- case 'twofish256-cbc':
- case 'twofish-cbc':
- if (!class_exists('Crypt_Twofish')) {
- require_once('Crypt/Twofish.php');
- }
- $this->encrypt = new Crypt_Twofish();
- $this->encrypt_block_size = 16;
- break;
- case 'twofish128-ctr':
- case 'twofish192-ctr':
- case 'twofish256-ctr':
- if (!class_exists('Crypt_Twofish')) {
- require_once('Crypt/Twofish.php');
- }
- $this->encrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
- $this->encrypt_block_size = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- case 'arcfour256':
- if (!class_exists('Crypt_RC4')) {
- require_once('Crypt/RC4.php');
- }
- $this->encrypt = new Crypt_RC4();
- break;
- case 'none';
-
- }
- switch ($decrypt) {
- case '3des-cbc':
- if (!class_exists('Crypt_TripleDES')) {
- require_once('Crypt/TripleDES.php');
- }
- $this->decrypt = new Crypt_TripleDES();
- break;
- case '3des-ctr':
- if (!class_exists('Crypt_TripleDES')) {
- require_once('Crypt/TripleDES.php');
- }
- $this->decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
- break;
- case 'aes256-cbc':
- case 'aes192-cbc':
- case 'aes128-cbc':
- if (!class_exists('Crypt_AES')) {
- require_once('Crypt/AES.php');
- }
- $this->decrypt = new Crypt_AES();
- $this->decrypt_block_size = 16;
- break;
- case 'aes256-ctr':
- case 'aes192-ctr':
- case 'aes128-ctr':
- if (!class_exists('Crypt_AES')) {
- require_once('Crypt/AES.php');
- }
- $this->decrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
- $this->decrypt_block_size = 16;
- break;
- case 'blowfish-cbc':
- if (!class_exists('Crypt_Blowfish')) {
- require_once('Crypt/Blowfish.php');
- }
- $this->decrypt = new Crypt_Blowfish();
- $this->decrypt_block_size = 8;
- break;
- case 'blowfish-ctr':
- if (!class_exists('Crypt_Blowfish')) {
- require_once('Crypt/Blowfish.php');
- }
- $this->decrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
- $this->decrypt_block_size = 8;
- break;
- case 'twofish128-cbc':
- case 'twofish192-cbc':
- case 'twofish256-cbc':
- case 'twofish-cbc':
- if (!class_exists('Crypt_Twofish')) {
- require_once('Crypt/Twofish.php');
- }
- $this->decrypt = new Crypt_Twofish();
- $this->decrypt_block_size = 16;
- break;
- case 'twofish128-ctr':
- case 'twofish192-ctr':
- case 'twofish256-ctr':
- if (!class_exists('Crypt_Twofish')) {
- require_once('Crypt/Twofish.php');
- }
- $this->decrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
- $this->decrypt_block_size = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- case 'arcfour256':
- if (!class_exists('Crypt_RC4')) {
- require_once('Crypt/RC4.php');
- }
- $this->decrypt = new Crypt_RC4();
- break;
- case 'none';
-
- }
- $keyBytes = pack('Na*', strlen($keyBytes), $keyBytes);
- if ($this->encrypt) {
- $this->encrypt->enableContinuousBuffer();
- $this->encrypt->disablePadding();
- $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'A' . $this->session_id);
- while ($this->encrypt_block_size > strlen($iv)) {
- $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
- }
- $this->encrypt->setIV(substr($iv, 0, $this->encrypt_block_size));
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'C' . $this->session_id);
- while ($encryptKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->encrypt->setKey(substr($key, 0, $encryptKeyLength));
- }
- if ($this->decrypt) {
- $this->decrypt->enableContinuousBuffer();
- $this->decrypt->disablePadding();
- $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'B' . $this->session_id);
- while ($this->decrypt_block_size > strlen($iv)) {
- $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
- }
- $this->decrypt->setIV(substr($iv, 0, $this->decrypt_block_size));
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'D' . $this->session_id);
- while ($decryptKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->decrypt->setKey(substr($key, 0, $decryptKeyLength));
- }
-
- if ($encrypt == 'arcfour128' || $encrypt == 'arcfour256') {
- $this->encrypt->encrypt(str_repeat("\0", 1536));
- }
- if ($decrypt == 'arcfour128' || $decrypt == 'arcfour256') {
- $this->decrypt->decrypt(str_repeat("\0", 1536));
- }
- for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_client_to_server); $i++);
- if ($i == count($mac_algorithms)) {
- user_error('No compatible client to server message authentication algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $createKeyLength = 0;
- switch ($mac_algorithms[$i]) {
- case 'hmac-sha1':
- $this->hmac_create = new Crypt_Hash('sha1');
- $createKeyLength = 20;
- break;
- case 'hmac-sha1-96':
- $this->hmac_create = new Crypt_Hash('sha1-96');
- $createKeyLength = 20;
- break;
- case 'hmac-md5':
- $this->hmac_create = new Crypt_Hash('md5');
- $createKeyLength = 16;
- break;
- case 'hmac-md5-96':
- $this->hmac_create = new Crypt_Hash('md5-96');
- $createKeyLength = 16;
- }
- for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_server_to_client); $i++);
- if ($i == count($mac_algorithms)) {
- user_error('No compatible server to client message authentication algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $checkKeyLength = 0;
- $this->hmac_size = 0;
- switch ($mac_algorithms[$i]) {
- case 'hmac-sha1':
- $this->hmac_check = new Crypt_Hash('sha1');
- $checkKeyLength = 20;
- $this->hmac_size = 20;
- break;
- case 'hmac-sha1-96':
- $this->hmac_check = new Crypt_Hash('sha1-96');
- $checkKeyLength = 20;
- $this->hmac_size = 12;
- break;
- case 'hmac-md5':
- $this->hmac_check = new Crypt_Hash('md5');
- $checkKeyLength = 16;
- $this->hmac_size = 16;
- break;
- case 'hmac-md5-96':
- $this->hmac_check = new Crypt_Hash('md5-96');
- $checkKeyLength = 16;
- $this->hmac_size = 12;
- }
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'E' . $this->session_id);
- while ($createKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->hmac_create->setKey(substr($key, 0, $createKeyLength));
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'F' . $this->session_id);
- while ($checkKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->hmac_check->setKey(substr($key, 0, $checkKeyLength));
- for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_server_to_client); $i++);
- if ($i == count($compression_algorithms)) {
- user_error('No compatible server to client compression algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $this->decompress = $compression_algorithms[$i] == 'zlib';
- for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_client_to_server); $i++);
- if ($i == count($compression_algorithms)) {
- user_error('No compatible client to server compression algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $this->compress = $compression_algorithms[$i] == 'zlib';
- return true;
- }
-
- function login($username)
- {
- $args = array_slice(func_get_args(), 1);
- if (empty($args)) {
- return $this->_login_helper($username);
- }
- foreach ($args as $arg) {
- if ($this->_login_helper($username, $arg)) {
- return true;
- }
- }
- return false;
- }
-
- function _login_helper($username, $password = null)
- {
- if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) {
- return false;
- }
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN_REQ)) {
- $packet = pack('CNa*',
- NET_SSH2_MSG_SERVICE_REQUEST, strlen('ssh-userauth'), 'ssh-userauth'
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) {
- user_error('Expected SSH_MSG_SERVICE_ACCEPT');
- return false;
- }
- $this->bitmap |= NET_SSH2_MASK_LOGIN_REQ;
- }
- if (strlen($this->last_interactive_response)) {
- return !is_string($password) && !is_array($password) ? false : $this->_keyboard_interactive_process($password);
- }
-
- if (is_object($password) && strtolower(get_class($password)) == 'crypt_rsa') {
- return $this->_privatekey_login($username, $password);
- }
- if (is_array($password)) {
- if ($this->_keyboard_interactive_login($username, $password)) {
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
- if (!isset($password)) {
- $packet = pack('CNa*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('none'), 'none'
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
-
- default:
- return false;
- }
- }
- $packet = pack('CNa*Na*Na*CNa*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('password'), 'password', 0, strlen($password), $password
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
- $packet = pack('CNa*Na*Na*CNa*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection',
- strlen('password'), 'password', 0, strlen('password'), 'password'
- );
- $this->message_log[count($this->message_log) - 1] = $packet;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ:
- if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ';
- }
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_USERAUTH_PASSWD_CHANGEREQ: ' . utf8_decode($this->_string_shift($response, $length));
- return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
- case NET_SSH2_MSG_USERAUTH_FAILURE:
-
-
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $auth_methods = explode(',', $this->_string_shift($response, $length));
- extract(unpack('Cpartial_success', $this->_string_shift($response, 1)));
- $partial_success = $partial_success != 0;
- if (!$partial_success && in_array('keyboard-interactive', $auth_methods)) {
- if ($this->_keyboard_interactive_login($username, $password)) {
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
- return false;
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
-
- function _keyboard_interactive_login($username, $password)
- {
- $packet = pack('CNa*Na*Na*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('keyboard-interactive'), 'keyboard-interactive', 0, '', 0, ''
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- return $this->_keyboard_interactive_process($password);
- }
-
- function _keyboard_interactive_process()
- {
- $responses = func_get_args();
- if (strlen($this->last_interactive_response)) {
- $response = $this->last_interactive_response;
- } else {
- $orig = $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_INFO_REQUEST:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length);
- extract(unpack('Nnum_prompts', $this->_string_shift($response, 4)));
- for ($i = 0; $i < count($responses); $i++) {
- if (is_array($responses[$i])) {
- foreach ($responses[$i] as $key => $value) {
- $this->keyboard_requests_responses[$key] = $value;
- }
- unset($responses[$i]);
- }
- }
- $responses = array_values($responses);
- if (isset($this->keyboard_requests_responses)) {
- for ($i = 0; $i < $num_prompts; $i++) {
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
-
- $prompt = $this->_string_shift($response, $length);
-
- foreach ($this->keyboard_requests_responses as $key => $value) {
- if (substr($prompt, 0, strlen($key)) == $key) {
- $responses[] = $value;
- break;
- }
- }
- }
- }
-
- if (strlen($this->last_interactive_response)) {
- $this->last_interactive_response = '';
- } else if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- }
- if (!count($responses) && $num_prompts) {
- $this->last_interactive_response = $orig;
- $this->bitmap |= NET_SSH_MASK_LOGIN_INTERACTIVE;
- return false;
- }
-
-
- $packet = $logged = pack('CN', NET_SSH2_MSG_USERAUTH_INFO_RESPONSE, count($responses));
- for ($i = 0; $i < count($responses); $i++) {
- $packet.= pack('Na*', strlen($responses[$i]), $responses[$i]);
- $logged.= pack('Na*', strlen('dummy-answer'), 'dummy-answer');
- }
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- $this->message_log[count($this->message_log) - 1] = $logged;
- }
-
-
-
- return $this->_keyboard_interactive_process();
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- return true;
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- return false;
- }
- return false;
- }
-
- function _privatekey_login($username, $privatekey)
- {
-
- $publickey = $privatekey->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
- if ($publickey === false) {
- return false;
- }
- $publickey = array(
- 'e' => $publickey['e']->toBytes(true),
- 'n' => $publickey['n']->toBytes(true)
- );
- $publickey = pack('Na*Na*Na*',
- strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey['e']), $publickey['e'], strlen($publickey['n']), $publickey['n']
- );
- $part1 = pack('CNa*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('publickey'), 'publickey'
- );
- $part2 = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey), $publickey);
- $packet = $part1 . chr(0) . $part2;
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_USERAUTH_FAILURE: ' . $this->_string_shift($response, $length);
- return false;
- case NET_SSH2_MSG_USERAUTH_PK_OK:
-
-
- if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_PK_OK',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- }
- }
- $packet = $part1 . chr(1) . $part2;
- $privatekey->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
- $signature = $privatekey->sign(pack('Na*a*', strlen($this->session_id), $this->session_id, $packet));
- $signature = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($signature), $signature);
- $packet.= pack('Na*', strlen($signature), $signature);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_FAILURE:
-
- return false;
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
-
- function setTimeout($timeout)
- {
- $this->timeout = $this->curTimeout = $timeout;
- }
-
- function getStdError()
- {
- return $this->stdErrorLog;
- }
-
- function exec($command, $callback = NULL)
- {
- $this->curTimeout = $this->timeout;
- $this->is_timeout = false;
- $this->stdErrorLog = '';
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
-
-
-
- $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC] = 0x7FFFFFFF;
-
-
- $packet_size = 0x4000;
- $packet = pack('CNa*N3',
- NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_EXEC, $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC], $packet_size);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN;
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- if ($response === false) {
- return false;
- }
- if ($this->request_pty === true) {
- $terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
- $packet = pack('CNNa*CNa*N5a*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100',
- 80, 24, 0, 0, strlen($terminal_modes), $terminal_modes);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- list(, $type) = unpack('C', $this->_string_shift($response, 1));
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- break;
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- default:
- user_error('Unable to request pseudo-terminal');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- $this->in_request_pty_exec = true;
- }
-
-
-
-
-
-
-
-
- $packet = pack('CNNa*CNa*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST;
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- if ($response === false) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA;
- if ($callback === false || $this->in_request_pty_exec) {
- return true;
- }
- $output = '';
- while (true) {
- $temp = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- switch (true) {
- case $temp === true:
- return is_callable($callback) ? true : $output;
- case $temp === false:
- return false;
- default:
- if (is_callable($callback)) {
- $callback($temp);
- } else {
- $output.= $temp;
- }
- }
- }
- }
-
- function _initShell()
- {
- if ($this->in_request_pty_exec === true) {
- return true;
- }
- $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL] = 0x7FFFFFFF;
- $packet_size = 0x4000;
- $packet = pack('CNa*N3',
- NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SHELL, $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL], $packet_size);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN;
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
- if ($response === false) {
- return false;
- }
- $terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
- $packet = pack('CNNa*CNa*N5a*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100',
- 80, 24, 0, 0, strlen($terminal_modes), $terminal_modes);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- list(, $type) = unpack('C', $this->_string_shift($response, 1));
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- break;
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- default:
- user_error('Unable to request pseudo-terminal');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- $packet = pack('CNNa*C',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('shell'), 'shell', 1);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST;
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
- if ($response === false) {
- return false;
- }
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA;
- $this->bitmap |= NET_SSH2_MASK_SHELL;
- return true;
- }
-
- function read($expect = '', $mode = NET_SSH2_READ_SIMPLE)
- {
- $this->curTimeout = $this->timeout;
- $this->is_timeout = false;
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
- if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
- $channel = $this->in_request_pty_exec ? NET_SSH2_CHANNEL_EXEC : NET_SSH2_CHANNEL_SHELL;
- $match = $expect;
- while (true) {
- if ($mode == NET_SSH2_READ_REGEX) {
- preg_match($expect, $this->interactiveBuffer, $matches);
- $match = isset($matches[0]) ? $matches[0] : '';
- }
- $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false;
- if ($pos !== false) {
- return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match));
- }
- $response = $this->_get_channel_packet($channel);
- if (is_bool($response)) {
- $this->in_request_pty_exec = false;
- return $response ? $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)) : false;
- }
- $this->interactiveBuffer.= $response;
- }
- }
-
- function write($cmd)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
- if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
- $channel = $this->in_request_pty_exec ? NET_SSH2_CHANNEL_EXEC : NET_SSH2_CHANNEL_SHELL;
- return $this->_send_channel_packet($channel, $cmd);
- }
-
- function reset()
- {
- $channel = $this->in_request_pty_exec ? NET_SSH2_CHANNEL_EXEC : NET_SSH2_CHANNEL_SHELL;
- $this->_close_channel($channel);
- }
-
- function isTimeout()
- {
- return $this->is_timeout;
- }
-
- function disconnect()
- {
- $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- if (isset($this->realtime_log_file) && is_resource($this->realtime_log_file)) {
- fclose($this->realtime_log_file);
- }
- }
-
- function __destruct()
- {
- $this->disconnect();
- }
-
- function isConnected()
- {
- return $this->bitmap & NET_SSH2_MASK_LOGIN;
- }
-
- function _get_binary_packet()
- {
- if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely');
- $this->bitmask = 0;
- return false;
- }
- $start = strtok(microtime(), ' ') + strtok('');
- $raw = fread($this->fsock, $this->decrypt_block_size);
- if (!strlen($raw)) {
- return '';
- }
- if ($this->decrypt !== false) {
- $raw = $this->decrypt->decrypt($raw);
- }
- if ($raw === false) {
- user_error('Unable to decrypt content');
- return false;
- }
- extract(unpack('Npacket_length/Cpadding_length', $this->_string_shift($raw, 5)));
- $remaining_length = $packet_length + 4 - $this->decrypt_block_size;
-
-
-
- if ($remaining_length < -$this->decrypt_block_size || $remaining_length > 0x9000 || $remaining_length % $this->decrypt_block_size != 0) {
- user_error('Invalid size');
- return false;
- }
- $buffer = '';
- while ($remaining_length > 0) {
- $temp = fread($this->fsock, $remaining_length);
- $buffer.= $temp;
- $remaining_length-= strlen($temp);
- }
- $stop = strtok(microtime(), ' ') + strtok('');
- if (strlen($buffer)) {
- $raw.= $this->decrypt !== false ? $this->decrypt->decrypt($buffer) : $buffer;
- }
- $payload = $this->_string_shift($raw, $packet_length - $padding_length - 1);
- $padding = $this->_string_shift($raw, $padding_length);
- if ($this->hmac_check !== false) {
- $hmac = fread($this->fsock, $this->hmac_size);
- if ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) {
- user_error('Invalid HMAC');
- return false;
- }
- }
-
-
-
- $this->get_seq_no++;
- if (defined('NET_SSH2_LOGGING')) {
- $current = strtok(microtime(), ' ') + strtok('');
- $message_number = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')';
- $message_number = '<- ' . $message_number .
- ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
- $this->_append_log($message_number, $payload);
- $this->last_packet = $current;
- }
- return $this->_filter($payload);
- }
-
- function _filter($payload)
- {
- switch (ord($payload[0])) {
- case NET_SSH2_MSG_DISCONNECT:
- $this->_string_shift($payload, 1);
- extract(unpack('Nreason_code/Nlength', $this->_string_shift($payload, 8)));
- $this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n" . utf8_decode($this->_string_shift($payload, $length));
- $this->bitmask = 0;
- return false;
- case NET_SSH2_MSG_IGNORE:
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_DEBUG:
- $this->_string_shift($payload, 2);
- extract(unpack('Nlength', $this->_string_shift($payload, 4)));
- $this->errors[] = 'SSH_MSG_DEBUG: ' . utf8_decode($this->_string_shift($payload, $length));
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_UNIMPLEMENTED:
- return false;
- case NET_SSH2_MSG_KEXINIT:
- if ($this->session_id !== false) {
- if (!$this->_key_exchange($payload)) {
- $this->bitmask = 0;
- return false;
- }
- $payload = $this->_get_binary_packet();
- }
- }
-
- if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
- $this->_string_shift($payload, 1);
- extract(unpack('Nlength', $this->_string_shift($payload, 4)));
- $this->banner_message = utf8_decode($this->_string_shift($payload, $length));
- $payload = $this->_get_binary_packet();
- }
-
- if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && ($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- switch (ord($payload[0])) {
- case NET_SSH2_MSG_GLOBAL_REQUEST:
- $this->_string_shift($payload, 1);
- extract(unpack('Nlength', $this->_string_shift($payload)));
- $this->errors[] = 'SSH_MSG_GLOBAL_REQUEST: ' . utf8_decode($this->_string_shift($payload, $length));
- if (!$this->_send_binary_packet(pack('C', NET_SSH2_MSG_REQUEST_FAILURE))) {
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_CHANNEL_OPEN:
- $this->_string_shift($payload, 1);
- extract(unpack('N', $this->_string_shift($payload, 4)));
- $this->errors[] = 'SSH_MSG_CHANNEL_OPEN: ' . utf8_decode($this->_string_shift($payload, $length));
- $this->_string_shift($payload, 4);
- extract(unpack('Nserver_channel', $this->_string_shift($payload, 4)));
- $packet = pack('CN3a*Na*',
- NET_SSH2_MSG_REQUEST_FAILURE, $server_channel, NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED, 0, '', 0, '');
- if (!$this->_send_binary_packet($packet)) {
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST:
- $payload = $this->_get_binary_packet();
- }
- }
- return $payload;
- }
-
- function enableQuietMode()
- {
- $this->quiet_mode = true;
- }
-
- function disableQuietMode()
- {
- $this->quiet_mode = false;
- }
-
- function enablePTY()
- {
- $this->request_pty = true;
- }
-
- function disablePTY()
- {
- $this->request_pty = false;
- }
-
- function _get_channel_packet($client_channel, $skip_extended = false)
- {
- if (!empty($this->channel_buffers[$client_channel])) {
- return array_shift($this->channel_buffers[$client_channel]);
- }
- while (true) {
- if ($this->curTimeout) {
- if ($this->curTimeout < 0) {
- $this->is_timeout = true;
- return true;
- }
- $read = array($this->fsock);
- $write = $except = NULL;
- $start = strtok(microtime(), ' ') + strtok('');
- $sec = floor($this->curTimeout);
- $usec = 1000000 * ($this->curTimeout - $sec);
-
- if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- $this->is_timeout = true;
- return true;
- }
- $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
- $this->curTimeout-= $elapsed;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- if (!strlen($response)) {
- return '';
- }
-
- $this->window_size_server_to_client[$client_channel]-= strlen($response);
- if ($this->window_size_server_to_client[$client_channel] < 0) {
- $packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$client_channel], $this->window_size);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->window_size_server_to_client[$client_channel]+= $this->window_size;
- }
- extract(unpack('Ctype/Nchannel', $this->_string_shift($response, 5)));
- switch ($this->channel_status[$channel]) {
- case NET_SSH2_MSG_CHANNEL_OPEN:
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
- extract(unpack('Nserver_channel', $this->_string_shift($response, 4)));
- $this->server_channels[$channel] = $server_channel;
- $this->_string_shift($response, 4);
- $temp = unpack('Npacket_size_client_to_server', $this->_string_shift($response, 4));
- $this->packet_size_client_to_server[$channel] = $temp['packet_size_client_to_server'];
- return $client_channel == $channel ? true : $this->_get_channel_packet($client_channel, $skip_extended);
-
- default:
- user_error('Unable to open channel');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- break;
- case NET_SSH2_MSG_CHANNEL_REQUEST:
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- return true;
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- return false;
- default:
- user_error('Unable to fulfill channel request');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- case NET_SSH2_MSG_CHANNEL_CLOSE:
- return $type == NET_SSH2_MSG_CHANNEL_CLOSE ? true : $this->_get_channel_packet($client_channel, $skip_extended);
- }
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_DATA:
-
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $data = $this->_string_shift($response, $length);
- if ($client_channel == $channel) {
- return $data;
- }
- if (!isset($this->channel_buffers[$client_channel])) {
- $this->channel_buffers[$client_channel] = array();
- }
- $this->channel_buffers[$client_channel][] = $data;
- break;
- case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA:
-
-
- extract(unpack('Ndata_type_code/Nlength', $this->_string_shift($response, 8)));
- $data = $this->_string_shift($response, $length);
- $this->stdErrorLog .= $data;
- if ($skip_extended || $this->quiet_mode) {
- break;
- }
- if ($client_channel == $channel) {
- return $data;
- }
- if (!isset($this->channel_buffers[$client_channel])) {
- $this->channel_buffers[$client_channel] = array();
- }
- $this->channel_buffers[$client_channel][] = $data;
- break;
- case NET_SSH2_MSG_CHANNEL_REQUEST:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $value = $this->_string_shift($response, $length);
- switch ($value) {
- case 'exit-signal':
- $this->_string_shift($response, 1);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_CHANNEL_REQUEST (exit-signal): ' . $this->_string_shift($response, $length);
- $this->_string_shift($response, 1);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- if ($length) {
- $this->errors[count($this->errors)].= "\r\n" . $this->_string_shift($response, $length);
- }
- case 'exit-status':
- extract(unpack('Cfalse/Nexit_status', $this->_string_shift($response, 5)));
- $this->exit_status = $exit_status;
-
-
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
- $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_EOF;
- default:
-
-
- break;
- }
- break;
- case NET_SSH2_MSG_CHANNEL_CLOSE:
- $this->curTimeout = 0;
- if ($this->bitmap & NET_SSH2_MASK_SHELL) {
- $this->bitmap&= ~NET_SSH2_MASK_SHELL;
- }
- if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) {
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
- }
- $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
- return true;
- case NET_SSH2_MSG_CHANNEL_EOF:
- break;
- default:
- user_error('Error reading channel data');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- }
- }
-
- function _send_binary_packet($data)
- {
- if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely');
- $this->bitmask = 0;
- return false;
- }
-
-
-
-
-
-
- $packet_length = strlen($data) + 9;
-
- $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size;
-
- $padding_length = $packet_length - strlen($data) - 5;
- $padding = crypt_random_string($padding_length);
-
- $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);
- $hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : '';
- $this->send_seq_no++;
- if ($this->encrypt !== false) {
- $packet = $this->encrypt->encrypt($packet);
- }
- $packet.= $hmac;
- $start = strtok(microtime(), ' ') + strtok('');
- $result = strlen($packet) == fputs($this->fsock, $packet);
- $stop = strtok(microtime(), ' ') + strtok('');
- if (defined('NET_SSH2_LOGGING')) {
- $current = strtok(microtime(), ' ') + strtok('');
- $message_number = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')';
- $message_number = '-> ' . $message_number .
- ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
- $this->_append_log($message_number, $data);
- $this->last_packet = $current;
- }
- return $result;
- }
-
- function _append_log($message_number, $message)
- {
- switch (NET_SSH2_LOGGING) {
-
- case NET_SSH2_LOG_SIMPLE:
- $this->message_number_log[] = $message_number;
- break;
-
- case NET_SSH2_LOG_COMPLEX:
- $this->message_number_log[] = $message_number;
- $this->_string_shift($message);
- $this->log_size+= strlen($message);
- $this->message_log[] = $message;
- while ($this->log_size > NET_SSH2_LOG_MAX_SIZE) {
- $this->log_size-= strlen(array_shift($this->message_log));
- array_shift($this->message_number_log);
- }
- break;
-
-
-
- case NET_SSH2_LOG_REALTIME:
- echo "<pre>\r\n" . $this->_format_log(array($message), array($message_number)) . "\r\n</pre>\r\n";
- @flush();
- @ob_flush();
- break;
-
-
-
-
- case NET_SSH2_LOG_REALTIME_FILE:
- if (!isset($this->realtime_log_file)) {
-
- $filename = NET_SSH2_LOG_REALTIME_FILENAME;
- $fp = fopen($filename, 'w');
- $this->realtime_log_file = $fp;
- }
- if (!is_resource($this->realtime_log_file)) {
- break;
- }
- $entry = $this->_format_log(array($message), array($message_number));
- if ($this->realtime_log_wrap) {
- $temp = "<<< START >>>\r\n";
- $entry.= $temp;
- fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp));
- }
- $this->realtime_log_size+= strlen($entry);
- if ($this->realtime_log_size > NET_SSH2_LOG_MAX_SIZE) {
- fseek($this->realtime_log_file, 0);
- $this->realtime_log_size = strlen($entry);
- $this->realtime_log_wrap = true;
- }
- fputs($this->realtime_log_file, $entry);
- }
- }
-
- function _send_channel_packet($client_channel, $data)
- {
- while (strlen($data) > $this->packet_size_client_to_server[$client_channel]) {
- $packet = pack('CN2a*',
- NET_SSH2_MSG_CHANNEL_DATA,
- $this->server_channels[$client_channel],
- $this->packet_size_client_to_server[$client_channel],
- $this->_string_shift($data, $this->packet_size_client_to_server[$client_channel])
- );
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- }
- return $this->_send_binary_packet(pack('CN2a*',
- NET_SSH2_MSG_CHANNEL_DATA,
- $this->server_channels[$client_channel],
- strlen($data),
- $data));
- }
-
- function _close_channel($client_channel)
- {
-
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
- $this->channel_status[$client_channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
- $this->curTimeout = 0;
- while (!is_bool($this->_get_channel_packet($client_channel)));
- if ($this->bitmap & NET_SSH2_MASK_SHELL) {
- $this->bitmap&= ~NET_SSH2_MASK_SHELL;
- }
- }
-
- function _disconnect($reason)
- {
- if ($this->bitmap) {
- $data = pack('CNNa*Na*', NET_SSH2_MSG_DISCONNECT, $reason, 0, '', 0, '');
- $this->_send_binary_packet($data);
- $this->bitmap = 0;
- fclose($this->fsock);
- return false;
- }
- }
-
- function _string_shift(&$string, $index = 1)
- {
- $substr = substr($string, 0, $index);
- $string = substr($string, $index);
- return $substr;
- }
-
- function _define_array()
- {
- $args = func_get_args();
- foreach ($args as $arg) {
- foreach ($arg as $key=>$value) {
- if (!defined($value)) {
- define($value, $key);
- } else {
- break 2;
- }
- }
- }
- }
-
- function getLog()
- {
- if (!defined('NET_SSH2_LOGGING')) {
- return false;
- }
- switch (NET_SSH2_LOGGING) {
- case NET_SSH2_LOG_SIMPLE:
- return $this->message_number_log;
- break;
- case NET_SSH2_LOG_COMPLEX:
- return $this->_format_log($this->message_log, $this->message_number_log);
- break;
- default:
- return false;
- }
- }
-
- function _format_log($message_log, $message_number_log)
- {
- static $boundary = ':', $long_width = 65, $short_width = 16;
- $output = '';
- for ($i = 0; $i < count($message_log); $i++) {
- $output.= $message_number_log[$i] . "\r\n";
- $current_log = $message_log[$i];
- $j = 0;
- do {
- if (strlen($current_log)) {
- $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
- }
- $fragment = $this->_string_shift($current_log, $short_width);
- $hex = substr(
- preg_replace(
- '#(.)#es',
- '"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)',
- $fragment),
- strlen($boundary)
- );
-
-
-
- $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
- $output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n";
- $j++;
- } while (strlen($current_log));
- $output.= "\r\n";
- }
- return $output;
- }
-
- function getErrors()
- {
- return $this->errors;
- }
-
- function getLastError()
- {
- return $this->errors[count($this->errors) - 1];
- }
-
- function getServerIdentification()
- {
- return $this->server_identifier;
- }
-
- function getKexAlgorithms()
- {
- return $this->kex_algorithms;
- }
-
- function getServerHostKeyAlgorithms()
- {
- return $this->server_host_key_algorithms;
- }
-
- function getEncryptionAlgorithmsClient2Server()
- {
- return $this->encryption_algorithms_client_to_server;
- }
-
- function getEncryptionAlgorithmsServer2Client()
- {
- return $this->encryption_algorithms_server_to_client;
- }
-
- function getMACAlgorithmsClient2Server()
- {
- return $this->mac_algorithms_client_to_server;
- }
-
- function getMACAlgorithmsServer2Client()
- {
- return $this->mac_algorithms_server_to_client;
- }
-
- function getCompressionAlgorithmsClient2Server()
- {
- return $this->compression_algorithms_client_to_server;
- }
-
- function getCompressionAlgorithmsServer2Client()
- {
- return $this->compression_algorithms_server_to_client;
- }
-
- function getLanguagesServer2Client()
- {
- return $this->languages_server_to_client;
- }
-
- function getLanguagesClient2Server()
- {
- return $this->languages_client_to_server;
- }
-
- function getBannerMessage()
- {
- return $this->banner_message;
- }
-
- function getServerPublicHostKey()
- {
- $signature = $this->signature;
- $server_public_host_key = $this->server_public_host_key;
- extract(unpack('Nlength', $this->_string_shift($server_public_host_key, 4)));
- $this->_string_shift($server_public_host_key, $length);
- if ($this->signature_validated) {
- return $this->bitmap ?
- $this->signature_format . ' ' . base64_encode($this->server_public_host_key) :
- false;
- }
- $this->signature_validated = true;
- switch ($this->signature_format) {
- case 'ssh-dss':
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $p = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $q = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $g = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $y = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- $temp = unpack('Nlength', $this->_string_shift($signature, 4));
- if ($temp['length'] != 40) {
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $r = new Math_BigInteger($this->_string_shift($signature, 20), 256);
- $s = new Math_BigInteger($this->_string_shift($signature, 20), 256);
- if ($r->compare($q) >= 0 || $s->compare($q) >= 0) {
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $w = $s->modInverse($q);
- $u1 = $w->multiply(new Math_BigInteger(sha1($this->exchange_hash), 16));
- list(, $u1) = $u1->divide($q);
- $u2 = $w->multiply($r);
- list(, $u2) = $u2->divide($q);
- $g = $g->modPow($u1, $p);
- $y = $y->modPow($u2, $p);
- $v = $g->multiply($y);
- list(, $v) = $v->divide($p);
- list(, $v) = $v->divide($q);
- if (!$v->equals($r)) {
- user_error('Bad server signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
- break;
- case 'ssh-rsa':
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $e = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $n = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $nLength = $temp['length'];
-
- $temp = unpack('Nlength', $this->_string_shift($signature, 4));
- $s = new Math_BigInteger($this->_string_shift($signature, $temp['length']), 256);
-
-
-
-
- if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) {
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $s = $s->modPow($e, $n);
- $s = $s->toBytes();
- $h = pack('N4H*', 0x00302130, 0x0906052B, 0x0E03021A, 0x05000414, sha1($this->exchange_hash));
- $h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 3 - strlen($h)) . $h;
- if ($s != $h) {
- user_error('Bad server signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
- break;
- default:
- user_error('Unsupported signature format');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
- return $this->signature_format . ' ' . base64_encode($this->server_public_host_key);
- }
-
- function getExitStatus()
- {
- if (is_null($this->exit_status)) {
- return false;
- }
- return $this->exit_status;
- }
- }
|