简体   繁体   English

从 PHP 获取计算机唯一 ID

[英]Get Computer Unique ID from PHP

I've created an application using PHP and I'm going to sell it to my local market.我使用 PHP 创建了一个应用程序,我打算将它卖到我的本地市场。 I will personally be going to their locations to install/configure Apache & MySQL as well as installing my own code.我将亲自去他们的位置安装/配置 Apache 和 MySQL 以及安装我自己的代码。

I would like a security system so that if anyone attempts to copy my code to an unauthorized machine, it won't run.我想要一个安全系统,这样如果有人试图将我的代码复制到未经授权的机器上,它就不会运行。

I know no one can prevent reverse engineering an application.我知道没有人可以阻止对应用程序进行逆向工程。 even.exe (binary) files are cracked and with PHP (source code) anyone can do. even.exe(二进制)文件已破解,任何人都可以使用 PHP(源代码)。

In my country those reverse engineers are really hard to find, so I would like to propose minimal security options like:在我的国家,那些逆向工程师真的很难找到,所以我想提出最小的安全选项,比如:

1) Create class (say, Navigation ) which identifies system information like CPU ID, Computer name or any combination of hardware ID to make a UNIQUE_ID and matches with my given UNIQUE_ID (to the individual to whom I sold the application). 1) 创建 class(比如Navigation ),它识别系统信息,如 CPU ID、计算机名称或硬件 ID 的任意组合,以生成 UNIQUE_ID 并与我给定的 UNIQUE_ID(我向其出售应用程序的个人)匹配。 If it's valid, it returns the navigation menu.如果有效,则返回导航菜单。 Otherwise it will simply destroy the database and halt the execution by throwing an exception, maybe like:否则它只会破坏数据库并通过抛出异常来停止执行,可能像:

class Navigation {

    public function d() {
        return current system UNIQUE_ID;
    }

    public function get() {
        $a = file_get_contents('hash');
        $c = $this->d();
        if (crypt($c) != $a) {
            //destory database
            throw new Exception('');
        } else {
            return "<ul><li><a>home</a></li></ul>"; //navigation menu
        }
    }

}

2) Then during the installation process I'll change system UNIQUE_ID in "hash" file, create an object, and save it into a file (nav.obj): 2) 然后在安装过程中,我将更改“哈希”文件中的系统 UNIQUE_ID,创建一个 object,并将其保存到文件 (nav.obj) 中:

(install.php) (安装.php)

<?php
      $a=new Navigation;
      $out=serialize($a);
      file_put_contents('nav.obj', $out);

3) in header.php (which gets included in every file): 3)在 header.php(包含在每个文件中):

<?php
     $menu=file_get_contents('nav.obj');
     $menu=unserialize($a);
     echo $menu->get();
 ?>

I know this method isn't full proof, but I'm pretty sure that around 60% of PHP developers won't be able to crack it!我知道这种方法不是完全证明,但我敢肯定,大约 60% 的 PHP 开发人员将无法破解它!

Now I only need to get current system UNIQUE_ID.现在我只需要获取当前系统的 UNIQUE_ID。

I have created this function to get an unique ID based on hardware (Hard disk UUID). 我创建了这个函数来获取基于硬件(硬盘UUID)的唯一ID。 It is possible to use different resources like machine names, domains or even hard disk size to get a better approach depending on your needs. 可以使用不同的资源(如计算机名称,域或甚至硬盘大小)来根据您的需要获得更好的方法。

 function UniqueMachineID($salt = "") {
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $temp = sys_get_temp_dir().DIRECTORY_SEPARATOR."diskpartscript.txt";
        if(!file_exists($temp) && !is_file($temp)) file_put_contents($temp, "select disk 0\ndetail disk");
        $output = shell_exec("diskpart /s ".$temp);
        $lines = explode("\n",$output);
        $result = array_filter($lines,function($line) {
            return stripos($line,"ID:")!==false;
        });
        if(count($result)>0) {
            $result = array_shift(array_values($result));
            $result = explode(":",$result);
            $result = trim(end($result));       
        } else $result = $output;       
    } else {
        $result = shell_exec("blkid -o value -s UUID");  
        if(stripos($result,"blkid")!==false) {
            $result = $_SERVER['HTTP_HOST'];
        }
    }   
    return md5($salt.md5($result));
}


echo UniqueMachineID();

As per http://man7.org/linux/man-pages/man5/machine-id.5.html 根据http://man7.org/linux/man-pages/man5/machine-id.5.html

$machineId = trim(shell_exec('cat /etc/machine-id 2>/dev/null'));

EDIT for Tito: 编辑铁托:

