zoukankan      html  css  js  c++  java
  • mitmproxy 抓包工具(1)

    官网:https://docs.mitmproxy.org/stable/addons-examples/

    github: https://github.com/mitmproxy/mitmproxy/

    mitmproxy 基本使用:

    EV:

    ubuntu16.04

    python3.7

    install :

    pip install mitmproxy
    
    #check version
    mitmproxy --version
    可能会报错,貌似是因为在安装mitmproxy的时候,会涉及到找一些软件的安装目录,但如果找不到就会报错。
    
    所以输入下面的指令就能成功:
    
    sudo pip3 install mitmproxy --ignore-installed
    .还有就是mitmproxy在进行中间人攻击的时候,在手机安装mitmproxy证书的时候,要经过mitmproxy代理才能下载到手机本地。还有就是如果遇到一些代理https失败的情况,
    在启动指令mitmproxy -p 8080 后面加上 --ssl-insecure 会解决一些问题。

    默认监听 8080端口,使用 -p 指定端口

    mitmdump -p   8888

    下载证书: localhost://mitm.it    安装即可

     使用mitmproxy中间人代理,爬取数据,入库

    请求拦截函数名:

    def request(flow):
        pass

    响应拦截:

    def responset(flow):
        // flow 表示数据流
        pass

    实例代码

    testscript.py:

    import mitmproxy.http
    import redis
    import re
    import json
    import time
    
    from kafka import KafkaProducer
    
    class Counter:
        def __init__(self):
            self.conn_redis = redis.Redis(host='192.168.18.199', port=6379, password='ACahlofh', db=10)
            self.producer = KafkaProducer(bootstrap_servers=['192.168.18.129:9092'])
    
        def request(self, flow: mitmproxy.http.HTTPFlow):
            flow_url = flow.request.url
            if '/api/v2/item/get?' in flow_url:
                headers = flow.request.headers
                itemid = re.search(r"itemid=(d+)", flow_url, re.M|re.I).group(1)
                shopid = re.search(r"shopid=(d+)", flow_url, re.M|re.I).group(1)
                headers_dict = {}
                for (k, v) in headers.items():
                    # headers_dict[h[0].decode("utf-8")] = h[1].decode("utf-8")
                    headers_dict[k] = v
                self.conn_redis.set("header"+shopid+itemid, json.dumps(headers_dict).encode("utf-8"), 60)
    
    
        def response(self, flow : mitmproxy.http.HTTPFlow):
            flow_url = flow.request.url
            if '/api/v2/item/get?' in flow_url:
                data = flow.response.text
                # data=json.loads(data)
                # parse_data = page_query_items_parse(data)
                data_json = json.dumps(self.page_query_items_parse(data)).encode("utf-8")
                self.producer.send(topic="new_monitor", value=data_json)
                # save to redis tmp
                itemid = re.search(r"itemid=(d+)", flow_url, re.M|re.I).group(1)
                shopid = re.search(r"shopid=(d+)", flow_url, re.M|re.I).group(1)
                self.conn_redis.set(shopid+itemid, data_json, 60)
    
    
        def page_query_items_parse(self, response):
            pass
            result_dict = json.loads(response)
            item_dict = result_dict["item"]
            # item_dict["price_max_before_discount"]
            # item_dict["ctime"]
            item = {}
            item["estimated_days"] = item_dict["estimated_days"]
            item["shop_id"] = item_dict["shopid"]
            item["item_id"] = item_dict["itemid"]
            item["name"] = item_dict["name"]
            item["historical_sold"] = item_dict["historical_sold"]
            item["sold"] = item_dict["sold"]
            item["view_count"] = item_dict["view_count"]
            item["liked_count"] = item_dict["liked_count"]
            item["image"] = item_dict["image"]
            item["clt_date"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            model_sold = 0
            models = item_dict["models"]
            for model in models:
                model_sold += model["sold"]
            item["model_sold"] = model_sold
            return item
    
    addons = [
        Counter()
    ]

    指定脚本运行:

    使用命令  mitmdump -p 8888 -s testscript.py  回车运行

    pip install  kafka-python 

    from kafka import KafkaProducer
    def response(flow):
        if 'xxxxxx/api/v2/item/get' in flow.request.url:
            data = flow.response.text
            print(data)
            producer = KafkaProducer(bootstrap_servers=['192.168.18.129:9092'])
            producer.send(topic="mitmproxy", value=data.encode('utf-8'))
            producer.close()
          
            # data=json.loads(flow.response.text)
            f = open("C:/Users/DELL/Desktop/Python/hhha.text", 'w')
            f.write(flow.response.text)
            f.close()

    推荐使用已下第二种方式

    脚本 详见:https://www.cnblogs.com/grandlulu/p/9525417.html

    完成了上述工作,我们已经具备了操作 mitmproxy 的基本能力 了。接下来开始开发自定义脚本,这才是 mitmproxy 真正强大的地方。

    脚本的编写需要遵循 mitmproxy 规定的套路,这样的套路有两个。

    第一个是,编写一个 py 文件供 mitmproxy 加载,文件中定义了若干函数,这些函数实现了某些 mitmproxy 提供的事件,mitmproxy 会在某个事件发生时调用对应的函数,形如:

    复制代码
    import mitmproxy.http
    from mitmproxy import ctx
    
    num = 0
    
    
    def request(flow: mitmproxy.http.HTTPFlow):
        global num
        num = num + 1
        ctx.log.info("We've seen %d flows" % num)
    复制代码

    第二个是,编写一个 py 文件供 mitmproxy 加载,文件定义了变量 addons,addons 是个数组,每个元素是一个类实例,这些类有若干方法,这些方法实现了某些 mitmproxy 提供的事件,mitmproxy 会在某个事件发生时调用对应的方法。这些类,称为一个个 addon,比如一个叫 Counter 的 addon:

     

    复制代码
    import mitmproxy.http
    from mitmproxy import ctx
    
    
    class Counter:
        def __init__(self):
            self.num = 0
    
        def request(self, flow: mitmproxy.http.HTTPFlow):
            self.num = self.num + 1
            ctx.log.info("We've seen %d flows" % self.num)
    
    
    addons = [
        Counter()
    ]
    复制代码

    这里强烈建议使用第二种套路,直觉上就会感觉第二种套路更为先进,使用会更方便也更容易管理和拓展。况且这也是官方内置的一些 addon 的实现方式。

    我们将上面第二种套路的示例代码存为 addons.py,再重新启动 mitmproxy:

    mitmweb -p 8888 -s addons.py 

    ko!    推荐这哥们写的一篇,比较详细:https://www.cnblogs.com/lbzbky/articles/14305383.html

    参考:https://www.cnblogs.com/angle6-liu/p/10814890.html

              https://blog.csdn.net/u011422450/article/details/105834021

  • 相关阅读:
    SQList基础+ListView基本使用
    Git本地上传口令
    记住用户名和登录密码+虚拟机没有root权限解决办法
    API+gir上传错误解决办法
    界面跳转+信息传递+AS中如何将ADV转移到其他盘中
    界面跳转
    Android学习——ListView
    开课第一周周总结
    体温上报APP——班级统计
    体温上报APP——打印
  • 原文地址:https://www.cnblogs.com/lshan/p/15026748.html
Copyright © 2011-2022 走看看