Android学习笔记之一谁动了我的接口附checkPermission流程(Dean)[转]

前段时间接到一个有趣的需求!我们实现的某某功能的手机对外提供了API!结果有希望只有自己承认的厂商的APK才能使用这些接口!其他的过滤掉! 

哈哈!看上去挺变态的!对外公布的API又不让人家用!没办法啊!需求就是爷!一个字做! 
回顾android安全机制貌似没有这么一项功能!不过咱能做!谁叫咱能改源码呢!有源码什么不能改了啊!哈哈!。。。。 

那么方案调研过程我们省略这里面涉及的知识比较多!直接上方案了! 

步骤是先想法知道谁调用了你的接口!然后把调用接口的apk的厂商信息找出来! 
同一个厂商的apk的签名应该是一样的!拥有同样的signature以及pubkeyid!所以最终目的是得到signature! 
研究查到有如下接口 

Java代码




  1. PackageInfo pkgInfo;  
  2.         try {  
  3.             pkgInfo = getPackageManager().getPackageInfo("packagename", PackageManager.GET_SIGNATURES);  
  4.   
  5.             Signature enterproidSign = pkgInfo.signatures[0];  
  6.               
  7.             if (!enterproidSign.toCharsString().equals(key)) {  
  8.                 // Wrong signature  
  9.                 return;  
  10.             }  
  11.         } catch (NameNotFoundException e) {  
  12.             // TODO Auto-generated catch block  
  13.             e.printStackTrace();  
复制代码

也就是说当务之急就是找到谁调用了这个接口!即是packagename! 
本来我们知道想知道谁调用了你只要在参数中加入context的信息就很容易得到apk的唯一标识包名了! 
如下面的code 

Java代码  




  1. String packageName = this.getApplicationInfo().packageName;  
  2. Log.e("Dean", "getApplicationInfo:"+packageName);  
  3. String dataDir = this.getApplicationInfo().dataDir;  
  4. Log.e("Dean", "getApplicationInfo:dataDir"+dataDir);  
  5. String sourceDir = this.getApplicationInfo().sourceDir;  
  6. Log.e("Dean", "getApplicationInfo:sourceDir"+sourceDir);      
  7. String publicSourceDir = this.getApplicationInfo().publicSourceDir;  
  8. Log.e("Dean", "getApplicationInfo:publicSourceDir"+publicSourceDir);  
  9. int descriptionRes = this.getApplicationInfo().descriptionRes;  
  10. Log.e("Dean", "getApplicationInfo:descriptionRes"+descriptionRes);
复制代码


偏偏接口中没有context的参数!而且这个方式貌似就有局限性了! 
那么我们可以想想在做checkpermission的判断的时候也需要知道是哪个apk吧!可以看看别人是怎么做到的! 
看一下源代码: 

Java代码




  1. public int checkCallingOrSelfPermission(String permission) {  
  2.     if (permission == null) {  
  3.         throw new IllegalArgumentException("permission is null");  
  4.     }  
  5.     return checkPermission(permission, Binder.getCallingPid(),  
  6.             Binder.getCallingUid());  
  7. }  
复制代码

这里可以通过Binder.getCallingPid(),Binder.getCallingUid()的到apk的 pid 和 uid! 
那看看源码流程是怎么样的 

Java代码




  1. /** 
  2.      * This can be called with or without the global lock held. 
  3.      */  
  4.     int checkComponentPermission(String permission, int pid, int uid,  
  5.             int owningUid, boolean exported) {  
  6.         // We might be performing an operation on behalf of an indirect binder  
  7.         // invocation, e.g. via {@link #openContentUri}.  Check and adjust the  
  8.         // client identity accordingly before proceeding.  
  9.         Identity tlsIdentity = sCallerIdentity.get();  
  10.         if (tlsIdentity != null) {  
  11.             Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"  
  12.                     + tlsIdentity.pid + "," + tlsIdentity.uid + "}");  
  13.             uid = tlsIdentity.uid;  
  14.             pid = tlsIdentity.pid;  
  15.         }  
  16.   
  17.         // Root, system server and our own process get to do everything.  
  18.         if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID ||  
  19.             !Process.supportsProcesses()) {  
  20.             return PackageManager.PERMISSION_GRANTED;  
  21.         }  
  22.         // If there is a uid that owns whatever is being accessed, it has  
  23.         // blanket access to it regardless of the permissions it requires.  
  24.         if (owningUid >= 0 && uid == owningUid) {  
  25.             return PackageManager.PERMISSION_GRANTED;  
  26.         }  
  27.         Log.e("Dean", "ActivityManagerService checkComponentPermission uid:" + uid + "pid:" + pid  
  28.                 + "!Process.supportsProcesses()" + !Process.supportsProcesses() + "owningUid:"  
  29.                 + owningUid + "exported:" + exported + "permission:" + permission);  
  30.   
  31.         // If the target is not exported, then nobody else can get to it.  
  32.         if (!exported) {  
  33.             Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid);  
  34.             return PackageManager.PERMISSION_DENIED;  
  35.         }  
  36.         if (permission == null) {  
  37.             return PackageManager.PERMISSION_GRANTED;  
  38.         }  
  39.         try {  
  40.             return AppGlobals.getPackageManager()  
  41.                     .checkUidPermission(permission, uid);  
  42.         } catch (RemoteException e) {  
  43.             // Should never happen, but if it does... deny!  
  44.             Slog.e(TAG, "PackageManager is dead?!?", e);  
  45.         }  
  46.         return PackageManager.PERMISSION_DENIED;  
  47.     }  
