Skip to content

Commit

Permalink
Added scan for INST opcode (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrinkiani authored Oct 25, 2023
1 parent 14f5268 commit cb98765
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modelscan/tools/picklescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _list_globals(
if op_name == "MEMOIZE" and n > 0:
memo[len(memo)] = ops[n - 1][1]

if op_name == "GLOBAL":
if op_name in ["GLOBAL", "INST"]:
globals.add(tuple(op_value.split(" ", 1)))
elif op_name == "STACK_GLOBAL":
values: List[str] = []
Expand Down
25 changes: 25 additions & 0 deletions tests/test_modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ def file_path(tmp_path_factory: Any) -> Any:
pickle.dumps(Malicious1(), protocol=4),
)

malicious10_pickle_bytes = (
b"(S'print(\"Injection running\")'\ni__builtin__\nexec\n."
)
initialize_data_file(f"{tmp}/data/malicious10.pkl", malicious10_pickle_bytes)

return tmp


Expand Down Expand Up @@ -577,6 +582,19 @@ def test_scan_pickle_operators(file_path: Any) -> None:
malicious9.scan_path(Path(f"{file_path}/data/malicious9.pkl"))
assert malicious9.issues.all_issues == expected_malicious9

expected_malicious10 = [
Issue(
IssueCode.UNSAFE_OPERATOR,
IssueSeverity.CRITICAL,
OperatorIssueDetails(
"__builtin__", "exec", f"{file_path}/data/malicious10.pkl"
),
)
]
malicious10 = Modelscan()
malicious10.scan_path(Path(f"{file_path}/data/malicious10.pkl"))
assert malicious10.issues.all_issues == expected_malicious10


def test_scan_directory_path(file_path: str) -> None:
expected = {
Expand Down Expand Up @@ -728,6 +746,13 @@ def test_scan_directory_path(file_path: str) -> None:
"posix", "system", f"{file_path}/data/malicious2_v0.pkl"
),
),
Issue(
IssueCode.UNSAFE_OPERATOR,
IssueSeverity.CRITICAL,
OperatorIssueDetails(
"__builtin__", "exec", f"{file_path}/data/malicious10.pkl"
),
),
}
ms = Modelscan()
p = Path(f"{file_path}/data/")
Expand Down

0 comments on commit cb98765

Please sign in to comment.