首页
社区
课程
招聘
通过frida控制Android微信浏览器拿到控制权上下文document
发表于: 6天前 1411

通过frida控制Android微信浏览器拿到控制权上下文document

6天前
1411

段落引用 代码前几个月的了,具体的逆向过程就不讲解了,直接上代码,代码已经注释了具体

主要hook点:
com.tencent.xweb.WebView.loadUrl(String url)
通过 this 拿到当前类实例,最后可以检测浏览器是否加载完毕,执行js代码等操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    hook微信浏览器,拿到webview
    使用frida反射java调用http请求
    最后通过执行js代码
*/
 
    function log(text) {
        console.log(text)
        //Java.send({"act":"Frida_Log","msg": text});
    }
    var analyToken = "4aab795025bbeee52bca2a8ee4123cb5"
    var mWebView = null;
    var analyGoodsInfo = {}
    var historyUrl;
 
   
 
    function httpGet(url){
        var URL = Java.use("java.net.URL");
        var HttpURLConnection = Java.use("java.net.HttpURLConnection");
        var BufferedReader = Java.use("java.io.BufferedReader");
        var InputStreamReader = Java.use("java.io.InputStreamReader");
 
        var urlObj = URL.$new(url);
        var connection = urlObj.openConnection();
        var inputStream = connection.getInputStream();
        var inputStreamReader = InputStreamReader.$new(inputStream);
        var bufferedReader = BufferedReader.$new(inputStreamReader);
 
        var response = "";
        var line;
        while ((line = bufferedReader.readLine()) !== null) {
        response += line;
        }   
        try{
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            connection.disconnect();
        }catch(e){
            console.log(e)
        }
         
        return response
    }
 
    function httpPost(url,data){
        var URL = Java.use("java.net.URL");
        var HttpURLConnection = Java.use("java.net.HttpURLConnection");
        var OutputStreamWriter = Java.use("java.io.OutputStreamWriter");
 
        var urlObj = URL.$new(url);
        var connection = urlObj.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        var wr = OutputStreamWriter.$new(connection.getOutputStream());
        wr.write(data);
        wr.flush();
 
        var BufferedReader = Java.use("java.io.BufferedReader");
        var InputStreamReader = Java.use("java.io.InputStreamReader");
        var inputStream = connection.getInputStream();
        var inputStreamReader = InputStreamReader.$new(inputStream);
        var bufferedReader = BufferedReader.$new(inputStreamReader);
 
        var response = "";
        var line;
        while ((line = bufferedReader.readLine()) !== null) {
        response += line;
        }
        console.log(response);
        
        try{
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            connection.disconnect();
        }catch(e){
            console.log(e)
        }
         
        return response
    }
 
    function toast(message) {
        try{
            console.log(message)
        }catch(e){
             
        }
        Java.perform(function() {
            var Looper = Java.use("android.os.Looper");
            // 检查当前线程是否已有Looper,如果没有,则调用prepare()
            if (Looper.myLooper() === null) {
                Looper.prepare();
            }
     
            var Toast = Java.use("android.widget.Toast");
            var context = Java.use("android.app.ActivityThread").currentApplication().getApplicationContext();
            var javaString = Java.use("java.lang.String").$new(message);
            Toast.makeText(context, javaString, Toast.LENGTH_SHORT.value).show();
     
            // 启动消息队列处理,仅在调用了prepare()后才调用loop()
            if (Looper.myLooper() === Looper.getMainLooper()) {
                Looper.loop();
            }
        });
    }
     
     
    function analys(){
        try{
            var sj = setInterval(function(){
                var analyGoodsId = analyGetGoods()
                if(analyGoodsId){
                    var rawGoodsId = historyUrl.split("goods_id=")[1].split("&")[0]
                    mWebView.loadUrl(historyUrl.replace(rawGoodsId,analyGoodsId))
                    clearInterval(sj)
                }else{
                    toast("暂无可解析商品,稍后重新获取")
                }
            },2000);
        }catch(e){
            toast("跳转商品出错,浏览器可能关闭")
            clearInterval(sj)
        }
        
    }
 
    function analyGetGoods(){
        var data = httpGet("http://localhost:8080/get_goods")
        try{
            var retJson = JSON.parse(data)
            if(retJson['code'] == 0){
                analyGoodsInfo = retJson;
                return retJson['goods_id'];
            }
        }catch(e){
            console.log("获取失败",e)
        }
        return null
    }
 
    Java.perform(function () {
        toast("开始Hook微信浏览器")
        let WebView = Java.use("com.tencent.xweb.WebView");
        WebView["loadUrl"].overload('j  ').implementation = function (str) {
            var myWebView = this;
            myWebView.loadUrl(str);
            mWebView = Java.retain(myWebView);
 
            //toast("等待页面加载完毕...")
 
            var sj = setInterval(function(){
                try{
                    var title = mWebView.getTitle();
                    var progress = mWebView.getProgress()
                    var url = mWebView.getUrl();
                    if(title == null && url == null){
                        //浏览器可以已经关闭,停止循环
                        mWebView = null;
                        clearInterval(sj);
                    }
                     
                     //等待页面加载完毕
                    if(progress == 100 && historyUrl != url){
                        console.log("加载完成",url)
                        historyUrl = url;
                        var goods_id = undefined;
                        //从url获取goods_id参数
                        try{
                            goods_id = url.split("goods_id=")[1].split("&")[0]
                        }catch(e){
                        }
                         
                        if(analyGoodsInfo && goods_id){
                            if(goods_id == analyGoodsInfo['goods_id']){
                                toast("读取商品数据并上报:"+goods_id)
                                
                                var script_str =   "(function() {" +
                                " window.analyGoodsInfo = '"+JSON.stringify(analyGoodsInfo)+"';"+
                                "   var script = document.createElement('script');"+
                                "   script.type = 'text/javascript';" +
                                "   script.src = 'http://localhost:8000/wechat_post.js?t='+ new Date().getTime();"
                                "   document.body.appendChild(script);" +
                                "})()"
 
                     
                                 
                                mWebView.evaluateJavascript(script_str, null);
 
                                //模拟上报完成,5秒后加载新商品
                                setTimeout(function(){
                                    analys()
                                },6000)
                            }else{
                                toast("拉取商品并跳转中...")
                                analys()
                            }
                            clearInterval(sj);
                        }else{
                            toast("当前非多多页面")
                            
                        }
                        //
                         
                         
                    }
 
                }catch(e){
                    console.log(e)
                }
            },200)
        };
    });
 
 
    setInterval(function() {
        //log("Frida Process is "+Process.id)
    }, 3 * 1000);

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 4
支持
分享
最新回复 (1)
雪    币: 761
活跃值: (656)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
学习了
1天前
0
游客
登录 | 注册 方可回帖
返回
//