rapid7/metasploit-framework — pull request #2982 by OJ (2014-02-12 04:03:27+00:00)
Title: Fix issue with getenv failing

The call to `getenv` failed when `%` or `$` were used because of the differences between Meterpreter handling and MSF handling.

Meterpreter effectively ignores (ie. strips out) the platform-specific characters which are used for environment variables. In the `getenv` call, MSF was invoking `getenvs` and getting a full hash of values, then attempting to index into the hash using a string which may be "polluted" with those platform-specific characters. This meant that there was a discrepency between what was returned and what was used to index and as a result, the value would come out as `nil`.

For example, calling `getenv('%FOO%')` would result in a hash with `{'FOO'=>'bar'}`, so looking for `%FOO%` in this result would yield nothing.

This commit changes this so that the name is ignored and the first value is returned.

Functionality in Meterpreter should remain as-is so that it maximises the possibility of returning meaningful data to the user. It is up to the caller to make sure that they handle the result set correctly.

Here's a sample run:

```
meterpreter > getenv username

Environment Variables
=====================

Variable Value
-------- -----
username OJ

meterpreter > getenv %USERNAME%

Environment Variables
=====================

Variable Value
-------- -----
USERNAME OJ

meterpreter > getenv DOESNOTEXIST
[-] None of the specified environment variables were found/set.
meterpreter > irb
[*] Starting IRB shell
[*] The 'client' variable holds the meterpreter client

>> client.sys.config.getenv('username')
=> "OJ"
>> client.sys.config.getenv('%username%')
=> "OJ"
>> client.sys.config.getenv('%DOESNOTEXIST%')
=> nil
```