[ekerner@**** ~]$ ls -l /etc/machine-id
-r--r--r--. 1 root root 33 Jul  8  2016 /etc/machine-id

EDIT 2 for Tito: Some things to consider and scenarios: 编辑2 for Tito:需要考虑的一些事项和场景:

Is the user allowed to get a new machine? 用户是否可以获得新机器? Id guess yes. 我想是的。 Or run on multiple devices? 或者在多个设备上运行? Sounds like the machine could be irrelevant in your case? 听起来机器可能与你的情况无关?

If its user only (no machine restrictions) then Id go for a licencing service (relies on network). 如果仅限用户(没有机器限制),那么Id将获得许可服务(依赖于网络)。 There are many services for this: Google Play (for Android apps) is a good example: https://developer.android.com/google/play/licensing/index.html MS and Apple have similar services. 有很多服务:Google Play(适用于Android应用)就是一个很好的例子: https//developer.android.com/google/play/licensing/index.html MS和Apple也有类似的服务。 However just search the web for the term "Software Licensing Service" or "Cloud Based Software Licensing Service". 但是,只需在网上搜索“软件许可服务”或“基于云的软件许可服务”这一术语。

If its user + single device, then youll need to pass up the device id to whatever service you use or make , then allow the machine id to be updated, but not allow revert to previous machine id (would mean multiple devices). 如果它的用户+单个设备,那么您需要将设备ID传递给您使用或制作的任何服务,然后允许更新机器ID,但不允许恢复到以前的机器ID(将意味着多个设备)。 However said services will give you the client code which should take care of that if its a requirement. 但是,所述服务将为您提供客户端代码,如果需要,应该对其进行处理。

Two scenarios from experience: 1: User on any device: we simply made an API in the cloud (in a website) and a login screen in the app, when the user logged in it authenticated via the API and kept a token, and whenever the device was connected to the net the app would query the API and update the login and/or token. 经验中的两个场景:1:任何设备上的用户:我们只是在云中(在网站中)和应用程序中的登录屏幕上创建API,当用户登录时通过API进行身份验证并保留令牌,并且每当设备已连接到网络,应用程序将查询API并更新登录和/或令牌。 You could alternatively have the login screen in the purchase (like maybe they already logged into a site to purchase), generate a key and pack it with or bind it into the app. 您也可以在购买时使用登录屏幕(例如,他们已经登录到要购买的网站),生成密钥并将其打包或绑定到应用程序中。

2: User plus machine: Same thing except when the API is queried the machine id is passed up. 2:用户加机器:同样的事情,除了查询API时,机器ID被传递出去。 The machine ID can change as many times as the user updates their device, but we kept a record of machine ids and made to ban rule on: if we saw an old (previously used) machine id then a certain amount of time had to have passed. 机器ID可以随着用户更新设备而改变多次,但是我们保留了机器ID的记录并禁止对其进行规则:如果我们看到旧的(以前使用过的)机器ID,那么必须有一定的时间通过。 Thus allowed the user to break their machine and pull out an old one. 从而允许用户打破他们的机器并拉出旧机器。

Also to consider if you make one, how will you stop the app from working? 另外要考虑一下,如何阻止应用程序运行? Ppl are pretty clever it will need to be core compiled. Ppl很聪明,需要核心编译。

However that all being said, the various licensing services are pro at this and can cater for most needs. 然而,尽管如此,各种许可服务都是专业的,可以满足大多数需求。 Plus in their experience theyve already overcome the security pitfalls. 除了他们的经验,他们已经克服了安全隐患。 Id name one that I like except its yours to search out. Id命名我喜欢的,除了你的搜索。

Nice if you can come on back with and positive or negative outcomes from your trails. 如果您可以从您的路径中获得积极或消极的结果,那就太好了。

function getMachineId() {
    $fingerprint = [php_uname(), disk_total_space('.'), filectime('/'), phpversion()];
    return hash('sha256', json_encode($fingerprint));
}

This will get a probably-unique id based on a hash of:这将获得一个基于 hash 的可能唯一的 ID:

  1. The server's OS, OS version, hostname, and architecture.服务器的操作系统、操作系统版本、主机名和体系结构。
  2. The total space (not free space) on the drive where the php script is. php 脚本所在的驱动器上的总空间(不是可用空间)。
  3. The Unix timestamp creation time of the computer's root file system. Unix 计算机根文件系统的时间戳创建时间。
  4. The currently installed PHP version.当前安装的PHP版本。

Unlike the other answers it doesn't depend on shell_exec() being enabled.与其他答案不同,它不依赖于启用shell_exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM