import time import time formatted_date = time.strftime("%Y年%m月%d日", time.localtime()) from mitmproxy.tools.main import mitmdump import json,socket,os from mitmproxy import websocket from winproxy import ProxySetting import jsonpath from lxml import etree def get_script_path(script_name): # 保留定位用 scriptpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), script_name) print(os.path.join(os.path.dirname(os.path.abspath(__file__)), script_name)) return scriptpath def set_proxy(): # mac系统这段代码可以删除 ps = ProxySetting() """设置系统代理""" ps.enable = True ps.server = '127.0.0.1:8080' ps.registry_write() print('代理已经打开!') def send_socket(da): # 这个可以删除 a2 = json.dumps(da) a3 = a2.encode() s = socket.socket() s.connect(('localhost', 12354)) s.sendall(a3) s.close() def reqdata(url2): data = {} # print(url2) for i in url2.split('&'): key = i.split('=')[0] value = i.split('=')[1] data[key] = value # print(f'"{key}":"{value}",') return data def get_headers(headers): headers2 = {} for header, value in headers.items(): print(f"{header}: {value}") headers2[header] = value return headers2 def choose2_api(url): # 把你需要的url 放入列表 target_url = [ "https://scsearchapi.waimai.meituan.com/wxdrug/v2/search/inshop/products",#外卖_买药_inshop_products "https://hcapi-wx.waimai.meituan.com/wxapp/v1/poi/product/smooth/render",#外卖_买药_poi_render "https://hcapi-wx.waimai.meituan.com/wxapp/v1/poi/sputag/products",#外卖_买药_poi_products "https://hcapi-wx.waimai.meituan.com/wxapp/v1/poi/food",#外卖_买药_poi_food "https://wx-shangou.meituan.com/wxapp/v1/search/inshop/products",#外卖_便利店_inshop_products "https://wx-shangou.meituan.com/wxapp/v1/poi/product/smooth/render",#外卖_便利店_poi_render "https://wx-shangou.meituan.com/wxapp/v1/poi/sputag/products",#外卖_便利店_poi_products "https://wx-shangou.meituan.com/wxapp/v1/poi/food",#外卖_便利店_poi_food "https://hcapi-wx.waimai.meituan.com/mtweapp/v1/poi/product/smooth/render",#美团_买药_poi_render "https://hcapi-wx.waimai.meituan.com/mtweapp/v1/poi/sputag/products",#美团_买药_poi_products "https://hcapi-wx.waimai.meituan.com/mtweapp/v1/poi/food",#美团_买药_poi_food "https://wx-shangou.meituan.com/mtweapp/v1/poi/product/smooth/render",#美团_便利店poi_render "https://wx-shangou.meituan.com/mtweapp/v1/poi/sputag/products",#美团_便利店poi_products "https://wx-shangou.meituan.com/mtweapp/v1/poi/food",#美团_便利店poi_food "https://wx-shangou.meituan.com/mtweapp/v1/search/inshop/products",#美团_便利店_inshop_products "https://hcapi-wx.waimai.meituan.com/quickbuy/v1/poi/product/smooth/render",#闪购_买药poi_render "https://hcapi-wx.waimai.meituan.com/quickbuy/v1/poi/sputag/products",#闪购_买药poi_products "https://hcapi-wx.waimai.meituan.com/quickbuy/v1/poi/food",#闪购_买药poi_food "https://wx-shangou.meituan.com/quickbuy/v1/search/inshop/products",#闪购_便利店_inshop_products "https://wx-shangou.meituan.com/quickbuy/v1/poi/product/smooth/render",#闪购_便利店_poi_render "https://wx-shangou.meituan.com/quickbuy/v1/poi/sputag/products",#闪购_便利店_poi_products "https://wx-shangou.meituan.com/quickbuy/v1/poi/food",#闪购_便利店_poi_food 'https://wx.waimai.meituan.com/weapp/v1/poi/food',#美团美食food 'https://wx.waimai.meituan.com/weapp/v1/poi/info',#美团美食info 'https://wx.waimai.meituan.com/weapp/shop/v1/poi/productlist',#美团美食productlist 'https://wx.waimai.meituan.com/weapp/poi/food/render',#美团美食render 'https://apimobile.meituan.com/meishi/deal/v5/info/',#团购详情 'https://apimobile.meituan.com/group/v4/poi/search/miniprogram/',#美团全局搜索 'https://apimobile.meituan.com/group/v1/deal/search/suggest/',#美团全局suggest 'https://ihotel.meituan.com/productapi/v3/prepayGoodDetail?',#美团酒店1 'https://ihotel.meituan.com/productapi/v3/prepayList?',#美团酒店2- 'https://ihotel.meituan.com/coresearch/v2/hotel/list?',#美团酒店3-搜索列表 "https://m.ctrip.com/restapi/soa2/",#携程, 'https://waimai-guide.ele.me/h5/mtop.venus.shopcategoryservice.getcategoryv2/1.1/5.0/?',#elm便利店 'https://waimai-guide.ele.me/h5/mtop.venus.shopcategoryservice.getcategorydetail/1.1/5.0/?',#elm便利店 'https://waimai-guide.ele.me/h5/mtop.venus.shopresourceservice.getshopresource/1.4/5.0/?',#elm便利店 'https://waimai-guide.ele.me/h5/mtop.relationrecommend.elemetinyapprecommend.recommend/1.0/5.0/',#饿了么搜索 'https://waimai-guide.ele.me/h5/mtop.alsc.waimai.store.miniapp.store.detail.body.query.v2/1.0/5.0/',#饿了么美食菜单 'https://waimai-guide.ele.me/h5/mtop.alsc.waimai.store.miniapp.store.detail.head.query.v2/1.0/5.0/',#饿了么美食优惠 'https://alsc-buy2.ele.me/h5/mtop.eleme.waimai.carts.shop.operate.um/1.0/5.0/?',#饿了么美食地址 '.html?goods_id=',#拼多多 'https://mobile.yangkeduo.com/goods',#拼多多 'https://m.pinduoduo.net/goods.html?goods_id=',#拼多多 'https://mobile.pinduoduo.com/goods.html?goods_id=',#拼多多 'api/oak/integration/render?pdduid=',#拼多多 "integration/render?pdduid",#拼多多 'pinduoduo.com/api/oak/integration/render?pdduid=',#拼多多 'https://yiyao-h5.meituan.com/wedrug/v2/poi/qualification?',#美团买药营业执照 'https://hcapi-wx.waimai.meituan.com/wedrug/v1/poi/channelpage',#美团买药channel 'https://h5api.m.tmall.com/h5/mtop.taobao.pcdetail.data.get',#淘宝 'https://h5api.m.tmall.hk/h5/mtop.taobao.pcdetail.data.get/',#淘宝 "https://mobile.yangkeduo.com/order_checkout.html?",#拼多多下单 "https://i.waimai.meituan.com/openapi/v1/poi/food",#美团h5美食 "https://i.waimai.meituan.com/openh5/v2/poi/menuproducts",#美团h5美食product "https://i.waimai.meituan.com/openh5/v2/poi/smooth/render",#美团h5美食render "https://i.waimai.meituan.com/openh5/poi/info",#美团h5美食info图图 "pailitaorecommend.recommend", "https://m.dianping.com/ugcdetail/",#点评笔记 "https://www.dianping.com/shop/",#点评店铺 "https://m.dianping.com/mapi/dish/recommenddishlistpage.bin?", "https://hcapi-wx.waimai.meituan.com/storm/bridge/king-kong-page?", "https://waimai-guide.ele.me/h5/mtop.alsc.eleme.miniapp.collection.homepagev1", 'https://detail.tmall.com/item.htm?id', "https://www.dianping.com/ajax/json/shopDynamic/shopTabs?shopId=", 'https://online.ctrip.com/restapi/soa2/', "https://mall.bilibili.com/mall-merchant-c-search/shop/getShopQualify?", "https://mall.bilibili.com/mall-merchant-c-search/shop/getMerchantPublicity", "https://waimai-guide.ele.me/h5/mtop.alsc.wamai.store.detail.shop.certification",#饿了么美食营业执照 "https://cube.elemecdn.com/",#饿了么图片 "https://detail.1688.com/", 'https://h5api.m.1688.com/h5/mtop.1688.trade.service.mtoprateservice.querydsrratedatav2/1.0/', 'https://h5api.m.1688.com/h5/mtop.alibaba.alisite.cbu.server.moduleasyncservice/1.0/?', 'https://h5api.m.1688.com/h5/mtop.mbox.fc.common.gateway/2.0/', "https://i.waimai.meituan.com/openh5/poi/info?", "https://i.waimai.meituan.com/external/ajax/poi/", "hitch.ElevatorPublishPage.PageRefreshFunction", "/ws/amap/ride/hitch/car_list?", "https://hcapi-wx.waimai.meituan.com/storm/graphql/tile/query/from_app_drug_detail?", "food?", "https://wx-shangou.meituan.com/mtweapp/v8/poi/sputag/products?", "https://wx-shangou.meituan.com/mtweapp/v10/product/smooth/render?", "https://i.waimai.meituan.com/openh5/search/globalpage?", "https://h5.waimai.meituan.com/waimai/mindex/menu?" ] for b in target_url: if b in url: print('*********hit************') print(url) return True return False def save_file(datws): batch = '美食1' url2 = datws['url'].split('?')[0].split('/')[-1] days = (formatted_date) filepath = os.path.join('./',days,batch,url2) os.makedirs(filepath,exist_ok=True) with open(f'{filepath}/{time.time()}.json','w',encoding='utf-8') as f: f.write(json.dumps(datws,ensure_ascii=False)) print('保存完成') def response(flow): false = '' null = '' true = '' if flow.request.headers.get('Upgrade', '').lower() == 'websocket' and ('wpush.meituan.com/websocket/' in flow.request.url or 'https://sharkwsbj.sankuai.com/shark' in flow.request.url): # 拒绝websocket # 这里可以修改返回的状态码和原因,默认是403 Forbidden flow.response = flow.response flow.response.status_code = 403 flow.response.reason = 'Forbidden' # 结束流,不再转发请求 flow.kill(flow.response) # https://h5.waimai.meituan.com/waimai/mindex/menu?actualLat=23.129393&actualLng=113.299172&channel=default&dishId=&h5_detail_back=&initialLat=23.129393&initialLng=113.299172&mtOrderId=&mtShopId=-100&poi_id_str=OjRB8_v6Iv6GiFiOP2oGQAI&source=searchresult&utm_source= # https://h5.waimai.meituan.com/waimai/mindex/menu?actualLat=23.129392&actualLng=113.299171&channel=default&dishId=&h5_detail_back=&initialLat=23.129392&initialLng=113.299171&mtOrderId=&mtShopId=-100&poi_id_str=Mi23tmZr_q1P78DogZUrDwI&source=shoplist&utm_source= if 'https://h5.waimai.meituan.com/waimai/mindex/menu?' in flow.request.url or 'https://h5.waimai.meituan.com/' in flow.request.url or 'https://yiyao-h5.meituan.com/web/merchantQualification.html?' in flow.request.url: # js注入 print(flow.request.url) if flow.response.headers.get('Content-Type', '').startswith('text/html'): # 注入JavaScript代码 js_code = """ """ flow.response.content = flow.response.content.replace(b'', js_code.encode('utf-8') + b'') url = (flow.request.url) t2 = choose2_api(url) if t2: if flow.request.method == 'POST': headers = dict(flow.request.headers) params = dict(flow.request.query) data = flow.request.content if bool(data): data2 = data.decode('utf8') else: data2 = '' req2 = flow.response.content status_code = flow.response.status_code req = req2.decode('utf-8') out_data = {'headers': headers, 'url': flow.request.url, 'method': flow.request.method, 'params': params, 'data': data2, 'request': req,'status_code':status_code} # send_socket(out_data) save_file(out_data) def run(): mitmdump(["-s",__file__,"-q","-m","socks5@2080","-m","regular@8080"])#修改代码 if __name__ == '__main__': set_proxy() run()