Check two hardware id (hwid) if they're the same

Issues related to VMProtect
emile_ying
Posts: 6
Joined: Sat Jun 04, 2022 5:42 am

Check two hardware id (hwid) if they're the same

Postby emile_ying » Tue Jun 21, 2022 12:59 pm

I implement a function CheckHwidSame($hwid, $other_hwid), which returns true iff the inputs are the same hardwares. It follows the sql logic in web license manager. As my server's database has a different design, I cannot reuse the sql, so implement it myself.

I need this function in server side. The server stores user's license as well as the hardware id. If user requests license again, the server would check if a previous hwid the same user provided is the same as current hwid. If so, the related stored license is returned to user, so server doesn't need to generate a license each time and can manager and limit the number of licenses a user can have.

@Admin, could you check the implementation if it's correct?

Code: Select all

<?php

// $hwid and $other_hwid are both valid base64 encoded hardware id strings.
function CheckHwidSame($hwid, $other_hwid) {
   $hwdata = HwidDecode($hwid);
   $other_hwdata = HwidDecode($other_hwid);
   if (empty($hwdata) || empty($other_hwdata)) {
      return false;
   }
   return CheckHwdataSame($hwdata, $other_hwdata);
}

function HwidDecode($hwid)
{
   if (empty($hwid))
      return FALSE;
   // a decoded hwid is an array of devices. Each device is a 32-bit unsigned int value.
   $data = unpack("V*", base64_decode($hwid));
   //x64 system PHP bug workaround, to keep numbers unsigned
   foreach ($data as $i => $num)
   {
      if ($data[$i] < 0)
         $data[$i] += 4294967296;
   }
   return $data;
}

function CheckHwdataSame($hwdata, $other_hwdata) {
   $common = array();
   // $common would store common devices of the 2 hwids.
   foreach ($hwdata as $device) {
      foreach($other_hwdata as $other_device) {
         if ($device == $other_device) {
            array_push($common, $device);
            break;
         }
      }
   }
   // There're 4 types of devices, specified by the least 2 bits.
   // To prevent duplications, use $type array to mask occurrences.
   $type = array(false, false, false, false);
   foreach ($common as $device) {
      $type[$device & 3] = true;
   }
   // 2 hwids are the same if type 0 exists and 2 of the remaining 3 types exist.
   $two_of_three = ($type[1] && $type[2]) || ($type[1] && $type[3]) || ($type[2] && $type[3]);
   return $type[0] && $two_of_three;
}
?>

emile_ying
Posts: 6
Joined: Sat Jun 04, 2022 5:42 am

Re: Check two hardware id (hwid) if they're the same

Postby emile_ying » Wed Jun 22, 2022 4:13 am

Please ignore this post.
https://vmpsoft.com/forum/viewtopic.php?f=2&t=27544 contains the official source code.