Library libssh2

Provides a binding for the libssh2 library.

SSH2 is a complex protocol and libssh2 simplifies many tasks involved in interacting with ssh servers. This module provides bindings for some of the most commonly used libssh2 functions. You may wish to use the functionality in libssh2-utility instead, which wraps many of the functions here in an easier to use class, SSHConnection.

For performance reasons, the modules reuses the NSE's existing nsock socket pool.

Authors:

  • Devin Bjelland
  • Sergey Khegay

Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html

Functions

channel_close (session, channel)

Gracefully closes open libssh2 channel

channel_eof (channel)

Checks if server has sent EOF on libssh2 channel

channel_exec (session, channel, cmd)

Executes command on libssh2 channel and returns output

channel_read (session, channel)

Reads data from stdin on libssh2 channel.

channel_read_stderr (session, channel)

Reads data from stderr on libssh2 channel.

channel_send_eof (session, channel)

Sends EOF on libssh2 channel. Note that the server may continue to send data until it sends its own EOF (which can be checked with channel_eof()

channel_write (session, channel, buffer)

Writes data to libssh2 channel. Not garenteed to write entire buffer.

hostkey_hash (session, hashtype)

Returns SHA1 or MD5 hostkey hash of session

open_channel (session)

Opens channel on authenticated ssh2 session and sets it to pseudo terminal mode.

publickey_canauth (session, username, publickeydata)

Checks to see if ssh server accepts public key for authentication as given user. This doesn't require the private key as it doesn't finish authenticating.

read_publickey (publickeyfile)

Read publickey from id_*.pub type key file

session_close (session)

Gracefully closes connected libssh2 session

session_open (host, port)

Creates libssh2 session and performs handshake

set_timeout (session, timeout)

Sets timeout of libssh2 session

userauth_list (session)

Returns list of authentication methods supported by the server

userauth_password (session, username, password)

Attempts to authenicate libssh2 session using provided credentials

userauth_publickey (session, username, privatekeyfile, passphrase, publickeyfile)

Attempts to authenticate libssh2 session using provided publickey

Functions

channel_close (session, channel)

Gracefully closes open libssh2 channel

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel
channel_eof (channel)

Checks if server has sent EOF on libssh2 channel

Parameters

channel
Open libssh2 channel

Return value:

true/false depending on whether server has send EOF
channel_exec (session, channel, cmd)

Executes command on libssh2 channel and returns output

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel
cmd
String containing command to execute

Return value:

String containing output from command
channel_read (session, channel)

Reads data from stdin on libssh2 channel.

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel

Return value:

string containing data read from channel
channel_read_stderr (session, channel)

Reads data from stderr on libssh2 channel.

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel

Return value:

string containing data read from channel
channel_send_eof (session, channel)

Sends EOF on libssh2 channel. Note that the server may continue to send data until it sends its own EOF (which can be checked with channel_eof()

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel
channel_write (session, channel, buffer)

Writes data to libssh2 channel. Not garenteed to write entire buffer.

Parameters

session
Authenticated libssh2 session
channel
Open libssh2 channel
buffer
String containing data to be written

Return value:

Number of bytes written to channel
hostkey_hash (session, hashtype)

Returns SHA1 or MD5 hostkey hash of session

Parameters

session
Connected libssh2 session.
hashtype
(optional) "sha1" or "md5" (sha1 is default)

Return value:

hostkey hash of server
open_channel (session)

Opens channel on authenticated ssh2 session and sets it to pseudo terminal mode.

Parameters

session
Authenticated libssh2 session

Return value:

libssh2 channel
publickey_canauth (session, username, publickeydata)

Checks to see if ssh server accepts public key for authentication as given user. This doesn't require the private key as it doesn't finish authenticating.

Parameters

session
Connected libssh2 session
username
Username to authenicate as
publickeydata
String containing raw publickey blob

Return value:

true/false, depending on whether user can authenticate with given key
read_publickey (publickeyfile)

Read publickey from id_*.pub type key file

Parameters

publickeyfile
File containing publickey

Return value:

string containing raw key data
session_close (session)

Gracefully closes connected libssh2 session

Parameters

session
Connected libssh2 session
session_open (host, port)

Creates libssh2 session and performs handshake

Parameters

host
Host to connect to.
port
Port to connect to.

Return value:

session or nil on failure
set_timeout (session, timeout)

Sets timeout of libssh2 session

Parameters

session
Connected libssh2 session.
timeout
Timeout for session in milliseconds.
userauth_list (session)

Returns list of authentication methods supported by the server

Parameters

session
Connected libssh2 session.

Return value:

List of supported authentication methods/
userauth_password (session, username, password)

Attempts to authenicate libssh2 session using provided credentials

Parameters

session
 
username
Username to authenicate as.
password
Password to authenicate with.

Return value:

true/false, depending on success
userauth_publickey (session, username, privatekeyfile, passphrase, publickeyfile)

Attempts to authenticate libssh2 session using provided publickey

Parameters

session
Connected libssh2 session
username
Username to authenicate as
privatekeyfile
File containing privatekey
passphrase
Passphrase for privatekey
publickeyfile
File containing publickey. Not necessary if libssh2 is compiled against OpenSSL

Return value:

true/false, depending on success