您的位置:首页 > 运维架构 > 网站架构

杀毒软件 防火墙 PHPIDS打造php网站服务器三位一体宙斯盾安防

2012-08-07 16:22 627 查看
张智翔
杀毒软件防火墙 PHPIDS打造php网站服务器三位一体宙斯盾安防
 只要要发布信息到外网,就不可避免的碰到入侵与嗅探,环眼扫去,安全事故层出不穷,尤其php在web服务器这一块的巨大市场,如何打造安全体系呢?杀毒软件、防火墙、PHPIDS是我选择的铁三角,打造php网站服务器三位一体宙斯盾安防体系,选择元老级的防病毒软件(Antivirus)与防火墙(Firewall)防护主机外,其他还有入侵检测系统(IDS)、诱捕入侵检测系统(Honeypot)、使用分析系统、log文件分析系统等方式,以抵御或侦测各种可能的信息安全事件(如病毒、木马程序、DOS攻击);而目前最常使用的侦测防御系统非IDS莫属。
首先装上杀毒软件:360 金山趋势 麦咖啡
其次装上防火墙:第三方防火墙
深信服
绿盟 等其它检测工具
最后 装上IDS吧。
 IDS为一种对网络流量进行实时检测的系统,当发现可疑的传输(例如:DOS攻击),IDS会立即发出警报或主动防御;但由于近几年的攻击型态已经慢慢转变在应用层上发生,如XSS与SQL
Injection等Web端的攻击手法,传统的IDS已无法有效侦测。
PHPIDS』是一套供 PHP开发环境使用的入侵检测系统,它是利用事先定义的规则文件,逐项比对用户的输入数据,来达到预防入侵的效果。以下笔者将说明安装方式与试用的结果。





 请至「PHPIDS下载页面
下载:

PHPIDS 0.7 (ZIP)MD5SHA1

PHPIDS 0.7 (tarball, gzipped)MD5SHA1

PHPIDS 0.7 (tarball, bzipped)MD5SHA1

PHPIDS 0.6.5 (ZIP)MD5SHA1

PHPIDS 0.6.5 (tarball, gzipped)MD5SHA1

PHPIDS 0.6.5 (tarball, bzipped)MD5SHA1

PHPIDS 0.6.4 (ZIP)MD5SHA1

PHPIDS 0.6.4 (tarball, gzipped)MD5SHA1

PHPIDS 0.6.4 (tarball, bzipped)MD5SHA1

下载最新版本),解压缩之后共有三个文件夹:docs、lib、tests。其中,
「lib」文件夹存放其核心的程序代码,包括自定义规则文件与log暂存盘;
「docs」文件夹则存放说明文件,其中也包括范例档案。使用方法只要将lib文件夹复制到网页服务器的目录底下,
再将「docs/examples」内的example.php档案放在与lib文件夹同一个目录下,用户即可对example.php更改文件名。接着,请开启example.php找到如下程序代码:
set_include_path(

get_include_path()

. PATH_SEPARATOR

. 'lib/'

);
 将第四行的程序代码改成与lib文件夹相对的位置(因为笔者是将lib与example.php放置在同个目录之下,所以将其设成'lib/'),最后只要将网站内的所有php的档案引用example.php,即可使用本监控平台。例如在index.php这个档案的第一行加入:
include("example.php");
 以下笔者利用XSS与SQL Injection两种不同的攻击型态来对PHPIDS监控平台进行实验。
XSS是黑客利用网站上允许用户输入字符或字符串的字段插入HTML与Script语言,以达成其他正常使用者在观看网页的同时,浏览器会主动下载并执行部份恶意的程序代码,或被暗地里导入到恶意的网站,而受到某种型态的影响;
SQL Injection所攻击的目标并不是利用数据库本身的问题,也不是针对操作系统或是网站的漏洞,而是利用验证设计上的疏失进行攻击。
 在程序设计师撰写Web应用程序时,如果没有对使用者的输入做妥善的过滤与处理,那么,程序便将用户输入的内容组合成查询的指令,传送给数据库server执行;若用户输入之数据中含有某些对数据库系统有特殊意义的符号或命令时,便可能让使用者有机会对数据库系统下达指令,因而造成入侵行为。与XSS相同的是,使用者都是利用Web的输入字段来执行;不同的是,SQL
Injection会造成数据库的损失或者遭窃取数据。

 

 攻击的方式只要在有加入example.php的档案之网址后方加上攻击的字符串,例如XSS攻击:
index.php?<IFRAME

src="javascript:document.location.href='http://tw.yahoo.com';"></IFRAME>
 在网页内则会出现如【图1】侦测结果;若攻击型态变更成SQL Injection,则会出现如【图2】的结果。

