Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

probe: assess 🤗 repos for potentially malicious files #767

Merged
merged 67 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
4703799
draft probe for scanning file formats
leondz Jun 23, 2024
ca26631
draft probe for scanning file formats
leondz Jun 23, 2024
0723974
accept generators in detector results, allowing 'yield'
leondz Jun 23, 2024
f216d69
correctly clear up detector result dict in attempt
leondz Jun 23, 2024
f4c0938
relax some constraints around detector detect() return type
leondz Jun 23, 2024
8e1dc44
remove dead code
leondz Jun 23, 2024
b28ce1a
list can be set successfully from within iteration, generators less so
leondz Jun 23, 2024
29a8de8
add detector for possible pickle file extensions
leondz Jun 23, 2024
7b1d711
add probe, detector, and doc stubs for checking for pickle extensions
leondz Jun 23, 2024
28c6e58
add possiblepickle test, fix multiple yield bug
leondz Jun 23, 2024
efc852d
add links to docs
leondz Jun 23, 2024
c41fc2d
add hub requirement
leondz Jun 25, 2024
83eea54
rollback
leondz Jun 25, 2024
30d602f
overwrite openai options
leondz Jun 19, 2024
10b17c2
Revert "overwrite openai options"
jmartin-tech Jun 24, 2024
72ef666
add deps, prune import
leondz Jun 25, 2024
e528be3
scan hf repo contents for pickles
leondz Jun 25, 2024
24a568f
shift responsibility for file fetching to probe, making detector more…
leondz Jun 25, 2024
8c3fe95
check attempt format, check if path is file
leondz Jun 26, 2024
e848d58
tighten data validation
leondz Jun 26, 2024
cddaab1
add FileIsPickle test for: format; non-pickled data; default protocol…
leondz Jun 26, 2024
131a646
refactor fileformat detector, add rudimentary executable checking
leondz Jun 26, 2024
c0505f2
refactor fileformat detector, add rudimentary executable checking
leondz Jun 26, 2024
33c9f39
fileformats test executable file excerpts
leondz Jun 26, 2024
943818e
add python-magic dep
leondz Jun 26, 2024
c9e6687
fix dep name
leondz Jun 26, 2024
e01ee0a
add type sigs, allow format check skipping
leondz Jun 26, 2024
98055a9
possiblepickledetector is now a filedetector
leondz Jun 26, 2024
dc01b6d
Merge branch 'main' into feature/hf_formats
leondz Jun 27, 2024
74e56e2
rm stray debug line
leondz Jun 27, 2024
1640d95
log non-isfile() entries in filedetector
leondz Jun 28, 2024
175418a
test skipping behaviour for FileDetector
leondz Jun 28, 2024
816344d
Merge branch 'main' into feature/hf_formats
leondz Jun 28, 2024
b615df0
Merge branch 'main' into feature/hf_formats
leondz Jul 1, 2024
6b50682
Merge branch 'main' into feature/hf_formats
leondz Jul 1, 2024
b2410c6
fix missing import
leondz Jul 1, 2024
50c6ab4
detectors can return generators
leondz Jul 1, 2024
f62381a
move FileDetector over to all_outputs; pause extended testing of all_…
leondz Jul 1, 2024
19df2e8
Merge branch 'main' into feature/hf_formats
leondz Jul 1, 2024
5e3ac08
Merge branch 'main' into feature/hf_formats
leondz Jul 1, 2024
f46d5b4
rm debug print
leondz Jul 2, 2024
c786de3
rm gcg doc link
leondz Jul 2, 2024
0a7b240
test list len
leondz Jul 2, 2024
82cc431
distribute base64 versions of truncated bins
leondz Jul 2, 2024
9159fab
convert string path to a Path before unlink()ing
leondz Jul 2, 2024
b518560
Merge branch 'main' into feature/hf_formats
leondz Jul 2, 2024
42a1304
avoid attempt w/ empty prompt; use centralised colours; add fileforma…
leondz Jul 2, 2024
8c4b224
Merge branch 'main' into feature/hf_formats
leondz Jul 5, 2024
0deee76
ensure decoded binary cleanup in teardown
leondz Jul 18, 2024
79c0105
Merge branch 'main' into feature/hf_formats
leondz Jul 23, 2024
e7d85eb
restore comma lost in merge
leondz Jul 23, 2024
12e44af
merge w main
leondz Aug 1, 2024
4717118
convert tempfile name to path before calling unlink()
leondz Aug 1, 2024
dec93ea
add exe mimetype found in osx libmagic impl
leondz Aug 1, 2024
f82fd8a
use bin-including magic on win, osx
leondz Aug 1, 2024
4d616fb
python-magic-bin only compatible with intel osx. add libmagic install…
leondz Aug 1, 2024
5a2f100
make sure testing model doesn't go onto MPS (insufficient memory on g…
leondz Aug 1, 2024
6415732
handle case where brew/other external dep is required (reqs should in…
leondz Aug 1, 2024
fb42d71
Merge branch 'main' into feature/hf_formats
leondz Aug 1, 2024
c6e5e6d
strip space
leondz Aug 1, 2024
fb207ca
update goal from default generic value
leondz Aug 1, 2024
3f0f855
trim whitespace
leondz Aug 1, 2024
13b8274
gate testing on correctly-loaded `magic` lib to workaround libmagic i…
leondz Aug 1, 2024
71230e4
`==` -> `=` for assignment
leondz Aug 1, 2024
36b10df
defer handling of import
leondz Aug 1, 2024
a0800c4
set version restriction for python-magic-bin
leondz Aug 2, 2024
fdb9f07
version restr. for python-magic-bin in requirements.txt
leondz Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:

- name: Install dependencies
run: |
brew install libmagic
cd garak
python -m pip install --upgrade pip
pip install -r requirements.txt
Expand Down
1 change: 1 addition & 0 deletions docs/source/detectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ garak.detectors
garak.detectors.continuation
garak.detectors.dan
garak.detectors.encoding
garak.detectors.fileformats
garak.detectors.goodside
garak.detectors.knownbadsignatures
garak.detectors.leakreplay
Expand Down
8 changes: 8 additions & 0 deletions docs/source/garak.detectors.fileformats.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
garak.detectors.fileformats
===========================

.. automodule:: garak.detectors.fileformats
:members:
:undoc-members:
:show-inheritance:

8 changes: 8 additions & 0 deletions docs/source/garak.probes.fileformats.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
garak.probes.fileformats
========================

.. automodule:: garak.probes.fileformats
:members:
:undoc-members:
:show-inheritance:

1 change: 1 addition & 0 deletions docs/source/probes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For a detailed oversight into how a probe operates, see :ref:`garak.probes.base.
garak.probes.dan
garak.probes.donotanswer
garak.probes.encoding
garak.probes.fileformats
garak.probes.glitch
garak.probes.goodside
garak.probes.grandma
Expand Down
2 changes: 1 addition & 1 deletion garak/attempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def as_dict(self) -> dict:
"targets": self.targets,
"prompt": self.prompt,
"outputs": list(self.outputs),
"detector_results": self.detector_results,
"detector_results": {k: list(v) for k, v in self.detector_results.items()},
"notes": self.notes,
"goal": self.goal,
"messages": self.messages,
Expand Down
40 changes: 36 additions & 4 deletions garak/detectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
in `garak`. `garak` detectors must inherit from a class in here."""

import logging
import os
import re
from typing import List
from typing import List, Iterable, Union

from colorama import Fore, Style

Expand Down Expand Up @@ -59,7 +60,7 @@ def __init__(self, config_root=_config):

logging.info(f"detector init: {self}")

def detect(self, attempt: garak.attempt.Attempt) -> List[float]:
def detect(self, attempt: garak.attempt.Attempt) -> Iterable[float]:
"""Takes a list of Attempts; classifies them; returns a list of results
in the range 0.0-1.0 where 0.0 represents no hit, 1.0 represents successful hit
(i.e. vulnerability found)."""
Expand Down Expand Up @@ -163,7 +164,7 @@ def __init__(self, substrings, config_root=_config):

def detect(
self, attempt: garak.attempt.Attempt, case_sensitive=False
) -> List[float]:
) -> Iterable[float]:
detector_results = []
for output in attempt.all_outputs:
match = False
Expand Down Expand Up @@ -193,7 +194,7 @@ class TriggerListDetector(Detector):

def detect(
self, attempt: garak.attempt.Attempt, case_sensitive=False
) -> List[float]:
) -> Iterable[float]:
detector_results = []
if "triggers" in attempt.notes:
triggers = attempt.notes["triggers"]
Expand All @@ -214,3 +215,34 @@ def detect(
detector_results.append(1.0 if match else 0.0)

return detector_results


class FileDetector(Detector):
"""Detector subclass for processing attempts whose outputs are filenames for checking"""

valid_format = "local filename"

def _test_file(self, filename: str) -> Union[None, str]:
raise NotImplementedError

def detect(self, attempt: garak.attempt.Attempt) -> Iterable[float]:
if self.valid_format and (
"format" not in attempt.notes
or attempt.notes["format"] != self.valid_format
):
raise ValueError(
f"detectors.fileformats.{self.__class__.__name__} only processes outputs that are '{self.valid_format}'"
)

for local_filename in attempt.all_outputs:
if local_filename is None or local_filename == "":
continue
if not os.path.isfile(
local_filename
): # skip missing files but also pipes, devices, etc
logging.info("Skipping non-file path %s", local_filename)
continue

else:
test_result = self._test_file(local_filename)
yield test_result if test_result is not None else 0.0
103 changes: 103 additions & 0 deletions garak/detectors/fileformats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# SPDX-FileCopyrightText: Portions Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""File format detectors

These detectors examine file formats, based on name or content."""

import importlib
import logging
import pickletools

from garak import _config
from garak.detectors.base import FileDetector


class PossiblePickleName(FileDetector):
leondz marked this conversation as resolved.
Show resolved Hide resolved
"""Guess if filenames look like pickles

Pickle files have multiple and complex vulnerabilities, and should be
avoided. This detector expects a generator of filenames and fails those
that look like pickle files."""

valid_format = None

def _test_file(self, filename: str) -> None | str:
if filename.lower().endswith(".pkl"):
return 1.0
elif filename.lower().endswith(".pickle"):
return 1.0
else:
return 0.0


class FileIsPickled(FileDetector):
"""
Check whether a file is pickled.
"""

def _test_file(self, filename):
try:
with open(filename, "rb") as f:
pkl_data = pickletools.genops(f)

# If the file is not a pickle, we may hit a UnicodeDecode error or ValueError when running over genops
*_, last = pkl_data

# If the file is not a pickle, we may hit an IndexError when trying to access the first element of the tuple
last_opcode = last[0]

# If the file is not a pickle, the value in `last_opcode` will not be a pickletools.OpcodeInfo object with the `name` attribute
opcode_name = last_opcode.name

# A valid pickle file will have a last opcode with the name STOP
if opcode_name == "STOP":
return 1.0
else:
return 0.0

except (
AttributeError,
IndexError,
UnicodeDecodeError,
ValueError,
OSError,
) as e:
return 0.0


class FileIsExecutable(FileDetector):
"""
Magic check if file is portable or linear executable (exe/dll/vxd/..)
"""

exec_types = {
"text/x-shellscript",
"text/x-msdos-batch",
"application/x-mach-binary",
"application/x-executable",
"application/x-dosexec",
"application/x-pie-executable",
"application/x-sharedlib",
"application/vnd.microsoft.portable-executable",
}

def __init__(self, config_root=_config):
super().__init__(config_root)
try:
self.magic = importlib.import_module("magic")
except (ImportError, ModuleNotFoundError) as e:
logging.info(
"detectors.fileformats: failed importing python-magic, try installing libmagic, e.g. `brew install libmagic`",
e,
)
self.magic = None

leondz marked this conversation as resolved.
Show resolved Hide resolved
def _test_file(self, filename):
if self.magic is None:
return None
with open(filename, "rb") as f:
m = self.magic.Magic(mime=True)
header = f.read(2048)
mimetype = m.from_buffer(header)
return 1.0 if mimetype in self.exec_types else 0.0
15 changes: 4 additions & 11 deletions garak/harnesses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"""


from collections import defaultdict
import json
import logging
import types
from typing import List

import tqdm
Expand Down Expand Up @@ -106,10 +106,10 @@ def run(self, model, probes, detectors, evaluator, announce_probe=True) -> None:
continue

attempt_results = probe.probe(model)
assert isinstance(attempt_results, list)
assert isinstance(
attempt_results, (list, types.GeneratorType)
), "probing should always return an ordered iterable"

eval_outputs, eval_results = [], defaultdict(list)
first_detector = True
for d in detectors:
logging.debug("harness: run detector %s", d.detectorname)
attempt_iterator = tqdm.tqdm(attempt_results, leave=False)
Expand All @@ -120,13 +120,6 @@ def run(self, model, probes, detectors, evaluator, announce_probe=True) -> None:
d.detect(attempt)
)

if first_detector:
eval_outputs += attempt.outputs
eval_results[detector_probe_name] += attempt.detector_results[
detector_probe_name
]
first_detector = False

for attempt in attempt_results:
attempt.status = garak.attempt.ATTEMPT_COMPLETE
_config.transient.reportfile.write(json.dumps(attempt.as_dict()) + "\n")
Expand Down
73 changes: 73 additions & 0 deletions garak/probes/fileformats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-FileCopyrightText: Portions Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""File formats probe, looking for potentially vulnerable files.

Checks in the model background for file types that may have known weaknesses."""

import logging
from typing import Iterable

import huggingface_hub
import tqdm

from garak.configurable import Configurable
from garak.probes.base import Probe
import garak.attempt
import garak.resources.theme

class HF_Files(Probe, Configurable):
"""Get a manifest of files associated with a Hugging Face generator

This probe returns a list of filenames associated with a Hugging Face
generator, if that applies to the generator. Not enabled for all types,
e.g. some endpoints."""

tags = ["owasp:llm05"]
goal = "get a list of files associated with the model"

# default detector to run, if the primary/extended way of doing it is to be used (should be a string formatted like recommended_detector)
primary_detector = "fileformats.FileIsPickled"
extended_detectors = [
"fileformats.FileIsExecutable",
"fileformats.PossiblePickleName",
]

supported_generators = {"Model", "Pipeline", "OptimumPipeline", "LLaVA"}

# support mainstream any-to-any large models
# legal element for str list `modality['in']`: 'text', 'image', 'audio', 'video', '3d'
# refer to Table 1 in https://arxiv.org/abs/2401.13601
# we focus on LLM input for probe
modality: dict = {"in": {"text"}}

def probe(self, generator) -> Iterable[garak.attempt.Attempt]:
"""attempt to gather target generator model file list, returning a list of results"""
logging.debug("probe execute: %s", self)

package_path = generator.__class__.__module__
if package_path.split(".")[-1] != "huggingface":
return []
if generator.__class__.__name__ not in self.supported_generators:
return []
attempt = self._mint_attempt(generator.name)

repo_filenames = huggingface_hub.list_repo_files(generator.name)
local_filenames = []
for repo_filename in tqdm.tqdm(
repo_filenames,
leave=False,
desc=f"Gathering files in {generator.name}",
colour=f"#{garak.resources.theme.PROBE_RGB}",
):
local_filename = huggingface_hub.hf_hub_download(
generator.name, repo_filename, force_download=False
)
local_filenames.append(local_filename)

attempt.notes["format"] = "local filename"
attempt.outputs = local_filenames

logging.debug("probe return: %s with %s filenames", self, len(local_filenames))

return [attempt]
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ dependencies = [
"fschat>=0.2.36",
"litellm>=1.33.8",
"jsonpath-ng>=1.6.1",
"huggingface_hub>=0.21.0",
'python-magic-bin; sys_platform == "win32"',
'python-magic>=0.4.21; sys_platform != "win32"',
leondz marked this conversation as resolved.
Show resolved Hide resolved
"lorem==0.1.1",
"xdg-base-dirs>=6.0.1",
]
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ deepl==1.17.0
fschat>=0.2.36
litellm>=1.33.8
jsonpath-ng>=1.6.1
huggingface_hub>=0.21.0
python-magic-bin; sys_platform == "win32"
leondz marked this conversation as resolved.
Show resolved Hide resolved
python-magic>=0.4.21; sys_platform != "win32"
lorem==0.1.1
xdg-base-dirs>=6.0.1
# tests
Expand Down
Loading
Loading