在 Android 系统中, 存在 uid、pid 等概念, uid 即用户 ID, pid 即进程 ID。 uid 每个进程可能会重复, pid 每个进程唯一。
博主博客
- https://blog.uso6.com
- https://blog.****.net/dxk539687357
一、Android
1.1 代码获取 uid
public static void getUid(Context context, String packageName){
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(packageName, PackageManager.GET_ACTIVITIES);
Log.e("nukix", "uid: " + ai.uid);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
1.2 代码通过 uid 获取包名
可通过 getPackageManager().getNameForUid(uid);
获取到指定 uid 的包名。
1.3 Binder
int uid = Binder.getCallingUid();
int pid = Binder.getCallingPid();
二、使用 Shell 命令获取
2.0 adb shell
使用 shell
命令前需要使用 adb shell
先进入 shell
命令行。 如果不进入命令行, 每个操作需要带上 adb shell
, 比如 adb shell ps
。
2.1 ps 查看
emu64a:/ # ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 59:30 ? 00:00:07 init second_stage
root 2 0 0 59:30 ? 00:00:00 [kthreadd]
u0_a152 2370 341 0 08:29:48 ? 00:00:02 com.uso6
上图包名 com.uso6
的 uid
为 10152。
2.2 pid 查看
emu64a:/ # cat /proc/2370/status
Name: com.uso6
Umask: 0077
State: S (sleeping)
Tgid: 2370
Ngid: 0
Pid: 2370
PPid: 341
TracerPid: 0
Uid: 10152 10152 10152 10152
Gid: 10152 10152 10152 10152
FDSize: 128
Groups: 3003 9997 20152 50152
VmPeak: 15802860 kB
VmSize: 15141828 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 146008 kB
VmRSS: 142144 kB
RssAnon: 68712 kB
RssFile: 72536 kB
RssShmem: 896 kB
VmData: 1175152 kB
VmStk: 8192 kB
VmExe: 4 kB
VmLib: 138188 kB
VmPTE: 1228 kB
VmSwap: 0 kB
CoreDumping: 0
THP_enabled: 1
Threads: 20
SigQ: 0/10649
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000080001204
SigIgn: 0000000000000001
SigCgt: 0000006e400084f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp: 2
Seccomp_filters: 1
Speculation_Store_Bypass: thread vulnerable
SpeculationIndirectBranch: unknown
Cpus_allowed: f
Cpus_allowed_list: 0-3
Mems_allowed: 1
Mems_allowed_list: 0
voluntary_ctxt_switches: 242
nonvoluntary_ctxt_switches: 166
通过上面 ps
命令获得 pid
后, 可通过 /proc/[pid]/status
查看进程的详情。
注:由于系统版本更新和权限限制, 高版本的 Android 手机可能无法使用, 需要 root 权限。
packages 查看(需要 root 权限)
packages.list
emu64a:/ # cat /data/system/packages.list
com.android.cts.priv.ctsshim 10134 0 /data/user/0/com.android.cts.priv.ctsshim default:privapp:targetSdkVersion=28 none 0 34 1 @system
com.android.uwb.resources 10138 0 /data/user_de/0/com.android.uwb.resources default:privapp:targetSdkVersion=34 none 0 1 1 @system
com.uso6 10152 1 /data/user/0/com.uso6.demo default:targetSdkVersion=34 3003 0 1 1 @null
packages.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<packages>
<version sdkVersion="34" databaseVersion="3" buildFingerprint="Android/sdk_phone64_arm64/emu64a:14/UE1A.230829.036.A1/11228894:userdebug/test-keys" fingerprint="ac03ec39cece4d351ed92707ae7910bc44efa072" />
<version volumeUuid="primary_physical" sdkVersion="34" databaseVersion="3" buildFingerprint="Android/sdk_phone64_arm64/emu64a:14/UE1A.230829.036.A1/11228894:userdebug/test-keys" fingerprint="ac03ec39cece4d351ed92707ae7910bc44efa072" />
<permission-trees />
<permissions>
<item name="android.permission.health.READ_RESTING_HEART_RATE" package="com.android.healthconnect.controller" protection="1" />
</permissions>
<package name="com.uso6" codePath="/data/app/~~VHH5JNYgi_enAqDDyv2vSg==/com.uso6-QXk3EBHXw_vxWJvDNQESbQ==" nativeLibraryPath="/data/app/~~VHH5JNYgi_enAqDDyv2vSg==/com.uso6-QXk3EBHXw_vxWJvDNQESbQ==/lib" publicFlags="541638470" privateFlags="-1946152960" ft="18ed4dc3d47" ut="18ed4dc3e6e" version="1" userId="10152" packageSource="1" installInitiator="com.android.shell" loadingProgress="1.0" loadingCompletedTime="0" domainSetId="85f08e26-383b-4a57-8695-ce18e4c7f003">
<sigs count="1" schemeVersion="2">
<cert index="7" key="308202E4308201CC020101300D06092A864886F70D010105050030373116301406035504030C0D416E64726F69642044656275673110300E060355040A0C07416E64726F6964310B30090603550406130255533020170D3234303330343039323132375A180F32303534303232353039323132375A30373116301406035504030C0D416E64726F69642044656275673110300E060355040A0C07416E64726F6964310B300906035504061302555330820122300D06092A864886F70D01010105000382010F003082010A0282010100A24ABFB31D15E167F6B9E6676358310AAA7642C0AB443BF2FFDBE0F80875C02459EBABC115556CAC33B7679DE71CFB51C1C89FE8E11C4A8818D0F1DB52FCF066E007E9D08390842F66D776817CE96A9E29CF10EDC20FF52FB64482E9602D0F8F0C809660864BB3DCF91F377A0329F8B571B47E6F128400CFA835CEC6A089A64613727C35A01463B83BF9A9D84C003DDC8277A1627A549E9C5BFB428D0AC15F870A31FCF5FE8A3DE772B12838DDA53F128511E440F9D0F41218155A34D5D1FDD36B5F795BA5066F8E2FE7D4D15850D02C42CE3333114B363B1EC25C9D4AA7FD0A4ABFCC23BA5C5AF897E874785626A8D0A1CD002CCA0152B7B12B5F45A44B970B0203010001300D06092A864886F70D010105050003820101003208931DD2B67C4A3B1BF111D0F80A04E9A5D7476B217ADCFB50F4DD7703862A9EDE92C35AF7E3176F854A0B6F2016021820927082715B6BDE3DB48E2C4344627B34EB5C20B5CBFA4E3F0491A68A3F2BFECB1B95B699505E2B3D3FD3A16C4829DB7CE72C5B74F6BB83271C8D8C55899A547D4D7E91DC8D1C037CF25624667FB19D674A62F92DE793E93040A7017E81665C78CAA2D4F5CD80F58798CF5F5EB5AA1736F248163541E776BCE770D9B8C6FF1512D5C7A64BF13C5985F4FC4BDCC6DA5E6895796E5411674A0B6F77E477172DF4AD50BEF099D456D258E640F30F085602A4DE58F86EC2FEDC750769F847AC2B0528FD389AF92FB2F54485E97C465B24" />
</sigs>
<proper-signing-keyset identifier="30" />
</package>
<shared-user name="android.media" userId="10059">
<sigs count="1" schemeVersion="3">
<cert index="5" />
</sigs>
</shared-user>
<domain-verifications>
<active>
<package-state packageName="com.uso6" id="85f08e26-383b-4a57-8695-ce18e4c7f003" />
</active>
<restored />
</domain-verifications>
<domain-verifications-legacy>
<user-states packageName="com.android.cts.priv.ctsshim">
<user-state userId="0" state="0" />
</user-states>
</domain-verifications-legacy>
<keyset-settings version="1">
<keys>
<public-key identifier="1" value="MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA1pMZBN7GCySx7cdi4NnYJT4+zWzrHeL/Boyo6LyozWvTeG6nCqds5g67D5k1Wf/ZPnepQ+foPUtkuOT+otPmVvHiZ6gbv7IwtXjCBEO+THIYuEb1IRWG8DihTonCvjh/jr7Pj8rD2h7jMMnqk9Cnw9xK81AiDVAIBzLggJcX7moFM1nmppTsLLPyhKCkZsh6lNg7MQk6ZzcuL2QSwG5tQvFYGN/+A4HMDNRE2mzdw7gkWBlIAbMlZBNPv96YySh3SNv1Z2pUDYFUyLvKB7niR1UzEcRrmvdv3uzMjmnnyKLQjngmIJQ/mXJ9PAT+cpkdmd+brjigshd/ox1bav7pHwIBAw==" />
</keys>
<keysets>
<keyset identifier="1">
<key-id identifier="1" />
</keyset>
</keysets>
<lastIssuedKeyId value="30" />
<lastIssuedKeySetId value="30" />
</keyset-settings>
</packages>
注意:packages.xml 默认被设置为二进制xml格式, 直接打开是乱码, 需要使用 adb 修改配置将其修改为普通的 xml 格式。
adb shell setprop persist.sys.binary_xml false
修改完成后, 需要重启手机, 可通过 adb boot now
进行快速重启, 重启完后再次获取就是普通的 xml 格式。
三、系统预设的 uid
android.os.Process
public static final int INVALID_UID = -1;
public static final int SYSTEM_UID = 1000;
public static final int PHONE_UID = 1001;
public static final int SHELL_UID = 2000;
public static final int LOG_UID = 1007;
public static final int WIFI_UID = 1010;
public static final int MEDIA_UID = 1013;
public static final int DRM_UID = 1019;
public static final int VPN_UID = 1016;
public static final int KEYSTORE_UID = 1017;
public static final int CREDSTORE_UID = 1076;
public static final int NFC_UID = 1027;
public static final int CLAT_UID = 1029;
public static final int BLUETOOTH_UID = 1002;
public static final int SHARED_RELRO_UID = 1037;
public static final int AUDIOSERVER_UID = 1041;
public static final int CAMERASERVER_UID = 1047;
public static final int DNS_TETHER_UID = 1052;
public static final int WEBVIEW_ZYGOTE_UID = 1053;
public static final int OTA_UPDATE_UID = 1061;
public static final int STATSD_UID = 1066;
public static final int INCIDENTD_UID = 1067;
public static final int SE_UID = 1068;
public static final int NETWORK_STACK_UID = 1073;
public static final int FSVERITY_CERT_UID = 1075;
public static final int UWB_UID = 1083;
public static final int SDK_SANDBOX_VIRTUAL_UID = 1090;
public static final int NOBODY_UID = 9999;
public static final int FIRST_APPLICATION_UID = 10000;
public static final int LAST_APPLICATION_UID = 19999;
public static final int FIRST_SDK_SANDBOX_UID = 20000;
public static final int LAST_SDK_SANDBOX_UID = 29999;
public static final int FIRST_APP_ZYGOTE_ISOLATED_UID = 90000;
public static final int LAST_APP_ZYGOTE_ISOLATED_UID = 98999;
public static final int FIRST_ISOLATED_UID = 99000;
public static final int LAST_ISOLATED_UID = 99999;
里面大部分都是 @hide
不能在应用中使用。