图1 侦测XSS攻击结果

图2 侦测SQL Injection攻击结果
 首先,系统会自动统计受到影响的规则条件共有多少个;其次,是列出侦测出哪些类别的攻击型态(如xss表示侦测出Cross Site Scripting攻击、csrf表示侦测出Cross-site
request forgery、sqli表示侦测出SQL指令等),最后是条列式细部的侦测结果。另外,当系统侦测出具有攻击性语言的字符串时,会自动储存至lib/IDS/tmp的phpids_log.txt纪录,以方便系统管理者进行监控。

图3 XSS之规则文件
 PHPIDS之规则文件放置于「/lib/IDS/default_filter.xml」,如【图3】所示其中之一的规则,利用正规表示式(Regular
Expression)描述,并将所有描述存成XML文件格式;官方网站会不定时地更新规则文件,可有效防御新一波的Web攻击。另一方面,如果不想使用
PHPIDS,此处所提供的规则,也许可供开发人员的过滤程序参考。
结语与最优方案
 PHPIDS提供一个免费的程序代码,供Web开发人员参考,并可有效地杜绝Web的攻击手法,同时也提供实时监控的平台,供网站管理者查阅是否受到黑客攻击。但目前最大的缺点在于,网站下所有的php程序文件都需要加入example.php,才能将攻击隔绝于外,且每个程序文件加入之后是否会对网站效能有所影响,也是开发人员必须考虑的,有兴趣的读者,不妨下载安装藉以加强Web的安全性。

最优方案
网站有几百个php文件,我不可能都一一 include吧,那怎么办呢?
继续往下看——
安装步骤:
1、下载phpids
,我测试用的是版本7. http://www.open-open.com/news/view/cc55a2 2、解压phpids至网站根目录

3、如果无法解压至根目录可使用mod_rewrite

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/phpids(.*)

RewriteRule ^(.+)$ - [F]
配置使用:
1、编辑config/config.ini.php自定义配置。 无非是设置邮箱,发送邮箱,很简单。
2、 ; <?phpdie();?>
3、
4、 ; PHPIDS Config.ini
5、
6、 ; General configuration settings
7、
8、
9、 [General]
10、
11、 ; basic settings - customize to make the PHPIDS work at all
12、 filter_type = xml
13、
14、 ;base_path = /full/path/to/IDS/
15、 use_base_path = false
16、
17、 filter_path = default_filter.xml
18、 tmp_path = tmp
19、 scan_keys = false
20、
21、 ; in case you want to use a different HTMLPurifier source, specify it here
22、 ; By default, those files are used that are being shipped with PHPIDS
23、 HTML_Purifier_Path = vendors/htmlpurifier/HTMLPurifier.auto.php
24、 HTML_Purifier_Cache = vendors/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer
25、
26、 ; define which fields contain html and need preparation before
27、 ; hitting the PHPIDS rules (new in PHPIDS 0.5)
28、 ;html[] = POST.__wysiwyg
29、
30、 ; define which fields contain JSON data and should be treated as such
31、 ; for fewer false positives (new in PHPIDS 0.5.3)
32、 ;json[] = POST.__jsondata
33、
34、 ; define which fields shouldn't be monitored (a=c should be referenced via a.b)
35、 exceptions[] = GET.__utmz
36、 exceptions[] = GET.__utmc
37、
38、 ; you can use regular expressions for wildcard exceptions - example: /.*foo/i
39、
40、 ; PHPIDS should run with PHP 5.1.2 but this is untested - set
41、 ; this value to force compatibilty with minor versions
42、 min_php_version = 5.1.6
43、
44、 ; If you use the PHPIDS logger you can define specific configuration here
45、
46、 [Logging]
47、
48、 ; file logging
49、 path = tmp/phpids_log.txt
50、
51、 ; email logging
52、
53、 ; note that enabling safemode you can prevent spam attempts,
54、 ; see documentation
55、 recipients[] = test@test.com.invalid
56、 subject = "PHPIDS detected an intrusion attempt!"
57、 header = "From:<PHPIDS> info@phpids.org"
58、 envelope = ""
59、 safemode = true
60、 urlencode = true
61、 allowed_rate = 15
62、
63、 ; database logging
64、
65、 wrapper = "mysql:host=localhost;port=3306;dbname=phpids"
66、 user = phpids_user
67、 password = 123456
68、 table = intrusions
69、
70、 ; If you would like to use other methods than file caching you can configure them here
71、
72、 [Caching]
73、
74、 ; caching: session|file|database|memcached|none
75、 caching = file
76、 expiration_time = 600
77、
78、 ; file cache
79、 path = tmp/default_filter.cache
80、
81、 ; database cache
82、 wrapper = "mysql:host=localhost;port=3306;dbname=phpids"
83、 user = phpids_user
84、 password = 123456
85、 table = cache
86、
87、 ; memcached
88、 ;host = localhost
89、 ;port = 11211
90、 ;key_prefix = PHPIDS

