Last active
June 21, 2024 23:05
-
-
Save ormaaj/6195070 to your computer and use it in GitHub Desktop.
Shell pattern quote/escape fuzzer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess, itertools | |
class Shell(list): | |
def __init__(self, shell, cmds): | |
self.shell = shell | |
super().__init__([(x, self.__run(x)) for x in cmds]) | |
def __iter__(self): | |
while True: | |
try: | |
self[0][1].communicate() | |
yield (lambda x: (x[0], x[1].returncode, self.shell))(self.pop(0)) | |
except IndexError: | |
raise StopIteration() | |
def __run(self, cmd): | |
return subprocess.Popen([self.shell, "-c", cmd]) | |
def main(): | |
template = r'x=\\; case \\x in {0}{1}x) :;; *) false; esac' | |
tests = [template.format(*x) for x in itertools.product(['"${x}"', '${x}', r'"\\"', r'\\'], repeat=2)] | |
shells = [Shell(x, tests) for x in ["bash", "dash", "ksh", "mksh", "zsh", "bb", "posh", "jsh"]] | |
print(" " * 54, " ".join(x.shell for x in shells)) | |
for row in zip(*shells): | |
print("{0:55}{1}".format(row[0][0], "".join(str(test) + (" " * len(shell)) for x, test, shell in row))) | |
if __name__ == "__main__": | |
main() | |
# bash dash ksh mksh zsh bb posh jsh | |
# x=\\; case \\x in "${x}""${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in "${x}"${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1 | |
# x=\\; case \\x in "${x}""\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in "${x}"\\x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in ${x}"${x}"x) :;; *) false; esac 1 0 1 1 1 1 1 1 | |
# x=\\; case \\x in ${x}${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1 | |
# x=\\; case \\x in ${x}"\\"x) :;; *) false; esac 1 0 1 1 1 1 1 1 | |
# x=\\; case \\x in ${x}\\x) :;; *) false; esac 1 0 1 1 1 1 1 1 | |
# x=\\; case \\x in "\\""${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in "\\"${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1 | |
# x=\\; case \\x in "\\""\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in "\\"\\x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in \\"${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in \\${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1 | |
# x=\\; case \\x in \\"\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# x=\\; case \\x in \\\\x) :;; *) false; esac 1 1 1 1 1 1 1 1 | |
# vim: set fenc=utf-8 ff=unix ft=python ts=4 sts=4 sw=4 nowrap et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment