从 PHP 获取计算机唯一 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17523167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Computer Unique ID from PHP
提问by Tito
I've created an application using PHP and I'm going to sell it to my local market. I will personally be going to their locations to install/configure Apache & MySQL as well as installing my own code.
我已经使用 PHP 创建了一个应用程序,我将把它卖给我的本地市场。我将亲自前往他们的位置安装/配置 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.
我知道没有人可以阻止对应用程序进行逆向工程。甚至 .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). 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:
1) 创建类(例如Navigation
),用于识别系统信息,如 CPU ID、计算机名称或硬件 ID 的任意组合,以生成 UNIQUE_ID 并与我给定的 UNIQUE_ID(与我向其出售应用程序的个人)匹配。如果有效,则返回导航菜单。否则它会简单地破坏数据库并通过抛出异常来停止执行,可能像:
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)然后在安装过程中,我将在“hash”文件中更改系统UNIQUE_ID,创建一个对象,并将其保存到一个文件(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。
回答by cardeol
I have created this function to get an unique ID based on hardware (Hard disk UUID). 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.
我创建了这个函数来获取基于硬件的唯一 ID(硬盘 UUID)。根据您的需要,可以使用不同的资源(如机器名称、域甚至硬盘大小)来获得更好的方法。
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();
回答by ekerner
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:
Tito 的编辑 2:需要考虑的一些事情和场景:
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). There are many services for this: Google Play (for Android apps) is a good example: https://developer.android.com/google/play/licensing/index.htmlMS and Apple have similar services. However just search the web for the term "Software Licensing Service" or "Cloud Based Software Licensing Service".
如果只有它的用户(没有机器限制),那么 Id 去获得许可服务(依赖于网络)。这方面有很多服务:Google Play(用于 Android 应用程序)就是一个很好的例子:https: //developer.android.com/google/play/licensing/index.htmlMS 和 Apple 有类似的服务。但是,只需在网络上搜索“软件许可服务”或“基于云的软件许可服务”一词即可。
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). However said services will give you the client code which should take care of that if its a requirement.
如果它的用户 + 单个设备,那么您需要将设备 ID 传递给您使用或制作的任何服务,然后允许更新机器 ID,但不允许恢复到以前的机器 ID(意味着多个设备)。但是,所述服务将为您提供客户端代码,如果需要,该代码应该负责。
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. 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.
经验中的两个场景: 1:用户在任何设备上:我们只是在云中(在网站中)制作了一个 API 并在应用程序中制作了一个登录屏幕,当用户登录时通过 API 进行身份验证并保留令牌,并且每当设备已连接到网络,应用程序将查询 API 并更新登录名和/或令牌。您也可以在购买时使用登录屏幕(比如他们可能已经登录到要购买的网站),生成一个密钥并将其打包或绑定到应用程序中。
2: User plus machine: Same thing except when the API is queried the machine id is passed up. 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. Thus allowed the user to break their machine and pull out an old one.
2:用户加机器:相同的事情,除了在查询API时传递机器ID。机器 ID 可以随着用户更新他们的设备而改变多次,但我们保留了机器 ID 的记录并禁止规则:如果我们看到旧的(以前使用过的)机器 ID 那么必须有一定的时间通过。因此允许用户打破他们的机器并拉出一台旧机器。
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.
如果你能从你的足迹中得到积极或消极的结果,那就太好了。