Deleting files using Swift that require admin privileges

290 views Asked by At

I am currently working on a Mac Application where I need to delete files(potentially malicious) from /Library/LaunchDaemons directory and other similar directories where I would need administrator privileges. Using the FileManager.default.removeItem was not fruitful. I have come across articles pointing me towards EvenBetterAuthorizationSample and SMJobBless and I find the documentation to be rather confusing. Am I on the right track and if so, how should I proceed?

2

There are 2 answers

1
julia_v On

One idea is to look at the init(authorization:) method of the FileManager. It is available since 10.14. I think it is the easiest way to perform the operation.

https://developer.apple.com/documentation/foundation/filemanager/3025773-init

Also, it worth checking if the directories you want to delete are not protected by SIP. In this case on SIP-enabled machines there is no way to remove them.

Good luck!

0
Joshua Kaplan On

The documentation for SMJobBless is indeed confusing. I've put together the SwiftAuthorizationSample along with a pretty extensive README. So even if the sample isn't quite what you're looking for, hopefully the README will be of some help.