Skip to content

Commit d6f717d

Browse files
carlospolopgitbook-bot
authored andcommitted
GITBOOK-4109: change request with no subject merged in GitBook
1 parent 7f25eb4 commit d6f717d

File tree

3 files changed

+338
-236
lines changed

3 files changed

+338
-236
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
* [macOS Library Injection](macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-library-injection/README.md)
172172
* [macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES](macos-hardening/macos-security-and-privilege-escalation/macos-dyld-hijacking-and-dyld\_insert\_libraries.md)
173173
* [macOS Security Protections](macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/README.md)
174+
* [macOS Gatekeeper](macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/macos-gatekeeper.md)
174175
* [macOS SIP](macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/macos-sip.md)
175176
* [macOS Sandbox](macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/macos-sandbox/README.md)
176177
* [macOS Default Sandbox Debug](macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/macos-sandbox/macos-default-sandbox-debug.md)

macos-hardening/macos-security-and-privilege-escalation/macos-security-protections/README.md

Lines changed: 5 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -14,244 +14,13 @@
1414

1515
## Gatekeeper
1616

17-
**Gatekeeper** is a security feature developed for Mac operating systems, designed to ensure that users **run only trusted software** on their systems. It functions by **validating software** that a user downloads and attempts to open from **sources outside the App Store**, such as an app, a plug-in, or an installer package.
17+
Gatekeeper is usually used to refer to the combination of **Quarantine + Gatekeeper + XProtect**, 3 macOS security modules that will try to **prevent users from executing potentially malicious software downloaded**.
1818

19-
The key mechanism of Gatekeeper lies in its **verification** process. It checks if the downloaded software is **signed by a recognized developer**, ensuring the software's authenticity. Further, it ascertains whether the software is **notarised by Apple**, confirming that it is devoid of known malicious content and has not been tampered with after notarisation.
19+
More information in:
2020

