How to spoof NetworkInterface.getHardwareAddress() (Java) - java

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

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

Related

How to resolve UnsupportedOperationException?

I'm trying to develop a simple SNMP GET/SET program in java using SNMP4j. I've followed the following tutorials
http://www.developer-tricks.com/2012/11/how-to-get-started-with-snmp4j.html
https://blog.jayway.com/2010/05/21/introduction-to-snmp4j/
I have also read through the 'Getting started with SNMP4J' stackoverflow thread.
Every tutorial and program I've tried to replicate so far to get me started has resulted in "Error:java: java.lang.UnsupportedOperationException" when I compile. I can't figure out why. I used the exact code in both the tutorials I listed above, and both resulted in the same error as soon as I compile. I've read up on other threads involving the exception, but haven't found anything relevant to SNMP4j, a lot of what I read involved something with lists using the AsList method, which isn't used at all.
The code im trying to run is directly copied from the 'developer-tricks' link I posted earlier. The only difference is I changed the OID and IP address to ones for my own machine.
If anyone else has some experience in how to solve this exception, I would realy appreciate any advice.
Here is the console output when I try to compile.
Information:javac 10 was used to compile java sources
Information:3/29/2018 4:19 PM - Compilation completed with 1 error and
0 warnings in 716ms Error:java:
java.lang.UnsupportedOperationException
Here is my code, nearly identical to the 'how-to-get-started-with-snmp4j' tutorial i linked to.
public static void main(String[] args) throws IOException {
try {
Snmp snmp4j = new Snmp(new DefaultUdpTransportMapping());
snmp4j.listen();
Address add = new UdpAddress("192.168.1.10" + "/" + "161");
CommunityTarget target = new CommunityTarget();
target.setAddress(add);
target.setTimeout(500);
target.setRetries(3);
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
PDU request = new PDU();
request.setType(PDU.GET);
OID oid = new OID(".1.3.6.1.4.1.34832.512.1.1.1.2");
request.add(new VariableBinding(oid));
PDU responsePDU = null;
ResponseEvent responseEvent;
responseEvent = snmp4j.send(request, target);
if (responseEvent != null) {
responsePDU = responseEvent.getResponse();
if (responsePDU != null) {
Vector tmpv = responsePDU.getVariableBindings();
if (tmpv != null) {
for (int k = 0; k < tmpv.size(); k++) {
VariableBinding vb = (VariableBinding) tmpv.get(k);
String output = null;
if (vb.isException()) {
String errorstring = vb.getVariable().getSyntaxString();
System.out.println("Error:" + errorstring);
} else {
String sOid = vb.getOid().toString();
Variable var = vb.getVariable();
OctetString oct = new OctetString((OctetString) var);
String sVar = oct.toString();
System.out.println("success:" + sVar);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
Turns out the error had nothing to do with SNMP4j. It happened with any program I compiled.
In order to fix this, I uninstalled JDK 10 and installed JDK 9 instead. I was using Intellij. Not sure exactly what caused this, but uninstalling and reinstalling was the solution.

How to find MAC address of another device in Java or Android

How can I find MAC address of another device (Cellphones, Printers, Computers) on LAN using Java or Android? I had read all the answer related to this there but most of them are finding MAC address of their own Device which is possible.
What I need is I put IP address of another device on my LAN and it will return the MAC address. The code which I am currently trying is mentioned below.
public static String findMACForIP(String IP) throws SocketException, UnknownHostException {
NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(IP));
if (network != null) {
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int x = 0; x < mac.length; x++) {
sb.append(String.format("%02X%s", mac[x], (x < mac.length - 1) ? "-" : ""));
}
Log.d(Config.TAG, "MAC:" + sb.toString());
return sb.toString();
}
return null;
}
The above code clear means that it is trying to find the interface to which the given IP is assigned, so in case of someone other computer/device it will be not able to find that interface as that exists on that device, not our so it will return null as a result I am receiving null.
But when I input my own device IP(android device IP) it is returning its MAC address since the above can find an interface with that IP.
What I want is someone another computer MAC. it is possible because there are many apps on google play store which actually get the MAC address of all the device on LAN.

Getting mac address without internet connection in java

I have a chunk of code that returns the mac address of a PC very correctly, but that's only when there is an internet access, but I need it offline in a project I'm carrying out. If not possible, is there any other possible way of uniquely identifying a PC?
You certainly use an indirection based on the IP address, in your code snippet. This may explain why you do not get anything when Internet network access is down.
Here is a code snippet that does not depend on the network connection status.
It displays each MAC address of your PC. Note that a PC often has multiple MAC addresses. Each address will be displayed by this code snippet.
package com.stackoverflow;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetHWAddresses {
public static void main(String[] args) throws SocketException {
final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
final byte [] mac = e.nextElement().getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++)
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
System.out.println(sb.toString());
}
}
}
}

why elasticsearch MacAddressProvider use munged bytes

Recently I learn how to generate UUID. I find the class TimeBasedUUIDGenerator.java in elasticsearch.
I find it use mac adress to identify current machine(MacAddressProvide.java). But it XOR the mac adress with random bytes (as the code snippet below). As I know this will make the mac adress to random and increase the probability of conflict. Why we don't use mac adress directly?
public static byte[] getSecureMungedAddress() {
byte[] address = null;
try {
address = getMacAddress();
} catch (SocketException e) {
// address will be set below
}
if (!isValidAddress(address)) {
address = constructDummyMulticastAddress();
}
byte[] mungedBytes = new byte[6];
SecureRandomHolder.INSTANCE.nextBytes(mungedBytes);
for (int i = 0; i < 6; ++i) {
mungedBytes[i] ^= address[i];
}
return mungedBytes;
}
The code author is not sure about this either.
He said the reason may be not reveal the server's real mac address for security concern.
I think this also enable multi UUID generators deploy in the same server.

find the network connection type(wired or wireless) by its IP address in java

I want to list all the devices connected to my network, I done like this
InetAddress i = InetAddress.getLocalHost();
byte[] ip1 = i.getAddress();
for (int b = 0; b <255;b++) {
ip1[3] = (byte)b;
InetAddress address = InetAddress.getByAddress(ip1);
if (address.isReachable(3000)) {
System.out.println("\tIP :"+address.getHostAddress());
} else if (!address.getHostAddress().equals(address.getHostName())) {
System.out.println("\tIP :"+address.getHostAddress());
} else {
}
}
It prints all the connected devices but how to I identify which are wired connection and which are wireless, among them
In pure Java, you are limited to what NetworkInterface provides.
Additional information may be found e.g. in this Stackoverflow question.

Categories

Resources