站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        精確查找PHP WEBSHELL木馬 修正版

        先來看下反引號(hào)可以成功執(zhí)行命名的代碼片段。代碼如下:

        復(fù)制代碼 代碼如下:
        `ls -al`;
        `ls -al`;
        echo “sss”; `ls -al`;

        $sql = “SELECT `username` FROM `table` WHERE 1”;

        $sql = ‘SELECT `username` FROM `table` WHERE 1’
        /*
        無非是 前面有空白字符,或者在一行代碼的結(jié)束之后,后面接著寫,下面兩行為意外情況,也就是SQL命令里的反引號(hào),要排除的就是它。
        */

        正則表達(dá)式該如何寫?
        分析:
        對(duì)于可移植性的部分共同點(diǎn)是什么?與其他正常的包含反引號(hào)的部分,區(qū)別是什么?
        他們前面可以有空格,tab鍵等空白字符。也可以有程序代碼,前提是如果有引號(hào)(單雙)必須是閉合的。才是危險(xiǎn)有隱患的。遂CFC4N給出的正則如下:【(?:(?:^(?:s+)?)|(?:(?P<quote>[“‘])[^(?P=quote)]+?(?P=quote)[^`]*?))`(?P<shell>[^`]+)`】。

        解釋一下:

        【(?:(?:^(?:s+)?)|(?:(?P<quote>[“‘])[^(?P=quote)]+?(?P=quote)[^`]*?))】匹配開始位置或者開始位置之后有空白字符或者前面有代碼,且代碼有閉合的單雙引號(hào)。(這段PYTHON的正則中用了捕獲命名以及反向引用)

        【`(?P<shell>[^`]+)`】這個(gè)就比較簡單了,匹配反引號(hào)中間的字符串。
        精確查找PHP WEBSHELL木馬 修正版
        python腳本檢測(cè)PHP WEBSHELL
        然后我將這段代碼寫入程序中,測(cè)試跑了一下discuz的程序。結(jié)果有一個(gè)誤報(bào)。誤報(bào)的位置為“config.inc.php”中的“define(‘UC_DBTABLEPRE’, ‘`ucenter`.uc_’);”,什么原因造成的?這行代碼符合了前面有閉合的引號(hào),也有反引號(hào)的使用,所以,符合要求,被檢測(cè)到了。如何再排除這種情況呢?這個(gè)有什么特殊的?前面有逗號(hào)“,”?如果是字符串連接的點(diǎn)號(hào)“.”呢?再排除逗號(hào)?

        好吧,我錯(cuò)了,我不該用我的思維來誤導(dǎo)你。換個(gè)思路。找下反引號(hào)可執(zhí)行的代碼的前面字符串的情況,他們只能是行的開始,或者有空白字符(包括空格,tab鍵等),再前面也可以有代碼的結(jié)束標(biāo)識(shí)分號(hào)“;”,其他的情況,都是不可以執(zhí)行的吧?嗯,應(yīng)該是這樣。(如有錯(cuò)誤,歡迎斧正)既然思路有了,那正則代碼更好寫了。如下【(^|(?<=;))s*`[^`]+`】,解釋一下,【(^|(?<=;))】匹配位置,是行的開始,或者前面有分號(hào)“;”。【s*`[^`]+`】空白字符任一個(gè),然后是….(你懂的)。OK,寫好之后,檢測(cè),又發(fā)現(xiàn)一個(gè)問題。
        精確查找PHP WEBSHELL木馬 修正版
        匹配引入文件的正則也匹配了“require_once ‘./include/db_’.$database.’.class.php’;”這種代碼,什么原因造成的,您自己分析吧。
        給出修復(fù)之后的python代碼,如下:

        復(fù)制代碼 代碼如下:
        #!/usr/bin/python
        #-*- encoding:UTF-8 -*-
        ###
        ## @package
        ##
        ## @author CFC4N <cfc4nphp@gmail.com>
        ## @copyright copyright (c) Www.cnxct.Com
        ## @Version $Id: check_php_shell.py 37 2010-07-22 09:56:28Z cfc4n $
        ###
        import os
        import sys
        import re
        import time
        def listdir(dirs,liston=’0′):
        flog = open(os.getcwd()+”/check_php_shell.log”,”a+”)
        if not os.path.isdir(dirs):
        print “directory %s is not exist”% (dirs)
        return
        lists = os.listdir(dirs)
        for list in lists:
        filepath = os.path.join(dirs,list)
        if os.path.isdir(filepath):
        if liston == ‘1’:
        listdir(filepath,’1′)
        elif os.path.isfile(filepath):
        filename = os.path.basename(filepath)
        if re.search(r”.(?:php|inc|html?)$”, filename, re.IGNORECASE):
        i = 0
        iname = 0
        f = open(filepath)
        while f:
        file_contents = f.readline()
        if not file_contents:
        break
        i += 1
        match = re.search(r”'(?P<function>b(?:include|require)(?:_once)?b)s*(?s*[“‘](?P<filename>[^;]*(?<!.(?:php|inc)))[“‘])?s*;”’, file_contents, re.IGNORECASE| re.MULTILINE)
        if match:
        function = match.group(“function”)
        filename = match.group(“filename”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [%s] – [%s] line [%d] n’% (function,filename,i)
        flog.write(info)
        print info
        iname += 1
        match = re.search(r’b(?P<function>eval|proc_open|popen|shell_exec|exec|passthru|system)bs*(‘, file_contents, re.IGNORECASE| re.MULTILINE)
        if match:
        function = match.group(“function”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [%s] line [%d] n’% (function,i)
        flog.write(info)
        print info
        iname += 1
        match = re.search(r'(^|(?<=;))s*`(?P<shell>[^`]+)`s*;’, file_contents, re.IGNORECASE)
        if match:
        shell = match.group(“shell”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [“] command is [%s] in line [%d] n’% (shell,i)
        flog.write(info)
        print info
        iname += 1
        f.close()
        flog.close()
        if ‘__main__’ == __name__:
        argvnum = len(sys.argv)
        liston = ‘0’
        if argvnum == 1:
        action = os.path.basename(sys.argv[0])
        print “Command is like:n %s D:wwwroot n %s D:wwwroot 1 — recurse subfolders”% (action,action)
        quit()
        elif argvnum == 2:
        path = os.path.realpath(sys.argv[1])
        listdir(path,liston)
        else:
        liston = sys.argv[2]
        path = os.path.realpath(sys.argv[1])
        listdir(path,liston)
        flog = open(os.getcwd()+”/check_php_shell.log”,”a+”)
        ISOTIMEFORMAT=’%Y-%m-%d %X’
        now_time = time.strftime(ISOTIMEFORMAT,time.localtime())
        flog.write(“n———————-%s checked ———————n”% (now_time))
        flog.close()

        稍微檢測(cè)了一下Discuz7.2的代碼,還是有誤報(bào)的,誤報(bào)的為這種包含sql的代碼:

        復(fù)制代碼 代碼如下:
        $query = $db->query(“SELECT `status`,`threads`,`posts`
        FROM `{$tablepre}forums` WHERE
        `status`=’1′;
        “);

        稍微檢測(cè)了一下Discuz7.2的代碼,還是有誤報(bào)的,誤報(bào)的為這種包含sql的代碼:

        復(fù)制代碼 代碼如下:
        $query = $db->query(“SELECT `status`,`threads`,`posts`
        FROM `{$tablepre}forums` WHERE
        `status`=’1′;
        “);

        由于這個(gè)腳本是按照一行一行的代碼來處理的,所以,有這種誤報(bào)。您自己去修復(fù)吧。相對(duì)網(wǎng)上流傳的腳本來說,還是比較準(zhǔn)確的。
        歡迎轉(zhuǎn)載。轉(zhuǎn)載請(qǐng)注明來源,以及留下博客鏈接,同時(shí),不能用于商業(yè)用途。(已經(jīng)修復(fù),增加了反引號(hào)后面【s*;】的判斷。2010-07-27 17:06)

        PS:如果說上傳文件也算是危險(xiǎn)的、值得注意的操作的話,建議加上move_uploaded_file函數(shù)的檢測(cè)。你知道在哪里加的。^_^

        2010-12-17 關(guān)于這些代碼,已經(jīng)放到google 的代碼托管上了。SVN地址為 http://code.google.com/p/cnxct/ 大家個(gè)獲得最新版。

        我是一個(gè)PHPer,寫的python有點(diǎn)憋,有點(diǎn)懶,還請(qǐng)各位安全界的大牛,程序界的前輩不要鄙視,要給建議,謝謝。php版的以后在寫吧。同時(shí),也歡迎各位安全愛好者反饋?zhàn)钚碌膚eb shell特征代碼,我盡力增加到程序中區(qū)。
        完整的代碼

        復(fù)制代碼 代碼如下:
        #!/usr/bin/python
        #-*- encoding:UTF-8 -*-
        ###
        ## @package
        ##
        ## @author CFC4N <cfc4nphp@gmail.com>
        ## @copyright copyright (c) Www.cnxct.Com
        ## @Version $Id$
        ###
        import os
        import sys
        import re
        import time
        def listdir(dirs,liston=’0′):
        flog = open(os.getcwd()+”/check_php_shell.log”,”a+”)
        if not os.path.isdir(dirs):
        print “directory %s is not exist”% (dirs)
        return
        lists = os.listdir(dirs)
        for list in lists:
        filepath = os.path.join(dirs,list)
        if os.path.isdir(filepath):
        if liston == ‘1’:
        listdir(filepath,’1′)
        elif os.path.isfile(filepath):
        filename = os.path.basename(filepath)
        if re.search(r”.(?:php|inc|html?)$”, filename, re.IGNORECASE):
        i = 0
        iname = 0
        f = open(filepath)
        while f:
        file_contents = f.readline()
        if not file_contents:
        break
        i += 1
        match = re.search(r”'(?P<function>b(?:include|require)(?:_once)?b)s*(?s*[“‘](?P<filename>[^;]*(?<!.(?:php|inc)))[“‘])?s*;”’, file_contents, re.IGNORECASE| re.MULTILINE)
        if match:
        function = match.group(“function”)
        filename = match.group(“filename”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [%s] – [%s] line [%d] n’% (function,filename,i)
        flog.write(info)
        print info
        iname += 1
        match = re.search(r’b(?P<function>eval|proc_open|popen|shell_exec|exec|passthru|system|assert|fwrite|create_function)bs*(‘, file_contents, re.IGNORECASE| re.MULTILINE)
        if match:
        function = match.group(“function”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [%s] line [%d] n’% (function,i)
        flog.write(info)
        print info
        iname += 1
        match = re.search(r'(^|(?<=;))s*`(?P<shell>[^`]+)`s*;’, file_contents, re.IGNORECASE)
        if match:
        shell = match.group(“shell”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [“] command is [%s] in line [%d] n’% (shell,i)
        flog.write(info)
        print info
        iname += 1
        match = re.search(r'(?P<shell>$_(?:POS|GE|REQUES)T)s*[[^]]+]s*(‘, file_contents, re.IGNORECASE)
        if match:
        shell = match.group(“shell”)
        if iname == 0:
        info = ‘n[%s] :n’% (filepath)
        else:
        info = ”
        info += ‘t|– [“] command is [%s] in line [%d] n’% (shell,i)
        flog.write(info)
        print info
        iname += 1
        f.close()
        flog.close()
        if ‘__main__’ == __name__:
        argvnum = len(sys.argv)
        liston = ‘0’
        if argvnum == 1:
        action = os.path.basename(sys.argv[0])
        print “Command is like:n %s D:wwwroot n %s D:wwwroot 1 — recurse subfolders”% (action,action)
        quit()
        elif argvnum == 2:
        path = os.path.realpath(sys.argv[1])
        listdir(path,liston)
        else:
        liston = sys.argv[2]
        path = os.path.realpath(sys.argv[1])
        listdir(path,liston)
        flog = open(os.getcwd()+”/check_php_shell.log”,”a+”)
        ISOTIMEFORMAT=’%Y-%m-%d %X’
        now_time = time.strftime(ISOTIMEFORMAT,time.localtime())
        flog.write(“n———————-%s checked ———————n”% (now_time))
        flog.close()
        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 精品人妻无码一区二区色欲产成人| 91麻豆精品视频| 久久99精品久久久久久hb无码| 国产精品亚洲玖玖玖在线观看 | 久久精品无码午夜福利理论片| 国产乱子伦精品免费视频| 久久国产精品99久久久久久老狼| 精品人妻大屁股白浆无码| 中文字幕在线亚洲精品| 欧美亚洲精品中文字幕乱码免费高清| 亚洲国产成人精品不卡青青草原| 国产精品内射后入合集| 色偷偷88888欧美精品久久久| 青青久久精品国产免费看| 国产精品日韩欧美在线第3页| 久久99热狠狠色精品一区| 国产精品久久网| 国精品无码一区二区三区左线| 亚洲精品国产精品国自产观看| 精品一区二区三区免费视频| 国产在线观看高清精品| 国产精品亚洲高清一区二区| 国产精品成人无码久久久久久| 四虎国产精品永久地址99| 99久久国产综合精品麻豆| 精品人妻va出轨中文字幕| 国自产偷精品不卡在线| 久久发布国产伦子伦精品| 精品亚洲成a人片在线观看| 无码精品一区二区三区在线 | 97久视频精品视频在线老司机| 久久亚洲国产精品一区二区| 久久久国产精品网站| 欧美日韩精品在线| 97精品国产高清自在线看超| 国产精品∧v在线观看| 国产精品热久久无码av| 国产亚洲精品精品国产亚洲综合 | 91久久婷婷国产综合精品青草| 国产精品视频一区二区三区四 | 精品国产福利在线观看|