Tag: security

  • Run httpd with SELinux

    我实在不理解 SELinux,以前为了求简单,都是直接 disable SELinux。我也知道这样不好,可以没办法。

    最近重装系统,第一次选 Web Server 来装,我想这样搭配出来的 SELinux 最接近我的要求。可是,但我添加了 zend.repo 以后,发现 httpd 无法启动,错误信息为:

    # service httpd start
    Starting httpd: httpd: Syntax error on line 221 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 of /etc/httpd/conf.d/zendserver_php.conf: Cannot load /usr/local/zend/lib/apache2/libphp5.so into server: /usr/local/zend/lib/apache2/libphp5.so: cannot enable executable stack as shared object requires: Permission denied
                                                               [FAILED]
    

    我按论坛上的建议,执行了

    # find /usr/local/zend/lib/ -name "*.so*" -exec semanage fcontext -a -t textrel_shlib_t {} \;
    

    错误仍在。然后按 SELinux 的建议,执行了

    # setsebool -P httpd_execmem 1
    

    呵,成了。因为我不会 undo # find /usr/local/zend/lib/ -name “*.so*” -exec semanage fcontext -a -t textrel_shlib_t {} \; 所以我不知道此命令是否必要。

  • SSL, Nginx and Magento

    SSL, Nginx and Magento 这三件东西对我都不陌生。但三件全排在一起,着实挑战了我一下,发生错误是 secure page redirect loop。

    Magento secure page redirect loop
    Magento secure page redirect loop

    原因是 $_SERVER[] 里缺少 HTTPS directive,需要在 fastcgi_params 里添加一行

    fastcgi_param HTTPS on;

    以 Magento 1.4.0.1 为例深究一下—— 在 $_SERVER[‘HTTPS’] 缺失时 app/code/core/Mage/Core/Model/Store.php 的 isCurrentlySecure() 返回值 false,所以 Magento 不停地重定向到 secure url 而不知道当前 url 已经是 secure 了。

    
    public function isCurrentlySecure()
    {
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
    return true;
    }
    
    if (Mage::isInstalled()) {
    $secureBaseUrl = Mage::getStoreConfig('web/secure/base_route_url');
    if (!$secureBaseUrl) {
    return false;
    }
    $uri = Zend_Uri::factory($secureBaseUrl);
    $isSecure = ($uri->getScheme() == 'https' )
    && isset($_SERVER['SERVER_PORT'])
    && ($uri->getPort() == $_SERVER['SERVER_PORT']);
    return $isSecure;
    } else {
    $isSecure = isset($_SERVER['SERVER_PORT']) && (443 == $_SERVER['SERVER_PORT']);
    return $isSecure;
    }
    }
    
    

    我一直觉得用 php 探测当前 protocol 是否为 https 的算法比较“土”,曾经以为会有更好的探测办法,目前看来是没有。不光是没有,而且不可能有。就如有个小秘负责拆信,然后把有用的信纸交给 CEO 阅读,所以 CEO 不可能知道某封信是拿什么信封装的。

  • Install SSL certificate on Nginx step by step

    Although I have installed SSL certificates for several times, every time I google step by step guide. So now I take my own note:

    Step 1: Generate key

    $ openssl genrsa -des3 -out verisign-mydomain.co.uk.key 1024
    Generating RSA private key, 1024 bit long modulus
    ………………….++++++
    …………………++++++
    e is 65537 (0x10001)
    Enter pass phrase for verisign-mydomain.co.uk.key:
    Verifying – Enter pass phrase for verisign-mydomain.co.uk.key:

    Step 2:  Remove pass phrase from key (otherwise Nginx asks for pass phrase every time it starts)

    $ openssl rsa -in verisign-mydomain.co.uk.key -out verisign-mydomain.co.uk.no-passphrase.key
    Enter pass phrase for verisign-mydomain.co.uk.key:
    writing RSA key

    Step 3: Generate CSR

    $ openssl req -new -key verisign-mydomain.co.uk.no-passphrase.key -out verisign-mydomain.co.uk.csr
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter ‘.’, the field will be left blank.
    —–
    Country Name (2 letter code) [GB]:
    State or Province Name (full name) [Berkshire]:E Sussex
    Locality Name (eg, city) [Newbury]:Brighton
    Organization Name (eg, company) [My Company Ltd]:Euro Ltd
    Organizational Unit Name (eg, section) []:
    Common Name (eg, your name or your server’s hostname) []:mydomain.co.uk
    Email Address []:

    Please enter the following ‘extra’ attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:

    Step 4: Send CSR to authority and get CRT

    Step 5: Download CA intermediate certificate from authroity

    Step 6: Concatenate CA intermediate certificate with CRT (CRT on top)

    $ cat ca.crt >> my.crt

    Step 7: Make necessary changes to Nginx configuration

    Nginx only need two file: my.crt (concatenated) and verisign-mydomain.co.uk.no-passphrase.key

  • Magento user be warned: eav_entity_store has realtime sales data

    最近在一个 Magento 网站上大幅调整了 catalog structure。我先在测试服务器上调整产品属性、目录属性等,然后把测试服务器数据库里所有 catalog 和 eav 开头的表导入到生产服务器。因为生产站点的销售没有中断,我不能简单地从测试服务器往生产服务器导入整个 magento 数据库。

    这次升级初看很成功,随后就发现百密中有一疏。我装有 protx standard (for SagePay Form integration),顾客在重定向到 SagePay 付款时,表单预填的数据是别人的。原因是测试服务器的跟销售有关数据是生产服务器若干天前的,eav_entity_store 表里保存有 increment_last_id,我从测试服务器往生产服务器导入所有 catalog 和 eav 开头的表,导致 magento 再次分配几天已分配过的 order ID 给新订单。如果顾客在重定向到 SagePay 后点击 cancel,会导致同订单号的老订单 status change to cancelled。

    ID 重复是一个很低级的错误,我不应该去导入 eav_entity_store 表。我是知道这张表的作用的,这个错误应该归咎于我考虑不周到。

    Protx standard 这个模块也不够周到,我用的版本比较老,不知新版是否在这方面有改进。

  • CNVD-2010-00979

    互联网是一个很容易以讹传讹的地方,这大概是人们不满意 google 的地方,要搞个知识搜索取而代之。

    最近金山毒霸和360杀毒口水之争,我无意关心了一把。看到被多家网站(包括人民网)转发的金山网盾再陷新的诚信危机“漏洞门”,其中提到

    5月24日,中国国家信息安全漏洞库证实金山网盾存在一个高危的内核本地提权漏洞(国家漏洞库编号:CNVD-2010-00979),并公布了该漏洞利用相关细节。同一天,国外权威漏洞机构Secunia研究并收录了金山网盾这一漏洞,同时对该漏洞进行了自己的解释和归类(SA ID:SA39916)。国内外两大权威安全机构同时证实金山网盾存在高危漏洞,把此前一直高调否认软件有漏洞的金山公司置于非常尴尬的境地。

    我好奇了一下,上 cnvd 和 secunia 去查了一下,CNVD-2010-00979 和 SA ID:SA39916 讲的是多个供应商”rpc.pcnfsd”整数溢出漏洞,跟金山网盾无关,真不知道这个刹有其事的报导从何而来。

    虽然CNVD-2010-00979 跟金山网盾无关,但我有感觉无风不起浪,因为这是在 cnvd 上搜索金山网盾的结果:

    Google search result cached something
    Google search result cached something

    但实际查看被缓存的页面却什么也没有:

    Nothing on cached page
    Nothing on cached page

    或许 cnvd 只把详细信息公开给了 google spider;或许这其中有太多的内幕是我们常人无法知道的……

  • Discovery: PhProxy & CHtml

    我认为突破访问限制的终级方案是 VPN,可是设置 VPN 的技术含量有点高,到现在我还搞不掂 OpenVPN,只能徘徊于 PPTP。今天发现可爱的 php 也有了 proxy(或许早就有了,我不知道罢了),虽然我用不着,但对国内的朋友们确实是天大的福音。简单易行,一分钟就能搭一个!

    我只发现 phproxy 三个不大的缺陷:

    1. 无法代理流文件(比如无法看 youtube 上的 video,但访问 youtube 没问题)
    2. 因为 phproxy 工具栏在网页顶部,如果代理网页在相同位置绝对定位,则会重叠在一起(比如 wikipedia,我想修改一下 style.css 就好了,或者做一个自动隐藏的工具栏)
    3. 有些被代理的网页自检而跳出代理,重定向到原来的 url(比如 google spreadsheet)

    牛人真不少,还有一个 CHtml,竟然用 recaptcha 去实现突破,构思非常巧妙。

  • Magento product model is read only to frontend controller

    最近我写了一个小程序:Deal of the day for Magento。

    它主要有以下功能:
    每天定时按预设权重随机抽取一个产品作为 deal of the day;在产品名称前加上 “Deal of the day:” 字样;设定它的 is_deal_of_the_day 为 true;设定一个 special price;归类到一个 sales category 下;同时把昨天的已过时的 deal 重置为原先状态。

    Magento 支持 cronjob 我是知道的,但测试的第一个环节倒不是这个程序能否定时启动,而是程序能否完成独立完成这一系列的操作。为求简单,我把整个过程写在一个 frontend controller 的 indexAction 里,然后用访问 url 的方式去触发这个过程。我没有用 backend controller,因为我考虑到 Magento wiki 上介绍的 cronjob 万一不管用(之前我没有用过 Magento conjob),我还可以用 curl + linux crontab (这个我很熟)去触发这个过程。当然我会把这个 url 做得谁都猜不到,以策安全。

    但是,在 frontend controller 里一运行到 $product->save() 或 $productCollection->save(),就抛出以下错误:

    Warning: Invalid argument supplied for foreach() in /path/to/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 995

    初看这个错误,我总以为是 product EAV 某个 attribute 损坏了,因为我经常修改 product attributes,一不小心,改坏了哪个不该改的 attribute 也不奇怪。可难就难在 attributes 那么多,我无法知道哪个坏了啊。我围绕着 Abstract.php 995 行左看右看,没有头绪整整一个下午,最后发现,如果把同样代码放在 backend controller 里,product 就可以 save 了。

    整整一个下午的时间只给我一个启示:product model 在 frontend 是只读的,估计又属于 Magento 安全机制范畴。联想到以前碰到的一个问题:如果用户在 Magento backend 已登录,能否在 frontend 探知,以便针对 backend 用户访问 frontend 时显示不对外人开放的内容(就像 wordpress 那样会在每个 post 显示 edit link)。但 Magento 把 backend 和 frontend 严格地分开,我研究好久,也没有让 frontend 访问到 backend session。

    我想,打破 Magento 苦心经营的安全机制去做一些事是不可取的。比如 deal of the day 定时切换的功能,就用 magento 鼓励的 cron.php 去启动,非常的方便,也非常的安全。只是我空下来还是想钻牛角尖: Magento 是怎么把 backend 和 frontend 严格地分开的?只有知道了原理,如果、万一、假如、倘若我要 frontend 执行 backend 操作,也能做到了。

  • Absolutely secure online banking

    一年前我老板中了个毒导致银行账号密码失窃差点损失7,800镑,之后他来问我公司应该建立怎么的安全制度(security protocol)。

    我认为,要绝对的安全很容易,但往往增强安全的同时给用户日常操作带来不便,抵消了网上银行的优势。如何加强安全又不失方便性?我当时的解决方案是:

    1. 设立一台独立主机服务器,打上所有的安全补丁,启用必要的防火墙。服务器上只要有个浏览器,其他的都是不必要的。建议使用Firefox with Linux。
    2. 客户端以ssh+vnc方式连入服务器。
    3. 客户端连入服务器后,只能操作网上银行,不得浏览其他任何网站,也不得进行其他任何操作。

    第2步是个技术活,可以保证客户端即使在中木马等病毒的情况下,病毒也无法探知客户端通过服务器中转的输入。除非病毒能监视并分析所有的键盘、鼠标操作。

    第3步是关键,必须严格执行。安全隐患大多是人为的因素,如何用户没有好的安全意识,再好的技术支持也枉然。

    但这个方案未能得到贯彻。没过几天老板好了伤疤忘了痛,仍用日常电脑登录网上银行。在我看来不复杂的绝对安全的方案对他来说还是太复杂了,没有方便性的方案就不是解决方案。如何保证网上银行的绝对安全作为悬而未决的课题在我脑袋里盘绕了一年,直到最近尝试了 Chromium OS,我看到曙光。

    稍后谈谈我对 Chromium OS 的看法。

  • OpenPassword

    Inspired by OpenId, I want to start a project – OpenPassword.

    What is it for?
    People are hesitate to register themselves from site to site. One of the reasons is people are afraid of giving password to the web sites they do not trust much. Most people do not have big memory, so they have to use the same password for all web sites. They may or may not aware that any webmaster / web owner who has access to the password database could try the same password on other sites (or even worse – for online banking). Whether people’s passwords are in safe depends on how honest the webmaster / web owner is. Even the webmaster / web owner is honest, does he have basic skills to keep password in safe? I see a lot of web sites save passwords unencrypted, which could be a disaster.

    OpenId comes to solve this problem. But currently not all web sites support OpenId. What if you are in a position you have to use some web sites which do not support OpenId? You have to take care of security yourself. My proposal is:

    1. You register different web sites with different passwords.
    2. The passwords are generated with one-way encryption.
    3. You host encryption algorithm and salt in a safe place, so you do not need remember the passwords. Every time you need them, you generate / retrieve them on the fly.
  • My worry about Linksys wireless security

    前文提到 Linksys IP Camera 只能连入 Linksys wireless router,一开始就不喜欢厂家设置人为技术障碍的做法。细想之下,更觉得 Linksys wireless 安全机制堪忧。

    因为 Linksys WVC210 wireless IP camera 在设置无线联网时,只问一个 SSID,只要是 Linksys wireless router,不需密码就自动联入?如果是别人家的 Linksys wireless router 怎么办?虽然我凭着一个 Linksys WVC210 wireless IP camera 也控制不了别人家的网络设备,但如果我能对 Linksys WVC210 wireless IP camera 进行编程,是不是就相当于在别人家放置了颗隐形炸弹?Linksys wireless 或许有它独到之处,或许能隔离自动联网的设备,但我总觉得 wireless device isolation 不如直接用 password 来得更安全(至少心理上更舒服)。