Skip to main content

Object Global table T

The global table named T can be accessed from anywhere. It defines some constants and utility functions.

Global Table T index

NameInfo
T.contextidA number that identifies the threading execution context of this lua script. You can have more than one instance of your script loaded see Threading
T.execution_contextidSame as T.contextid
T.probeidA string that identifies the probe within the domain running this script, such as probe0
T.probeversionThe version of the Trisul Probe running this script, such as 7.0.2779
T.context_nameThe database context name.
T.argsThe -args command line option from trisul
T.enginetypeA string backend or frontend – scripts may want to use this
T.hostHost methods that can be called from LUA
T.KConstants
T.utilUtility methods
T.asyncMethods to call async LUA functions out of fast packet path
T.re2A fast and powerful regex engine (Google RE2)
T.acA minimal but fast Aho-Corasick multi pattern matcher
T.logFunction to log a message that goes into the main Trisul logging framework.
T.countergroupsA table of ( countergroup name, guid ) currently loaded. Only for backend scripts. For frontend scripts this field has a nil
T.resourcegroupsBackend scripts only : A table of ( resourcegroup name, guid ) currently loaded.
T.ftsgroupsBackend scripts only : A table of ( FTS (Full Text Search) name, guid ) currently loaded.
T.sessiongroupsBackend scripts only : A table of ( session group name, guid ) currently loaded.
T.envEnvironment and Trisul Config file

Table T.host

Interact with the Trisul environment.

Use the object calling notation T.host:function(..) to invoke these methods.

NameInOutDescription
get_homenetsnoneTable, Array of [ string IP, string Netmask ]Get home networks defined by Trisul.
is_homenetnumber 32bit IPv4 or string IPv4 in dotted decimalboolIs the 32-bit IPv4 address within the home network?

LUACopyT.host:is_homenet(“192.168.2.1”)
is_homenet_keystring IPv4 in trisul key formatboolIs the Trisul key format IP address in the home network.
get_configpathnonestring directoryConfiguration directory
get_datapathnonestring directoryData directory
createkeyguid counter group id, string – key, string – labelCreate a userlabel for a given key. Use this to pre-load human labels for keys
prepare_configguid – plugin id, string template fileplugin configPrepare a configuration file for your plugin
broadcastguid – message id, guid class id, string messagenoneBroadcast a state update to other plugins

Table T.K

Pre-defined constants to use with other Lua functions.

NameTypeNumeric values
loglevelLog levelsEMERG, FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG
Usage : print(T.K.loglevel.ERROR) => 4
vartypesMeter typesCOUNTER, RATE_COUNTER, GAUGE, RUNNING_COUNTER

Constants T.K.vartype

Constants : Types of counters.

This table defines counter types supported by Trisul. You typically use this table when creating custom countergroups

Meter TypeDescription
COUNTERIncrement a counter that resets to zero at start of every time bucket
RATE_COUNTEREqual to COUNTER / Bucket Size in seconds
GAUGEInstantaneous values
RUNNING_COUNTERIncrement or decrement a counter, does not reset every time bucket
AVERAGEAverage of all samples in the window.
DELTA_RATE_COUNTERDelta counter per second. Use this for SNMP like samples that represent a running counter but when you need to do a c2 – c1 to get the total volume. The RATE part indicates that the final value will be (c2-c1)/bucketsize
MAXIMUMMaximum of all samples in the window
MINIMUMMinimum of all samples in the window

Table T.util

Useful utility functions written in C, exported to LUA via T.util table. We find them most handy for network related scripting.

-- called using T.util. (dot format)

local hexstr= T.util.bin2hex( binstr)
NameInOutDescription
ntopnumber 32-bit IPv4string IP in dotted decimal formatConvert a 32 bit number to IPv4 address string
ptonstring ip in dotted decimalnumber 32 bit IP addressConvert an IPv4 address string to a number
bornumber p ,number qnumber p OR qbitwise OR of two numbers
bandnumber p,number qnumber p AND qbitwise AND of two numbers
testbit32number p, number (bit position)boolTest bit position of a 32 bit number. LSB=0, MSB=31
T.util.testbit32(num,8)@ returns true if bit 8 = 1
bitval32number p, number (start bit), number widthnumberGet value of continous bits.
T.util.bitval32(num,20,4) returns the numeric value of bits 20,19,18,17. Bit numbering start from 0 (C-style)
splitstring the string to split, string the delimiterarray of string tokensSplit a string into tokens local tok_array = T.util.split(args,',')
splitmstring the string to split, string the delimitermultiple string returnsSplit a string into tokens and returns multiple values. Example local flowid,path = T.util.splitm(args,',')
hashstring to hash , optional number number of bits wide hashnumber the hash codeCompute a hash of the input string – Uses Murmur2 hash, output is n-bits wide.
local h =T.util.hash("Mystring",16) The second argument nbits is optional with a default of 32
bin2hexstring can include binarystring the hexConvert the binary to hex
hex2binstring a hexstring binaryConvert the hex to binary string
note

The bit utilities have been provided because we are using LuaJIT which does not support Lua5.2’s bit32 library.

Function T.re2

The LUA regex functionality is quite limited. Google RE2 is a fast, threadsafe, and powerful Regex engine. You may want to use this Regex engine over Lua’s default find and match methods in the following cases.

  1. T.re2 also allows you to employ very common string matching idioms like (octet-stream|application-x|application-pdf) which arent available in Lua’s find method.
  2. T.re2 allows you to precompile the regexes once and run them later
NameInOutDescription
T.re2string, table (optional)A re2 objectPrecompile the regex string and return an re2 object. The optional second argument is a table containing boolean RE2::Options.

RE2 Options

The following RE2 options are supported, the default values are in parantheses

//   utf8             (true)  text and pattern are UTF-8; otherwise Latin-1
// posix_syntax (false) restrict regexps to POSIX egrep syntax
// longest_match (false) search for longest match, not first match
// log_errors (true) log syntax and execution errors to ERROR
// literal (false) interpret string as literal, not regexp
// never_nl (false) never match \n, even if it is in regexp
// never_capture (false) parse all parens as non-capturing
// case_sensitive (true) match is case-sensitive (regexp can override
// with (?i) unless in posix_syntax mode)

Simple use

A sample illustrating a typical use.

-- precompile in onload
onload = function()

my_regex = T.re2("User-Agent\\s*:\\s*(.*)\r\n")

end

onflowattribute = function(...)
--
-- my_regex is a precompiled re2 object, just compare
-- or extract from a target string
---
if my_regex:partial_match( a_string ) then
..
end

With RE2 options

We create a RE2 object with a regex and the “case_sensitive” and another option

-- precompile in onload
onload = function()

my_regex = T.re2("User-Agent\\s*:\\s*(.*)\r\n", ( case_sensitive = true, posix_syntax = true))

end## [Function `T.ac`](/docs/lua/obj_globalt#function-_t.ac)

A fast and minimal Aho-Corasick multi pattern matcher.

T.actable (An array of patterns)An AC objectLoad all the patterns into an AC matcher and return an AC matcher object

A sample illustrating a typical use.

onload = function() 
-- add patterns in array and create a new AC matcher
ac_headers = T.ac(( "Host:",
"User-Agent:",
"Referer",
"Server:",
"Content-Type:",
"Content-Length:") )


-- later on you can use the match methods
ac_headers:match_all(...)

Function T.ac

A fast and minimal Aho-Corasick multi pattern matcher.

T.actable (An array of patterns)An AC objectLoad all the patterns into an AC matcher and return an AC matcher object

A sample illustrating a typical use.

A sample illustrating a typical use.

onload = function() 
-- add patterns in array and create a new AC matcher
ac_headers = T.ac({ "Host:",
"User-Agent:",
"Referer",
"Server:",
"Content-Type:",
"Content-Length:"} )


-- later on you can use the match methods
ac_headers:match_all(...)## Function `T.log`

Adds a log message to the main Trisul log file. Trisul automatically adds the lua script filename to the log message so you know where the message is actually coming from. Also see Printing and Logging from LUA script

NameInOutDescription
T.logT.K.loglevel – optional loglevel
string – log message
noneLog a message to the Trisul log file, usually located for the default setup in /usr/local/var/log/trisul-probe/domain0/probe0/context0 . The default loglevel is T.K.loglevel.DEBUG
T.logerrorstring – msgnoneUseful shortcut to log a message with loglevel of ERROR. T.logerror(msg) is the same as T.log(T.K.loglevel.ERROR,msg)
T.logwarningstring – msgnoneLog a message with WARN category
T.logdebugstring – msgnoneLog a message with DEBUG category
T.loginfostring – msgnoneLog a message with INFO category