21-
Additionally, Gatekeeper reinforces user control and security by **prompting users to approve the opening** of downloaded software for the first time. This safeguard helps prevent users from inadvertently running potentially harmful executable code that they may have mistaken for a harmless data file.
22-
23-
### Application Signatures
24-
25-
Application signatures, also known as code signatures, are a critical component of Apple's security infrastructure. They're used to **verify the identity of the software author** (the developer) and to ensure that the code hasn't been tampered with since it was last signed.
26-
27-
Here's how it works:
28-
29-
1. **Signing the Application:** When a developer is ready to distribute their application, they **sign the application using a private key**. This private key is associated with a **certificate that Apple issues to the developer** when they enroll in the Apple Developer Program. The signing process involves creating a cryptographic hash of all parts of the app and encrypting this hash with the developer's private key.
30-
2. **Distributing the Application:** The signed application is then distributed to users along with the developer's certificate, which contains the corresponding public key.
31-
3. **Verifying the Application:** When a user downloads and attempts to run the application, their Mac operating system uses the public key from the developer's certificate to decrypt the hash. It then recalculates the hash based on the current state of the application and compares this with the decrypted hash. If they match, it means **the application hasn't been modified** since the developer signed it, and the system permits the application to run.
32-
33-
Application signatures are an essential part of Apple's Gatekeeper technology. When a user attempts to **open an application downloaded from the internet**, Gatekeeper verifies the application signature. If it's signed with a certificate issued by Apple to a known developer and the code hasn't been tampered with, Gatekeeper permits the application to run. Otherwise, it blocks the application and alerts the user.
34-
35-
Starting from macOS Catalina, **Gatekeeper also checks whether the application has been notarized** by Apple, adding an extra layer of security. The notarization process checks the application for known security issues and malicious code, and if these checks pass, Apple adds a ticket to the application that Gatekeeper can verify.
36-
37-
#### Check Signatures
38-
39-
When checking some **malware sample** you should always **check the signature** of the binary as the **developer** that signed it may be already **related** with **malware.**
40-
41-
```bash
42-
# Get signer
43-
codesign -vv -d /bin/ls 2>&1 | grep -E "Authority|TeamIdentifier"
44-
45-
# Check if the app’s contents have been modified
46-
codesign --verify --verbose /Applications/Safari.app
47-
48-
# Get entitlements from the binary
49-
codesign -d --entitlements :- /System/Applications/Automator.app # Check the TCC perms
50-
51-
# Check if the signature is valid
52-
spctl --assess --verbose /Applications/Safari.app
53-
54-
# Sign a binary
55-
codesign -s <cert-name-keychain> toolsdemo
56-
```
57-
58-
### Notarization
59-
60-
Apple's notarization process serves as an additional safeguard to protect users from potentially harmful software. It involves the **developer submitting their application for examination** by **Apple's Notary Service**, which should not be confused with App Review. This service is an **automated system** that scrutinizes the submitted software for the presence of **malicious content** and any potential issues with code-signing.
61-
62-
If the software **passes** this inspection without raising any concerns, the Notary Service generates a notarization ticket. The developer is then required to **attach this ticket to their software**, a process known as 'stapling.' Furthermore, the notarization ticket is also published online where Gatekeeper, Apple's security technology, can access it.
63-
64-
Upon the user's first installation or execution of the software, the existence of the notarization ticket - whether stapled to the executable or found online - **informs Gatekeeper that the software has been notarized by Apple**. As a result, Gatekeeper displays a descriptive message in the initial launch dialog, indicating that the software has undergone checks for malicious content by Apple. This process thereby enhances user confidence in the security of the software they install or run on their systems.
65-
66-
### Enumerating GateKeeper
67-
68-
GateKeeper is both, **several security components** that prevent untrusted apps from being executed and also **one of the components**.
69-
70-
It's possible to see the **status** of GateKeeper with:
71-
72-
```bash
73-
# Check the status
74-
spctl --status
75-
```
76-
77-
{% hint style="danger" %}
78-
Note that GateKeeper signature checks are performed only to **files with the Quarantine attribute**, not to every file.
79-
{% endhint %}
80-
81-
GateKeeper will check if according to the **preferences & the signature** a binary can be executed:
82-
83-
<figure><img src="../../../.gitbook/assets/image (678).png" alt=""><figcaption></figcaption></figure>
84-
85-
The database that keeps this configuration ins located in **`/var/db/SystemPolicy`**. You can check this database as root with:
86-
87-
```bash
88-
# Open database
89-
sqlite3 /var/db/SystemPolicy
90-
91-
# Get allowed rules
92-
SELECT requirement,allow,disabled,label from authority where label != 'GKE' and disabled=0;
93-
requirement|allow|disabled|label
94-
anchor apple generic and certificate 1[subject.CN] = "Apple Software Update Certification Authority"|1|0|Apple Installer
95-
anchor apple|1|0|Apple System
96-
anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.9] exists|1|0|Mac App Store
97-
anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists and (certificate leaf[field.1.2.840.113635.100.6.1.14] or certificate leaf[field.1.2.840.113635.100.6.1.13]) and notarized|1|0|Notarized Developer ID
98-
[...]
99-
```
100-
101-
Note how the first rule ended in "**App Store**" and the second one in "**Developer ID**" and that in the previous imaged it was **enabled to execute apps from the App Store and identified developers**.\
102-
If you **modify** that setting to App Store, the "**Notarized Developer ID" rules will disappear**.
103-
104-
There are also thousands of rules of **type GKE**:
105-
106-
```bash
107-
SELECT requirement,allow,disabled,label from authority where label = 'GKE' limit 5;
108-
cdhash H"b40281d347dc574ae0850682f0fd1173aa2d0a39"|1|0|GKE
109-
cdhash H"5fd63f5342ac0c7c0774ebcbecaf8787367c480f"|1|0|GKE
110-
cdhash H"4317047eefac8125ce4d44cab0eb7b1dff29d19a"|1|0|GKE
111-
cdhash H"0a71962e7a32f0c2b41ddb1fb8403f3420e1d861"|1|0|GKE
112-
cdhash H"8d0d90ff23c3071211646c4c9c607cdb601cb18f"|1|0|GKE
113-
```
114-
115-
These are hashes that come from **`/var/db/SystemPolicyConfiguration/gke.bundle/Contents/Resources/gke.auth`, `/var/db/gke.bundle/Contents/Resources/gk.db`** and **`/var/db/gkopaque.bundle/Contents/Resources/gkopaque.db`**
116-
117-
The options **`--master-disable`** and **`--global-disable`** of **`spctl`** will completely **disable** these signature checks:
118-
119-
```bash
120-
# Disable GateKeeper
121-
spctl --global-disable
122-
spctl --master-disable
123-
124-
# Enable it
125-
spctl --global-enable
126-
spctl --master-enable
127-
```
128-
129-
When completely enabled, a new option will appead:
130-
131-
<figure><img src="../../../.gitbook/assets/image (679).png" alt=""><figcaption></figcaption></figure>
132-
133-
It's possible to **check if an App will be allowed by GateKeeper** with:
134-
135-
```bash
136-
spctl --assess -v /Applications/App.app
137-
```
138-
139-
It's possible to add new rules in GateKeeper to allow the execution of certain apps with:
140-
141-
```bash
142-
# Check if allowed - nop
143-
spctl --assess -v /Applications/App.app
144-
/Applications/App.app: rejected
145-
source=no usable signature
146-
147-
# Add a label and allow this label in GateKeeper
148-
sudo spctl --add --label "whitelist" /Applications/App.app
149-
sudo spctl --enable --label "whitelist"
150-
151-
# Check again - yep
152-
spctl --assess -v /Applications/App.app
153-
/Applications/App.app: accepted
154-
```
155-
156-
### Quarantine Files
157-
158-
Upon **downloading** an application or file, specific macOS **applications** such as web browsers or email clients **attach an extended file attribute**, commonly known as the "**quarantine flag**," to the downloaded file. This attribute acts as a security measure to **mark the file** as coming from an untrusted source (the internet), and potentially carrying risks. However, not all applications attach this attribute, for instance, common BitTorrent client software usually bypasses this process.
159-
160-
**The presence of a quarantine flag signals macOS's Gatekeeper security feature when a user attempts to execute the file**.
161-
162-
In the case where the **quarantine flag is not present** (as with files downloaded via some BitTorrent clients), Gatekeeper's **checks may not be performed**. Thus, users should exercise caution when opening files downloaded from less secure or unknown sources.
163-
164-
{% hint style="info" %}
165-
**Checking** the **validity** of code signatures is a **resource-intensive** process that includes generating cryptographic **hashes** of the code and all its bundled resources. Furthermore, checking certificate validity involves doing an **online check** to Apple's servers to see if it has been revoked after it was issued. For these reasons, a full code signature and notarization check is **impractical to run every time an app is launched**.
166-
167-
Therefore, these checks are **only run when executing apps with the quarantined attribute.**
168-
{% endhint %}
169-
170-
{% hint style="warning" %}
171-
This attribute must be **set by the application creating/downloading** the file.
172-
173-
However, files that are sandboxed will have this attribute set to every file they create. And non sandboxed apps can set it theirselves, or specify the [**LSFileQuarantineEnabled**](https://developer.apple.com/documentation/bundleresources/information\_property\_list/lsfilequarantineenabled?language=objc) key in the **Info.plist** which will make the system set the `com.apple.quarantine` extended attribute on the files created,
174-
{% endhint %}
175-
176-
It's possible to **check it's status and enable/disable** (root required) with:
177-
178-
```bash
179-
spctl --status
180-
assessments enabled
181-
182-
spctl --enable
183-
spctl --disable
184-
#You can also allow nee identifies to execute code using the binary "spctl"
185-
```
186-
187-
You can also **find if a file has the quarantine extended attribute** with:
188-
189-
```bash
190-
xattr portada.png
191-
com.apple.macl
192-
com.apple.quarantine
193-
```
194-
195-
Check the **value** of the **extended** **attributes** and find out the app that wrote the quarantine attr with:
196-
197-
```bash
198-
xattr -l portada.png
199-
com.apple.macl:
200-
00000000 03 00 53 DA 55 1B AE 4C 4E 88 9D CA B7 5C 50 F3 |..S.U..LN.....P.|
201-
00000010 16 94 03 00 27 63 64 97 98 FB 4F 02 84 F3 D0 DB |....'cd...O.....|
202-
00000020 89 53 C3 FC 03 00 27 63 64 97 98 FB 4F 02 84 F3 |.S....'cd...O...|
203-
00000030 D0 DB 89 53 C3 FC 00 00 00 00 00 00 00 00 00 00 |...S............|
204-
00000040 00 00 00 00 00 00 00 00 |........|
205-
00000048
206-
com.apple.quarantine: 00C1;607842eb;Brave;F643CD5F-6071-46AB-83AB-390BA944DEC5
207-
# 00c1 -- It has been allowed to eexcute this file
208-
# 607842eb -- Timestamp
209-
# Brave -- App
210-
# F643CD5F-6071-46AB-83AB-390BA944DEC5 -- UID assigned to the file downloaded
211-
```
212-
213-
And **remove** that attribute with:
214-
215-
```bash
216-
xattr -d com.apple.quarantine portada.png
217-
#You can also remove this attribute from every file with
218-
find . -iname '*' -print0 | xargs -0 xattr -d com.apple.quarantine
219-
```
220-
221-
And find all the quarantined files with:
222-
223-
{% code overflow="wrap" %}
224-
```bash
225-
find / -exec ls -ld {} \; 2>/dev/null | grep -E "[x\-]@ " | awk '{printf $9; printf "\n"}' | xargs -I {} xattr -lv {} | grep "com.apple.quarantine"
226-
```
227-
{% endcode %}
228-
229-
Quarantine information is also stored in a central database managed by LaunchServices in **`~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2`**.
230-
231-
### XProtect
232-
233-
XProtect is a built-in **anti-malware** feature in macOS. XProtect **checks any application when it's first launched or modified against its database** of known malware and unsafe file types. When you download a file through certain apps, such as Safari, Mail, or Messages, XProtect automatically scans the file. If it matches any known malware in its database, XProtect will **prevent the file from running** and alert you to the threat.
234-
235-
The XProtect database is **updated regularly** by Apple with new malware definitions, and these updates are automatically downloaded and installed on your Mac. This ensures that XProtect is always up-to-date with the latest known threats.
236-
237-
However, it's worth noting that **XProtect isn't a full-featured antivirus solution**. It only checks for a specific list of known threats and doesn't perform on-access scanning like most antivirus software.
238-
239-
You can get information about the latest XProtect update running:
240-
241-
{% code overflow="wrap" %}
242-
```bash
243-
system_profiler SPInstallHistoryDataType 2>/dev/null | grep -A 4 "XProtectPlistConfigData" | tail -n 5
244-
```
245-
{% endcode %}
246-
247-
XProtect is located on. SIP protected location at **/Library/Apple/System/Library/CoreServices/XProtect.bundle** and inside the bundle you can find information XProtect uses:
248-
249-
* **`XProtect.bundle/Contents/Resources/LegacyEntitlementAllowlist.plist`**: Allows code with those cdhashes to use legacy entitlements.
250-
* **`XProtect.bundle/Contents/Resources/XProtect.meta.plist`**: List of plugins and extensions that are disallowed to load via BundleID and TeamID or indicating a minimum version.
251-
* **`XProtect.bundle/Contents/Resources/XProtect.yara`**: Yara rules to detect malware.
252-
* **`XProtect.bundle/Contents/Resources/gk.db`**: SQLite3 database with hashes of blocked applications and TeamIDs.
253-
254-
Note that there is another App in **`/Library/Apple/System/Library/CoreServices/XProtect.app`** related to XProtect that isn't involved when an app is run.
21+
{% content-ref url="macos-gatekeeper.md" %}
22+
[macos-gatekeeper.md](macos-gatekeeper.md)
23+
{% endcontent-ref %}
25524

25625
## MRT - Malware Removal Tool
25726

0 commit comments

Comments
 (0)