Getting MAC address on a web page using a Java applet
Asked Answered
W

5

2

I want to create an application where a web server can get the MAC Address of the clients logging in. The only possible way I could think of was to create a JAVA applet which contains java.net methods to find the mac address

I am using javascript to call the applet methods, but the browser is not allowing those methods to execute. Below is the applet I have created.

import java.applet.*;
import java.awt.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class AppletRunner extends Applet{
 // The method that will be automatically called  when the applet is started
    public void init()
    {
// It is required but does not need anything.
    }


//This method gets called when the applet is terminated
//That's when the user goes to another page or exits the browser.
    public void stop()
    {
    // no actions needed here now.
    }


//The standard method that you have to use to paint things on screen
//This overrides the empty Applet method, you can't called it "display" for example.

    public void paint(Graphics g)
    {
//method to draw text on screen
// String first, then x and y coordinate.
     g.drawString(getMacAddr(),20,20);
     g.drawString("Hello World",20,40);

    } 
  public String getMacAddr() {
   String macAddr= ""; 
    InetAddress addr;
 try {
  addr = InetAddress.getLocalHost();

        System.out.println(addr.getHostAddress());
        NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
        byte[] dirMac = dir.getHardwareAddress();

        int count=0;
        for (int b:dirMac){
         if (b<0) b=256+b;
         if (b==0) {
               macAddr=macAddr.concat("00"); 
         }
         if (b>0){

          int a=b/16;
          if (a==10) macAddr=macAddr.concat("A");
          else if (a==11) macAddr=macAddr.concat("B");
          else if (a==12) macAddr=macAddr.concat("C");
          else if (a==13) macAddr=macAddr.concat("D");
          else if (a==14) macAddr=macAddr.concat("E");
          else if (a==15) macAddr=macAddr.concat("F");
          else macAddr=macAddr.concat(String.valueOf(a));
             a = (b%16);
          if (a==10) macAddr=macAddr.concat("A");
          else if (a==11) macAddr=macAddr.concat("B");
          else if (a==12) macAddr=macAddr.concat("C");
          else if (a==13) macAddr=macAddr.concat("D");
          else if (a==14) macAddr=macAddr.concat("E");
          else if (a==15) macAddr=macAddr.concat("F");
          else macAddr=macAddr.concat(String.valueOf(a));
         }
         if (count<dirMac.length-1)macAddr=macAddr.concat("-");
         count++;
        }

 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  macAddr=e.getMessage();
 } catch (SocketException e) {
  // TODO Auto-generated catch block
  macAddr = e.getMessage();
 }
 return macAddr;
 }

  }
Wernsman answered 17/12, 2010 at 5:18 Comment(2)
Why do you want that? I can only think of privacy invasion.Overstuffed
Are you getting an error or how do you know the browser is not allowing it?Threewheeler
K
5

Applets cannot normally access these functions for security reasons. To avoid these restrictions, you need a signed applet, along with a policy file.

You can then write a policy file which grants your applet access to the functionality it needs. If the user then grants your applet the necessary permissions (it will prompt for them), your applet can use the functions.

Knighterrantry answered 19/6, 2012 at 13:17 Comment(3)
I've signed my applet, what next? how can I ask permission to grant to the user? Please help meBohol
@JayPatel: Hi, please don't ask questions in comments. Ask a new question where you describe your problem.Knighterrantry
please check my question #53081049Bohol
F
2

In Netbeans, you can sign an application enabling the WebStart:

  1. Access to Your project > properties > Application > WebStart
  2. Check "Enable Web Start". This show a sectin titled signing.
  3. Click the "Customize" button located in the signing section.
  4. Select "self-sign by generated key".
Fujio answered 9/1, 2013 at 1:3 Comment(0)
M
1

I don't think this will be possible. Web servers communicate with clients several layers above the link layer where MAC addresses live -- it's abstracted away by TCP/IP and there's no reason for the client to send it unless you specifically have client code to do that.

The reason your Java code isn't working is because the Java sandbox's security manager disallows such low-level calls -- which it should! If you ever do find a way to get that thing to work (which I doubt you will) you should promptly report it to Oracle because it shouldn't be happening at all.

I can't see much of a reason why you'd want it either, to be honest.

Morette answered 17/12, 2010 at 5:22 Comment(0)
L
1

The Java applet is prevented to access those methods on the client because it runs in a protected sandbox.

Lovettalovich answered 17/12, 2010 at 5:23 Comment(0)
F
0

It might not be possible within a browser, since it is against the sandboxing paradigm. You might have some luck with browser-specific native code extensions.

However, the important exception is if your web server is in the same local area network (same switch) as the client - then, the MAC address of the client is known to the server because it is still present in the IP packet.

Flem answered 17/12, 2010 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.