rapid7/metasploit-framework — pull request #2689 by OJ (2013-11-26 00:16:46+00:00)
Title: Add the getenv command which pulls env vars from the victim

This command will allow the attacker to grab environment variables from the target, if they exist. Calling this function allows for one or more values to be passed in, which should match the name of the variable required. If the variable is found, it is returned. If it is not found, the variable is not returned (ie. it's not present in the resulting hash).

Notes:

1. POSIX environment vars are case-senstive, whereas Windows is not.
1. POSIX doesn't seem to cough up user environment vars, it only returns system vars. I'm not sure why this is, but it could be because of the way we do linking on POSIX.

Associated Meterpreter [PR is over here](https://github.com/rapid7/meterpreter/pull/58).

### Verification

- [ ] Windows X64 works
- [ ] Windows X86 works
- [ ] POSIX works

### Sample Run (Windows x64)

```
meterpreter > sysinfo
Computer : WIN-S45GUQ5KGVK
OS : Windows 7 (Build 7601, Service Pack 1).
Architecture : x64
System Language : en_US
Meterpreter : x64/win64
meterpreter > getenv DOESNOTEXIST
[-] None of the specified environment variables were found/set.
meterpreter > getenv temp $prompt $USERNAME %WINDIR% COMSPEC %windows_tracing_flags%

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

Variable Value
-------- -----
prompt $P$G
windows_tracing_flags 3
temp C:\Users\OJ\AppData\Local\Temp
WINDIR C:\Windows
COMSPEC C:\Windows\system32\cmd.exe
USERNAME OJ
```