Hands-On Web Penetration Testing with Metasploit
上QQ阅读APP看书,第一时间看更新

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:

  • Stores data in key/value pairs
  • Enables the MSF console to configure settings at the time of module execution
  • Enables MSF to pass the values to other modules internally

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

  • Module datastore: This datastore only saves information and options related to the loaded module (local declaration). In the MSF console, you can use the set command to save the module options and the get command to fetch the values that have been saved:

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.

  • Global Datastore: This datastore saves information and options to all the modules (global declaration). In the MSF console, you can use the setg command to save the module options and the getg command to fetch:

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.