def response(flow: http.HTTPFlow) -> None: with open(sys.argv[1], 'a') as f: try: data = flow.get_state() # 去掉不需要的信息 data.pop('client_conn', None) data.pop('server_conn', None) # 为提高传输效率,HTTP协议会存在gzip压缩传输内容的情况 # 由于HTTP协议特性,浏览器端无法在访问网页时知道服务端是否支持gzip压缩 # 所以大部分业务场景下都是响应(Response)内容使用gzip压缩,而请求(Request)内容是明文。 # 而对于结果gzip压缩的响应内容,在MITMProxy中可以通过`-z`选项打开即可解压缩响应内容 # 在部分业务场景下,请求的内容也非常多,所以为提升传输效率也会对请求内容进行gzip压缩 # 对于这类场景,MITMProxy目前是无法支持的,已提交ISSUE(https://github.com/mitmproxy/mitmproxy/issues/2782) data['request']['content'] = base64.b64encode( data['request']['content']) data['response']['content'] = base64.b64encode( data['response']['content']) # 转为JSON data = json.dumps(data, cls=DateEncoder) # 写文件以换行来区分每个请求 f.write(data + '\n') except UnicodeDecodeError as e: ctx.log.error(data) traceback.print_exc() ctx.log.error('Decode failed')
def response(flow: http.HTTPFlow) -> None: monitor_type = r.get("monitor_type").decode('utf-8') data = flow.get_state() if monitor_type == 'no': pass elif monitor_type == "all": #监听所有的url 存储 parser_data(flow.request, flow.response) elif monitor_type == 'single': # 监听指定url url = flow.request.url juge_result = judge_monitor_for_single(url) if juge_result: #juge_result = 匹配条件 parser_data(flow.request, flow.response) else: pass elif monitor_type == "content-type": '''根据文件类型判断''' juge_result = judge_monitor_for_type(flow.response) if juge_result: #juge_result = 匹配条件(文件类型) parser_data(flow.request, flow.response) else: pass else: pass
def get_flow_state(flow: HTTPFlow) -> dict: state = flow.get_state() for phase in ['request', 'response']: raw_attr = f'{phase}_raw' raw = getattr(flow, raw_attr, None) if raw: state[raw_attr] = raw.get_state() phase_obj = getattr(flow, phase, None) match_details = phase_obj and getattr(phase_obj, 'match_details', None) if match_details: state[phase]['match_details'] = deepcopy(match_details) return state
def response(flow: http.HTTPFlow) -> None: data = flow.get_state() print(data) if "findQuiz" in data['request']['path'].decode('utf-8'): # 获取题目信息 data_find = data['response']['content'].decode('utf-8') find_json = json.loads(data_find) quiz = find_json['data']['quiz'] options = find_json['data']['options'] school = find_json['data']['school'] _type = find_json['data']['type'] body = data['request']['content'].decode('utf-8') roomid = body.split("&")[0].split("=")[1] quizNum = body.split("&")[1].split("=")[1] # 存储题目信息 后面更新或者存储 upsql = "insert into questions(quiz, school, type, options, answer)\ VALUES('{quiz}', '{school}', '{_type}', '{options}', '{answer}')".format( quiz=quiz, school=school, _type=_type, options=str(options).replace("'", "\\'"), answer='') try: c.execute(upsql) conn.commit() print("题目存储成功") except pymysql.err.IntegrityError: pass except Exception as e: print(upsql) print("信息存储失败", e) r.set('roomid', roomid) r.set('quizNum', quizNum) r.set('quiz', quiz) r.set('options', options) c.execute(sql.format(quiz)) answer = c.fetchone()[0] #获取答案的index 用于自动答题 if answer: answerOK = 1 for op in options: if op == answer: break answerOK += 1 r.set("answerOK", answerOK) print("答案是:", answer) newresp = data['response']['content'].decode('utf-8').replace( answer, answer + "----") flow.response.content = newresp.encode('utf-8') print("请注意-----") else: r.set("answerOK", 1) answer = '' print("没有匹配到题目") if "choose" in data['request']['path'].decode('utf-8'): #---------------------------------------------------自动修改答案开关 request_body = data['request']['content'].decode('utf-8') city_option = re.search('option=', request_body).end() answerOK = r.get('answerOK').decode('utf-8') new_request_body = request_body[:city_option] + answerOK + request_body[ city_option + 1:] #修改请求数据 print("请求数据:", new_request_body) flow.request.content = new_request_body.encode('utf-8') #-----------------------------------------------------------------自动答题开关 data_choose = data['response']['content'].decode('utf-8') resp_choose_json = json.loads(data_choose) # 1234 option 序号1- 正确答案 answer = resp_choose_json['data']['answer'] roomid = r.get('roomid').decode('utf-8') quizNum = r.get('quizNum').decode('utf-8') print(roomid == data['request']['content'].decode('utf-8').split("&") [0].split("=")[1]) print(quizNum == data['request']['content'].decode('utf-8').split("&") [1].split("=")[1]) print(data['request']['content'].decode('utf-8').split("&")[1].split( "=")[1] == 1) if roomid == data['request']['content'].decode('utf-8').split( "&")[0].split("=")[1] and ( quizNum == data['request']['content'].decode('utf-8'). split("&")[1].split("=")[1] or data['request']['content']. decode('utf-8').split("&")[1].split("=")[1] == 1): # if data_choose_json['response']['data']['yes'] == 'false': # 答案错误 更新答案 #全部更新 quiz = r.get('quiz').decode('utf-8') options_str = r.get('options').decode('utf-8') print(options_str) options = eval(options_str) #列表 answer = options[answer - 1] print("正确答案:", answer) update_answer_sql = "update questions set answer='{answer}' where quiz='{quiz}'".format( quiz=quiz, answer=answer) print(update_answer_sql) try: c.execute(update_answer_sql) conn.commit() print("答案更新成功") except Exception as e: print("update_answer_sql ERROR:" + e) else: print(resp_choose_json) print(r.get('quiz').decode('utf-8')) print("原先的:", roomid, quizNum) print( "第二次:", data['request']['content'].decode('utf-8').split("&") [0].split("=")[1], data['request']['content'].decode( 'utf-8').split("&")[1].split("=")[1]) print("答案和题号不对", answer) print("本次答案对应的题目是", r.get('quiz').decode('utf-8'))