Asked  6 Months ago    Answers:  5   Viewed   74 times

I know that we can get the MAC address of a user via IE (ActiveX objects).

Is there a way to obtain a user's MAC address using JavaScript?

 Answers

37

I concur with all the previous answers that it would be a privacy/security vulnerability if you would be able to do this directly from Javascript. There are two things I can think of:

  • Using Java (with a signed applet)
  • Using signed Javascript, which in FF (and Mozilla in general) gets higher privileges than normal JS (but it is fairly complicated to set up)
Tuesday, June 1, 2021
 
LOKESH
answered 6 Months ago
48

Here is the code for getting IP and MAC addresses

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>

int main(void)
{
  char buf[8192] = {0};
  struct ifconf ifc = {0};
  struct ifreq *ifr = NULL;
  int sck = 0;
  int nInterfaces = 0;
  int i = 0;
  char ip[INET6_ADDRSTRLEN] = {0};
  char macp[19];
  struct ifreq *item;
  struct sockaddr *addr;

  /* Get a socket handle. */
  sck = socket(PF_INET, SOCK_DGRAM, 0);
  if(sck < 0) 
  {
    perror("socket");
    return 1;
  }

  /* Query available interfaces. */
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if(ioctl(sck, SIOCGIFCONF, &ifc) < 0) 
  {
    perror("ioctl(SIOCGIFCONF)");
    return 1;
  }

  /* Iterate through the list of interfaces. */
  ifr = ifc.ifc_req;
  nInterfaces = ifc.ifc_len / sizeof(struct ifreq);

  for(i = 0; i < nInterfaces; i++) 
  {
    item = &ifr[i];

    addr = &(item->ifr_addr);

    /* Get the IP address*/
    if(ioctl(sck, SIOCGIFADDR, item) < 0) 
    {
      perror("ioctl(OSIOCGIFADDR)");
    }

    if (inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof ip) == NULL) //vracia adresu interf
        {
           perror("inet_ntop");
           continue;
        }

    /* Get the MAC address */
    if(ioctl(sck, SIOCGIFHWADDR, item) < 0) {
      perror("ioctl(SIOCGIFHWADDR)");
      return 1;
    }

    /* display result */

    sprintf(macp, " %02x:%02x:%02x:%02x:%02x:%02x", 
    (unsigned char)item->ifr_hwaddr.sa_data[0],
    (unsigned char)item->ifr_hwaddr.sa_data[1],
    (unsigned char)item->ifr_hwaddr.sa_data[2],
    (unsigned char)item->ifr_hwaddr.sa_data[3],
    (unsigned char)item->ifr_hwaddr.sa_data[4],
    (unsigned char)item->ifr_hwaddr.sa_data[5]);

    printf("%s %s ", ip, macp);

  }

  return 0;
}
Tuesday, July 20, 2021
 
Easen
answered 5 Months ago
81
var de = false; // true when debugging
function bug( msg ){ ... display msg ... }

Usage:

de&&bug( "hello world")

When "de" is false (production mode), the overhead is minimal.

Wednesday, August 11, 2021
 
Lime
answered 4 Months ago
85

OK, I've tested this locally on my own Mac (version 10.6.8) and Windows XP so the good news is that it works (and it is surprisingly easy).

The essential idea is:

  1. Know how to open the on-screen keyboard from command line
  2. Set up your browser to allow the command line to be executed from JavaScript
  3. Write your HTML :-)

Instructions for both platforms are as follows.

Mac

For Mac, download and build this Xcode project:

  • https://github.com/nriley/keyboardViewer

Make sure your build target is the same as the client Mac (e.g. 64 bit Intel, etc). The output will be an executable file called keyboardViewer. This will pop open the on-screen keyboard when executed.

Let's assume you've saved keyboadViewer onto the user's Desktop then the command you will want to execute is (as in my case):

  • /Users/Oliver/Desktop/keyboardViewer.

Windows

On Windows, it is much easier to open the on-screen keyboard from the command line. The following (or similar) will do it:

  • C:WINDOWSsystem32osk.exe

Firefox

Next, you are going to have to execute this file (or Windows command) from a browser. So, install the Firefox add-on here:

  • https://addons.mozilla.org/en-US/firefox/addon/commandrun/

This add-on will allow you to execute OS commands (e.g. execute files) from JavaScript. Before you can execute this from the add-on, you will need to add this command to the list of allowed commands.

To do this, go to about:config in the browser address bar. Right click on the list of preferences and select New > String. The name of the new preference you want to add is extensions.commandrun.allowedcommands. For the value, enter the following:

  • On Mac: ["/Users/Oliver/Desktop/keyboardViewer"]
  • On Windows: ["C:\WINDOWS\system32\osk.exe"]

HTML

Now, you will be able to open the on-screen keyboard from Firefox with HTML like this:

<script language="javascript">
function openKeyboard(){
    CommandRun.run("/Users/Oliver/Desktop/keyboardViewer", []);
}
</script>

<input type="text" onfocus="javascript:openKeyboard();" />

On Windows, substitute the following:

CommandRun.run("C:\WINDOWS\system32\osk.exe", []);

Alternative

An alternative is to write your own browser in something like Adobe Air. Using that method, your JavaScript calls your Air app and your Air app then executes keyboardViewer (or the Windows equivalent).

Saturday, October 23, 2021
 
demaxSH
answered 1 Month ago
37

Webkit is not compatible with the touch API for the moment.

If you want to know if the touch events api is supported, you can use modernizr and modify your UI depending to the results ...

Download it here : http://www.modernizr.com/ Then write something like this :

if (Modernizr.touch){
  // bind to touchstart, touchmove, etc and watch `event.streamId`
} else {
  // bind to normal click, mousemove, etc
}        

Modernizr customize also your body element's class, so you can access to it with CSS

You can complete it with yepnope js : http://yepnopejs.com/

yepnope({
  test: Modernizr.touch,
  yep: 'touch-ui.js',
  nope: 'standard-ui.js'
});

And when the events are supported, you can use jQuery mobile.

Friday, November 12, 2021
 
Rob N
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share