Better internet access block?
Hi,
Is it possible to implement internet block that way so application will think there is no active connections at all? Currently, it looks like application can see internet connection but cannot connect due to block. Some application refuse to work this way, some give exceptions like this:
E/Cloudcell(13711): Throwable: Permission denied (missing INTERNET permission?)
E/Cloudcell(13711): java.lang.SecurityException: Permission denied (missing INTERNET permission?)
E/Cloudcell(13711): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
E/Cloudcell(13711): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/Cloudcell(13711): at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/Cloudcell(13711): at libcore.net.http.HttpConnection.
E/Cloudcell(13711): at libcore.net.http.HttpConnection.
E/Cloudcell(13711): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
E/Cloudcell(13711): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
E/Cloudcell(13711): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
E/Cloudcell(13711): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
E/Cloudcell(13711): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
E/Cloudcell(13711): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
E/Cloudcell(13711): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
E/Cloudcell(13711): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
E/Cloudcell(13711): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
E/Cloudcell(13711): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
E/Cloudcell(13711): at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
E/Cloudcell(13711): at com.firemonkeys.cloudcellapi.HttpThread.run(CC_HttpPost_Class.java:211)
E/Cloudcell(13711): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
E/Cloudcell(13711): at libcore.io.Posix.getaddrinfo(Native Method)
E/Cloudcell(13711): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
E/Cloudcell(13711): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
E/Cloudcell(13711): ... 16 more
E/Cloudcell(13711): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
E/Cloudcell(13711): ... 19 more
this is from Real Racing 3 v1.2.0
Youtube has similar behaviour: It crashes if you block internet access for it (but starts fine if there are really no active connections). Of course, it's useless to block Youtube, but i've just accidentally discovered it.
By the way, LBE Security Master can block internet for RR3 and app won't give exception. But actually, i want to move from LBE to XPrivacy.
It happened to me with other apps as well, it's because xprivacy blocks the internet access. If it could lure the app into believing there's no internet access eventhough there is then the app would work.
I will see what I can do.
This will be realized in the next release. Bonus: fake unmounted state for sdcard
Both RealRacing 3 and Youtube still crash upon starting if i apply internet restriction to them. Did you implement new way to restrict internet already or not yet?
RealRacing 3: E/AndroidRuntime( 9669): FATAL EXCEPTION: Thread-455
E/AndroidRuntime( 9669): java.lang.SecurityException: Permission denied (missing INTERNET permission?)
Is it possible to not remove INTERNET permission, but still intercept "inet" framework access and report disconnected/not connected status?
XPrivacy already reports internet as offline, but some applications ignore this.
But it looks like XPrivacy also revokes android.permission.INTERNET making application crash. Is it possible still grant android.permission.INTERNET, but intercept functions (like open socket, etc..) to return errors similar when there is no connections available?
Maybe, I will look into it.
How about intercept functions of java.net.InetAddress ? At least RR3 crashes here:
E/AndroidRuntime( 9669): java.lang.SecurityException: Permission denied (missing INTERNET permission?)
E/AndroidRuntime( 9669): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
E/AndroidRuntime( 9669): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/AndroidRuntime( 9669): at java.net.InetAddress.getAllByName(InetAddress.java:214)
I'm not sure is Youtube crash on the same function or not, but you can start from investigating Youtube crash since it's pre-installed already.
Sorry, to spam the topic. Just as an idea: Probably if you intercept DNS queries and return error while resolving address then most applications should give up to connect.
I'm ready to experiment by myself. I've download source code and it compiles fine. The only problem - i don't know how to trow exception to original application. InetAddress functions trigger exception if host name cannot be resolved. So, i need simulate this exception to original application. I cannot find info about it in XProsed forums/google.. :(
http://forum.xda-developers.com/showpost.php?p=43655000&postcount=2101 :)
@sorgelig if you want to chat (so I can help you), send me a XDA PM to exchange addresses. @Tungstwenty thanks for helping here.
Ok, it looks like working :)
- I've added hooks to 2 classes (see below). NetworkInterface is not used by these apps, but i think it should be hooked in case of usage by other applications. Probably, there are other functions exist for checking connections. Need to use and see. Actually, need to block functions for socket/port opening for completeness.
- I've removed "inet" permission from "Internet" group. I think, it should be moved to other group, because many applications with INTERNET permission will crash with this option marked. So, to simplify group marking it's better to not include "inet" into "Internet" group.
Now both RR3 and Youtube start fine and cannot see any connections! :) With RR3 while starting i see some short activity of connection, but i think it's called from native code and cannot be blocked.
@M66B, thanks for help offering. I will contact you at XDA if i will have further thoughts/questions about XPrivacy. My name is "sorg" on XDA if you would like to contact me. @Tungstwenty thanks for help with Throwables.
package biz.bokhorst.xprivacy;
import java.lang.reflect.Field;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import de.robv.android.xposed.XC_MethodHook.MethodHookParam;
public class XInetAddress extends XHook {
public XInetAddress(String methodName, String restrictionName, String[] permissions) {
super(methodName, restrictionName, permissions, null);
}
// static InetAddress[] getAllByName(String host)
// static InetAddress getByAddress(byte[] ipAddress)
// static InetAddress getByAddress(String hostName, byte[] ipAddress)
// static InetAddress getByName(String host)
// static InetAddress getLocalHost()
// libcore/luni/src/main/java/java/net/InetAddress.java
@Override
protected void before(MethodHookParam param) throws Throwable {
if (isRestricted(param)) {
String methodName = param.method.getName();
if (methodName.equals("getAllByName") ||
methodName.equals("getByAddress"))
{
param.setThrowable(new UnknownHostException("Unable to resolve host"));
}
else if (methodName.equals("getByName"))
{
if(param.args[0] != null)
{
String name = (String)param.args[0];
if(name.equals("localhost"))
{
InetAddress addr = getLoopback();
if(addr != null)
{
param.setResult(addr);
return;
}
}
}
param.setThrowable(new UnknownHostException("Unable to resolve host"));
}
else if (methodName.equals("getLocalHost"))
{
InetAddress addr = getLoopback();
if(addr != null)
{
param.setResult(addr);
return;
}
param.setThrowable(new UnknownHostException("Unable to resolve host"));
}
}
}
InetAddress getLoopback()
{
try
{
Field f = Inet4Address.class.getDeclaredField("LOOPBACK");
f.setAccessible(true);
return (InetAddress) f.get(Inet4Address.class);
}
catch (Exception e)
{
}
return null;
}
@Override
protected void after(MethodHookParam param) throws Throwable
{
}
}
package biz.bokhorst.xprivacy;
import de.robv.android.xposed.XC_MethodHook.MethodHookParam;
public class XNetworkInterface2 extends XHook {
public XNetworkInterface2(String methodName, String restrictionName, String[] permissions) {
super(methodName, restrictionName, permissions, null);
}
// static NetworkInterface getByInetAddress(InetAddress address)
// static NetworkInterface getByName(String interfaceName)
// static Enumeration<NetworkInterface> getNetworkInterfaces()
// libcore/luni/src/main/java/java/net/NetworkInterface.java
@Override
protected void before(MethodHookParam param) throws Throwable {
if (isRestricted(param)) {
String methodName = param.method.getName();
if (methodName.equals("getByInetAddress") ||
methodName.equals("getByName") ||
methodName.equals("getNetworkInterfaces"))
{
param.setResult(null);
}
}
}
@Override
protected void after(MethodHookParam param) throws Throwable
{
}
}
Thanks for your code contribution. Rest assured I will look into it, but I cannot do everything at the same time.
I've found more functions to block. ConnectivityManager, for example. I will provide you full patch later. You can concentrate on other issues for now :)
Why do you replace localhost by loopback? (it requires getting a private field, which might not always be available)
Why do you hook getByInetAddress, getByName and getNetworkInterfaces?
If this is going to work okay, inet can be marked dangerous and exclude by default, like this: https://github.com/M66B/XPrivacy/blob/master/src/biz/bokhorst/xprivacy/PrivacyManager.java#L348 https://github.com/M66B/XPrivacy/blob/master/src/biz/bokhorst/xprivacy/PrivacyManager.java#L560
InetAddress has no loopback and loopback is the same as localhost:
public static final InetAddress LOOPBACK = new Inet4Address(new byte[] { 127, 0, 0, 1 }, "localhost");
public static final InetAddress LOOPBACK = new Inet6Address(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, "localhost", 0);