| 1 |
import os |
| 2 |
import pickle |
| 3 |
import pytest |
| 4 |
from deepdiff import Delta |
| 5 |
from deepdiff.helper import Opcode |
| 6 |
from deepdiff.serialization import ForbiddenModule |
| 7 |
|
| 8 |
|
| 9 |
class TestDeltaClassPollution: |
| 10 |
|
| 11 |
def test_builtins_int(self): |
| 12 |
|
| 13 |
pollute_int = pickle.dumps( |
| 14 |
{ |
| 15 |
"values_changed": {"root['tmp']": {"new_value": Opcode("", 0, 0, 0, 0)}}, |
| 16 |
"dictionary_item_added": { |
| 17 |
( |
| 18 |
("root", "GETATTR"), |
| 19 |
("tmp", "GET"), |
| 20 |
("__repr__", "GETATTR"), |
| 21 |
("__globals__", "GETATTR"), |
| 22 |
("__builtins__", "GET"), |
| 23 |
("int", "GET"), |
| 24 |
): "no longer a class" |
| 25 |
}, |
| 26 |
} |
| 27 |
) |
| 28 |
|
| 29 |
assert isinstance(pollute_int, bytes) |
| 30 |
|
| 31 |
# ------------[ Exploit ]------------ |
| 32 |
# This could be some example, vulnerable, application. |
| 33 |
# The inputs above could be sent via HTTP, for example. |
| 34 |
|
| 35 |
|
| 36 |
# Existing dictionary; it is assumed that it contains |
| 37 |
# at least one entry, otherwise a different Delta needs to be |
| 38 |
# applied first, adding an entry to the dictionary. |
| 39 |
mydict = {"tmp": "foobar"} |
| 40 |
|
| 41 |
# Before pollution |
| 42 |
assert 42 == int("41") + 1 |
| 43 |
|
| 44 |
# Apply Delta to mydict |
| 45 |
result = mydict + Delta(pollute_int) |
| 46 |
|
| 47 |
assert 1337 == int("1337") |
| 48 |
|
| 49 |
def test_remote_code_execution(self): |
| 50 |
if os.path.exists('/tmp/pwned'): |
| 51 |
os.remove('/tmp/pwned') |
| 52 |
|
| 53 |
pollute_safe_to_import = pickle.dumps( |
| 54 |
{ |
| 55 |
"values_changed": {"root['tmp']": {"new_value": Opcode("", 0, 0, 0, 0)}}, |
| 56 |
"set_item_added": { |
| 57 |
( |
| 58 |
("root", "GETATTR"), |
| 59 |
("tmp", "GET"), |
| 60 |
("__repr__", "GETATTR"), |
| 61 |
("__globals__", "GETATTR"), |
| 62 |
("sys", "GET"), |
| 63 |
("modules", "GETATTR"), |
| 64 |
("deepdiff.serialization", "GET"), |
| 65 |
("SAFE_TO_IMPORT", "GETATTR"), |
| 66 |
): set(["posix.system"]) |
| 67 |
}, |
| 68 |
} |
| 69 |
) |
| 70 |
|
| 71 |
# From https://davidhamann.de/2020/04/05/exploiting-python-pickle/ |
| 72 |
class RCE: |
| 73 |
def __reduce__(self): |
| 74 |
cmd = "id > /tmp/pwned" |
| 75 |
return os.system, (cmd,) |
| 76 |
|
| 77 |
# Wrap object with dictionary so that Delta does not crash |
| 78 |
rce_pickle = pickle.dumps({"_": RCE()}) |
| 79 |
|
| 80 |
assert isinstance(pollute_safe_to_import, bytes) |
| 81 |
assert isinstance(rce_pickle, bytes) |
| 82 |
|
| 83 |
# ------------[ Exploit ]------------ |
| 84 |
# This could be some example, vulnerable, application. |
| 85 |
# The inputs above could be sent via HTTP, for example. |
| 86 |
|
| 87 |
# Existing dictionary; it is assumed that it contains |
| 88 |
# at least one entry, otherwise a different Delta needs to be |
| 89 |
# applied first, adding an entry to the dictionary. |
| 90 |
mydict = {"tmp": "foobar"} |
| 91 |
|
| 92 |
# Apply Delta to mydict |
| 93 |
with pytest.raises(ValueError) as exc_info: |
| 94 |
mydict + Delta(pollute_safe_to_import) |
| 95 |
assert "traversing dunder attributes is not allowed" == str(exc_info.value) |
| 96 |
|
| 97 |
with pytest.raises(ForbiddenModule) as exc_info: |
| 98 |
Delta(rce_pickle) # no need to apply this Delta |
| 99 |
assert "Module 'posix.system' is forbidden. You need to explicitly pass it by passing a safe_to_import parameter" == str(exc_info.value) |
| 100 |
|
| 101 |
assert not os.path.exists('/tmp/pwned'), "We should not have created this file" |
| 102 |
|
| 103 |
def test_delta_should_not_access_globals(self): |
| 104 |
|
| 105 |
pollute_global = pickle.dumps( |
| 106 |
{ |
| 107 |
"dictionary_item_added": { |
| 108 |
( |
| 109 |
("root", "GETATTR"), |
| 110 |
("myfunc", "GETATTR"), |
| 111 |
("__globals__", "GETATTR"), |
| 112 |
("PWNED", "GET"), |
| 113 |
): 1337 |
| 114 |
} |
| 115 |
} |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
# demo application |
| 120 |
class Foo: |
| 121 |
def __init__(self): |
| 122 |
pass |
| 123 |
|
| 124 |
def myfunc(self): |
| 125 |
pass |
| 126 |
|
| 127 |
|
| 128 |
PWNED = False |
| 129 |
delta = Delta(pollute_global) |
| 130 |
assert PWNED is False |
| 131 |
b = Foo() + delta |
| 132 |
|
| 133 |
assert PWNED is False |