复制代码

这样我们发现在对apk拥有的是否是root等权限判断后调用了

Java代码




  1. AppGlobals.getPackageManager()  
  2.                     .checkUidPermission(permission, uid);  
复制代码

这样就调回到packagemanage里面来通过uid群找它所拥有的permission,code 如下 

Java代码




  1. public int checkUidPermission(String permName, int uid) {  
  2.   
  3.     synchronized (mPackages) {  
  4.         Object obj = mSettings.getUserIdLP(uid);  
  5.         if (obj != null) {  
  6.             GrantedPermissions gp = (GrantedPermissions)obj;  
  7.             Log.e("Dean", "PackageManagerService checkUidPermission obj != null");  
  8.             if (gp.grantedPermissions.contains(permName)) {  
  9.                 return PackageManager.PERMISSION_GRANTED;  
  10.             }  
  11.         } else {  
  12.             HashSet<String> perms = mSystemPermissions.get(uid);  
  13.             Log.e("Dean", "PackageManagerService checkUidPermission obj == null");  
  14.             if (perms != null && perms.contains(permName)) {  
  15.                 return PackageManager.PERMISSION_GRANTED;  
  16.             }  
  17.         }  
  18.     }  
  19.     Log.e("Dean", "PackageManagerService checkUidPermission");  
  20.     return PackageManager.PERMISSION_DENIED;  
  21. }  
复制代码

通过这个uid获得GrantedPermissions!这个就是他所拥有的permission列表! 
这个方法后面的部分貌似对我们的需求么用! 
我们需要得到的是apk的唯一标识包名! 
这里有两个方法 

一个用uid 获得包名 

Java代码




  1. public String[] getPackagesForUid(int uid)  
复制代码

这里得到的是一个数组 
这里之所以会得到一个数组是因为有一个属性android:sharedUserId 
多个apk可以共享一个userid!那么这会不会影响我们的结果了! 
答案是不会!当然这个答案是做过测试的!如果有android:sharedUserId那么两个apk必须有相同的证书!否则会报INSTALL_FAILED_SHARED_USER_INCOMPATIBLE 
错误!也就是说得到的数组的任何一个packagename取到的Signature应该是一样的! 
所以此方案可行! 

另一个通过pid可以知道当前的进程运行pkglist! 

Java代码


  1. List<RunningAppProcessInfo> getRunningAppProcesses = null;  
  2.         try {  
  3.             getRunningAppProcesses = ActivityManagerNative.getDefault()  
  4.                     .getRunningAppProcesses();  
  5.         } catch (Exception e) {  
  6.         }  
  7.           
  8.         for (RunningAppProcessInfo runningappprocessinfo : getRunningAppProcesses) {  
  9.             Log.e("Dean", "runningappprocessinfo.pid:"+runningappprocessinfo.pid+"runningappprocessinfo.processName"+runningappprocessinfo.processName);  
  10.               
  11.             for(int i = 0;i<runningappprocessinfo.pkgList.length;i++){  
  12.                 Log.e("Dean","pkgname:"+runningappprocessinfo.pid+":"+runningappprocessinfo.pkgList[i]);  
  13.             }  
  14.         }  
  15.         Log.e("Dean", "...................................................");  
  16.         for (RunningAppProcessInfo runningappprocessinfo : getRunningAppProcesses) {  
  17.             Log.e("Dean","runningappprocessinfo.pid:"+runningappprocessinfo.pid);  
  18.             if (runningappprocessinfo.pid == Binder.getCallingPid()) {  
  19.                 Log.e("Dean","runningappprocessinfo.pid:"+runningappprocessinfo.pid);  
  20.                 Log.e("Dean", "runningappprocessinfo" + runningappprocessinfo  
  21.                         + "runningappprocessinfo.processName:"  
  22.                         + runningappprocessinfo.processName  
  23.                         + "runningappprocessinfo.pkgList:"  
  24.                         + runningappprocessinfo.pkgList);  
  25.                 ApplicationInfo mAppInfo = null;  
  26.                 try {  
  27.                 Log.e("Dean","runningappprocessinfo.pkgList"+runningappprocessinfo.pkgList);  
  28.                   
  29.                 Log.e("Dean","runningappprocessinfo.pkgList"+runningappprocessinfo.pkgList.length);  
  30.   
  31.                 for(int i = 0;i<runningappprocessinfo.pkgList.length;i++){  
  32.                     Log.e("Dean","pkgname:"+runningappprocessinfo.pkgList[i]);  
  33.                 }  
  34.                   
  35.                 mAppInfo = getPackageManager().getApplicationInfo(runningappprocessinfo.processName,  
  36.                         PackageManager.GET_UNINSTALLED_PACKAGES);  
  37.                   
  38.                 Log.e("Dean", "mAppInfo.publicSourceDir"+mAppInfo.publicSourceDir);  
  39.                 Log.e("Dean", "mAppInfo.sourceDir"+mAppInfo.sourceDir);  
  40.                 } catch (Exception e) {  
  41.                 }  
  42.             }  
  43.         }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值