How to spoof NetworkInterface.getHardwareAddress() (Java)

5/24/2019

I have a software running in OpenShift (Kubernetes) where the licence is based on the MAC address. When restarting the app, the MAC address of the container changes and I have to apply for a new licence file.

Since there are no static MAC-Adressess in k8s pods, I want to spoof the Java call to NetworkInterface.getHardwareAddress() to trick the software into thinking the MAC Address is still the same.

Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
    NetworkInterface networkInterface = (NetworkInterface) enumeration.nextElement();
    if (networkInterface.isLoopback() || networkInterface.isPointToPoint() || networkInterface.isVirtual()) {
        continue;
    }
    if (networkInterface.isUp()) {
        byte[] arrayOfByte = networkInterface.getHardwareAddress();
        if (arrayOfByte != null && arrayOfByte.length == 6) {
            StringBuilder stringBuilder = new StringBuilder();
            for (byte b = 0; b < arrayOfByte.length; b++) {
                if (b != 0) {
                    stringBuilder.append(":");
                }
                stringBuilder.append(String.format("%02x", arrayOfByte[b]));
            }
            System.out.println(networkInterface.getName() + ": " + stringBuilder);
        }
    }
}

actual: eth0: 01:14:4d:ec:01:42

expected: eth0: ee:ee:ee:ee:ee:ee

-- leNerd42
java
kubernetes
mac-address
openshift

1 Answer

5/24/2019

Unfortunately, you cannot do this in Java.

The method NetworkInterface::getNetworkInterfaces() is implemented as native, which means it does not access any specific field to obtain its results. If it did, you might have luck hacking stuff with reflection, but as it stands, you have to manage it in your OS configuration instead.

EDIT: As for Kubernetes solutions, you might want to look here

-- Piotr Wilkin
Source: StackOverflow