Algorithm validation Mac Address

1

Does anyone know which validation algorithm is used by this site?

link

I searched the internet a lot but I could not find it on the site until it says how it is done but I do not have much time to study .. so I would like to know if anyone has a ready already ..

Note: it is the validation of Mac Address as existence, not just the format.

Language Preference: Javascript or PHP

    
asked by anonymous 17.11.2016 / 18:01

2 answers

5

Can not detect if the MAC address is real, you first need to understand what a MAC address is, as per wiki:

  

The MAC address ( Media Access Control ) is a physical address associated with the communication interface, which connects a device to the network. The MAC is a "single" address, there are no two ports with the same numbering, it is used for access control in computer networks. Its identification is recorded in hardware, that is, in the ROM of the network card of equipment such as desktops, notebooks, routers, smartphones, tablets, network printers, etc.

Then understand that:

  • The physical address can only be observed in LAN
  • Will not be public (or propagated to the internet)
  • It will be available through the ARP mapping.
  • You may be able to make the data public, but this will be the specific device that you have set up and "forced / made available"

Note that even getting it via a language running at a "lower level" and LAN was a little tricky for me ( Is it possible to determine the hardware address of the router? )

I was forced to use commands like (Windows operating system):

  • Get all addresses connected on the same network and "visible":

    arp -a
    

    Output:

    Interface: 192.168.2.54 --- 0xe
      Endereço IP           Endereço físico       Tipo
      192.168.2.1           b8-38-61-5d-84-28     dinâmico
      192.168.2.2           48-f8-b3-bc-45-d1     dinâmico
      192.168.2.4           c0-4a-00-87-aa-d6     dinâmico
      192.168.2.150         88-51-fb-22-31-9a     dinâmico
      192.168.2.255         ff-ff-ff-ff-ff-ff     estático
      224.0.0.2             01-00-5e-00-00-02     estático
      224.0.0.252           01-00-5e-00-00-fc     estático
      239.255.255.250       01-00-5e-7f-ff-fa     estático
      255.255.255.255       ff-ff-ff-ff-ff-ff     estático
    
  • Get address from a specified gateway or ip:

    arp -a 129.168.0.1
    

    Output:

    Interface: 192.168.2.54 --- 0xe
      Endereço IP           Endereço físico       Tipo
      192.168.2.1           b8-38-61-5d-84-28     dinâmico
    
  • Get data with ipconfig:

    ipconfig /all
    

    Output:

    Windows IP Configuration

     Nome do host. . . . . . . . . . . . . . . . : guilherme-PC
     Sufixo DNS primário . . . . . . . . . . . . :
     Tipo de nó. . . . . . . . . . . . . . . . . : híbrido
     Roteamento de IP ativado. . . . . . . . . . : não
     Proxy WINS ativado. . . . . . . . . . . . . : não
     Lista de pesquisa de sufixo DNS . . . . . . : home
    

    Wireless Network Adapter Wireless Network Connection:

     Sufixo DNS específico de conexão. . . . . . : router5d8428.com
     Descrição . . . . . . . . . . . . . . . . . : Intel(R) WiFi Link 1000 BGN
     Endereço Físico . . . . . . . . . . . . . . : 00-26-C7-D8-E8-08
     DHCP Habilitado . . . . . . . . . . . . . . : Sim
     Configuração Automática Habilitada. . . . . : Sim
     Endereço IPv6 de link local . . . . . . . . : fe80::34d0:d738:4aab:83cf%14(Preferencial)
     Endereço IPv4. . . . . . . .  . . . . . . . : 192.168.2.54(Preferencial)
     Máscara de Sub-rede . . . . . . . . . . . . : 255.255.255.0
     Concessão Obtida. . . . . . . . . . . . . . : quinta-feira, 17 de novembro de 2016 15:41:07
     Concessão Expira. . . . . . . . . . . . . . : domingo, 20 de novembro de 2016 03:57:36
     Gateway Padrão. . . . . . . . . . . . . . . : 192.168.2.1
     Servidor DHCP . . . . . . . . . . . . . . . : 192.168.2.1
     IAID de DHCPv6. . . . . . . . . . . . . . . : 184559303
     DUID de Cliente DHCPv6. . . . . . . . . . . : 00-01-00-01-1F-8D-3F-74-3C-4A-92-4E-40-CC
     Servidores DNS. . . . . . . . . . . . . . . : fd37:267c:7d7a:1:204:dfff:fe8c:e72d
                                                   192.168.2.1
     NetBIOS em Tcpip. . . . . . . . . . . . . . : Habilitado
     Lista de pesquisa de sufixos DNS específicos da conexão: home
    

