MacOS App code escalation

Original: https://jacobpan3g.github.io/cn/2018/02/07/gain-root-permission-for-mac-app/

In MacOS App development, some operations require administrator privileges, and an authentication dialog box needs to pop up for users to enter their account and password. This process is the process of MacOS App privilege escalation. There are mainly the following methods:

1. AuthorizationExecuteWithPrivileges()

The most representative library using this interface is STPrivilegedTask , which is a well-packaged App privilege escalation library. The interface is very similar to NSTask, and it is very convenient to use.

It is a pity that the AuthorizationExecuteWithPrivileges() interface has been deprecated since MacOS 10.7, and it is said that the interface was closed in version 10.12.

Therefore, if you want to be compatible with MacOS versions after 10.12, you have to give up using the STPrivilegedTask library.

2. Register LaunchdDaemon with ServiceManagement.framework

This is the currently recommended method of privilege escalation by Apple. There is an official SMJobBless demo, which is a bit complicated. For details, please refer to another document "SMJobBless Official Demo Notes"

This method has a few minor drawbacks:

  • The prompt content of the pop-up authentication dialog box is "Install Helper is required", and this prompt is always displayed every time it is opened. For novice users, will it mislead the user that the App is always installing something?
  • LaunchdDaemon and its configuration files need to be installed /Library. Will there be "residual problems" after the user uninstalls the app?

3. Using AppleScript

do shell script "..." with administrator privileges

The ellipsis part is filled in the shell script, and any executable file needs to write the full path, eg /bin/ls.

The above is an AppleScript script. The privilege escalation in this way has the following advantages:

  • Much simpler to implement than the above "register LaunchdDaemon" method
  • At the same time, you don't have to worry about uninstalling the residual problem, because everything is in the .app

There are two ways of executing AppleScript scripts in objective-c:

  • Execute "/usr/bin/osascript -e "do shell ..."" via NSTask
  • Execute via NSAppleScript

Method 1 has two disadvantages:

  • do shell scriptAll stdout will be returned after all shell executions are completed, so when the Daemon process is started, even if the fileHandle Notification of NSTask is used, stdout cannot be read out in stages.

  • The prompt message in the authentication window of this method is "osascript wants to make changes.", for novice users, will there be such a feeling "I clearly installed XXX.app, how come there is an osascript for me to enter?" Account password? Could it be a malicious program?"

When using the NSAppleScript method, the prompt message in the authentication window is "APP_NAME wants to make changes.", which is more friendly. But there are also some disadvantages:

  • If NSAppleScript executes the Daemon process, it will not exit until the Daemon exits, that is, it will always occupy the thread

    Solution: It is recommended to use NSAppleScript in the child thread to avoid UI unresponsiveness

  • There is also a problem that stdout can only be printed after the Daemon exits

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325946812&siteId=291194637