UTS Namespace
Tip
AWS ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:HackTricks Training GCP Red Team Expert (GRTE)
Azure ν΄νΉ λ°°μ°κΈ° λ° μ°μ΅νκΈ°:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks μ§μνκΈ°
- ꡬλ κ³ν νμΈνκΈ°!
- **π¬ λμ€μ½λ κ·Έλ£Ή λλ ν λ κ·Έλ¨ κ·Έλ£Ήμ μ°Έμ¬νκ±°λ νΈμν° π¦ @hacktricks_liveλ₯Ό νλ‘μ°νμΈμ.
- HackTricks λ° HackTricks Cloud κΉνλΈ λ¦¬ν¬μ§ν 리μ PRμ μ μΆνμ¬ ν΄νΉ νΈλ¦μ 곡μ νμΈμ.
Basic Information
A UTS (UNIX Time-Sharing System) namespace is a Linux kernel feature that provides iλ μμ€ν μλ³μμ 격리: the hostname and the NIS (Network Information Service) domain name. μ΄ κ²©λ¦¬λ₯Ό ν΅ν΄ κ° UTS λ€μμ€νμ΄μ€λ μ체 λ 립μ μΈ hostname λ° NIS λλ©μΈ μ΄λ¦μ κ°μ§ μ μμΌλ©°, μ΄λ κ° μ»¨ν μ΄λκ° μ체 hostnameμ κ°μ§ λ³λμ μμ€ν μ²λΌ 보μ΄λλ‘ ν΄μΌ νλ containerization νκ²½μμ νΉν μ μ©ν©λλ€.
How it works:
- When a new UTS namespace is created, it starts with a copy of the hostname and NIS domain name from its parent namespace. This means that, at creation, the new namespace sμμ λ€μμ€νμ΄μ€μ λμΌν μλ³μλ₯Ό 곡μ ν©λλ€. κ·Έλ¬λ λ€μμ€νμ΄μ€ λ΄μμ hostnameμ΄λ NIS λλ©μΈ μ΄λ¦μ λν μ΄ν λ³κ²½μ λ€λ₯Έ λ€μμ€νμ΄μ€μ μν₯μ μ£Όμ§ μμ΅λλ€.
- Processes within a UTS namespace can change the hostname and NIS domain name using the
sethostname()andsetdomainname()system calls, respectively. μ΄λ¬ν λ³κ²½μ λ€μμ€νμ΄μ€ λ‘컬μ΄λ©° λ€λ₯Έ λ€μμ€νμ΄μ€λ νΈμ€νΈ μμ€ν μ μν₯μ μ£Όμ§ μμ΅λλ€. - Processes can move between namespaces using the
setns()system call or create new namespaces using theunshare()orclone()system calls with theCLONE_NEWUTSflag. νλ‘μΈμ€κ° μ λ€μμ€νμ΄μ€λ‘ μ΄λνκ±°λ μ λ€μμ€νμ΄μ€λ₯Ό μμ±νλ©΄ ν΄λΉ λ€μμ€νμ΄μ€μ μ°κ΄λ hostname λ° NIS λλ©μΈ μ΄λ¦μ μ¬μ©νκΈ° μμν©λλ€.
Lab:
Create different Namespaces
CLI
sudo unshare -u [--mount-proc] /bin/bash
By mounting a new instance of the /proc filesystem if you use the param --mount-proc, you ensure that the new mount namespace has an ν΄λΉ λ€μμ€νμ΄μ€μ νΉνλ νλ‘μΈμ€ μ 보λ₯Ό μ ννκ³ κ²©λ¦¬λ κ΄μ μΌλ‘ λ³Ό μ μλλ‘.
Error: bash: fork: Cannot allocate memory
When unshare is executed without the -f option, an error is encountered due to the way Linux handles new PID (Process ID) namespaces. The key details and the solution are outlined below:
- λ¬Έμ μ€λͺ :
- Linux 컀λμ νλ‘μΈμ€κ°
unshareμμ€ν μ½μ μ¬μ©ν΄ μ λ€μμ€νμ΄μ€λ₯Ό μμ±νλ κ²μ νμ©ν©λλ€. νμ§λ§ μλ‘μ΄ PID (Process ID) λ€μμ€νμ΄μ€ μμ±μ μμν νλ‘μΈμ€(μ΄λ₯Ό βunshareβ νλ‘μΈμ€λΌκ³ μΉν¨)λ μ λ€μμ€νμ΄μ€λ‘ λ€μ΄κ°μ§ μμΌλ©°, μ€μ§ κ·Έ μμ νλ‘μΈμ€λ€λ§ λ€μ΄κ°λλ€. - Running %unshare -p /bin/bash% starts
/bin/bashin the same process asunshare. Consequently,/bin/bashand its child processes are in the original PID namespace. - μ λ€μμ€νμ΄μ€μμ
/bin/bashμ 첫 λ²μ§Έ μμ νλ‘μΈμ€κ° PID 1μ΄ λ©λλ€. μ΄ νλ‘μΈμ€κ° μ’ λ£λλ©΄(λ€λ₯Έ νλ‘μΈμ€κ° μμ κ²½μ°) λ€μμ€νμ΄μ€ μ λ¦¬κ° νΈλ¦¬κ±°λ©λλ€. PID 1μ κ³ μ νλ‘μΈμ€λ₯Ό μΈκ³νλ νΉμν μν μ κ°μ§κΈ° λλ¬Έμ λλ€. κ·Έ ν Linux 컀λμ ν΄λΉ λ€μμ€νμ΄μ€μμ PID ν λΉμ λΉνμ±νν©λλ€.
- κ²°κ³Ό:
- μ λ€μμ€νμ΄μ€μμ PID 1μ μ’
λ£λ
PIDNS_HASH_ADDINGνλκ·Έμ μ λ¦¬λ‘ μ΄μ΄μ§λλ€. μ΄λ‘ μΈν΄alloc_pidν¨μκ° μ νλ‘μΈμ€ μμ± μ μλ‘μ΄ PIDλ₯Ό ν λΉνμ§ λͺ»νκ³ βCannot allocate memoryβ μ€λ₯κ° λ°μν©λλ€.
- ν΄κ²°μ± :
- μ΄ λ¬Έμ λ
unshareμ-fμ΅μ μ μ¬μ©νλ©΄ ν΄κ²°λ©λλ€. μ΄ μ΅μ μ μλ‘μ΄ PID λ€μμ€νμ΄μ€λ₯Ό λ§λ νunshareκ° μ νλ‘μΈμ€λ₯Ό fork νκ² λ§λλλ€. - %unshare -fp /bin/bash%λ₯Ό μ€ννλ©΄
unshareμμ μ΄ μ λ€μμ€νμ΄μ€μμ PID 1μ΄ λ©λλ€. κ·Έλ¬λ©΄/bin/bashμ κ·Έ μμ νλ‘μΈμ€λ€μ μ΄ μ λ€μμ€νμ΄μ€ λ΄μ μμ νκ² ν¬ν¨λμ΄ PID 1μ μ‘°κΈ° μ’ λ£λ₯Ό λ°©μ§νκ³ μ μμ μΈ PID ν λΉμ΄ κ°λ₯ν΄μ§λλ€.
unshareλ₯Ό -f νλκ·Έμ ν¨κ» μ€ννλλ‘ νλ©΄ μ PID λ€μμ€νμ΄μ€κ° μ¬λ°λ₯΄κ² μ μ§λμ΄ /bin/bashμ κ·Έ νμ νλ‘μΈμ€λ€μ΄ λ©λͺ¨λ¦¬ ν λΉ μ€λ₯ μμ΄ λμν μ μμ΅λλ€.
Docker
docker run -ti --name ubuntu1 -v /usr:/ubuntu1 ubuntu bash
νλ‘μΈμ€κ° μ΄λ€ λ€μμ€νμ΄μ€μ μλμ§ νμΈ
ls -l /proc/self/ns/uts
lrwxrwxrwx 1 root root 0 Apr 4 20:49 /proc/self/ns/uts -> 'uts:[4026531838]'
λͺ¨λ UTS namespaces μ°ΎκΈ°
sudo find /proc -maxdepth 3 -type l -name uts -exec readlink {} \; 2>/dev/null | sort -u
# Find the processes with an specific namespace
sudo find /proc -maxdepth 3 -type l -name uts -exec ls -l {} \; 2>/dev/null | grep <ns-number>
UTS namespaceμ λ€μ΄κ°κΈ°
nsenter -u TARGET_PID --pid /bin/bash
νΈμ€νΈ UTS 곡μ μ μ©
컨ν
μ΄λκ° --uts=hostλ‘ μμλλ©΄, 격리λ UTS λ€μμ€νμ΄μ€λ₯Ό μ»λ λμ νΈμ€νΈμ UTS λ€μμ€νμ΄μ€μ ν©λ₯ν©λλ€. --cap-add SYS_ADMIN κ°μ κΆνμ΄ μμ κ²½μ°, 컨ν
μ΄λ λ΄μ μ½λλ sethostname()/setdomainname()μ ν΅ν΄ νΈμ€νΈμ hostname/NIS nameμ λ³κ²½ν μ μμ΅λλ€:
docker run --rm -it --uts=host --cap-add SYS_ADMIN alpine sh -c "hostname hacked-host && exec sh"
# Hostname on the host will immediately change to "hacked-host"
νΈμ€νΈ μ΄λ¦μ λ³κ²½νλ©΄ λ‘κ·Έ/μλ¦Όμ μ‘°μν μ μκ³ , ν΄λ¬μ€ν° κ²μμ νΌλμν€κ±°λ νΈμ€νΈ μ΄λ¦μ κ³ μ (pin)ν TLS/SSH μ€μ μ κΉ¨λ¨λ¦΄ μ μμ΅λλ€.
νΈμ€νΈμ UTSλ₯Ό 곡μ νλ 컨ν μ΄λ κ°μ§
docker ps -aq | xargs -r docker inspect --format '{{.Id}} UTSMode={{.HostConfig.UTSMode}}'
# Shows "host" when the container uses the host UTS namespace
μ£μ‘νμ§λ§ μ λ λ‘컬 νμΌμ΄λ 리ν¬μ§ν 리μ μ§μ μ κ·Όν μ μμ΅λλ€. λ²μνλ €λ src/linux-hardening/privilege-escalation/docker-security/namespaces/uts-namespace.md νμΌμ λ΄μ©μ μ¬κΈ° λνμ°½μ λΆμ¬λ£μ΄ μ£Όμκ² μ΄μ? λΆμ¬λ£μ΄ μ£Όμλ©΄ μμ²νμ κ·μΉ(μ½λΒ·νκ·ΈΒ·κ²½λ‘ λ³΄μ‘΄ λ±)μ μ§μΌ μ ννκ² νκ΅μ΄λ‘ λ²μν΄ λλ¦¬κ² μ΅λλ€.