How is the format?

According to the wiki:

  

The Image below presents a simplified version of the frame used in local Ethernet networks, known as the Ethernet frame. The first address identifies the recipient of the message, that is, the receiver. The second address identifies the sender, ie the sender. Each address consists of six bytes, theoretically allowing 2⁴⁸ addresses. For example, the number 00-0C-6E-3C-D1-6D represents an Ethernet address in hexadecimal format.

You can then validate by PHP or JavaScript the format only, using a like this:

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$

An example in PHP would be:

<?php
$mac_address = empty($_GET['mac_address']) ? '' : $_GET['mac_address'];

if (preg_match('#^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$#', $mac_address) > 0) {
     echo 'Validou!';
} else {
     echo 'Não Validou!';
}

In JavaScript:

<input placeholder="Digite seu endereço de MAC" type="text" id="mac_address" name="mac_address">
<button id="validar">Evniar</button>

<script>
function validaEnderecoFisico(endereco) {
    return /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(endereco);
}

var input = document.getElementById("mac_address");
var btn   = document.getElementById("validar");

btn.onclick = function() {
    if (validaEnderecoFisico(input.value)) {
        alert("Validou");
    } else {
        alert("Não validou");
        return false;
    }
};
</script>
However

However @Bacco pointed me to a service that can help make this link , they have a REST API that can bring you help check:

For example, go to (change your Mac address): link

Yet before using it, be aware that like the @ Bacco said :

  

You can not validate if it's real. It only has to validate if it is of "known brand", because not all the manufacturers respect this (and some pass for others, mainly in the market of "second line"). Virtually any 6 bytes described in hex are valid.

Some may try to pass themselves off as others.

However a simple example of using the API with PHP would be:

<?php 
$enderecoMac = 'DIGITE SEU ENDEREÇO DE MAC';
$url = 'http://www.macvendorlookup.com/api/v2/' . urlencode($enderecoMac);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);

$curl_err = curl_errno($ch);

if ($curl_err != 0) {
    $result = array( 'error' => 'Erro ao usar o CURL: ' . $curl_err );
} else {
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpCode != 200) {
        $result = array( 'error' => 'Erro ao baixar: ' . $httpCode );
    } else {
        $result = json_decode($data);
        $data = null;
    }
}

curl_close($ch);

var_dump($result);
    
17.11.2016 / 19:14
1

Well, according to the site you put the default of the MAC can be:

  • 6 groups of 2 hexadecimal separated by hyphen (-), eg 01-23-45-67-89-ab

  • 6 groups of 2 hexadecimal separated by a colon (:), eg 01: 23: 45: 67: 89: ab

  • 3 groups of 4 hexadecimal separated by period (.), eg 0123.4567.89ab

Given this pattern we can make a regex to validate all cases, starting with the first 2 that are practically the same, just changing the separator:

( ([0-9a-fA-F]{2}[:-]){5} ([0-9a-fA-F]{2}) )

Within the brackets I define valid hexadecimal values, they would be 0 through 9 and A through F (uppercase and lowercase), right after I set the amount I expect from these values which in this case is {2} , in then I have to define the separator which can be a colon or hyphen [:-] , and right after I define the amount of times it will be repeated, which in this case is the total number of groups-1 ** and then I put the group of hexadecimal again, only this time without the separator.

** The reason for putting the total of groups (which in this case is 6) minus 1 is for the end to be accepted without the tab, so I repeat the set at the end.

Now let's go to the last pattern:

( ([0-9a-fA-F]{4}\.){2} ([0-9a-fA-F]{4}) )

The hexadecimal set is exactly the same as the first patterns, just changing the number of repetitions to {4} , and then I define a new separator (which in the case is a dot) and repeat all this by the total number of groups - 1, and add the group again without the separator at the end.

Below is an example of the working script:

document.querySelector('#mac').addEventListener('keyup', function() {
  
  var status = this.value.match(/^((([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2}))|(([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})))$/);

  document.querySelector('#result').innerHTML = status ? 'Válido' : 'Inválido';
});
<div>
  <label>Digite o MAC:</label>  
  <input type="text" id="mac">
</div>

<div id="result"> </div>

NOTE: ^ is equal to the start of the regex, $ is equal to the end of the regex, and | is equal to the logical OR.

    
17.11.2016 / 19:55