1 2 Previous Next 20 Replies Latest reply on Oct 29, 2010 1:56 PM by 806253

    Get MAC address with an applet


      I'm trying to get MAC adress of the NIC through which user is connected to the internet.

      Example: I call an URL "www.someurl.com/page.html" and applet on this page should resolve MAC address of my network card.

      I'm already able to get MACs of all network interfaces:
      public void getMacs() {
              try {
                   * Get NetworkInterface for the current host and then read the
                   * hardware address.
                  Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                  if (nis != null) {
                      while (nis.hasMoreElements()) {
                          NetworkInterface currentNI = nis.nextElement();
                          Enumeration<InetAddress> addresses = currentNI.getInetAddresses();
                          while (addresses.hasMoreElements()) {
                              InetAddress inetAddress = addresses.nextElement();
                              byte[] mac = currentNI.getHardwareAddress();
                              if (mac != null) {
                                  if (mac.length > 0) {
                                      String hostName = inetAddress.getHostName();
                                      String hostAddr = inetAddress.getHostAddress();
                                      System.out.println("HOSTNAME = " + hostName + " HOSTADDR = " + hostAddr);
                                          "Name: " + currentNI.getName() + " | " +
                                          "DisplayName: " + currentNI.getDisplayName() + " MAC: ");
                                       * Extract each array of mac address and convert it to hexa with the
                                       * following format 08-00-27-DC-4A-9E.
                                      for (int i = 0; i < mac.length; i++) {
                                          System.out.format("%02X%s", mac, (i < mac.length - 1) ? "-" : "");
      } else {
      System.out.println("Address doesn't exist or is not accessible.");

      } else {
      System.out.println("Network Interface for the specified address is not found.");

      } catch (SocketException e) {
      But how I can find out whether *currentNI* is the network interface I'm looking for?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: Get MAC address with an applet
          • 2. Re: Get MAC address with an applet
            We are developing a website where media content can be downloaded by registered users. We don't want that some of them create several accounts to get ovet the limit.
            For example: allowed are only N videos a day, so we log user's name and his last known MAC addres and check them every time he / she clicks download link and compare date of last download attempt to actual date. Since PHP has no API to get client's MAC, we try to use Java.

            Any idea?
            • 3. Re: Get MAC address with an applet
              Then as I suspected you are wasting your time with this approach. The MAC address can be changed by the user, so the scheme cannot achieve its objective.
              • 4. Re: Get MAC address with an applet
                You are right. This approach is definitely not the best one, and, as you say, MAC can easily be changed. But our user are not so very skilled in such things, and even if there will be a couple of them, who'll get through this protection, that would be not critical.

                In older version of the website users had to download a software that was sending a kind of hardware code to the server so download coud be started. That is what we want to avoid with applet.

                Do you have a better idea?
                • 5. Re: Get MAC address with an applet
                  fnick wrote:
                  We are developing a website where media content can be downloaded by registered users. We don't want that some of them create several accounts to get ovet the limit.
                  For example: allowed are only N videos a day, so we log user's name and his last known MAC address
                  Why, you already have the user's name, what is a MAC address going to add?
                  • 6. Re: Get MAC address with an applet
                    Users can still create multiple accounts. If we get user's MAC, we can block him from doing that. He still can use another computer, but most of users won't do that.
                    • 7. Re: Get MAC address with an applet
                      Save something into the system preferences using the Preferences class? Something like time of last download.
                      I know they can tamper with, but if the idea appeals to you there may be ways to make it better (like signing it).

                      Edit: Reading your thread more carefully now. Your server seem to have the logic for time delays etc.
                      All you need to store is a unique number (UUID if you wish) that becomes the machine identifier regardless of MAC addresses and such.
                      If this number is generated and digitally signed on the server, pass it along with the signature to the applet to store it in Preferences and you are decently safe, I think.

                      Edited by: baftos on Jul 25, 2010 1:38 PM
                      • 8. Re: Get MAC address with an applet
                        Hi everybody,
                        Since last Java update 1.6 0_22-b04, I am not able to retrieve the mac address anymore.
                        I can retrieve it through an desktop application, but through an Applet it returns 0 and/or null.
                        I am using "*getHardwareAddress()*" method.

                        Does anybody know if this feature was disabled for Applets in this new Java update?

                        I have already looked on change logs, but I didn't see anything.
                        I appreciate for any help!!
                        • 9. Re: Get MAC address with an applet
                          Same problem with signed JAR on WIN32

                          Thx for support
                          • 10. Re: Get MAC address with an applet
                            Found it !

                            I was calling NetworkInterface.getHardwareAddress() from an applet method called from javascript

                            Such a call has now to be made from Applet.init()
                            (as suggested in 6u22 release notes ... sorry for bug reported)
                            • 11. Re: Get MAC address with an applet
                              It doesn't work on init. Neither asking for privileges nor signing the applet.
                              public void init() {
                                   AccessController.doPrivileged(new PrivilegedAction() {
                                        public Object run() {
                                             // privileged code goes here:
                                             try {
                                                  Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                                                  firstMac = getMacFromInterface(nis.nextElement());
                                             catch (SocketException ex) {
                                                  firstMac = "undefined";
                                             return null; // nothing to return
                              Edited by: user2647245 on 18/10/2010 10:07
                              • 12. Re: Get MAC address with an applet
                                Such a call has now to be made from Applet.init()
                                (as suggested in 6u22 release notes ... )
                                Where does it suggest that? Can't see it myself, or a reason why it would work either.
                                • 13. Re: Get MAC address with an applet
                                  Has anyone found a solution yet?

                                  I can't find anything about calling from Applet.init().
                                  I´m going to try with c# & ActiveX, but I´d rather have the applet.
                                  • 14. Re: Get MAC address with an applet
                                    Refer to CVE-2010-3560 section of 6u22 release notes

                                    I am using a signed JAR to handle licences granted on a full web professional solution
                                    A licence key is encoded in an applet method called from Javascript

                                    Before 6u22, a call to NetworkInterface.getHardwareAddress() on each element returned in NetworkInterface.getNetworkInterfaces() was made in this method
                                    When this new version was released, NetworkInterface.getHardwareAddress() returned null for each interface (some of them had a valid return MAC address before 6u22)

                                    CVE-2010-3560 release notes section refers to such a case, without giving any clue about any solution
                                    But, I had a similar problem with a call to System.getenv() in the same applet that finally had to be placed in applet init() method in order to work properly
                                    So I finally had the idea of placing the call to NetworkInterface.getHardwareAddress() in the init() method instead of within the applet method called from Javascript

                                    I'm sorry if it does not work for you, but I can assure you it is working for me, without any asking for privileges

                                    Code snippet :
                                    public void init() {
                                    try {
                                         Enumeration<NetworkInterface> eth = NetworkInterface.getNetworkInterfaces();
                                         while (eth.hasMoreElements()) {
                                              NetworkInterface eth0 = eth.nextElement();
                                              byte[] mac = eth0.getHardwareAddress();
                                    catch (Exception e) {
                                    ... }
                                    Edited by: user8883322 on 20 oct. 2010 15:29

                                    Edited by: user8883322 on 20 oct. 2010 15:35
                                    1 2 Previous Next