简体   繁体   English

linux上的系统时间变化检测

[英]System Time change detection on linux

I was reading about the licensing of software and one question that came to my mind is that "how software detect the change in system time and block themselves if someone changes system time?".我正在阅读有关软件许可的文章,我想到的一个问题是“如果有人更改系统时间,软件如何检测系统时间的变化并阻止自己?”。 Since there is no other reference available(provided we don't have internet connection, otherwise we can use time servers as reference), how such software manages to do it?由于没有其他可用的参考(假设我们没有互联网连接,否则我们可以使用时间服务器作为参考),这样的软件是如何做到的? Are there standard algorithms or any standard libraries(C/C++) available for implementing the same.是否有标准算法或任何标准库(C/C++)可用于实现相同的算法。 Please suggest.请建议。

You cannot assume that because the clock goes backwards, it is caused by someone trying to circumvent your licensing.你不能假设因为时钟倒退,它是由试图规避你的许可的人造成的。

Consider, for example, the situation where someone accidentally sets the clock way into the future, then resets it to the correct time?例如,考虑一下某人不小心将时钟设置为未来的情况,然后将其重置为正确的时间?

Crippling your software by attempting to time-restrict it is not the right thing to do.通过尝试限制时间来削弱您的软件不是正确的做法。 Consider making decent software instead.考虑制作像样的软件。

Your software can regularly ask for system time (using boost::posix_time::second_clock::local_time() ) and compare it with the last stored value.您的软件可以定期询问系统时间(使用boost::posix_time::second_clock::local_time() )并将其与上次存储的值进行比较。 If you observe a significant negative difference, someone probably changed the system time.如果您观察到显着的负差异,则可能是有人更改了系统时间。

Beware that time can be changed on a system for legitimate reasons : the computer can be moved to another timezone, the system can be using NTP and the computer clock is ticking too fast so NTP very often sets time to an earlier value or when daylight saving time change occurs.请注意,系统上的时间可以出于合法原因更改:计算机可以移动到另一个时区,系统可以使用 NTP 并且计算机时钟滴答得太快,因此 NTP 经常将时间设置为较早的值或夏令时发生时间变化。

Keep also in mind that those kind of "protection" aren't really effective in practice.还要记住,这种“保护”在实践中并不是真正有效的。 If you have to store the last system date somewhere, you take the risk that someone will find where and change it to something in a far future to grant himself new licensing rights.如果您必须将最后一个系统日期存储在某个地方,您就会冒着有人会在很远的将来找到位置并将其更改为某些内容以授予自己新的许可权的风险。

As far as I know, there is no elegant and reliable solution to this problem.据我所知,这个问题没有优雅可靠的解决方案。

clock_gettime(CLOCK_MONOTONIC, &ts) gives you a timespec independent of the system time. clock_gettime(CLOCK_MONOTONIC, &ts) 为您提供独立于系统时间的时间规范。 You can use it to calc an offset between CLOCK_REALTIME and if that changes, your systemtime has changed您可以使用它来计算 CLOCK_REALTIME 之间的偏移量,如果该偏移量发生变化,则您的系统时间已更改

One strategy I have read about in the past is to create, update and check a file in a folder.我过去读过的一种策略是在文件夹中创建、更新和检查文件。 You can update the date as it changes and if it goes backwards you leave it alone and can get suspicious.您可以在日期发生变化时更新日期,如果日期倒退,您可以不理会它并且可能会引起怀疑。

However there is no 100% method of proving time shifting, in my limited experience any piracy feature usually hurts the helpdesk and your customers more than it hurts the pirates.然而,没有 100% 的方法可以证明时间转移,根据我有限的经验,任何盗版功能通常对帮助台和您的客户的伤害都大于对盗版的伤害。

It is a far better strategy to get to know your trial users than to criminalise them.了解您的试用用户比将他们定为犯罪要好得多。

You can set up a timer that checks every n ticks (one tick = one (multitude of) cpu clock tick) if the system time difference is positive... Should be enough to check every second or even once a minute.如果系统时间差为正,您可以设置一个计时器,每 n 个滴答(一个滴答 = 一个(多个)cpu 时钟滴答)检查一次……应该足以每秒检查一次,甚至每分钟检查一次。 Usually a timer shouldn't use up that much resources.通常计时器不应该消耗那么多资源。 You must make sure though that the timer uses the ticks, not the system clock...您必须确保计时器使用滴答声,而不是系统时钟......

Generally you can't detect such things consistently, since people may just reboot the machine in question.通常,您无法始终如一地检测到此类事情,因为人们可能只是重新启动了有问题的机器。

In an ancient place I was working once an admin changed the clock because he had forgotten to renew the license for a compiler.在一个古老的地方,我曾经工作过一次,管理员更改了时钟,因为他忘记更新编译器的许可证。 Not really a brilliant idea.这不是一个绝妙的主意。 Basically the machine soon became completely unusable: NFS was messed up, make didn't work, a lot of trouble.基本上机器很快就完全无法使用:NFS 搞砸了, make不起作用,很麻烦。

So my guess is that your problem is more of theoretical nature.所以我的猜测是你的问题更具有理论性质。 In real life, nobody sane will probably do that.在现实生活中,没有理智的人可能会这样做。

You could always set the timestamp on a file to the current system time every time your application starts.每次应用程序启动时,您始终可以将文件的时间戳设置为当前系统时间。

Check the time first before setting it - if it's in the future, then the clock has gone backwards since you last set it.在设置之前先检查时间 - 如果是在未来,那么自上次设置以来时钟已经倒退。

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

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