{"id":227,"date":"2014-06-29T09:04:32","date_gmt":"2014-06-29T09:04:32","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=227"},"modified":"2014-06-29T09:04:32","modified_gmt":"2014-06-29T09:04:32","slug":"%e6%9f%a5%e6%89%bephpwebshell%e7%9a%84python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/227.html","title":{"rendered":"\u67e5\u627ephpwebshell\u7684Python\u811a\u672c"},"content":{"rendered":"<div style=\"color: #000000;\">\n<h3 style=\"color: #333333;\">\u67e5\u627ephpwebshell\u5c0f\u5de5\u5177<\/h3>\n<\/div>\n<div style=\"color: #000000;\">\n<p style=\"color: #666666;\">\u601d\u8def\u5c31\u662f<span style=\"color: #ff0000; font-size: medium;\"><strong>\u5bf9\u6bd4\u811a\u672c\u6587\u4ef6\u4ee5\u53ca\u914d\u7f6e\u6587\u4ef6\u7684hash\uff0c\u627e\u51fahash\u6539\u53d8\u7684\u811a\u672c\u6216\u8005\u914d\u7f6e\u6587\u4ef6<\/strong><\/span>\uff0c\u811a\u672c\u5171\u4e09\u4e2a\u53c2\u6570\uff1a<\/p>\n<p style=\"color: #666666;\">1\uff0c-save \u5373\u8bfb\u53d6web\u76ee\u5f55\u548c\u914d\u7f6e\u6587\u4ef6\uff0c\u8ba1\u7b97\u6587\u4ef6\u7684hash\u503c\u5e76\u4e14\u6279\u91cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1b<\/p>\n<p style=\"color: #666666;\">2\uff0c-find \u5373\u901a\u8fc7\u4fdd\u5b58\u7684hash\u503c\u5217\u8868\u6587\u4ef6\u4e0e\u5f53\u524dweb\u76ee\u5f55\u811a\u672c\u6587\u4ef6\u548c\u914d\u7f6e\u6587\u4ef6hash\u8fdb\u884c\u6bd4\u8f83\uff0c\u627e\u51fahash\u53d8\u52a8\u7684\u6587\u4ef6\uff1b<\/p>\n<p style=\"color: #666666;\">3\uff0c-listen \u9996\u5148\u6267\u884csave\u3002\u7136\u540e\u5faa\u73af\u6267\u884cfind\uff0c\u5982\u679c\u53d1\u73b0hash\u5f02\u5e38\u901a\u8fc7email\u901a\u77e5\u7ba1\u7406\u5458\u3002\u6bcf\u6b21find\u5b8c\u6210\u540e\u90fdsleep\u4e00\u6bb5\u65f6\u95f4\uff0c\u8fd9\u91cc\u9ed8\u8ba4\u534a\u4e2a\u5c0f\u65f6\uff0c\u76f8\u5f53\u4e8e\u534a\u4e2a\u5c0f\u65f6\u68c0\u67e5\u4e00\u6b21\u3002<\/p>\n<pre class=\"lang:default decode:true \">import hashlib\nimport sys\nimport os\nimport re\nimport time\nimport smtplib\nfrom email.mime.text import MIMEText\n\ndef send_mail(content):\n    to_list=[\"xxx@qq.com\"]\n    mail_host=\"smtp.163.com\"\n    mail_user=\"xxxx\"\n    mail_pass=\"xxxr\"\n    mail_postfix=\"163.com\"\n    me=mail_user+\"&lt;\"+mail_user+\"@\"+mail_postfix+\"&gt;\"\n    msg = MIMEText(content)\n    msg['Subject'] ='warning'\n    msg['From'] = me\n    msg['To'] = \";\".join(to_list)\n    try:\n        s = smtplib.SMTP()\n        s.connect(mail_host)\n        s.login(mail_user,mail_pass)\n        s.sendmail(me, to_list, msg.as_string())\n        s.close()\n        return True\n    except Exception, e:\n        print str(e)\n        return False\n\ndef md5Checksum(filePath):\n        fh = open(filePath, 'rb')\n        m = hashlib.md5()\n        while True:\n            data = fh.read(8192)\n            if not data:\n                break\n            m.update(data)\n        fh.close()\n        return m.hexdigest()\ndef load_hash(filepath):\n        pass\ndef load_filelist(f):\n        f1=open(f,'r')\n        f_list=[]\n        while 1:\n            line=f1.readline()\n            if not line:\n                break\n            f_list.append(line)\n        dic={}\n        for str1 in f_list:\n            item1,item2= str1.split(':')\n            dic[item1]=item2\n        f1.close()\n        return dic\n\ndef save_config(configpath,webdir):\n        f1=open('config','w')\n        f1.writelines('configpath:'+configpath+'rn')\n        f1.writelines('webdir:'+webdir+'rn')\n        f1.close()\ndef find():\n        lists=[]\n        lists=findchange()\n        for str1 in lists:\n           print str1\ndef findchange():\n        relist=[]\n        dic1={}\n        dic1= load_filelist('save_hash')\n        dic2={}\n        dic2=load_filelist('config')\n        weblist=[]\n        weblist=load_all_path(dic2['webdir'].replace('rn',''))\n        weblist.append(str(dic2['configpath'].replace('rn','')))\n        for webpage in weblist:\n           if str(dic1.get(webpage))=='None':\n               relist.append(webpage+' is new filern')\n           elif str(dic1.get(webpage)).replace('rn','')!=md5Checksum(webpage):\n              relist.append(webpage+'   has been changedrn')\n        return relist\ndef load_all_path(rootDir):\n    str1=[]\n    list_dirs = os.walk(rootDir)\n    for root, dirs, files in list_dirs:\n        for f in files:\n               if  str(os.path.splitext(f)[1])=='.php' or str(os.path.splitext(f)[0])=='.htaccess':\n                   str1.append(str(os.path.join(root, f)))\n    return str1\ndef save(config,webpath):\n    save_config(config,webpath)\n    confighash=md5Checksum(config)\n    weblist=[]\n    weblist=load_all_path(webpath)\n    print weblist\n    f1=open('save_hash','w')\n    f1.writelines(config+':'+confighash+\"rn\")\n    for str1 in weblist:\n        print str1\n        f1.writelines(str1+':'+md5Checksum(str1)+\"rn\")\n    f1.close()\ndef listen(config,webpath):\n    save(config,webpath)\n    while 1:\n        lists=[]\n        lists=findchange()\n        if(len(lists)!=0):\n           str2=''\n           for str1 in lists:\n               str2=str2+str1.replace('rn','')+'n'\n           send_mail(str2)\n        time.sleep(3600)\n\nif __name__ == '__main__':\n    banner='''usage:\n    find.py -save config webpath\n    find.py -find\n    nohup python  find.py -listen config webpath $\n    Example:\n    python find.py -save \/etc\/apache2\/apache2.conf \/var\/www\n    python find.py -find\n    nohup python find.py -listen \/etc\/apache2\/apache2.conf \/var\/www &amp;\n    email:2243280774@qq.com\n    '''\n\n    if (len(sys.argv)&lt;2):\n        print banner\n    elif (len(sys.argv)==4 and sys.argv[1]=='-save'):\n        save(sys.argv[2],sys.argv[3])\n    elif (len(sys.argv)==2 and sys.argv[1]=='-find'):\n        find()\n    elif (sys.argv[1]=='-listen'):\n         listen(sys.argv[2],sys.argv[3])\n    else :\n        print banner<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div style=\"color: #000000;\">====\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u5df2\u89e3\u51b3====<br \/>\n\u811a\u672c\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff1a<\/div>\n<div style=\"color: #000000;\">1.\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u8be5\u811a\u672c\u65f6\uff0c\u662f\u4e0d\u4e00\u5b9a\u5b58\u5728config\u6587\u4ef6\u7684\uff0c\u800c\u4ee3\u7801\u4e2d\u76f4\u63a5\u4f7f\u7528open(filePath, &#8216;rb&#8217;)\u65b9\u5f0f\u8bfb\u53d6\uff0c\u5728\u4f7f\u7528&#8221;-save&#8221;\u9009\u9879\u65f6\u4f1a\u62a5\u9519{IOError: [Errno 2] No such file or directory: &#8216;D:temp_configure.conf&#8217;}\uff08<span style=\"color: #ff0000;\">\u8865\u5145\uff1a\u4e4b\u524d\u662f\u81ea\u5df1\u7406\u89e3\u9519\u8bef\uff0c\u8fd9\u91cc\u7684\u914d\u7f6e\u6587\u4ef6\u662f\u6307\u9700\u8981\u88ab\u76d1\u63a7\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6240\u4ee5\u80af\u5b9a\u662f\u5b58\u5728\u7684\uff0c\u4e0d\u8fc7\u5982\u679c\u80fd\u5728\u4ee3\u7801\u4e2d\u52a0\u5165\u5bb9\u9519\u8bed\u53e5\u80af\u5b9a\u662f\u6700\u597d\u4e0d\u8fc7\u7684<\/span>\uff09<\/div>\n<div style=\"color: #000000;\">2.\u81ea\u5df1\u5728\u65b0\u5efa\u4e86\u914d\u7f6e\u6587\u4ef6(\u5185\u5bb9\u53ef\u4e3a\u7a7a)\u4e4b\u540e\uff0c\u6267\u884c&#8221;-find&#8221;\u9009\u9879\u540e\uff0c\u56e0\u4e3a\u4ee3\u7801\u4e2d\u7684load_filelist()\u51fd\u6570\uff0c\u662f\u901a\u8fc7split(&#8216;:&#8217;)\u6765\u8bfb\u53d6\u7684\u6587\u4ef6\u540d\u548cHash\u503c\uff0c\u4f46\u662f\uff0c\u5728Windows\u4e2d\uff0c\u8def\u5f84\u4e2d\u59cb\u7ec8\u90fd\u4f1a\u5b58\u5728\u4e00\u4e2a\u76d8\u7b26\u540e\u9762\u8ddf\u7740&#8217;:&#8217;\u56e0\u6b64\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u6bd4\u8f83\u597d\u7684\u89e3\u51b3\u529e\u6cd5(\u6211\u89c9\u5f97\u5e94\u8be5\u662f\u4ee5&#8221;#&#8221;\u6765\u5206\u9694\u8981\u597d\u4e9b\uff0c\u6216\u8005\u662f&#8217;t&#8217;){ValueError: too many values to unpack}<\/div>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\"><span style=\"color: #ff0000;\"><strong>\u4ee3\u7801\u6765\u6e90<\/strong><\/span>\uff1a<a href=\"http:\/\/www.freebuf.com\/tools\/8341.html\" target=\"_blank\">\u67e5\u627ephpwebshell\u5c0f\u5de5\u5177<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u627ephpwebshell\u5c0f\u5de5\u5177 \u601d\u8def\u5c31\u662f\u5bf9\u6bd4\u811a\u672c\u6587\u4ef6\u4ee5\u53ca\u914d\u7f6e\u6587\u4ef6\u7684hash\uff0c\u627e\u51fahash\u6539\u53d8\u7684\u811a\u672c\u6216\u8005\u914d\u7f6e\u6587 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,25,12],"tags":[8,49],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-programing","category-security","category-tools","tag-python","tag-webshell"],"views":4737,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}