How to Get uid from packet - android

两盒软妹~` 提交于 2021-01-27 06:50:49

问题


I am developing a firewall application in android where I need to identify apps based on uid. I have created a vpn service and I am able to capture packets and get destination ip address.

Is there any way to get uid of apps from packets or datagram sockets.


回答1:


Parse the packet, for TCP/UDP, you can retrieve the (local_ip, local_port, remote_ip, remote_port) tuple. Then read and parse /proc/net/tcp or /proc/net/udp, there's uid field in it.

This is a loopback socket whose owner uid is 10117:

sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
7: 0100007F:8FC5 0100007F:0438 01 00000000:00000000 00:00000000 00000000 10117        0 242607 1 00000000 21 4 26 10 -1



回答2:


You will have to write some native code for this to communicate at kernel level.



来源:https://stackoverflow.com/questions/28930342/how-to-get-uid-from-packet-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!