Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak java meterpreter to resolve multiple ips per host #683

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public interface TLVType {
public static final int TLV_TYPE_CONNECT_RETRIES = TLVPacket.TLV_META_TYPE_UINT | 1504;

public static final int TLV_TYPE_SHUTDOWN_HOW = TLVPacket.TLV_META_TYPE_UINT | 1530;
public static final int TLV_TYPE_RESOLVE_HOST_ENTRY = TLVPacket.TLV_META_TYPE_GROUP | 1550;

// Registry
public static final int TLV_TYPE_HKEY = TLVPacket.TLV_META_TYPE_QWORD | 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import java.net.InetAddress;
import java.net.UnknownHostException;

import java.util.List;
import java.util.ArrayList;

public class stdapi_net_resolve_host implements Command {

private static final int AF_INET = 2;
private static final int AF_INET6 = 23;

public static InetAddress resolve_host(String host, int family) {
public static List<InetAddress> resolve_host(String host, int family) {
InetAddress[] inetAddresses;
List<InetAddress> addressList = new ArrayList<InetAddress>();
try {
inetAddresses = InetAddress.getAllByName(host);
} catch (UnknownHostException e) {
Expand All @@ -25,27 +29,37 @@ public static InetAddress resolve_host(String host, int family) {
for (InetAddress address : inetAddresses) {
if (family == AF_INET6) {
if (address instanceof Inet6Address) {
return address;
addressList.add(address);
}
} else if (family == AF_INET) {
if (address instanceof Inet4Address) {
return address;
addressList.add(address);
}
} else {
return address;
addressList.add(address);
}
}
return null;
if (addressList.isEmpty()) {
return null;
} else {
return addressList;
}
}

public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket response) throws Exception {
String host = request.getStringValue(TLVType.TLV_TYPE_HOST_NAME);
int family = request.getIntValue(TLVType.TLV_TYPE_ADDR_TYPE);
InetAddress inetAddress = resolve_host(host, family);
if (inetAddress != null) {
response.addOverflow(TLVType.TLV_TYPE_IP, inetAddress.getAddress());
response.addOverflow(TLVType.TLV_TYPE_ADDR_TYPE, family);
int return_val = ERROR_FAILURE;
List<InetAddress> inetAddresses = resolve_host(host, family);
if (inetAddresses != null) {
TLVPacket addrTLV = new TLVPacket();
return_val = ERROR_SUCCESS;
for(int i = 0; i < inetAddresses.size(); i++){
addrTLV.addOverflow(TLVType.TLV_TYPE_IP, inetAddresses.get(i).getAddress());
addrTLV.addOverflow(TLVType.TLV_TYPE_ADDR_TYPE, family);
}
response.addOverflow(TLVType.TLV_TYPE_RESOLVE_HOST_ENTRY, addrTLV);
}
return ERROR_SUCCESS;
return return_val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket respons
int family = request.getIntValue(TLVType.TLV_TYPE_ADDR_TYPE);
for (int i=0;i<hosts.size();i++) {
String host = hosts.get(i);
InetAddress inetAddress = stdapi_net_resolve_host.resolve_host(host, family);
if (inetAddress != null) {
response.addOverflow(TLVType.TLV_TYPE_IP, inetAddress.getAddress());
response.addOverflow(TLVType.TLV_TYPE_ADDR_TYPE, family);
List<InetAddress> inetAddresses = stdapi_net_resolve_host.resolve_host(host, family);
TLVPacket addrTLV = new TLVPacket();
if (inetAddresses != null) {
for(int j = 0; j < inetAddresses.size(); j++){
addrTLV.addOverflow(TLVType.TLV_TYPE_IP, inetAddresses.get(j).getAddress());
addrTLV.addOverflow(TLVType.TLV_TYPE_ADDR_TYPE, family);
}
response.addOverflow(TLVType.TLV_TYPE_RESOLVE_HOST_ENTRY, addrTLV);
} else {
response.addOverflow(TLVType.TLV_TYPE_IP, new byte[0]);
addrTLV.addOverflow(TLVType.TLV_TYPE_IP, new byte[0]);
response.addOverflow(TLVType.TLV_TYPE_RESOLVE_HOST_ENTRY, addrTLV);
response.addOverflow(TLVType.TLV_TYPE_ADDR_TYPE, family);
}
}
Expand Down