Last active
March 30, 2017 07:59
-
-
Save LucaFilipozzi/e4f1e118202aff27af6aadebda1b5d91 to your computer and use it in GitHub Desktop.
python implementation of getpeereid() and getpeerpid() for darwin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# vim: set ts=4 sw=4 et ai si: | |
import socket | |
import struct | |
def getpeereid(sock): | |
LOCAL_PEERCRED = 0x001 | |
fmt = struct.Struct('2Ih16I') # see /usr/include/sys/ucred.h struct xucred | |
res = tuple(fmt.unpack(sock.getsockopt(0, LOCAL_PEERCRED, fmt.size))) | |
return (res[1], res[3:3+res[2]]) # return peer's uid and gids | |
def getpeerpid(sock): | |
LOCAL_PEERPID = 0x002 | |
fmt = struct.Struct('I') | |
res = fmt.unpack(sock.getsockopt(0, LOCAL_PEERPID, fmt.size)) | |
return (res[0]) # return peer's pid | |
path = '/var/run/mDNSResponder' | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
sock.connect(path) | |
uid, gids = getpeereid(sock) | |
pid = getpeerpid(sock) | |
print("socket: %s" % path) | |
print("peer uid = %s" % uid) | |
print("peer pid = %s" % pid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment