Fortinet black logo

Global commands

Copy Link
Copy Doc ID 8173d994-aa85-11e9-81a4-00505692583a:277642
Download PDF

Global commands

Crc32(str)

Returns the crc32 check value of the string, or 0 if it is an empty string.

Syntax

crc32(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = "any string for crc32 calculation"

crc = crc32(str);

debug("crc is %d\n", crc);

}

FortiADC version: V5.2

Used in events: ALL

Key_gen(str_pass, str_salt, iter_num, len_num)

Create an AES key to encrypt/decrypt data, either generated by password or user defined.

Syntax

key_gen(str_pass, str_salt, iter_num, len_num);

Arguments

Name Description

str_pass

The password string.

str_salt

The salt string.

iter_num

The number of iterations.

len_num

The key length.

Examples

when HTTP_REQUEST {

new_key = key_gen("pass", "salt", 32, 32); -- first parameter is the password string, second the salt string, third the number of iterations, fourth the key length

debug("new key in hex is %s\n", to_HEX(new_key));

}

FortiADC version: V5.2

Used in events: ALL

Aes_enc(t)

encrypt the data using the previously-created AES key

Syntax

Aes_enc(t);

Arguments

Name Description

t

A table which specifies the message, key, size that used to encrypt.

Examples

when HTTP_REQUEST {

t={};

t["message"] = "MICK-TEST";

t["key"] = "aaaaaaaaaabbbbbb" --16bit

t["size"]= 128 -- 128, 192, or 256, the corresponding key length is 16, 24, and 32

enc = aes_enc(t)

debug("The aes_enc output to HEX\n %s\n",to_HEX(enc));

}

Note:

  • Message: a string which will be encrypted
  • Key: a string to encrypt str
  • Size: must be 128, 192, or 256, the corresponding key length is 16, 24, and 32.

FortiADC version: V5.2

Used in events: ALL

aes_dec(t)

decrypt the data using the previously-created AES key

Syntax

Aes_dec(t);

Arguments

Name Description

t

A table which specifies the message, key, size that used to decrypt.

Examples

when HTTP_REQUEST {

t={};

t["message"] = "MICK-TEST";

t["key"] = "aaaaaaaaaabbbbbb"

t["size"]= 128 -- 128, 192, or 256, the corresponding key length is 16, 24, and 32

enc = aes_enc(t)

--aes decryption

a={};

a["message"] = enc;

a["key"] = "aaaaaaaaaabbbbbb"

a["size"]= 128;

dec = aes_dec(a);

debug("key length %s decrypted is %s\n",”128” ,dec);

}

Note:

  • Message: a string which will be decrypted
  • Key: a string to decrypt str
  • Size: must be 128, 192, or 256, the corresponding key length is 16, 24, and 32

FortiADC version: V5.2

Used in events: ALL

EVP_Digest(alg, str)

EVP_Digest for oneshot digest calculation.

Syntax

EVP_Digest(alg, str);

Arguments

Name Description

alg

A string which specifies the algorithm.

str

A string which will be EVP_Digested.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

re = EVP_Digest(alg, data);

debug("the digest in hex is %s\n", to_HEX(re));

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

HMAC(alg, str, key)

HMAC message authentication code.

Syntax

HMAC(alg, str, key);

Arguments

Name Description

alg

A string which specifies the algorithm.

str

A string which will be calculated.

key

A string which is a secret key.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = HMAC(alg, data, key);

debug("the HMAC in hex is %s\n", to_HEX(re));

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

HMAC_verify(alg, data, key, verify)

Check if the signature is same as the current digest.

Syntax

HMAC_verify(alg, data, key verify);

Arguments

Name Description

alg

A string which specifies the algorithm.

key

A string which is a secret key.

data

A string which will be calculated.

verify

A signature to compare the current digest against.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

verify = "your result to compare"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = HMAC_verify(alg, data, key, verify);

if re then

debug("verified\n")

else

debug("not verified\n")

end

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

G2F(alg, key)

Returns a G2F random value.

Syntax

G2F(alg, key);

Arguments

Name Description

alg

A string which specifies the algorithm.

key

A string which is a secret key.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = G2F(alg, key);

debug("the G2F value is %d\n", re);

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

Class_match(str, method, list)

Used to match the string against an element.

Syntax

Class_match(str, method, list);

Arguments

Name Description

str

A string which will be matched.

method

A string which specifies the match method

list

A list which specifies the match target.

Examples

when HTTP_REQUEST {

url_list = “”

url = HTTP:uri_get()

status, count, t = class_match(url, "starts_with", url_list); --or "ends_with", "equals", "contains"

debug("status %s, count %s\n", status, count);

for k,v in pairs(t) do

debug("index %s, value %s\n", k,v);

end

}

Note:

Method: must be “starts_with”, “equals”, “contains”, “end_with”

This command return three parameter, first “status”: true or false means if match or not; second “count”: return the number of times matches; third “t”: return matched index and matched value in the list.

FortiADC version: V5.2

Used in events: ALL

Class_search(list, method, str)

Used to search an element in the list against a string.

Syntax

Class_search(list, method, str);

Arguments

Name Description

str

A string which will be calculated.

list

A string which will be matched.

method

A string which specifies the match method.

Examples

when HTTP_REQUEST {

status, count, t = class_search(url_list, "starts_with", url); --or "ends_with", "equals", "contains"

for k,v in pairs(t) do

debug("index %s, value %s\n", k,v);

end

}

Note:

Method: , must be “starts_with”, “equals”, “contains”, “end_with”

FortiADC version: V5.2

Used in events: ALL

Cmp_addr()

Used to match one IP address against a group of IP addresses.

It can automatically detect IPv4 and IPv6 and can be used to compare IPv4 addresses with IPv6 addresses

Syntax

Cmp_addr(client_Ip, addr_group );

Arguments

Name Description

Client_ip

For an IPv4 ip_addr/[mask], the mask can be a number between 0 and 32 or a dotted format like 255.255.255.0

For an IPv6 ip_addr/[mask], the mask can be a number between 0 and 128.

Addr_group

A group of IP address.

addr_group = "192.168.1.0/24" --first network address

addr_group = addr_group..",::ffff:172.30.1.0/120" --second

network address

Examples

when RULE_INIT{

--initialize the address group here

--for IPv4 address, mask can be a number between 0 to 32 or a dotted format

--support both IPv4 and IPv6, for IPv6, the mask is a number between 0 and 128

addr_group = "192.168.1.0/24"

addr_group = addr_group..",172.30.1.0/255.255.0.0"

addr_group = addr_group..",::ffff:172.40.1.0/120"

}

when HTTP_REQUEST{

client_ip = HTTP:client_addr()

matched = cmp_addr(client_ip, addr_group)

if matched then

debug("client ip found in address group\n");

else

debug("client ip not in address group\n");

end

}

FortiADC version: V4.8

Used in events: ALL

url_enc(str)

converted the url info a valid ASCII format

Syntax

url_enc(str);

Arguments

Name Description

str

A string which will be converted.

Examples

when HTTP_REQUEST {

url_list =https://abc.www.123.bbEEE.com/?5331=212&qe1=222

debug("Ori= %s \nencodeed= %s\n", url_list,url_enc(url_list));

}

FortiADC version: V5.2

Used in events: ALL

url_dec(str)

converted the encoding-url into a original url.

Syntax

url_dec(str);

Arguments

Name Description

str

A string which will be converted.

Examples

when HTTP_REQUEST {

str = "http%3A%2F%2Fwww.example.com%3A890%2Furl%2Fpath%2Fdata%3Fname%3Dforest%23nose“

debug("String= %s\ndecoded= %s\n", str,url_dec(str));

}

FortiADC version: V5.2

Used in events: ALL

url_parser(str)

Parse a url, return a table contains host, port, path, query, fragment, the username, password etc from the url.

Syntax

url_parser(str);

Arguments

Name Description

str

A url which will be parser.

Examples

when HTTP_REQUEST {

url_list="http://foo:bar@w1.superman.com/very/long/path.html?p1=v1&p2=v2#more-details"

purl = url_parser(url_list);

debug("parsed url scheme %s host %s\n port %s path %s query %s\n fragment %s, the username %s\n passowrd %s\n", purl["scheme"], purl["host"], purl["port"],purl["path"], purl["query"], purl["fragment"], purl["username"], purl["password"]);

}

FortiADC version: V5.2

Used in events: ALL

url_compare(url1, url2)

Compare two url string, return true if they are the same.

Syntax

url_compare(url1, url2);

Arguments

Name Description

url1, url2

Two urls which will be compared.

Examples

when HTTP_REQUEST {

url_list={};

url_list[1]="http://10.10.10.10:80/"

url_list[2]="http://10.10.10.10/"

url_list[3]="https://5.5.5.5:443/"

url_list[4]="https://5.5.5.5/"

url_list[5]="http://[2001::1]:80"

url_list[6]="http://[2001::1]"

url_list[7]="https://[2001:99:1]:443"

url_list[8]="https://[2001:99:1]"

for i = 1,8,2 do

if url_compare(url_list[i],url_list[i+1]) then

debug("URL_List %d %d Match !\n",i,i+1);

else

debug("URL_List %d %d NOT Match !\n",i,i+1);

end

end

}

FortiADC version: V5.2

Used in events: ALL

Rand()

Generate a random number. Returns is a integer number. After FAD reboot, the random number will be different.

Syntax

rand();

Arguments: N/A

Examples

when HTTP_REQUEST {

a = rand()

debug(“a = %d\n”, a)

}

FortiADC version: V5.2

Used in events: ALL

srand(str)

Set the random seed.

Syntax

srand(str);

Arguments

Name Description

str

A string which specifies the seed.

Examples

when HTTP_REQUEST {

srand(1111)

a = rand()

debug(“a = %d\n”, a)

}

FortiADC version: V5.2

Used in events: ALL

Rand_hex(int)

Generate a random number in HEX. Returns is a string, length is the <int>.

Syntax