Usage

The following snippets demonstrates the usage of T.log

T.log( T.K.loglevel.INFO,  "Your log message from lua ")
T.log( "This message has uses the default DEBUG log level ")

T.log(T.K.loglevel.ERROR, "This is an error from my LUA script")
T.logerror( "This is an error message from my LUA script, same as above T.log() call")

Function T.log

Adds a log message to the main Trisul log file. Trisul automatically adds the lua script filename to the log message so you know where the message is actually coming from. Also see Printing and Logging from LUA script

NameInOutDescription
T.logT.K.loglevel – optional loglevel
string – log message
noneLog a message to the Trisul log file, usually located for the default setup in /usr/local/var/log/trisul-probe/domain0/probe0/context0 . The default loglevel is T.K.loglevel.DEBUG
T.logerrorstring – msgnoneUseful shortcut to log a message with loglevel of ERROR. T.logerror(msg) is the same as T.log(T.K.loglevel.ERROR,msg)
T.logwarningstring – msgnoneLog a message with WARN category
T.logdebugstring – msgnoneLog a message with DEBUG category
T.loginfostring – msgnoneLog a message with INFO category

Usage

The following snippets demonstrates the usage of T.log

T.log( T.K.loglevel.INFO,  "Your log message from lua ")
T.log( "This message has uses the default DEBUG log level ")

T.log(T.K.loglevel.ERROR, "This is an error from my LUA script")
T.logerror( "This is an error message from my LUA script, same as above T.log() call")

Table T.alertgroups

Applicability

This section applies equally to T.countergroups, T.resourcegroups, T.sessionggroups, T.ftsgroups as well.

The purpose for this table is to provide a searchable name to guid mapping table of all the alertgroups currently loaded in Trisul. The backend alert_monitor scripts require you to specify a GUID that identifies the entity you are attaching the script to. If you do not know the GUID of the alert group but you know the name, you can use this table. See below.

Example alert_monitor code

If you know the GUID of the alert group named Malware Domain as (FAC478BC-8891-0009-5F31-80774B010086) you can attach an alert monitor as shown below

LUACopyTrisulPlugin = ( alert_monitor = ( alert_guid = '(FAC478BC-8891-0009-5F31-80774B010086)',

TrisulPlugin = ( 

alert_monitor = (

alert_guid = '(FAC478BC-8891-0009-5F31-80774B010086)',

Say you only know the name Malware Domain , this is where you use the T.alertgroups table to find the GUID

TrisulPlugin = ( 

alert_monitor = (

alert_guid = function()
for name ,guid in pairs(T.alertgroups) do
if name:match("Malware Domain") then return guid; end
end
end,

Table T.env

Trisul environment. Allows you to read probe configuration for the context in which the LUA script is loaded.

NameInOutDescription
configfilestring – path of config fileFull path of the trisulProbeConfig.xml file used by the running Trisul instance
get_configstring – ‘xml path’. See description columnstring – value of config parameterRead a configuration parameter from the Trisul Probe configuration file. The XML Path supported is a very simple format “Node>Node>..Node”. So to read the config parameter User under App parent node the path is App>User
domain_configfilestring – path of domain config fileFull path of the domain.xml configuration file used by the running instance. This config file is mainly used when your scripts want to connect and communicate to domain elements, such as TRP queries.
get_domain_configstring – ‘xml path’.string – config valueSimilar to get_config above but for the domain config file. Typical example T.env.get_domain_config("Domain>LocalReq") to find a local endpoint to connect to TRP

Usage

The most common use case of T.env is to find out where the following directories :

  • the “run state” directory — where per-run files, unix sockets are stored
  • the “config” directory — where persistent config is stored

Use 1 From a debug session

print("the path is "..T.env.configfile)

print("the username of Trisul process in config file (App>User) is "..T.env.get_config("App>User"))

Use 2 Opening a socket in runstate directory

local rundir  = T.env.get_config("App>RunStateDirectory")

local socket_file = rundir.."/myinput.socket"