Rumah >pembangunan bahagian belakang >tutorial php >使用Cron和Php检测网页是否被篡改_PHP
网络安全一直是业界永恒的话题,人们一直在与黑客之间进行着从不间断的较量。那么作为普通的一个网站如何能用最简单的方法去防止自己的网页被篡改呢?或者至少在当自己的网页被篡改后能以最快的速度得知呢?在本文中,将介绍使用Cron和Php结合的方法,检查某些指定的网页是否被篡改。
这个方法的核心思路其实很简单:“创建一个简单的数据库,其中保存了需要保护的文件的hash值,并且采用unix的cron调度方法,定期检测实际文件的hash值和原始文件的hash值,并且形成报告。”
当然,其中要涉及递归遍历服务器文件目录下的大量文件,并且要计算它们的hash值,下面将具体讲解其过程。
数据库设计
我们先来看下数据库应该如何设计。为了安全起见,我们应该单独建立一个数据库以存放需要保护文件的。如果读者的网站是托管网站的话,建议使用cPanel去创建数据库并且使用强密码(比如可以用strongpasswordgenerator.com这个网站去产生各长度位数的密码,至少要8位以上)。我们将数据库命名为 baseline,表设计如下:
<ol class="dp-c"> <li class="alt"><span><span>CREATE TABLE baseline ( </span></span></li> <li><span> file_path VARCHAR(200) NOT NULL, </span></li> <li class="alt"><span> file_hash CHAR(40) NOT NULL, </span></li> <li><span> acct VARCHAR(40) NOT NULL </span></li> <li class="alt"><span> PRIMARY KEY (file_path) </span></li> <li><span> ); </span></li> <li class="alt"><span> </span></li> <li><span> CREATE TABLE tested ( </span></li> <li class="alt"><span> tested DATETIME NOT NULL, </span></li> <li><span> account VARCHAR(40) NOT NULL </span></li> <li class="alt"><span> PRIMARY KEY (tested) </span></li> <li><span> ); </span></li> </ol>
在baseline表中,包含了一个长度很大的字段file_path,存放的是要保护文件的在服务器上的路径,而file_hash(用40位的长度去进行SHA1算法),而acct字段则表示是否监视账号还是域名。我们并将file_path设置为主键。
而tested表中的tested字段则保存每次扫描的具体时间,而account字段和baseline表的acct字段是相同的,以允许分别单独扫描账号还是域名。
定义PHP文件前的一些准备工作
接下来,我们为开发php文件做一些准备工作,首先要定义一些php文件中要用到的常量。
PATH。这个是要在你的服务器上进行扫描的起始路径,通常是指代DocumentRoot。记得不要使用Windows中的反斜杠因为Apache和PHP都使用的是正向的斜杠。
访问数据库要涉及的地址,用户密码等参数,如SERVER ('localhost'), USER, PASSWORD and DATABASE
以及一些其他变量如下:
保存需要检查文件扩展名的数组。在这个例子中,只用数组保存了如.php、.htm和.js格式的文件。在本文中,如果使用了一个空的数组,则默认检查所有格式的文件(这是最安全的,但耗费不少资源)。
需要排除检查的目录。一般不建议这么做,如果确实需要不检查某个目录,则可以将其放置在本文中的一个数组中
此外还有几个参数需要设置,包括$file数组,初始化为空,$report初始化为空字符串以及$act字符串(配合数据表中的account/acct字段使用)。
开始编码
下面我们正式编码,先看如下代码:
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span><span class="comment">//初始化</span><span> </span></span></li> <li class="alt"><span><span class="vars">$ext</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">"php"</span><span>,</span><span class="string">"html"</span><span>,</span><span class="string">"js"</span><span>); </span></span></li> <li><span><span class="comment">//skip保存要忽略检查的文件夹 </span><span> </span></span></li> <li class="alt"><span><span class="vars">$skip</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">"protected"</span><span>); </span></span></li> <li><span><span class="comment">// use define statements or enter values directly in the mysqli_connect</span><span> </span></span></li> <li class="alt"><span>define(<span class="string">'SERVER'</span><span>,</span><span class="string">'localhost'</span><span>); </span></span></li> <li><span>define(<span class="string">'USER'</span><span>,</span><span class="string">'your user name'</span><span>); </span></span></li> <li class="alt"><span>define(<span class="string">'PASS'</span><span>,</span><span class="string">'your password'</span><span>); </span></span></li> <li><span>define(<span class="string">'DATABASE'</span><span>,</span><span class="string">'database name'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="vars">$db</span><span> = mysqli_connect(SERVER,USER,PASS,DATABASE); </span></span></li> <li class="alt"><span><span class="vars">$dir</span><span> = </span><span class="keyword">new</span><span> RecursiveDirectoryIterator(PATH); </span></span></li> <li><span><span class="vars">$iter</span><span> = </span><span class="keyword">new</span><span> RecursiveIteratorIterator(</span><span class="vars">$dir</span><span>); </span></span></li> <li class="alt"><span><span class="keyword">while</span><span> (</span><span class="vars">$iter</span><span>->valid()) </span></span></li> <li><span>{ </span></li> <li class="alt"><span> <span class="comment">// 忽略不需要检测的目录</span><span> </span></span></li> <li><span> <span class="keyword">if</span><span> (!</span><span class="vars">$iter</span><span>->isDot() && !in_array(</span><span class="vars">$iter</span><span>->getSubPath(), </span><span class="vars">$skip</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="comment">// 获得指定要检测文件的扩展名</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$ext</span><span>)) </span></span></li> <li><span> { </span></li> <li class="alt"><span> <span class="comment">// PHP 5.3.4使用如下语句 if (in_array($iter->getExtension(), $ext))</span><span> </span></span></li> <li><span> <span class="keyword">if</span><span> (in_array(</span><span class="func">pathinfo</span><span>(</span><span class="vars">$iter</span><span>->key(), PATHINFO_EXTENSION), </span><span class="vars">$ext</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="vars">$files</span><span>[</span><span class="vars">$iter</span><span>->key()] = hash_file(</span><span class="string">"sha1"</span><span>, </span><span class="vars">$iter</span><span>->key()); </span></span></li> <li class="alt"><span> } </span></li> <li><span> } <span class="keyword">else</span><span> { </span></span></li> <li class="alt"><span> <span class="comment">// 针对要忽略检查的文件</span><span> </span></span></li> <li><span> <span class="vars">$files</span><span>[</span><span class="vars">$iter</span><span>->key()] = hash_file(</span><span class="string">"sha1"</span><span>, </span><span class="vars">$iter</span><span>->key()); </span></span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"><span> <span class="vars">$iter</span><span>->next(); </span></span></li> <li><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>
下面我们来讲解下上面的代码。首先,使用的是php中的两个内置函数RecursiveDirectoryIterator(获得指定目录下的所有文件和目录),然后进行循环遍历,并且检查每一个目录是否在需要排除检测的目录之中,如果包含在检测列表中的话同时检测是否有需要排除检测的文件。最后将最终需要检测的文件放置在数组$files中,这个数组的键为文件的名称,而值则为经过SHA1算法运算后的哈希值。所以文件的数量可以马上通过以下方法获得
<ol class="dp-c"><li class="alt"><span><span class="vars">$report</span><span> .= </span><span class="string">"Files has "</span><span> . </span><span class="func">count</span><span>(</span><span class="vars">$files</span><span>) . </span><span class="string">" records.\r\n"</span><span>; </span></span></li></ol>
然后,我们要从tested表中,获得最新一次经过哈希扫描的文件的时间,如下代码:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$results</span><span> = mysqli_query(</span><span class="vars">$db</span><span>,"SELECT tested FROM tested WHERE acct = </span><span class="string">'$acct'</span><span> </span></span></li> <li><span> ORDER BY tested DESC LIMIT 1"); </span></li> <li class="alt"><span><span class="keyword">if</span><span> (</span><span class="vars">$results</span><span>) </span></span></li> <li><span>{ </span></li> <li class="alt"><span> <span class="keyword">while</span><span>(</span><span class="vars">$result</span><span>=mysqli_fetch_array(</span><span class="vars">$results</span><span>)) </span></span></li> <li><span> { </span></li> <li class="alt"><span> <span class="vars">$tested</span><span> = </span><span class="vars">$result</span><span>[</span><span class="string">'tested'</span><span>]; </span></span></li> <li><span> } </span></li> <li class="alt"><span><span class="vars">$report</span><span> .= </span><span class="string">"Last tested $tested.\r\n"</span><span>; </span></span></li> <li><span>} </span></li> </ol>
接下来,要对比的是经过hash扫描的文件的最新hash值和原来baseline表中的文件的哈希值是否有改变,使用的代码如下:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$files</span><span>)) </span></span></li> <li><span>{ </span></li> <li class="alt"><span> <span class="vars">$result</span><span> = mysqli_query(</span><span class="vars">$db</span><span>,</span><span class="string">"SELECT * FROM baseline"</span><span>); </span></span></li> <li><span> <span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$result</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="keyword">foreach</span><span> (</span><span class="vars">$result</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$value</span><span>) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="vars">$baseline</span><span>[</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_hash"</span><span>]; </span></span></li> <li class="alt"><span> } </span></li> <li><span> <span class="vars">$diffs</span><span> = </span><span class="func">array_diff_assoc</span><span>(</span><span class="vars">$files</span><span>, </span><span class="vars">$baseline</span><span>); </span></span></li> <li class="alt"><span> unset(<span class="vars">$baseline</span><span>); </span></span></li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span><span class="comment">// 分别将不相同的部分保存到 Deleted, Altered 和 Added 数组</span><span> </span></span></li> <li class="alt"><span><span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$files</span><span>)) </span></span></li> <li><span>{ </span></li> <li class="alt"><span> <span class="vars">$results</span><span> = mysqli_query(</span><span class="vars">$db</span><span>,</span><span class="string">"SELECT file_path, file_hash FROM baseline WHERE acct = '$acct'"</span><span>); </span></span></li> <li><span> <span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$results</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="vars">$baseline</span><span> = </span><span class="keyword">array</span><span>(); </span><span class="comment">// from database</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$diffs</span><span> = </span><span class="keyword">array</span><span>(); </span><span class="comment">// $files 和 $baseline数组的不同</span><span> </span></span></li> <li><span> <span class="keyword">while</span><span> (</span><span class="vars">$value</span><span> = mysqli_fetch_array(</span><span class="vars">$results</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="keyword">if</span><span> (!</span><span class="func">array_key_exists</span><span>(</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>], </span><span class="vars">$files</span><span>)) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="comment">// 删除了的文件</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$diffs</span><span>[</span><span class="string">"Deleted"</span><span>][</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]; </span></span></li> <li><span> <span class="vars">$baseline</span><span>[</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_hash"</span><span>]; </span></span></li> <li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li> <li><span> <span class="comment">// 改变过的文件</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">if</span><span> (</span><span class="vars">$files</span><span>[</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] </span><span class="vars">$value</span><span>[</span><span class="string">"file_hash"</span><span>]) </span></span></li> <li><span> { </span></li> <li class="alt"><span> <span class="vars">$diffs</span><span>[</span><span class="string">"Altered"</span><span>][</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]; </span></span></li> <li><span> <span class="vars">$baseline</span><span>[</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]; </span></span></li> <li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li> <li><span> <span class="comment">// 没改变的文件</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$baseline</span><span>[</span><span class="vars">$value</span><span>[</span><span class="string">"file_path"</span><span>]] = </span><span class="vars">$value</span><span>[</span><span class="string">"file_hash"</span><span>]; </span></span></li> <li><span> } </span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"><span> <span class="keyword">if</span><span> (</span><span class="func">count</span><span>(</span><span class="vars">$baseline</span><span>) <span class="func">count</span><span>(</span><span class="vars">$files</span><span>)) </span></span></span></li> <li><span> { </span></li> <li class="alt"><span> <span class="comment">// 增加的文件</span><span> </span></span></li> <li><span> <span class="vars">$diffs</span><span>[</span><span class="string">"Added"</span><span>] = </span><span class="func">array_diff_assoc</span><span>(</span><span class="vars">$files</span><span>, </span><span class="vars">$baseline</span><span>); </span></span></li> <li class="alt"><span> } </span></li> <li><span> unset(<span class="vars">$baseline</span><span>); </span></span></li> <li class="alt"><span> } </span></li> <li><span>} </span></li> </ol>
当上面这段代码执行完毕后,$diffs数组或者是空的或者会包含改变了的文件(删除,修改,增加)和它们的哈希值。
然后我们可以将结果通过EMAIL发送给用户了。代码如下:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span> (!</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$diffs</span><span>)) { </span></span></li> <li><span><span class="vars">$report</span><span> .= </span><span class="string">"The following discrepancies were found:\r\n\r\n"</span><span>; </span></span></li> <li class="alt"><span><span class="keyword">foreach</span><span> (</span><span class="vars">$diffs</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$status</span><span> => </span><span class="vars">$affected</span><span>) </span></span></li> <li><span>{ </span></li> <li class="alt"><span> <span class="keyword">if</span><span> (</span><span class="func">is_array</span><span>(</span><span class="vars">$affected</span><span>) && !</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$affected</span><span>)) </span></span></li> <li><span> { </span></li> <li class="alt"><span> <span class="vars">$report</span><span> .= </span><span class="string">"* $status *\r\n\r\n"</span><span>; </span></span></li> <li><span> <span class="keyword">foreach</span><span>(</span><span class="vars">$affected</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$path</span><span> => </span><span class="vars">$hash</span><span>) </span><span class="vars">$report</span><span> .= </span><span class="string">" ?$path\r\n"</span><span>; </span></span></li> <li class="alt"><span> } </span></li> <li><span>} </span></li> <li class="alt"><span>} <span class="keyword">else</span><span> { </span></span></li> <li><span> <span class="vars">$report</span><span> .= </span><span class="string">"File structure is intact.\r\n"</span><span>; </span></span></li> <li class="alt"><span>} </span></li> <li><span><span class="vars">$mailed</span><span> = mail(</span><span class="string">'you@example.com'</span><span>, </span><span class="vars">$acct</span><span> . </span><span class="string">' Integrity Monitor Report'</span><span>,</span><span class="vars">$report</span><span>); </span></span></li> </ol><p> </p>
并且要更新baseline表和tested表的数据,代码如下:
<ol class="dp-c"> <li class="alt"><span><span class="comment">// 清除旧数据</span><span> </span></span></li> <li><span>mysqli_query(<span class="vars">$db</span><span>,</span><span class="string">"DELETE FROM baseline WHERE acct = '$acct'"</span><span>); </span></span></li> <li class="alt"><span><span class="comment">// 将新文件和对应的hash值加入</span><span> </span></span></li> <li><span><span class="keyword">foreach</span><span> (</span><span class="vars">$files</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$path</span><span> => </span><span class="vars">$hash</span><span>) </span></span></li> <li class="alt"><span>{ </span></li> <li><span> mysqli_query(<span class="vars">$db</span><span>,"INSERT INTO baseline (file_path, file_hash, acct) </span></span></li> <li class="alt"><span> VALUES (<span class="string">'$path'</span><span>,</span><span class="string">'$hash'</span><span>, </span><span class="string">'$acct'</span><span>)"); </span></span></li> <li><span>} </span></li> <li class="alt"><span>mysqli_query(<span class="vars">$db</span><span>,</span><span class="string">"INSERT INTO tested (tested, acct) VALUES (NOW(), '$acct')"</span><span>); </span></span></li> <li><span>mysqli_close(<span class="vars">$db</span><span>); </span></span></li> </ol>
最后,为了能让系统定时地执行这个php文件,可以充分利用unix中的cron任务计划,因此可以编写cron的文件如下:
<ol class="dp-c"><li class="alt"><span><span>/usr/local/bin/php -q /home/account/hashscan.php </span></span></li></ol>
其中,/usr/local/bin/php就是你服务器上php的路径,可以根据实际情况设置每天隔多久去检测一下服务器上的文件(这可以通过编写cron表达式去实现,关于如何编写cron表达式,读者可以参考相关的资料)。
要注意的是,本文介绍的只是如何在unix/linux上,针对简单的php网站使用检验文件哈希值的方法对文件进行检验是否篡改,其中依然要求用户首先的确保文件服务器安全的情况下,将所有相关文件上传完毕。假如管理员需要对文件进行更新修改,则读者可以利用本文中提到的原理,进行扩展修改以达到自定制的目的。本文的参考代码可以在:
http://dk.co.nz/HashAlert2.zip下载。
原文链接:ttp://www.sitepoint.com/detect-hacked-files-via-cronphp/