From 4813ba3663725ae9e194ceee22cc382b56d95626 Mon Sep 17 00:00:00 2001 From: zmackie Date: Thu, 8 Feb 2024 10:15:03 -0500 Subject: [PATCH] Adds new encodings (#459) * Create Nato probe and do formatting Signed-off-by: Zander Mackie * Zalgo probe Signed-off-by: Zander Mackie * Formatting and fix interfaces Signed-off-by: Zander Mackie * amend ecoji writer to be StringIO; put funcs into classes, let's see how it plays out --------- Signed-off-by: Zander Mackie Co-authored-by: Leon Derczynski --- garak/probes/encoding.py | 107 ++++++++++++++++++++++++++++++++++----- requirements.txt | 5 +- 2 files changed, 99 insertions(+), 13 deletions(-) diff --git a/garak/probes/encoding.py b/garak/probes/encoding.py index dabf1c12..9d98ba98 100644 --- a/garak/probes/encoding.py +++ b/garak/probes/encoding.py @@ -168,7 +168,7 @@ def braille(text): elif char in characterUnicodes: if char.isdigit(): if not is_number: - is_number == True + is_number = True output += characterUnicodes["num"] output += characterUnicodes[char] if is_number and char not in numberPunctuations: @@ -321,17 +321,6 @@ def __init__(self): Probe.__init__(self) -class InjectBase32(BaseEncodingProbe, Probe): - """Probe for susceptibility to base32 encoded injections""" - - encoding_funcs = [base64.b32encode] - encoding_name = "BASE32" - - def __init__(self): - BaseEncodingProbe.__init__(self) - Probe.__init__(self) - - class InjectHex(BaseEncodingProbe, Probe): """Probe for susceptibility to hex encoded injections""" @@ -433,3 +422,97 @@ class InjectMorse(BaseEncodingProbe, Probe): def __init__(self): BaseEncodingProbe.__init__(self) Probe.__init__(self) + + +class InjectNato(BaseEncodingProbe, Probe): + """Probe for susceptibility to Nato phonetic alphabet encoding.""" + + uri = "https://en.wikipedia.org/wiki/NATO_phonetic_alphabet" + + def nato(text: bytes) -> bytes: + nato_map = { + "A": "Alfa", + "B": "Bravo", + "C": "Charlie", + "D": "Delta", + "E": "Echo", + "F": "Foxtrot", + "G": "Golf", + "H": "Hotel", + "I": "India", + "J": "Juliett", + "K": "Kilo", + "L": "Lima", + "M": "Mike", + "N": "November", + "O": "Oscar", + "P": "Papa", + "Q": "Quebec", + "R": "Romeo", + "S": "Sierra", + "T": "Tango", + "U": "Uniform", + "V": "Victor", + "W": "Whiskey", + "X": "Xray", + "Y": "Yankee", + "Z": "Zulu", + } + + output = [] + for char in text.decode().upper(): + if char in nato_map: + output.append(nato_map[char]) + return bytes(" ".join(output), "utf-8") + + encoding_funcs = [nato] + encoding_name = "Nato" + + def __init__(self): + BaseEncodingProbe.__init__(self) + Probe.__init__(self) + + +class InjectEcoji(BaseEncodingProbe, Probe): + """Probe for susceptibility to Ecoji encoding.""" + + uri = "https://ecoji.io/" + + def _ecoji(text: bytes) -> bytes: + import io + + import ecoji + + r = io.BytesIO(text) + w = io.StringIO() + + ecoji.encode(r, w) + + return w.getvalue() + + encoding_name = "Ecoji" + encoding_funcs = [_ecoji] + + def __init__(self): + BaseEncodingProbe.__init__(self) + Probe.__init__(self) + + +class InjectZalgo(BaseEncodingProbe, Probe): + """Probe for susceptibility to Zalgo encoding.""" + + uri = "https://en.wikipedia.org/wiki/Zalgo_text" + + def zalgo(text: bytes) -> bytes: + from zalgolib.zalgolib import enzalgofy + + zalged = enzalgofy(text=text.decode(), intensity=50) + + return bytes(zalged, "utf-8") + + encoding_name = "Zalgo" + encoding_funcs = [zalgo] + + def __init__(self): + BaseEncodingProbe.__init__(self) + Probe.__init__(self) diff --git a/requirements.txt b/requirements.txt index acf10615..7e1d5993 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,4 +22,7 @@ octoai-sdk cmd2 torch>=2.1.0 sentencepiece>=0.1.99 -markdown \ No newline at end of file +markdown +zalgolib>=0.2.2 +basest>=0.7.3 +ecoji>=0.1.0 \ No newline at end of file