美文网首页程序员
Xposed使用小结,android Wifi代理,权限申请模拟

Xposed使用小结,android Wifi代理,权限申请模拟

作者: 李阁 | 来源:发表于2017-12-26 17:36 被阅读0次

•1.Xposed安装器(5.0以上版本有一个,5.0以下有一个,两个不同的apk包)

•2.Root权限(手机需要root才可以使用Xposed)

能下载到

wifi代理

/** * Created by peter.li on 2017/12/13. */ 

public class HttpProxyUtil { 

private static HttpProxyUtil instance; public static HttpProxyUtil getInstance() {         if(instance == null){ instance = new HttpProxyUtil(); } return instance; } public static Object getField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ Field f = obj.getClass().getField(name); Object out = f.get(obj); return out; } public static Object getDeclaredField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); Object out = f.get(obj); return out; } public static void setEnumField(Object obj, String value, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class) f.getType(), value)); } public static void setProxySettings(String assign , WifiConfiguration wifiConf) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{ setEnumField(wifiConf, assign, "proxySettings"); } WifiConfiguration GetCurrentWifiConfiguration(WifiManager manager) { if (!manager.isWifiEnabled()) return null; List configurationList = manager.getConfiguredNetworks(); WifiConfiguration configuration = null; int cur = manager.getConnectionInfo().getNetworkId(); for (int i = 0; i < configurationList.size(); ++i) { WifiConfiguration wifiConfiguration = configurationList.get(i); if (wifiConfiguration.networkId == cur) configuration = wifiConfiguration; } return configuration; } public void setWifiProxySettings(Context context,String ip,int port) { //get the current wifi configuration WifiManager manager = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = GetCurrentWifiConfiguration(manager); if(null == config) return; try { //get the link properties from the wifi configuration Object linkProperties = getField(config, "linkProperties"); if(null == linkProperties) return; //get the setHttpProxy method for LinkProperties Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); Class[] setHttpProxyParams = new Class[1]; setHttpProxyParams[0] = proxyPropertiesClass; Class lpClass = Class.forName("android.net.LinkProperties"); Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); setHttpProxy.setAccessible(true); //get ProxyProperties constructor Class[] proxyPropertiesCtorParamTypes = new Class[3]; proxyPropertiesCtorParamTypes[0] = String.class; proxyPropertiesCtorParamTypes[1] = int.class; proxyPropertiesCtorParamTypes[2] = String.class; Constructor proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes); //create the parameters for the constructor Object[] proxyPropertiesCtorParams = new Object[3]; proxyPropertiesCtorParams[0] = ip; proxyPropertiesCtorParams[1] = port; proxyPropertiesCtorParams[2] = null; //create a new object using the params Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams); //pass the new object to setHttpProxy Object[] params = new Object[1]; params[0] = proxySettings; setHttpProxy.invoke(linkProperties, params); setProxySettings("STATIC", config); //save the settings manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); } catch(Exception e) { } } public void unsetWifiProxySettings(Context context) { WifiManager manager = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = GetCurrentWifiConfiguration(manager); if(null == config) return; try { //get the link properties from the wifi configuration Object linkProperties = getField(config, "linkProperties"); if(null == linkProperties) return; //get the setHttpProxy method for LinkProperties Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); Class[] setHttpProxyParams = new Class[1]; setHttpProxyParams[0] = proxyPropertiesClass; Class lpClass = Class.forName("android.net.LinkProperties"); Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); setHttpProxy.setAccessible(true); //pass null as the proxy Object[] params = new Object[1]; params[0] = null; setHttpProxy.invoke(linkProperties, params); setProxySettings("NONE", config); //save the config manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); } catch(Exception e) { } } }



/**

* Created by peter.li on 2017/12/13.

*/

public class HttpProxyUtilHigher {

private static HttpProxyUtilHigherinstance;

    public static HttpProxyUtilHighergetInstance() {

if(instance ==null){

instance =new HttpProxyUtilHigher();

        }

return instance;

    }

public static void setEnumField(Object obj, String value, String name)

throws SecurityException, NoSuchFieldException,IllegalArgumentException, IllegalAccessException{

Field f = obj.getClass().getField(name);

        f.set(obj, Enum.valueOf((Class) f.getType(), value));

    }

public static ObjectgetDeclaredFieldObject(Object obj, String name)

throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{

Field f = obj.getClass().getDeclaredField(name);

        f.setAccessible(true);

        Object out = f.get(obj); return out;

    }

public static void setDeclardFildObject(Object obj,String name,Object object){

Field f =null;

        try {

f = obj.getClass().getDeclaredField(name);

        }catch (NoSuchFieldException e) {

e.printStackTrace();

        }

f.setAccessible(true);

        try {

f.set(obj,object);

        }catch (IllegalAccessException e) {

e.printStackTrace();

        }

}

// 获取当前的Wifi连接

    public static WifiConfigurationgetCurrentWifiConfiguration(WifiManager wifiManager) {

if (!wifiManager.isWifiEnabled())

return null;

        List configurationList = wifiManager.getConfiguredNetworks();

        WifiConfiguration configuration =null;

        int cur = wifiManager.getConnectionInfo().getNetworkId();

        // Log.d("当前wifi连接信息",wifiManager.getConnectionInfo().toString());

        for (int i =0; i < configurationList.size(); ++i) {

WifiConfiguration wifiConfiguration = configurationList.get(i);

            if (wifiConfiguration.networkId == cur)

configuration = wifiConfiguration;

        }

return configuration;

    }

/**

    * 设置代理信息 exclList是添加不用代理的网址用的

    * */

    public void setHttpPorxySetting(Context context,String host, int port, List exclList)

throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,

            IllegalAccessException, NoSuchFieldException {

WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        WifiConfiguration config =getCurrentWifiConfiguration(wifiManager);

        ProxyInfo mInfo =null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

mInfo = ProxyInfo.buildDirectProxy(host,port);

        }

if (config !=null){

Class clazz = Class.forName("android.net.wifi.WifiConfiguration");

            Class parmars = Class.forName("android.net.ProxyInfo");

            Method method = clazz.getMethod("setHttpProxy",parmars);

//            Method methodGetHttpProxy = clazz.getMethod("getHttpProxy",parmars);

            method.invoke(config,mInfo);

            Object mIpConfiguration =getDeclaredFieldObject(config,"mIpConfiguration");

            setEnumField(mIpConfiguration, "STATIC", "proxySettings");

            setDeclardFildObject(config,"mIpConfiguration",mIpConfiguration);

            //save the settings

            wifiManager.updateNetwork(config);

            wifiManager.disconnect();

            wifiManager.reconnect();

            Log.i("MainAcitivity","修改之后");

        }

}

/**

    * 取消代理设置

    * */

    public void unSetHttpProxy(Context context)

throws ClassNotFoundException, InvocationTargetException, IllegalAccessException,

            NoSuchFieldException, NoSuchMethodException {

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        WifiConfiguration configuration =getCurrentWifiConfiguration(wifiManager);

        ProxyInfo mInfo =null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

mInfo = ProxyInfo.buildDirectProxy(null,0);

        }

if (configuration !=null){

Class clazz = Class.forName("android.net.wifi.WifiConfiguration");

            Class parmars = Class.forName("android.net.ProxyInfo");

            Method method = clazz.getMethod("setHttpProxy",parmars);

            method.invoke(configuration,mInfo);

            Object mIpConfiguration =getDeclaredFieldObject(configuration,"mIpConfiguration");

            setEnumField(mIpConfiguration, "NONE", "proxySettings");

            setDeclardFildObject(configuration,"mIpConfiguration",mIpConfiguration);

            //保存设置

            wifiManager.updateNetwork(configuration);

            wifiManager.disconnect();

            wifiManager.reconnect();

        }

}

}