91、 复制到lib目录下,更名为ids.php
92、 [b]<?php

93、
94、 /** 张迅雷原创,欢迎加qq—— 158393237 讨论
95、 *PHPIDS
96、 *Requirements:PHP5,SimpleXML
97、 *
98、 *Copyright(c)2010PHPIDSgroup(https://phpids.org)
99、 *
100、 *Thisprogramisfreesoftware;youcanredistributeitand/ormodify
101、 *itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
102、 *theFreeSoftwareFoundation;version2ofthelicense.
103、 *
104、 *Thisprogramisdistributedinthehopethatitwillbeuseful,
105、 *butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
106、 *MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE. Seethe
107、 *GNUGeneralPublicLicenseformoredetails.
108、 */
109、
110、 // set the include path properly for PHPIDS
111、 set_include_path(
112、 get_include_path()
113、 .
PATH_SEPARATOR
114、 . 'lib/'
115、 );
116、
117、 if (!session_id()) {
118、 session_start();
119、 }
120、
121、 require_once'IDS/Init.php';
122、
123、 try {
124、
125、 /*
126、 * It's pretty easy to get the PHPIDS running
127、 * 1. Define what to scan
128、 *
129、 * Please keep in mind what array_merge does and how this might interfer
130、 * with your variables_order settings
131、 */
132、 $request =array(
133、 'REQUEST' =>$_REQUEST,
134、 'GET' =>$_GET,
135、 'POST' =>$_POST,
136、 'COOKIE' =>$_COOKIE
137、 );
138、
139、 $init = IDS_Init::init(dirname(__FILE__)
. '/IDS/Config/Config.ini.php');
140、
141、 /**
142、 *Youcanalsoresetthewholeconfiguration
143、 *arrayormergeinowndata
144、 *
145、 *Thisusagedoesn'toverwritealreadyexistingvalues
146、 *$config->setConfig(array('General'=>array('filter_type'=>'xml')));
147、 *
148、 *Thisdoes(see2ndparameter)
149、 *$config->setConfig(array('General'=>array('filter_type'=>'xml')),true);
150、 *
151、 *oryoucanaccesstheconfigdirectlylikehere:
152、 */
153、
154、 $init->config['General']['base_path']
= dirname(__FILE__) .'/IDS/';
155、 $init->config['General']['use_base_path']
= true;
156、 $init->config['Caching']['caching']
= 'none';
157、
158、 // 2. Initiate the PHPIDS and fetch the results
159、 $ids =new IDS_Monitor($request,$init);
160、 $result =$ids->run();
161、
162、 /*
163、 * That's it - now you can analyze the results:
164、 *
165、 * In the result object you will find any suspicious
166、 * fields of the passed array enriched with additional info
167、 *
168、 * Note: it is moreover possible to dump this information by
169、 * simply echoing the result object, since IDS_Report implemented
170、 * a __toString method.
171、 */
172、 if (!$result->isEmpty()) {
173、 echo$result;
174、
175、 /*
176、 * The following steps are optional to log the results
177、 */
178、 require_once'IDS/Log/File.php';
179、 require_once'IDS/Log/Composite.php';
180、
181、 $compositeLog =new IDS_Log_Composite();
182、 $compositeLog->addLogger(IDS_Log_File::getInstance($init));
183、
184、 /*
185、 * Note that you might also use different logging facilities
186、 * such as IDS_Log_Email or IDS_Log_Database
187、 *
188、 * Just uncomment the following lines to test the wrappers
189、 */
190、 /*
191、 *
192、 require_once 'IDS/Log/Email.php';
193、 require_once 'IDS/Log/Database.php';
194、
195、 $compositeLog->addLogger(
196、 IDS_Log_Email::getInstance($init),
197、 IDS_Log_Database::getInstance($init)
198、 );
199、 */
200、 $compositeLog->execute($result);
201、
202、
203、 } else {
204、 echo'<a href="?test=%22><script>eval(window.name)</script>">No attack detected - click for an example attack</a>';
205、 }
206、 } catch (Exception$e) {
207、 /*
208、 * sth went terribly wrong - maybe the
209、 * filter rules weren't found?
210、 */
211、 printf(
212、 'An error occured: %s',
213、 $e->getMessage()
214、 );
215、 }

2、编辑php.ini,加入以下内容:
auto_prepend_file=”D:\MYOA\webroot\phpids\lib\ids.php”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: