rapid7/metasploit-javapayload — pull request #14 by OJ (2014-03-20 05:19:56+00:00)
Title: Add getenv support to the Java payload

This will bring this meterpreter in line with the other meterpreter payloads so that the getenv feature works consistently across them.

Here's a test run:

```
meterpreter > sysinfo
Computer : ubuntu
OS : Linux 3.11.0-18-generic (amd64)
Meterpreter : java/java
meterpreter > getenv $HOME %PATH% DISPLAY

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

Variable Value
-------- -----
HOME /home/oj
PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
DISPLAY :0
```

That experience was horrible. Yay for having to resort to substring hacks to get it to work.

## IMPORTANT

To get this working, I had to implement support for multiple Values of the same type. That is, `getenv` lets you pass in more than one value, and hence the same TLV type appears more than once in the payload. Java Meterpreter didn't like that at all. As a result I had to implement it. This is what I did:

* Added a container `ArrayList` which contained ALL the values that are pulled out of the packet.
* Changed the `valueMap` so that it contained `ArrayLists`s of _indices_ that represent the location of the values in the new list of packet values. This is stored for each type.
* To get access to a valid the index array for that type needs to be read and then the indices are used to look up the value list to get the value itself.
* The existing functionality was changed so that when someone asks for a value, they the first value in the array. This covers all the existing cases nicely.
* A new function called `getValues` was added which returns a list based on the type. This allows the caller to iterate through it themselves.

I hope that makes sense. I wanted to retain the existing functionality as much as possible.

Cheers!