Documentation

SSH1
in package

Pure-PHP implementation of SSHv1.

Tags
author

Jim Wigginton terrafrost@php.net

access

public

Table of Contents

AUTH_PASSWORD  = 3
password authentication
AUTH_RHOSTS  = 1
AUTH_RHOSTS_RSA  = 4
AUTH_RSA  = 2
pure RSA authentication
CIPHER_3DES  = 3
Triple-DES in CBC mode
CIPHER_BLOWFISH  = 6
Blowfish
CIPHER_BROKEN_TSS  = 4
TRI's Simple Stream encryption CBC
CIPHER_DES  = 2
DES in CBC mode
CIPHER_IDEA  = 1
IDEA in CFB mode
CIPHER_NONE  = 0
No encryption
LOG_COMPLEX  = 2
Returns the message content
LOG_REALTIME  = 3
Outputs the content real-time
LOG_REALTIME_FILE  = 4
Dumps the content real-time to a file
LOG_SIMPLE  = 1
Returns the message numbers
MASK_CONNECTED  = 0x2
MASK_CONSTRUCTOR  = 0x1
MASK_LOGIN  = 0x4
MASK_SHELL  = 0x8
READ_REGEX  = 2
Returns when a string matching the regular expression $expect is found
READ_SIMPLE  = 1
Returns when a string matching $expect exactly is found
RESPONSE_DATA  = 2
The Response Data
RESPONSE_TYPE  = 1
The Response Type
TTY_OP_END  = 0
$bitmap  : int
Execution Bitmap
$cipher  : int
Default cipher
$connectionTimeout  : int
Timeout for initial connection
$crypto  : object
The cryptography object
$curTimeout  : mixed
Current Timeout
$fsock  : object
The Socket Object
$host  : string
Hostname
$host_key_public_exponent  : string
The Host Key Public Exponent
$host_key_public_modulus  : string
The Host Key Public Modulus
$identifier  : string
The SSH identifier
$interactiveBuffer  : array<string|int, mixed>
Interactive Buffer
$log_boundary  : mixed
Log Boundary
$log_long_width  : mixed
Log Long Width
$log_short_width  : mixed
Log Short Width
$message_log  : array<string|int, mixed>
Message Log
$port  : int
Port Number
$protocol_flag_log  : array<string|int, mixed>
Protocol Flag Log
$protocol_flags  : array<string|int, mixed>
Protocol Flags
$realtime_log_file  : resource
Real-time log file pointer
$realtime_log_size  : int
Real-time log file size
$realtime_log_wrap  : bool
Real-time log file wrap boolean
$server_identification  : string
Server Identification
$server_key_public_exponent  : string
The Server Key Public Exponent
$server_key_public_modulus  : string
The Server Key Public Modulus
$supported_authentications  : array<string|int, mixed>
Supported Authentications
$supported_ciphers  : array<string|int, mixed>
Supported Ciphers
$timeout  : mixed
Timeout
__construct()  : SSH1
Default Constructor.
__destruct()  : mixed
Destructor.
_append_log()  : mixed
Logs data packets
_connect()  : bool
Connect to an SSHv1 server
_crc()  : int
Cyclic Redundancy Check (CRC)
_define_array()  : mixed
Define Array
_disconnect()  : mixed
Disconnect
_format_log()  : string
Formats a log for printing
_format_log_helper()  : string
Helper function for _format_log
_get_binary_packet()  : array<string|int, mixed>
Gets Binary Packets
_initShell()  : bool
Creates an interactive shell
_rsa_crypt()  : BigInteger
RSA Encrypt
_send_binary_packet()  : bool
Sends Binary Packets
_string_shift()  : string
String Shift
disconnect()  : mixed
Disconnect
exec()  : mixed
Executes a command on a non-interactive shell, returns the output, and quits.
getHostKeyPublicExponent()  : string
Return the host key public exponent
getHostKeyPublicModulus()  : string
Return the host key public modulus
getLog()  : array<string|int, mixed>|false|string
Returns a log of the packets that have been sent and received.
getServerIdentification()  : string
Return the server identification.
getServerKeyPublicExponent()  : string
Return the server key public exponent
getServerKeyPublicModulus()  : string
Return the server key public modulus
getSupportedAuthentications()  : array<string|int, mixed>
Return a list of authentications supported by SSH1 server.
getSupportedCiphers()  : array<string|int, mixed>
Return a list of ciphers supported by SSH1 server.
interactiveRead()  : string
Returns the output of an interactive shell when no more output is available.
interactiveWrite()  : bool
Inputs a command into an interactive shell.
login()  : bool
Login
read()  : bool
Returns the output of an interactive shell when there's a match for $expect
setTimeout()  : mixed
Set Timeout
write()  : bool
Inputs a command into an interactive shell.

Constants

AUTH_PASSWORD

password authentication

public mixed AUTH_PASSWORD = 3

This is the only method that is supported by this library.

AUTH_RHOSTS

public mixed AUTH_RHOSTS = 1

.rhosts or /etc/hosts.equiv

AUTH_RHOSTS_RSA

public mixed AUTH_RHOSTS_RSA = 4

.rhosts with RSA host authentication

AUTH_RSA

pure RSA authentication

public mixed AUTH_RSA = 2

CIPHER_3DES

Triple-DES in CBC mode

public mixed CIPHER_3DES = 3

All implementations are required to support this

CIPHER_BLOWFISH

Blowfish

public mixed CIPHER_BLOWFISH = 6

Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and uses it (see cipher.c)

CIPHER_BROKEN_TSS

TRI's Simple Stream encryption CBC

public mixed CIPHER_BROKEN_TSS = 4

Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h), although it doesn't use it (see cipher.c)

CIPHER_DES

DES in CBC mode

public mixed CIPHER_DES = 2

CIPHER_IDEA

IDEA in CFB mode

public mixed CIPHER_IDEA = 1

Not supported.

CIPHER_NONE

No encryption

public mixed CIPHER_NONE = 0

Not supported.

LOG_COMPLEX

Returns the message content

public mixed LOG_COMPLEX = 2

LOG_REALTIME

Outputs the content real-time

public mixed LOG_REALTIME = 3

LOG_REALTIME_FILE

Dumps the content real-time to a file

public mixed LOG_REALTIME_FILE = 4

LOG_SIMPLE

Returns the message numbers

public mixed LOG_SIMPLE = 1

MASK_CONNECTED

public mixed MASK_CONNECTED = 0x2

MASK_CONSTRUCTOR

public mixed MASK_CONSTRUCTOR = 0x1

MASK_LOGIN

public mixed MASK_LOGIN = 0x4

MASK_SHELL

public mixed MASK_SHELL = 0x8

READ_REGEX

Returns when a string matching the regular expression $expect is found

public mixed READ_REGEX = 2

READ_SIMPLE

Returns when a string matching $expect exactly is found

public mixed READ_SIMPLE = 1

TTY_OP_END

public mixed TTY_OP_END = 0

Properties

$bitmap

Execution Bitmap

public int $bitmap = 0

The bits that are set represent functions that have been called already. This is used to determine if a requisite function has been successfully executed. If not, an error should be thrown.

Tags
access

private

$cipher

Default cipher

public int $cipher
Tags
see
self::__construct()
see
self::_connect()
access

private

$connectionTimeout

Timeout for initial connection

public int $connectionTimeout

Set by the constructor call. Calling setTimeout() is optional. If it's not called functions like exec() won't timeout unless some PHP setting forces it too. The timeout specified in the constructor, however, is non-optional. There will be a timeout, whether or not you set it. If you don't it'll be 10 seconds. It is used by fsockopen() in that function.

Tags
see
self::__construct()
see
self::_connect()
access

private

$crypto

The cryptography object

public object $crypto = false
Tags
access

private

$curTimeout

Current Timeout

public mixed $curTimeout
Tags
see
self::_get_channel_packet()
access

private

$fsock

The Socket Object

public object $fsock
Tags
access

private

$host

Hostname

public string $host
Tags
see
self::__construct()
see
self::_connect()
access

private

$host_key_public_exponent

The Host Key Public Exponent

public string $host_key_public_exponent

Logged for debug purposes

Tags
see
self::getHostKeyPublicExponent()
access

private

$host_key_public_modulus

The Host Key Public Modulus

public string $host_key_public_modulus

Logged for debug purposes

Tags
see
self::getHostKeyPublicModulus()
access

private

$identifier

The SSH identifier

public string $identifier = 'SSH-1.5-phpseclib'
Tags
access

private

$interactiveBuffer

Interactive Buffer

public array<string|int, mixed> $interactiveBuffer = ''
Tags
see
self::read()
access

private

$log_boundary

Log Boundary

public mixed $log_boundary = ':'
Tags
see
self::_format_log()
access

private

$log_long_width

Log Long Width

public mixed $log_long_width = 65
Tags
see
self::_format_log()
access

private

$log_short_width

Log Short Width

public mixed $log_short_width = 16
Tags
see
self::_format_log()
access

private

$message_log

Message Log

public array<string|int, mixed> $message_log = array()
Tags
see
self::getLog()
access

private

$port

Port Number

public int $port
Tags
see
self::__construct()
see
self::_connect()
access

private

$protocol_flag_log

Protocol Flag Log

public array<string|int, mixed> $protocol_flag_log = array()
Tags
see
self::getLog()
access

private

$protocol_flags

Protocol Flags

public array<string|int, mixed> $protocol_flags = array()
Tags
see
self::__construct()
access

private

$realtime_log_file

Real-time log file pointer

public resource $realtime_log_file
Tags
see
self::_append_log()
access

private

$realtime_log_size

Real-time log file size

public int $realtime_log_size
Tags
see
self::_append_log()
access

private

$realtime_log_wrap

Real-time log file wrap boolean

public bool $realtime_log_wrap
Tags
see
self::_append_log()
access

private

$server_identification

Server Identification

public string $server_identification = ''
Tags
see
self::getServerIdentification()
access

private

$server_key_public_exponent

The Server Key Public Exponent

public string $server_key_public_exponent

Logged for debug purposes

Tags
see
self::getServerKeyPublicExponent()
access

private

$server_key_public_modulus

The Server Key Public Modulus

public string $server_key_public_modulus

Logged for debug purposes

Tags
see
self::getServerKeyPublicModulus()
access

private

$supported_authentications

Supported Authentications

public array<string|int, mixed> $supported_authentications = array(self::AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv', self::AUTH_RSA => 'pure RSA authentication', self::AUTH_PASSWORD => 'password authentication', self::AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication')

Logged for debug purposes

Tags
see
self::getSupportedAuthentications()
access

private

$supported_ciphers

Supported Ciphers

public array<string|int, mixed> $supported_ciphers = array(self::CIPHER_NONE => 'No encryption', self::CIPHER_IDEA => 'IDEA in CFB mode', self::CIPHER_DES => 'DES in CBC mode', self::CIPHER_3DES => 'Triple-DES in CBC mode', self::CIPHER_BROKEN_TSS => 'TRI's Simple Stream encryption CBC', self::CIPHER_RC4 => 'RC4', self::CIPHER_BLOWFISH => 'Blowfish')

Logged for debug purposes

Tags
see
self::getSupportedCiphers()
access

private

$timeout

Timeout

public mixed $timeout
Tags
see
self::setTimeout()
access

private

Methods

__construct()

Default Constructor.

public __construct(string $host[, int $port = 22 ][, int $timeout = 10 ][, int $cipher = self::CIPHER_3DES ]) : SSH1

Connects to an SSHv1 server

Parameters
$host : string
$port : int = 22
$timeout : int = 10
$cipher : int = self::CIPHER_3DES
Tags
access

public

Return values
SSH1

__destruct()

Destructor.

public __destruct() : mixed

Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call disconnect().

Tags
access

public

Return values
mixed

_append_log()

Logs data packets

public _append_log(mixed $protocol_flags, mixed $message) : mixed

Makes sure that only the last 1MB worth of packets will be logged

Parameters
$protocol_flags : mixed
$message : mixed
Tags
access

private

Return values
mixed

_connect()

Connect to an SSHv1 server

public _connect() : bool
Tags
access

private

Return values
bool

_crc()

Cyclic Redundancy Check (CRC)

public _crc(string $data) : int

PHP's crc32 function is implemented slightly differently than the one that SSH v1 uses, so we've reimplemented it. A more detailed discussion of the differences can be found after $crc_lookup_table's initialization.

Parameters
$data : string
Tags
see
self::_get_binary_packet()
see
self::_send_binary_packet()
access

private

Return values
int

_define_array()

Define Array

public _define_array() : mixed

Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of named constants from it, using the value as the name of the constant and the index as the value of the constant. If any of the constants that would be defined already exists, none of the constants will be defined.

Tags
access

private

Return values
mixed

_disconnect()

Disconnect

public _disconnect([string $msg = 'Client Quit' ]) : mixed
Parameters
$msg : string = 'Client Quit'
Tags
access

private

Return values
mixed

_format_log()

Formats a log for printing

public _format_log(array<string|int, mixed> $message_log, array<string|int, mixed> $message_number_log) : string
Parameters
$message_log : array<string|int, mixed>
$message_number_log : array<string|int, mixed>
Tags
access

private

Return values
string

_format_log_helper()

Helper function for _format_log

public _format_log_helper(array<string|int, mixed> $matches) : string

For use with preg_replace_callback()

Parameters
$matches : array<string|int, mixed>
Tags
access

private

Return values
string

_get_binary_packet()

Gets Binary Packets

public _get_binary_packet() : array<string|int, mixed>

See 'The Binary Packet Protocol' of protocol-1.5.txt for more info.

Also, this function could be improved upon by adding detection for the following exploit: http://www.securiteam.com/securitynews/5LP042K3FY.html

Tags
see
self::_send_binary_packet()
access

private

Return values
array<string|int, mixed>

_initShell()

Creates an interactive shell

public _initShell() : bool
Tags
see
self::interactiveRead()
see
self::interactiveWrite()
access

private

Return values
bool

_rsa_crypt()

RSA Encrypt

public _rsa_crypt(BigInteger $m, array<string|int, mixed> $key) : BigInteger

Returns mod(pow($m, $e), $n), where $n should be the product of two (large) primes $p and $q and where $e should be a number with the property that gcd($e, ($p - 1) * ($q - 1)) == 1. Could just make anything that calls this call modexp, instead, but I think this makes things clearer, maybe...

Parameters
$m : BigInteger
$key : array<string|int, mixed>
Tags
see
self::__construct()
access

private

Return values
BigInteger

_send_binary_packet()

Sends Binary Packets

public _send_binary_packet(string $data) : bool

Returns true on success, false on failure.

Parameters
$data : string
Tags
see
self::_get_binary_packet()
access

private

Return values
bool

_string_shift()

String Shift

public _string_shift(string &$string[, int $index = 1 ]) : string

Inspired by array_shift

Parameters
$string : string
$index : int = 1
Tags
access

private

Return values
string

disconnect()

Disconnect

public disconnect() : mixed
Tags
access

public

Return values
mixed

exec()

Executes a command on a non-interactive shell, returns the output, and quits.

public exec(string $cmd[, mixed $block = true ]) : mixed

An SSH1 server will close the connection after a command has been executed on a non-interactive shell. SSH2 servers don't, however, this isn't an SSH2 client. The way this works, on the server, is by initiating a shell with the -s option, as discussed in the following links:

http://www.faqs.org/docs/bashman/bashref_65.html http://www.faqs.org/docs/bashman/bashref_62.html

To execute further commands, a new \phpseclib\Net\SSH1 object will need to be created.

Returns false on failure and the output, otherwise.

Parameters
$cmd : string
$block : mixed = true
Tags
see
self::interactiveRead()
see
self::interactiveWrite()
access

public

Return values
mixed

getHostKeyPublicExponent()

Return the host key public exponent

public getHostKeyPublicExponent([bool $raw_output = false ]) : string

Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, the raw bytes. This behavior is similar to PHP's md5() function.

Parameters
$raw_output : bool = false
Tags
access

public

Return values
string

getHostKeyPublicModulus()

Return the host key public modulus

public getHostKeyPublicModulus([bool $raw_output = false ]) : string

Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, the raw bytes. This behavior is similar to PHP's md5() function.

Parameters
$raw_output : bool = false
Tags
access

public

Return values
string

getLog()

Returns a log of the packets that have been sent and received.

public getLog() : array<string|int, mixed>|false|string

Returns a string if NET_SSH1_LOGGING == self::LOG_COMPLEX, an array if NET_SSH1_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING')

Tags
access

public

Return values
array<string|int, mixed>|false|string

getServerIdentification()

Return the server identification.

public getServerIdentification() : string
Tags
access

public

Return values
string

getServerKeyPublicExponent()

Return the server key public exponent

public getServerKeyPublicExponent([bool $raw_output = false ]) : string

Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, the raw bytes. This behavior is similar to PHP's md5() function.

Parameters
$raw_output : bool = false
Tags
access

public

Return values
string

getServerKeyPublicModulus()

Return the server key public modulus

public getServerKeyPublicModulus([bool $raw_output = false ]) : string

Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, the raw bytes. This behavior is similar to PHP's md5() function.

Parameters
$raw_output : bool = false
Tags
access

public

Return values
string

getSupportedAuthentications()

Return a list of authentications supported by SSH1 server.

public getSupportedAuthentications([bool $raw_output = false ]) : array<string|int, mixed>

Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output is set to true, returns, instead, an array of constants. ie. instead of array('password authentication'), you'll get array(self::AUTH_PASSWORD).

Parameters
$raw_output : bool = false
Tags
access

public

Return values
array<string|int, mixed>

getSupportedCiphers()

Return a list of ciphers supported by SSH1 server.

public getSupportedCiphers([bool $raw_output = false ]) : array<string|int, mixed>

Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output is set to true, returns, instead, an array of constants. ie. instead of array('Triple-DES in CBC mode'), you'll get array(self::CIPHER_3DES).

Parameters
$raw_output : bool = false
Tags
access

public

Return values
array<string|int, mixed>

interactiveRead()

Returns the output of an interactive shell when no more output is available.

public interactiveRead() : string

Requires PHP 4.3.0 or later due to the use of the stream_select() function. If you see stuff like "^[[00m", you're seeing ANSI escape codes. According to How to Enable ANSI.SYS in a Command Window, "Windows NT does not support ANSI escape sequences in Win32 Console applications", so if you're a Windows user, there's not going to be much recourse.

Tags
see
self::interactiveRead()
access

public

Return values
string

interactiveWrite()

Inputs a command into an interactive shell.

public interactiveWrite(string $cmd) : bool
Parameters
$cmd : string
Tags
see
self::interactiveRead()
access

public

Return values
bool

login()

Login

public login(string $username[, string $password = '' ]) : bool
Parameters
$username : string
$password : string = ''
Tags
access

public

Return values
bool

read()

Returns the output of an interactive shell when there's a match for $expect

public read(string $expect[, int $mode = self::READ_SIMPLE ]) : bool

$expect can take the form of a string literal or, if $mode == self::READ_REGEX, a regular expression.

Parameters
$expect : string
$mode : int = self::READ_SIMPLE
Tags
see
self::write()
access

public

Return values
bool

setTimeout()

Set Timeout

public setTimeout(mixed $timeout) : mixed

$ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout. Setting $timeout to false or 0 will mean there is no timeout.

Parameters
$timeout : mixed
Return values
mixed

write()

Inputs a command into an interactive shell.

public write(string $cmd) : bool
Parameters
$cmd : string
Tags
see
self::interactiveWrite()
access

public

Return values
bool

Search results