Rand_hex(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

b = rand_hex(15)

debug("-----rand_hex b = %s-----\n", b)

}

Result:

-----rand_hex b = 43474FB47A8A8C4-----

FortiADC version: V5.2

Used in events: ALL

Rand_alphanum(int)

Generate a random alphabet+number sequence. Returns is a string, length is the <int>.

Syntax

Random_alphanum(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

c = rand_alphanum(17)

debug("-----rand_alphanum c = %s-----\n", c)

}

Result:

-----rand_alphanum c = XTHQpb6ngabMqH7nx-----

FortiADC version: V5.2

Used in events: ALL

Rand_seq(int)

Generate a random in sequence. Returns is a string, length is the <int>.

Syntax

Rand_seq(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

d = rand_seq(18)

debug("-----rand_seq d = %s-----\n", d)

}

Result:

FortiADC version: V5.2

Used in events: ALL

Time()

Returns the current time in a number which is the time since the Epoch measured in seconds.

Syntax

time();

Arguments: N/A

Examples

when HTTP_REQUEST {

t = time()

debug(“-----time: t %s-----\n”, t)

}

Result:

-----time: t 1561424783-----

FortiADC version: V4.8

Used in events: ALL

Ctime()

Returns a string descripting the current time like “Tue Jun 25 14:11:01 2019”.

Syntax

ctime();

Arguments: N/A

Examples

when HTTP_REQUEST {

ct = ctime()

debug(“-----ctime: ct %s-----\n”, ct)

}

Result:

-----ctime: ct Mon Jun 24 18:06:23 2019-----

FortiADC version: V4.8

Used in events: ALL

gmtime()

Returns the GMT time like “Thu 27 Jun 2019 18:27:42 GMT”.

Syntax

gmtime();

Arguments: N/A

Examples

when HTTP_REQUEST {

gt = gmtime()

debug(“-----gmtime: gt %s-----\n”, gt)

}

Result:

-----gmtime: gt Thu 27 Jun 2019 18:27:42 GMT -----

FortiADC version: V5.3

Used in events: ALL

Md5(str)

Rreturn the MD5 calculated for the string provided.

Syntax

Md5(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

md5 = md5(str1)

str = “test string”

a=12

md = md5(“%s,123%d”,str,a)

}

FortiADC version: V4.8

Used in events: ALL

Md5_hex(str)

Returns the md5 value in hex to the string

Syntax

Md5_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = md5_hex(str1)

}

FortiADC version: V4.8

Used in events: ALL

Md5_str(str)

Calculate the MD5 of a string input and stores the results in an intermediate variable, in some cases you need a version to deal with it.

Syntax

Md5_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

md5 = md5_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Md5_hex_str(str)

Calculate the MD5 of a string input of a string input and outputs the results in HEX format, in some cases you need a version to deal with it.

Syntax

Md5_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

md = md5_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha1(str)

Returns the sha1 calculated for the string provided.

Syntax

Sha1(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1(input)

}

FortiADC version: V4.8

Used in events: ALL

Sha1_hex(str)

Returns the sha1 calculated for the string in hex.

Syntax

Sha1_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “123456789”

