|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# maybe - see what a program does before deciding whether you really want it to happen |
| 4 | +# |
| 5 | +# Copyright (c) 2016 Philipp Emanuel Weidmann <pew@worldwidemann.com> |
| 6 | +# |
| 7 | +# Nemo vir est qui mundum non reddat meliorem. |
| 8 | +# |
| 9 | +# Released under the terms of the GNU General Public License, version 3 |
| 10 | +# (https://gnu.org/licenses/gpl.html) |
| 11 | + |
| 12 | + |
| 13 | +from sys import argv, exit |
| 14 | +from subprocess import call |
| 15 | + |
| 16 | +from ptrace.tools import locateProgram |
| 17 | +from ptrace.debugger import ProcessSignal, NewProcessEvent, ProcessExecution, ProcessExit |
| 18 | +from ptrace.debugger.child import createChild |
| 19 | +from ptrace.debugger.debugger import PtraceDebugger |
| 20 | +from ptrace.func_call import FunctionCallOptions |
| 21 | +from ptrace.syscall import SYSCALL_PROTOTYPES, FILENAME_ARGUMENTS |
| 22 | +from ptrace.syscall.posix_constants import SYSCALL_ARG_DICT |
| 23 | +from ptrace.syscall.syscall_argument import ARGUMENT_CALLBACK |
| 24 | + |
| 25 | +from syscall_filters import SYSCALL_FILTERS |
| 26 | +from utilities import T, SYSCALL_REGISTER, RETURN_VALUE_REGISTER |
| 27 | + |
| 28 | + |
| 29 | +# Register filtered syscalls with python-ptrace so they are parsed correctly |
| 30 | +SYSCALL_PROTOTYPES.clear() |
| 31 | +FILENAME_ARGUMENTS.clear() |
| 32 | +for syscall_filter in SYSCALL_FILTERS: |
| 33 | + SYSCALL_PROTOTYPES[syscall_filter.name] = syscall_filter.signature |
| 34 | + for argument in syscall_filter.signature[1]: |
| 35 | + if argument[0] == "const char *": |
| 36 | + FILENAME_ARGUMENTS.add(argument[1]) |
| 37 | + |
| 38 | +# Turn list into dictionary indexed by syscall name for fast filter retrieval |
| 39 | +SYSCALL_FILTERS = {syscall_filter.name: syscall_filter for syscall_filter in SYSCALL_FILTERS} |
| 40 | + |
| 41 | +# Prevent python-ptrace from decoding arguments to keep raw numerical values |
| 42 | +SYSCALL_ARG_DICT.clear() |
| 43 | +ARGUMENT_CALLBACK.clear() |
| 44 | + |
| 45 | + |
| 46 | +def prepareProcess(process): |
| 47 | + process.syscall() |
| 48 | + process.syscall_state.ignore_callback = lambda syscall: syscall.name not in SYSCALL_FILTERS |
| 49 | + |
| 50 | + |
| 51 | +def parse_argument(argument): |
| 52 | + argument = argument.createText() |
| 53 | + if argument.startswith("'"): |
| 54 | + # Remove quotes from string argument |
| 55 | + return argument[1:-1] |
| 56 | + else: |
| 57 | + # Note that "int" with base 0 infers the base from the prefix |
| 58 | + return int(argument, 0) |
| 59 | + |
| 60 | + |
| 61 | +format_options = FunctionCallOptions( |
| 62 | + replace_socketcall=False, |
| 63 | + string_max_length=4096, |
| 64 | +) |
| 65 | + |
| 66 | + |
| 67 | +def get_operations(debugger): |
| 68 | + operations = [] |
| 69 | + |
| 70 | + while True: |
| 71 | + if not debugger: |
| 72 | + # All processes have exited |
| 73 | + break |
| 74 | + |
| 75 | + # This logic is mostly based on python-ptrace's "strace" example |
| 76 | + try: |
| 77 | + syscall_event = debugger.waitSyscall() |
| 78 | + except ProcessSignal as event: |
| 79 | + event.process.syscall(event.signum) |
| 80 | + continue |
| 81 | + except NewProcessEvent as event: |
| 82 | + prepareProcess(event.process) |
| 83 | + event.process.parent.syscall() |
| 84 | + continue |
| 85 | + except ProcessExecution as event: |
| 86 | + event.process.syscall() |
| 87 | + continue |
| 88 | + except ProcessExit as event: |
| 89 | + continue |
| 90 | + |
| 91 | + process = syscall_event.process |
| 92 | + syscall_state = process.syscall_state |
| 93 | + |
| 94 | + syscall = syscall_state.event(format_options) |
| 95 | + |
| 96 | + if syscall and syscall_state.next_event == "exit": |
| 97 | + # Syscall is about to be executed (just switched from "enter" to "exit") |
| 98 | + syscall_filter = SYSCALL_FILTERS[syscall.name] |
| 99 | + |
| 100 | + arguments = [parse_argument(argument) for argument in syscall.arguments] |
| 101 | + |
| 102 | + operation = syscall_filter.format(arguments) |
| 103 | + if operation is not None: |
| 104 | + operations.append(operation) |
| 105 | + |
| 106 | + return_value = syscall_filter.substitute(arguments) |
| 107 | + if return_value is not None: |
| 108 | + # Set invalid syscall number to prevent call execution |
| 109 | + process.setreg(SYSCALL_REGISTER, -1) |
| 110 | + # Substitute return value to make syscall appear to have succeeded |
| 111 | + process.setreg(RETURN_VALUE_REGISTER, return_value) |
| 112 | + |
| 113 | + process.syscall() |
| 114 | + |
| 115 | + return operations |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + if len(argv) < 2: |
| 120 | + print(T.red("Error: No command given.")) |
| 121 | + print("Usage: %s COMMAND [ARGUMENT]..." % argv[0]) |
| 122 | + exit(1) |
| 123 | + |
| 124 | + # This is basically "shlex.join" |
| 125 | + command = " ".join([(("'%s'" % arg) if (" " in arg) else arg) for arg in argv[1:]]) |
| 126 | + |
| 127 | + arguments = argv[1:] |
| 128 | + arguments[0] = locateProgram(arguments[0]) |
| 129 | + |
| 130 | + try: |
| 131 | + pid = createChild(arguments, False) |
| 132 | + except Exception as error: |
| 133 | + print(T.red("Error executing %s: %s." % (T.bold(command) + T.red, error))) |
| 134 | + exit(1) |
| 135 | + |
| 136 | + debugger = PtraceDebugger() |
| 137 | + debugger.traceFork() |
| 138 | + debugger.traceExec() |
| 139 | + |
| 140 | + process = debugger.addProcess(pid, True) |
| 141 | + prepareProcess(process) |
| 142 | + |
| 143 | + operations = get_operations(debugger) |
| 144 | + |
| 145 | + debugger.quit() |
| 146 | + |
| 147 | + if operations: |
| 148 | + print("%s has prevented %s from performing %d file system operations:\n" % |
| 149 | + (T.bold("maybe"), T.bold(command), len(operations))) |
| 150 | + for operation in operations: |
| 151 | + print(" " + operation) |
| 152 | + try: |
| 153 | + choice = raw_input("\nDo you want to rerun %s and permit these operations? [y/N] " % T.bold(command)) |
| 154 | + except KeyboardInterrupt: |
| 155 | + choice = "" |
| 156 | + if choice.lower() == "y": |
| 157 | + call(argv[1:]) |
| 158 | + else: |
| 159 | + print("%s has not detected any file system operations from %s." % |
| 160 | + (T.bold("maybe"), T.bold(command))) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments