Variable manipulation in MSF

Variable manipulation in Metasploit Framework can help users utilize the features of the modules to their full extent. As pen testers, sometimes, we need to scan a lot of targets and in almost all our testing scenarios, we have to set the options required by the Metasploit module. These options, such as the remote host IP/port, and the local host IP/port are set for the specific Metasploit module in use. The sooner we learn about variable manipulation, the more efficiently we'll be able to use the module.

Variable manipulation can be achieved using datastores. A datastore is a type of variable that has the following functionalities:

Datastores are used by various classes to hold option values and other state information. There are two types of datastores:

As shown in the preceding screenshot, the smb_version module was loaded and the RHOSTS option was set to 192.168.2.17. But once we unloaded the module (using the back command), there was no value to set the RHOSTS option globally. To set these options globally, we need to use the global datastore.

In the preceding screenshot, we saved the value 192.168.2.17 in the RHOSTS option globally, which means the RHOSTS option will be set in case we use another module. If setg is used, we can always retrieve the data by using get or getg.

Executing just the set command in the module will show all the available options (for both module datastore and the global datastore) that have been saved:

In the case of removing the values from the datastores, you can always use the unset and unsetg commands.


Note: If an option is set globally using setg, you cannot remove it using the unset command. Instead, you need to use unsetg.