Use function set_kb_item to create arrays but values are not recorded in order

Hi,
I am writing a NASL script and I need to set KB items that can hold several values.

Everything works fine except that the values are not recorded in the order they are written, but they assume an apparently random order.

For example:
I set the KB item named “banner” first with the value “Nr. 22” then with “Original” and then “New”.
When I print the array, the output is “[0] New” , “[1] Nr.22” and “[2] Original”.

set_kb_item(name: “banner”, value: “Nr. 22”);
set_kb_item(name:" banner", value: “Original” );
set_kb_item(name: “banner”, value: “New”);

banner_list = make list ( get_kb_list ( banner ) );

display = (banner_list [0]);
display = (banner_list [1]);
display = (banner_list [2]);

This happens both when the KB item is set several times in the same script and when the item is set in different scripts over the same scan.

Can someone help me to understand why this happens? And maybe also how to keep the order?

Thanks!

Hi Niko,

Welcome to the forum!

Depending on what you would like to achieve a call to sort() on the KB list you read out might help you get always the same order (mind you that this is not the order you have written it to the KB).

If the order of the written KB entries is important to you too you might have to work with different KB names (e.g. “banner/1”, “banner/2”, etc), as a list might or might not keep it’s order. You can then get the KB list with something like get_kb_list("banner/*") and iterate through it again.

Hope this gives you some idea. I’ve never really come to a situation where the write order of a KB list was crucial but rather just getting a list and iterate through it to find/process what is needed. But of course your needs might differ.

2 Likes