• mitmproxy 使用mitmdump 过滤请求


    mitmproxy 抓包工具,优点可以使用python进行二次开发,或者进行接口的mock

    官网地址:https://www.mitmproxy.org/

    打算用这个最初的需求是,想对app做接口测试,但是app的接口有200多个,这么多接口,如果人工的的抓包或者获取,很浪费人力,那能不能自动的抓取到所有的请求呢?

    答案是可以,使用mitmdump -s xx.py 可以过滤我们想要的数据,比如存到数据库之类的

    from mitmproxy import ctx

    # 所有的请求都会经过request
    def request(flow):
        info = ctx.log.info
        d = {}
        if flow.request.host == 'xxxxx':  # 过滤请求,如果host是xxx则写入请求相关信息
            d['url'] = flow.request.url
            d['host'] = flow.request.host
            d['headers'] = flow.request.headers
            d['method'] = flow.request.method
            if flow.request.method == 'POST':
            d['body'] = flow.request.get_text()
            fp = open("/Users/dcc/Desktop/aaaa.txt",'a+',encoding='utf-8')
            fp.write(str(d) + ' ')

    # 所有的请求都会经过request
    def response(flow):
        info = ctx.log.info
        # info(str(flow.response.headers))
        # info(str(flow.response.cookies))
        info(str(flow.response.text))
        fp = open("/Users/dcc/Desktop/bbbb.txt",'a+',encoding='utf-8')
        fp.write(str(flow.response.text) + ' ')

    那既然能拿到所有的请求是不是可以为所欲为了呢?具体的应用场景可以自行尝试哈!比如爬虫?

    MOCK功能:

        mock功能也是类似,在request里判断发送的请求是不是自己想要的,然后进行修改

    修改请求:    

    def request(flow):
        if flow.request.url.startswith("http://xxx.x.xxx.com.cn/xxx/xxxx/xxxx"):
            ctx.log.info("modify request form")
        if flow.request.urlencoded_form:
            flow.request.urlencoded_form["code"] = "11111"
        else:
            flow.request.urlencoded_form = [
                ("abc", "1234"),("name","name")
            ]

    修改返回:

    def response(self, flow):
        if flow.request.url.startswith("https://xxx.x.xxx.com.cn/activityInfo/getPrizeInfo=="):
        //获取响应的json字符串,转成python对象进行解析和修改
            response = json.loads(flow.response.get_text())
            response['limitCount'] = 1
            //修改完成后,奖python对象转成json字符串,set进请求的响应体重发送给客户端
            flow.response.set_text(json.dumps(response))
            ctx.log.info('modify limitCount')

    学习最大的乐趣在于分享,我是绝世老中医,欢迎来诊 个人qq:1978529954
  • 相关阅读:
    徒手画个disk不容易啊。。。
    fast powf
    SSE sqrt还是比C math库的sqrtf快了不少
    Mongoose也是个大坑
    A tiny program to benchmark image transpose algorithms
    On extracting ops from LLVM backend
    Into concurrent LRU caching once again
    性能大坑
    多项式在线拟合神器
    Spark 1.6.1源码编译
  • 原文地址:https://www.cnblogs.com/jueshilaozhongyi/p/11890320.html
Copyright © 2020-2023  润新知