6

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.

SwiftParser
  • 164
  • 1
  • 11
  • 2
    Direct access to /proc/ files no longer works from android-Q [Refer](https://stackoverflow.com/questions/58497492/acccess-to-proc-net-tcp-in-android-q) – Anoop M Maddasseri Oct 23 '19 at 12:06

2 Answers2

7

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
ed9er
  • 311
  • 1
  • 3
  • 12
  • 1
    Just a note for an example. this function: get_uid_sub() https://github.com/M66B/NetGuard/blob/master/app/src/main/jni/netguard/ip.c – albert Oct 26 '17 at 18:14
0

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

d.k.
  • 415
  • 4
  • 16