Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

python - Untrusted pickle source

From the python docs for pickle :

Warning The pickle module is not secure. Only unpickle data you trust.

What would be an example of pickling and then un-pickling data that would do something malicious? I've only ever used pickle to save objects that don't necessarily json encode well (date, decimal, etc.) so I don't have much experience with it or what it's capable outside of being a "simpler json" encoder.

What would be an example of something malicious that could be done with it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like Chase said, functions can be executed by unpickling. According to this source: https://intoli.com/blog/dangerous-pickles/, pickled objects are actually stored as bytecode instructions which are interpreted on opening (similar to Python itself). By writing pickle bytecode, it's possible to make a pickle "program" to do anything.

Here I made a pickle program to run the bash command say "malicious code", but you could run commands like rm -rf / as well.

I saved the following bytecode to a file:

c__builtin__
exec
(Vimport os; os.system('say "malicious code"')
tR.

and then unpickled it with:

import pickle
loadfile = open('malicious', 'rb')
data = pickle.load(loadfile)

I immediately heard some "malicious code".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...