Library pgsql

PostgreSQL library supporting both version 2 and version 3 of the protocol. The library currently contains the bare minimum to perform authentication. Authentication is supported with or without SSL enabled and using the plain-text or MD5 authentication mechanisms.

The PGSQL protocol is explained in detail in the following references.

Author:

  • Patrik Karlsson <patrik@cqure.net>

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

Source: https://svn.nmap.org/nmap/nselib/pgsql.lua

Functions

[MessageType.AuthRequest] (data, len, pos)

Decodes an Auth Request packet

[MessageType.AuthRequest] (data, len, pos)

Decodes an Auth Request packet

[MessageType.BackendKeyData] (data, len, pos)

Decodes the BackendKeyData packet

[MessageType.Error] (data, len, pos)

Decodes an Error packet

[MessageType.Error] (data, len, pos)

Decodes an Error packet

[MessageType.ParameterStatus] (data, len, pos)

Decodes an ParameterStatus packet

[MessageType.ReadyForQuery] (data, len, pos)

Decodes an ReadyForQuery packet

createMD5LoginHash (username, password, salt)

Creates a cryptographic hash to be used for login

decodeHeader (data, pos)

Decodes the postgres header

detectVersion (host, port)

Attempts to determine if the server supports v3 or v2 of the protocol

loginRequest (socket, params, username, password, salt)

Attempts to authenticate to the pgsql server Supports plain-text and MD5 authentication

loginRequest (socket, params, username, password, salt)

Attempts to authenticate to the pgsql server Supports plain-text and MD5 authentication

printErrorMessage (dberror)

Prints the contents of the error table returned from the Error message decoder

processResponse (data, pos)

Process the server response

processResponse (data, pos)

Process the server response

readPacket (socket, data, pos)

Reads a packet and handles additional socket reads to retrieve remaining data

readPacket (socket, data, pos)

Reads a packet and handles additional socket reads to retrieve remaining data

requestSSL (socket)

Sends a packet requesting SSL communication to be activated

sendStartup (socket, user, database)

Sends a startup message to the server containing the username and database to connect to

sendStartup (socket, user, database)

Sends a startup message to the server containing the username and database to connect to

zeroPad (str, len)

Pad a string with zeroes

Functions

[MessageType.AuthRequest] (data, len, pos)

Decodes an Auth Request packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding, -1 on error
  2. response table containing zero or more of the following salt and success error string containing error message if pos is -1
[MessageType.AuthRequest] (data, len, pos)

Decodes an Auth Request packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding, -1 on error
  2. response table containing zero or more of the following salt and success error string containing error message if pos is -1
[MessageType.BackendKeyData] (data, len, pos)

Decodes the BackendKeyData packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding, -1 on error
  2. response table containing zero or more of the following pid and key error string containing error message if pos is -1
[MessageType.Error] (data, len, pos)

Decodes an Error packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding
  2. response table containing zero or more of the following error.severity, error.code, error.message, error.file, error.line and error.routine
[MessageType.Error] (data, len, pos)

Decodes an Error packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding
  2. response table containing zero or more of the following error.severity, error.code, error.message, error.file, error.line and error.routine
[MessageType.ParameterStatus] (data, len, pos)

Decodes an ParameterStatus packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding
  2. response table containing zero or more of the following key and value
[MessageType.ReadyForQuery] (data, len, pos)

Decodes an ReadyForQuery packet

Parameters

data
string containing raw data received from socket
len
number containing the length as retrieved from the header
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding, -1 on error
  2. response table containing zero or more of the following status error string containing error message if pos is -1
createMD5LoginHash (username, password, salt)

Creates a cryptographic hash to be used for login

Parameters

username
username
password
password
salt
salt

Return value:

string suitable for login request
decodeHeader (data, pos)

Decodes the postgres header

Parameters

data
string containing the server response
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing the offset after decoding
  2. header table containing type and len
detectVersion (host, port)

Attempts to determine if the server supports v3 or v2 of the protocol

Parameters

host
table
port
table

Return value:

class v2 or v3
loginRequest (socket, params, username, password, salt)

Attempts to authenticate to the pgsql server Supports plain-text and MD5 authentication

Parameters

socket
socket already connected to the pgsql server
params
table containing any additional parameters authtype, version
username
string containing the username to use for authentication
password
string containing the password to use for authentication
salt
string containing the cryptographic salt value

Return values:

  1. status true on success, false on failure
  2. result table containing parameter status information, result string containing an error message if login fails
loginRequest (socket, params, username, password, salt)

Attempts to authenticate to the pgsql server Supports plain-text and MD5 authentication

Parameters

socket
socket already connected to the pgsql server
params
table containing any additional parameters authtype, version
username
string containing the username to use for authentication
password
string containing the password to use for authentication
salt
string containing the cryptographic salt value

Return values:

  1. status true on success, false on failure
  2. result table containing parameter status information, result string containing an error message if login fails
printErrorMessage (dberror)

Prints the contents of the error table returned from the Error message decoder

Parameters

dberror
table containing the error
processResponse (data, pos)

Process the server response

Parameters

data
string containing the server response
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing offset after decoding
  2. response string containing decoded data error message if pos is -1
processResponse (data, pos)

Process the server response

Parameters

data
string containing the server response
pos
number containing the offset into the data buffer

Return values:

  1. pos number containing offset after decoding
  2. response string containing decoded data error message if pos is -1
readPacket (socket, data, pos)

Reads a packet and handles additional socket reads to retrieve remaining data

Parameters

socket
socket already connected to the pgsql server
data
string containing any data already retrieved from the socket
pos
number containing the offset into the data buffer

Return value:

data string containing the initial and any additional data
readPacket (socket, data, pos)

Reads a packet and handles additional socket reads to retrieve remaining data

Parameters

socket
socket already connected to the pgsql server
data
string containing any data already retrieved from the socket
pos
number containing the offset into the data buffer

Return value:

data string containing the initial and any additional data
requestSSL (socket)

Sends a packet requesting SSL communication to be activated

Parameters

socket
socket already connected to the pgsql server

Return value:

boolean true if request was accepted, false if request was denied
sendStartup (socket, user, database)

Sends a startup message to the server containing the username and database to connect to

Parameters

socket
socket already connected to the pgsql server
user
string containing the name of the user
database
string containing the name of the database

Return values:

  1. status true on success, false on failure
  2. table containing a processed response from processResponse string containing error message if status is false
sendStartup (socket, user, database)

Sends a startup message to the server containing the username and database to connect to

Parameters

socket
socket already connected to the pgsql server
user
string containing the name of the user
database
string containing the name of the database

Return values:

  1. status true on success, false on failure
  2. table containing a processed response from processResponse string containing error message if status is false
zeroPad (str, len)

Pad a string with zeroes

Parameters

str
string containing the string to be padded
len
number containing the wanted length

Return value:

string containing the padded string value