权限模拟点击

if (loadPackageParam.packageName.equals("com.android.packageinstaller")) {

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){

//7.0

                    findAndHookMethod("com.android.packageinstaller.permission.ui.handheld.GrantPermissionsViewHandlerImpl",loadPackageParam.classLoader, "createView",new XC_MethodHook() {

@Override

                        protected void afterHookedMethod(MethodHookParam param)

throws Throwable {

super.afterHookedMethod(param);

                            ViewGroup viewGroup = (ViewGroup)param.getResult();

                            LinearLayout linearLayouteanrLayout = (LinearLayout)viewGroup.getChildAt(0);

                            LinearLayout linearLayouteanrLayout2 = (LinearLayout)linearLayouteanrLayout.getChildAt(1);

                            LinearLayout buttonBarLayout = (LinearLayout)linearLayouteanrLayout2.getChildAt(1);

                            final Button btnAllow = (Button)buttonBarLayout.getChildAt(3);

//                    Log.i("Xposed", "Holk测试程序:btnAllow="+btnAllow.getText().toString());

                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

@Override

                                public void run() {

Log.i("Xposed", "Holk测试程序:btnAllow111="+btnAllow.getText().toString());

                                    btnAllow.performClick();

                                }

},2000l);

                        }

});

                }else{

//6.0

                    findAndHookMethod("com.android.packageinstaller.permission.ui.GrantPermissionsDefaultViewHandler",loadPackageParam.classLoader, "createView",new XC_MethodHook() {

@Override

                        protected void afterHookedMethod(MethodHookParam param)

throws Throwable {

super.afterHookedMethod(param);

                            ViewGroup viewGroup = (ViewGroup)param.getResult();

                            LinearLayout linearLayouteanrLayout = (LinearLayout)viewGroup.getChildAt(0);

                            LinearLayout linearLayouteanrLayout2 = (LinearLayout)linearLayouteanrLayout.getChildAt(2);

//                            LinearLayout buttonBarLayout = (LinearLayout)linearLayouteanrLayout2.getChildAt(3);

                            final Button btnAllow = (Button)linearLayouteanrLayout2.getChildAt(3);

                    Log.i("Xposed", "Holk测试程序:btnAllow="+btnAllow.getText().toString());

                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

@Override

                                public void run() {

Log.i("Xposed", "Holk测试程序:btnAllow111="+btnAllow.getText().toString());

                                    btnAllow.performClick();

                                }

},2000l);

                        }

});

                }

}

}

相关文章

网友评论

    本文标题:Xposed使用小结,android Wifi代理,权限申请模拟

    本文链接:https://www.haomeiwen.com/subject/tfyjgxtx.html