0

I have a process that attempts to get the package name from another process. I am using C++ code and compiling it using Android toolchain. I have its socket fd and ip address, however, I didn't find in the api a function that will return a string containing the package name. I have the socket fd and the address, if it helps:

uid_t GetSocketID(int sockfd)
{
    int err;
    socklen_t len;
    struct sockaddr_in addr;
    len = sizeof(addr);
    int res = getpeername(sockfd, (struct sockaddr*)&addr, &len);

    if (res < 0)
    {
        err = errno;
        return -1;
    }

    int iSockIp = addr.sin_addr.s_addr;
    int iSockPort = ntohs(addr.sin_port);
    int iUid = -1;

    if (iSockIp == 0 || iSockPort == 0)
    {
            return -1;

    }
}

example taken from: https://github.com/android/platform_system_core/blob/master/libcutils/qtaguid.c

Does anybody know how can I get the package name?

Thanks!

inbaly
  • 582
  • 1
  • 6
  • 16

1 Answers1

0

To do it in 3 steps:

  • Use netstat to find the process from the socket
  • Use ps to find the executable from the process information
  • Search AndroidManifest.xml in the app's root dir to find the APK owning the executable.
stark
  • 12,615
  • 3
  • 33
  • 50
  • It sounds simple, however can you be more specific in the commands? I couldn't find the relevant process. Thanks – inbaly May 15 '16 at 10:52
  • A helpfull command: busybox netstat -tp | grep | busybox awk '{print $7} should return the PID/Program name (first chars of it), for example: '10404/com.facebook.' that already helped me since I got the PID. – inbaly May 22 '16 at 09:04