sha1 = sha1_hex(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha1_str(str)

Calculate the SHA1 of a string input and stores the results in an intermediate variable, in some cases you need a version to deal with it.

Syntax

Sha1_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha1_hex_str(str)

Calculate the SHA1 of a string input and output the results in HEX format, in some cases you need a version to deal with it.

Syntax

Sha1_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1_hex_str(input); -- input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha256(str)

Calculates the SHA256 of a string input and store the result in an intermediate variable.

Syntax

Sha256(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha256(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha256_hex(str)

Calculate the SHA256 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha256_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha256 = sha256_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha256_str(str)

Calculate the SHA256 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha256_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha256_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha256_hex_str(str)

Calculate the SHA256 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha256_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha256_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha384(str)

Calculates the SHA384 of a string input and store the result in an intermediate variable.

Syntax

Sha384(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha384(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha384_hex(str)

Calculate the SHA384 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha384_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha384 = sha384_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha384_str(str)

Calculate the SHA384 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha384_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha384_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha384_hex_str(str)

Calculate the SHA384 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha384_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha384_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha512(str)

Calculates the SHA512 of a string input and store the result in an intermediate variable.

Syntax

Sha512(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha512(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha512_hex(str)

Calculate the SHA512 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha512_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha512 = sha512_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha512_str(str)

Calculate the SHA512 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha512_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha512_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha512_hex_str(str)

Calculate the SHA512 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha512_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha512_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

b32_enc(str)

encode a string input in base32 and output the result in string format.

Syntax

B32_enc(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “abc”

en = b32_enc(str)

}

FortiADC version: V5.2

Used in events: ALL

B32_enc_str(str)

Encode a string input in base32 and output the result in string format. In some cases you need a version to deal with it.

Syntax

B32_enc_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b32_enc_str(input); --input can be a cert in DER format

}

FortiADC version: V5.2

Used in events: ALL

B32_dec(str)

Decode a base32 encoded string input and output the result in string format

Syntax

B32_dec(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “abc”

dec = b32_dec(str)

}

FortiADC version: V5.2

Used in events: ALL

B32_dec_str(str)

Decode a base32 encoded string input and output the result in string format. In some cases you need a version to deal with it.

Syntax

B32_dec_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b32_dec_str(input); --input can be a cert in DER format

}

FortiADC version: V5.2

Used in events: ALL

B64_enc(str)

Returns base64 encryption of string

Syntax

B64_enc(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b64_enc(input);

--Input can be general format:

str=”test string”

a=12

en=b64_enc(“%s, 123 %d”, str, a);

}

FortiADC version: V4.8

Used in events: ALL

B64_dec(str)

Returns base64 decryption of string

Syntax

B64_dec(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b64_dec(input);

str=”test string”

a=12

de=b64_dec(“%s, 123 %d”, str, a);

}

FortiADC version: V4.8

Used in events: ALL

Get_pid()

Returns the PID value of the VS process.

Syntax

Get_pid();

Arguments: N/A

Examples

when HTTP_REQUEST {

pid = get_pid();

debug(“VS PID is : %d\n”, pid)

}

FortiADC version: V5.2

Used in events: ALL

table_to_string(t)

Returns the table in a string.

Syntax

Table_to_string(t);

Arguments

Name Description

t

A table which specifies the info.

Examples

when HTTP_REQUEST {

t={};

t[1]=97;

t[2]=98;

t[3]=99;

t[4]=1;

str = table_to_string(t);

debug(“str is %s\n”, str)

}

Result:

str is abc

FortiADC version: V4.8

Used in events: ALL

Htonl(int)

Convert a 32 bit long integer from host byte order to network byte order

Syntax

htonl(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=htonl(str)

debug("return : %x \n", test)

}

Result:

return: 78563412

FortiADC version: V4.8

Used in events: ALL

Ntohs(int)

Convert a 16 bit short integer from network byte order to host byte order

Syntax

ntohs(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=ntohs(str)

debug("return : %x \n", test)

}

Result:

Return: 7856

FortiADC version: V4.8

Used in events: ALL

htons(int)

Convert a 16 bit short integer from host byte order to network byte order

Syntax

htons(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=htons(str)

debug("return : %x \n", test)

}

Result

Return: 7856

FortiADC version: V4.8

Used in events: ALL

Ntohl(int)

When receive some long integrates in HTTP response from the network, convert a 32 bit long integer from network byte order to host byte order.

Syntax

ntohl(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=ntohl(str)

debug("return : %x \n", test)

log("record a log: %x \n", test)

}

Result:

return: 78563412

FortiADC version: V4.8

Used in events: ALL

to_HEX(str)

Returns the HEX calculate of the string.

Syntax

To_HEX(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “\0\123\3”

hex = to_HEX(str)

debug(“this str in hex is: %s\n”, hex)

}

FortiADC version: V4.8

Used in events: ALL

Debug(str)

Prints debug info when vs using scripting.

Syntax

debug(str);

Arguments

Name Description

str

A string which will be printed.

Examples

when HTTP_REQUEST {

debug(“http request method is %s.\n”, HTTP:method_get())

}

FortiADC version: V4.3

Used in events: ALL

Log(str)

Prints the scripting running info in log format. When using this command, you should enable scripting log.

Syntax

log(str);

Arguments

Name Description

str

A string which will be logged.

Examples

when HTTP_REQUEST {

log(“http request method is %s.\n”, HTTP:method_get())

}

FortiADC version: V4.8

Used in events: ALL

File_open(path, str)

Opens a file, returns a file object.

Syntax

File_open(path, str);

Arguments

Name Description

str

A string which specifies the method to open the file.

Path

A string which specifies the file path.

Examples

when HTTP_REQUEST {

filepath = "/etc/resolv.conf";

fp = file_open(filepath,"r");

if not fp then

debug("file open failed\n");

end

repeat

line = file_gets(fp, 256);

if line then

debug("line %s", line);

end

until not line

file_close(fp);

}

FortiADC version: V5.2

Used in events: ALL

File_get(file, size)

Returns the file content.

Syntax

File_get(file, size);

Arguments

Name Description

file

A file object that get from file_open()

Examples

FortiADC version: V5.2

Used in events: ALL

File_close(file)

Closes a file.

Syntax

File_close(file);

Arguments

Name Description

file

A file object which will be closed.

Examples

FortiADC version: V5.2

Used in events: ALL

Global commands

Crc32(str)

Returns the crc32 check value of the string, or 0 if it is an empty string.

Syntax

crc32(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = "any string for crc32 calculation"

crc = crc32(str);

debug("crc is %d\n", crc);

}

FortiADC version: V5.2

Used in events: ALL

Key_gen(str_pass, str_salt, iter_num, len_num)

Create an AES key to encrypt/decrypt data, either generated by password or user defined.

Syntax

key_gen(str_pass, str_salt, iter_num, len_num);

Arguments

Name Description

str_pass

The password string.

str_salt

The salt string.

iter_num

The number of iterations.

len_num

The key length.

Examples

when HTTP_REQUEST {

new_key = key_gen("pass", "salt", 32, 32); -- first parameter is the password string, second the salt string, third the number of iterations, fourth the key length

debug("new key in hex is %s\n", to_HEX(new_key));

}

FortiADC version: V5.2

Used in events: ALL

Aes_enc(t)

encrypt the data using the previously-created AES key

Syntax

Aes_enc(t);

Arguments

Name Description

t

A table which specifies the message, key, size that used to encrypt.

Examples

when HTTP_REQUEST {

t={};

t["message"] = "MICK-TEST";

t["key"] = "aaaaaaaaaabbbbbb" --16bit

t["size"]= 128 -- 128, 192, or 256, the corresponding key length is 16, 24, and 32

enc = aes_enc(t)

debug("The aes_enc output to HEX\n %s\n",to_HEX(enc));

}

Note:

  • Message: a string which will be encrypted
  • Key: a string to encrypt str
  • Size: must be 128, 192, or 256, the corresponding key length is 16, 24, and 32.

FortiADC version: V5.2

Used in events: ALL

aes_dec(t)

decrypt the data using the previously-created AES key

Syntax

Aes_dec(t);

Arguments

Name Description

t

A table which specifies the message, key, size that used to decrypt.

Examples

when HTTP_REQUEST {

t={};

t["message"] = "MICK-TEST";

t["key"] = "aaaaaaaaaabbbbbb"

t["size"]= 128 -- 128, 192, or 256, the corresponding key length is 16, 24, and 32

enc = aes_enc(t)

--aes decryption

a={};

a["message"] = enc;

a["key"] = "aaaaaaaaaabbbbbb"

a["size"]= 128;

dec = aes_dec(a);

debug("key length %s decrypted is %s\n",”128” ,dec);

}

Note:

  • Message: a string which will be decrypted
  • Key: a string to decrypt str
  • Size: must be 128, 192, or 256, the corresponding key length is 16, 24, and 32

FortiADC version: V5.2

Used in events: ALL

EVP_Digest(alg, str)

EVP_Digest for oneshot digest calculation.

Syntax

EVP_Digest(alg, str);

Arguments

Name Description

alg

A string which specifies the algorithm.

str

A string which will be EVP_Digested.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

re = EVP_Digest(alg, data);

debug("the digest in hex is %s\n", to_HEX(re));

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

HMAC(alg, str, key)

HMAC message authentication code.

Syntax

HMAC(alg, str, key);

Arguments

Name Description

alg

A string which specifies the algorithm.

str

A string which will be calculated.

key

A string which is a secret key.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = HMAC(alg, data, key);

debug("the HMAC in hex is %s\n", to_HEX(re));

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

HMAC_verify(alg, data, key, verify)

Check if the signature is same as the current digest.

Syntax

HMAC_verify(alg, data, key verify);

Arguments

Name Description

alg

A string which specifies the algorithm.

key

A string which is a secret key.

data

A string which will be calculated.

verify

A signature to compare the current digest against.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

data = "your data"

verify = "your result to compare"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = HMAC_verify(alg, data, key, verify);

if re then

debug("verified\n")

else

debug("not verified\n")

end

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

G2F(alg, key)

Returns a G2F random value.

Syntax

G2F(alg, key);

Arguments

Name Description

alg

A string which specifies the algorithm.

key

A string which is a secret key.

Examples

when HTTP_REQUEST {

alg = "MD5"; -- or "SHA1", "SHA256", "SHA384", "SHA512"

key = "123456789ABCDEF0123456789ABCDEF\121"; -- or you can generate a key using key_gen

re = G2F(alg, key);

debug("the G2F value is %d\n", re);

}

Note:

Alg: type of hashing algorithms to use, must be MD5, SHA1, SHA256, SHA384, SHA512

FortiADC version: V5.2

Used in events: ALL

Class_match(str, method, list)

Used to match the string against an element.

Syntax

Class_match(str, method, list);

Arguments

Name Description

str

A string which will be matched.

method

A string which specifies the match method

list

A list which specifies the match target.

Examples

when HTTP_REQUEST {

url_list = “”

url = HTTP:uri_get()

status, count, t = class_match(url, "starts_with", url_list); --or "ends_with", "equals", "contains"

debug("status %s, count %s\n", status, count);

for k,v in pairs(t) do

debug("index %s, value %s\n", k,v);

end

}

Note:

Method: must be “starts_with”, “equals”, “contains”, “end_with”

This command return three parameter, first “status”: true or false means if match or not; second “count”: return the number of times matches; third “t”: return matched index and matched value in the list.

FortiADC version: V5.2

Used in events: ALL

Class_search(list, method, str)

Used to search an element in the list against a string.

Syntax

Class_search(list, method, str);

Arguments

Name Description

str

A string which will be calculated.

list

A string which will be matched.

method

A string which specifies the match method.

Examples

when HTTP_REQUEST {

status, count, t = class_search(url_list, "starts_with", url); --or "ends_with", "equals", "contains"

for k,v in pairs(t) do

debug("index %s, value %s\n", k,v);

end

}

Note:

Method: , must be “starts_with”, “equals”, “contains”, “end_with”

FortiADC version: V5.2

Used in events: ALL

Cmp_addr()

Used to match one IP address against a group of IP addresses.

It can automatically detect IPv4 and IPv6 and can be used to compare IPv4 addresses with IPv6 addresses

Syntax

Cmp_addr(client_Ip, addr_group );

Arguments

Name Description

Client_ip

For an IPv4 ip_addr/[mask], the mask can be a number between 0 and 32 or a dotted format like 255.255.255.0

For an IPv6 ip_addr/[mask], the mask can be a number between 0 and 128.

Addr_group

A group of IP address.

addr_group = "192.168.1.0/24" --first network address

addr_group = addr_group..",::ffff:172.30.1.0/120" --second

network address

Examples

when RULE_INIT{

--initialize the address group here

--for IPv4 address, mask can be a number between 0 to 32 or a dotted format

--support both IPv4 and IPv6, for IPv6, the mask is a number between 0 and 128

addr_group = "192.168.1.0/24"

addr_group = addr_group..",172.30.1.0/255.255.0.0"

addr_group = addr_group..",::ffff:172.40.1.0/120"

}

when HTTP_REQUEST{

client_ip = HTTP:client_addr()

matched = cmp_addr(client_ip, addr_group)

if matched then

debug("client ip found in address group\n");

else

debug("client ip not in address group\n");

end

}

FortiADC version: V4.8

Used in events: ALL

url_enc(str)

converted the url info a valid ASCII format

Syntax

url_enc(str);

Arguments

Name Description

str

A string which will be converted.

Examples

when HTTP_REQUEST {

url_list =https://abc.www.123.bbEEE.com/?5331=212&qe1=222

debug("Ori= %s \nencodeed= %s\n", url_list,url_enc(url_list));

}

FortiADC version: V5.2

Used in events: ALL

url_dec(str)

converted the encoding-url into a original url.

Syntax

url_dec(str);

Arguments

Name Description

str

A string which will be converted.

Examples

when HTTP_REQUEST {

str = "http%3A%2F%2Fwww.example.com%3A890%2Furl%2Fpath%2Fdata%3Fname%3Dforest%23nose“

debug("String= %s\ndecoded= %s\n", str,url_dec(str));

}

FortiADC version: V5.2

Used in events: ALL

url_parser(str)

Parse a url, return a table contains host, port, path, query, fragment, the username, password etc from the url.

Syntax

url_parser(str);

Arguments

Name Description

str

A url which will be parser.

Examples

when HTTP_REQUEST {

url_list="http://foo:bar@w1.superman.com/very/long/path.html?p1=v1&p2=v2#more-details"

purl = url_parser(url_list);

debug("parsed url scheme %s host %s\n port %s path %s query %s\n fragment %s, the username %s\n passowrd %s\n", purl["scheme"], purl["host"], purl["port"],purl["path"], purl["query"], purl["fragment"], purl["username"], purl["password"]);

}

FortiADC version: V5.2

Used in events: ALL

url_compare(url1, url2)

Compare two url string, return true if they are the same.

Syntax

url_compare(url1, url2);

Arguments

Name Description

url1, url2

Two urls which will be compared.

Examples

when HTTP_REQUEST {

url_list={};

url_list[1]="http://10.10.10.10:80/"

url_list[2]="http://10.10.10.10/"

url_list[3]="https://5.5.5.5:443/"

url_list[4]="https://5.5.5.5/"

url_list[5]="http://[2001::1]:80"

url_list[6]="http://[2001::1]"

url_list[7]="https://[2001:99:1]:443"

url_list[8]="https://[2001:99:1]"

for i = 1,8,2 do

if url_compare(url_list[i],url_list[i+1]) then

debug("URL_List %d %d Match !\n",i,i+1);

else

debug("URL_List %d %d NOT Match !\n",i,i+1);

end

end

}

FortiADC version: V5.2

Used in events: ALL

Rand()

Generate a random number. Returns is a integer number. After FAD reboot, the random number will be different.

Syntax

rand();

Arguments: N/A

Examples

when HTTP_REQUEST {

a = rand()

debug(“a = %d\n”, a)

}

FortiADC version: V5.2

Used in events: ALL

srand(str)

Set the random seed.

Syntax

srand(str);

Arguments

Name Description

str

A string which specifies the seed.

Examples

when HTTP_REQUEST {

srand(1111)

a = rand()

debug(“a = %d\n”, a)

}

FortiADC version: V5.2

Used in events: ALL

Rand_hex(int)

Generate a random number in HEX. Returns is a string, length is the <int>.

Syntax

Rand_hex(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

b = rand_hex(15)

debug("-----rand_hex b = %s-----\n", b)

}

Result:

-----rand_hex b = 43474FB47A8A8C4-----

FortiADC version: V5.2

Used in events: ALL

Rand_alphanum(int)

Generate a random alphabet+number sequence. Returns is a string, length is the <int>.

Syntax

Random_alphanum(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

c = rand_alphanum(17)

debug("-----rand_alphanum c = %s-----\n", c)

}

Result:

-----rand_alphanum c = XTHQpb6ngabMqH7nx-----

FortiADC version: V5.2

Used in events: ALL

Rand_seq(int)

Generate a random in sequence. Returns is a string, length is the <int>.

Syntax

Rand_seq(int);

Arguments

Name Description

Int

An integer which specifies the length of the returned string.

Examples

when HTTP_REQUEST {

d = rand_seq(18)

debug("-----rand_seq d = %s-----\n", d)

}

Result:

FortiADC version: V5.2

Used in events: ALL

Time()

Returns the current time in a number which is the time since the Epoch measured in seconds.

Syntax

time();

Arguments: N/A

Examples

when HTTP_REQUEST {

t = time()

debug(“-----time: t %s-----\n”, t)

}

Result:

-----time: t 1561424783-----

FortiADC version: V4.8

Used in events: ALL

Ctime()

Returns a string descripting the current time like “Tue Jun 25 14:11:01 2019”.

Syntax

ctime();

Arguments: N/A

Examples

when HTTP_REQUEST {

ct = ctime()

debug(“-----ctime: ct %s-----\n”, ct)

}

Result:

-----ctime: ct Mon Jun 24 18:06:23 2019-----

FortiADC version: V4.8

Used in events: ALL

gmtime()

Returns the GMT time like “Thu 27 Jun 2019 18:27:42 GMT”.

Syntax

gmtime();

Arguments: N/A

Examples

when HTTP_REQUEST {

gt = gmtime()

debug(“-----gmtime: gt %s-----\n”, gt)

}

Result:

-----gmtime: gt Thu 27 Jun 2019 18:27:42 GMT -----

FortiADC version: V5.3

Used in events: ALL

Md5(str)

Rreturn the MD5 calculated for the string provided.

Syntax

Md5(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

md5 = md5(str1)

str = “test string”

a=12

md = md5(“%s,123%d”,str,a)

}

FortiADC version: V4.8

Used in events: ALL

Md5_hex(str)

Returns the md5 value in hex to the string

Syntax

Md5_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = md5_hex(str1)

}

FortiADC version: V4.8

Used in events: ALL

Md5_str(str)

Calculate the MD5 of a string input and stores the results in an intermediate variable, in some cases you need a version to deal with it.

Syntax

Md5_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

md5 = md5_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Md5_hex_str(str)

Calculate the MD5 of a string input of a string input and outputs the results in HEX format, in some cases you need a version to deal with it.

Syntax

Md5_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

md = md5_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha1(str)

Returns the sha1 calculated for the string provided.

Syntax

Sha1(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1(input)

}

FortiADC version: V4.8

Used in events: ALL

Sha1_hex(str)

Returns the sha1 calculated for the string in hex.

Syntax

Sha1_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “123456789”

sha1 = sha1_hex(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha1_str(str)

Calculate the SHA1 of a string input and stores the results in an intermediate variable, in some cases you need a version to deal with it.

Syntax

Sha1_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha1_hex_str(str)

Calculate the SHA1 of a string input and output the results in HEX format, in some cases you need a version to deal with it.

Syntax

Sha1_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha1_hex_str(input); -- input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha256(str)

Calculates the SHA256 of a string input and store the result in an intermediate variable.

Syntax

Sha256(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha256(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha256_hex(str)

Calculate the SHA256 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha256_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha256 = sha256_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha256_str(str)

Calculate the SHA256 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha256_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha256_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha256_hex_str(str)

Calculate the SHA256 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha256_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha256_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha384(str)

Calculates the SHA384 of a string input and store the result in an intermediate variable.

Syntax

Sha384(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha384(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha384_hex(str)

Calculate the SHA384 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha384_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha384 = sha384_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha384_str(str)

Calculate the SHA384 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha384_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha384_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha384_hex_str(str)

Calculate the SHA384 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha384_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha384_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha512(str)

Calculates the SHA512 of a string input and store the result in an intermediate variable.

Syntax

Sha512(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

str2 = sha512(str1)

}

FortiADC version: V4.8

Used in events: ALL

Sha512_hex(str)

Calculate the SHA512 of a string input and output the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha512_hex(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str1 = “abc”

sha512 = sha512_hex(str)

}

FortiADC version: V4.8

Used in events: ALL

Sha512_str(str)

Calculate the SHA512 of a string input and restore the result in an intermediate variable. In some cases you need a version to deal with it.

Syntax

Sha512_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha512_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

Sha512_hex_str(str)

Calculate the SHA512 of a string input and restore the result in an intermediate variable. In some case you need a version to deal with it.

Syntax

Sha512_hex_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = sha512_hex_str(input); --input can be a cert in DER format

}

FortiADC version: V4.8

Used in events: ALL

b32_enc(str)

encode a string input in base32 and output the result in string format.

Syntax

B32_enc(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “abc”

en = b32_enc(str)

}

FortiADC version: V5.2

Used in events: ALL

B32_enc_str(str)

Encode a string input in base32 and output the result in string format. In some cases you need a version to deal with it.

Syntax

B32_enc_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b32_enc_str(input); --input can be a cert in DER format

}

FortiADC version: V5.2

Used in events: ALL

B32_dec(str)

Decode a base32 encoded string input and output the result in string format

Syntax

B32_dec(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “abc”

dec = b32_dec(str)

}

FortiADC version: V5.2

Used in events: ALL

B32_dec_str(str)

Decode a base32 encoded string input and output the result in string format. In some cases you need a version to deal with it.

Syntax

B32_dec_str(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b32_dec_str(input); --input can be a cert in DER format

}

FortiADC version: V5.2

Used in events: ALL

B64_enc(str)

Returns base64 encryption of string

Syntax

B64_enc(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b64_enc(input);

--Input can be general format:

str=”test string”

a=12

en=b64_enc(“%s, 123 %d”, str, a);

}

FortiADC version: V4.8

Used in events: ALL

B64_dec(str)

Returns base64 decryption of string

Syntax

B64_dec(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

result = b64_dec(input);

str=”test string”

a=12

de=b64_dec(“%s, 123 %d”, str, a);

}

FortiADC version: V4.8

Used in events: ALL

Get_pid()

Returns the PID value of the VS process.

Syntax

Get_pid();

Arguments: N/A

Examples

when HTTP_REQUEST {

pid = get_pid();

debug(“VS PID is : %d\n”, pid)

}

FortiADC version: V5.2

Used in events: ALL

table_to_string(t)

Returns the table in a string.

Syntax

Table_to_string(t);

Arguments

Name Description

t

A table which specifies the info.

Examples

when HTTP_REQUEST {

t={};

t[1]=97;

t[2]=98;

t[3]=99;

t[4]=1;

str = table_to_string(t);

debug(“str is %s\n”, str)

}

Result:

str is abc

FortiADC version: V4.8

Used in events: ALL

Htonl(int)

Convert a 32 bit long integer from host byte order to network byte order

Syntax

htonl(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=htonl(str)

debug("return : %x \n", test)

}

Result:

return: 78563412

FortiADC version: V4.8

Used in events: ALL

Ntohs(int)

Convert a 16 bit short integer from network byte order to host byte order

Syntax

ntohs(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=ntohs(str)

debug("return : %x \n", test)

}

Result:

Return: 7856

FortiADC version: V4.8

Used in events: ALL

htons(int)

Convert a 16 bit short integer from host byte order to network byte order

Syntax

htons(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=htons(str)

debug("return : %x \n", test)

}

Result

Return: 7856

FortiADC version: V4.8

Used in events: ALL

Ntohl(int)

When receive some long integrates in HTTP response from the network, convert a 32 bit long integer from network byte order to host byte order.

Syntax

ntohl(int);

Arguments

Name Description

int

An integer which will be calculated.

Examples

when HTTP_REQUEST {

str="0x12345678"

test=ntohl(str)

debug("return : %x \n", test)

log("record a log: %x \n", test)

}

Result:

return: 78563412

FortiADC version: V4.8

Used in events: ALL

to_HEX(str)

Returns the HEX calculate of the string.

Syntax

To_HEX(str);

Arguments

Name Description

str

A string which will be calculated.

Examples

when HTTP_REQUEST {

str = “\0\123\3”

hex = to_HEX(str)

debug(“this str in hex is: %s\n”, hex)

}

FortiADC version: V4.8

Used in events: ALL

Debug(str)

Prints debug info when vs using scripting.

Syntax

debug(str);

Arguments

Name Description

str

A string which will be printed.

Examples

when HTTP_REQUEST {

debug(“http request method is %s.\n”, HTTP:method_get())

}

FortiADC version: V4.3

Used in events: ALL

Log(str)

Prints the scripting running info in log format. When using this command, you should enable scripting log.

Syntax

log(str);

Arguments

Name Description

str

A string which will be logged.

Examples

when HTTP_REQUEST {

log(“http request method is %s.\n”, HTTP:method_get())

}

FortiADC version: V4.8

Used in events: ALL

File_open(path, str)

Opens a file, returns a file object.

Syntax

File_open(path, str);

Arguments

Name Description

str

A string which specifies the method to open the file.

Path

A string which specifies the file path.

Examples

when HTTP_REQUEST {

filepath = "/etc/resolv.conf";

fp = file_open(filepath,"r");

if not fp then

debug("file open failed\n");

end

repeat

line = file_gets(fp, 256);

if line then

debug("line %s", line);

end

until not line

file_close(fp);

}

FortiADC version: V5.2

Used in events: ALL

File_get(file, size)

Returns the file content.

Syntax

File_get(file, size);

Arguments

Name Description

file

A file object that get from file_open()

Examples

FortiADC version: V5.2

Used in events: ALL

File_close(file)

Closes a file.

Syntax

File_close(file);

Arguments

Name Description

file

A file object which will be closed.

Examples

FortiADC version: V5.2

Used in events: ALL