雪球抓包与app实战

课后作业

  • 使用mitmproxy,贴出来脚本内容和效果截图
    • 对第一个股票保持原样
    • 对第二个股票名字加长一倍
    • 对第三个股票名字变成空

AD老师,作业请查收
before map_local after map_local

老师作业请查收

rewrite

import json
from mitmproxy import ctx, http

class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow: http.HTTPFlow):
        self.num = self.num + 1
        ctx.log.info("We've seen %d flows" % self.num)

    def response(self, flow: http.HTTPFlow):
        # 通过url确定对应的接口信息
        if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
            # 修改原始数据
            # 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换
            data = json.loads(flow.response.text)
            data['data']['items'][1]['quote']['name'] = '中国平安'*2
            data['data']['items'][2]['quote']['name'] = ' '
            # 赋值给响应信息
            flow.response.text = json.dumps(data)

addons = [
    Counter()
]

map_local

import json
from mitmproxy import ctx, http

class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow: http.HTTPFlow):
        self.num = self.num + 1
        ctx.log.info("We've seen %d flows" % self.num)

    def response(self, flow: http.HTTPFlow):
        # 通过url确定对应的接口信息
        if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
            # 加载本地的json文件
            with open('2.json', encoding='utf-8') as f:
                data = json.load(f)
            # 赋值给响应信息
            flow.response.text = json.dumps(data)

addons = [
    Counter()
]

作业提交

class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow: http.HTTPFlow):
        self.num = self.num + 1
        ctx.log.info("We've seen %d flows" % self.num)
        # 判断是否是想要的请求信息,通过url进行判断

    def response(self, flow: http.HTTPFlow):
        name_url = "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t="
        if name_url in flow.request.pretty_url:
            print("---------begin---------")
            # 修改原始数据
            data = json.loads(flow.response.text)
            data["data"]["items"][1]["quote"]["name"] = "阿里巴巴" * 2
            data["data"]["items"][2]["quote"]["name"] = ""
            # 赋值给响应信息
            flow.response.text = json.dumps(data)
        else:
            print("no exists!!!!!")


addons = [
    Counter()
]

image

老师,请查收作业,https://github.com/tangyihuai/HGWZLG6


老师,请查收作业 https://github.com/fatfatfatFatTiger/HogwartsLG6

修改之前

修改之后

【rewrite】

【map_local】

【recursion】

作业如下:
效果图如下:


脚本内容如下:

map_local 实现:
image

rewrite实现:
mock_rewrite

算法实现:
mock_algorithm

image

image

image
image

https://github.com/Th0mas1ee/Hogwarts.git

修改前:
股票原始图

修改后:
rewrite方法修改

代码:

代码:


更改前:
image
更改后:
image

修改前:
图片
修改后:
图片

rewrite

import json
import sys
from mitmproxy import http
from mitmproxy import ctx
from mitmproxy.tools.main import mitmdump

class Counter:
def init(self):
self.num = 0

def request(self, flow: http.HTTPFlow):
    self.num = self.num + 1
    ctx.log.info("We've seen %d flows" % self.num)
    # 判断是否是想要的请求信息,通过url进行判断

def response(self, flow):
    if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" \
            in flow.request.pretty_url:
        # 修改原始数据
        # 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换
        data = json.loads(flow.response.text)
        old_data = data["data"]["items"][1]["quote"]["name"]
        data["data"]["items"][1]["quote"]["name"] = old_data * 2
        data["data"]["items"][2]["quote"]["name"] = ""
        flow.response.text = json.dumps(data)
    # 赋值给响应信息

addons = [
Counter()
]

if name == ‘main’:
sys.argv = [file, “-s”, file]
#
# 官方要求必须主线程
mitmdump()

map_local

import json
import sys
from mitmproxy.tools.main import mitmdump

class Counter:
def init(self):
self.num = 0

def response(self, flow):
    if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" \
            in flow.request.pretty_url:
    # 打开文件,读取文件数据,作为响应,给返回
        with open("new_data.json", encoding="UTF-8") as f:
            data = json.load(f)
        flow.response.text = json.dumps(data)

addons = [
Counter()
]

if name == ‘main’:

sys.argv = [__file__, "-s", __file__]
#
# 官方要求必须主线程
mitmdump()

算法

import json
import sys
from mitmproxy import http
from mitmproxy.tools.main import mitmdump

class Counter:
def init(self):
self.num = 0
# 定义股票编码
self.first_symbol = “RLX”
self.second_symbol = “SZ000100”

def response(self, flow: http.HTTPFlow):
    # 便于拿到含股票名称的接口响应
    if "/v5/stock/batch/quote.json?_t" \
            in flow.request.pretty_url \
            and ((self.first_symbol in flow.request.pretty_url) \
                 or (self.second_symbol in flow.request.pretty_url)):
        # 打开文件,读取文件数据,作为响应,给返回
        data = json.loads(flow.response.text)
        self.json_travel(data)
        flow.response.text = json.dumps(self.json_travel(data))

def json_travel(self, data):
    # 判断传入的数据类型{"a": {"b":{"c":1}}}
    if isinstance(data, dict):
        
        for key, value in data.items():
            # 响应结果替换,将第二支股票名称加倍,第三支股票名称去掉
            if  key == "items":
                old_data = data["items"][1]["quote"]["name"]
                new_data = old_data * 2
                data["items"][1]["quote"]["name"] = new_data
                data["items"][2]["quote"]["name"] = ""
            else:
                # 遍历字典的数据
                # 当字典格式,递归value值
                data[key] = self.json_travel(value)

    elif isinstance(data, list):
        # 当数据类型 为 list 的时候, 添加到结构内,并继续递归遍历,
        # 知道数据类型不为可迭代对象时
        data = [self.json_travel(value) for value in data]
    elif isinstance(data, bool):
        data = data
    elif isinstance(data, int) or isinstance(data, float):
        data = data
    elif isinstance(data, str):
        data = data
    else:
        data = data
    return data

addons = [
Counter()
]

if name == ‘main’:
sys.argv = [file, “-s”, file]
mitmdump()

修改前
image
修改后
image



image

class Counter(object):
“”"
request, response 皆为固定写法,对应每一次请求与响应
“”"
def init(self):
self.num = 0

def request(self, flow: http.HTTPFlow):
    self.num = self.num + 1
    ctx.log.info("We've seen %d flows" % self.num)
    # 判断是否是想要的请求信息,通过url进行判断

def response(self, flow):
    # 抓包确定需要修改的信息在哪个接口中,并判断是否在pretty_url中
    if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
    # 修改原始数据
    # 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换
        data = json.loads(flow.response.text)
    # 依次修改123只股票的名称
        data["data"]["items"][0]["quote"]["name"] = data["data"]["items"][0]["quote"]["name"]
        data["data"]["items"][1]["quote"]["name"] = data["data"]["items"][1]["quote"]["name"]*2
        data["data"]["items"][2]["quote"]["name"] = ""
    # 赋值给响应信息
        flow.response.text = json.dumps(data)

addons:固定写法,加入Counter插件到列表中

addons = [
Counter()
]
QQ截图20210327213356

修改前
image
修改后
image
命令
mitmdump -p 8887 -s demo2.py
代码

作业请查收:
修改前
image
修改后
image
image

1