Skip to content

Commit

Permalink
Better method for hooking the package manager, hook three more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Jan 15, 2014
1 parent a65baa9 commit a27bc10
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Test and beta releases will have experimental functions enabled by default.

**Next release**

* Better method for hooking the bluetooth manager
* Better method for hooking the package manager
* Hook *getPackagesForUid*, *getPackagesHoldingPermissions* (JellyBean MR2) and *queryIntentContentProviders* (Kitkat)
* Updated Slovak translation

[Open issues](https://github.com/M66B/XPrivacy/issues?state=open)
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For interested developers:
* Close app view when packages removed
* Hook location intents
* More efficient implementation of XSystemProperties
* Hook more package manager methods (which return info about one package)

* Split settings file?
* Remove indeterminate progress indicator?
21 changes: 13 additions & 8 deletions assets/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,24 @@
<Hook restriction="storage" method="sdcard" dangerous="true" restart="true" permissions="READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE" sdk="10" />
<Hook restriction="storage" method="getExternalStorageState" sdk="10" />

<Hook restriction="system" method="getInstalledApplications" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="getInstalledPackages" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="getPackagesForUid" dangerous="true" from="1.11.14" permissions="" sdk="1" />
<Hook restriction="system" method="getPackagesHoldingPermissions" dangerous="true" from="1.11.14" permissions="" sdk="1" />
<Hook restriction="system" method="getPreferredPackages" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="queryBroadcastReceivers" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="queryContentProviders" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="queryIntentActivities" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="queryIntentActivityOptions" dangerous="true" permissions="" sdk="1" />
<Hook restriction="system" method="queryIntentContentProviders" dangerous="true" from="1.11.14" permissions="" sdk="19" />
<Hook restriction="system" method="queryIntentServices" dangerous="true" permissions="" sdk="1" />

<Hook restriction="system" method="getRecentTasks" dangerous="true" permissions="GET_TASKS" sdk="10" />
<Hook restriction="system" method="getRunningAppProcesses" dangerous="true" permissions="GET_TASKS" sdk="10" />
<Hook restriction="system" method="getRunningServices" dangerous="true" permissions="GET_TASKS" sdk="10" />
<Hook restriction="system" method="getRunningTasks" dangerous="true" permissions="GET_TASKS" sdk="10" />
<Hook restriction="system" method="getInstalledProviders" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="getInstalledApplications" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="getInstalledPackages" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="getPreferredPackages" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="queryBroadcastReceivers" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="queryContentProviders" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="queryIntentActivities" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="queryIntentActivityOptions" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="queryIntentServices" dangerous="true" permissions="" sdk="10" />

<Hook restriction="system" method="android.intent.action.PACKAGE_ADDED" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="android.intent.action.PACKAGE_REPLACED" dangerous="true" permissions="" sdk="10" />
<Hook restriction="system" method="android.intent.action.PACKAGE_RESTARTED" dangerous="true" permissions="" sdk="10" />
Expand Down
7 changes: 5 additions & 2 deletions src/biz/bokhorst/xprivacy/XActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ protected void before(MethodHookParam param) throws Throwable {
@Override
protected void after(MethodHookParam param) throws Throwable {
if (mMethod == Methods.getSystemService) {
// TODO: check if this hook is needed
if (param.args.length > 0 && param.args[0] != null) {
String name = (String) param.args[0];
Object instance = param.getResult();
if (instance != null)
XPrivacy.handleGetSystemService(this, (String) param.args[0], instance);
if (name != null && instance != null)
XPrivacy.handleGetSystemService(this, name, instance);
}
} else if (mMethod == Methods.onDestroy || mMethod == Methods.onPause) {
// TODO: remove after implementing privacy service
try {
PrivacyManager.sendUsageData(this, (Context) param.thisObject);
} catch (Throwable ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/XActivityThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected void before(MethodHookParam param) throws Throwable {
else
packageNames = new String[] { intent.getData().getEncodedSchemeSpecificPart() };
for (String packageName : packageNames)
if (!XApplicationPackageManager.isPackageAllowed(packageName)) {
if (!XPackageManager.isPackageAllowed(packageName)) {
finish(param);
param.setResult(null);
break;
Expand Down
1 change: 1 addition & 0 deletions src/biz/bokhorst/xprivacy/XApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void setDefaultHandler(Thread.UncaughtExceptionHandler handler) {

@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO: remove after implementing privacy service
try {
Util.log(mHook, Log.WARN, "Uncaught exception uid=" + Process.myUid() + ": " + ex);
PrivacyManager.sendUsageData(null, mContext);
Expand Down
1 change: 1 addition & 0 deletions src/biz/bokhorst/xprivacy/XBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class XBinder extends XHook {
"android.location.ILocationManager",
// TODO: sensor
"com.android.internal.telephony.ITelephonyRegistry",
"android.content.pm.IPackageManager",
"com.android.internal.telephony.IPhoneSubInfo",
"android.view.IWindowManager",
"android.net.wifi.IWifiManager"
Expand Down
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/XContentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ else if (sUri.startsWith("content://com.android.contacts/raw_contacts"))
while (cursor.moveToNext()) {
int colPackage = cursor.getColumnIndex("package");
String packageName = (colPackage < 0 ? null : cursor.getString(colPackage));
if (packageName != null && XApplicationPackageManager.isPackageAllowed(packageName))
if (packageName != null && XPackageManager.isPackageAllowed(packageName))
copyColumns(cursor, result);
}
result.respond(cursor.getExtras());
Expand Down
14 changes: 10 additions & 4 deletions src/biz/bokhorst/xprivacy/XContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public String getClassName() {
// frameworks/base/core/java/android/app/ContextImpl.java

private enum Methods {
getSystemService
getPackageManager, getSystemService
};

public static List<XHook> getInstances() {
List<XHook> listHook = new ArrayList<XHook>();
listHook.add(new XContextImpl(Methods.getPackageManager, null));
listHook.add(new XContextImpl(Methods.getSystemService, null));
return listHook;
}
Expand All @@ -40,11 +41,16 @@ protected void before(MethodHookParam param) throws Throwable {
@Override
protected void after(MethodHookParam param) throws Throwable {
// Do nothing
if (mMethod == Methods.getSystemService) {
if (mMethod == Methods.getPackageManager) {
Object instance = param.getResult();
if (instance != null)
XPrivacy.handleGetSystemService(this, "PackageManager", instance);
} else if (mMethod == Methods.getSystemService) {
if (param.args.length > 0 && param.args[0] != null) {
String name = (String) param.args[0];
Object instance = param.getResult();
if (instance != null)
XPrivacy.handleGetSystemService(this, (String) param.args[0], instance);
if (name != null && instance != null)
XPrivacy.handleGetSystemService(this, name, instance);
}
} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;

import android.os.Binder;
import android.os.Build;
import android.util.Log;
import android.content.Context;
import android.content.pm.ApplicationInfo;
Expand All @@ -16,40 +17,53 @@

import de.robv.android.xposed.XC_MethodHook.MethodHookParam;

public class XApplicationPackageManager extends XHook {
public class XPackageManager extends XHook {
private Methods mMethod;
private String mClassName;

private XApplicationPackageManager(Methods method, String restrictionName) {
private XPackageManager(Methods method, String restrictionName, String className) {
super(restrictionName, method.name(), null);
mMethod = method;
mClassName = className;
}

public String getClassName() {
return "android.app.ApplicationPackageManager";
return mClassName;
}

// @formatter:off

// public List<ApplicationInfo> getInstalledApplications(int flags)
// public List<PackageInfo> getInstalledPackages(int flags)
// public String[] getPackagesForUid(int uid)
// public List<PackageInfo> getPackagesHoldingPermissions(String[] permissions, int flags)
// public List<PackageInfo> getPreferredPackages(int flags)
// public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags)
// public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags)
// public List<ResolveInfo> queryIntentActivities(Intent intent, int flags)
// public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, Intent[] specifics, Intent intent, int flags)
// public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags)
// public List<ResolveInfo> queryIntentServices(Intent intent, int flags)
// frameworks/base/core/java/android/app/ApplicationPackageManager.java

// @formatter:on

private enum Methods {
getInstalledApplications, getInstalledPackages, getPreferredPackages, queryBroadcastReceivers, queryContentProviders, queryIntentActivities, queryIntentActivityOptions, queryIntentServices
getInstalledApplications, getInstalledPackages, getPackagesForUid, getPackagesHoldingPermissions, getPreferredPackages, queryBroadcastReceivers, queryContentProviders, queryIntentActivities, queryIntentActivityOptions, queryIntentContentProviders, queryIntentServices
};

public static List<XHook> getInstances() {
public static List<XHook> getInstances(Object instance) {
String className = instance.getClass().getName();
List<XHook> listHook = new ArrayList<XHook>();
for (Methods am : Methods.values())
listHook.add(new XApplicationPackageManager(am, PrivacyManager.cSystem));
if (am == Methods.getPackagesHoldingPermissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
listHook.add(new XPackageManager(am, PrivacyManager.cSystem, className));
} else if (am == Methods.queryIntentContentProviders) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
listHook.add(new XPackageManager(am, PrivacyManager.cSystem, className));
} else
listHook.add(new XPackageManager(am, PrivacyManager.cSystem, className));
return listHook;
}

Expand All @@ -64,11 +78,16 @@ protected void after(MethodHookParam param) throws Throwable {
if (mMethod == Methods.getInstalledApplications) {
if (param.getResult() != null && isRestricted(param))
param.setResult(filterApplicationInfo((List<ApplicationInfo>) param.getResult()));
} else if (mMethod == Methods.getInstalledPackages || mMethod == Methods.getPreferredPackages) {
} else if (mMethod == Methods.getPackagesForUid) {
if (param.getResult() != null && isRestricted(param))
param.setResult(new String[0]);
} else if (mMethod == Methods.getInstalledPackages || mMethod == Methods.getPackagesHoldingPermissions
|| mMethod == Methods.getPreferredPackages) {
if (param.getResult() != null && isRestricted(param))
param.setResult(filterPackageInfo((List<PackageInfo>) param.getResult()));
} else if (mMethod == Methods.queryBroadcastReceivers || mMethod == Methods.queryIntentActivities
|| mMethod == Methods.queryIntentActivityOptions || mMethod == Methods.queryIntentServices) {
|| mMethod == Methods.queryIntentActivityOptions || mMethod == Methods.queryIntentContentProviders
|| mMethod == Methods.queryIntentServices) {
if (param.getResult() != null && isRestricted(param))
param.setResult(filterResolveInfo((List<ResolveInfo>) param.getResult()));
} else if (mMethod == Methods.queryContentProviders) {
Expand Down
10 changes: 7 additions & 3 deletions src/biz/bokhorst/xprivacy/XPrivacy.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
// Application
hookAll(XApplication.getInstances());

// Application package manager
hookAll(XApplicationPackageManager.getInstances());

// Audio record
hookAll(XAudioRecord.getInstances());

Expand Down Expand Up @@ -184,6 +181,7 @@ public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
private static boolean mClipboardManagerHooked = false;
private static boolean mConnectivityManagerHooked = false;
private static boolean mLocationManagerHooked = false;
private static boolean mPackageManagerHooked = false;
private static boolean mSensorManagerHooked = false;
private static boolean mTelephonyManagerHooked = false;
private static boolean mWindowManagerHooked = false;
Expand Down Expand Up @@ -223,6 +221,12 @@ public static void handleGetSystemService(XHook hook, String name, Object instan
hookAll(XLocationManager.getInstances(instance));
mLocationManagerHooked = true;
}
} else if (name.equals("PackageManager")) {
// Package manager
if (!mPackageManagerHooked) {
hookAll(XPackageManager.getInstances(instance));
mPackageManagerHooked = true;
}
} else if (name.equals(Context.SENSOR_SERVICE)) {
// Sensor manager
if (!mSensorManagerHooked) {
Expand Down
1 change: 1 addition & 0 deletions src/biz/bokhorst/xprivacy/XService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected void before(MethodHookParam param) throws Throwable {
@Override
protected void after(MethodHookParam param) throws Throwable {
if (mMethod == Methods.onDestroy) {
// TODO: remove after implementing privacy service
Util.log(this, Log.INFO, "Service destroyed uid=" + Process.myUid());
try {
PrivacyManager.sendUsageData(this, (Context) param.thisObject);
Expand Down

0 comments on commit a27bc10

Please sign in to comment.