Dear all,
In macOS Catalina we have the new NetworkExtension framework that can filter network trafic on a computer.
In my usecase I need the PID of the process that is the originator of the network flow. I'm aware that PID are not a reliable way to identify a process (since PIDs can be reused), but in my usecase only PID can identify what I need.
In handleNewFlow(_ flow: NEFilterFlow) we can get the sourceAppAuditToken (flow.sourceAppAuditToken), where sourceAppAuditToken is a Data type. Is there a way to convert this sourceAppAuditToken to a PID value?
I'm also aware of getting the signature of the process (eventually the Bundle ID) with SecCodeCopySigningInformation / kSecCSDynamicInformation, but again in my usecase it does not help.
A way to do this is to call "netstat" and look for the local port in the output and get the PID from there, but sometimes this is not very reliable.
Any ideas how to do this?
Regards,
Alex
I wanted to suggest an alternative approach here, namely to add an initialiser to audit_token_t
:
extension audit_token_t {
init?(data: Data) {
guard data.count == MemoryLayout<audit_token_t>.size else { return nil }
self = data.withUnsafeBytes { buf in
buf.baseAddress!.load(as: audit_token_t.self)
}
}
}
These days I prefer this option because I can use it in more circumstances. For example, NEFilterFlow
now has a second audit token property, sourceProcessAuditToken
, and this initialiser makes it easy to work with either.
Oh, and this code uses load(fromByteOffset:as:)
, which I find to be very convenient.
But, who knows, maybe in another four years I’ll have changed my mind again and present yet another new option (-:
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"